vusb.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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 "compiler_support.h"
  18. #include "usbconfig.h"
  19. #include "host.h"
  20. #include "report.h"
  21. #include "host_driver.h"
  22. #include "vusb.h"
  23. #include "print.h"
  24. #include "debug.h"
  25. #include "wait.h"
  26. #include "usb_descriptor_common.h"
  27. #include "usb_device_state.h"
  28. #ifdef RAW_ENABLE
  29. # include "raw_hid.h"
  30. #endif
  31. #ifdef JOYSTICK_ENABLE
  32. # include "joystick.h"
  33. #endif
  34. #ifdef XAP_ENABLE
  35. # include "xap.h"
  36. # include <string.h>
  37. #endif
  38. #if defined(CONSOLE_ENABLE)
  39. # define RBUF_SIZE 128
  40. # include "ring_buffer.h"
  41. #endif
  42. #ifdef OS_DETECTION_ENABLE
  43. # include "os_detection.h"
  44. #endif
  45. /*
  46. * Interface indexes
  47. */
  48. enum usb_interfaces {
  49. #ifndef KEYBOARD_SHARED_EP
  50. KEYBOARD_INTERFACE,
  51. #else
  52. SHARED_INTERFACE,
  53. # define KEYBOARD_INTERFACE SHARED_INTERFACE
  54. #endif
  55. // It is important that the Raw HID interface is at a constant
  56. // interface number, to support Linux/OSX platforms and chrome.hid
  57. // If Raw HID is enabled, let it be always 1.
  58. #ifdef RAW_ENABLE
  59. RAW_INTERFACE,
  60. #endif
  61. #ifdef PLOVER_HID_ENABLE
  62. PLOVER_HID_INTERFACE,
  63. #endif
  64. #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
  65. SHARED_INTERFACE,
  66. #endif
  67. #ifdef CONSOLE_ENABLE
  68. CONSOLE_INTERFACE,
  69. #endif
  70. #ifdef XAP_ENABLE
  71. XAP_INTERFACE,
  72. #endif
  73. TOTAL_INTERFACES
  74. };
  75. #define MAX_INTERFACES 3
  76. STATIC_ASSERT(TOTAL_INTERFACES <= MAX_INTERFACES, "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.");
  77. #if (defined(MOUSE_ENABLE) || defined(EXTRAKEY_ENABLE)) && CONSOLE_ENABLE
  78. # error Mouse/Extra Keys share an endpoint with Console. Please disable one of the two.
  79. #endif
  80. #if defined(PLOVER_HID_ENABLE) && defined(RAW_ENABLE)
  81. # error Plover HID shares an endpoint with Raw HID. Please disable one of the two.
  82. #endif
  83. static report_keyboard_t keyboard_report_sent;
  84. static void send_report_fragment(uint8_t endpoint, void *data, size_t size) {
  85. for (uint8_t retries = 5; retries > 0; retries--) {
  86. switch (endpoint) {
  87. case 1:
  88. if (usbInterruptIsReady()) {
  89. usbSetInterrupt(data, size);
  90. return;
  91. }
  92. break;
  93. case USB_CFG_EP3_NUMBER:
  94. if (usbInterruptIsReady3()) {
  95. usbSetInterrupt3(data, size);
  96. return;
  97. }
  98. break;
  99. case USB_CFG_EP4_NUMBER:
  100. if (usbInterruptIsReady4()) {
  101. usbSetInterrupt4(data, size);
  102. return;
  103. }
  104. break;
  105. default:
  106. return;
  107. }
  108. usbPoll();
  109. wait_ms(5);
  110. }
  111. }
  112. static void send_report(uint8_t endpoint, void *report, size_t size) {
  113. uint8_t *temp = (uint8_t *)report;
  114. // Send as many full packets as possible
  115. for (uint8_t i = 0; i < size / 8; i++) {
  116. send_report_fragment(endpoint, temp, 8);
  117. temp += 8;
  118. }
  119. // Send any data left over
  120. uint8_t remainder = size % 8;
  121. if (remainder) {
  122. send_report_fragment(endpoint, temp, remainder);
  123. }
  124. }
  125. /*------------------------------------------------------------------*
  126. * RAW HID
  127. *------------------------------------------------------------------*/
  128. #ifdef RAW_ENABLE
  129. # define RAW_BUFFER_SIZE 32
  130. # define RAW_EPSIZE 8
  131. static uint8_t raw_output_buffer[RAW_BUFFER_SIZE];
  132. static uint8_t raw_output_received_bytes = 0;
  133. static void send_raw_hid(uint8_t *data, uint8_t length) {
  134. if (length != RAW_BUFFER_SIZE) {
  135. return;
  136. }
  137. send_report(4, data, RAW_BUFFER_SIZE);
  138. }
  139. void raw_hid_task(void) {
  140. usbPoll();
  141. if (!usbConfiguration || !usbInterruptIsReady4()) {
  142. return;
  143. }
  144. if (raw_output_received_bytes == RAW_BUFFER_SIZE) {
  145. raw_hid_receive(raw_output_buffer, RAW_BUFFER_SIZE);
  146. raw_output_received_bytes = 0;
  147. }
  148. }
  149. #endif
  150. /*------------------------------------------------------------------*
  151. * XAP
  152. *------------------------------------------------------------------*/
  153. #ifdef XAP_ENABLE
  154. # define XAP_BUFFER_SIZE 64
  155. # define XAP_EPSIZE 8
  156. static uint8_t xap_output_buffer[XAP_BUFFER_SIZE];
  157. static uint8_t xap_output_received_bytes = 0;
  158. extern void xap_receive(xap_token_t token, const uint8_t *data, size_t length);
  159. void xap_send_base(uint8_t *data, uint8_t length) {
  160. if (length != XAP_BUFFER_SIZE) {
  161. return;
  162. }
  163. send_report(4, data, XAP_BUFFER_SIZE);
  164. }
  165. void xap_send(xap_token_t token, xap_response_flags_t response_flags, const void *data, size_t length) {
  166. uint8_t rdata[XAP_BUFFER_SIZE] = {0};
  167. xap_response_header_t *header = (xap_response_header_t *)&rdata[0];
  168. header->token = token;
  169. if (length > (XAP_BUFFER_SIZE - sizeof(xap_response_header_t))) response_flags &= ~(XAP_RESPONSE_FLAG_SUCCESS);
  170. header->flags = response_flags;
  171. if (response_flags & (XAP_RESPONSE_FLAG_SUCCESS)) {
  172. header->length = (uint8_t)length;
  173. if (data != NULL) {
  174. memcpy(&rdata[sizeof(xap_response_header_t)], data, length);
  175. }
  176. }
  177. xap_send_base(rdata, sizeof(rdata));
  178. }
  179. void xap_broadcast(uint8_t type, const void *data, size_t length) {
  180. uint8_t rdata[XAP_BUFFER_SIZE] = {0};
  181. xap_broadcast_header_t *header = (xap_broadcast_header_t *)&rdata[0];
  182. header->token = XAP_BROADCAST_TOKEN;
  183. header->type = type;
  184. if (length > (XAP_BUFFER_SIZE - sizeof(xap_broadcast_header_t))) return;
  185. header->length = (uint8_t)length;
  186. if (data != NULL) {
  187. memcpy(&rdata[sizeof(xap_broadcast_header_t)], data, length);
  188. }
  189. xap_send_base(rdata, sizeof(rdata));
  190. }
  191. void xap_receive_base(const void *data) {
  192. const uint8_t *u8data = (const uint8_t *)data;
  193. xap_request_header_t *header = (xap_request_header_t *)&u8data[0];
  194. if (header->length <= (XAP_BUFFER_SIZE - sizeof(xap_request_header_t))) {
  195. xap_receive(header->token, &u8data[sizeof(xap_request_header_t)], header->length);
  196. }
  197. }
  198. void xap_task(void) {
  199. usbPoll();
  200. if (!usbConfiguration || !usbInterruptIsReady4()) {
  201. return;
  202. }
  203. if (xap_output_received_bytes == XAP_BUFFER_SIZE) {
  204. xap_receive_base(xap_output_buffer);
  205. xap_output_received_bytes = 0;
  206. }
  207. }
  208. #endif
  209. /*------------------------------------------------------------------*
  210. * Console
  211. *------------------------------------------------------------------*/
  212. #ifdef CONSOLE_ENABLE
  213. # define CONSOLE_BUFFER_SIZE 32
  214. # define CONSOLE_EPSIZE 8
  215. int8_t sendchar(uint8_t c) {
  216. rbuf_enqueue(c);
  217. return 0;
  218. }
  219. void console_task(void) {
  220. usbPoll();
  221. if (!usbConfiguration || !usbInterruptIsReady3()) {
  222. return;
  223. }
  224. if (!rbuf_has_data()) {
  225. return;
  226. }
  227. // Send in chunks of 8 padded to 32
  228. char send_buf[CONSOLE_BUFFER_SIZE] = {0};
  229. uint8_t send_buf_count = 0;
  230. while (rbuf_has_data() && send_buf_count < CONSOLE_EPSIZE) {
  231. send_buf[send_buf_count++] = rbuf_dequeue();
  232. }
  233. send_report(3, send_buf, CONSOLE_BUFFER_SIZE);
  234. }
  235. #endif
  236. /*------------------------------------------------------------------*
  237. * Host driver
  238. *------------------------------------------------------------------*/
  239. static void send_keyboard(report_keyboard_t *report);
  240. static void send_nkro(report_nkro_t *report);
  241. static void send_mouse(report_mouse_t *report);
  242. static void send_extra(report_extra_t *report);
  243. #ifdef RAW_ENABLE
  244. static void send_raw_hid(uint8_t *data, uint8_t length);
  245. #endif
  246. static host_driver_t driver = {
  247. .keyboard_leds = usb_device_state_get_leds,
  248. .send_keyboard = send_keyboard,
  249. .send_nkro = send_nkro,
  250. .send_mouse = send_mouse,
  251. .send_extra = send_extra,
  252. #ifdef RAW_ENABLE
  253. .send_raw_hid = send_raw_hid,
  254. #endif
  255. };
  256. host_driver_t *vusb_driver(void) {
  257. return &driver;
  258. }
  259. static void send_keyboard(report_keyboard_t *report) {
  260. if (usb_device_state_get_protocol() == USB_PROTOCOL_BOOT) {
  261. send_report(1, &report->mods, 8);
  262. } else {
  263. send_report(1, report, sizeof(report_keyboard_t));
  264. }
  265. keyboard_report_sent = *report;
  266. }
  267. #ifndef KEYBOARD_SHARED_EP
  268. # define MOUSE_IN_EPNUM 3
  269. # define SHARED_IN_EPNUM 3
  270. #else
  271. # define MOUSE_IN_EPNUM 1
  272. # define SHARED_IN_EPNUM 1
  273. #endif
  274. static void send_nkro(report_nkro_t *report) {
  275. #ifdef NKRO_ENABLE
  276. send_report(3, report, sizeof(report_nkro_t));
  277. #endif
  278. }
  279. static void send_mouse(report_mouse_t *report) {
  280. #ifdef MOUSE_ENABLE
  281. send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t));
  282. #endif
  283. }
  284. static void send_extra(report_extra_t *report) {
  285. #ifdef EXTRAKEY_ENABLE
  286. send_report(SHARED_IN_EPNUM, report, sizeof(report_extra_t));
  287. #endif
  288. }
  289. void send_joystick(report_joystick_t *report) {
  290. #ifdef JOYSTICK_ENABLE
  291. send_report(SHARED_IN_EPNUM, report, sizeof(report_joystick_t));
  292. #endif
  293. }
  294. void send_digitizer(report_digitizer_t *report) {
  295. #ifdef DIGITIZER_ENABLE
  296. send_report(SHARED_IN_EPNUM, report, sizeof(report_digitizer_t));
  297. #endif
  298. }
  299. void send_programmable_button(report_programmable_button_t *report) {
  300. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  301. send_report(SHARED_IN_EPNUM, report, sizeof(report_programmable_button_t));
  302. #endif
  303. }
  304. #define PLOVER_HID_EPSIZE 9
  305. void send_plover_hid(report_plover_hid_t *report) {
  306. #ifdef PLOVER_HID_ENABLE
  307. send_report(4, report, sizeof(report_plover_hid_t));
  308. #endif
  309. }
  310. /*------------------------------------------------------------------*
  311. * Request from host *
  312. *------------------------------------------------------------------*/
  313. static struct {
  314. uint16_t len;
  315. enum { NONE, SET_LED } kind;
  316. } last_req;
  317. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  318. usbRequest_t *rq = (void *)data;
  319. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  320. switch (rq->bRequest) {
  321. case USBRQ_HID_GET_REPORT:
  322. dprint("GET_REPORT:");
  323. if (rq->wIndex.word == KEYBOARD_INTERFACE) {
  324. usbMsgPtr = (usbMsgPtr_t)&keyboard_report_sent;
  325. return sizeof(keyboard_report_sent);
  326. }
  327. break;
  328. case USBRQ_HID_GET_IDLE:
  329. dprint("GET_IDLE:");
  330. static uint8_t keyboard_idle;
  331. keyboard_idle = usb_device_state_get_idle_rate();
  332. usbMsgPtr = (usbMsgPtr_t)&keyboard_idle;
  333. return 1;
  334. case USBRQ_HID_GET_PROTOCOL:
  335. dprint("GET_PROTOCOL:");
  336. static uint8_t keyboard_protocol;
  337. keyboard_protocol = usb_device_state_get_protocol();
  338. usbMsgPtr = (usbMsgPtr_t)&keyboard_protocol;
  339. return 1;
  340. case USBRQ_HID_SET_REPORT:
  341. dprint("SET_REPORT:");
  342. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  343. if (rq->wValue.word == 0x0200 && rq->wIndex.word == KEYBOARD_INTERFACE) {
  344. dprint("SET_LED:");
  345. last_req.kind = SET_LED;
  346. last_req.len = rq->wLength.word;
  347. }
  348. return USB_NO_MSG; // to get data in usbFunctionWrite
  349. case USBRQ_HID_SET_IDLE:
  350. usb_device_state_set_idle_rate(rq->wValue.word >> 8);
  351. dprintf("SET_IDLE: %02X", usb_device_state_get_idle_rate());
  352. break;
  353. case USBRQ_HID_SET_PROTOCOL:
  354. if (rq->wIndex.word == KEYBOARD_INTERFACE) {
  355. usb_device_state_set_protocol(rq->wValue.word & 0xFF);
  356. dprintf("SET_PROTOCOL: %02X", usb_device_state_get_protocol());
  357. }
  358. break;
  359. default:
  360. dprint("UNKNOWN:");
  361. break;
  362. }
  363. } else {
  364. dprint("VENDOR:");
  365. /* no vendor specific requests implemented */
  366. }
  367. dprint("\n");
  368. return 0; /* default for not implemented requests: return no data back to host */
  369. }
  370. uchar usbFunctionWrite(uchar *data, uchar len) {
  371. if (last_req.len == 0) {
  372. return -1;
  373. }
  374. switch (last_req.kind) {
  375. case SET_LED:
  376. usb_device_state_set_leds(data[0]);
  377. dprintf("SET_LED: %02X\n", usb_device_state_get_leds());
  378. last_req.len = 0;
  379. return 1;
  380. break;
  381. case NONE:
  382. default:
  383. return -1;
  384. break;
  385. }
  386. return 1;
  387. }
  388. void usbFunctionWriteOut(uchar *data, uchar len) {
  389. #ifdef RAW_ENABLE
  390. // Data from host must be divided every 8bytes
  391. if (len != 8) {
  392. dprint("RAW: invalid length\n");
  393. raw_output_received_bytes = 0;
  394. return;
  395. }
  396. if (raw_output_received_bytes + len > RAW_BUFFER_SIZE) {
  397. dprint("RAW: buffer full\n");
  398. raw_output_received_bytes = 0;
  399. } else {
  400. for (uint8_t i = 0; i < 8; i++) {
  401. raw_output_buffer[raw_output_received_bytes + i] = data[i];
  402. }
  403. raw_output_received_bytes += len;
  404. }
  405. #endif
  406. #ifdef XAP_ENABLE
  407. // Data from host must be divided every 8bytes
  408. if (len != 8) {
  409. dprint("XAP: invalid length\n");
  410. xap_output_received_bytes = 0;
  411. return;
  412. }
  413. if (xap_output_received_bytes + len > XAP_BUFFER_SIZE) {
  414. dprint("XAP: buffer full\n");
  415. xap_output_received_bytes = 0;
  416. } else {
  417. for (uint8_t i = 0; i < 8; i++) {
  418. xap_output_buffer[xap_output_received_bytes + i] = data[i];
  419. }
  420. xap_output_received_bytes += len;
  421. }
  422. #endif
  423. }
  424. /*------------------------------------------------------------------*
  425. * Descriptors *
  426. *------------------------------------------------------------------*/
  427. #ifdef KEYBOARD_SHARED_EP
  428. const PROGMEM uchar shared_hid_report[] = {
  429. # define SHARED_REPORT_STARTED
  430. #else
  431. const PROGMEM uchar keyboard_hid_report[] = {
  432. #endif
  433. 0x05, 0x01, // Usage Page (Generic Desktop)
  434. 0x09, 0x06, // Usage (Keyboard)
  435. 0xA1, 0x01, // Collection (Application)
  436. #ifdef KEYBOARD_SHARED_EP
  437. 0x85, REPORT_ID_KEYBOARD, // Report ID
  438. #endif
  439. // Modifiers (8 bits)
  440. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  441. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  442. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  443. 0x15, 0x00, // Logical Minimum (0)
  444. 0x25, 0x01, // Logical Maximum (1)
  445. 0x95, 0x08, // Report Count (8)
  446. 0x75, 0x01, // Report Size (1)
  447. 0x81, 0x02, // Input (Data, Variable, Absolute)
  448. // Reserved (1 byte)
  449. 0x95, 0x01, // Report Count (1)
  450. 0x75, 0x08, // Report Size (8)
  451. 0x81, 0x03, // Input (Constant)
  452. // Keycodes (6 bytes)
  453. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  454. 0x19, 0x00, // Usage Minimum (0)
  455. 0x29, 0xFF, // Usage Maximum (255)
  456. 0x15, 0x00, // Logical Minimum (0)
  457. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  458. 0x95, 0x06, // Report Count (6)
  459. 0x75, 0x08, // Report Size (8)
  460. 0x81, 0x00, // Input (Data, Array, Absolute)
  461. // Status LEDs (5 bits)
  462. 0x05, 0x08, // Usage Page (LED)
  463. 0x19, 0x01, // Usage Minimum (Num Lock)
  464. 0x29, 0x05, // Usage Maximum (Kana)
  465. 0x15, 0x00, // Logical Minimum (0)
  466. 0x25, 0x01, // Logical Maximum (1)
  467. 0x95, 0x05, // Report Count (5)
  468. 0x75, 0x01, // Report Size (1)
  469. 0x91, 0x02, // Output (Data, Variable, Absolute)
  470. // LED padding (3 bits)
  471. 0x95, 0x01, // Report Count (1)
  472. 0x75, 0x03, // Report Size (3)
  473. 0x91, 0x03, // Output (Constant)
  474. 0xC0, // End Collection
  475. #ifndef KEYBOARD_SHARED_EP
  476. };
  477. #endif
  478. #if defined(SHARED_EP_ENABLE) && !defined(SHARED_REPORT_STARTED)
  479. const PROGMEM uchar shared_hid_report[] = {
  480. # define SHARED_REPORT_STARTED
  481. #endif
  482. #ifdef NKRO_ENABLE
  483. // NKRO report descriptor
  484. 0x05, 0x01, // Usage Page (Generic Desktop)
  485. 0x09, 0x06, // Usage (Keyboard)
  486. 0xA1, 0x01, // Collection (Application)
  487. 0x85, REPORT_ID_NKRO, // Report ID
  488. // Modifiers (8 bits)
  489. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  490. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  491. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  492. 0x15, 0x00, // Logical Minimum (0)
  493. 0x25, 0x01, // Logical Maximum (1)
  494. 0x95, 0x08, // Report Count (8)
  495. 0x75, 0x01, // Report Size (1)
  496. 0x81, 0x02, // Input (Data, Variable, Absolute)
  497. // Keycodes
  498. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  499. 0x19, 0x00, // Usage Minimum (0)
  500. 0x29, NKRO_REPORT_BITS * 8 - 1, // Usage Maximum
  501. 0x15, 0x00, // Logical Minimum (0)
  502. 0x25, 0x01, // Logical Maximum (1)
  503. 0x95, NKRO_REPORT_BITS * 8, // Report Count
  504. 0x75, 0x01, // Report Size (1)
  505. 0x81, 0x02, // Input (Data, Variable, Absolute)
  506. // Status LEDs (5 bits)
  507. 0x05, 0x08, // Usage Page (LED)
  508. 0x19, 0x01, // Usage Minimum (Num Lock)
  509. 0x29, 0x05, // Usage Maximum (Kana)
  510. 0x95, 0x05, // Report Count (5)
  511. 0x75, 0x01, // Report Size (1)
  512. 0x91, 0x02, // Output (Data, Variable, Absolute)
  513. // LED padding (3 bits)
  514. 0x95, 0x01, // Report Count (1)
  515. 0x75, 0x03, // Report Size (3)
  516. 0x91, 0x03, // Output (Constant)
  517. 0xC0, // End Collection
  518. #endif
  519. #ifdef MOUSE_ENABLE
  520. // Mouse report descriptor
  521. 0x05, 0x01, // Usage Page (Generic Desktop)
  522. 0x09, 0x02, // Usage (Mouse)
  523. 0xA1, 0x01, // Collection (Application)
  524. 0x85, REPORT_ID_MOUSE, // Report ID
  525. 0x09, 0x01, // Usage (Pointer)
  526. 0xA1, 0x00, // Collection (Physical)
  527. // Buttons (8 bits)
  528. 0x05, 0x09, // Usage Page (Button)
  529. 0x19, 0x01, // Usage Minimum (Button 1)
  530. 0x29, 0x08, // Usage Maximum (Button 8)
  531. 0x15, 0x00, // Logical Minimum (0)
  532. 0x25, 0x01, // Logical Maximum (1)
  533. 0x95, 0x08, // Report Count (8)
  534. 0x75, 0x01, // Report Size (1)
  535. 0x81, 0x02, // Input (Data, Variable, Absolute)
  536. # ifdef MOUSE_EXTENDED_REPORT
  537. // Boot protocol XY ignored in Report protocol
  538. 0x95, 0x02, // Report Count (2)
  539. 0x75, 0x08, // Report Size (8)
  540. 0x81, 0x03, // Input (Constant)
  541. # endif
  542. // X/Y position (2 or 4 bytes)
  543. 0x05, 0x01, // Usage Page (Generic Desktop)
  544. 0x09, 0x30, // Usage (X)
  545. 0x09, 0x31, // Usage (Y)
  546. # ifndef MOUSE_EXTENDED_REPORT
  547. 0x15, MOUSE_REPORT_XY_MIN, // Logical Minimum (-127)
  548. 0x25, MOUSE_REPORT_XY_MAX, // Logical Maximum (127)
  549. 0x95, 0x02, // Report Count (2)
  550. 0x75, 0x08, // Report Size (8)
  551. # else
  552. 0x16, 0x01, HID_VALUE_16(MOUSE_REPORT_XY_MIN), // Logical Minimum (-32767)
  553. 0x26, 0xFF, HID_VALUE_16(MOUSE_REPORT_XY_MAX), // Logical Maximum (32767)
  554. 0x95, 0x02, // Report Count (2)
  555. 0x75, 0x10, // Report Size (16)
  556. # endif
  557. 0x81, 0x06, // Input (Data, Variable, Relative)
  558. # ifdef POINTING_DEVICE_HIRES_SCROLL_ENABLE
  559. // Feature report and padding (1 byte)
  560. 0xA1, 0x02, // Collection (Logical)
  561. 0x09, 0x48, // Usage (Resolution Multiplier)
  562. 0x95, 0x01, // Report Count (1)
  563. 0x75, 0x02, // Report Size (2)
  564. 0x15, 0x00, // Logical Minimum (0)
  565. 0x25, 0x01, // Logical Maximum (1)
  566. 0x35, 0x01, // Physical Minimum (1)
  567. 0x45, POINTING_DEVICE_HIRES_SCROLL_MULTIPLIER, // Physical Maximum (POINTING_DEVICE_HIRES_SCROLL_MULTIPLIER)
  568. 0x55, POINTING_DEVICE_HIRES_SCROLL_EXPONENT, // Unit Exponent (POINTING_DEVICE_HIRES_SCROLL_EXPONENT)
  569. 0xB1, 0x02, // Feature (Data, Variable, Absolute)
  570. 0x35, 0x00, // Physical Minimum (0)
  571. 0x45, 0x00, // Physical Maximum (0)
  572. 0x75, 0x06, // Report Size (6)
  573. 0xB1, 0x03, // Feature (Constant)
  574. # endif
  575. // Vertical wheel (1 or 2 bytes)
  576. 0x09, 0x38, // Usage (Wheel)
  577. # ifndef WHEEL_EXTENDED_REPORT
  578. 0x15, MOUSE_REPORT_HV_MIN, // Logical Minimum (-127)
  579. 0x25, MOUSE_REPORT_HV_MAX, // Logical Maximum (127)
  580. 0x95, 0x01, // Report Count (1)
  581. 0x75, 0x08, // Report Size (8)
  582. # else
  583. 0x16, 0x01, HID_VALUE_16(MOUSE_REPORT_HV_MIN), // Logical Minimum (-32767)
  584. 0x26, 0xFF, HID_VALUE_16(MOUSE_REPORT_HV_MAX), // Logical Maximum (32767)
  585. 0x95, 0x01, // Report Count (1)
  586. 0x75, 0x10, // Report Size (16)
  587. # endif
  588. 0x81, 0x06, // Input (Data, Variable, Relative)
  589. // Horizontal wheel (1 or 2 bytes)
  590. 0x05, 0x0C, // Usage Page (Consumer)
  591. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  592. # ifndef WHEEL_EXTENDED_REPORT
  593. 0x15, MOUSE_REPORT_HV_MIN, // Logical Minimum (-127)
  594. 0x25, MOUSE_REPORT_HV_MAX, // Logical Maximum (127)
  595. 0x95, 0x01, // Report Count (1)
  596. 0x75, 0x08, // Report Size (8)
  597. # else
  598. 0x16, 0x01, HID_VALUE_16(MOUSE_REPORT_HV_MIN), // Logical Minimum (-32767)
  599. 0x26, 0xFF, HID_VALUE_16(MOUSE_REPORT_HV_MAX), // Logical Maximum (32767)
  600. 0x95, 0x01, // Report Count (1)
  601. 0x75, 0x10, // Report Size (16)
  602. # endif
  603. 0x81, 0x06, // Input (Data, Variable, Relative)
  604. # ifdef POINTING_DEVICE_HIRES_SCROLL_ENABLE
  605. 0xC0, // End Collection
  606. # endif
  607. 0xC0, // End Collection
  608. 0xC0, // End Collection
  609. #endif
  610. #ifdef EXTRAKEY_ENABLE
  611. // Extrakeys report descriptor
  612. 0x05, 0x01, // Usage Page (Generic Desktop)
  613. 0x09, 0x80, // Usage (System Control)
  614. 0xA1, 0x01, // Collection (Application)
  615. 0x85, REPORT_ID_SYSTEM, // Report ID
  616. 0x1A, HID_VALUE_16(SYSTEM_CONTROL_USAGE_MINIMUM), // Usage Minimum
  617. 0x2A, HID_VALUE_16(SYSTEM_CONTROL_USAGE_MAXIMUM), // Usage Maximum
  618. 0x16, HID_VALUE_16(SYSTEM_CONTROL_USAGE_MINIMUM), // Logical Minimum
  619. 0x26, HID_VALUE_16(SYSTEM_CONTROL_USAGE_MAXIMUM), // Logical Maximum
  620. 0x95, 0x01, // Report Count (1)
  621. 0x75, 0x10, // Report Size (16)
  622. 0x81, 0x00, // Input (Data, Array, Absolute)
  623. 0xC0, // End Collection
  624. 0x05, 0x0C, // Usage Page (Consumer)
  625. 0x09, 0x01, // Usage (Consumer Control)
  626. 0xA1, 0x01, // Collection (Application)
  627. 0x85, REPORT_ID_CONSUMER, // Report ID
  628. 0x19, 0x01, // Usage Minimum (Consumer Control)
  629. 0x2A, 0xA0, 0x02, // Usage Maximum (AC Desktop Show All Applications)
  630. 0x15, 0x01, // Logical Minimum
  631. 0x26, 0xA0, 0x02, // Logical Maximum
  632. 0x95, 0x01, // Report Count (1)
  633. 0x75, 0x10, // Report Size (16)
  634. 0x81, 0x00, // Input (Data, Array, Absolute)
  635. 0xC0, // End Collection
  636. #endif
  637. #ifdef JOYSTICK_ENABLE
  638. // Joystick report descriptor
  639. 0x05, 0x01, // Usage Page (Generic Desktop)
  640. 0x09, 0x04, // Usage (Joystick)
  641. 0xA1, 0x01, // Collection (Application)
  642. 0x85, REPORT_ID_JOYSTICK, // Report ID
  643. 0xA1, 0x00, // Collection (Physical)
  644. # if JOYSTICK_AXIS_COUNT > 0
  645. 0x05, 0x01, // Usage Page (Generic Desktop)
  646. 0x09, 0x30, // Usage (X)
  647. # if JOYSTICK_AXIS_COUNT > 1
  648. 0x09, 0x31, // Usage (Y)
  649. # endif
  650. # if JOYSTICK_AXIS_COUNT > 2
  651. 0x09, 0x32, // Usage (Z)
  652. # endif
  653. # if JOYSTICK_AXIS_COUNT > 3
  654. 0x09, 0x33, // Usage (Rx)
  655. # endif
  656. # if JOYSTICK_AXIS_COUNT > 4
  657. 0x09, 0x34, // Usage (Ry)
  658. # endif
  659. # if JOYSTICK_AXIS_COUNT > 5
  660. 0x09, 0x35, // Usage (Rz)
  661. # endif
  662. # if JOYSTICK_AXIS_RESOLUTION == 8
  663. 0x15, -JOYSTICK_MAX_VALUE, // Logical Minimum
  664. 0x25, JOYSTICK_MAX_VALUE, // Logical Maximum
  665. 0x95, JOYSTICK_AXIS_COUNT, // Report Count
  666. 0x75, 0x08, // Report Size (8)
  667. # else
  668. 0x16, HID_VALUE_16(-JOYSTICK_MAX_VALUE), // Logical Minimum
  669. 0x26, HID_VALUE_16(JOYSTICK_MAX_VALUE), // Logical Maximum
  670. 0x95, JOYSTICK_AXIS_COUNT, // Report Count
  671. 0x75, 0x10, // Report Size (16)
  672. # endif
  673. 0x81, 0x02, // Input (Data, Variable, Absolute)
  674. # endif
  675. # ifdef JOYSTICK_HAS_HAT
  676. // Hat Switch (4 bits)
  677. 0x09, 0x39, // Usage (Hat Switch)
  678. 0x15, 0x00, // Logical Minimum (0)
  679. 0x25, 0x07, // Logical Maximum (7)
  680. 0x35, 0x00, // Physical Minimum (0)
  681. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  682. 0x65, 0x14, // Unit (Degree, English Rotation)
  683. 0x95, 0x01, // Report Count (1)
  684. 0x75, 0x04, // Report Size (4)
  685. 0x81, 0x42, // Input (Data, Variable, Absolute, Null State)
  686. // Padding (4 bits)
  687. 0x95, 0x04, // Report Count (4)
  688. 0x75, 0x01, // Report Size (1)
  689. 0x81, 0x01, // Input (Constant)
  690. # endif
  691. # if JOYSTICK_BUTTON_COUNT > 0
  692. 0x05, 0x09, // Usage Page (Button)
  693. 0x19, 0x01, // Usage Minimum (Button 1)
  694. 0x29, JOYSTICK_BUTTON_COUNT, // Usage Maximum
  695. 0x15, 0x00, // Logical Minimum (0)
  696. 0x25, 0x01, // Logical Maximum (1)
  697. 0x95, JOYSTICK_BUTTON_COUNT, // Report Count
  698. 0x75, 0x01, // Report Size (1)
  699. 0x81, 0x02, // Input (Data, Variable, Absolute)
  700. # if (JOYSTICK_BUTTON_COUNT % 8) != 0
  701. 0x95, 8 - (JOYSTICK_BUTTON_COUNT % 8), // Report Count
  702. 0x75, 0x01, // Report Size (1)
  703. 0x81, 0x03, // Input (Constant)
  704. # endif
  705. # endif
  706. 0xC0, // End Collection
  707. 0xC0, // End Collection
  708. #endif
  709. #ifdef DIGITIZER_ENABLE
  710. // Digitizer report descriptor
  711. 0x05, 0x0D, // Usage Page (Digitizers)
  712. 0x09, 0x01, // Usage (Digitizer)
  713. 0xA1, 0x01, // Collection (Application)
  714. 0x85, REPORT_ID_DIGITIZER, // Report ID
  715. 0x09, 0x20, // Usage (Stylus)
  716. 0xA1, 0x00, // Collection (Physical)
  717. // In Range, Tip Switch & Barrel Switch (3 bits)
  718. 0x09, 0x32, // Usage (In Range)
  719. 0x09, 0x42, // Usage (Tip Switch)
  720. 0x09, 0x44, // Usage (Barrel Switch)
  721. 0x15, 0x00, // Logical Minimum
  722. 0x25, 0x01, // Logical Maximum
  723. 0x95, 0x03, // Report Count (3)
  724. 0x75, 0x01, // Report Size (1)
  725. 0x81, 0x02, // Input (Data, Variable, Absolute)
  726. // Padding (5 bits)
  727. 0x95, 0x05, // Report Count (5)
  728. 0x81, 0x03, // Input (Constant)
  729. // X/Y Position (4 bytes)
  730. 0x05, 0x01, // Usage Page (Generic Desktop)
  731. 0x09, 0x30, // Usage (X)
  732. 0x09, 0x31, // Usage (Y)
  733. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  734. 0x95, 0x02, // Report Count (2)
  735. 0x75, 0x10, // Report Size (16)
  736. 0x65, 0x13, // Unit (Inch, English Linear)
  737. 0x55, 0x0E, // Unit Exponent (-2)
  738. 0x81, 0x02, // Input (Data, Variable, Absolute)
  739. 0xC0, // End Collection
  740. 0xC0, // End Collection
  741. #endif
  742. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  743. // Programmable buttons report descriptor
  744. 0x05, 0x0C, // Usage Page (Consumer)
  745. 0x09, 0x01, // Usage (Consumer Control)
  746. 0xA1, 0x01, // Collection (Application)
  747. 0x85, REPORT_ID_PROGRAMMABLE_BUTTON, // Report ID
  748. 0x09, 0x03, // Usage (Programmable Buttons)
  749. 0xA1, 0x04, // Collection (Named Array)
  750. 0x05, 0x09, // Usage Page (Button)
  751. 0x19, 0x01, // Usage Minimum (Button 1)
  752. 0x29, 0x20, // Usage Maximum (Button 32)
  753. 0x15, 0x00, // Logical Minimum (0)
  754. 0x25, 0x01, // Logical Maximum (1)
  755. 0x95, 0x20, // Report Count (32)
  756. 0x75, 0x01, // Report Size (1)
  757. 0x81, 0x02, // Input (Data, Variable, Absolute)
  758. 0xC0, // End Collection
  759. 0xC0, // End Collection
  760. #endif
  761. #ifdef SHARED_EP_ENABLE
  762. };
  763. #endif
  764. #ifdef RAW_ENABLE
  765. const PROGMEM uchar raw_hid_report[] = {
  766. 0x06, HID_VALUE_16(RAW_USAGE_PAGE), // Usage Page (Vendor Defined)
  767. 0x09, RAW_USAGE_ID, // Usage (Vendor Defined)
  768. 0xA1, 0x01, // Collection (Application)
  769. // Data to host
  770. 0x09, 0x62, // Usage (Vendor Defined)
  771. 0x15, 0x00, // Logical Minimum (0)
  772. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  773. 0x95, RAW_BUFFER_SIZE, // Report Count
  774. 0x75, 0x08, // Report Size (8)
  775. 0x81, 0x02, // Input (Data, Variable, Absolute)
  776. // Data from host
  777. 0x09, 0x63, // Usage (Vendor Defined)
  778. 0x15, 0x00, // Logical Minimum (0)
  779. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  780. 0x95, RAW_BUFFER_SIZE, // Report Count
  781. 0x75, 0x08, // Report Size (8)
  782. 0x91, 0x02, // Output (Data, Variable, Absolute)
  783. 0xC0 // End Collection
  784. };
  785. #endif
  786. #ifdef PLOVER_HID_ENABLE
  787. // clang-format off
  788. const PROGMEM uchar plover_hid_report[] = {
  789. 0x06, 0x50, 0xFF, // Usage Page (Vendor Defined)
  790. 0x0A, 0x56, 0x4C, // Usage (Vendor Defined) (0xff P L V)
  791. 0xA1, 0x01, // Collection (Application)
  792. 0x85, REPORT_ID_PLOVER_HID, // Report ID
  793. 0x15, 0x00, // Logical Minimum (0)
  794. 0x25, 0x01, // Logical Maximum (1)
  795. 0x75, 0x01, // Report Size (1)
  796. 0x95, 0x40, // Report Count (43)
  797. 0x05, 0x0A, // Usage Page: Ordinal
  798. 0x19, 0x00, // Usage Minimum
  799. 0x29, 0x3F, // Usage Maximum (63)
  800. 0x81, 0x02, // Output (Data, Variable, Absolute)
  801. 0xC0 // End Collection
  802. };
  803. // clang-format on
  804. #endif
  805. #if defined(CONSOLE_ENABLE)
  806. const PROGMEM uchar console_hid_report[] = {
  807. 0x06, 0x31, 0xFF, // Usage Page (Vendor Defined - PJRC Teensy compatible)
  808. 0x09, 0x74, // Usage (Vendor Defined - PJRC Teensy compatible)
  809. 0xA1, 0x01, // Collection (Application)
  810. // Data to host
  811. 0x09, 0x75, // Usage (Vendor Defined)
  812. 0x15, 0x00, // Logical Minimum (0x00)
  813. 0x26, 0xFF, 0x00, // Logical Maximum (0x00FF)
  814. 0x95, CONSOLE_BUFFER_SIZE, // Report Count
  815. 0x75, 0x08, // Report Size (8)
  816. 0x81, 0x02, // Input (Data, Variable, Absolute)
  817. 0xC0 // End Collection
  818. };
  819. #endif
  820. #ifdef XAP_ENABLE
  821. const PROGMEM uchar xap_report[] = {
  822. 0x06, 0x51, 0xFF, // Usage Page (Vendor Defined)
  823. 0x09, 0x58, // Usage (Vendor Defined)
  824. 0xA1, 0x01, // Collection (Application)
  825. // Data to host
  826. 0x09, 0x62, // Usage (Vendor Defined)
  827. 0x15, 0x00, // Logical Minimum (0)
  828. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  829. 0x95, XAP_BUFFER_SIZE, // Report Count
  830. 0x75, 0x08, // Report Size (8)
  831. 0x81, 0x02, // Input (Data, Variable, Absolute)
  832. // Data from host
  833. 0x09, 0x63, // Usage (Vendor Defined)
  834. 0x15, 0x00, // Logical Minimum (0)
  835. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  836. 0x95, XAP_BUFFER_SIZE, // Report Count
  837. 0x75, 0x08, // Report Size (8)
  838. 0x91, 0x02, // Output (Data, Variable, Absolute)
  839. 0xC0 // End Collection
  840. };
  841. #endif
  842. #ifndef USB_MAX_POWER_CONSUMPTION
  843. # define USB_MAX_POWER_CONSUMPTION 500
  844. #endif
  845. #ifndef USB_POLLING_INTERVAL_MS
  846. # define USB_POLLING_INTERVAL_MS 1
  847. #endif
  848. // clang-format off
  849. const PROGMEM usbStringDescriptor_t usbStringDescriptorZero = {
  850. .header = {
  851. .bLength = 4,
  852. .bDescriptorType = USBDESCR_STRING
  853. },
  854. .bString = {0x0409} // US English
  855. };
  856. const PROGMEM usbStringDescriptor_t usbStringDescriptorManufacturer = {
  857. .header = {
  858. .bLength = sizeof(USBSTR(MANUFACTURER)),
  859. .bDescriptorType = USBDESCR_STRING
  860. },
  861. .bString = USBSTR(MANUFACTURER)
  862. };
  863. const PROGMEM usbStringDescriptor_t usbStringDescriptorProduct = {
  864. .header = {
  865. .bLength = sizeof(USBSTR(PRODUCT)),
  866. .bDescriptorType = USBDESCR_STRING
  867. },
  868. .bString = USBSTR(PRODUCT)
  869. };
  870. #if defined(SERIAL_NUMBER)
  871. const PROGMEM usbStringDescriptor_t usbStringDescriptorSerial = {
  872. .header = {
  873. .bLength = sizeof(USBSTR(SERIAL_NUMBER)),
  874. .bDescriptorType = USBDESCR_STRING
  875. },
  876. .bString = USBSTR(SERIAL_NUMBER)
  877. };
  878. #endif
  879. /*
  880. * Device descriptor
  881. */
  882. const PROGMEM usbDeviceDescriptor_t usbDeviceDescriptor = {
  883. .header = {
  884. .bLength = sizeof(usbDeviceDescriptor_t),
  885. .bDescriptorType = USBDESCR_DEVICE
  886. },
  887. .bcdUSB = 0x0110,
  888. .bDeviceClass = 0x00,
  889. .bDeviceSubClass = 0x00,
  890. .bDeviceProtocol = 0x00,
  891. .bMaxPacketSize0 = 8,
  892. .idVendor = VENDOR_ID,
  893. .idProduct = PRODUCT_ID,
  894. .bcdDevice = DEVICE_VER,
  895. .iManufacturer = 0x01,
  896. .iProduct = 0x02,
  897. #if defined(SERIAL_NUMBER)
  898. .iSerialNumber = 0x03,
  899. #else
  900. .iSerialNumber = 0x00,
  901. #endif
  902. .bNumConfigurations = 1
  903. };
  904. /*
  905. * Configuration descriptors
  906. */
  907. const PROGMEM usbConfigurationDescriptor_t usbConfigurationDescriptor = {
  908. .header = {
  909. .header = {
  910. .bLength = sizeof(usbConfigurationDescriptorHeader_t),
  911. .bDescriptorType = USBDESCR_CONFIG
  912. },
  913. .wTotalLength = sizeof(usbConfigurationDescriptor_t),
  914. .bNumInterfaces = TOTAL_INTERFACES,
  915. .bConfigurationValue = 0x01,
  916. .iConfiguration = 0x00,
  917. .bmAttributes = (1 << 7) | USBATTR_REMOTEWAKE,
  918. .bMaxPower = USB_MAX_POWER_CONSUMPTION / 2
  919. },
  920. # ifndef KEYBOARD_SHARED_EP
  921. /*
  922. * Keyboard
  923. */
  924. .keyboardInterface = {
  925. .header = {
  926. .bLength = sizeof(usbInterfaceDescriptor_t),
  927. .bDescriptorType = USBDESCR_INTERFACE
  928. },
  929. .bInterfaceNumber = KEYBOARD_INTERFACE,
  930. .bAlternateSetting = 0x00,
  931. .bNumEndpoints = 1,
  932. .bInterfaceClass = 0x03,
  933. .bInterfaceSubClass = 0x01,
  934. .bInterfaceProtocol = 0x01,
  935. .iInterface = 0x00
  936. },
  937. .keyboardHID = {
  938. .header = {
  939. .bLength = sizeof(usbHIDDescriptor_t),
  940. .bDescriptorType = USBDESCR_HID
  941. },
  942. .bcdHID = 0x0101,
  943. .bCountryCode = 0x00,
  944. .bNumDescriptors = 1,
  945. .bDescriptorType = USBDESCR_HID_REPORT,
  946. .wDescriptorLength = sizeof(keyboard_hid_report)
  947. },
  948. .keyboardINEndpoint = {
  949. .header = {
  950. .bLength = sizeof(usbEndpointDescriptor_t),
  951. .bDescriptorType = USBDESCR_ENDPOINT
  952. },
  953. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  954. .bmAttributes = 0x03,
  955. .wMaxPacketSize = 8,
  956. .bInterval = USB_POLLING_INTERVAL_MS
  957. },
  958. # endif
  959. # if defined(RAW_ENABLE)
  960. /*
  961. * RAW HID
  962. */
  963. .rawInterface = {
  964. .header = {
  965. .bLength = sizeof(usbInterfaceDescriptor_t),
  966. .bDescriptorType = USBDESCR_INTERFACE
  967. },
  968. .bInterfaceNumber = RAW_INTERFACE,
  969. .bAlternateSetting = 0x00,
  970. .bNumEndpoints = 2,
  971. .bInterfaceClass = 0x03,
  972. .bInterfaceSubClass = 0x00,
  973. .bInterfaceProtocol = 0x00,
  974. .iInterface = 0x00
  975. },
  976. .rawHID = {
  977. .header = {
  978. .bLength = sizeof(usbHIDDescriptor_t),
  979. .bDescriptorType = USBDESCR_HID
  980. },
  981. .bcdHID = 0x0101,
  982. .bCountryCode = 0x00,
  983. .bNumDescriptors = 1,
  984. .bDescriptorType = USBDESCR_HID_REPORT,
  985. .wDescriptorLength = sizeof(raw_hid_report)
  986. },
  987. .rawINEndpoint = {
  988. .header = {
  989. .bLength = sizeof(usbEndpointDescriptor_t),
  990. .bDescriptorType = USBDESCR_ENDPOINT
  991. },
  992. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  993. .bmAttributes = 0x03,
  994. .wMaxPacketSize = RAW_EPSIZE,
  995. .bInterval = USB_POLLING_INTERVAL_MS
  996. },
  997. .rawOUTEndpoint = {
  998. .header = {
  999. .bLength = sizeof(usbEndpointDescriptor_t),
  1000. .bDescriptorType = USBDESCR_ENDPOINT
  1001. },
  1002. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  1003. .bmAttributes = 0x03,
  1004. .wMaxPacketSize = RAW_EPSIZE,
  1005. .bInterval = USB_POLLING_INTERVAL_MS
  1006. },
  1007. # endif
  1008. # if defined(PLOVER_HID_ENABLE)
  1009. /*
  1010. * Plover HID
  1011. */
  1012. .ploverInterface = {
  1013. .header = {
  1014. .bLength = sizeof(usbInterfaceDescriptor_t),
  1015. .bDescriptorType = USBDESCR_INTERFACE
  1016. },
  1017. .bInterfaceNumber = PLOVER_HID_INTERFACE,
  1018. .bAlternateSetting = 0x00,
  1019. .bNumEndpoints = 1,
  1020. .bInterfaceClass = 0x03,
  1021. .bInterfaceSubClass = 0x00,
  1022. .bInterfaceProtocol = 0x00,
  1023. .iInterface = 0x00
  1024. },
  1025. .ploverHID = {
  1026. .header = {
  1027. .bLength = sizeof(usbHIDDescriptor_t),
  1028. .bDescriptorType = USBDESCR_HID
  1029. },
  1030. .bcdHID = 0x0101,
  1031. .bCountryCode = 0x00,
  1032. .bNumDescriptors = 1,
  1033. .bDescriptorType = USBDESCR_HID_REPORT,
  1034. .wDescriptorLength = sizeof(plover_hid_report)
  1035. },
  1036. .ploverINEndpoint = {
  1037. .header = {
  1038. .bLength = sizeof(usbEndpointDescriptor_t),
  1039. .bDescriptorType = USBDESCR_ENDPOINT
  1040. },
  1041. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  1042. .bmAttributes = 0x03,
  1043. .wMaxPacketSize = PLOVER_HID_EPSIZE,
  1044. .bInterval = 0x01
  1045. },
  1046. # endif
  1047. # ifdef SHARED_EP_ENABLE
  1048. /*
  1049. * Shared
  1050. */
  1051. .sharedInterface = {
  1052. .header = {
  1053. .bLength = sizeof(usbInterfaceDescriptor_t),
  1054. .bDescriptorType = USBDESCR_INTERFACE
  1055. },
  1056. .bInterfaceNumber = SHARED_INTERFACE,
  1057. .bAlternateSetting = 0x00,
  1058. .bNumEndpoints = 1,
  1059. .bInterfaceClass = 0x03,
  1060. # ifdef KEYBOARD_SHARED_EP
  1061. .bInterfaceSubClass = 0x01,
  1062. .bInterfaceProtocol = 0x01,
  1063. # else
  1064. .bInterfaceSubClass = 0x00,
  1065. .bInterfaceProtocol = 0x00,
  1066. # endif
  1067. .iInterface = 0x00
  1068. },
  1069. .sharedHID = {
  1070. .header = {
  1071. .bLength = sizeof(usbHIDDescriptor_t),
  1072. .bDescriptorType = USBDESCR_HID
  1073. },
  1074. .bcdHID = 0x0101,
  1075. .bCountryCode = 0x00,
  1076. .bNumDescriptors = 1,
  1077. .bDescriptorType = USBDESCR_HID_REPORT,
  1078. .wDescriptorLength = sizeof(shared_hid_report)
  1079. },
  1080. .sharedINEndpoint = {
  1081. .header = {
  1082. .bLength = sizeof(usbEndpointDescriptor_t),
  1083. .bDescriptorType = USBDESCR_ENDPOINT
  1084. },
  1085. # ifdef KEYBOARD_SHARED_EP
  1086. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | 1),
  1087. # else
  1088. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  1089. # endif
  1090. .bmAttributes = 0x03,
  1091. .wMaxPacketSize = 8,
  1092. .bInterval = USB_POLLING_INTERVAL_MS
  1093. },
  1094. # endif
  1095. # if defined(CONSOLE_ENABLE)
  1096. /*
  1097. * Console
  1098. */
  1099. .consoleInterface = {
  1100. .header = {
  1101. .bLength = sizeof(usbInterfaceDescriptor_t),
  1102. .bDescriptorType = USBDESCR_INTERFACE
  1103. },
  1104. .bInterfaceNumber = CONSOLE_INTERFACE,
  1105. .bAlternateSetting = 0x00,
  1106. .bNumEndpoints = 2,
  1107. .bInterfaceClass = 0x03,
  1108. .bInterfaceSubClass = 0x00,
  1109. .bInterfaceProtocol = 0x00,
  1110. .iInterface = 0x00
  1111. },
  1112. .consoleHID = {
  1113. .header = {
  1114. .bLength = sizeof(usbHIDDescriptor_t),
  1115. .bDescriptorType = USBDESCR_HID
  1116. },
  1117. .bcdHID = 0x0111,
  1118. .bCountryCode = 0x00,
  1119. .bNumDescriptors = 1,
  1120. .bDescriptorType = USBDESCR_HID_REPORT,
  1121. .wDescriptorLength = sizeof(console_hid_report)
  1122. },
  1123. .consoleINEndpoint = {
  1124. .header = {
  1125. .bLength = sizeof(usbEndpointDescriptor_t),
  1126. .bDescriptorType = USBDESCR_ENDPOINT
  1127. },
  1128. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP3_NUMBER),
  1129. .bmAttributes = 0x03,
  1130. .wMaxPacketSize = CONSOLE_EPSIZE,
  1131. .bInterval = 0x01
  1132. },
  1133. # endif
  1134. # if defined(XAP_ENABLE)
  1135. /*
  1136. * XAP
  1137. */
  1138. .xapInterface = {
  1139. .header = {
  1140. .bLength = sizeof(usbInterfaceDescriptor_t),
  1141. .bDescriptorType = USBDESCR_INTERFACE
  1142. },
  1143. .bInterfaceNumber = XAP_INTERFACE,
  1144. .bAlternateSetting = 0x00,
  1145. .bNumEndpoints = 2,
  1146. .bInterfaceClass = 0x03,
  1147. .bInterfaceSubClass = 0x00,
  1148. .bInterfaceProtocol = 0x00,
  1149. .iInterface = 0x00
  1150. },
  1151. .xapHID = {
  1152. .header = {
  1153. .bLength = sizeof(usbHIDDescriptor_t),
  1154. .bDescriptorType = USBDESCR_HID
  1155. },
  1156. .bcdHID = 0x0101,
  1157. .bCountryCode = 0x00,
  1158. .bNumDescriptors = 1,
  1159. .bDescriptorType = USBDESCR_HID_REPORT,
  1160. .wDescriptorLength = sizeof(xap_report)
  1161. },
  1162. .xapINEndpoint = {
  1163. .header = {
  1164. .bLength = sizeof(usbEndpointDescriptor_t),
  1165. .bDescriptorType = USBDESCR_ENDPOINT
  1166. },
  1167. .bEndpointAddress = (USBRQ_DIR_DEVICE_TO_HOST | USB_CFG_EP4_NUMBER),
  1168. .bmAttributes = 0x03,
  1169. .wMaxPacketSize = XAP_EPSIZE,
  1170. .bInterval = USB_POLLING_INTERVAL_MS
  1171. },
  1172. .xapOUTEndpoint = {
  1173. .header = {
  1174. .bLength = sizeof(usbEndpointDescriptor_t),
  1175. .bDescriptorType = USBDESCR_ENDPOINT
  1176. },
  1177. .bEndpointAddress = (USBRQ_DIR_HOST_TO_DEVICE | USB_CFG_EP4_NUMBER),
  1178. .bmAttributes = 0x03,
  1179. .wMaxPacketSize = XAP_EPSIZE,
  1180. .bInterval = USB_POLLING_INTERVAL_MS
  1181. },
  1182. # endif
  1183. };
  1184. // clang-format on
  1185. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  1186. usbMsgLen_t len = 0;
  1187. switch (rq->wValue.bytes[1]) {
  1188. case USBDESCR_DEVICE:
  1189. usbMsgPtr = (usbMsgPtr_t)&usbDeviceDescriptor;
  1190. len = sizeof(usbDeviceDescriptor_t);
  1191. break;
  1192. case USBDESCR_CONFIG:
  1193. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor;
  1194. len = sizeof(usbConfigurationDescriptor_t);
  1195. break;
  1196. case USBDESCR_STRING:
  1197. switch (rq->wValue.bytes[0]) {
  1198. case 0:
  1199. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorZero;
  1200. len = usbStringDescriptorZero.header.bLength;
  1201. break;
  1202. case 1: // iManufacturer
  1203. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorManufacturer;
  1204. len = usbStringDescriptorManufacturer.header.bLength;
  1205. break;
  1206. case 2: // iProduct
  1207. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorProduct;
  1208. len = usbStringDescriptorProduct.header.bLength;
  1209. break;
  1210. #if defined(SERIAL_NUMBER)
  1211. case 3: // iSerialNumber
  1212. usbMsgPtr = (usbMsgPtr_t)&usbStringDescriptorSerial;
  1213. len = usbStringDescriptorSerial.header.bLength;
  1214. break;
  1215. #endif
  1216. }
  1217. #ifdef OS_DETECTION_ENABLE
  1218. process_wlength(rq->wLength.word);
  1219. #endif
  1220. break;
  1221. case USBDESCR_HID:
  1222. switch (rq->wIndex.word) {
  1223. #ifndef KEYBOARD_SHARED_EP
  1224. case KEYBOARD_INTERFACE:
  1225. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.keyboardHID;
  1226. len = sizeof(usbHIDDescriptor_t);
  1227. break;
  1228. #endif
  1229. #if defined(RAW_ENABLE)
  1230. case RAW_INTERFACE:
  1231. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.rawHID;
  1232. len = sizeof(usbHIDDescriptor_t);
  1233. break;
  1234. #endif
  1235. #ifdef SHARED_EP_ENABLE
  1236. case SHARED_INTERFACE:
  1237. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.sharedHID;
  1238. len = sizeof(usbHIDDescriptor_t);
  1239. break;
  1240. #endif
  1241. #if defined(CONSOLE_ENABLE)
  1242. case CONSOLE_INTERFACE:
  1243. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.consoleHID;
  1244. len = sizeof(usbHIDDescriptor_t);
  1245. break;
  1246. #endif
  1247. #if defined(XAP_ENABLE)
  1248. case XAP_INTERFACE:
  1249. usbMsgPtr = (usbMsgPtr_t)&usbConfigurationDescriptor.xapHID;
  1250. len = sizeof(usbHIDDescriptor_t);
  1251. break;
  1252. #endif
  1253. }
  1254. break;
  1255. case USBDESCR_HID_REPORT:
  1256. /* interface index */
  1257. switch (rq->wIndex.word) {
  1258. #ifndef KEYBOARD_SHARED_EP
  1259. case KEYBOARD_INTERFACE:
  1260. usbMsgPtr = (usbMsgPtr_t)keyboard_hid_report;
  1261. len = sizeof(keyboard_hid_report);
  1262. break;
  1263. #endif
  1264. #if defined(RAW_ENABLE)
  1265. case RAW_INTERFACE:
  1266. usbMsgPtr = (usbMsgPtr_t)raw_hid_report;
  1267. len = sizeof(raw_hid_report);
  1268. break;
  1269. #endif
  1270. #if defined(PLOVER_HID_ENABLE)
  1271. case PLOVER_HID_INTERFACE:
  1272. usbMsgPtr = (usbMsgPtr_t)plover_hid_report;
  1273. len = sizeof(plover_hid_report);
  1274. break;
  1275. #endif
  1276. #ifdef SHARED_EP_ENABLE
  1277. case SHARED_INTERFACE:
  1278. usbMsgPtr = (usbMsgPtr_t)shared_hid_report;
  1279. len = sizeof(shared_hid_report);
  1280. break;
  1281. #endif
  1282. #if defined(CONSOLE_ENABLE)
  1283. case CONSOLE_INTERFACE:
  1284. usbMsgPtr = (usbMsgPtr_t)console_hid_report;
  1285. len = sizeof(console_hid_report);
  1286. break;
  1287. #endif
  1288. #if defined(XAP_ENABLE)
  1289. case XAP_INTERFACE:
  1290. usbMsgPtr = (usbMsgPtr_t)xap_report;
  1291. len = sizeof(xap_report);
  1292. break;
  1293. #endif
  1294. }
  1295. break;
  1296. }
  1297. return len;
  1298. }