vusb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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 SERIAL_NUMBER
  472. # define SERIAL_NUMBER 0
  473. #endif
  474. #ifndef USB_MAX_POWER_CONSUMPTION
  475. # define USB_MAX_POWER_CONSUMPTION 500
  476. #endif
  477. // TODO: change this to 10ms to match LUFA
  478. #ifndef USB_POLLING_INTERVAL_MS
  479. # define USB_POLLING_INTERVAL_MS 1
  480. #endif
  481. // clang-format off
  482. const PROGMEM usbStringDescriptor_t usbStringDescriptorZero = {
  483. .header = {
  484. .bLength = USB_STRING_LEN(1),
  485. .bDescriptorType = USBDESCR_STRING
  486. },
  487. .bString = {0x0409} // US English
  488. };
  489. const PROGMEM usbStringDescriptor_t usbStringDescriptorManufacturer = {
  490. .header = {
  491. .bLength = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1),
  492. .bDescriptorType = USBDESCR_STRING
  493. },
  494. .bString = LSTR(MANUFACTURER)
  495. };
  496. const PROGMEM usbStringDescriptor_t usbStringDescriptorProduct = {
  497. .header = {
  498. .bLength = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1),
  499. .bDescriptorType = USBDESCR_STRING
  500. },
  501. .bString = LSTR(PRODUCT)
  502. };
  503. const PROGMEM usbStringDescriptor_t usbStringDescriptorSerial = {
  504. .header = {
  505. .bLength = USB_STRING_LEN(sizeof(STR(SERIAL_NUMBER)) - 1),
  506. .bDescriptorType = USBDESCR_STRING
  507. },
  508. .bString = LSTR(SERIAL_NUMBER)
  509. };
  510. /*
  511. * Device descriptor
  512. */
  513. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  514. .header = {
  515. .bLength = sizeof(usbDeviceDescriptor_t),
  516. .bDescriptorType = USBDESCR_DEVICE
  517. },
  518. .bcdUSB = 0x0110,
  519. .bDeviceClass = 0x00,
  520. .bDeviceSubClass = 0x00,
  521. .bDeviceProtocol = 0x00,
  522. .bMaxPacketSize0 = 8,
  523. .idVendor = VENDOR_ID,
  524. .idProduct = PRODUCT_ID,
  525. .bcdDevice = DEVICE_VER,
  526. .iManufacturer = 0x01,
  527. .iProduct = 0x02,
  528. .iSerialNumber = 0x03,
  529. .bNumConfigurations = 1
  530. };
  531. /*
  532. * Configuration descriptors
  533. */
  534. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  535. .header = {
  536. .header = {
  537. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  538. .bDescriptorType = USBDESCR_CONFIG
  539. },
  540. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  541. .bNumInterfaces = TOTAL_INTERFACES,
  542. .bConfigurationValue = 0x01,
  543. .iConfiguration = 0x00,
  544. .bmAttributes = (1 << 7) | USBATTR_REMOTEWAKE,
  545. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  546. },
  547. /*
  548. * Keyboard
  549. */
  550. .keyboardInterface = {
  551. .header = {
  552. .bLength = sizeof(usbInterfaceDescriptor_t),
  553. .bDescriptorType = USBDESCR_INTERFACE
  554. },
  555. .bInterfaceNumber = KEYBOARD_INTERFACE,
  556. .bAlternateSetting = 0x00,
  557. .bNumEndpoints = 1,
  558. .bInterfaceClass = 0x03,
  559. .bInterfaceSubClass = 0x01,
  560. .bInterfaceProtocol = 0x01,
  561. .iInterface = 0x00
  562. },
  563. .keyboardHID = {
  564. .header = {
  565. .bLength = sizeof(usbHIDDescriptor_t),
  566. .bDescriptorType = USBDESCR_HID
  567. },
  568. .bcdHID = 0x0101,
  569. .bCountryCode = 0x00,
  570. .bNumDescriptors = 1,
  571. .bDescriptorType = USBDESCR_HID_REPORT,
  572. .wDescriptorLength = sizeof(keyboard_hid_report)
  573. },
  574. .keyboardINEndpoint = {
  575. .header = {
  576. .bLength = sizeof(usbEndpointDescriptor_t),
  577. .bDescriptorType = USBDESCR_ENDPOINT
  578. },
  579. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  580. .bmAttributes = 0x03,
  581. .wMaxPacketSize = 8,
  582. .bInterval = USB_POLLING_INTERVAL_MS
  583. },
  584. # if defined(RAW_ENABLE)
  585. /*
  586. * RAW HID
  587. */
  588. .rawInterface = {
  589. .header = {
  590. .bLength = sizeof(usbInterfaceDescriptor_t),
  591. .bDescriptorType = USBDESCR_INTERFACE
  592. },
  593. .bInterfaceNumber = RAW_INTERFACE,
  594. .bAlternateSetting = 0x00,
  595. .bNumEndpoints = 2,
  596. .bInterfaceClass = 0x03,
  597. .bInterfaceSubClass = 0x00,
  598. .bInterfaceProtocol = 0x00,
  599. .iInterface = 0x00
  600. },
  601. .rawHID = {
  602. .header = {
  603. .bLength = sizeof(usbHIDDescriptor_t),
  604. .bDescriptorType = USBDESCR_HID
  605. },
  606. .bcdHID = 0x0101,
  607. .bCountryCode = 0x00,
  608. .bNumDescriptors = 2,
  609. .bDescriptorType = USBDESCR_HID_REPORT,
  610. .wDescriptorLength = sizeof(raw_hid_report)
  611. },
  612. .rawINEndpoint = {
  613. .header = {
  614. .bLength = sizeof(usbEndpointDescriptor_t),
  615. .bDescriptorType = USBDESCR_ENDPOINT
  616. },
  617. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  618. .bmAttributes = 0x03,
  619. .wMaxPacketSize = RAW_EPSIZE,
  620. .bInterval = USB_POLLING_INTERVAL_MS
  621. },
  622. .rawOUTEndpoint = {
  623. .header = {
  624. .bLength = sizeof(usbEndpointDescriptor_t),
  625. .bDescriptorType = USBDESCR_ENDPOINT
  626. },
  627. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  628. .bmAttributes = 0x03,
  629. .wMaxPacketSize = RAW_EPSIZE,
  630. .bInterval = USB_POLLING_INTERVAL_MS
  631. },
  632. # endif
  633. # if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  634. /*
  635. * Mouse/Extrakeys
  636. */
  637. .mouseExtraInterface = {
  638. .header = {
  639. .bLength = sizeof(usbInterfaceDescriptor_t),
  640. .bDescriptorType = USBDESCR_INTERFACE
  641. },
  642. .bInterfaceNumber = MOUSE_EXTRA_INTERFACE,
  643. .bAlternateSetting = 0x00,
  644. .bNumEndpoints = 1,
  645. .bInterfaceClass = 0x03,
  646. .bInterfaceSubClass = 0x00,
  647. .bInterfaceProtocol = 0x00,
  648. .iInterface = 0x00
  649. },
  650. .mouseExtraHID = {
  651. .header = {
  652. .bLength = sizeof(usbHIDDescriptor_t),
  653. .bDescriptorType = USBDESCR_HID
  654. },
  655. .bcdHID = 0x0101,
  656. .bCountryCode = 0x00,
  657. .bNumDescriptors = 1,
  658. .bDescriptorType = USBDESCR_HID_REPORT,
  659. .wDescriptorLength = sizeof(mouse_extra_hid_report)
  660. },
  661. .mouseExtraINEndpoint = {
  662. .header = {
  663. .bLength = sizeof(usbEndpointDescriptor_t),
  664. .bDescriptorType = USBDESCR_ENDPOINT
  665. },
  666. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  667. .bmAttributes = 0x03,
  668. .wMaxPacketSize = 8,
  669. .bInterval = USB_POLLING_INTERVAL_MS
  670. },
  671. # endif
  672. # if defined(CONSOLE_ENABLE)
  673. /*
  674. * Console
  675. */
  676. .consoleInterface = {
  677. .header = {
  678. .bLength = sizeof(usbInterfaceDescriptor_t),
  679. .bDescriptorType = USBDESCR_INTERFACE
  680. },
  681. .bInterfaceNumber = CONSOLE_INTERFACE,
  682. .bAlternateSetting = 0x00,
  683. .bNumEndpoints = 2,
  684. .bInterfaceClass = 0x03,
  685. .bInterfaceSubClass = 0x00,
  686. .bInterfaceProtocol = 0x00,
  687. .iInterface = 0x00
  688. },
  689. .consoleHID = {
  690. .header = {
  691. .bLength = sizeof(usbHIDDescriptor_t),
  692. .bDescriptorType = USBDESCR_HID
  693. },
  694. .bcdHID = 0x0111,
  695. .bCountryCode = 0x00,
  696. .bNumDescriptors = 1,
  697. .bDescriptorType = USBDESCR_HID_REPORT,
  698. .wDescriptorLength = sizeof(console_hid_report)
  699. },
  700. .consoleINEndpoint = {
  701. .header = {
  702. .bLength = sizeof(usbEndpointDescriptor_t),
  703. .bDescriptorType = USBDESCR_ENDPOINT
  704. },
  705. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  706. .bmAttributes = 0x03,
  707. .wMaxPacketSize = CONSOLE_EPSIZE,
  708. .bInterval = 0x01
  709. },
  710. .consoleOUTEndpoint = {
  711. .header = {
  712. .bLength = sizeof(usbEndpointDescriptor_t),
  713. .bDescriptorType = USBDESCR_ENDPOINT
  714. },
  715. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP3_NUMBER),
  716. .bmAttributes = 0x03,
  717. .wMaxPacketSize = CONSOLE_EPSIZE,
  718. .bInterval = 0x01
  719. },
  720. # endif
  721. };
  722. // clang-format on
  723. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  724. usbMsgLen_t len = 0;
  725. switch (rq->wValue.bytes[1]) {
  726. case USBDESCR_DEVICE:
  727. usbMsgPtr = (usbMsgPtr_t)&usbDeviceDescriptor;
  728. len = sizeof(usbDeviceDescriptor_t);
  729. break;
  730. case USBDESCR_CONFIG:
  731. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor;
  732. len = sizeof(usbConfigurationDescriptor_t);
  733. break;
  734. case USBDESCR_STRING:
  735. switch (rq->wValue.bytes[0]) {
  736. case 0:
  737. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorZero;
  738. len = usbStringDescriptorZero.header.bLength;
  739. break;
  740. case 1: // iManufacturer
  741. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorManufacturer;
  742. len = usbStringDescriptorManufacturer.header.bLength;
  743. break;
  744. case 2: // iProduct
  745. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorProduct;
  746. len = usbStringDescriptorProduct.header.bLength;
  747. break;
  748. case 3: // iSerialNumber
  749. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorSerial;
  750. len = usbStringDescriptorSerial.header.bLength;
  751. break;
  752. }
  753. break;
  754. case USBDESCR_HID:
  755. switch (rq->wValue.bytes[0]) {
  756. case KEYBOARD_INTERFACE:
  757. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID;
  758. len = sizeof(usbHIDDescriptor_t);
  759. break;
  760. #if defined(RAW_ENABLE)
  761. case RAW_INTERFACE:
  762. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID;
  763. len = sizeof(usbHIDDescriptor_t);
  764. break;
  765. #endif
  766. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  767. case MOUSE_EXTRA_INTERFACE:
  768. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.mouseExtraHID;
  769. len = sizeof(usbHIDDescriptor_t);
  770. break;
  771. #endif
  772. #if defined(CONSOLE_ENABLE)
  773. case CONSOLE_INTERFACE:
  774. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID;
  775. len = sizeof(usbHIDDescriptor_t);
  776. break;
  777. #endif
  778. }
  779. break;
  780. case USBDESCR_HID_REPORT:
  781. /* interface index */
  782. switch (rq->wIndex.word) {
  783. case KEYBOARD_INTERFACE:
  784. usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report;
  785. len = sizeof(keyboard_hid_report);
  786. break;
  787. #if defined(RAW_ENABLE)
  788. case RAW_INTERFACE:
  789. usbMsgPtr = (usbMsgPtr_t)raw_hid_report;
  790. len = sizeof(raw_hid_report);
  791. break;
  792. #endif
  793. #if defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)
  794. case MOUSE_EXTRA_INTERFACE:
  795. usbMsgPtr = (usbMsgPtr_t)mouse_extra_hid_report;
  796. len = sizeof(mouse_extra_hid_report);
  797. break;
  798. #endif
  799. #if defined(CONSOLE_ENABLE)
  800. case CONSOLE_INTERFACE:
  801. usbMsgPtr = (usbMsgPtr_t)console_hid_report;
  802. len = sizeof(console_hid_report);
  803. break;
  804. #endif
  805. }
  806. break;
  807. }
  808. return len;
  809. }