vusb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. static report_extra_t report;
  228. report = (report_extra_t){.report_id = report_id, .usage = data};
  229. if (usbInterruptIsReadyShared()) {
  230. usbSetInterruptShared((void *)&report, sizeof(report_extra_t));
  231. }
  232. }
  233. #endif
  234. static void send_system(uint16_t data) {
  235. #ifdef EXTRAKEY_ENABLE
  236. send_extra(REPORT_ID_SYSTEM, data);
  237. #endif
  238. }
  239. static void send_consumer(uint16_t data) {
  240. #ifdef EXTRAKEY_ENABLE
  241. send_extra(REPORT_ID_CONSUMER, data);
  242. #endif
  243. }
  244. void send_digitizer(report_digitizer_t *report) {
  245. #ifdef DIGITIZER_ENABLE
  246. if (usbInterruptIsReadyShared()) {
  247. usbSetInterruptShared((void *)report, sizeof(report_digitizer_t));
  248. }
  249. #endif
  250. }
  251. /*------------------------------------------------------------------*
  252. * Request from host *
  253. *------------------------------------------------------------------*/
  254. static struct {
  255. uint16_t len;
  256. enum { NONE, SET_LED } kind;
  257. } last_req;
  258. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  259. usbRequest_t *rq = (void *)data;
  260. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  261. if (rq->bRequest == USBRQ_HID_GET_REPORT) {
  262. dprint("GET_REPORT:");
  263. if (rq->wIndex.word == KEYBOARD_INTERFACE) {
  264. usbMsgPtr = (usbMsgPtr_t)&keyboard_report_sent;
  265. return sizeof(keyboard_report_sent);
  266. }
  267. } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
  268. dprint("GET_IDLE:");
  269. usbMsgPtr = (usbMsgPtr_t)&vusb_idle_rate;
  270. return 1;
  271. } else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
  272. vusb_idle_rate = rq->wValue.bytes[1];
  273. dprintf("SET_IDLE: %02X", vusb_idle_rate);
  274. } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
  275. dprint("SET_REPORT:");
  276. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  277. if (rq->wValue.word == 0x0200 && rq->wIndex.word == KEYBOARD_INTERFACE) {
  278. dprint("SET_LED:");
  279. last_req.kind = SET_LED;
  280. last_req.len = rq->wLength.word;
  281. }
  282. return USB_NO_MSG; // to get data in usbFunctionWrite
  283. } else {
  284. dprint("UNKNOWN:");
  285. }
  286. } else {
  287. dprint("VENDOR:");
  288. /* no vendor specific requests implemented */
  289. }
  290. dprint("\n");
  291. return 0; /* default for not implemented requests: return no data back to host */
  292. }
  293. uchar usbFunctionWrite(uchar *data, uchar len) {
  294. if (last_req.len == 0) {
  295. return -1;
  296. }
  297. switch (last_req.kind) {
  298. case SET_LED:
  299. dprintf("SET_LED: %02X\n", data[0]);
  300. keyboard_led_state = data[0];
  301. last_req.len = 0;
  302. return 1;
  303. break;
  304. case NONE:
  305. default:
  306. return -1;
  307. break;
  308. }
  309. return 1;
  310. }
  311. void usbFunctionWriteOut(uchar *data, uchar len) {
  312. #ifdef RAW_ENABLE
  313. // Data from host must be divided every 8bytes
  314. if (len != 8) {
  315. dprint("RAW: invalid length\n");
  316. raw_output_received_bytes = 0;
  317. return;
  318. }
  319. if (raw_output_received_bytes + len > RAW_BUFFER_SIZE) {
  320. dprint("RAW: buffer full\n");
  321. raw_output_received_bytes = 0;
  322. } else {
  323. for (uint8_t i = 0; i < 8; i++) {
  324. raw_output_buffer[raw_output_received_bytes + i] = data[i];
  325. }
  326. raw_output_received_bytes += len;
  327. }
  328. #endif
  329. }
  330. /*------------------------------------------------------------------*
  331. * Descriptors *
  332. *------------------------------------------------------------------*/
  333. #ifdef KEYBOARD_SHARED_EP
  334. const PROGMEM uchar shared_hid_report[] = {
  335. # define SHARED_REPORT_STARTED
  336. #else
  337. const PROGMEM uchar keyboard_hid_report[] = {
  338. #endif
  339. 0x05, 0x01, // Usage Page (Generic Desktop)
  340. 0x09, 0x06, // Usage (Keyboard)
  341. 0xA1, 0x01, // Collection (Application)
  342. #ifdef KEYBOARD_SHARED_EP
  343. 0x85, REPORT_ID_KEYBOARD, // Report ID
  344. #endif
  345. // Modifiers (8 bits)
  346. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  347. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  348. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  349. 0x15, 0x00, // Logical Minimum (0)
  350. 0x25, 0x01, // Logical Maximum (1)
  351. 0x95, 0x08, // Report Count (8)
  352. 0x75, 0x01, // Report Size (1)
  353. 0x81, 0x02, // Input (Data, Variable, Absolute)
  354. // Reserved (1 byte)
  355. 0x95, 0x01, // Report Count (1)
  356. 0x75, 0x08, // Report Size (8)
  357. 0x81, 0x03, // Input (Constant)
  358. // Keycodes (6 bytes)
  359. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  360. 0x19, 0x00, // Usage Minimum (0)
  361. 0x29, 0xFF, // Usage Maximum (255)
  362. 0x15, 0x00, // Logical Minimum (0)
  363. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  364. 0x95, 0x06, // Report Count (6)
  365. 0x75, 0x08, // Report Size (8)
  366. 0x81, 0x00, // Input (Data, Array, Absolute)
  367. // Status LEDs (5 bits)
  368. 0x05, 0x08, // Usage Page (LED)
  369. 0x19, 0x01, // Usage Minimum (Num Lock)
  370. 0x29, 0x05, // Usage Maximum (Kana)
  371. 0x95, 0x05, // Report Count (5)
  372. 0x75, 0x01, // Report Size (1)
  373. 0x91, 0x02, // Output (Data, Variable, Absolute)
  374. // LED padding (3 bits)
  375. 0x95, 0x01, // Report Count (1)
  376. 0x75, 0x03, // Report Size (3)
  377. 0x91, 0x03, // Output (Constant)
  378. 0xC0, // End Collection
  379. #ifndef KEYBOARD_SHARED_EP
  380. };
  381. #endif
  382. #if defined(SHARED_EP_ENABLE) && !defined(SHARED_REPORT_STARTED)
  383. const PROGMEM uchar shared_hid_report[] = {
  384. # define SHARED_REPORT_STARTED
  385. #endif
  386. #ifdef MOUSE_ENABLE
  387. // Mouse report descriptor
  388. 0x05, 0x01, // Usage Page (Generic Desktop)
  389. 0x09, 0x02, // Usage (Mouse)
  390. 0xA1, 0x01, // Collection (Application)
  391. 0x85, REPORT_ID_MOUSE, // Report ID
  392. 0x09, 0x01, // Usage (Pointer)
  393. 0xA1, 0x00, // Collection (Physical)
  394. // Buttons (8 bits)
  395. 0x05, 0x09, // Usage Page (Button)
  396. 0x19, 0x01, // Usage Minimum (Button 1)
  397. 0x29, 0x08, // Usage Maximum (Button 8)
  398. 0x15, 0x00, // Logical Minimum (0)
  399. 0x25, 0x01, // Logical Maximum (1)
  400. 0x95, 0x08, // Report Count (8)
  401. 0x75, 0x01, // Report Size (1)
  402. 0x81, 0x02, // Input (Data, Variable, Absolute)
  403. // X/Y position (2 bytes)
  404. 0x05, 0x01, // Usage Page (Generic Desktop)
  405. 0x09, 0x30, // Usage (X)
  406. 0x09, 0x31, // Usage (Y)
  407. 0x15, 0x81, // Logical Minimum (-127)
  408. 0x25, 0x7F, // Logical Maximum (127)
  409. 0x95, 0x02, // Report Count (2)
  410. 0x75, 0x08, // Report Size (8)
  411. 0x81, 0x06, // Input (Data, Variable, Relative)
  412. // Vertical wheel (1 byte)
  413. 0x09, 0x38, // Usage (Wheel)
  414. 0x15, 0x81, // Logical Minimum (-127)
  415. 0x25, 0x7F, // Logical Maximum (127)
  416. 0x95, 0x01, // Report Count (1)
  417. 0x75, 0x08, // Report Size (8)
  418. 0x81, 0x06, // Input (Data, Variable, Relative)
  419. // Horizontal wheel (1 byte)
  420. 0x05, 0x0C, // Usage Page (Consumer)
  421. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  422. 0x15, 0x81, // Logical Minimum (-127)
  423. 0x25, 0x7F, // Logical Maximum (127)
  424. 0x95, 0x01, // Report Count (1)
  425. 0x75, 0x08, // Report Size (8)
  426. 0x81, 0x06, // Input (Data, Variable, Relative)
  427. 0xC0, // End Collection
  428. 0xC0, // End Collection
  429. #endif
  430. #ifdef EXTRAKEY_ENABLE
  431. // Extrakeys report descriptor
  432. 0x05, 0x01, // Usage Page (Generic Desktop)
  433. 0x09, 0x80, // Usage (System Control)
  434. 0xA1, 0x01, // Collection (Application)
  435. 0x85, REPORT_ID_SYSTEM, // Report ID
  436. 0x19, 0x01, // Usage Minimum (Pointer)
  437. 0x2A, 0xB7, 0x00, // Usage Maximum (System Display LCD Autoscale)
  438. 0x15, 0x01, // Logical Minimum
  439. 0x26, 0xB7, 0x00, // Logical Maximum
  440. 0x95, 0x01, // Report Count (1)
  441. 0x75, 0x10, // Report Size (16)
  442. 0x81, 0x00, // Input (Data, Array, Absolute)
  443. 0xC0, // End Collection
  444. 0x05, 0x0C, // Usage Page (Consumer)
  445. 0x09, 0x01, // Usage (Consumer Control)
  446. 0xA1, 0x01, // Collection (Application)
  447. 0x85, REPORT_ID_CONSUMER, // Report ID
  448. 0x19, 0x01, // Usage Minimum (Consumer Control)
  449. 0x2A, 0xA0, 0x02, // Usage Maximum (AC Desktop Show All Applications)
  450. 0x15, 0x01, // Logical Minimum
  451. 0x26, 0xA0, 0x02, // Logical Maximum
  452. 0x95, 0x01, // Report Count (1)
  453. 0x75, 0x10, // Report Size (16)
  454. 0x81, 0x00, // Input (Data, Array, Absolute)
  455. 0xC0, // End Collection
  456. #endif
  457. #ifdef DIGITIZER_ENABLE
  458. // Digitizer report descriptor
  459. 0x05, 0x0D, // Usage Page (Digitizers)
  460. 0x09, 0x01, // Usage (Digitizer)
  461. 0xA1, 0x01, // Collection (Application)
  462. 0x85, REPORT_ID_DIGITIZER, // Report ID
  463. 0x09, 0x22, // Usage (Finger)
  464. 0xA1, 0x00, // Collection (Physical)
  465. // Tip Switch (1 bit)
  466. 0x09, 0x42, // Usage (Tip Switch)
  467. 0x15, 0x00, // Logical Minimum
  468. 0x25, 0x01, // Logical Maximum
  469. 0x95, 0x01, // Report Count (1)
  470. 0x75, 0x01, // Report Size (16)
  471. 0x81, 0x02, // Input (Data, Variable, Absolute)
  472. // In Range (1 bit)
  473. 0x09, 0x32, // Usage (In Range)
  474. 0x81, 0x02, // Input (Data, Variable, Absolute)
  475. // Padding (6 bits)
  476. 0x95, 0x06, // Report Count (6)
  477. 0x81, 0x03, // Input (Constant)
  478. // X/Y Position (4 bytes)
  479. 0x05, 0x01, // Usage Page (Generic Desktop)
  480. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  481. 0x95, 0x01, // Report Count (1)
  482. 0x75, 0x10, // Report Size (16)
  483. 0x65, 0x33, // Unit (Inch, English Linear)
  484. 0x55, 0x0E, // Unit Exponent (-2)
  485. 0x09, 0x30, // Usage (X)
  486. 0x81, 0x02, // Input (Data, Variable, Absolute)
  487. 0x09, 0x31, // Usage (Y)
  488. 0x81, 0x02, // Input (Data, Variable, Absolute)
  489. 0xC0, // End Collection
  490. 0xC0 // End Collection
  491. #endif
  492. #ifdef SHARED_EP_ENABLE
  493. };
  494. #endif
  495. #ifdef RAW_ENABLE
  496. const PROGMEM uchar raw_hid_report[] = {
  497. 0x06, RAW_USAGE_PAGE_LO, RAW_USAGE_PAGE_HI, // Usage Page (Vendor Defined)
  498. 0x09, RAW_USAGE_ID, // Usage (Vendor Defined)
  499. 0xA1, 0x01, // Collection (Application)
  500. // Data to host
  501. 0x09, 0x62, // Usage (Vendor Defined)
  502. 0x15, 0x00, // Logical Minimum (0)
  503. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  504. 0x95, RAW_BUFFER_SIZE, // Report Count
  505. 0x75, 0x08, // Report Size (8)
  506. 0x81, 0x02, // Input (Data, Variable, Absolute)
  507. // Data from host
  508. 0x09, 0x63, // Usage (Vendor Defined)
  509. 0x15, 0x00, // Logical Minimum (0)
  510. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  511. 0x95, RAW_BUFFER_SIZE, // Report Count
  512. 0x75, 0x08, // Report Size (8)
  513. 0x91, 0x02, // Output (Data, Variable, Absolute)
  514. 0xC0 // End Collection
  515. };
  516. #endif
  517. #if defined(CONSOLE_ENABLE)
  518. const PROGMEM uchar console_hid_report[] = {
  519. 0x06, 0x31, 0xFF, // Usage Page (Vendor Defined - PJRC Teensy compatible)
  520. 0x09, 0x74, // Usage (Vendor Defined - PJRC Teensy compatible)
  521. 0xA1, 0x01, // Collection (Application)
  522. // Data to host
  523. 0x09, 0x75, // Usage (Vendor Defined)
  524. 0x15, 0x00, // Logical Minimum (0x00)
  525. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  526. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  527. 0x75, 0x08, // Report Size (8)
  528. 0x81, 0x02, // Input (Data, Variable, Absolute)
  529. // Data from host
  530. 0x09, 0x76, // Usage (Vendor Defined)
  531. 0x15, 0x00, // Logical Minimum (0x00)
  532. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  533. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  534. 0x75, 0x08, // Report Size (8)
  535. 0x91, 0x02, // Output (Data)
  536. 0xC0 // End Collection
  537. };
  538. #endif
  539. #ifndef USB_MAX_POWER_CONSUMPTION
  540. # define USB_MAX_POWER_CONSUMPTION 500
  541. #endif
  542. // TODO: change this to 10ms to match LUFA
  543. #ifndef USB_POLLING_INTERVAL_MS
  544. # define USB_POLLING_INTERVAL_MS 1
  545. #endif
  546. // clang-format off
  547. const PROGMEM usbStringDescriptor_t usbStringDescriptorZero = {
  548. .header = {
  549. .bLength = USB_STRING_LEN(1),
  550. .bDescriptorType = USBDESCR_STRING
  551. },
  552. .bString = {0x0409} // US English
  553. };
  554. const PROGMEM usbStringDescriptor_t usbStringDescriptorManufacturer = {
  555. .header = {
  556. .bLength = USB_STRING_LEN(sizeof(STR(MANUFACTURER)) - 1),
  557. .bDescriptorType = USBDESCR_STRING
  558. },
  559. .bString = LSTR(MANUFACTURER)
  560. };
  561. const PROGMEM usbStringDescriptor_t usbStringDescriptorProduct = {
  562. .header = {
  563. .bLength = USB_STRING_LEN(sizeof(STR(PRODUCT)) - 1),
  564. .bDescriptorType = USBDESCR_STRING
  565. },
  566. .bString = LSTR(PRODUCT)
  567. };
  568. #if defined(SERIAL_NUMBER)
  569. const PROGMEM usbStringDescriptor_t usbStringDescriptorSerial = {
  570. .header = {
  571. .bLength = USB_STRING_LEN(sizeof(SERIAL_NUMBER) - 1),
  572. .bDescriptorType = USBDESCR_STRING
  573. },
  574. .bString = USBSTR(SERIAL_NUMBER)
  575. };
  576. #endif
  577. /*
  578. * Device descriptor
  579. */
  580. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  581. .header = {
  582. .bLength = sizeof(usbDeviceDescriptor_t),
  583. .bDescriptorType = USBDESCR_DEVICE
  584. },
  585. .bcdUSB = 0x0110,
  586. .bDeviceClass = 0x00,
  587. .bDeviceSubClass = 0x00,
  588. .bDeviceProtocol = 0x00,
  589. .bMaxPacketSize0 = 8,
  590. .idVendor = VENDOR_ID,
  591. .idProduct = PRODUCT_ID,
  592. .bcdDevice = DEVICE_VER,
  593. .iManufacturer = 0x01,
  594. .iProduct = 0x02,
  595. #if defined(SERIAL_NUMBER)
  596. .iSerialNumber = 0x03,
  597. #else
  598. .iSerialNumber = 0x00,
  599. #endif
  600. .bNumConfigurations = 1
  601. };
  602. /*
  603. * Configuration descriptors
  604. */
  605. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  606. .header = {
  607. .header = {
  608. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  609. .bDescriptorType = USBDESCR_CONFIG
  610. },
  611. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  612. .bNumInterfaces = TOTAL_INTERFACES,
  613. .bConfigurationValue = 0x01,
  614. .iConfiguration = 0x00,
  615. .bmAttributes = (1 << 7) | USBATTR_REMOTEWAKE,
  616. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  617. },
  618. # ifndef KEYBOARD_SHARED_EP
  619. /*
  620. * Keyboard
  621. */
  622. .keyboardInterface = {
  623. .header = {
  624. .bLength = sizeof(usbInterfaceDescriptor_t),
  625. .bDescriptorType = USBDESCR_INTERFACE
  626. },
  627. .bInterfaceNumber = KEYBOARD_INTERFACE,
  628. .bAlternateSetting = 0x00,
  629. .bNumEndpoints = 1,
  630. .bInterfaceClass = 0x03,
  631. .bInterfaceSubClass = 0x01,
  632. .bInterfaceProtocol = 0x01,
  633. .iInterface = 0x00
  634. },
  635. .keyboardHID = {
  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(keyboard_hid_report)
  645. },
  646. .keyboardINEndpoint = {
  647. .header = {
  648. .bLength = sizeof(usbEndpointDescriptor_t),
  649. .bDescriptorType = USBDESCR_ENDPOINT
  650. },
  651. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  652. .bmAttributes = 0x03,
  653. .wMaxPacketSize = 8,
  654. .bInterval = USB_POLLING_INTERVAL_MS
  655. },
  656. # endif
  657. # if defined(RAW_ENABLE)
  658. /*
  659. * RAW HID
  660. */
  661. .rawInterface = {
  662. .header = {
  663. .bLength = sizeof(usbInterfaceDescriptor_t),
  664. .bDescriptorType = USBDESCR_INTERFACE
  665. },
  666. .bInterfaceNumber = RAW_INTERFACE,
  667. .bAlternateSetting = 0x00,
  668. .bNumEndpoints = 2,
  669. .bInterfaceClass = 0x03,
  670. .bInterfaceSubClass = 0x00,
  671. .bInterfaceProtocol = 0x00,
  672. .iInterface = 0x00
  673. },
  674. .rawHID = {
  675. .header = {
  676. .bLength = sizeof(usbHIDDescriptor_t),
  677. .bDescriptorType = USBDESCR_HID
  678. },
  679. .bcdHID = 0x0101,
  680. .bCountryCode = 0x00,
  681. .bNumDescriptors = 1,
  682. .bDescriptorType = USBDESCR_HID_REPORT,
  683. .wDescriptorLength = sizeof(raw_hid_report)
  684. },
  685. .rawINEndpoint = {
  686. .header = {
  687. .bLength = sizeof(usbEndpointDescriptor_t),
  688. .bDescriptorType = USBDESCR_ENDPOINT
  689. },
  690. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  691. .bmAttributes = 0x03,
  692. .wMaxPacketSize = RAW_EPSIZE,
  693. .bInterval = USB_POLLING_INTERVAL_MS
  694. },
  695. .rawOUTEndpoint = {
  696. .header = {
  697. .bLength = sizeof(usbEndpointDescriptor_t),
  698. .bDescriptorType = USBDESCR_ENDPOINT
  699. },
  700. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  701. .bmAttributes = 0x03,
  702. .wMaxPacketSize = RAW_EPSIZE,
  703. .bInterval = USB_POLLING_INTERVAL_MS
  704. },
  705. # endif
  706. # ifdef SHARED_EP_ENABLE
  707. /*
  708. * Shared
  709. */
  710. .sharedInterface = {
  711. .header = {
  712. .bLength = sizeof(usbInterfaceDescriptor_t),
  713. .bDescriptorType = USBDESCR_INTERFACE
  714. },
  715. .bInterfaceNumber = SHARED_INTERFACE,
  716. .bAlternateSetting = 0x00,
  717. .bNumEndpoints = 1,
  718. .bInterfaceClass = 0x03,
  719. # ifdef KEYBOARD_SHARED_EP
  720. .bInterfaceSubClass = 0x01,
  721. .bInterfaceProtocol = 0x01,
  722. # else
  723. .bInterfaceSubClass = 0x00,
  724. .bInterfaceProtocol = 0x00,
  725. # endif
  726. .iInterface = 0x00
  727. },
  728. .sharedHID = {
  729. .header = {
  730. .bLength = sizeof(usbHIDDescriptor_t),
  731. .bDescriptorType = USBDESCR_HID
  732. },
  733. .bcdHID = 0x0101,
  734. .bCountryCode = 0x00,
  735. .bNumDescriptors = 1,
  736. .bDescriptorType = USBDESCR_HID_REPORT,
  737. .wDescriptorLength = sizeof(shared_hid_report)
  738. },
  739. .sharedINEndpoint = {
  740. .header = {
  741. .bLength = sizeof(usbEndpointDescriptor_t),
  742. .bDescriptorType = USBDESCR_ENDPOINT
  743. },
  744. # ifdef KEYBOARD_SHARED_EP
  745. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  746. # else
  747. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  748. # endif
  749. .bmAttributes = 0x03,
  750. .wMaxPacketSize = 8,
  751. .bInterval = USB_POLLING_INTERVAL_MS
  752. },
  753. # endif
  754. # if defined(CONSOLE_ENABLE)
  755. /*
  756. * Console
  757. */
  758. .consoleInterface = {
  759. .header = {
  760. .bLength = sizeof(usbInterfaceDescriptor_t),
  761. .bDescriptorType = USBDESCR_INTERFACE
  762. },
  763. .bInterfaceNumber = CONSOLE_INTERFACE,
  764. .bAlternateSetting = 0x00,
  765. .bNumEndpoints = 2,
  766. .bInterfaceClass = 0x03,
  767. .bInterfaceSubClass = 0x00,
  768. .bInterfaceProtocol = 0x00,
  769. .iInterface = 0x00
  770. },
  771. .consoleHID = {
  772. .header = {
  773. .bLength = sizeof(usbHIDDescriptor_t),
  774. .bDescriptorType = USBDESCR_HID
  775. },
  776. .bcdHID = 0x0111,
  777. .bCountryCode = 0x00,
  778. .bNumDescriptors = 1,
  779. .bDescriptorType = USBDESCR_HID_REPORT,
  780. .wDescriptorLength = sizeof(console_hid_report)
  781. },
  782. .consoleINEndpoint = {
  783. .header = {
  784. .bLength = sizeof(usbEndpointDescriptor_t),
  785. .bDescriptorType = USBDESCR_ENDPOINT
  786. },
  787. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  788. .bmAttributes = 0x03,
  789. .wMaxPacketSize = CONSOLE_EPSIZE,
  790. .bInterval = 0x01
  791. },
  792. .consoleOUTEndpoint = {
  793. .header = {
  794. .bLength = sizeof(usbEndpointDescriptor_t),
  795. .bDescriptorType = USBDESCR_ENDPOINT
  796. },
  797. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP3_NUMBER),
  798. .bmAttributes = 0x03,
  799. .wMaxPacketSize = CONSOLE_EPSIZE,
  800. .bInterval = 0x01
  801. }
  802. # endif
  803. };
  804. // clang-format on
  805. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  806. usbMsgLen_t len = 0;
  807. switch (rq->wValue.bytes[1]) {
  808. case USBDESCR_DEVICE:
  809. usbMsgPtr = (usbMsgPtr_t)&usbDeviceDescriptor;
  810. len = sizeof(usbDeviceDescriptor_t);
  811. break;
  812. case USBDESCR_CONFIG:
  813. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor;
  814. len = sizeof(usbConfigurationDescriptor_t);
  815. break;
  816. case USBDESCR_STRING:
  817. switch (rq->wValue.bytes[0]) {
  818. case 0:
  819. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorZero;
  820. len = usbStringDescriptorZero.header.bLength;
  821. break;
  822. case 1: // iManufacturer
  823. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorManufacturer;
  824. len = usbStringDescriptorManufacturer.header.bLength;
  825. break;
  826. case 2: // iProduct
  827. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorProduct;
  828. len = usbStringDescriptorProduct.header.bLength;
  829. break;
  830. #if defined(SERIAL_NUMBER)
  831. case 3: // iSerialNumber
  832. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorSerial;
  833. len = usbStringDescriptorSerial.header.bLength;
  834. break;
  835. #endif
  836. }
  837. break;
  838. case USBDESCR_HID:
  839. switch (rq->wValue.bytes[0]) {
  840. #ifndef KEYBOARD_SHARED_EP
  841. case KEYBOARD_INTERFACE:
  842. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID;
  843. len = sizeof(usbHIDDescriptor_t);
  844. break;
  845. #endif
  846. #if defined(RAW_ENABLE)
  847. case RAW_INTERFACE:
  848. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID;
  849. len = sizeof(usbHIDDescriptor_t);
  850. break;
  851. #endif
  852. #ifdef SHARED_EP_ENABLE
  853. case SHARED_INTERFACE:
  854. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.sharedHID;
  855. len = sizeof(usbHIDDescriptor_t);
  856. break;
  857. #endif
  858. #if defined(CONSOLE_ENABLE)
  859. case CONSOLE_INTERFACE:
  860. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID;
  861. len = sizeof(usbHIDDescriptor_t);
  862. break;
  863. #endif
  864. }
  865. break;
  866. case USBDESCR_HID_REPORT:
  867. /* interface index */
  868. switch (rq->wIndex.word) {
  869. #ifndef KEYBOARD_SHARED_EP
  870. case KEYBOARD_INTERFACE:
  871. usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report;
  872. len = sizeof(keyboard_hid_report);
  873. break;
  874. #endif
  875. #if defined(RAW_ENABLE)
  876. case RAW_INTERFACE:
  877. usbMsgPtr = (usbMsgPtr_t)raw_hid_report;
  878. len = sizeof(raw_hid_report);
  879. break;
  880. #endif
  881. #ifdef SHARED_EP_ENABLE
  882. case SHARED_INTERFACE:
  883. usbMsgPtr = (usbMsgPtr_t)shared_hid_report;
  884. len = sizeof(shared_hid_report);
  885. break;
  886. #endif
  887. #if defined(CONSOLE_ENABLE)
  888. case CONSOLE_INTERFACE:
  889. usbMsgPtr = (usbMsgPtr_t)console_hid_report;
  890. len = sizeof(console_hid_report);
  891. break;
  892. #endif
  893. }
  894. break;
  895. }
  896. return len;
  897. }