vusb.c 25 KB

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