vusb.c 40 KB

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