vusb.c 30 KB

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