vusb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. #if defined(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. #if defined(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. #if defined(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. #if USB_CFG_DESCR_PROPS_DEVICE
  429. /*
  430. * Device descriptor
  431. */
  432. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  433. .header = {
  434. .bLength = sizeof(usbDeviceDescriptor_t),
  435. .bDescriptorType = USBDESCR_DEVICE
  436. },
  437. .bcdUSB = 0x0110,
  438. .bDeviceClass = USB_CFG_DEVICE_CLASS,
  439. .bDeviceSubClass = USB_CFG_DEVICE_SUBCLASS,
  440. .bDeviceProtocol = 0x00,
  441. .bMaxPacketSize0 = 8,
  442. .idVendor = VENDOR_ID,
  443. .idProduct = PRODUCT_ID,
  444. .bcdDevice = DEVICE_VER,
  445. .iManufacturer = 0x01,
  446. .iProduct = 0x02,
  447. .iSerialNumber = 0x03,
  448. .bNumConfigurations = 1
  449. };
  450. #endif
  451. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  452. /*
  453. * Configuration descriptors
  454. */
  455. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  456. .header = {
  457. .header = {
  458. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  459. .bDescriptorType = USBDESCR_CONFIG
  460. },
  461. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  462. # if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE) || defined(RAW_ENABLE)
  463. .bNumInterfaces = 2,
  464. # else
  465. .bNumInterfaces = 1,
  466. # endif
  467. .bConfigurationValue = 0x01,
  468. .iConfiguration = 0x00,
  469. # if USB_CFG_IS_SELF_POWERED
  470. .bmAttributes = (1 << 7) | USBATTR_SELFPOWER,
  471. # else
  472. .bmAttributes = (1 << 7),
  473. # endif
  474. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  475. },
  476. /*
  477. * Keyboard
  478. */
  479. .keyboardInterface = {
  480. .header = {
  481. .bLength = sizeof(usbInterfaceDescriptor_t),
  482. .bDescriptorType = USBDESCR_INTERFACE
  483. },
  484. .bInterfaceNumber = 0,
  485. .bAlternateSetting = 0x00,
  486. .bNumEndpoints = USB_CFG_HAVE_INTRIN_ENDPOINT,
  487. .bInterfaceClass = USB_CFG_INTERFACE_CLASS,
  488. .bInterfaceSubClass = USB_CFG_INTERFACE_SUBCLASS,
  489. .bInterfaceProtocol = USB_CFG_INTERFACE_PROTOCOL,
  490. .iInterface = 0x00
  491. },
  492. .keyboardHID = {
  493. .header = {
  494. .bLength = sizeof(usbHIDDescriptor_t),
  495. .bDescriptorType = USBDESCR_HID
  496. },
  497. .bcdHID = 0x0101,
  498. .bCountryCode = 0x00,
  499. .bNumDescriptors = 1,
  500. .bDescriptorType = USBDESCR_HID_REPORT,
  501. .wDescriptorLength = sizeof(keyboard_hid_report)
  502. },
  503. # ifdef USB_CFG_HAVE_INTRIN_ENDPOINT
  504. .keyboardINEndpoint = {
  505. .header = {
  506. .bLength = sizeof(usbEndpointDescriptor_t),
  507. .bDescriptorType = USBDESCR_ENDPOINT
  508. },
  509. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  510. .bmAttributes = 0x03,
  511. .wMaxPacketSize = 8,
  512. .bInterval = USB_POLLING_INTERVAL_MS
  513. },
  514. # endif
  515. # if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  516. /*
  517. * Mouse/Extrakeys
  518. */
  519. .mouseExtraInterface = {
  520. .header = {
  521. .bLength = sizeof(usbInterfaceDescriptor_t),
  522. .bDescriptorType = USBDESCR_INTERFACE
  523. },
  524. .bInterfaceNumber = 1,
  525. .bAlternateSetting = 0x00,
  526. .bNumEndpoints = USB_CFG_HAVE_INTRIN_ENDPOINT3,
  527. .bInterfaceClass = 0x03,
  528. .bInterfaceSubClass = 0x00,
  529. .bInterfaceProtocol = 0x00,
  530. .iInterface = 0x00
  531. },
  532. .mouseExtraHID = {
  533. .header = {
  534. .bLength = sizeof(usbHIDDescriptor_t),
  535. .bDescriptorType = USBDESCR_HID
  536. },
  537. .bcdHID = 0x0101,
  538. .bCountryCode = 0x00,
  539. .bNumDescriptors = 1,
  540. .bDescriptorType = USBDESCR_HID_REPORT,
  541. .wDescriptorLength = sizeof(mouse_extra_hid_report)
  542. },
  543. # if USB_CFG_HAVE_INTRIN_ENDPOINT3
  544. .mouseExtraINEndpoint = {
  545. .header = {
  546. .bLength = sizeof(usbEndpointDescriptor_t),
  547. .bDescriptorType = USBDESCR_ENDPOINT
  548. },
  549. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  550. .bmAttributes = 0x03,
  551. .wMaxPacketSize = 8,
  552. .bInterval = USB_POLLING_INTERVAL_MS
  553. }
  554. # endif
  555. # elif defined(RAW_ENABLE)
  556. .rawInterface = {
  557. .header = {
  558. .bLength = sizeof(usbInterfaceDescriptor_t),
  559. .bDescriptorType = USBDESCR_INTERFACE
  560. },
  561. .bInterfaceNumber = 1,
  562. .bAlternateSetting = 0x00,
  563. .bNumEndpoints = 2,
  564. .bInterfaceClass = 0x03,
  565. .bInterfaceSubClass = 0x00,
  566. .bInterfaceProtocol = 0x00,
  567. .iInterface = 0x00
  568. },
  569. .rawHID = {
  570. .header = {
  571. .bLength = sizeof(usbHIDDescriptor_t),
  572. .bDescriptorType = USBDESCR_HID
  573. },
  574. .bcdHID = 0x0101,
  575. .bCountryCode = 0x00,
  576. .bNumDescriptors = 2,
  577. .bDescriptorType = USBDESCR_HID_REPORT,
  578. .wDescriptorLength = sizeof(raw_hid_report)
  579. },
  580. # if USB_CFG_HAVE_INTRIN_ENDPOINT3
  581. .rawINEndpoint = {
  582. .header = {
  583. .bLength = sizeof(usbEndpointDescriptor_t),
  584. .bDescriptorType = USBDESCR_ENDPOINT
  585. },
  586. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  587. .bmAttributes = 0x03,
  588. .wMaxPacketSize = RAW_EPSIZE,
  589. .bInterval = USB_POLLING_INTERVAL_MS
  590. },
  591. .rawOUTEndpoint = {
  592. .header = {
  593. .bLength = sizeof(usbEndpointDescriptor_t),
  594. .bDescriptorType = USBDESCR_ENDPOINT
  595. },
  596. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP3_NUMBER),
  597. .bmAttributes = 0x03,
  598. .wMaxPacketSize = RAW_EPSIZE,
  599. .bInterval = USB_POLLING_INTERVAL_MS
  600. }
  601. # endif
  602. # endif
  603. };
  604. #endif
  605. // clang-format on
  606. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  607. usbMsgLen_t len = 0;
  608. /*
  609. debug("usbFunctionDescriptor: ");
  610. debug_hex(rq->bmRequestType); debug(" ");
  611. debug_hex(rq->bRequest); debug(" ");
  612. debug_hex16(rq->wValue.word); debug(" ");
  613. debug_hex16(rq->wIndex.word); debug(" ");
  614. debug_hex16(rq->wLength.word); debug("\n");
  615. */
  616. switch (rq->wValue.bytes[1]) {
  617. #if USB_CFG_DESCR_PROPS_DEVICE
  618. case USBDESCR_DEVICE:
  619. usbMsgPtr = (unsigned char *)&usbDeviceDescriptor;
  620. len = sizeof(usbDeviceDescriptor_t);
  621. break;
  622. #endif
  623. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  624. case USBDESCR_CONFIG:
  625. usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor;
  626. len = sizeof(usbConfigurationDescriptor_t);
  627. break;
  628. #endif
  629. case USBDESCR_STRING:
  630. switch (rq->wValue.bytes[0]) {
  631. case 0:
  632. usbMsgPtr = (unsigned char *)&usbStringDescriptorZero;
  633. len = usbStringDescriptorZero.header.bLength;
  634. break;
  635. case 1: // iManufacturer
  636. usbMsgPtr = (unsigned char *)&usbStringDescriptorManufacturer;
  637. len = usbStringDescriptorManufacturer.header.bLength;
  638. break;
  639. case 2: // iProduct
  640. usbMsgPtr = (unsigned char *)&usbStringDescriptorProduct;
  641. len = usbStringDescriptorProduct.header.bLength;
  642. break;
  643. case 3: // iSerialNumber
  644. usbMsgPtr = (unsigned char *)&usbStringDescriptorSerial;
  645. len = usbStringDescriptorSerial.header.bLength;
  646. break;
  647. }
  648. break;
  649. case USBDESCR_HID:
  650. switch (rq->wValue.bytes[0]) {
  651. case 0:
  652. usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.keyboardHID;
  653. len = sizeof(usbHIDDescriptor_t);
  654. break;
  655. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  656. case 1:
  657. usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.mouseExtraHID;
  658. len = sizeof(usbHIDDescriptor_t);
  659. break;
  660. #elif defined(RAW_ENABLE)
  661. case 1:
  662. usbMsgPtr = (unsigned char *)&usbConfigurationDescriptor.rawHID;
  663. len = sizeof(usbHIDDescriptor_t);
  664. break;
  665. #endif
  666. }
  667. break;
  668. case USBDESCR_HID_REPORT:
  669. /* interface index */
  670. switch (rq->wIndex.word) {
  671. case 0:
  672. usbMsgPtr = (unsigned char *)keyboard_hid_report;
  673. len = sizeof(keyboard_hid_report);
  674. break;
  675. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  676. case 1:
  677. usbMsgPtr = (unsigned char *)mouse_extra_hid_report;
  678. len = sizeof(mouse_extra_hid_report);
  679. break;
  680. #elif defined(RAW_ENABLE)
  681. case 1:
  682. usbMsgPtr = (unsigned char *)raw_hid_report;
  683. len = sizeof(raw_hid_report);
  684. break;
  685. #endif
  686. }
  687. break;
  688. }
  689. // debug("desc len: "); debug_hex(len); debug("\n");
  690. return len;
  691. }