vusb.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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. #ifdef JOYSTICK_ENABLE
  30. # include "joystick.h"
  31. #endif
  32. #ifdef XAP_ENABLE
  33. # include "xap.h"
  34. # include <string.h>
  35. #endif
  36. #if defined(CONSOLE_ENABLE)
  37. # define RBUF_SIZE 128
  38. # include "ring_buffer.h"
  39. #endif
  40. #ifdef OS_DETECTION_ENABLE
  41. # include "os_detection.h"
  42. #endif
  43. /*
  44. * Interface indexes
  45. */
  46. enum usb_interfaces {
  47. #ifndef KEYBOARD_SHARED_EP
  48. KEYBOARD_INTERFACE,
  49. #else
  50. SHARED_INTERFACE,
  51. # define KEYBOARD_INTERFACE SHARED_INTERFACE
  52. #endif
  53. // It is important that the Raw HID interface is at a constant
  54. // interface number, to support Linux/OSX platforms and chrome.hid
  55. // If Raw HID is enabled, let it be always 1.
  56. #ifdef RAW_ENABLE
  57. RAW_INTERFACE,
  58. #endif
  59. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  60. SHARED_INTERFACE,
  61. #endif
  62. #ifdef CONSOLE_ENABLE
  63. CONSOLE_INTERFACE,
  64. #endif
  65. #ifdef XAP_ENABLE
  66. XAP_INTERFACE,
  67. #endif
  68. TOTAL_INTERFACES
  69. };
  70. #define MAX_INTERFACES 3
  71. _Static_assert(TOTAL_INTERFACES <= MAX_INTERFACES, "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.");
  72. #if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)) && CONSOLE_ENABLE
  73. # error Mouse/Extra Keys share an endpoint with Console. Please disable one of the two.
  74. #endif
  75. static uint8_t keyboard_led_state = 0;
  76. uint8_t keyboard_idle = 0;
  77. uint8_t keyboard_protocol = 1;
  78. static report_keyboard_t keyboard_report_sent;
  79. static void send_report_fragment(uint8_t endpoint, void *data, size_t size) {
  80. for (uint8_t retries = 5; retries > 0; retries--) {
  81. switch (endpoint) {
  82. case 1:
  83. if (usbInterruptIsReady()) {
  84. usbSetInterrupt(data, size);
  85. return;
  86. }
  87. break;
  88. case USB_CFG_EP3_NUMBER:
  89. if (usbInterruptIsReady3()) {
  90. usbSetInterrupt3(data, size);
  91. return;
  92. }
  93. break;
  94. case USB_CFG_EP4_NUMBER:
  95. if (usbInterruptIsReady4()) {
  96. usbSetInterrupt4(data, size);
  97. return;
  98. }
  99. break;
  100. default:
  101. return;
  102. }
  103. usbPoll();
  104. wait_ms(5);
  105. }
  106. }
  107. static void send_report(uint8_t endpoint, void *report, size_t size) {
  108. uint8_t *temp = (uint8_t *)report;
  109. // Send as many full packets as possible
  110. for (uint8_t i = 0; i < size / 8; i++) {
  111. send_report_fragment(endpoint, temp, 8);
  112. temp += 8;
  113. }
  114. // Send any data left over
  115. uint8_t remainder = size % 8;
  116. if (remainder) {
  117. send_report_fragment(endpoint, temp, remainder);
  118. }
  119. }
  120. /*------------------------------------------------------------------*
  121. * RAW HID
  122. *------------------------------------------------------------------*/
  123. #ifdef RAW_ENABLE
  124. # define RAW_BUFFER_SIZE 32
  125. # define RAW_EPSIZE 8
  126. static uint8_t raw_output_buffer[RAW_BUFFER_SIZE];
  127. static uint8_t raw_output_received_bytes = 0;
  128. void raw_hid_send(uint8_t *data, uint8_t length) {
  129. if (length != RAW_BUFFER_SIZE) {
  130. return;
  131. }
  132. send_report(4, data, RAW_BUFFER_SIZE);
  133. }
  134. __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
  135. // Users should #include "raw_hid.h" in their own code
  136. // and implement this function there. Leave this as weak linkage
  137. // so users can opt to not handle data coming in.
  138. }
  139. void raw_hid_task(void) {
  140. if (raw_output_received_bytes == RAW_BUFFER_SIZE) {
  141. raw_hid_receive(raw_output_buffer, RAW_BUFFER_SIZE);
  142. raw_output_received_bytes = 0;
  143. }
  144. }
  145. #endif
  146. /*------------------------------------------------------------------*
  147. * XAP
  148. *------------------------------------------------------------------*/
  149. #ifdef XAP_ENABLE
  150. # define XAP_BUFFER_SIZE 64
  151. # define XAP_EPSIZE 8
  152. static uint8_t xap_output_buffer[XAP_BUFFER_SIZE];
  153. static uint8_t xap_output_received_bytes = 0;
  154. extern void xap_receive(xap_token_t token, const uint8_t *data, size_t length);
  155. void xap_send_base(uint8_t *data, uint8_t length) {
  156. if (length != XAP_BUFFER_SIZE) {
  157. return;
  158. }
  159. send_report(4, data, XAP_BUFFER_SIZE);
  160. }
  161. void xap_send(xap_token_t token, xap_response_flags_t response_flags, const void *data, size_t length) {
  162. uint8_t rdata[XAP_BUFFER_SIZE] = {0};
  163. xap_response_header_t *header = (xap_response_header_t *)&rdata[0];
  164. header->token = token;
  165. if (length > (XAP_BUFFER_SIZE - sizeof(xap_response_header_t))) response_flags &= ~(XAP_RESPONSE_FLAG_SUCCESS);
  166. header->flags = response_flags;
  167. if (response_flags & (XAP_RESPONSE_FLAG_SUCCESS)) {
  168. header->length = (uint8_t)length;
  169. if (data != NULL) {
  170. memcpy(&rdata[sizeof(xap_response_header_t)], data, length);
  171. }
  172. }
  173. xap_send_base(rdata, sizeof(rdata));
  174. }
  175. void xap_broadcast(uint8_t type, const void *data, size_t length) {
  176. uint8_t rdata[XAP_BUFFER_SIZE] = {0};
  177. xap_broadcast_header_t *header = (xap_broadcast_header_t *)&rdata[0];
  178. header->token = XAP_BROADCAST_TOKEN;
  179. header->type = type;
  180. if (length > (XAP_BUFFER_SIZE - sizeof(xap_broadcast_header_t))) return;
  181. header->length = (uint8_t)length;
  182. if (data != NULL) {
  183. memcpy(&rdata[sizeof(xap_broadcast_header_t)], data, length);
  184. }
  185. xap_send_base(rdata, sizeof(rdata));
  186. }
  187. void xap_receive_base(const void *data) {
  188. const uint8_t * u8data = (const uint8_t *)data;
  189. xap_request_header_t *header = (xap_request_header_t *)&u8data[0];
  190. if (header->length <= (XAP_BUFFER_SIZE - sizeof(xap_request_header_t))) {
  191. xap_receive(header->token, &u8data[sizeof(xap_request_header_t)], header->length);
  192. }
  193. }
  194. void xap_task(void) {
  195. if (xap_output_received_bytes == XAP_BUFFER_SIZE) {
  196. xap_receive_base(xap_output_buffer);
  197. xap_output_received_bytes = 0;
  198. }
  199. }
  200. #endif
  201. /*------------------------------------------------------------------*
  202. * Console
  203. *------------------------------------------------------------------*/
  204. #ifdef CONSOLE_ENABLE
  205. # define CONSOLE_BUFFER_SIZE 32
  206. # define CONSOLE_EPSIZE 8
  207. int8_t sendchar(uint8_t c) {
  208. rbuf_enqueue(c);
  209. return 0;
  210. }
  211. void console_task(void) {
  212. if (!usbConfiguration) {
  213. return;
  214. }
  215. if (!rbuf_has_data()) {
  216. return;
  217. }
  218. // Send in chunks of 8 padded to 32
  219. char send_buf[CONSOLE_BUFFER_SIZE] = {0};
  220. uint8_t send_buf_count = 0;
  221. while (rbuf_has_data() && send_buf_count < CONSOLE_EPSIZE) {
  222. send_buf[send_buf_count++] = rbuf_dequeue();
  223. }
  224. send_report(3, send_buf, CONSOLE_BUFFER_SIZE);
  225. }
  226. #endif
  227. /*------------------------------------------------------------------*
  228. * Host driver
  229. *------------------------------------------------------------------*/
  230. static uint8_t keyboard_leds(void);
  231. static void send_keyboard(report_keyboard_t *report);
  232. static void send_nkro(report_nkro_t *report);
  233. static void send_mouse(report_mouse_t *report);
  234. static void send_extra(report_extra_t *report);
  235. static host_driver_t driver = {keyboard_leds, send_keyboard, send_nkro, send_mouse, send_extra};
  236. host_driver_t *vusb_driver(void) {
  237. return &driver;
  238. }
  239. static uint8_t keyboard_leds(void) {
  240. return keyboard_led_state;
  241. }
  242. static void send_keyboard(report_keyboard_t *report) {
  243. if (!keyboard_protocol) {
  244. send_report(1, &report->mods, 8);
  245. } else {
  246. send_report(1, report, sizeof(report_keyboard_t));
  247. }
  248. keyboard_report_sent = *report;
  249. }
  250. #ifndef KEYBOARD_SHARED_EP
  251. # define MOUSE_IN_EPNUM 3
  252. # define SHARED_IN_EPNUM 3
  253. #else
  254. # define MOUSE_IN_EPNUM 1
  255. # define SHARED_IN_EPNUM 1
  256. #endif
  257. static void send_nkro(report_nkro_t *report) {
  258. #ifdef NKRO_ENABLE
  259. send_report(3, report, sizeof(report_nkro_t));
  260. #endif
  261. }
  262. static void send_mouse(report_mouse_t *report) {
  263. #ifdef MOUSE_ENABLE
  264. send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t));
  265. #endif
  266. }
  267. static void send_extra(report_extra_t *report) {
  268. #ifdef EXTRAKEY_ENABLE
  269. send_report(SHARED_IN_EPNUM, report, sizeof(report_extra_t));
  270. #endif
  271. }
  272. void send_joystick(report_joystick_t *report) {
  273. #ifdef JOYSTICK_ENABLE
  274. send_report(SHARED_IN_EPNUM, report, sizeof(report_joystick_t));
  275. #endif
  276. }
  277. void send_digitizer(report_digitizer_t *report) {
  278. #ifdef DIGITIZER_ENABLE
  279. send_report(SHARED_IN_EPNUM, report, sizeof(report_digitizer_t));
  280. #endif
  281. }
  282. void send_programmable_button(report_programmable_button_t *report) {
  283. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  284. send_report(SHARED_IN_EPNUM, report, sizeof(report_programmable_button_t));
  285. #endif
  286. }
  287. /*------------------------------------------------------------------*
  288. * Request from host *
  289. *------------------------------------------------------------------*/
  290. static struct {
  291. uint16_t len;
  292. enum { NONE, SET_LED } kind;
  293. } last_req;
  294. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  295. usbRequest_t *rq = (void *)data;
  296. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  297. switch (rq->bRequest) {
  298. case USBRQ_HID_GET_REPORT:
  299. dprint("GET_REPORT:");
  300. if (rq->wIndex.word == KEYBOARD_INTERFACE) {
  301. usbMsgPtr = (usbMsgPtr_t)&keyboard_report_sent;
  302. return sizeof(keyboard_report_sent);
  303. }
  304. break;
  305. case USBRQ_HID_GET_IDLE:
  306. dprint("GET_IDLE:");
  307. usbMsgPtr = (usbMsgPtr_t)&keyboard_idle;
  308. return 1;
  309. case USBRQ_HID_GET_PROTOCOL:
  310. dprint("GET_PROTOCOL:");
  311. usbMsgPtr = (usbMsgPtr_t)&keyboard_protocol;
  312. return 1;
  313. case USBRQ_HID_SET_REPORT:
  314. dprint("SET_REPORT:");
  315. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  316. if (rq->wValue.word == 0x0200 && rq->wIndex.word == KEYBOARD_INTERFACE) {
  317. dprint("SET_LED:");
  318. last_req.kind = SET_LED;
  319. last_req.len = rq->wLength.word;
  320. }
  321. return USB_NO_MSG; // to get data in usbFunctionWrite
  322. case USBRQ_HID_SET_IDLE:
  323. keyboard_idle = (rq->wValue.word & 0xFF00) >> 8;
  324. dprintf("SET_IDLE: %02X", keyboard_idle);
  325. break;
  326. case USBRQ_HID_SET_PROTOCOL:
  327. if (rq->wIndex.word == KEYBOARD_INTERFACE) {
  328. keyboard_protocol = rq->wValue.word & 0xFF;
  329. dprintf("SET_PROTOCOL: %02X", keyboard_protocol);
  330. }
  331. break;
  332. default:
  333. dprint("UNKNOWN:");
  334. break;
  335. }
  336. } else {
  337. dprint("VENDOR:");
  338. /* no vendor specific requests implemented */
  339. }
  340. dprint("\n");
  341. return 0; /* default for not implemented requests: return no data back to host */
  342. }
  343. uchar usbFunctionWrite(uchar *data, uchar len) {
  344. if (last_req.len == 0) {
  345. return -1;
  346. }
  347. switch (last_req.kind) {
  348. case SET_LED:
  349. dprintf("SET_LED: %02X\n", data[0]);
  350. keyboard_led_state = data[0];
  351. last_req.len = 0;
  352. return 1;
  353. break;
  354. case NONE:
  355. default:
  356. return -1;
  357. break;
  358. }
  359. return 1;
  360. }
  361. void usbFunctionWriteOut(uchar *data, uchar len) {
  362. #ifdef RAW_ENABLE
  363. // Data from host must be divided every 8bytes
  364. if (len != 8) {
  365. dprint("RAW: invalid length\n");
  366. raw_output_received_bytes = 0;
  367. return;
  368. }
  369. if (raw_output_received_bytes + len > RAW_BUFFER_SIZE) {
  370. dprint("RAW: buffer full\n");
  371. raw_output_received_bytes = 0;
  372. } else {
  373. for (uint8_t i = 0; i < 8; i++) {
  374. raw_output_buffer[raw_output_received_bytes + i] = data[i];
  375. }
  376. raw_output_received_bytes += len;
  377. }
  378. #endif
  379. #ifdef XAP_ENABLE
  380. // Data from host must be divided every 8bytes
  381. if (len != 8) {
  382. dprint("XAP: invalid length\n");
  383. xap_output_received_bytes = 0;
  384. return;
  385. }
  386. if (xap_output_received_bytes + len > XAP_BUFFER_SIZE) {
  387. dprint("XAP: buffer full\n");
  388. xap_output_received_bytes = 0;
  389. } else {
  390. for (uint8_t i = 0; i < 8; i++) {
  391. xap_output_buffer[xap_output_received_bytes + i] = data[i];
  392. }
  393. xap_output_received_bytes += len;
  394. }
  395. #endif
  396. }
  397. /*------------------------------------------------------------------*
  398. * Descriptors *
  399. *------------------------------------------------------------------*/
  400. #ifdef KEYBOARD_SHARED_EP
  401. const PROGMEM uchar shared_hid_report[] = {
  402. # define SHARED_REPORT_STARTED
  403. #else
  404. const PROGMEM uchar keyboard_hid_report[] = {
  405. #endif
  406. 0x05, 0x01, // Usage Page (Generic Desktop)
  407. 0x09, 0x06, // Usage (Keyboard)
  408. 0xA1, 0x01, // Collection (Application)
  409. #ifdef KEYBOARD_SHARED_EP
  410. 0x85, REPORT_ID_KEYBOARD, // Report ID
  411. #endif
  412. // Modifiers (8 bits)
  413. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  414. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  415. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  416. 0x15, 0x00, // Logical Minimum (0)
  417. 0x25, 0x01, // Logical Maximum (1)
  418. 0x95, 0x08, // Report Count (8)
  419. 0x75, 0x01, // Report Size (1)
  420. 0x81, 0x02, // Input (Data, Variable, Absolute)
  421. // Reserved (1 byte)
  422. 0x95, 0x01, // Report Count (1)
  423. 0x75, 0x08, // Report Size (8)
  424. 0x81, 0x03, // Input (Constant)
  425. // Keycodes (6 bytes)
  426. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  427. 0x19, 0x00, // Usage Minimum (0)
  428. 0x29, 0xFF, // Usage Maximum (255)
  429. 0x15, 0x00, // Logical Minimum (0)
  430. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  431. 0x95, 0x06, // Report Count (6)
  432. 0x75, 0x08, // Report Size (8)
  433. 0x81, 0x00, // Input (Data, Array, Absolute)
  434. // Status LEDs (5 bits)
  435. 0x05, 0x08, // Usage Page (LED)
  436. 0x19, 0x01, // Usage Minimum (Num Lock)
  437. 0x29, 0x05, // Usage Maximum (Kana)
  438. 0x15, 0x00, // Logical Minimum (0)
  439. 0x25, 0x01, // Logical Maximum (1)
  440. 0x95, 0x05, // Report Count (5)
  441. 0x75, 0x01, // Report Size (1)
  442. 0x91, 0x02, // Output (Data, Variable, Absolute)
  443. // LED padding (3 bits)
  444. 0x95, 0x01, // Report Count (1)
  445. 0x75, 0x03, // Report Size (3)
  446. 0x91, 0x03, // Output (Constant)
  447. 0xC0, // End Collection
  448. #ifndef KEYBOARD_SHARED_EP
  449. };
  450. #endif
  451. #if defined(SHARED_EP_ENABLE) && !defined(SHARED_REPORT_STARTED)
  452. const PROGMEM uchar shared_hid_report[] = {
  453. # define SHARED_REPORT_STARTED
  454. #endif
  455. #ifdef NKRO_ENABLE
  456. // NKRO report descriptor
  457. 0x05, 0x01, // Usage Page (Generic Desktop)
  458. 0x09, 0x06, // Usage (Keyboard)
  459. 0xA1, 0x01, // Collection (Application)
  460. 0x85, REPORT_ID_NKRO, // Report ID
  461. // Modifiers (8 bits)
  462. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  463. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  464. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  465. 0x15, 0x00, // Logical Minimum (0)
  466. 0x25, 0x01, // Logical Maximum (1)
  467. 0x95, 0x08, // Report Count (8)
  468. 0x75, 0x01, // Report Size (1)
  469. 0x81, 0x02, // Input (Data, Variable, Absolute)
  470. // Keycodes
  471. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  472. 0x19, 0x00, // Usage Minimum (0)
  473. 0x29, NKRO_REPORT_BITS * 8 - 1, // Usage Maximum
  474. 0x15, 0x00, // Logical Minimum (0)
  475. 0x25, 0x01, // Logical Maximum (1)
  476. 0x95, NKRO_REPORT_BITS * 8, // Report Count
  477. 0x75, 0x01, // Report Size (1)
  478. 0x81, 0x02, // Input (Data, Variable, Absolute)
  479. // Status LEDs (5 bits)
  480. 0x05, 0x08, // Usage Page (LED)
  481. 0x19, 0x01, // Usage Minimum (Num Lock)
  482. 0x29, 0x05, // Usage Maximum (Kana)
  483. 0x95, 0x05, // Report Count (5)
  484. 0x75, 0x01, // Report Size (1)
  485. 0x91, 0x02, // Output (Data, Variable, Absolute)
  486. // LED padding (3 bits)
  487. 0x95, 0x01, // Report Count (1)
  488. 0x75, 0x03, // Report Size (3)
  489. 0x91, 0x03, // Output (Constant)
  490. 0xC0, // End Collection
  491. #endif
  492. #ifdef MOUSE_ENABLE
  493. // Mouse report descriptor
  494. 0x05, 0x01, // Usage Page (Generic Desktop)
  495. 0x09, 0x02, // Usage (Mouse)
  496. 0xA1, 0x01, // Collection (Application)
  497. 0x85, REPORT_ID_MOUSE, // Report ID
  498. 0x09, 0x01, // Usage (Pointer)
  499. 0xA1, 0x00, // Collection (Physical)
  500. // Buttons (8 bits)
  501. 0x05, 0x09, // Usage Page (Button)
  502. 0x19, 0x01, // Usage Minimum (Button 1)
  503. 0x29, 0x08, // Usage Maximum (Button 8)
  504. 0x15, 0x00, // Logical Minimum (0)
  505. 0x25, 0x01, // Logical Maximum (1)
  506. 0x95, 0x08, // Report Count (8)
  507. 0x75, 0x01, // Report Size (1)
  508. 0x81, 0x02, // Input (Data, Variable, Absolute)
  509. # ifdef MOUSE_EXTENDED_REPORT
  510. // Boot protocol XY ignored in Report protocol
  511. 0x95, 0x02, // Report Count (2)
  512. 0x75, 0x08, // Report Size (8)
  513. 0x81, 0x03, // Input (Constant)
  514. # endif
  515. // X/Y position (2 or 4 bytes)
  516. 0x05, 0x01, // Usage Page (Generic Desktop)
  517. 0x09, 0x30, // Usage (X)
  518. 0x09, 0x31, // Usage (Y)
  519. # ifndef MOUSE_EXTENDED_REPORT
  520. 0x15, 0x81, // Logical Minimum (-127)
  521. 0x25, 0x7F, // Logical Maximum (127)
  522. 0x95, 0x02, // Report Count (2)
  523. 0x75, 0x08, // Report Size (8)
  524. # else
  525. 0x16, 0x01, 0x80, // Logical Minimum (-32767)
  526. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  527. 0x95, 0x02, // Report Count (2)
  528. 0x75, 0x10, // Report Size (16)
  529. # endif
  530. 0x81, 0x06, // Input (Data, Variable, Relative)
  531. // Vertical wheel (1 byte)
  532. 0x09, 0x38, // Usage (Wheel)
  533. 0x15, 0x81, // Logical Minimum (-127)
  534. 0x25, 0x7F, // Logical Maximum (127)
  535. 0x95, 0x01, // Report Count (1)
  536. 0x75, 0x08, // Report Size (8)
  537. 0x81, 0x06, // Input (Data, Variable, Relative)
  538. // Horizontal wheel (1 byte)
  539. 0x05, 0x0C, // Usage Page (Consumer)
  540. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  541. 0x15, 0x81, // Logical Minimum (-127)
  542. 0x25, 0x7F, // Logical Maximum (127)
  543. 0x95, 0x01, // Report Count (1)
  544. 0x75, 0x08, // Report Size (8)
  545. 0x81, 0x06, // Input (Data, Variable, Relative)
  546. 0xC0, // End Collection
  547. 0xC0, // End Collection
  548. #endif
  549. #ifdef EXTRAKEY_ENABLE
  550. // Extrakeys report descriptor
  551. 0x05, 0x01, // Usage Page (Generic Desktop)
  552. 0x09, 0x80, // Usage (System Control)
  553. 0xA1, 0x01, // Collection (Application)
  554. 0x85, REPORT_ID_SYSTEM, // Report ID
  555. 0x19, 0x01, // Usage Minimum (Pointer)
  556. 0x2A, 0xB7, 0x00, // Usage Maximum (System Display LCD Autoscale)
  557. 0x15, 0x01, // Logical Minimum
  558. 0x26, 0xB7, 0x00, // Logical Maximum
  559. 0x95, 0x01, // Report Count (1)
  560. 0x75, 0x10, // Report Size (16)
  561. 0x81, 0x00, // Input (Data, Array, Absolute)
  562. 0xC0, // End Collection
  563. 0x05, 0x0C, // Usage Page (Consumer)
  564. 0x09, 0x01, // Usage (Consumer Control)
  565. 0xA1, 0x01, // Collection (Application)
  566. 0x85, REPORT_ID_CONSUMER, // Report ID
  567. 0x19, 0x01, // Usage Minimum (Consumer Control)
  568. 0x2A, 0xA0, 0x02, // Usage Maximum (AC Desktop Show All Applications)
  569. 0x15, 0x01, // Logical Minimum
  570. 0x26, 0xA0, 0x02, // Logical Maximum
  571. 0x95, 0x01, // Report Count (1)
  572. 0x75, 0x10, // Report Size (16)
  573. 0x81, 0x00, // Input (Data, Array, Absolute)
  574. 0xC0, // End Collection
  575. #endif
  576. #ifdef JOYSTICK_ENABLE
  577. // Joystick report descriptor
  578. 0x05, 0x01, // Usage Page (Generic Desktop)
  579. 0x09, 0x04, // Usage (Joystick)
  580. 0xA1, 0x01, // Collection (Application)
  581. 0x85, REPORT_ID_JOYSTICK, // Report ID
  582. 0xA1, 0x00, // Collection (Physical)
  583. # if JOYSTICK_AXIS_COUNT > 0
  584. 0x05, 0x01, // Usage Page (Generic Desktop)
  585. 0x09, 0x30, // Usage (X)
  586. # if JOYSTICK_AXIS_COUNT > 1
  587. 0x09, 0x31, // Usage (Y)
  588. # endif
  589. # if JOYSTICK_AXIS_COUNT > 2
  590. 0x09, 0x32, // Usage (Z)
  591. # endif
  592. # if JOYSTICK_AXIS_COUNT > 3
  593. 0x09, 0x33, // Usage (Rx)
  594. # endif
  595. # if JOYSTICK_AXIS_COUNT > 4
  596. 0x09, 0x34, // Usage (Ry)
  597. # endif
  598. # if JOYSTICK_AXIS_COUNT > 5
  599. 0x09, 0x35, // Usage (Rz)
  600. # endif
  601. # if JOYSTICK_AXIS_RESOLUTION == 8
  602. 0x15, -JOYSTICK_MAX_VALUE, // Logical Minimum
  603. 0x25, JOYSTICK_MAX_VALUE, // Logical Maximum
  604. 0x95, JOYSTICK_AXIS_COUNT, // Report Count
  605. 0x75, 0x08, // Report Size (8)
  606. # else
  607. 0x16, HID_VALUE_16(-JOYSTICK_MAX_VALUE), // Logical Minimum
  608. 0x26, HID_VALUE_16(JOYSTICK_MAX_VALUE), // Logical Maximum
  609. 0x95, JOYSTICK_AXIS_COUNT, // Report Count
  610. 0x75, 0x10, // Report Size (16)
  611. # endif
  612. 0x81, 0x02, // Input (Data, Variable, Absolute)
  613. # endif
  614. # if JOYSTICK_BUTTON_COUNT > 0
  615. 0x05, 0x09, // Usage Page (Button)
  616. 0x19, 0x01, // Usage Minimum (Button 1)
  617. 0x29, JOYSTICK_BUTTON_COUNT, // Usage Maximum
  618. 0x15, 0x00, // Logical Minimum (0)
  619. 0x25, 0x01, // Logical Maximum (1)
  620. 0x95, JOYSTICK_BUTTON_COUNT, // Report Count
  621. 0x75, 0x01, // Report Size (1)
  622. 0x81, 0x02, // Input (Data, Variable, Absolute)
  623. # if (JOYSTICK_BUTTON_COUNT % 8) != 0
  624. 0x95, 8 - (JOYSTICK_BUTTON_COUNT % 8), // Report Count
  625. 0x75, 0x01, // Report Size (1)
  626. 0x81, 0x03, // Input (Constant)
  627. # endif
  628. # endif
  629. 0xC0, // End Collection
  630. 0xC0, // End Collection
  631. #endif
  632. #ifdef DIGITIZER_ENABLE
  633. // Digitizer report descriptor
  634. 0x05, 0x0D, // Usage Page (Digitizers)
  635. 0x09, 0x01, // Usage (Digitizer)
  636. 0xA1, 0x01, // Collection (Application)
  637. 0x85, REPORT_ID_DIGITIZER, // Report ID
  638. 0x09, 0x20, // Usage (Stylus)
  639. 0xA1, 0x00, // Collection (Physical)
  640. // In Range, Tip Switch & Barrel Switch (3 bits)
  641. 0x09, 0x32, // Usage (In Range)
  642. 0x09, 0x42, // Usage (Tip Switch)
  643. 0x09, 0x44, // Usage (Barrel Switch)
  644. 0x15, 0x00, // Logical Minimum
  645. 0x25, 0x01, // Logical Maximum
  646. 0x95, 0x03, // Report Count (3)
  647. 0x75, 0x01, // Report Size (1)
  648. 0x81, 0x02, // Input (Data, Variable, Absolute)
  649. // Padding (5 bits)
  650. 0x95, 0x05, // Report Count (5)
  651. 0x81, 0x03, // Input (Constant)
  652. // X/Y Position (4 bytes)
  653. 0x05, 0x01, // Usage Page (Generic Desktop)
  654. 0x09, 0x30, // Usage (X)
  655. 0x09, 0x31, // Usage (Y)
  656. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  657. 0x95, 0x02, // Report Count (2)
  658. 0x75, 0x10, // Report Size (16)
  659. 0x65, 0x33, // Unit (Inch, English Linear)
  660. 0x55, 0x0E, // Unit Exponent (-2)
  661. 0x81, 0x02, // Input (Data, Variable, Absolute)
  662. 0xC0, // End Collection
  663. 0xC0, // End Collection
  664. #endif
  665. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  666. // Programmable buttons report descriptor
  667. 0x05, 0x0C, // Usage Page (Consumer)
  668. 0x09, 0x01, // Usage (Consumer Control)
  669. 0xA1, 0x01, // Collection (Application)
  670. 0x85, REPORT_ID_PROGRAMMABLE_BUTTON, // Report ID
  671. 0x09, 0x03, // Usage (Programmable Buttons)
  672. 0xA1, 0x04, // Collection (Named Array)
  673. 0x05, 0x09, // Usage Page (Button)
  674. 0x19, 0x01, // Usage Minimum (Button 1)
  675. 0x29, 0x20, // Usage Maximum (Button 32)
  676. 0x15, 0x00, // Logical Minimum (0)
  677. 0x25, 0x01, // Logical Maximum (1)
  678. 0x95, 0x20, // Report Count (32)
  679. 0x75, 0x01, // Report Size (1)
  680. 0x81, 0x02, // Input (Data, Variable, Absolute)
  681. 0xC0, // End Collection
  682. 0xC0, // End Collection
  683. #endif
  684. #ifdef SHARED_EP_ENABLE
  685. };
  686. #endif
  687. #ifdef RAW_ENABLE
  688. const PROGMEM uchar raw_hid_report[] = {
  689. 0x06, HID_VALUE_16(RAW_USAGE_PAGE), // Usage Page (Vendor Defined)
  690. 0x09, RAW_USAGE_ID, // Usage (Vendor Defined)
  691. 0xA1, 0x01, // Collection (Application)
  692. // Data to host
  693. 0x09, 0x62, // Usage (Vendor Defined)
  694. 0x15, 0x00, // Logical Minimum (0)
  695. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  696. 0x95, RAW_BUFFER_SIZE, // Report Count
  697. 0x75, 0x08, // Report Size (8)
  698. 0x81, 0x02, // Input (Data, Variable, Absolute)
  699. // Data from host
  700. 0x09, 0x63, // Usage (Vendor Defined)
  701. 0x15, 0x00, // Logical Minimum (0)
  702. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  703. 0x95, RAW_BUFFER_SIZE, // Report Count
  704. 0x75, 0x08, // Report Size (8)
  705. 0x91, 0x02, // Output (Data, Variable, Absolute)
  706. 0xC0 // End Collection
  707. };
  708. #endif
  709. #if defined(CONSOLE_ENABLE)
  710. const PROGMEM uchar console_hid_report[] = {
  711. 0x06, 0x31, 0xFF, // Usage Page (Vendor Defined - PJRC Teensy compatible)
  712. 0x09, 0x74, // Usage (Vendor Defined - PJRC Teensy compatible)
  713. 0xA1, 0x01, // Collection (Application)
  714. // Data to host
  715. 0x09, 0x75, // Usage (Vendor Defined)
  716. 0x15, 0x00, // Logical Minimum (0x00)
  717. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  718. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  719. 0x75, 0x08, // Report Size (8)
  720. 0x81, 0x02, // Input (Data, Variable, Absolute)
  721. 0xC0 // End Collection
  722. };
  723. #endif
  724. #ifdef XAP_ENABLE
  725. const PROGMEM uchar xap_report[] = {
  726. 0x06, 0x51, 0xFF, // Usage Page (Vendor Defined)
  727. 0x09, 0x58, // Usage (Vendor Defined)
  728. 0xA1, 0x01, // Collection (Application)
  729. // Data to host
  730. 0x09, 0x62, // Usage (Vendor Defined)
  731. 0x15, 0x00, // Logical Minimum (0)
  732. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  733. 0x95, XAP_BUFFER_SIZE, // Report Count
  734. 0x75, 0x08, // Report Size (8)
  735. 0x81, 0x02, // Input (Data, Variable, Absolute)
  736. // Data from host
  737. 0x09, 0x63, // Usage (Vendor Defined)
  738. 0x15, 0x00, // Logical Minimum (0)
  739. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  740. 0x95, XAP_BUFFER_SIZE, // Report Count
  741. 0x75, 0x08, // Report Size (8)
  742. 0x91, 0x02, // Output (Data, Variable, Absolute)
  743. 0xC0 // End Collection
  744. };
  745. #endif
  746. #ifndef USB_MAX_POWER_CONSUMPTION
  747. # define USB_MAX_POWER_CONSUMPTION 500
  748. #endif
  749. #ifndef USB_POLLING_INTERVAL_MS
  750. # define USB_POLLING_INTERVAL_MS 1
  751. #endif
  752. // clang-format off
  753. const PROGMEM usbStringDescriptor_t usbStringDescriptorZero = {
  754. .header = {
  755. .bLength = 4,
  756. .bDescriptorType = USBDESCR_STRING
  757. },
  758. .bString = {0x0409} // US English
  759. };
  760. const PROGMEM usbStringDescriptor_t usbStringDescriptorManufacturer = {
  761. .header = {
  762. .bLength = sizeof(USBSTR(MANUFACTURER)),
  763. .bDescriptorType = USBDESCR_STRING
  764. },
  765. .bString = USBSTR(MANUFACTURER)
  766. };
  767. const PROGMEM usbStringDescriptor_t usbStringDescriptorProduct = {
  768. .header = {
  769. .bLength = sizeof(USBSTR(PRODUCT)),
  770. .bDescriptorType = USBDESCR_STRING
  771. },
  772. .bString = USBSTR(PRODUCT)
  773. };
  774. #if defined(SERIAL_NUMBER)
  775. const PROGMEM usbStringDescriptor_t usbStringDescriptorSerial = {
  776. .header = {
  777. .bLength = sizeof(USBSTR(SERIAL_NUMBER)),
  778. .bDescriptorType = USBDESCR_STRING
  779. },
  780. .bString = USBSTR(SERIAL_NUMBER)
  781. };
  782. #endif
  783. /*
  784. * Device descriptor
  785. */
  786. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  787. .header = {
  788. .bLength = sizeof(usbDeviceDescriptor_t),
  789. .bDescriptorType = USBDESCR_DEVICE
  790. },
  791. .bcdUSB = 0x0110,
  792. .bDeviceClass = 0x00,
  793. .bDeviceSubClass = 0x00,
  794. .bDeviceProtocol = 0x00,
  795. .bMaxPacketSize0 = 8,
  796. .idVendor = VENDOR_ID,
  797. .idProduct = PRODUCT_ID,
  798. .bcdDevice = DEVICE_VER,
  799. .iManufacturer = 0x01,
  800. .iProduct = 0x02,
  801. #if defined(SERIAL_NUMBER)
  802. .iSerialNumber = 0x03,
  803. #else
  804. .iSerialNumber = 0x00,
  805. #endif
  806. .bNumConfigurations = 1
  807. };
  808. /*
  809. * Configuration descriptors
  810. */
  811. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  812. .header = {
  813. .header = {
  814. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  815. .bDescriptorType = USBDESCR_CONFIG
  816. },
  817. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  818. .bNumInterfaces = TOTAL_INTERFACES,
  819. .bConfigurationValue = 0x01,
  820. .iConfiguration = 0x00,
  821. .bmAttributes = (1 << 7) | USBATTR_REMOTEWAKE,
  822. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  823. },
  824. # ifndef KEYBOARD_SHARED_EP
  825. /*
  826. * Keyboard
  827. */
  828. .keyboardInterface = {
  829. .header = {
  830. .bLength = sizeof(usbInterfaceDescriptor_t),
  831. .bDescriptorType = USBDESCR_INTERFACE
  832. },
  833. .bInterfaceNumber = KEYBOARD_INTERFACE,
  834. .bAlternateSetting = 0x00,
  835. .bNumEndpoints = 1,
  836. .bInterfaceClass = 0x03,
  837. .bInterfaceSubClass = 0x01,
  838. .bInterfaceProtocol = 0x01,
  839. .iInterface = 0x00
  840. },
  841. .keyboardHID = {
  842. .header = {
  843. .bLength = sizeof(usbHIDDescriptor_t),
  844. .bDescriptorType = USBDESCR_HID
  845. },
  846. .bcdHID = 0x0101,
  847. .bCountryCode = 0x00,
  848. .bNumDescriptors = 1,
  849. .bDescriptorType = USBDESCR_HID_REPORT,
  850. .wDescriptorLength = sizeof(keyboard_hid_report)
  851. },
  852. .keyboardINEndpoint = {
  853. .header = {
  854. .bLength = sizeof(usbEndpointDescriptor_t),
  855. .bDescriptorType = USBDESCR_ENDPOINT
  856. },
  857. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  858. .bmAttributes = 0x03,
  859. .wMaxPacketSize = 8,
  860. .bInterval = USB_POLLING_INTERVAL_MS
  861. },
  862. # endif
  863. # if defined(RAW_ENABLE)
  864. /*
  865. * RAW HID
  866. */
  867. .rawInterface = {
  868. .header = {
  869. .bLength = sizeof(usbInterfaceDescriptor_t),
  870. .bDescriptorType = USBDESCR_INTERFACE
  871. },
  872. .bInterfaceNumber = RAW_INTERFACE,
  873. .bAlternateSetting = 0x00,
  874. .bNumEndpoints = 2,
  875. .bInterfaceClass = 0x03,
  876. .bInterfaceSubClass = 0x00,
  877. .bInterfaceProtocol = 0x00,
  878. .iInterface = 0x00
  879. },
  880. .rawHID = {
  881. .header = {
  882. .bLength = sizeof(usbHIDDescriptor_t),
  883. .bDescriptorType = USBDESCR_HID
  884. },
  885. .bcdHID = 0x0101,
  886. .bCountryCode = 0x00,
  887. .bNumDescriptors = 1,
  888. .bDescriptorType = USBDESCR_HID_REPORT,
  889. .wDescriptorLength = sizeof(raw_hid_report)
  890. },
  891. .rawINEndpoint = {
  892. .header = {
  893. .bLength = sizeof(usbEndpointDescriptor_t),
  894. .bDescriptorType = USBDESCR_ENDPOINT
  895. },
  896. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  897. .bmAttributes = 0x03,
  898. .wMaxPacketSize = RAW_EPSIZE,
  899. .bInterval = USB_POLLING_INTERVAL_MS
  900. },
  901. .rawOUTEndpoint = {
  902. .header = {
  903. .bLength = sizeof(usbEndpointDescriptor_t),
  904. .bDescriptorType = USBDESCR_ENDPOINT
  905. },
  906. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  907. .bmAttributes = 0x03,
  908. .wMaxPacketSize = RAW_EPSIZE,
  909. .bInterval = USB_POLLING_INTERVAL_MS
  910. },
  911. # endif
  912. # ifdef SHARED_EP_ENABLE
  913. /*
  914. * Shared
  915. */
  916. .sharedInterface = {
  917. .header = {
  918. .bLength = sizeof(usbInterfaceDescriptor_t),
  919. .bDescriptorType = USBDESCR_INTERFACE
  920. },
  921. .bInterfaceNumber = SHARED_INTERFACE,
  922. .bAlternateSetting = 0x00,
  923. .bNumEndpoints = 1,
  924. .bInterfaceClass = 0x03,
  925. # ifdef KEYBOARD_SHARED_EP
  926. .bInterfaceSubClass = 0x01,
  927. .bInterfaceProtocol = 0x01,
  928. # else
  929. .bInterfaceSubClass = 0x00,
  930. .bInterfaceProtocol = 0x00,
  931. # endif
  932. .iInterface = 0x00
  933. },
  934. .sharedHID = {
  935. .header = {
  936. .bLength = sizeof(usbHIDDescriptor_t),
  937. .bDescriptorType = USBDESCR_HID
  938. },
  939. .bcdHID = 0x0101,
  940. .bCountryCode = 0x00,
  941. .bNumDescriptors = 1,
  942. .bDescriptorType = USBDESCR_HID_REPORT,
  943. .wDescriptorLength = sizeof(shared_hid_report)
  944. },
  945. .sharedINEndpoint = {
  946. .header = {
  947. .bLength = sizeof(usbEndpointDescriptor_t),
  948. .bDescriptorType = USBDESCR_ENDPOINT
  949. },
  950. # ifdef KEYBOARD_SHARED_EP
  951. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  952. # else
  953. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  954. # endif
  955. .bmAttributes = 0x03,
  956. .wMaxPacketSize = 8,
  957. .bInterval = USB_POLLING_INTERVAL_MS
  958. },
  959. # endif
  960. # if defined(CONSOLE_ENABLE)
  961. /*
  962. * Console
  963. */
  964. .consoleInterface = {
  965. .header = {
  966. .bLength = sizeof(usbInterfaceDescriptor_t),
  967. .bDescriptorType = USBDESCR_INTERFACE
  968. },
  969. .bInterfaceNumber = CONSOLE_INTERFACE,
  970. .bAlternateSetting = 0x00,
  971. .bNumEndpoints = 2,
  972. .bInterfaceClass = 0x03,
  973. .bInterfaceSubClass = 0x00,
  974. .bInterfaceProtocol = 0x00,
  975. .iInterface = 0x00
  976. },
  977. .consoleHID = {
  978. .header = {
  979. .bLength = sizeof(usbHIDDescriptor_t),
  980. .bDescriptorType = USBDESCR_HID
  981. },
  982. .bcdHID = 0x0111,
  983. .bCountryCode = 0x00,
  984. .bNumDescriptors = 1,
  985. .bDescriptorType = USBDESCR_HID_REPORT,
  986. .wDescriptorLength = sizeof(console_hid_report)
  987. },
  988. .consoleINEndpoint = {
  989. .header = {
  990. .bLength = sizeof(usbEndpointDescriptor_t),
  991. .bDescriptorType = USBDESCR_ENDPOINT
  992. },
  993. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  994. .bmAttributes = 0x03,
  995. .wMaxPacketSize = CONSOLE_EPSIZE,
  996. .bInterval = 0x01
  997. },
  998. # endif
  999. # if defined(XAP_ENABLE)
  1000. /*
  1001. * XAP
  1002. */
  1003. .xapInterface = {
  1004. .header = {
  1005. .bLength = sizeof(usbInterfaceDescriptor_t),
  1006. .bDescriptorType = USBDESCR_INTERFACE
  1007. },
  1008. .bInterfaceNumber = XAP_INTERFACE,
  1009. .bAlternateSetting = 0x00,
  1010. .bNumEndpoints = 2,
  1011. .bInterfaceClass = 0x03,
  1012. .bInterfaceSubClass = 0x00,
  1013. .bInterfaceProtocol = 0x00,
  1014. .iInterface = 0x00
  1015. },
  1016. .xapHID = {
  1017. .header = {
  1018. .bLength = sizeof(usbHIDDescriptor_t),
  1019. .bDescriptorType = USBDESCR_HID
  1020. },
  1021. .bcdHID = 0x0101,
  1022. .bCountryCode = 0x00,
  1023. .bNumDescriptors = 1,
  1024. .bDescriptorType = USBDESCR_HID_REPORT,
  1025. .wDescriptorLength = sizeof(xap_report)
  1026. },
  1027. .xapINEndpoint = {
  1028. .header = {
  1029. .bLength = sizeof(usbEndpointDescriptor_t),
  1030. .bDescriptorType = USBDESCR_ENDPOINT
  1031. },
  1032. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  1033. .bmAttributes = 0x03,
  1034. .wMaxPacketSize = XAP_EPSIZE,
  1035. .bInterval = USB_POLLING_INTERVAL_MS
  1036. },
  1037. .xapOUTEndpoint = {
  1038. .header = {
  1039. .bLength = sizeof(usbEndpointDescriptor_t),
  1040. .bDescriptorType = USBDESCR_ENDPOINT
  1041. },
  1042. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  1043. .bmAttributes = 0x03,
  1044. .wMaxPacketSize = XAP_EPSIZE,
  1045. .bInterval = USB_POLLING_INTERVAL_MS
  1046. },
  1047. # endif
  1048. };
  1049. // clang-format on
  1050. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  1051. usbMsgLen_t len = 0;
  1052. switch (rq->wValue.bytes[1]) {
  1053. case USBDESCR_DEVICE:
  1054. usbMsgPtr = (usbMsgPtr_t)&usbDeviceDescriptor;
  1055. len = sizeof(usbDeviceDescriptor_t);
  1056. break;
  1057. case USBDESCR_CONFIG:
  1058. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor;
  1059. len = sizeof(usbConfigurationDescriptor_t);
  1060. break;
  1061. case USBDESCR_STRING:
  1062. switch (rq->wValue.bytes[0]) {
  1063. case 0:
  1064. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorZero;
  1065. len = usbStringDescriptorZero.header.bLength;
  1066. break;
  1067. case 1: // iManufacturer
  1068. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorManufacturer;
  1069. len = usbStringDescriptorManufacturer.header.bLength;
  1070. break;
  1071. case 2: // iProduct
  1072. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorProduct;
  1073. len = usbStringDescriptorProduct.header.bLength;
  1074. break;
  1075. #if defined(SERIAL_NUMBER)
  1076. case 3: // iSerialNumber
  1077. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorSerial;
  1078. len = usbStringDescriptorSerial.header.bLength;
  1079. break;
  1080. #endif
  1081. }
  1082. #ifdef OS_DETECTION_ENABLE
  1083. process_wlength(rq->wLength.word);
  1084. #endif
  1085. break;
  1086. case USBDESCR_HID:
  1087. switch (rq->wIndex.word) {
  1088. #ifndef KEYBOARD_SHARED_EP
  1089. case KEYBOARD_INTERFACE:
  1090. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID;
  1091. len = sizeof(usbHIDDescriptor_t);
  1092. break;
  1093. #endif
  1094. #if defined(RAW_ENABLE)
  1095. case RAW_INTERFACE:
  1096. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID;
  1097. len = sizeof(usbHIDDescriptor_t);
  1098. break;
  1099. #endif
  1100. #ifdef SHARED_EP_ENABLE
  1101. case SHARED_INTERFACE:
  1102. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.sharedHID;
  1103. len = sizeof(usbHIDDescriptor_t);
  1104. break;
  1105. #endif
  1106. #if defined(CONSOLE_ENABLE)
  1107. case CONSOLE_INTERFACE:
  1108. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID;
  1109. len = sizeof(usbHIDDescriptor_t);
  1110. break;
  1111. #endif
  1112. #if defined(XAP_ENABLE)
  1113. case XAP_INTERFACE:
  1114. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.xapHID;
  1115. len = sizeof(usbHIDDescriptor_t);
  1116. break;
  1117. #endif
  1118. }
  1119. break;
  1120. case USBDESCR_HID_REPORT:
  1121. /* interface index */
  1122. switch (rq->wIndex.word) {
  1123. #ifndef KEYBOARD_SHARED_EP
  1124. case KEYBOARD_INTERFACE:
  1125. usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report;
  1126. len = sizeof(keyboard_hid_report);
  1127. break;
  1128. #endif
  1129. #if defined(RAW_ENABLE)
  1130. case RAW_INTERFACE:
  1131. usbMsgPtr = (usbMsgPtr_t)raw_hid_report;
  1132. len = sizeof(raw_hid_report);
  1133. break;
  1134. #endif
  1135. #ifdef SHARED_EP_ENABLE
  1136. case SHARED_INTERFACE:
  1137. usbMsgPtr = (usbMsgPtr_t)shared_hid_report;
  1138. len = sizeof(shared_hid_report);
  1139. break;
  1140. #endif
  1141. #if defined(CONSOLE_ENABLE)
  1142. case CONSOLE_INTERFACE:
  1143. usbMsgPtr = (usbMsgPtr_t)console_hid_report;
  1144. len = sizeof(console_hid_report);
  1145. break;
  1146. #endif
  1147. #if defined(XAP_ENABLE)
  1148. case XAP_INTERFACE:
  1149. usbMsgPtr = (usbMsgPtr_t)xap_report;
  1150. len = sizeof(xap_report);
  1151. break;
  1152. #endif
  1153. }
  1154. break;
  1155. }
  1156. return len;
  1157. }