vusb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. static void send_extra(uint8_t report_id, uint16_t data) {
  97. static uint8_t last_id = 0;
  98. static uint16_t last_data = 0;
  99. if ((report_id == last_id) && (data == last_data)) return;
  100. last_id = report_id;
  101. last_data = data;
  102. report_extra_t report = {.report_id = report_id, .usage = data};
  103. if (usbInterruptIsReady3()) {
  104. usbSetInterrupt3((void *)&report, sizeof(report));
  105. }
  106. }
  107. static void send_system(uint16_t data) { send_extra(REPORT_ID_SYSTEM, data); }
  108. static void send_consumer(uint16_t data) { send_extra(REPORT_ID_CONSUMER, data); }
  109. /*------------------------------------------------------------------*
  110. * Request from host *
  111. *------------------------------------------------------------------*/
  112. static struct {
  113. uint16_t len;
  114. enum { NONE, SET_LED } kind;
  115. } last_req;
  116. usbMsgLen_t usbFunctionSetup(uchar data[8]) {
  117. usbRequest_t *rq = (void *)data;
  118. if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* class request type */
  119. if (rq->bRequest == USBRQ_HID_GET_REPORT) {
  120. debug("GET_REPORT:");
  121. /* we only have one report type, so don't look at wValue */
  122. usbMsgPtr = (void *)&keyboard_report;
  123. return sizeof(keyboard_report);
  124. } else if (rq->bRequest == USBRQ_HID_GET_IDLE) {
  125. debug("GET_IDLE: ");
  126. // debug_hex(vusb_idle_rate);
  127. usbMsgPtr = &vusb_idle_rate;
  128. return 1;
  129. } else if (rq->bRequest == USBRQ_HID_SET_IDLE) {
  130. vusb_idle_rate = rq->wValue.bytes[1];
  131. debug("SET_IDLE: ");
  132. debug_hex(vusb_idle_rate);
  133. } else if (rq->bRequest == USBRQ_HID_SET_REPORT) {
  134. debug("SET_REPORT: ");
  135. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  136. if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
  137. debug("SET_LED: ");
  138. last_req.kind = SET_LED;
  139. last_req.len = rq->wLength.word;
  140. }
  141. return USB_NO_MSG; // to get data in usbFunctionWrite
  142. } else {
  143. debug("UNKNOWN:");
  144. }
  145. } else {
  146. debug("VENDOR:");
  147. /* no vendor specific requests implemented */
  148. }
  149. debug("\n");
  150. return 0; /* default for not implemented requests: return no data back to host */
  151. }
  152. uchar usbFunctionWrite(uchar *data, uchar len) {
  153. if (last_req.len == 0) {
  154. return -1;
  155. }
  156. switch (last_req.kind) {
  157. case SET_LED:
  158. debug("SET_LED: ");
  159. debug_hex(data[0]);
  160. debug("\n");
  161. vusb_keyboard_leds = data[0];
  162. last_req.len = 0;
  163. return 1;
  164. break;
  165. case NONE:
  166. default:
  167. return -1;
  168. break;
  169. }
  170. return 1;
  171. }
  172. /*------------------------------------------------------------------*
  173. * Descriptors *
  174. *------------------------------------------------------------------*/
  175. /*
  176. * Report Descriptor for keyboard
  177. *
  178. * from an example in HID spec appendix
  179. */
  180. const PROGMEM uchar keyboard_hid_report[] = {
  181. 0x05, 0x01, // Usage Page (Generic Desktop),
  182. 0x09, 0x06, // Usage (Keyboard),
  183. 0xA1, 0x01, // Collection (Application),
  184. 0x75, 0x01, // Report Size (1),
  185. 0x95, 0x08, // Report Count (8),
  186. 0x05, 0x07, // Usage Page (Key Codes),
  187. 0x19, 0xE0, // Usage Minimum (224),
  188. 0x29, 0xE7, // Usage Maximum (231),
  189. 0x15, 0x00, // Logical Minimum (0),
  190. 0x25, 0x01, // Logical Maximum (1),
  191. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  192. 0x95, 0x01, // Report Count (1),
  193. 0x75, 0x08, // Report Size (8),
  194. 0x81, 0x03, // Input (Constant), ;Reserved byte
  195. 0x95, 0x05, // Report Count (5),
  196. 0x75, 0x01, // Report Size (1),
  197. 0x05, 0x08, // Usage Page (LEDs),
  198. 0x19, 0x01, // Usage Minimum (1),
  199. 0x29, 0x05, // Usage Maximum (5),
  200. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  201. 0x95, 0x01, // Report Count (1),
  202. 0x75, 0x03, // Report Size (3),
  203. 0x91, 0x03, // Output (Constant), ;LED report padding
  204. 0x95, 0x06, // Report Count (6),
  205. 0x75, 0x08, // Report Size (8),
  206. 0x15, 0x00, // Logical Minimum (0),
  207. 0x26, 0xFF, 0x00, // Logical Maximum(255),
  208. 0x05, 0x07, // Usage Page (Key Codes),
  209. 0x19, 0x00, // Usage Minimum (0),
  210. 0x29, 0xFF, // Usage Maximum (255),
  211. 0x81, 0x00, // Input (Data, Array),
  212. 0xc0 // End Collection
  213. };
  214. /*
  215. * Report Descriptor for mouse
  216. *
  217. * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  218. * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
  219. * http://www.keil.com/forum/15671/
  220. * http://www.microsoft.com/whdc/device/input/wheel.mspx
  221. */
  222. const PROGMEM uchar mouse_hid_report[] = {
  223. /* mouse */
  224. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  225. 0x09, 0x02, // USAGE (Mouse)
  226. 0xa1, 0x01, // COLLECTION (Application)
  227. 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
  228. 0x09, 0x01, // USAGE (Pointer)
  229. 0xa1, 0x00, // COLLECTION (Physical)
  230. // ---------------------------- Buttons
  231. 0x05, 0x09, // USAGE_PAGE (Button)
  232. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  233. 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
  234. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  235. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  236. 0x75, 0x01, // REPORT_SIZE (1)
  237. 0x95, 0x05, // REPORT_COUNT (5)
  238. 0x81, 0x02, // INPUT (Data,Var,Abs)
  239. 0x75, 0x03, // REPORT_SIZE (3)
  240. 0x95, 0x01, // REPORT_COUNT (1)
  241. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  242. // ---------------------------- X,Y position
  243. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  244. 0x09, 0x30, // USAGE (X)
  245. 0x09, 0x31, // USAGE (Y)
  246. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  247. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  248. 0x75, 0x08, // REPORT_SIZE (8)
  249. 0x95, 0x02, // REPORT_COUNT (2)
  250. 0x81, 0x06, // INPUT (Data,Var,Rel)
  251. // ---------------------------- Vertical wheel
  252. 0x09, 0x38, // USAGE (Wheel)
  253. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  254. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  255. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  256. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  257. 0x75, 0x08, // REPORT_SIZE (8)
  258. 0x95, 0x01, // REPORT_COUNT (1)
  259. 0x81, 0x06, // INPUT (Data,Var,Rel)
  260. // ---------------------------- Horizontal wheel
  261. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  262. 0x0a, 0x38, 0x02, // USAGE (AC Pan)
  263. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  264. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  265. 0x75, 0x08, // REPORT_SIZE (8)
  266. 0x95, 0x01, // REPORT_COUNT (1)
  267. 0x81, 0x06, // INPUT (Data,Var,Rel)
  268. 0xc0, // END_COLLECTION
  269. 0xc0, // END_COLLECTION
  270. /* system control */
  271. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  272. 0x09, 0x80, // USAGE (System Control)
  273. 0xa1, 0x01, // COLLECTION (Application)
  274. 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
  275. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  276. 0x26, 0xb7, 0x00, // LOGICAL_MAXIMUM (0xb7)
  277. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  278. 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
  279. 0x75, 0x10, // REPORT_SIZE (16)
  280. 0x95, 0x01, // REPORT_COUNT (1)
  281. 0x81, 0x00, // INPUT (Data,Array,Abs)
  282. 0xc0, // END_COLLECTION
  283. /* consumer */
  284. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  285. 0x09, 0x01, // USAGE (Consumer Control)
  286. 0xa1, 0x01, // COLLECTION (Application)
  287. 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
  288. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  289. 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
  290. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  291. 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
  292. 0x75, 0x10, // REPORT_SIZE (16)
  293. 0x95, 0x01, // REPORT_COUNT (1)
  294. 0x81, 0x00, // INPUT (Data,Array,Abs)
  295. 0xc0, // END_COLLECTION
  296. };
  297. #ifndef USB_MAX_POWER_CONSUMPTION
  298. # define USB_MAX_POWER_CONSUMPTION 500
  299. #endif
  300. // TODO: change this to 10ms to match LUFA
  301. #ifndef USB_POLLING_INTERVAL_MS
  302. # define USB_POLLING_INTERVAL_MS 1
  303. #endif
  304. /*
  305. * Descriptor for compite device: Keyboard + Mouse
  306. *
  307. * contains: device, interface, HID and endpoint descriptors
  308. */
  309. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  310. const PROGMEM char usbDescriptorConfiguration[] = {
  311. /* USB configuration descriptor */
  312. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  313. USBDESCR_CONFIG, /* descriptor type */
  314. 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
  315. // 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
  316. /* total length of data returned (including inlined descriptors) */
  317. 2, /* number of interfaces in this configuration */
  318. 1, /* index of this configuration */
  319. 0, /* configuration name string index */
  320. # if USB_CFG_IS_SELF_POWERED
  321. (1 << 7) | USBATTR_SELFPOWER, /* attributes */
  322. # else
  323. (1 << 7), /* attributes */
  324. # endif
  325. USB_MAX_POWER_CONSUMPTION / 2, /* max USB current in 2mA units */
  326. /*
  327. * Keyboard interface
  328. */
  329. /* Interface descriptor */
  330. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  331. USBDESCR_INTERFACE, /* descriptor type */
  332. 0, /* index of this interface */
  333. 0, /* alternate setting for this interface */
  334. USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
  335. USB_CFG_INTERFACE_CLASS, USB_CFG_INTERFACE_SUBCLASS, USB_CFG_INTERFACE_PROTOCOL, 0, /* string index for interface */
  336. /* HID descriptor */
  337. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  338. USBDESCR_HID, /* descriptor type: HID */
  339. 0x01, 0x01, /* BCD representation of HID version */
  340. 0x00, /* target country code */
  341. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  342. 0x22, /* descriptor type: report */
  343. sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
  344. /* Endpoint descriptor */
  345. # if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  346. 7, /* sizeof(usbDescrEndpoint) */
  347. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  348. (char)0x81, /* IN endpoint number 1 */
  349. 0x03, /* attrib: Interrupt endpoint */
  350. 8, 0, /* maximum packet size */
  351. USB_POLLING_INTERVAL_MS, /* in ms */
  352. # endif
  353. /*
  354. * Mouse interface
  355. */
  356. /* Interface descriptor */
  357. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  358. USBDESCR_INTERFACE, /* descriptor type */
  359. 1, /* index of this interface */
  360. 0, /* alternate setting for this interface */
  361. USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
  362. 0x03, /* CLASS: HID */
  363. 0, /* SUBCLASS: none */
  364. 0, /* PROTOCOL: none */
  365. 0, /* string index for interface */
  366. /* HID descriptor */
  367. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  368. USBDESCR_HID, /* descriptor type: HID */
  369. 0x01, 0x01, /* BCD representation of HID version */
  370. 0x00, /* target country code */
  371. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  372. 0x22, /* descriptor type: report */
  373. sizeof(mouse_hid_report), 0, /* total length of report descriptor */
  374. # if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
  375. /* Endpoint descriptor */
  376. 7, /* sizeof(usbDescrEndpoint) */
  377. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  378. (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
  379. 0x03, /* attrib: Interrupt endpoint */
  380. 8, 0, /* maximum packet size */
  381. USB_POLLING_INTERVAL_MS, /* in ms */
  382. # endif
  383. };
  384. #endif
  385. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  386. usbMsgLen_t len = 0;
  387. /*
  388. debug("usbFunctionDescriptor: ");
  389. debug_hex(rq->bmRequestType); debug(" ");
  390. debug_hex(rq->bRequest); debug(" ");
  391. debug_hex16(rq->wValue.word); debug(" ");
  392. debug_hex16(rq->wIndex.word); debug(" ");
  393. debug_hex16(rq->wLength.word); debug("\n");
  394. */
  395. switch (rq->wValue.bytes[1]) {
  396. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  397. case USBDESCR_CONFIG:
  398. usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
  399. len = sizeof(usbDescriptorConfiguration);
  400. break;
  401. #endif
  402. case USBDESCR_HID:
  403. switch (rq->wValue.bytes[0]) {
  404. case 0:
  405. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
  406. len = 9;
  407. break;
  408. case 1:
  409. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
  410. len = 9;
  411. break;
  412. }
  413. break;
  414. case USBDESCR_HID_REPORT:
  415. /* interface index */
  416. switch (rq->wIndex.word) {
  417. case 0:
  418. usbMsgPtr = (unsigned char *)keyboard_hid_report;
  419. len = sizeof(keyboard_hid_report);
  420. break;
  421. case 1:
  422. usbMsgPtr = (unsigned char *)mouse_hid_report;
  423. len = sizeof(mouse_hid_report);
  424. break;
  425. }
  426. break;
  427. }
  428. // debug("desc len: "); debug_hex(len); debug("\n");
  429. return len;
  430. }