vusb.c 36 KB

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