vusb.c 33 KB

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