vusb.c 34 KB

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