vusb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 <avr/eeprom.h>
  15. #include <avr/wdt.h>
  16. #include <stdint.h>
  17. #include "usbdrv.h"
  18. #include "usbconfig.h"
  19. #include "host.h"
  20. #include "report.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "host_driver.h"
  24. #include "vusb.h"
  25. #include <util/delay.h>
  26. static uint8_t vusb_keyboard_leds = 0;
  27. static uint8_t vusb_idle_rate = 0;
  28. /* Keyboard report send buffer */
  29. #define KBUF_SIZE 16
  30. static report_keyboard_t kbuf[KBUF_SIZE];
  31. static uint8_t kbuf_head = 0;
  32. static uint8_t kbuf_tail = 0;
  33. typedef struct {
  34. uint8_t modifier;
  35. uint8_t reserved;
  36. uint8_t keycode[6];
  37. } keyboard_report_t;
  38. static keyboard_report_t keyboard_report; // sent to PC
  39. #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
  40. /* transfer keyboard report from buffer */
  41. void vusb_transfer_keyboard(void) {
  42. for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
  43. if (usbInterruptIsReady()) {
  44. if (kbuf_head != kbuf_tail) {
  45. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  46. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  47. if (debug_keyboard) {
  48. print("V-USB: kbuf[");
  49. pdec(kbuf_tail);
  50. print("->");
  51. pdec(kbuf_head);
  52. print("](");
  53. phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
  54. print(")\n");
  55. }
  56. }
  57. break;
  58. }
  59. usbPoll();
  60. _delay_ms(1);
  61. }
  62. }
  63. /*------------------------------------------------------------------*
  64. * Host driver
  65. *------------------------------------------------------------------*/
  66. static uint8_t keyboard_leds(void);
  67. static void send_keyboard(report_keyboard_t *report);
  68. static void send_mouse(report_mouse_t *report);
  69. static void send_system(uint16_t data);
  70. static void send_consumer(uint16_t data);
  71. static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  72. host_driver_t *vusb_driver(void) { return &driver; }
  73. static uint8_t keyboard_leds(void) { return vusb_keyboard_leds; }
  74. static void send_keyboard(report_keyboard_t *report) {
  75. uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
  76. if (next != kbuf_tail) {
  77. kbuf[kbuf_head] = *report;
  78. kbuf_head = next;
  79. } else {
  80. debug("kbuf: full\n");
  81. }
  82. // NOTE: send key strokes of Macro
  83. usbPoll();
  84. vusb_transfer_keyboard();
  85. }
  86. typedef struct {
  87. uint8_t report_id;
  88. report_mouse_t report;
  89. } __attribute__((packed)) vusb_mouse_report_t;
  90. static void send_mouse(report_mouse_t *report) {
  91. vusb_mouse_report_t r = {.report_id = REPORT_ID_MOUSE, .report = *report};
  92. if (usbInterruptIsReady3()) {
  93. usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
  94. }
  95. }
  96. #ifdef EXTRAKEY_ENABLE
  97. static void send_extra(uint8_t report_id, uint16_t data) {
  98. static uint8_t last_id = 0;
  99. static uint16_t last_data = 0;
  100. if ((report_id == last_id) && (data == last_data)) return;
  101. last_id = report_id;
  102. last_data = data;
  103. report_extra_t report = {.report_id = report_id, .usage = data};
  104. if (usbInterruptIsReady3()) {
  105. usbSetInterrupt3((void *)&report, sizeof(report));
  106. }
  107. }
  108. #endif
  109. static void send_system(uint16_t data) {
  110. #ifdef EXTRAKEY_ENABLE
  111. send_extra(REPORT_ID_SYSTEM, data);
  112. #endif
  113. }
  114. static void send_consumer(uint16_t data) {
  115. #ifdef EXTRAKEY_ENABLE
  116. send_extra(REPORT_ID_CONSUMER, data);
  117. #endif
  118. }
  119. /*------------------------------------------------------------------*
  120. * Request from host *
  121. *------------------------------------------------------------------*/
  122. static struct {
  123. uint16_t len;
  124. enum { NONE, SET_LED } kind;
  125. } last_req;
  126. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  127. usbRequest_t *rq = (void *)data;
  128. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  129. if (rq->bRequest == USBRQ_HID_GET_REPORT) {
  130. debug("GET_REPORT:");
  131. /* we only have one report type, so don't look at wValue */
  132. usbMsgPtr = (void *)&keyboard_report;
  133. return sizeof(keyboard_report);
  134. } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
  135. debug("GET_IDLE: ");
  136. // debug_hex(vusb_idle_rate);
  137. usbMsgPtr = &vusb_idle_rate;
  138. return 1;
  139. } else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
  140. vusb_idle_rate = rq->wValue.bytes[1];
  141. debug("SET_IDLE: ");
  142. debug_hex(vusb_idle_rate);
  143. } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
  144. debug("SET_REPORT: ");
  145. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  146. if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
  147. debug("SET_LED: ");
  148. last_req.kind = SET_LED;
  149. last_req.len = rq->wLength.word;
  150. }
  151. return USB_NO_MSG; // to get data in usbFunctionWrite
  152. } else {
  153. debug("UNKNOWN:");
  154. }
  155. } else {
  156. debug("VENDOR:");
  157. /* no vendor specific requests implemented */
  158. }
  159. debug("\n");
  160. return 0; /* default for not implemented requests: return no data back to host */
  161. }
  162. uchar usbFunctionWrite(uchar *data, uchar len) {
  163. if (last_req.len == 0) {
  164. return -1;
  165. }
  166. switch (last_req.kind) {
  167. case SET_LED:
  168. debug("SET_LED: ");
  169. debug_hex(data[0]);
  170. debug("\n");
  171. vusb_keyboard_leds = data[0];
  172. last_req.len = 0;
  173. return 1;
  174. break;
  175. case NONE:
  176. default:
  177. return -1;
  178. break;
  179. }
  180. return 1;
  181. }
  182. /*------------------------------------------------------------------*
  183. * Descriptors *
  184. *------------------------------------------------------------------*/
  185. const PROGMEM uchar keyboard_hid_report[] = {
  186. 0x05, 0x01, // Usage Page (Generic Desktop)
  187. 0x09, 0x06, // Usage (Keyboard)
  188. 0xA1, 0x01, // Collection (Application)
  189. // Modifiers (8 bits)
  190. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  191. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  192. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  193. 0x15, 0x00, // Logical Minimum (0)
  194. 0x25, 0x01, // Logical Maximum (1)
  195. 0x95, 0x08, // Report Count (8)
  196. 0x75, 0x01, // Report Size (1)
  197. 0x81, 0x02, // Input (Data, Variable, Absolute)
  198. // Reserved (1 byte)
  199. 0x95, 0x01, // Report Count (1)
  200. 0x75, 0x08, // Report Size (8)
  201. 0x81, 0x03, // Input (Constant)
  202. // Keycodes (6 bytes)
  203. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  204. 0x19, 0x00, // Usage Minimum (0)
  205. 0x29, 0xFF, // Usage Maximum (255)
  206. 0x15, 0x00, // Logical Minimum (0)
  207. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  208. 0x95, 0x06, // Report Count (6)
  209. 0x75, 0x08, // Report Size (8)
  210. 0x81, 0x00, // Input (Data, Array, Absolute)
  211. // Status LEDs (5 bits)
  212. 0x05, 0x08, // Usage Page (LED)
  213. 0x19, 0x01, // Usage Minimum (Num Lock)
  214. 0x29, 0x05, // Usage Maximum (Kana)
  215. 0x95, 0x05, // Report Count (5)
  216. 0x75, 0x01, // Report Size (1)
  217. 0x91, 0x02, // Output (Data, Variable, Absolute)
  218. // LED padding (3 bits)
  219. 0x95, 0x01, // Report Count (1)
  220. 0x75, 0x03, // Report Size (3)
  221. 0x91, 0x03, // Output (Constant)
  222. 0xC0 // End Collection
  223. };
  224. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  225. const PROGMEM uchar mouse_extra_hid_report[] = {
  226. # ifdef MOUSE_ENABLE
  227. // Mouse report descriptor
  228. 0x05, 0x01, // Usage Page (Generic Desktop)
  229. 0x09, 0x02, // Usage (Mouse)
  230. 0xA1, 0x01, // Collection (Application)
  231. 0x85, REPORT_ID_MOUSE, // Report ID
  232. 0x09, 0x01, // Usage (Pointer)
  233. 0xA1, 0x00, // Collection (Physical)
  234. // Buttons (5 bits)
  235. 0x05, 0x09, // Usage Page (Button)
  236. 0x19, 0x01, // Usage Minimum (Button 1)
  237. 0x29, 0x05, // Usage Maximum (Button 5)
  238. 0x15, 0x00, // Logical Minimum (0)
  239. 0x25, 0x01, // Logical Maximum (1)
  240. 0x95, 0x05, // Report Count (5)
  241. 0x75, 0x01, // Report Size (1)
  242. 0x81, 0x02, // Input (Data, Variable, Absolute)
  243. // Button padding (3 bits)
  244. 0x95, 0x01, // Report Count (1)
  245. 0x75, 0x03, // Report Size (3)
  246. 0x81, 0x03, // Input (Constant)
  247. // X/Y position (2 bytes)
  248. 0x05, 0x01, // Usage Page (Generic Desktop)
  249. 0x09, 0x30, // Usage (X)
  250. 0x09, 0x31, // Usage (Y)
  251. 0x15, 0x81, // Logical Minimum (-127)
  252. 0x25, 0x7F, // Logical Maximum (127)
  253. 0x95, 0x02, // Report Count (2)
  254. 0x75, 0x08, // Report Size (8)
  255. 0x81, 0x06, // Input (Data, Variable, Relative)
  256. // Vertical wheel (1 byte)
  257. 0x09, 0x38, // Usage (Wheel)
  258. 0x15, 0x81, // Logical Minimum (-127)
  259. 0x25, 0x7F, // Logical Maximum (127)
  260. 0x95, 0x01, // Report Count (1)
  261. 0x75, 0x08, // Report Size (8)
  262. 0x81, 0x06, // Input (Data, Variable, Relative)
  263. // Horizontal wheel (1 byte)
  264. 0x05, 0x0C, // Usage Page (Consumer)
  265. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  266. 0x15, 0x81, // Logical Minimum (-127)
  267. 0x25, 0x7F, // Logical Maximum (127)
  268. 0x95, 0x01, // Report Count (1)
  269. 0x75, 0x08, // Report Size (8)
  270. 0x81, 0x06, // Input (Data, Variable, Relative)
  271. 0xC0, // End Collection
  272. 0xC0, // End Collection
  273. # endif
  274. # ifdef EXTRAKEY_ENABLE
  275. // Extrakeys report descriptor
  276. 0x05, 0x01, // Usage Page (Generic Desktop)
  277. 0x09, 0x80, // Usage (System Control)
  278. 0xA1, 0x01, // Collection (Application)
  279. 0x85, REPORT_ID_SYSTEM, // Report ID
  280. 0x19, 0x01, // Usage Minimum (Pointer)
  281. 0x2A, 0xB7, 0x00, // Usage Maximum (System Display LCD Autoscale)
  282. 0x15, 0x01, // Logical Minimum
  283. 0x26, 0xB7, 0x00, // Logical Maximum
  284. 0x95, 0x01, // Report Count (1)
  285. 0x75, 0x10, // Report Size (16)
  286. 0x81, 0x00, // Input (Data, Array, Absolute)
  287. 0xC0, // End Collection
  288. 0x05, 0x0C, // Usage Page (Consumer)
  289. 0x09, 0x01, // Usage (Consumer Control)
  290. 0xA1, 0x01, // Collection (Application)
  291. 0x85, REPORT_ID_CONSUMER, // Report ID
  292. 0x19, 0x01, // Usage Minimum (Consumer Control)
  293. 0x2A, 0xA0, 0x02, // Usage Maximum (AC Desktop Show All Applications)
  294. 0x15, 0x01, // Logical Minimum
  295. 0x26, 0xA0, 0x02, // Logical Maximum
  296. 0x95, 0x01, // Report Count (1)
  297. 0x75, 0x10, // Report Size (16)
  298. 0x81, 0x00, // Input (Data, Array, Absolute)
  299. 0xC0 // End Collection
  300. # endif
  301. };
  302. #endif
  303. #ifndef SERIAL_NUMBER
  304. # define SERIAL_NUMBER 0
  305. #endif
  306. #ifndef USB_MAX_POWER_CONSUMPTION
  307. # define USB_MAX_POWER_CONSUMPTION 500
  308. #endif
  309. // TODO: change this to 10ms to match LUFA
  310. #ifndef USB_POLLING_INTERVAL_MS
  311. # define USB_POLLING_INTERVAL_MS 1
  312. #endif
  313. // clang-format off
  314. const PROGMEM usbStringDescriptor_t usbStringDescriptorZero = {
  315. .header = {
  316. .bLength = USB_STRING_LEN(1),
  317. .bDescriptorType = USBDESCR_STRING
  318. },
  319. .bString = {0x0409} // US English
  320. };
  321. const PROGMEM usbStringDescriptor_t usbStringDescriptorManufacturer = {
  322. .header = {
  323. .bLength = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1),
  324. .bDescriptorType = USBDESCR_STRING
  325. },
  326. .bString = LSTR(MANUFACTURER)
  327. };
  328. const PROGMEM usbStringDescriptor_t usbStringDescriptorProduct = {
  329. .header = {
  330. .bLength = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1),
  331. .bDescriptorType = USBDESCR_STRING
  332. },
  333. .bString = LSTR(PRODUCT)
  334. };
  335. const PROGMEM usbStringDescriptor_t usbStringDescriptorSerial = {
  336. .header = {
  337. .bLength = USB_STRING_LEN(sizeof(STR(SERIAL_NUMBER)) - 1),
  338. .bDescriptorType = USBDESCR_STRING
  339. },
  340. .bString = LSTR(SERIAL_NUMBER)
  341. };
  342. #if USB_CFG_DESCR_PROPS_DEVICE
  343. /*
  344. * Device descriptor
  345. */
  346. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  347. .header = {
  348. .bLength = sizeof(usbDeviceDescriptor_t),
  349. .bDescriptorType = USBDESCR_DEVICE
  350. },
  351. .bcdUSB = 0x0110,
  352. .bDeviceClass = USB_CFG_DEVICE_CLASS,
  353. .bDeviceSubClass = USB_CFG_DEVICE_SUBCLASS,
  354. .bDeviceProtocol = 0x00,
  355. .bMaxPacketSize0 = 8,
  356. .idVendor = VENDOR_ID,
  357. .idProduct = PRODUCT_ID,
  358. .bcdDevice = DEVICE_VER,
  359. .iManufacturer = 0x01,
  360. .iProduct = 0x02,
  361. .iSerialNumber = 0x03,
  362. .bNumConfigurations = 1
  363. };
  364. #endif
  365. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  366. /*
  367. * Configuration descriptors
  368. */
  369. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  370. .header = {
  371. .header = {
  372. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  373. .bDescriptorType = USBDESCR_CONFIG
  374. },
  375. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  376. # if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  377. .bNumInterfaces = 2,
  378. # else
  379. .bNumInterfaces = 1,
  380. # endif
  381. .bConfigurationValue = 0x01,
  382. .iConfiguration = 0x00,
  383. # if USB_CFG_IS_SELF_POWERED
  384. .bmAttributes = (1 << 7) | USBATTR_SELFPOWER,
  385. # else
  386. .bmAttributes = (1 << 7),
  387. # endif
  388. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  389. },
  390. /*
  391. * Keyboard
  392. */
  393. .keyboardInterface = {
  394. .header = {
  395. .bLength = sizeof(usbInterfaceDescriptor_t),
  396. .bDescriptorType = USBDESCR_INTERFACE
  397. },
  398. .bInterfaceNumber = 0,
  399. .bAlternateSetting = 0x00,
  400. .bNumEndpoints = USB_CFG_HAVE_INTRIN_ENDPOINT,
  401. .bInterfaceClass = USB_CFG_INTERFACE_CLASS,
  402. .bInterfaceSubClass = USB_CFG_INTERFACE_SUBCLASS,
  403. .bInterfaceProtocol = USB_CFG_INTERFACE_PROTOCOL,
  404. .iInterface = 0x00
  405. },
  406. .keyboardHID = {
  407. .header = {
  408. .bLength = sizeof(usbHIDDescriptor_t),
  409. .bDescriptorType = USBDESCR_HID
  410. },
  411. .bcdHID = 0x0101,
  412. .bCountryCode = 0x00,
  413. .bNumDescriptors = 1,
  414. .bDescriptorType = USBDESCR_HID_REPORT,
  415. .wDescriptorLength = sizeof(keyboard_hid_report)
  416. },
  417. # ifdef USB_CFG_HAVE_INTRIN_ENDPOINT
  418. .keyboardINEndpoint = {
  419. .header = {
  420. .bLength = sizeof(usbEndpointDescriptor_t),
  421. .bDescriptorType = USBDESCR_ENDPOINT
  422. },
  423. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  424. .bmAttributes = 0x03,
  425. .wMaxPacketSize = 8,
  426. .bInterval = USB_POLLING_INTERVAL_MS
  427. },
  428. # endif
  429. # if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  430. /*
  431. * Mouse/Extrakeys
  432. */
  433. .mouseExtraInterface = {
  434. .header = {
  435. .bLength = sizeof(usbInterfaceDescriptor_t),
  436. .bDescriptorType = USBDESCR_INTERFACE
  437. },
  438. .bInterfaceNumber = 1,
  439. .bAlternateSetting = 0x00,
  440. .bNumEndpoints = USB_CFG_HAVE_INTRIN_ENDPOINT3,
  441. .bInterfaceClass = 0x03,
  442. .bInterfaceSubClass = 0x00,
  443. .bInterfaceProtocol = 0x00,
  444. .iInterface = 0x00
  445. },
  446. .mouseExtraHID = {
  447. .header = {
  448. .bLength = sizeof(usbHIDDescriptor_t),
  449. .bDescriptorType = USBDESCR_HID
  450. },
  451. .bcdHID = 0x0101,
  452. .bCountryCode = 0x00,
  453. .bNumDescriptors = 1,
  454. .bDescriptorType = USBDESCR_HID_REPORT,
  455. .wDescriptorLength = sizeof(mouse_extra_hid_report)
  456. },
  457. # if USB_CFG_HAVE_INTRIN_ENDPOINT3
  458. .mouseExtraINEndpoint = {
  459. .header = {
  460. .bLength = sizeof(usbEndpointDescriptor_t),
  461. .bDescriptorType = USBDESCR_ENDPOINT
  462. },
  463. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  464. .bmAttributes = 0x03,
  465. .wMaxPacketSize = 8,
  466. .bInterval = USB_POLLING_INTERVAL_MS
  467. }
  468. # endif
  469. # endif
  470. };
  471. #endif
  472. // clang-format on
  473. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  474. usbMsgLen_t len = 0;
  475. /*
  476. debug("usbFunctionDescriptor: ");
  477. debug_hex(rq->bmRequestType); debug(" ");
  478. debug_hex(rq->bRequest); debug(" ");
  479. debug_hex16(rq->wValue.word); debug(" ");
  480. debug_hex16(rq->wIndex.word); debug(" ");
  481. debug_hex16(rq->wLength.word); debug("\n");
  482. */
  483. switch (rq->wValue.bytes[1]) {
  484. #if USB_CFG_DESCR_PROPS_DEVICE
  485. case USBDESCR_DEVICE:
  486. usbMsgPtr = (unsigned char *)&usbDeviceDescriptor;
  487. len = sizeof(usbDeviceDescriptor_t);
  488. break;
  489. #endif
  490. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  491. case USBDESCR_CONFIG:
  492. usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor;
  493. len = sizeof(usbConfigurationDescriptor_t);
  494. break;
  495. #endif
  496. case USBDESCR_STRING:
  497. switch (rq->wValue.bytes[0]) {
  498. case 0:
  499. usbMsgPtr = (unsigned char *)&usbStringDescriptorZero;
  500. len = usbStringDescriptorZero.header.bLength;
  501. break;
  502. case 1: // iManufacturer
  503. usbMsgPtr = (unsigned char *)&usbStringDescriptorManufacturer;
  504. len = usbStringDescriptorManufacturer.header.bLength;
  505. break;
  506. case 2: // iProduct
  507. usbMsgPtr = (unsigned char *)&usbStringDescriptorProduct;
  508. len = usbStringDescriptorProduct.header.bLength;
  509. break;
  510. case 3: // iSerialNumber
  511. usbMsgPtr = (unsigned char *)&usbStringDescriptorSerial;
  512. len = usbStringDescriptorSerial.header.bLength;
  513. break;
  514. }
  515. break;
  516. case USBDESCR_HID:
  517. switch (rq->wValue.bytes[0]) {
  518. case 0:
  519. usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.keyboardHID;
  520. len = sizeof(usbHIDDescriptor_t);
  521. break;
  522. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  523. case 1:
  524. usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.mouseExtraHID;
  525. len = sizeof(usbHIDDescriptor_t);
  526. break;
  527. #endif
  528. }
  529. break;
  530. case USBDESCR_HID_REPORT:
  531. /* interface index */
  532. switch (rq->wIndex.word) {
  533. case 0:
  534. usbMsgPtr = (unsigned char *)keyboard_hid_report;
  535. len = sizeof(keyboard_hid_report);
  536. break;
  537. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  538. case 1:
  539. usbMsgPtr = (unsigned char *)mouse_extra_hid_report;
  540. len = sizeof(mouse_extra_hid_report);
  541. break;
  542. #endif
  543. }
  544. break;
  545. }
  546. // debug("desc len: "); debug_hex(len); debug("\n");
  547. return len;
  548. }