vusb.c 29 KB

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