vusb.c 34 KB

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