vusb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 <avr/eeprom.h>
  15. #include <avr/wdt.h>
  16. #include <stdint.h>
  17. #include "usbdrv.h"
  18. #include "usbconfig.h"
  19. #include "host.h"
  20. #include "report.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "host_driver.h"
  24. #include "vusb.h"
  25. #include <util/delay.h>
  26. static uint8_t vusb_keyboard_leds = 0;
  27. static uint8_t vusb_idle_rate = 0;
  28. /* Keyboard report send buffer */
  29. #define KBUF_SIZE 16
  30. static report_keyboard_t kbuf[KBUF_SIZE];
  31. static uint8_t kbuf_head = 0;
  32. static uint8_t kbuf_tail = 0;
  33. typedef struct {
  34. uint8_t modifier;
  35. uint8_t reserved;
  36. uint8_t keycode[6];
  37. } keyboard_report_t;
  38. static keyboard_report_t keyboard_report; // sent to PC
  39. #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
  40. /* transfer keyboard report from buffer */
  41. void vusb_transfer_keyboard(void) {
  42. for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
  43. if (usbInterruptIsReady()) {
  44. if (kbuf_head != kbuf_tail) {
  45. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  46. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  47. if (debug_keyboard) {
  48. print("V-USB: kbuf[");
  49. pdec(kbuf_tail);
  50. print("->");
  51. pdec(kbuf_head);
  52. print("](");
  53. phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
  54. print(")\n");
  55. }
  56. }
  57. break;
  58. }
  59. usbPoll();
  60. _delay_ms(1);
  61. }
  62. }
  63. /*------------------------------------------------------------------*
  64. * Host driver
  65. *------------------------------------------------------------------*/
  66. static uint8_t keyboard_leds(void);
  67. static void send_keyboard(report_keyboard_t *report);
  68. static void send_mouse(report_mouse_t *report);
  69. static void send_system(uint16_t data);
  70. static void send_consumer(uint16_t data);
  71. static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  72. host_driver_t *vusb_driver(void) { return &driver; }
  73. static uint8_t keyboard_leds(void) { return vusb_keyboard_leds; }
  74. static void send_keyboard(report_keyboard_t *report) {
  75. uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
  76. if (next != kbuf_tail) {
  77. kbuf[kbuf_head] = *report;
  78. kbuf_head = next;
  79. } else {
  80. debug("kbuf: full\n");
  81. }
  82. // NOTE: send key strokes of Macro
  83. usbPoll();
  84. vusb_transfer_keyboard();
  85. }
  86. typedef struct {
  87. uint8_t report_id;
  88. report_mouse_t report;
  89. } __attribute__((packed)) vusb_mouse_report_t;
  90. static void send_mouse(report_mouse_t *report) {
  91. vusb_mouse_report_t r = {.report_id = REPORT_ID_MOUSE, .report = *report};
  92. if (usbInterruptIsReady3()) {
  93. usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
  94. }
  95. }
  96. #ifdef EXTRAKEY_ENABLE
  97. static void send_extra(uint8_t report_id, uint16_t data) {
  98. static uint8_t last_id = 0;
  99. static uint16_t last_data = 0;
  100. if ((report_id == last_id) && (data == last_data)) return;
  101. last_id = report_id;
  102. last_data = data;
  103. report_extra_t report = {.report_id = report_id, .usage = data};
  104. if (usbInterruptIsReady3()) {
  105. usbSetInterrupt3((void *)&report, sizeof(report));
  106. }
  107. }
  108. #endif
  109. static void send_system(uint16_t data) {
  110. #ifdef EXTRAKEY_ENABLE
  111. send_extra(REPORT_ID_SYSTEM, data - SYSTEM_POWER_DOWN + 1);
  112. #endif
  113. }
  114. static void send_consumer(uint16_t data) {
  115. #ifdef EXTRAKEY_ENABLE
  116. send_extra(REPORT_ID_CONSUMER, data);
  117. #endif
  118. }
  119. /*------------------------------------------------------------------*
  120. * Request from host *
  121. *------------------------------------------------------------------*/
  122. static struct {
  123. uint16_t len;
  124. enum { NONE, SET_LED } kind;
  125. } last_req;
  126. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  127. usbRequest_t *rq = (void *)data;
  128. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  129. if (rq->bRequest == USBRQ_HID_GET_REPORT) {
  130. debug("GET_REPORT:");
  131. /* we only have one report type, so don't look at wValue */
  132. usbMsgPtr = (void *)&keyboard_report;
  133. return sizeof(keyboard_report);
  134. } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
  135. debug("GET_IDLE: ");
  136. // debug_hex(vusb_idle_rate);
  137. usbMsgPtr = &vusb_idle_rate;
  138. return 1;
  139. } else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
  140. vusb_idle_rate = rq->wValue.bytes[1];
  141. debug("SET_IDLE: ");
  142. debug_hex(vusb_idle_rate);
  143. } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
  144. debug("SET_REPORT: ");
  145. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  146. if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
  147. debug("SET_LED: ");
  148. last_req.kind = SET_LED;
  149. last_req.len = rq->wLength.word;
  150. }
  151. return USB_NO_MSG; // to get data in usbFunctionWrite
  152. } else {
  153. debug("UNKNOWN:");
  154. }
  155. } else {
  156. debug("VENDOR:");
  157. /* no vendor specific requests implemented */
  158. }
  159. debug("\n");
  160. return 0; /* default for not implemented requests: return no data back to host */
  161. }
  162. uchar usbFunctionWrite(uchar *data, uchar len) {
  163. if (last_req.len == 0) {
  164. return -1;
  165. }
  166. switch (last_req.kind) {
  167. case SET_LED:
  168. debug("SET_LED: ");
  169. debug_hex(data[0]);
  170. debug("\n");
  171. vusb_keyboard_leds = data[0];
  172. last_req.len = 0;
  173. return 1;
  174. break;
  175. case NONE:
  176. default:
  177. return -1;
  178. break;
  179. }
  180. return 1;
  181. }
  182. /*------------------------------------------------------------------*
  183. * Descriptors *
  184. *------------------------------------------------------------------*/
  185. const PROGMEM uchar keyboard_hid_report[] = {
  186. 0x05, 0x01, // Usage Page (Generic Desktop)
  187. 0x09, 0x06, // Usage (Keyboard)
  188. 0xA1, 0x01, // Collection (Application)
  189. // Modifiers (8 bits)
  190. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  191. 0x19, 0xE0, // Usage Minimum (Keyboard Left Control)
  192. 0x29, 0xE7, // Usage Maximum (Keyboard Right GUI)
  193. 0x15, 0x00, // Logical Minimum (0)
  194. 0x25, 0x01, // Logical Maximum (1)
  195. 0x95, 0x08, // Report Count (8)
  196. 0x75, 0x01, // Report Size (1)
  197. 0x81, 0x02, // Input (Data, Variable, Absolute)
  198. // Reserved (1 byte)
  199. 0x95, 0x01, // Report Count (1)
  200. 0x75, 0x08, // Report Size (8)
  201. 0x81, 0x03, // Input (Constant)
  202. // Keycodes (6 bytes)
  203. 0x05, 0x07, // Usage Page (Keyboard/Keypad)
  204. 0x19, 0x00, // Usage Minimum (0)
  205. 0x29, 0xFF, // Usage Maximum (255)
  206. 0x15, 0x00, // Logical Minimum (0)
  207. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  208. 0x95, 0x06, // Report Count (6)
  209. 0x75, 0x08, // Report Size (8)
  210. 0x81, 0x00, // Input (Data, Array, Absolute)
  211. // Status LEDs (5 bits)
  212. 0x05, 0x08, // Usage Page (LED)
  213. 0x19, 0x01, // Usage Minimum (Num Lock)
  214. 0x29, 0x05, // Usage Maximum (Kana)
  215. 0x95, 0x05, // Report Count (5)
  216. 0x75, 0x01, // Report Size (1)
  217. 0x91, 0x02, // Output (Data, Variable, Absolute)
  218. // LED padding (3 bits)
  219. 0x95, 0x01, // Report Count (1)
  220. 0x75, 0x03, // Report Size (3)
  221. 0x91, 0x03, // Output (Constant)
  222. 0xC0 // End Collection
  223. };
  224. const PROGMEM uchar mouse_hid_report[] = {
  225. // Mouse report descriptor
  226. 0x05, 0x01, // Usage Page (Generic Desktop)
  227. 0x09, 0x02, // Usage (Mouse)
  228. 0xA1, 0x01, // Collection (Application)
  229. 0x85, REPORT_ID_MOUSE, // Report ID
  230. 0x09, 0x01, // Usage (Pointer)
  231. 0xA1, 0x00, // Collection (Physical)
  232. // Buttons (5 bits)
  233. 0x05, 0x09, // Usage Page (Button)
  234. 0x19, 0x01, // Usage Minimum (Button 1)
  235. 0x29, 0x05, // Usage Maximum (Button 5)
  236. 0x15, 0x00, // Logical Minimum (0)
  237. 0x25, 0x01, // Logical Maximum (1)
  238. 0x95, 0x05, // Report Count (5)
  239. 0x75, 0x01, // Report Size (1)
  240. 0x81, 0x02, // Input (Data, Variable, Absolute)
  241. // Button padding (3 bits)
  242. 0x95, 0x01, // Report Count (1)
  243. 0x75, 0x03, // Report Size (3)
  244. 0x81, 0x03, // Input (Constant)
  245. // X/Y position (2 bytes)
  246. 0x05, 0x01, // Usage Page (Generic Desktop)
  247. 0x09, 0x30, // Usage (X)
  248. 0x09, 0x31, // Usage (Y)
  249. 0x15, 0x81, // Logical Minimum (-127)
  250. 0x25, 0x7F, // Logical Maximum (127)
  251. 0x95, 0x02, // Report Count (2)
  252. 0x75, 0x08, // Report Size (8)
  253. 0x81, 0x06, // Input (Data, Variable, Relative)
  254. // Vertical wheel (1 byte)
  255. 0x09, 0x38, // Usage (Wheel)
  256. 0x15, 0x81, // Logical Minimum (-127)
  257. 0x25, 0x7F, // Logical Maximum (127)
  258. 0x95, 0x01, // Report Count (1)
  259. 0x75, 0x08, // Report Size (8)
  260. 0x81, 0x06, // Input (Data, Variable, Relative)
  261. // Horizontal wheel (1 byte)
  262. 0x05, 0x0C, // Usage Page (Consumer)
  263. 0x0A, 0x38, 0x02, // Usage (AC Pan)
  264. 0x15, 0x81, // Logical Minimum (-127)
  265. 0x25, 0x7F, // Logical Maximum (127)
  266. 0x95, 0x01, // Report Count (1)
  267. 0x75, 0x08, // Report Size (8)
  268. 0x81, 0x06, // Input (Data, Variable, Relative)
  269. 0xC0, // End Collection
  270. 0xC0, // End Collection
  271. #ifdef EXTRAKEY_ENABLE
  272. // Extrakeys report descriptor
  273. 0x05, 0x01, // Usage Page (Generic Desktop)
  274. 0x09, 0x80, // Usage (System Control)
  275. 0xA1, 0x01, // Collection (Application)
  276. 0x85, REPORT_ID_SYSTEM, // Report ID
  277. 0x1A, 0x81, 0x00, // Usage Minimum (System Power Down)
  278. 0x2A, 0x83, 0x00, // Usage Maximum (System Wake Up)
  279. 0x16, 0x01, 0x00, // Logical Minimum
  280. 0x26, 0x03, 0x00, // Logical Maximum
  281. 0x95, 0x01, // Report Count (1)
  282. 0x75, 0x10, // Report Size (16)
  283. 0x81, 0x00, // Input (Data, Array, Absolute)
  284. 0xC0, // End Collection
  285. 0x05, 0x0C, // Usage Page (Consumer)
  286. 0x09, 0x01, // Usage (Consumer Control)
  287. 0xA1, 0x01, // Collection (Application)
  288. 0x85, REPORT_ID_CONSUMER, // Report ID
  289. 0x1A, 0x01, 0x00, // Usage Minimum (Consumer Control)
  290. 0x2A, 0x9C, 0x02, // Usage Maximum (AC Distribute Vertically)
  291. 0x16, 0x01, 0x00, // Logical Minimum
  292. 0x26, 0x9C, 0x02, // Logical Maximum
  293. 0x95, 0x01, // Report Count (1)
  294. 0x75, 0x10, // Report Size (16)
  295. 0x81, 0x00, // Input (Data, Array, Absolute)
  296. 0xC0 // End Collection
  297. #endif
  298. };
  299. #ifndef USB_MAX_POWER_CONSUMPTION
  300. # define USB_MAX_POWER_CONSUMPTION 500
  301. #endif
  302. // TODO: change this to 10ms to match LUFA
  303. #ifndef USB_POLLING_INTERVAL_MS
  304. # define USB_POLLING_INTERVAL_MS 1
  305. #endif
  306. /*
  307. * Descriptor for compite device: Keyboard + Mouse
  308. *
  309. * contains: device, interface, HID and endpoint descriptors
  310. */
  311. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  312. const PROGMEM char usbDescriptorConfiguration[] = {
  313. /* USB configuration descriptor */
  314. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  315. USBDESCR_CONFIG, /* descriptor type */
  316. 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
  317. // 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
  318. /* total length of data returned (including inlined descriptors) */
  319. 2, /* number of interfaces in this configuration */
  320. 1, /* index of this configuration */
  321. 0, /* configuration name string index */
  322. # if USB_CFG_IS_SELF_POWERED
  323. (1 << 7) | USBATTR_SELFPOWER, /* attributes */
  324. # else
  325. (1 << 7), /* attributes */
  326. # endif
  327. USB_MAX_POWER_CONSUMPTION / 2, /* max USB current in 2mA units */
  328. /*
  329. * Keyboard interface
  330. */
  331. /* Interface descriptor */
  332. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  333. USBDESCR_INTERFACE, /* descriptor type */
  334. 0, /* index of this interface */
  335. 0, /* alternate setting for this interface */
  336. USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
  337. USB_CFG_INTERFACE_CLASS, USB_CFG_INTERFACE_SUBCLASS, USB_CFG_INTERFACE_PROTOCOL, 0, /* string index for interface */
  338. /* HID descriptor */
  339. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  340. USBDESCR_HID, /* descriptor type: HID */
  341. 0x01, 0x01, /* BCD representation of HID version */
  342. 0x00, /* target country code */
  343. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  344. 0x22, /* descriptor type: report */
  345. sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
  346. /* Endpoint descriptor */
  347. # if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  348. 7, /* sizeof(usbDescrEndpoint) */
  349. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  350. (char)0x81, /* IN endpoint number 1 */
  351. 0x03, /* attrib: Interrupt endpoint */
  352. 8, 0, /* maximum packet size */
  353. USB_POLLING_INTERVAL_MS, /* in ms */
  354. # endif
  355. /*
  356. * Mouse interface
  357. */
  358. /* Interface descriptor */
  359. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  360. USBDESCR_INTERFACE, /* descriptor type */
  361. 1, /* index of this interface */
  362. 0, /* alternate setting for this interface */
  363. USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
  364. 0x03, /* CLASS: HID */
  365. 0, /* SUBCLASS: none */
  366. 0, /* PROTOCOL: none */
  367. 0, /* string index for interface */
  368. /* HID descriptor */
  369. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  370. USBDESCR_HID, /* descriptor type: HID */
  371. 0x01, 0x01, /* BCD representation of HID version */
  372. 0x00, /* target country code */
  373. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  374. 0x22, /* descriptor type: report */
  375. sizeof(mouse_hid_report), 0, /* total length of report descriptor */
  376. # if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
  377. /* Endpoint descriptor */
  378. 7, /* sizeof(usbDescrEndpoint) */
  379. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  380. (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
  381. 0x03, /* attrib: Interrupt endpoint */
  382. 8, 0, /* maximum packet size */
  383. USB_POLLING_INTERVAL_MS, /* in ms */
  384. # endif
  385. };
  386. #endif
  387. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  388. usbMsgLen_t len = 0;
  389. /*
  390. debug("usbFunctionDescriptor: ");
  391. debug_hex(rq->bmRequestType); debug(" ");
  392. debug_hex(rq->bRequest); debug(" ");
  393. debug_hex16(rq->wValue.word); debug(" ");
  394. debug_hex16(rq->wIndex.word); debug(" ");
  395. debug_hex16(rq->wLength.word); debug("\n");
  396. */
  397. switch (rq->wValue.bytes[1]) {
  398. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  399. case USBDESCR_CONFIG:
  400. usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
  401. len = sizeof(usbDescriptorConfiguration);
  402. break;
  403. #endif
  404. case USBDESCR_HID:
  405. switch (rq->wValue.bytes[0]) {
  406. case 0:
  407. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
  408. len = 9;
  409. break;
  410. case 1:
  411. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
  412. len = 9;
  413. break;
  414. }
  415. break;
  416. case USBDESCR_HID_REPORT:
  417. /* interface index */
  418. switch (rq->wIndex.word) {
  419. case 0:
  420. usbMsgPtr = (unsigned char *)keyboard_hid_report;
  421. len = sizeof(keyboard_hid_report);
  422. break;
  423. case 1:
  424. usbMsgPtr = (unsigned char *)mouse_hid_report;
  425. len = sizeof(mouse_hid_report);
  426. break;
  427. }
  428. break;
  429. }
  430. // debug("desc len: "); debug_hex(len); debug("\n");
  431. return len;
  432. }