You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1102 lines
36 KiB

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