vusb.c 19 KB

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