vusb.c 30 KB

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