vusb.c 42 KB

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