vusb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 "bootloader.h"
  26. #include <util/delay.h>
  27. static uint8_t vusb_keyboard_leds = 0;
  28. static uint8_t vusb_idle_rate = 0;
  29. /* Keyboard report send buffer */
  30. #define KBUF_SIZE 16
  31. static report_keyboard_t kbuf[KBUF_SIZE];
  32. static uint8_t kbuf_head = 0;
  33. static uint8_t kbuf_tail = 0;
  34. typedef struct {
  35. uint8_t modifier;
  36. uint8_t reserved;
  37. uint8_t keycode[6];
  38. } keyboard_report_t;
  39. static keyboard_report_t keyboard_report; // sent to PC
  40. #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
  41. /* transfer keyboard report from buffer */
  42. void vusb_transfer_keyboard(void) {
  43. for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
  44. if (usbInterruptIsReady()) {
  45. if (kbuf_head != kbuf_tail) {
  46. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  47. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  48. if (debug_keyboard) {
  49. print("V-USB: kbuf[");
  50. pdec(kbuf_tail);
  51. print("->");
  52. pdec(kbuf_head);
  53. print("](");
  54. phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
  55. print(")\n");
  56. }
  57. }
  58. break;
  59. }
  60. usbPoll();
  61. _delay_ms(1);
  62. }
  63. }
  64. /*------------------------------------------------------------------*
  65. * Host driver
  66. *------------------------------------------------------------------*/
  67. static uint8_t keyboard_leds(void);
  68. static void send_keyboard(report_keyboard_t *report);
  69. static void send_mouse(report_mouse_t *report);
  70. static void send_system(uint16_t data);
  71. static void send_consumer(uint16_t data);
  72. static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
  73. host_driver_t *vusb_driver(void) { return &driver; }
  74. static uint8_t keyboard_leds(void) { return vusb_keyboard_leds; }
  75. static void send_keyboard(report_keyboard_t *report) {
  76. uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
  77. if (next != kbuf_tail) {
  78. kbuf[kbuf_head] = *report;
  79. kbuf_head = next;
  80. } else {
  81. debug("kbuf: full\n");
  82. }
  83. // NOTE: send key strokes of Macro
  84. usbPoll();
  85. vusb_transfer_keyboard();
  86. }
  87. typedef struct {
  88. uint8_t report_id;
  89. report_mouse_t report;
  90. } __attribute__((packed)) vusb_mouse_report_t;
  91. static void send_mouse(report_mouse_t *report) {
  92. vusb_mouse_report_t r = {.report_id = REPORT_ID_MOUSE, .report = *report};
  93. if (usbInterruptIsReady3()) {
  94. usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
  95. }
  96. }
  97. typedef struct {
  98. uint8_t report_id;
  99. uint16_t usage;
  100. } __attribute__((packed)) report_extra_t;
  101. static void send_system(uint16_t data) {
  102. static uint16_t last_data = 0;
  103. if (data == last_data) return;
  104. last_data = data;
  105. report_extra_t report = {.report_id = REPORT_ID_SYSTEM, .usage = data};
  106. if (usbInterruptIsReady3()) {
  107. usbSetInterrupt3((void *)&report, sizeof(report));
  108. }
  109. }
  110. static void send_consumer(uint16_t data) {
  111. static uint16_t last_data = 0;
  112. if (data == last_data) return;
  113. last_data = data;
  114. report_extra_t report = {.report_id = REPORT_ID_CONSUMER, .usage = data};
  115. if (usbInterruptIsReady3()) {
  116. usbSetInterrupt3((void *)&report, sizeof(report));
  117. }
  118. }
  119. /*------------------------------------------------------------------*
  120. * Request from host *
  121. *------------------------------------------------------------------*/
  122. static struct {
  123. uint16_t len;
  124. enum { NONE, BOOTLOADER, 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. #ifdef BOOTLOADER_SIZE
  151. } else if (rq->wValue.word == 0x0301) {
  152. last_req.kind = BOOTLOADER;
  153. last_req.len = rq->wLength.word;
  154. #endif
  155. }
  156. return USB_NO_MSG; // to get data in usbFunctionWrite
  157. } else {
  158. debug("UNKNOWN:");
  159. }
  160. } else {
  161. debug("VENDOR:");
  162. /* no vendor specific requests implemented */
  163. }
  164. debug("\n");
  165. return 0; /* default for not implemented requests: return no data back to host */
  166. }
  167. uchar usbFunctionWrite(uchar *data, uchar len) {
  168. if (last_req.len == 0) {
  169. return -1;
  170. }
  171. switch (last_req.kind) {
  172. case SET_LED:
  173. debug("SET_LED: ");
  174. debug_hex(data[0]);
  175. debug("\n");
  176. vusb_keyboard_leds = data[0];
  177. last_req.len = 0;
  178. return 1;
  179. break;
  180. case BOOTLOADER:
  181. usbDeviceDisconnect();
  182. bootloader_jump();
  183. return 1;
  184. break;
  185. case NONE:
  186. default:
  187. return -1;
  188. break;
  189. }
  190. return 1;
  191. }
  192. /*------------------------------------------------------------------*
  193. * Descriptors *
  194. *------------------------------------------------------------------*/
  195. /*
  196. * Report Descriptor for keyboard
  197. *
  198. * from an example in HID spec appendix
  199. */
  200. const PROGMEM uchar keyboard_hid_report[] = {
  201. 0x05, 0x01, // Usage Page (Generic Desktop),
  202. 0x09, 0x06, // Usage (Keyboard),
  203. 0xA1, 0x01, // Collection (Application),
  204. 0x75, 0x01, // Report Size (1),
  205. 0x95, 0x08, // Report Count (8),
  206. 0x05, 0x07, // Usage Page (Key Codes),
  207. 0x19, 0xE0, // Usage Minimum (224),
  208. 0x29, 0xE7, // Usage Maximum (231),
  209. 0x15, 0x00, // Logical Minimum (0),
  210. 0x25, 0x01, // Logical Maximum (1),
  211. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  212. 0x95, 0x01, // Report Count (1),
  213. 0x75, 0x08, // Report Size (8),
  214. 0x81, 0x03, // Input (Constant), ;Reserved byte
  215. 0x95, 0x05, // Report Count (5),
  216. 0x75, 0x01, // Report Size (1),
  217. 0x05, 0x08, // Usage Page (LEDs),
  218. 0x19, 0x01, // Usage Minimum (1),
  219. 0x29, 0x05, // Usage Maximum (5),
  220. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  221. 0x95, 0x01, // Report Count (1),
  222. 0x75, 0x03, // Report Size (3),
  223. 0x91, 0x03, // Output (Constant), ;LED report padding
  224. 0x95, 0x06, // Report Count (6),
  225. 0x75, 0x08, // Report Size (8),
  226. 0x15, 0x00, // Logical Minimum (0),
  227. 0x26, 0xFF, 0x00, // Logical Maximum(255),
  228. 0x05, 0x07, // Usage Page (Key Codes),
  229. 0x19, 0x00, // Usage Minimum (0),
  230. 0x29, 0xFF, // Usage Maximum (255),
  231. 0x81, 0x00, // Input (Data, Array),
  232. 0xc0 // End Collection
  233. };
  234. /*
  235. * Report Descriptor for mouse
  236. *
  237. * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  238. * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
  239. * http://www.keil.com/forum/15671/
  240. * http://www.microsoft.com/whdc/device/input/wheel.mspx
  241. */
  242. const PROGMEM uchar mouse_hid_report[] = {
  243. /* mouse */
  244. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  245. 0x09, 0x02, // USAGE (Mouse)
  246. 0xa1, 0x01, // COLLECTION (Application)
  247. 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
  248. 0x09, 0x01, // USAGE (Pointer)
  249. 0xa1, 0x00, // COLLECTION (Physical)
  250. // ---------------------------- Buttons
  251. 0x05, 0x09, // USAGE_PAGE (Button)
  252. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  253. 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
  254. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  255. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  256. 0x75, 0x01, // REPORT_SIZE (1)
  257. 0x95, 0x05, // REPORT_COUNT (5)
  258. 0x81, 0x02, // INPUT (Data,Var,Abs)
  259. 0x75, 0x03, // REPORT_SIZE (3)
  260. 0x95, 0x01, // REPORT_COUNT (1)
  261. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  262. // ---------------------------- X,Y position
  263. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  264. 0x09, 0x30, // USAGE (X)
  265. 0x09, 0x31, // USAGE (Y)
  266. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  267. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  268. 0x75, 0x08, // REPORT_SIZE (8)
  269. 0x95, 0x02, // REPORT_COUNT (2)
  270. 0x81, 0x06, // INPUT (Data,Var,Rel)
  271. // ---------------------------- Vertical wheel
  272. 0x09, 0x38, // USAGE (Wheel)
  273. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  274. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  275. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  276. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  277. 0x75, 0x08, // REPORT_SIZE (8)
  278. 0x95, 0x01, // REPORT_COUNT (1)
  279. 0x81, 0x06, // INPUT (Data,Var,Rel)
  280. // ---------------------------- Horizontal wheel
  281. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  282. 0x0a, 0x38, 0x02, // USAGE (AC Pan)
  283. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  284. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  285. 0x75, 0x08, // REPORT_SIZE (8)
  286. 0x95, 0x01, // REPORT_COUNT (1)
  287. 0x81, 0x06, // INPUT (Data,Var,Rel)
  288. 0xc0, // END_COLLECTION
  289. 0xc0, // END_COLLECTION
  290. /* system control */
  291. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  292. 0x09, 0x80, // USAGE (System Control)
  293. 0xa1, 0x01, // COLLECTION (Application)
  294. 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
  295. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  296. 0x26, 0xb7, 0x00, // LOGICAL_MAXIMUM (0xb7)
  297. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  298. 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
  299. 0x75, 0x10, // REPORT_SIZE (16)
  300. 0x95, 0x01, // REPORT_COUNT (1)
  301. 0x81, 0x00, // INPUT (Data,Array,Abs)
  302. 0xc0, // END_COLLECTION
  303. /* consumer */
  304. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  305. 0x09, 0x01, // USAGE (Consumer Control)
  306. 0xa1, 0x01, // COLLECTION (Application)
  307. 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
  308. 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
  309. 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
  310. 0x19, 0x01, // USAGE_MINIMUM (0x1)
  311. 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
  312. 0x75, 0x10, // REPORT_SIZE (16)
  313. 0x95, 0x01, // REPORT_COUNT (1)
  314. 0x81, 0x00, // INPUT (Data,Array,Abs)
  315. 0xc0, // END_COLLECTION
  316. };
  317. #ifndef USB_MAX_POWER_CONSUMPTION
  318. # define USB_MAX_POWER_CONSUMPTION 500
  319. #endif
  320. // TODO: change this to 10ms to match LUFA
  321. #ifndef USB_POLLING_INTERVAL_MS
  322. # define USB_POLLING_INTERVAL_MS 1
  323. #endif
  324. /*
  325. * Descriptor for compite device: Keyboard + Mouse
  326. *
  327. * contains: device, interface, HID and endpoint descriptors
  328. */
  329. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  330. const PROGMEM char usbDescriptorConfiguration[] = {
  331. /* USB configuration descriptor */
  332. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  333. USBDESCR_CONFIG, /* descriptor type */
  334. 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
  335. // 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
  336. /* total length of data returned (including inlined descriptors) */
  337. 2, /* number of interfaces in this configuration */
  338. 1, /* index of this configuration */
  339. 0, /* configuration name string index */
  340. # if USB_CFG_IS_SELF_POWERED
  341. (1 << 7) | USBATTR_SELFPOWER, /* attributes */
  342. # else
  343. (1 << 7), /* attributes */
  344. # endif
  345. USB_MAX_POWER_CONSUMPTION / 2, /* max USB current in 2mA units */
  346. /*
  347. * Keyboard interface
  348. */
  349. /* Interface descriptor */
  350. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  351. USBDESCR_INTERFACE, /* descriptor type */
  352. 0, /* index of this interface */
  353. 0, /* alternate setting for this interface */
  354. USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
  355. USB_CFG_INTERFACE_CLASS, USB_CFG_INTERFACE_SUBCLASS, USB_CFG_INTERFACE_PROTOCOL, 0, /* string index for interface */
  356. /* HID descriptor */
  357. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  358. USBDESCR_HID, /* descriptor type: HID */
  359. 0x01, 0x01, /* BCD representation of HID version */
  360. 0x00, /* target country code */
  361. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  362. 0x22, /* descriptor type: report */
  363. sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
  364. /* Endpoint descriptor */
  365. # if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  366. 7, /* sizeof(usbDescrEndpoint) */
  367. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  368. (char)0x81, /* IN endpoint number 1 */
  369. 0x03, /* attrib: Interrupt endpoint */
  370. 8, 0, /* maximum packet size */
  371. USB_POLLING_INTERVAL_MS, /* in ms */
  372. # endif
  373. /*
  374. * Mouse interface
  375. */
  376. /* Interface descriptor */
  377. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  378. USBDESCR_INTERFACE, /* descriptor type */
  379. 1, /* index of this interface */
  380. 0, /* alternate setting for this interface */
  381. USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
  382. 0x03, /* CLASS: HID */
  383. 0, /* SUBCLASS: none */
  384. 0, /* PROTOCOL: none */
  385. 0, /* string index for interface */
  386. /* HID descriptor */
  387. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  388. USBDESCR_HID, /* descriptor type: HID */
  389. 0x01, 0x01, /* BCD representation of HID version */
  390. 0x00, /* target country code */
  391. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  392. 0x22, /* descriptor type: report */
  393. sizeof(mouse_hid_report), 0, /* total length of report descriptor */
  394. # if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
  395. /* Endpoint descriptor */
  396. 7, /* sizeof(usbDescrEndpoint) */
  397. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  398. (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
  399. 0x03, /* attrib: Interrupt endpoint */
  400. 8, 0, /* maximum packet size */
  401. USB_POLLING_INTERVAL_MS, /* in ms */
  402. # endif
  403. };
  404. #endif
  405. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq) {
  406. usbMsgLen_t len = 0;
  407. /*
  408. debug("usbFunctionDescriptor: ");
  409. debug_hex(rq->bmRequestType); debug(" ");
  410. debug_hex(rq->bRequest); debug(" ");
  411. debug_hex16(rq->wValue.word); debug(" ");
  412. debug_hex16(rq->wIndex.word); debug(" ");
  413. debug_hex16(rq->wLength.word); debug("\n");
  414. */
  415. switch (rq->wValue.bytes[1]) {
  416. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  417. case USBDESCR_CONFIG:
  418. usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
  419. len = sizeof(usbDescriptorConfiguration);
  420. break;
  421. #endif
  422. case USBDESCR_HID:
  423. switch (rq->wValue.bytes[0]) {
  424. case 0:
  425. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
  426. len = 9;
  427. break;
  428. case 1:
  429. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
  430. len = 9;
  431. break;
  432. }
  433. break;
  434. case USBDESCR_HID_REPORT:
  435. /* interface index */
  436. switch (rq->wIndex.word) {
  437. case 0:
  438. usbMsgPtr = (unsigned char *)keyboard_hid_report;
  439. len = sizeof(keyboard_hid_report);
  440. break;
  441. case 1:
  442. usbMsgPtr = (unsigned char *)mouse_hid_report;
  443. len = sizeof(mouse_hid_report);
  444. break;
  445. }
  446. break;
  447. }
  448. // debug("desc len: "); debug_hex(len); debug("\n");
  449. return len;
  450. }