vusb.c 35 KB

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