vusb.c 45 KB

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