vusb.c 29 KB

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