You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

vusb.c 18KB

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