vusb.c 37 KB

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