vusb.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <avr/wdt.h>
  16. #include <usbdrv/usbdrv.h>
  17. #include "usbconfig.h"
  18. #include "host.h"
  19. #include "report.h"
  20. #include "host_driver.h"
  21. #include "vusb.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "wait.h"
  25. #include "usb_descriptor_common.h"
  26. #ifdef RAW_ENABLE
  27. # include "raw_hid.h"
  28. #endif
  29. #if defined(CONSOLE_ENABLE)
  30. # define RBUF_SIZE 128
  31. # include "ring_buffer.h"
  32. #endif
  33. #define NEXT_INTERFACE __COUNTER__
  34. /*
  35. * Interface indexes
  36. */
  37. enum usb_interfaces {
  38. #ifndef KEYBOARD_SHARED_EP
  39. KEYBOARD_INTERFACE = NEXT_INTERFACE,
  40. #else
  41. SHARED_INTERFACE = NEXT_INTERFACE,
  42. # define KEYBOARD_INTERFACE SHARED_INTERFACE
  43. #endif
  44. // It is important that the Raw HID interface is at a constant
  45. // interface number, to support Linux/OSX platforms and chrome.hid
  46. // If Raw HID is enabled, let it be always 1.
  47. #ifdef RAW_ENABLE
  48. RAW_INTERFACE = NEXT_INTERFACE,
  49. #endif
  50. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  51. SHARED_INTERFACE = NEXT_INTERFACE,
  52. #endif
  53. #ifdef CONSOLE_ENABLE
  54. CONSOLE_INTERFACE = NEXT_INTERFACE,
  55. #endif
  56. TOTAL_INTERFACES = NEXT_INTERFACE
  57. };
  58. #define MAX_INTERFACES 3
  59. #if (NEXT_INTERFACE - 1) > MAX_INTERFACES
  60. # error There are not enough available interfaces to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Raw HID, Console
  61. #endif
  62. #if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)) && CONSOLE_ENABLE
  63. # error Mouse/Extra Keys share an endpoint with Console. Please disable one of the two.
  64. #endif
  65. static uint8_t keyboard_led_state = 0;
  66. static uint8_t vusb_idle_rate = 0;
  67. /* Keyboard report send buffer */
  68. #define KBUF_SIZE 16
  69. static report_keyboard_t kbuf[KBUF_SIZE];
  70. static uint8_t kbuf_head = 0;
  71. static uint8_t kbuf_tail = 0;
  72. static report_keyboard_t keyboard_report_sent;
  73. #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
  74. /* transfer keyboard report from buffer */
  75. void vusb_transfer_keyboard(void) {
  76. for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
  77. if (usbInterruptIsReady()) {
  78. if (kbuf_head != kbuf_tail) {
  79. #ifndef KEYBOARD_SHARED_EP
  80. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  81. #else
  82. // Ugly hack! :(
  83. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t) - 1);
  84. while (!usbInterruptIsReady()) {
  85. usbPoll();
  86. }
  87. usbSetInterrupt((void *)(&(kbuf[kbuf_tail].keys[5])), 1);
  88. #endif
  89. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  90. if (debug_keyboard) {
  91. dprintf("V-USB: kbuf[%d->%d](%02X)\n", kbuf_tail, kbuf_head, (kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
  92. }
  93. }
  94. break;
  95. }
  96. usbPoll();
  97. wait_ms(1);
  98. }
  99. }
  100. /*------------------------------------------------------------------*
  101. * RAW HID
  102. *------------------------------------------------------------------*/
  103. #ifdef RAW_ENABLE
  104. # define RAW_BUFFER_SIZE 32
  105. # define RAW_EPSIZE 8
  106. static uint8_t raw_output_buffer[RAW_BUFFER_SIZE];
  107. static uint8_t raw_output_received_bytes = 0;
  108. void raw_hid_send(uint8_t *data, uint8_t length) {
  109. if (length != RAW_BUFFER_SIZE) {
  110. return;
  111. }
  112. uint8_t *temp = data;
  113. for (uint8_t i = 0; i < 4; i++) {
  114. while (!usbInterruptIsReady4()) {
  115. usbPoll();
  116. }
  117. usbSetInterrupt4(temp, 8);
  118. temp += 8;
  119. }
  120. while (!usbInterruptIsReady4()) {
  121. usbPoll();
  122. }
  123. usbSetInterrupt4(0, 0);
  124. }
  125. __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
  126. // Users should #include "raw_hid.h" in their own code
  127. // and implement this function there. Leave this as weak linkage
  128. // so users can opt to not handle data coming in.
  129. }
  130. void raw_hid_task(void) {
  131. if (raw_output_received_bytes == RAW_BUFFER_SIZE) {
  132. raw_hid_receive(raw_output_buffer, RAW_BUFFER_SIZE);
  133. raw_output_received_bytes = 0;
  134. }
  135. }
  136. #endif
  137. /*------------------------------------------------------------------*
  138. * Console
  139. *------------------------------------------------------------------*/
  140. #ifdef CONSOLE_ENABLE
  141. # define CONSOLE_BUFFER_SIZE 32
  142. # define CONSOLE_EPSIZE 8
  143. int8_t sendchar(uint8_t c) {
  144. rbuf_enqueue(c);
  145. return 0;
  146. }
  147. static inline bool usbSendData3(char *data, uint8_t len) {
  148. uint8_t retries = 5;
  149. while (!usbInterruptIsReady3()) {
  150. if (!(retries--)) {
  151. return false;
  152. }
  153. usbPoll();
  154. }
  155. usbSetInterrupt3((unsigned char *)data, len);
  156. return true;
  157. }
  158. void console_task(void) {
  159. if (!usbConfiguration) {
  160. return;
  161. }
  162. if (!rbuf_has_data()) {
  163. return;
  164. }
  165. // Send in chunks of 8 padded to 32
  166. char send_buf[CONSOLE_BUFFER_SIZE] = {0};
  167. uint8_t send_buf_count = 0;
  168. while (rbuf_has_data() && send_buf_count < CONSOLE_EPSIZE) {
  169. send_buf[send_buf_count++] = rbuf_dequeue();
  170. }
  171. char *temp = send_buf;
  172. for (uint8_t i = 0; i < 4; i++) {
  173. if (!usbSendData3(temp, 8)) {
  174. break;
  175. }
  176. temp += 8;
  177. }
  178. usbSendData3(0, 0);
  179. usbPoll();
  180. }
  181. #endif
  182. /*------------------------------------------------------------------*
  183. * Host driver
  184. *------------------------------------------------------------------*/
  185. static uint8_t keyboard_leds(void);
  186. static void send_keyboard(report_keyboard_t *report);
  187. static void send_mouse(report_mouse_t *report);
  188. static void send_system(uint16_t data);
  189. static void send_consumer(uint16_t data);
  190. static void send_programmable_button(uint32_t data);
  191. static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer, send_programmable_button};
  192. host_driver_t *vusb_driver(void) { return &driver; }
  193. static uint8_t keyboard_leds(void) { return keyboard_led_state; }
  194. static void send_keyboard(report_keyboard_t *report) {
  195. uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
  196. if (next != kbuf_tail) {
  197. kbuf[kbuf_head] = *report;
  198. kbuf_head = next;
  199. } else {
  200. dprint("kbuf: full\n");
  201. }
  202. // NOTE: send key strokes of Macro
  203. usbPoll();
  204. vusb_transfer_keyboard();
  205. keyboard_report_sent = *report;
  206. }
  207. #ifndef KEYBOARD_SHARED_EP
  208. # define usbInterruptIsReadyShared usbInterruptIsReady3
  209. # define usbSetInterruptShared usbSetInterrupt3
  210. #else
  211. # define usbInterruptIsReadyShared usbInterruptIsReady
  212. # define usbSetInterruptShared usbSetInterrupt
  213. #endif
  214. static void send_mouse(report_mouse_t *report) {
  215. #ifdef MOUSE_ENABLE
  216. if (usbInterruptIsReadyShared()) {
  217. usbSetInterruptShared((void *)report, sizeof(report_mouse_t));
  218. }
  219. #endif
  220. }
  221. #ifdef EXTRAKEY_ENABLE
  222. static void send_extra(uint8_t report_id, uint16_t data) {
  223. static uint8_t last_id = 0;
  224. static uint16_t last_data = 0;
  225. if ((report_id == last_id) && (data == last_data)) return;
  226. last_id = report_id;
  227. last_data = data;
  228. static report_extra_t report;
  229. report = (report_extra_t){.report_id = report_id, .usage = data};
  230. if (usbInterruptIsReadyShared()) {
  231. usbSetInterruptShared((void *)&report, sizeof(report_extra_t));
  232. }
  233. }
  234. #endif
  235. static void send_system(uint16_t data) {
  236. #ifdef EXTRAKEY_ENABLE
  237. send_extra(REPORT_ID_SYSTEM, data);
  238. #endif
  239. }
  240. static void send_consumer(uint16_t data) {
  241. #ifdef EXTRAKEY_ENABLE
  242. send_extra(REPORT_ID_CONSUMER, data);
  243. #endif
  244. }
  245. void send_digitizer(report_digitizer_t *report) {
  246. #ifdef DIGITIZER_ENABLE
  247. if (usbInterruptIsReadyShared()) {
  248. usbSetInterruptShared((void *)report, sizeof(report_digitizer_t));
  249. #endif
  250. }
  251. static void send_programmable_button(uint32_t data) {
  252. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  253. static report_programmable_button_t report = {
  254. .report_id = REPORT_ID_PROGRAMMABLE_BUTTON,
  255. };
  256. report.usage = data;
  257. if (usbInterruptIsReadyShared()) {
  258. usbSetInterruptShared((void *)&report, sizeof(report));
  259. }
  260. #endif
  261. }
  262. /*------------------------------------------------------------------*
  263. * Request from host *
  264. *------------------------------------------------------------------*/
  265. static struct {
  266. uint16_t len;
  267. enum { NONE, SET_LED } kind;
  268. } last_req;
  269. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  270. usbRequest_t *rq = (void *)data;
  271. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  272. if (rq->bRequest == USBRQ_HID_GET_REPORT) {
  273. dprint("GET_REPORT:");
  274. if (rq->wIndex.word == KEYBOARD_INTERFACE) {
  275. usbMsgPtr = (usbMsgPtr_t)&keyboard_report_sent;
  276. return sizeof(keyboard_report_sent);
  277. }
  278. } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
  279. dprint("GET_IDLE:");
  280. usbMsgPtr = (usbMsgPtr_t)&vusb_idle_rate;
  281. return 1;
  282. } else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
  283. vusb_idle_rate = rq->wValue.bytes[1];
  284. dprintf("SET_IDLE: %02X", vusb_idle_rate);
  285. } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
  286. dprint("SET_REPORT:");
  287. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  288. if (rq->wValue.word == 0x0200 && rq->wIndex.word == KEYBOARD_INTERFACE) {
  289. dprint("SET_LED:");
  290. last_req.kind = SET_LED;
  291. last_req.len = rq->wLength.word;
  292. }
  293. return USB_NO_MSG; // to get data in usbFunctionWrite
  294. } else {
  295. dprint("UNKNOWN:");
  296. }
  297. } else {
  298. dprint("VENDOR:");
  299. /* no vendor specific requests implemented */
  300. }
  301. dprint("\n");
  302. return 0; /* default for not implemented requests: return no data back to host */
  303. }
  304. uchar usbFunctionWrite(uchar *data, uchar len) {
  305. if (last_req.len == 0) {
  306. return -1;
  307. }
  308. switch (last_req.kind) {
  309. case SET_LED:
  310. dprintf("SET_LED: %02X\n", data[0]);
  311. keyboard_led_state = data[0];
  312. last_req.len = 0;
  313. return 1;
  314. break;
  315. case NONE:
  316. default:
  317. return -1;
  318. break;
  319. }
  320. return 1;
  321. }
  322. void usbFunctionWriteOut(uchar *data, uchar len) {
  323. #ifdef RAW_ENABLE
  324. // Data from host must be divided every 8bytes
  325. if (len != 8) {
  326. dprint("RAW: invalid length\n");
  327. raw_output_received_bytes = 0;
  328. return;
  329. }
  330. if (raw_output_received_bytes + len > RAW_BUFFER_SIZE) {
  331. dprint("RAW: buffer full\n");
  332. raw_output_received_bytes = 0;
  333. } else {
  334. for (uint8_t i = 0; i < 8; i++) {
  335. raw_output_buffer[raw_output_received_bytes + i] = data[i];
  336. }
  337. raw_output_received_bytes += len;
  338. }
  339. #endif
  340. }
  341. /*------------------------------------------------------------------*
  342. * Descriptors *
  343. *------------------------------------------------------------------*/
  344. #ifdef KEYBOARD_SHARED_EP
  345. const PROGMEM uchar shared_hid_report[] = {
  346. # define SHARED_REPORT_STARTED
  347. #else
  348. const PROGMEM uchar keyboard_hid_report[] = {
  349. #endif
  350. 0x05, 0x01, // Usage Page (Generic Desktop)
  351. 0x09, 0x06, // Usage (Keyboard)
  352. 0xA1, 0x01, // Collection (Application)
  353. #ifdef KEYBOARD_SHARED_EP
  354. 0x85, REPORT_ID_KEYBOARD, // Report ID
  355. #endif
  356. // Modifiers (8 bits)
  357. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  358. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  359. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  360. 0x15, 0x00, // Logical Minimum (0)
  361. 0x25, 0x01, // Logical Maximum (1)
  362. 0x95, 0x08, // Report Count (8)
  363. 0x75, 0x01, // Report Size (1)
  364. 0x81, 0x02, // Input (Data, Variable, Absolute)
  365. // Reserved (1 byte)
  366. 0x95, 0x01, // Report Count (1)
  367. 0x75, 0x08, // Report Size (8)
  368. 0x81, 0x03, // Input (Constant)
  369. // Keycodes (6 bytes)
  370. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  371. 0x19, 0x00, // Usage Minimum (0)
  372. 0x29, 0xFF, // Usage Maximum (255)
  373. 0x15, 0x00, // Logical Minimum (0)
  374. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  375. 0x95, 0x06, // Report Count (6)
  376. 0x75, 0x08, // Report Size (8)
  377. 0x81, 0x00, // Input (Data, Array, Absolute)
  378. // Status LEDs (5 bits)
  379. 0x05, 0x08, // Usage Page (LED)
  380. 0x19, 0x01, // Usage Minimum (Num Lock)
  381. 0x29, 0x05, // Usage Maximum (Kana)
  382. 0x95, 0x05, // Report Count (5)
  383. 0x75, 0x01, // Report Size (1)
  384. 0x91, 0x02, // Output (Data, Variable, Absolute)
  385. // LED padding (3 bits)
  386. 0x95, 0x01, // Report Count (1)
  387. 0x75, 0x03, // Report Size (3)
  388. 0x91, 0x03, // Output (Constant)
  389. 0xC0, // End Collection
  390. #ifndef KEYBOARD_SHARED_EP
  391. };
  392. #endif
  393. #if defined(SHARED_EP_ENABLE) && !defined(SHARED_REPORT_STARTED)
  394. const PROGMEM uchar shared_hid_report[] = {
  395. # define SHARED_REPORT_STARTED
  396. #endif
  397. #ifdef MOUSE_ENABLE
  398. // Mouse report descriptor
  399. 0x05, 0x01, // Usage Page (Generic Desktop)
  400. 0x09, 0x02, // Usage (Mouse)
  401. 0xA1, 0x01, // Collection (Application)
  402. 0x85, REPORT_ID_MOUSE, // Report ID
  403. 0x09, 0x01, // Usage (Pointer)
  404. 0xA1, 0x00, // Collection (Physical)
  405. // Buttons (8 bits)
  406. 0x05, 0x09, // Usage Page (Button)
  407. 0x19, 0x01, // Usage Minimum (Button 1)
  408. 0x29, 0x08, // Usage Maximum (Button 8)
  409. 0x15, 0x00, // Logical Minimum (0)
  410. 0x25, 0x01, // Logical Maximum (1)
  411. 0x95, 0x08, // Report Count (8)
  412. 0x75, 0x01, // Report Size (1)
  413. 0x81, 0x02, // Input (Data, Variable, Absolute)
  414. // X/Y position (2 bytes)
  415. 0x05, 0x01, // Usage Page (Generic Desktop)
  416. 0x09, 0x30, // Usage (X)
  417. 0x09, 0x31, // Usage (Y)
  418. 0x15, 0x81, // Logical Minimum (-127)
  419. 0x25, 0x7F, // Logical Maximum (127)
  420. 0x95, 0x02, // Report Count (2)
  421. 0x75, 0x08, // Report Size (8)
  422. 0x81, 0x06, // Input (Data, Variable, Relative)
  423. // Vertical wheel (1 byte)
  424. 0x09, 0x38, // Usage (Wheel)
  425. 0x15, 0x81, // Logical Minimum (-127)
  426. 0x25, 0x7F, // Logical Maximum (127)
  427. 0x95, 0x01, // Report Count (1)
  428. 0x75, 0x08, // Report Size (8)
  429. 0x81, 0x06, // Input (Data, Variable, Relative)
  430. // Horizontal wheel (1 byte)
  431. 0x05, 0x0C, // Usage Page (Consumer)
  432. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  433. 0x15, 0x81, // Logical Minimum (-127)
  434. 0x25, 0x7F, // Logical Maximum (127)
  435. 0x95, 0x01, // Report Count (1)
  436. 0x75, 0x08, // Report Size (8)
  437. 0x81, 0x06, // Input (Data, Variable, Relative)
  438. 0xC0, // End Collection
  439. 0xC0, // End Collection
  440. #endif
  441. #ifdef EXTRAKEY_ENABLE
  442. // Extrakeys report descriptor
  443. 0x05, 0x01, // Usage Page (Generic Desktop)
  444. 0x09, 0x80, // Usage (System Control)
  445. 0xA1, 0x01, // Collection (Application)
  446. 0x85, REPORT_ID_SYSTEM, // Report ID
  447. 0x19, 0x01, // Usage Minimum (Pointer)
  448. 0x2A, 0xB7, 0x00, // Usage Maximum (System Display LCD Autoscale)
  449. 0x15, 0x01, // Logical Minimum
  450. 0x26, 0xB7, 0x00, // Logical Maximum
  451. 0x95, 0x01, // Report Count (1)
  452. 0x75, 0x10, // Report Size (16)
  453. 0x81, 0x00, // Input (Data, Array, Absolute)
  454. 0xC0, // End Collection
  455. 0x05, 0x0C, // Usage Page (Consumer)
  456. 0x09, 0x01, // Usage (Consumer Control)
  457. 0xA1, 0x01, // Collection (Application)
  458. 0x85, REPORT_ID_CONSUMER, // Report ID
  459. 0x19, 0x01, // Usage Minimum (Consumer Control)
  460. 0x2A, 0xA0, 0x02, // Usage Maximum (AC Desktop Show All Applications)
  461. 0x15, 0x01, // Logical Minimum
  462. 0x26, 0xA0, 0x02, // Logical Maximum
  463. 0x95, 0x01, // Report Count (1)
  464. 0x75, 0x10, // Report Size (16)
  465. 0x81, 0x00, // Input (Data, Array, Absolute)
  466. 0xC0, // End Collection
  467. #endif
  468. #ifdef DIGITIZER_ENABLE
  469. // Digitizer report descriptor
  470. 0x05, 0x0D, // Usage Page (Digitizers)
  471. 0x09, 0x01, // Usage (Digitizer)
  472. 0xA1, 0x01, // Collection (Application)
  473. 0x85, REPORT_ID_DIGITIZER, // Report ID
  474. 0x09, 0x22, // Usage (Finger)
  475. 0xA1, 0x00, // Collection (Physical)
  476. // Tip Switch (1 bit)
  477. 0x09, 0x42, // Usage (Tip Switch)
  478. 0x15, 0x00, // Logical Minimum
  479. 0x25, 0x01, // Logical Maximum
  480. 0x95, 0x01, // Report Count (1)
  481. 0x75, 0x01, // Report Size (16)
  482. 0x81, 0x02, // Input (Data, Variable, Absolute)
  483. // In Range (1 bit)
  484. 0x09, 0x32, // Usage (In Range)
  485. 0x81, 0x02, // Input (Data, Variable, Absolute)
  486. // Padding (6 bits)
  487. 0x95, 0x06, // Report Count (6)
  488. 0x81, 0x03, // Input (Constant)
  489. // X/Y Position (4 bytes)
  490. 0x05, 0x01, // Usage Page (Generic Desktop)
  491. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  492. 0x95, 0x01, // Report Count (1)
  493. 0x75, 0x10, // Report Size (16)
  494. 0x65, 0x33, // Unit (Inch, English Linear)
  495. 0x55, 0x0E, // Unit Exponent (-2)
  496. 0x09, 0x30, // Usage (X)
  497. 0x81, 0x02, // Input (Data, Variable, Absolute)
  498. 0x09, 0x31, // Usage (Y)
  499. 0x81, 0x02, // Input (Data, Variable, Absolute)
  500. 0xC0, // End Collection
  501. 0xC0 // End Collection
  502. #endif
  503. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  504. // Programmable buttons report descriptor
  505. 0x05, 0x0C, // Usage Page (Consumer)
  506. 0x09, 0x01, // Usage (Consumer Control)
  507. 0xA1, 0x01, // Collection (Application)
  508. 0x85, REPORT_ID_PROGRAMMABLE_BUTTON, // Report ID
  509. 0x09, 0x03, // Usage (Programmable Buttons)
  510. 0xA1, 0x04, // Collection (Named Array)
  511. 0x05, 0x09, // Usage Page (Button)
  512. 0x19, 0x01, // Usage Minimum (Button 1)
  513. 0x29, 0x20, // Usage Maximum (Button 32)
  514. 0x15, 0x00, // Logical Minimum (0)
  515. 0x25, 0x01, // Logical Maximum (1)
  516. 0x95, 0x20, // Report Count (32)
  517. 0x75, 0x01, // Report Size (1)
  518. 0x81, 0x02, // Input (Data, Variable, Absolute)
  519. 0xC0, // End Collection
  520. 0xC0 // End Collection
  521. #endif
  522. #ifdef SHARED_EP_ENABLE
  523. };
  524. #endif
  525. #ifdef RAW_ENABLE
  526. const PROGMEM uchar raw_hid_report[] = {
  527. 0x06, RAW_USAGE_PAGE_LO, RAW_USAGE_PAGE_HI, // Usage Page (Vendor Defined)
  528. 0x09, RAW_USAGE_ID, // Usage (Vendor Defined)
  529. 0xA1, 0x01, // Collection (Application)
  530. // Data to host
  531. 0x09, 0x62, // Usage (Vendor Defined)
  532. 0x15, 0x00, // Logical Minimum (0)
  533. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  534. 0x95, RAW_BUFFER_SIZE, // Report Count
  535. 0x75, 0x08, // Report Size (8)
  536. 0x81, 0x02, // Input (Data, Variable, Absolute)
  537. // Data from host
  538. 0x09, 0x63, // Usage (Vendor Defined)
  539. 0x15, 0x00, // Logical Minimum (0)
  540. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  541. 0x95, RAW_BUFFER_SIZE, // Report Count
  542. 0x75, 0x08, // Report Size (8)
  543. 0x91, 0x02, // Output (Data, Variable, Absolute)
  544. 0xC0 // End Collection
  545. };
  546. #endif
  547. #if defined(CONSOLE_ENABLE)
  548. const PROGMEM uchar console_hid_report[] = {
  549. 0x06, 0x31, 0xFF, // Usage Page (Vendor Defined - PJRC Teensy compatible)
  550. 0x09, 0x74, // Usage (Vendor Defined - PJRC Teensy compatible)
  551. 0xA1, 0x01, // Collection (Application)
  552. // Data to host
  553. 0x09, 0x75, // Usage (Vendor Defined)
  554. 0x15, 0x00, // Logical Minimum (0x00)
  555. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  556. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  557. 0x75, 0x08, // Report Size (8)
  558. 0x81, 0x02, // Input (Data, Variable, Absolute)
  559. // Data from host
  560. 0x09, 0x76, // Usage (Vendor Defined)
  561. 0x15, 0x00, // Logical Minimum (0x00)
  562. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  563. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  564. 0x75, 0x08, // Report Size (8)
  565. 0x91, 0x02, // Output (Data)
  566. 0xC0 // End Collection
  567. };
  568. #endif
  569. #ifndef USB_MAX_POWER_CONSUMPTION
  570. # define USB_MAX_POWER_CONSUMPTION 500
  571. #endif
  572. // TODO: change this to 10ms to match LUFA
  573. #ifndef USB_POLLING_INTERVAL_MS
  574. # define USB_POLLING_INTERVAL_MS 1
  575. #endif
  576. // clang-format off
  577. const PROGMEM usbStringDescriptor_t usbStringDescriptorZero = {
  578. .header = {
  579. .bLength = USB_STRING_LEN(1),
  580. .bDescriptorType = USBDESCR_STRING
  581. },
  582. .bString = {0x0409} // US English
  583. };
  584. const PROGMEM usbStringDescriptor_t usbStringDescriptorManufacturer = {
  585. .header = {
  586. .bLength = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1),
  587. .bDescriptorType = USBDESCR_STRING
  588. },
  589. .bString = LSTR(MANUFACTURER)
  590. };
  591. const PROGMEM usbStringDescriptor_t usbStringDescriptorProduct = {
  592. .header = {
  593. .bLength = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1),
  594. .bDescriptorType = USBDESCR_STRING
  595. },
  596. .bString = LSTR(PRODUCT)
  597. };
  598. #if defined(SERIAL_NUMBER)
  599. const PROGMEM usbStringDescriptor_t usbStringDescriptorSerial = {
  600. .header = {
  601. .bLength = USB_STRING_LEN(sizeof(SERIAL_NUMBER) - 1),
  602. .bDescriptorType = USBDESCR_STRING
  603. },
  604. .bString = USBSTR(SERIAL_NUMBER)
  605. };
  606. #endif
  607. /*
  608. * Device descriptor
  609. */
  610. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  611. .header = {
  612. .bLength = sizeof(usbDeviceDescriptor_t),
  613. .bDescriptorType = USBDESCR_DEVICE
  614. },
  615. .bcdUSB = 0x0110,
  616. .bDeviceClass = 0x00,
  617. .bDeviceSubClass = 0x00,
  618. .bDeviceProtocol = 0x00,
  619. .bMaxPacketSize0 = 8,
  620. .idVendor = VENDOR_ID,
  621. .idProduct = PRODUCT_ID,
  622. .bcdDevice = DEVICE_VER,
  623. .iManufacturer = 0x01,
  624. .iProduct = 0x02,
  625. #if defined(SERIAL_NUMBER)
  626. .iSerialNumber = 0x03,
  627. #else
  628. .iSerialNumber = 0x00,
  629. #endif
  630. .bNumConfigurations = 1
  631. };
  632. /*
  633. * Configuration descriptors
  634. */
  635. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  636. .header = {
  637. .header = {
  638. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  639. .bDescriptorType = USBDESCR_CONFIG
  640. },
  641. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  642. .bNumInterfaces = TOTAL_INTERFACES,
  643. .bConfigurationValue = 0x01,
  644. .iConfiguration = 0x00,
  645. .bmAttributes = (1 << 7) | USBATTR_REMOTEWAKE,
  646. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  647. },
  648. # ifndef KEYBOARD_SHARED_EP
  649. /*
  650. * Keyboard
  651. */
  652. .keyboardInterface = {
  653. .header = {
  654. .bLength = sizeof(usbInterfaceDescriptor_t),
  655. .bDescriptorType = USBDESCR_INTERFACE
  656. },
  657. .bInterfaceNumber = KEYBOARD_INTERFACE,
  658. .bAlternateSetting = 0x00,
  659. .bNumEndpoints = 1,
  660. .bInterfaceClass = 0x03,
  661. .bInterfaceSubClass = 0x01,
  662. .bInterfaceProtocol = 0x01,
  663. .iInterface = 0x00
  664. },
  665. .keyboardHID = {
  666. .header = {
  667. .bLength = sizeof(usbHIDDescriptor_t),
  668. .bDescriptorType = USBDESCR_HID
  669. },
  670. .bcdHID = 0x0101,
  671. .bCountryCode = 0x00,
  672. .bNumDescriptors = 1,
  673. .bDescriptorType = USBDESCR_HID_REPORT,
  674. .wDescriptorLength = sizeof(keyboard_hid_report)
  675. },
  676. .keyboardINEndpoint = {
  677. .header = {
  678. .bLength = sizeof(usbEndpointDescriptor_t),
  679. .bDescriptorType = USBDESCR_ENDPOINT
  680. },
  681. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  682. .bmAttributes = 0x03,
  683. .wMaxPacketSize = 8,
  684. .bInterval = USB_POLLING_INTERVAL_MS
  685. },
  686. # endif
  687. # if defined(RAW_ENABLE)
  688. /*
  689. * RAW HID
  690. */
  691. .rawInterface = {
  692. .header = {
  693. .bLength = sizeof(usbInterfaceDescriptor_t),
  694. .bDescriptorType = USBDESCR_INTERFACE
  695. },
  696. .bInterfaceNumber = RAW_INTERFACE,
  697. .bAlternateSetting = 0x00,
  698. .bNumEndpoints = 2,
  699. .bInterfaceClass = 0x03,
  700. .bInterfaceSubClass = 0x00,
  701. .bInterfaceProtocol = 0x00,
  702. .iInterface = 0x00
  703. },
  704. .rawHID = {
  705. .header = {
  706. .bLength = sizeof(usbHIDDescriptor_t),
  707. .bDescriptorType = USBDESCR_HID
  708. },
  709. .bcdHID = 0x0101,
  710. .bCountryCode = 0x00,
  711. .bNumDescriptors = 1,
  712. .bDescriptorType = USBDESCR_HID_REPORT,
  713. .wDescriptorLength = sizeof(raw_hid_report)
  714. },
  715. .rawINEndpoint = {
  716. .header = {
  717. .bLength = sizeof(usbEndpointDescriptor_t),
  718. .bDescriptorType = USBDESCR_ENDPOINT
  719. },
  720. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  721. .bmAttributes = 0x03,
  722. .wMaxPacketSize = RAW_EPSIZE,
  723. .bInterval = USB_POLLING_INTERVAL_MS
  724. },
  725. .rawOUTEndpoint = {
  726. .header = {
  727. .bLength = sizeof(usbEndpointDescriptor_t),
  728. .bDescriptorType = USBDESCR_ENDPOINT
  729. },
  730. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  731. .bmAttributes = 0x03,
  732. .wMaxPacketSize = RAW_EPSIZE,
  733. .bInterval = USB_POLLING_INTERVAL_MS
  734. },
  735. # endif
  736. # ifdef SHARED_EP_ENABLE
  737. /*
  738. * Shared
  739. */
  740. .sharedInterface = {
  741. .header = {
  742. .bLength = sizeof(usbInterfaceDescriptor_t),
  743. .bDescriptorType = USBDESCR_INTERFACE
  744. },
  745. .bInterfaceNumber = SHARED_INTERFACE,
  746. .bAlternateSetting = 0x00,
  747. .bNumEndpoints = 1,
  748. .bInterfaceClass = 0x03,
  749. # ifdef KEYBOARD_SHARED_EP
  750. .bInterfaceSubClass = 0x01,
  751. .bInterfaceProtocol = 0x01,
  752. # else
  753. .bInterfaceSubClass = 0x00,
  754. .bInterfaceProtocol = 0x00,
  755. # endif
  756. .iInterface = 0x00
  757. },
  758. .sharedHID = {
  759. .header = {
  760. .bLength = sizeof(usbHIDDescriptor_t),
  761. .bDescriptorType = USBDESCR_HID
  762. },
  763. .bcdHID = 0x0101,
  764. .bCountryCode = 0x00,
  765. .bNumDescriptors = 1,
  766. .bDescriptorType = USBDESCR_HID_REPORT,
  767. .wDescriptorLength = sizeof(shared_hid_report)
  768. },
  769. .sharedINEndpoint = {
  770. .header = {
  771. .bLength = sizeof(usbEndpointDescriptor_t),
  772. .bDescriptorType = USBDESCR_ENDPOINT
  773. },
  774. # ifdef KEYBOARD_SHARED_EP
  775. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  776. # else
  777. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  778. # endif
  779. .bmAttributes = 0x03,
  780. .wMaxPacketSize = 8,
  781. .bInterval = USB_POLLING_INTERVAL_MS
  782. },
  783. # endif
  784. # if defined(CONSOLE_ENABLE)
  785. /*
  786. * Console
  787. */
  788. .consoleInterface = {
  789. .header = {
  790. .bLength = sizeof(usbInterfaceDescriptor_t),
  791. .bDescriptorType = USBDESCR_INTERFACE
  792. },
  793. .bInterfaceNumber = CONSOLE_INTERFACE,
  794. .bAlternateSetting = 0x00,
  795. .bNumEndpoints = 2,
  796. .bInterfaceClass = 0x03,
  797. .bInterfaceSubClass = 0x00,
  798. .bInterfaceProtocol = 0x00,
  799. .iInterface = 0x00
  800. },
  801. .consoleHID = {
  802. .header = {
  803. .bLength = sizeof(usbHIDDescriptor_t),
  804. .bDescriptorType = USBDESCR_HID
  805. },
  806. .bcdHID = 0x0111,
  807. .bCountryCode = 0x00,
  808. .bNumDescriptors = 1,
  809. .bDescriptorType = USBDESCR_HID_REPORT,
  810. .wDescriptorLength = sizeof(console_hid_report)
  811. },
  812. .consoleINEndpoint = {
  813. .header = {
  814. .bLength = sizeof(usbEndpointDescriptor_t),
  815. .bDescriptorType = USBDESCR_ENDPOINT
  816. },
  817. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  818. .bmAttributes = 0x03,
  819. .wMaxPacketSize = CONSOLE_EPSIZE,
  820. .bInterval = 0x01
  821. },
  822. .consoleOUTEndpoint = {
  823. .header = {
  824. .bLength = sizeof(usbEndpointDescriptor_t),
  825. .bDescriptorType = USBDESCR_ENDPOINT
  826. },
  827. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP3_NUMBER),
  828. .bmAttributes = 0x03,
  829. .wMaxPacketSize = CONSOLE_EPSIZE,
  830. .bInterval = 0x01
  831. }
  832. # endif
  833. };
  834. // clang-format on
  835. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  836. usbMsgLen_t len = 0;
  837. switch (rq->wValue.bytes[1]) {
  838. case USBDESCR_DEVICE:
  839. usbMsgPtr = (usbMsgPtr_t)&usbDeviceDescriptor;
  840. len = sizeof(usbDeviceDescriptor_t);
  841. break;
  842. case USBDESCR_CONFIG:
  843. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor;
  844. len = sizeof(usbConfigurationDescriptor_t);
  845. break;
  846. case USBDESCR_STRING:
  847. switch (rq->wValue.bytes[0]) {
  848. case 0:
  849. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorZero;
  850. len = usbStringDescriptorZero.header.bLength;
  851. break;
  852. case 1: // iManufacturer
  853. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorManufacturer;
  854. len = usbStringDescriptorManufacturer.header.bLength;
  855. break;
  856. case 2: // iProduct
  857. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorProduct;
  858. len = usbStringDescriptorProduct.header.bLength;
  859. break;
  860. #if defined(SERIAL_NUMBER)
  861. case 3: // iSerialNumber
  862. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorSerial;
  863. len = usbStringDescriptorSerial.header.bLength;
  864. break;
  865. #endif
  866. }
  867. break;
  868. case USBDESCR_HID:
  869. switch (rq->wValue.bytes[0]) {
  870. #ifndef KEYBOARD_SHARED_EP
  871. case KEYBOARD_INTERFACE:
  872. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID;
  873. len = sizeof(usbHIDDescriptor_t);
  874. break;
  875. #endif
  876. #if defined(RAW_ENABLE)
  877. case RAW_INTERFACE:
  878. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID;
  879. len = sizeof(usbHIDDescriptor_t);
  880. break;
  881. #endif
  882. #ifdef SHARED_EP_ENABLE
  883. case SHARED_INTERFACE:
  884. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.sharedHID;
  885. len = sizeof(usbHIDDescriptor_t);
  886. break;
  887. #endif
  888. #if defined(CONSOLE_ENABLE)
  889. case CONSOLE_INTERFACE:
  890. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID;
  891. len = sizeof(usbHIDDescriptor_t);
  892. break;
  893. #endif
  894. }
  895. break;
  896. case USBDESCR_HID_REPORT:
  897. /* interface index */
  898. switch (rq->wIndex.word) {
  899. #ifndef KEYBOARD_SHARED_EP
  900. case KEYBOARD_INTERFACE:
  901. usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report;
  902. len = sizeof(keyboard_hid_report);
  903. break;
  904. #endif
  905. #if defined(RAW_ENABLE)
  906. case RAW_INTERFACE:
  907. usbMsgPtr = (usbMsgPtr_t)raw_hid_report;
  908. len = sizeof(raw_hid_report);
  909. break;
  910. #endif
  911. #ifdef SHARED_EP_ENABLE
  912. case SHARED_INTERFACE:
  913. usbMsgPtr = (usbMsgPtr_t)shared_hid_report;
  914. len = sizeof(shared_hid_report);
  915. break;
  916. #endif
  917. #if defined(CONSOLE_ENABLE)
  918. case CONSOLE_INTERFACE:
  919. usbMsgPtr = (usbMsgPtr_t)console_hid_report;
  920. len = sizeof(console_hid_report);
  921. break;
  922. #endif
  923. }
  924. break;
  925. }
  926. return len;
  927. }