vusb.c 19 KB

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