You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

889 lines
29 KiB

  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <avr/wdt.h>
  16. #include <usbdrv/usbdrv.h>
  17. #include "usbconfig.h"
  18. #include "host.h"
  19. #include "report.h"
  20. #include "host_driver.h"
  21. #include "vusb.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "wait.h"
  25. #include "usb_descriptor_common.h"
  26. #ifdef RAW_ENABLE
  27. # include "raw_hid.h"
  28. #endif
  29. #if defined(CONSOLE_ENABLE)
  30. # define RBUF_SIZE 128
  31. # include "ring_buffer.h"
  32. #endif
  33. #define NEXT_INTERFACE __COUNTER__
  34. /*
  35. * Interface indexes
  36. */
  37. enum usb_interfaces {
  38. KEYBOARD_INTERFACE = NEXT_INTERFACE,
  39. // It is important that the Raw HID interface is at a constant
  40. // interface number, to support Linux/OSX platforms and chrome.hid
  41. // If Raw HID is enabled, let it be always 1.
  42. #ifdef RAW_ENABLE
  43. RAW_INTERFACE = NEXT_INTERFACE,
  44. #endif
  45. #if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE))
  46. MOUSE_EXTRA_INTERFACE = NEXT_INTERFACE,
  47. #endif
  48. #ifdef CONSOLE_ENABLE
  49. CONSOLE_INTERFACE = NEXT_INTERFACE,
  50. #endif
  51. TOTAL_INTERFACES = NEXT_INTERFACE
  52. };
  53. #define MAX_INTERFACES 3
  54. #if (NEXT_INTERFACE - 1) > MAX_INTERFACES
  55. # error 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
  56. #endif
  57. #if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)) && CONSOLE_ENABLE
  58. # error Mouse/Extra Keys share an endpoint with Console. Please disable one of the two.
  59. #endif
  60. static uint8_t keyboard_led_state = 0;
  61. static uint8_t vusb_idle_rate = 0;
  62. /* Keyboard report send buffer */
  63. #define KBUF_SIZE 16
  64. static report_keyboard_t kbuf[KBUF_SIZE];
  65. static uint8_t kbuf_head = 0;
  66. static uint8_t kbuf_tail = 0;
  67. static report_keyboard_t keyboard_report_sent;
  68. #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
  69. /* transfer keyboard report from buffer */
  70. void vusb_transfer_keyboard(void) {
  71. for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
  72. if (usbInterruptIsReady()) {
  73. if (kbuf_head != kbuf_tail) {
  74. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  75. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  76. if (debug_keyboard) {
  77. dprintf("V-USB: kbuf[%d->%d](%02X)\n", kbuf_tail, kbuf_head, (kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
  78. }
  79. }
  80. break;
  81. }
  82. usbPoll();
  83. wait_ms(1);
  84. }
  85. }
  86. /*------------------------------------------------------------------*
  87. * RAW HID
  88. *------------------------------------------------------------------*/
  89. #ifdef RAW_ENABLE
  90. # define RAW_BUFFER_SIZE 32
  91. # define RAW_EPSIZE 8
  92. static uint8_t raw_output_buffer[RAW_BUFFER_SIZE];
  93. static uint8_t raw_output_received_bytes = 0;
  94. void raw_hid_send(uint8_t *data, uint8_t length) {
  95. if (length != RAW_BUFFER_SIZE) {
  96. return;
  97. }
  98. uint8_t *temp = data;
  99. for (uint8_t i = 0; i < 4; i++) {
  100. while (!usbInterruptIsReady4()) {
  101. usbPoll();
  102. }
  103. usbSetInterrupt4(temp, 8);
  104. temp += 8;
  105. }
  106. while (!usbInterruptIsReady4()) {
  107. usbPoll();
  108. }
  109. usbSetInterrupt4(0, 0);
  110. }
  111. __attribute__((weak)) void raw_hid_receive(uint8_t *data, uint8_t length) {
  112. // Users should #include "raw_hid.h" in their own code
  113. // and implement this function there. Leave this as weak linkage
  114. // so users can opt to not handle data coming in.
  115. }
  116. void raw_hid_task(void) {
  117. if (raw_output_received_bytes == RAW_BUFFER_SIZE) {
  118. raw_hid_receive(raw_output_buffer, RAW_BUFFER_SIZE);
  119. raw_output_received_bytes = 0;
  120. }
  121. }
  122. #endif
  123. /*------------------------------------------------------------------*
  124. * Console
  125. *------------------------------------------------------------------*/
  126. #ifdef CONSOLE_ENABLE
  127. # define CONSOLE_BUFFER_SIZE 32
  128. # define CONSOLE_EPSIZE 8
  129. int8_t sendchar(uint8_t c) {
  130. rbuf_enqueue(c);
  131. return 0;
  132. }
  133. static inline bool usbSendData3(char *data, uint8_t len) {
  134. uint8_t retries = 5;
  135. while (!usbInterruptIsReady3()) {
  136. if (!(retries--)) {
  137. return false;
  138. }
  139. usbPoll();
  140. }
  141. usbSetInterrupt3((unsigned char *)data, len);
  142. return true;
  143. }
  144. void console_task(void) {
  145. if (!usbConfiguration) {
  146. return;
  147. }
  148. if (!rbuf_has_data()) {
  149. return;
  150. }
  151. // Send in chunks of 8 padded to 32
  152. char send_buf[CONSOLE_BUFFER_SIZE] = {0};
  153. uint8_t send_buf_count = 0;
  154. while (rbuf_has_data() && send_buf_count < CONSOLE_EPSIZE) {
  155. send_buf[send_buf_count++] = rbuf_dequeue();
  156. }
  157. char *temp = send_buf;
  158. for (uint8_t i = 0; i < 4; i++) {
  159. if (!usbSendData3(temp, 8)) {
  160. break;
  161. }
  162. temp += 8;
  163. }
  164. usbSendData3(0, 0);
  165. usbPoll();
  166. }
  167. #endif
  168. /*------------------------------------------------------------------*
  169. * Host driver
  170. *------------------------------------------------------------------*/
  171. static uint8_t keyboard_leds(void);
  172. static void send_keyboard(report_keyboard_t *report);
  173. static void send_mouse(report_mouse_t *report);
  174. static void send_system(uint16_t data);
  175. static void send_consumer(uint16_t data);
  176. static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  177. host_driver_t *vusb_driver(void) { return &driver; }
  178. static uint8_t keyboard_leds(void) { return keyboard_led_state; }
  179. static void send_keyboard(report_keyboard_t *report) {
  180. uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
  181. if (next != kbuf_tail) {
  182. kbuf[kbuf_head] = *report;
  183. kbuf_head = next;
  184. } else {
  185. dprint("kbuf: full\n");
  186. }
  187. // NOTE: send key strokes of Macro
  188. usbPoll();
  189. vusb_transfer_keyboard();
  190. keyboard_report_sent = *report;
  191. }
  192. typedef struct {
  193. uint8_t report_id;
  194. report_mouse_t report;
  195. } __attribute__((packed)) vusb_mouse_report_t;
  196. static void send_mouse(report_mouse_t *report) {
  197. #ifdef MOUSE_ENABLE
  198. vusb_mouse_report_t r = {.report_id = REPORT_ID_MOUSE, .report = *report};
  199. if (usbInterruptIsReady3()) {
  200. usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
  201. }
  202. #endif
  203. }
  204. #ifdef EXTRAKEY_ENABLE
  205. static void send_extra(uint8_t report_id, uint16_t data) {
  206. static uint8_t last_id = 0;
  207. static uint16_t last_data = 0;
  208. if ((report_id == last_id) && (data == last_data)) return;
  209. last_id = report_id;
  210. last_data = data;
  211. report_extra_t report = {.report_id = report_id, .usage = data};
  212. if (usbInterruptIsReady3()) {
  213. usbSetInterrupt3((void *)&report, sizeof(report));
  214. }
  215. }
  216. #endif
  217. static void send_system(uint16_t data) {
  218. #ifdef EXTRAKEY_ENABLE
  219. send_extra(REPORT_ID_SYSTEM, data);
  220. #endif
  221. }
  222. static void send_consumer(uint16_t data) {
  223. #ifdef EXTRAKEY_ENABLE
  224. send_extra(REPORT_ID_CONSUMER, data);
  225. #endif
  226. }
  227. /*------------------------------------------------------------------*
  228. * Request from host *
  229. *------------------------------------------------------------------*/
  230. static struct {
  231. uint16_t len;
  232. enum { NONE, SET_LED } kind;
  233. } last_req;
  234. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  235. usbRequest_t *rq = (void *)data;
  236. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  237. if (rq->bRequest == USBRQ_HID_GET_REPORT) {
  238. dprint("GET_REPORT:");
  239. if (rq->wIndex.word == KEYBOARD_INTERFACE) {
  240. usbMsgPtr = (usbMsgPtr_t)&keyboard_report_sent;
  241. return sizeof(keyboard_report_sent);
  242. }
  243. } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
  244. dprint("GET_IDLE:");
  245. usbMsgPtr = (usbMsgPtr_t)&vusb_idle_rate;
  246. return 1;
  247. } else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
  248. vusb_idle_rate = rq->wValue.bytes[1];
  249. dprintf("SET_IDLE: %02X", vusb_idle_rate);
  250. } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
  251. dprint("SET_REPORT:");
  252. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  253. if (rq->wValue.word == 0x0200 && rq->wIndex.word == KEYBOARD_INTERFACE) {
  254. dprint("SET_LED:");
  255. last_req.kind = SET_LED;
  256. last_req.len = rq->wLength.word;
  257. }
  258. return USB_NO_MSG; // to get data in usbFunctionWrite
  259. } else {
  260. dprint("UNKNOWN:");
  261. }
  262. } else {
  263. dprint("VENDOR:");
  264. /* no vendor specific requests implemented */
  265. }
  266. dprint("\n");
  267. return 0; /* default for not implemented requests: return no data back to host */
  268. }
  269. uchar usbFunctionWrite(uchar *data, uchar len) {
  270. if (last_req.len == 0) {
  271. return -1;
  272. }
  273. switch (last_req.kind) {
  274. case SET_LED:
  275. dprintf("SET_LED: %02X\n", data[0]);
  276. keyboard_led_state = data[0];
  277. last_req.len = 0;
  278. return 1;
  279. break;
  280. case NONE:
  281. default:
  282. return -1;
  283. break;
  284. }
  285. return 1;
  286. }
  287. void usbFunctionWriteOut(uchar *data, uchar len) {
  288. #ifdef RAW_ENABLE
  289. // Data from host must be divided every 8bytes
  290. if (len != 8) {
  291. dprint("RAW: invalid length\n");
  292. raw_output_received_bytes = 0;
  293. return;
  294. }
  295. if (raw_output_received_bytes + len > RAW_BUFFER_SIZE) {
  296. dprint("RAW: buffer full\n");
  297. raw_output_received_bytes = 0;
  298. } else {
  299. for (uint8_t i = 0; i < 8; i++) {
  300. raw_output_buffer[raw_output_received_bytes + i] = data[i];
  301. }
  302. raw_output_received_bytes += len;
  303. }
  304. #endif
  305. }
  306. /*------------------------------------------------------------------*
  307. * Descriptors *
  308. *------------------------------------------------------------------*/
  309. const PROGMEM uchar keyboard_hid_report[] = {
  310. 0x05, 0x01, // Usage Page (Generic Desktop)
  311. 0x09, 0x06, // Usage (Keyboard)
  312. 0xA1, 0x01, // Collection (Application)
  313. // Modifiers (8 bits)
  314. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  315. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  316. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  317. 0x15, 0x00, // Logical Minimum (0)
  318. 0x25, 0x01, // Logical Maximum (1)
  319. 0x95, 0x08, // Report Count (8)
  320. 0x75, 0x01, // Report Size (1)
  321. 0x81, 0x02, // Input (Data, Variable, Absolute)
  322. // Reserved (1 byte)
  323. 0x95, 0x01, // Report Count (1)
  324. 0x75, 0x08, // Report Size (8)
  325. 0x81, 0x03, // Input (Constant)
  326. // Keycodes (6 bytes)
  327. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  328. 0x19, 0x00, // Usage Minimum (0)
  329. 0x29, 0xFF, // Usage Maximum (255)
  330. 0x15, 0x00, // Logical Minimum (0)
  331. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  332. 0x95, 0x06, // Report Count (6)
  333. 0x75, 0x08, // Report Size (8)
  334. 0x81, 0x00, // Input (Data, Array, Absolute)
  335. // Status LEDs (5 bits)
  336. 0x05, 0x08, // Usage Page (LED)
  337. 0x19, 0x01, // Usage Minimum (Num Lock)
  338. 0x29, 0x05, // Usage Maximum (Kana)
  339. 0x95, 0x05, // Report Count (5)
  340. 0x75, 0x01, // Report Size (1)
  341. 0x91, 0x02, // Output (Data, Variable, Absolute)
  342. // LED padding (3 bits)
  343. 0x95, 0x01, // Report Count (1)
  344. 0x75, 0x03, // Report Size (3)
  345. 0x91, 0x03, // Output (Constant)
  346. 0xC0 // End Collection
  347. };
  348. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  349. const PROGMEM uchar mouse_extra_hid_report[] = {
  350. # ifdef MOUSE_ENABLE
  351. // Mouse report descriptor
  352. 0x05, 0x01, // Usage Page (Generic Desktop)
  353. 0x09, 0x02, // Usage (Mouse)
  354. 0xA1, 0x01, // Collection (Application)
  355. 0x85, REPORT_ID_MOUSE, // Report ID
  356. 0x09, 0x01, // Usage (Pointer)
  357. 0xA1, 0x00, // Collection (Physical)
  358. // Buttons (5 bits)
  359. 0x05, 0x09, // Usage Page (Button)
  360. 0x19, 0x01, // Usage Minimum (Button 1)
  361. 0x29, 0x05, // Usage Maximum (Button 5)
  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. 0x81, 0x02, // Input (Data, Variable, Absolute)
  367. // Button padding (3 bits)
  368. 0x95, 0x01, // Report Count (1)
  369. 0x75, 0x03, // Report Size (3)
  370. 0x81, 0x03, // Input (Constant)
  371. // X/Y position (2 bytes)
  372. 0x05, 0x01, // Usage Page (Generic Desktop)
  373. 0x09, 0x30, // Usage (X)
  374. 0x09, 0x31, // Usage (Y)
  375. 0x15, 0x81, // Logical Minimum (-127)
  376. 0x25, 0x7F, // Logical Maximum (127)
  377. 0x95, 0x02, // Report Count (2)
  378. 0x75, 0x08, // Report Size (8)
  379. 0x81, 0x06, // Input (Data, Variable, Relative)
  380. // Vertical wheel (1 byte)
  381. 0x09, 0x38, // Usage (Wheel)
  382. 0x15, 0x81, // Logical Minimum (-127)
  383. 0x25, 0x7F, // Logical Maximum (127)
  384. 0x95, 0x01, // Report Count (1)
  385. 0x75, 0x08, // Report Size (8)
  386. 0x81, 0x06, // Input (Data, Variable, Relative)
  387. // Horizontal wheel (1 byte)
  388. 0x05, 0x0C, // Usage Page (Consumer)
  389. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  390. 0x15, 0x81, // Logical Minimum (-127)
  391. 0x25, 0x7F, // Logical Maximum (127)
  392. 0x95, 0x01, // Report Count (1)
  393. 0x75, 0x08, // Report Size (8)
  394. 0x81, 0x06, // Input (Data, Variable, Relative)
  395. 0xC0, // End Collection
  396. 0xC0, // End Collection
  397. # endif
  398. # ifdef EXTRAKEY_ENABLE
  399. // Extrakeys report descriptor
  400. 0x05, 0x01, // Usage Page (Generic Desktop)
  401. 0x09, 0x80, // Usage (System Control)
  402. 0xA1, 0x01, // Collection (Application)
  403. 0x85, REPORT_ID_SYSTEM, // Report ID
  404. 0x19, 0x01, // Usage Minimum (Pointer)
  405. 0x2A, 0xB7, 0x00, // Usage Maximum (System Display LCD Autoscale)
  406. 0x15, 0x01, // Logical Minimum
  407. 0x26, 0xB7, 0x00, // Logical Maximum
  408. 0x95, 0x01, // Report Count (1)
  409. 0x75, 0x10, // Report Size (16)
  410. 0x81, 0x00, // Input (Data, Array, Absolute)
  411. 0xC0, // End Collection
  412. 0x05, 0x0C, // Usage Page (Consumer)
  413. 0x09, 0x01, // Usage (Consumer Control)
  414. 0xA1, 0x01, // Collection (Application)
  415. 0x85, REPORT_ID_CONSUMER, // Report ID
  416. 0x19, 0x01, // Usage Minimum (Consumer Control)
  417. 0x2A, 0xA0, 0x02, // Usage Maximum (AC Desktop Show All Applications)
  418. 0x15, 0x01, // Logical Minimum
  419. 0x26, 0xA0, 0x02, // Logical Maximum
  420. 0x95, 0x01, // Report Count (1)
  421. 0x75, 0x10, // Report Size (16)
  422. 0x81, 0x00, // Input (Data, Array, Absolute)
  423. 0xC0 // End Collection
  424. # endif
  425. };
  426. #endif
  427. #ifdef RAW_ENABLE
  428. const PROGMEM uchar raw_hid_report[] = {
  429. 0x06, RAW_USAGE_PAGE_LO, RAW_USAGE_PAGE_HI, // Usage Page (Vendor Defined)
  430. 0x09, RAW_USAGE_ID, // Usage (Vendor Defined)
  431. 0xA1, 0x01, // Collection (Application)
  432. // Data to host
  433. 0x09, 0x62, // Usage (Vendor Defined)
  434. 0x15, 0x00, // Logical Minimum (0)
  435. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  436. 0x95, RAW_BUFFER_SIZE, // Report Count
  437. 0x75, 0x08, // Report Size (8)
  438. 0x81, 0x02, // Input (Data, Variable, Absolute)
  439. // Data from host
  440. 0x09, 0x63, // Usage (Vendor Defined)
  441. 0x15, 0x00, // Logical Minimum (0)
  442. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  443. 0x95, RAW_BUFFER_SIZE, // Report Count
  444. 0x75, 0x08, // Report Size (8)
  445. 0x91, 0x02, // Output (Data, Variable, Absolute)
  446. 0xC0 // End Collection
  447. };
  448. #endif
  449. #if defined(CONSOLE_ENABLE)
  450. const PROGMEM uchar console_hid_report[] = {
  451. 0x06, 0x31, 0xFF, // Usage Page (Vendor Defined - PJRC Teensy compatible)
  452. 0x09, 0x74, // Usage (Vendor Defined - PJRC Teensy compatible)
  453. 0xA1, 0x01, // Collection (Application)
  454. // Data to host
  455. 0x09, 0x75, // Usage (Vendor Defined)
  456. 0x15, 0x00, // Logical Minimum (0x00)
  457. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  458. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  459. 0x75, 0x08, // Report Size (8)
  460. 0x81, 0x02, // Input (Data, Variable, Absolute)
  461. // Data from host
  462. 0x09, 0x76, // Usage (Vendor Defined)
  463. 0x15, 0x00, // Logical Minimum (0x00)
  464. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  465. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  466. 0x75, 0x08, // Report Size (8)
  467. 0x91, 0x02, // Output (Data)
  468. 0xC0 // End Collection
  469. };
  470. #endif
  471. #ifndef USB_MAX_POWER_CONSUMPTION
  472. # define USB_MAX_POWER_CONSUMPTION 500
  473. #endif
  474. // TODO: change this to 10ms to match LUFA
  475. #ifndef USB_POLLING_INTERVAL_MS
  476. # define USB_POLLING_INTERVAL_MS 1
  477. #endif
  478. // clang-format off
  479. const PROGMEM usbStringDescriptor_t usbStringDescriptorZero = {
  480. .header = {
  481. .bLength = USB_STRING_LEN(1),
  482. .bDescriptorType = USBDESCR_STRING
  483. },
  484. .bString = {0x0409} // US English
  485. };
  486. const PROGMEM usbStringDescriptor_t usbStringDescriptorManufacturer = {
  487. .header = {
  488. .bLength = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1),
  489. .bDescriptorType = USBDESCR_STRING
  490. },
  491. .bString = LSTR(MANUFACTURER)
  492. };
  493. const PROGMEM usbStringDescriptor_t usbStringDescriptorProduct = {
  494. .header = {
  495. .bLength = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1),
  496. .bDescriptorType = USBDESCR_STRING
  497. },
  498. .bString = LSTR(PRODUCT)
  499. };
  500. #if defined(SERIAL_NUMBER)
  501. const PROGMEM usbStringDescriptor_t usbStringDescriptorSerial = {
  502. .header = {
  503. .bLength = USB_STRING_LEN(sizeof(STR(SERIAL_NUMBER)) - 1),
  504. .bDescriptorType = USBDESCR_STRING
  505. },
  506. .bString = LSTR(SERIAL_NUMBER)
  507. };
  508. #endif
  509. /*
  510. * Device descriptor
  511. */
  512. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  513. .header = {
  514. .bLength = sizeof(usbDeviceDescriptor_t),
  515. .bDescriptorType = USBDESCR_DEVICE
  516. },
  517. .bcdUSB = 0x0110,
  518. .bDeviceClass = 0x00,
  519. .bDeviceSubClass = 0x00,
  520. .bDeviceProtocol = 0x00,
  521. .bMaxPacketSize0 = 8,
  522. .idVendor = VENDOR_ID,
  523. .idProduct = PRODUCT_ID,
  524. .bcdDevice = DEVICE_VER,
  525. .iManufacturer = 0x01,
  526. .iProduct = 0x02,
  527. #if defined(SERIAL_NUMBER)
  528. .iSerialNumber = 0x03,
  529. #else
  530. .iSerialNumber = 0x00,
  531. #endif
  532. .bNumConfigurations = 1
  533. };
  534. /*
  535. * Configuration descriptors
  536. */
  537. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  538. .header = {
  539. .header = {
  540. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  541. .bDescriptorType = USBDESCR_CONFIG
  542. },
  543. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  544. .bNumInterfaces = TOTAL_INTERFACES,
  545. .bConfigurationValue = 0x01,
  546. .iConfiguration = 0x00,
  547. .bmAttributes = (1 << 7) | USBATTR_REMOTEWAKE,
  548. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  549. },
  550. /*
  551. * Keyboard
  552. */
  553. .keyboardInterface = {
  554. .header = {
  555. .bLength = sizeof(usbInterfaceDescriptor_t),
  556. .bDescriptorType = USBDESCR_INTERFACE
  557. },
  558. .bInterfaceNumber = KEYBOARD_INTERFACE,
  559. .bAlternateSetting = 0x00,
  560. .bNumEndpoints = 1,
  561. .bInterfaceClass = 0x03,
  562. .bInterfaceSubClass = 0x01,
  563. .bInterfaceProtocol = 0x01,
  564. .iInterface = 0x00
  565. },
  566. .keyboardHID = {
  567. .header = {
  568. .bLength = sizeof(usbHIDDescriptor_t),
  569. .bDescriptorType = USBDESCR_HID
  570. },
  571. .bcdHID = 0x0101,
  572. .bCountryCode = 0x00,
  573. .bNumDescriptors = 1,
  574. .bDescriptorType = USBDESCR_HID_REPORT,
  575. .wDescriptorLength = sizeof(keyboard_hid_report)
  576. },
  577. .keyboardINEndpoint = {
  578. .header = {
  579. .bLength = sizeof(usbEndpointDescriptor_t),
  580. .bDescriptorType = USBDESCR_ENDPOINT
  581. },
  582. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  583. .bmAttributes = 0x03,
  584. .wMaxPacketSize = 8,
  585. .bInterval = USB_POLLING_INTERVAL_MS
  586. },
  587. # if defined(RAW_ENABLE)
  588. /*
  589. * RAW HID
  590. */
  591. .rawInterface = {
  592. .header = {
  593. .bLength = sizeof(usbInterfaceDescriptor_t),
  594. .bDescriptorType = USBDESCR_INTERFACE
  595. },
  596. .bInterfaceNumber = RAW_INTERFACE,
  597. .bAlternateSetting = 0x00,
  598. .bNumEndpoints = 2,
  599. .bInterfaceClass = 0x03,
  600. .bInterfaceSubClass = 0x00,
  601. .bInterfaceProtocol = 0x00,
  602. .iInterface = 0x00
  603. },
  604. .rawHID = {
  605. .header = {
  606. .bLength = sizeof(usbHIDDescriptor_t),
  607. .bDescriptorType = USBDESCR_HID
  608. },
  609. .bcdHID = 0x0101,
  610. .bCountryCode = 0x00,
  611. .bNumDescriptors = 1,
  612. .bDescriptorType = USBDESCR_HID_REPORT,
  613. .wDescriptorLength = sizeof(raw_hid_report)
  614. },
  615. .rawINEndpoint = {
  616. .header = {
  617. .bLength = sizeof(usbEndpointDescriptor_t),
  618. .bDescriptorType = USBDESCR_ENDPOINT
  619. },
  620. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  621. .bmAttributes = 0x03,
  622. .wMaxPacketSize = RAW_EPSIZE,
  623. .bInterval = USB_POLLING_INTERVAL_MS
  624. },
  625. .rawOUTEndpoint = {
  626. .header = {
  627. .bLength = sizeof(usbEndpointDescriptor_t),
  628. .bDescriptorType = USBDESCR_ENDPOINT
  629. },
  630. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  631. .bmAttributes = 0x03,
  632. .wMaxPacketSize = RAW_EPSIZE,
  633. .bInterval = USB_POLLING_INTERVAL_MS
  634. },
  635. # endif
  636. # if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  637. /*
  638. * Mouse/Extrakeys
  639. */
  640. .mouseExtraInterface = {
  641. .header = {
  642. .bLength = sizeof(usbInterfaceDescriptor_t),
  643. .bDescriptorType = USBDESCR_INTERFACE
  644. },
  645. .bInterfaceNumber = MOUSE_EXTRA_INTERFACE,
  646. .bAlternateSetting = 0x00,
  647. .bNumEndpoints = 1,
  648. .bInterfaceClass = 0x03,
  649. .bInterfaceSubClass = 0x00,
  650. .bInterfaceProtocol = 0x00,
  651. .iInterface = 0x00
  652. },
  653. .mouseExtraHID = {
  654. .header = {
  655. .bLength = sizeof(usbHIDDescriptor_t),
  656. .bDescriptorType = USBDESCR_HID
  657. },
  658. .bcdHID = 0x0101,
  659. .bCountryCode = 0x00,
  660. .bNumDescriptors = 1,
  661. .bDescriptorType = USBDESCR_HID_REPORT,
  662. .wDescriptorLength = sizeof(mouse_extra_hid_report)
  663. },
  664. .mouseExtraINEndpoint = {
  665. .header = {
  666. .bLength = sizeof(usbEndpointDescriptor_t),
  667. .bDescriptorType = USBDESCR_ENDPOINT
  668. },
  669. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  670. .bmAttributes = 0x03,
  671. .wMaxPacketSize = 8,
  672. .bInterval = USB_POLLING_INTERVAL_MS
  673. },
  674. # endif
  675. # if defined(CONSOLE_ENABLE)
  676. /*
  677. * Console
  678. */
  679. .consoleInterface = {
  680. .header = {
  681. .bLength = sizeof(usbInterfaceDescriptor_t),
  682. .bDescriptorType = USBDESCR_INTERFACE
  683. },
  684. .bInterfaceNumber = CONSOLE_INTERFACE,
  685. .bAlternateSetting = 0x00,
  686. .bNumEndpoints = 2,
  687. .bInterfaceClass = 0x03,
  688. .bInterfaceSubClass = 0x00,
  689. .bInterfaceProtocol = 0x00,
  690. .iInterface = 0x00
  691. },
  692. .consoleHID = {
  693. .header = {
  694. .bLength = sizeof(usbHIDDescriptor_t),
  695. .bDescriptorType = USBDESCR_HID
  696. },
  697. .bcdHID = 0x0111,
  698. .bCountryCode = 0x00,
  699. .bNumDescriptors = 1,
  700. .bDescriptorType = USBDESCR_HID_REPORT,
  701. .wDescriptorLength = sizeof(console_hid_report)
  702. },
  703. .consoleINEndpoint = {
  704. .header = {
  705. .bLength = sizeof(usbEndpointDescriptor_t),
  706. .bDescriptorType = USBDESCR_ENDPOINT
  707. },
  708. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  709. .bmAttributes = 0x03,
  710. .wMaxPacketSize = CONSOLE_EPSIZE,
  711. .bInterval = 0x01
  712. },
  713. .consoleOUTEndpoint = {
  714. .header = {
  715. .bLength = sizeof(usbEndpointDescriptor_t),
  716. .bDescriptorType = USBDESCR_ENDPOINT
  717. },
  718. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP3_NUMBER),
  719. .bmAttributes = 0x03,
  720. .wMaxPacketSize = CONSOLE_EPSIZE,
  721. .bInterval = 0x01
  722. },
  723. # endif
  724. };
  725. // clang-format on
  726. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  727. usbMsgLen_t len = 0;
  728. switch (rq->wValue.bytes[1]) {
  729. case USBDESCR_DEVICE:
  730. usbMsgPtr = (usbMsgPtr_t)&usbDeviceDescriptor;
  731. len = sizeof(usbDeviceDescriptor_t);
  732. break;
  733. case USBDESCR_CONFIG:
  734. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor;
  735. len = sizeof(usbConfigurationDescriptor_t);
  736. break;
  737. case USBDESCR_STRING:
  738. switch (rq->wValue.bytes[0]) {
  739. case 0:
  740. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorZero;
  741. len = usbStringDescriptorZero.header.bLength;
  742. break;
  743. case 1: // iManufacturer
  744. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorManufacturer;
  745. len = usbStringDescriptorManufacturer.header.bLength;
  746. break;
  747. case 2: // iProduct
  748. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorProduct;
  749. len = usbStringDescriptorProduct.header.bLength;
  750. break;
  751. #if defined(SERIAL_NUMBER)
  752. case 3: // iSerialNumber
  753. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorSerial;
  754. len = usbStringDescriptorSerial.header.bLength;
  755. break;
  756. #endif
  757. }
  758. break;
  759. case USBDESCR_HID:
  760. switch (rq->wValue.bytes[0]) {
  761. case KEYBOARD_INTERFACE:
  762. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID;
  763. len = sizeof(usbHIDDescriptor_t);
  764. break;
  765. #if defined(RAW_ENABLE)
  766. case RAW_INTERFACE:
  767. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID;
  768. len = sizeof(usbHIDDescriptor_t);
  769. break;
  770. #endif
  771. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  772. case MOUSE_EXTRA_INTERFACE:
  773. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.mouseExtraHID;
  774. len = sizeof(usbHIDDescriptor_t);
  775. break;
  776. #endif
  777. #if defined(CONSOLE_ENABLE)
  778. case CONSOLE_INTERFACE:
  779. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID;
  780. len = sizeof(usbHIDDescriptor_t);
  781. break;
  782. #endif
  783. }
  784. break;
  785. case USBDESCR_HID_REPORT:
  786. /* interface index */
  787. switch (rq->wIndex.word) {
  788. case KEYBOARD_INTERFACE:
  789. usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report;
  790. len = sizeof(keyboard_hid_report);
  791. break;
  792. #if defined(RAW_ENABLE)
  793. case RAW_INTERFACE:
  794. usbMsgPtr = (usbMsgPtr_t)raw_hid_report;
  795. len = sizeof(raw_hid_report);
  796. break;
  797. #endif
  798. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  799. case MOUSE_EXTRA_INTERFACE:
  800. usbMsgPtr = (usbMsgPtr_t)mouse_extra_hid_report;
  801. len = sizeof(mouse_extra_hid_report);
  802. break;
  803. #endif
  804. #if defined(CONSOLE_ENABLE)
  805. case CONSOLE_INTERFACE:
  806. usbMsgPtr = (usbMsgPtr_t)console_hid_report;
  807. len = sizeof(console_hid_report);
  808. break;
  809. #endif
  810. }
  811. break;
  812. }
  813. return len;
  814. }