vusb.c 30 KB

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