vusb.c 36 KB

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