vusb.c 33 KB

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