vusb.c 30 KB

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