vusb.c 33 KB

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