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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. /* transfer keyboard report from buffer */
  31. void vusb_transfer_keyboard(void)
  32. {
  33. if (usbInterruptIsReady()) {
  34. if (kbuf_head != kbuf_tail) {
  35. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  36. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  37. if (debug_keyboard) {
  38. print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("](");
  39. phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
  40. print(")\n");
  41. }
  42. }
  43. }
  44. }
  45. /*------------------------------------------------------------------*
  46. * Host driver
  47. *------------------------------------------------------------------*/
  48. static uint8_t keyboard_leds(void);
  49. static void send_keyboard(report_keyboard_t *report);
  50. static void send_mouse(report_mouse_t *report);
  51. static void send_system(uint16_t data);
  52. static void send_consumer(uint16_t data);
  53. static host_driver_t driver = {
  54. keyboard_leds,
  55. send_keyboard,
  56. send_mouse,
  57. send_system,
  58. send_consumer
  59. };
  60. host_driver_t *vusb_driver(void)
  61. {
  62. return &driver;
  63. }
  64. static uint8_t keyboard_leds(void) {
  65. return vusb_keyboard_leds;
  66. }
  67. static void send_keyboard(report_keyboard_t *report)
  68. {
  69. uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
  70. if (next != kbuf_tail) {
  71. kbuf[kbuf_head] = *report;
  72. kbuf_head = next;
  73. } else {
  74. debug("kbuf: full\n");
  75. }
  76. // NOTE: send key strokes of Macro
  77. usbPoll();
  78. vusb_transfer_keyboard();
  79. }
  80. typedef struct {
  81. uint8_t report_id;
  82. report_mouse_t report;
  83. } __attribute__ ((packed)) vusb_mouse_report_t;
  84. static void send_mouse(report_mouse_t *report)
  85. {
  86. vusb_mouse_report_t r = {
  87. .report_id = REPORT_ID_MOUSE,
  88. .report = *report
  89. };
  90. if (usbInterruptIsReady3()) {
  91. usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
  92. }
  93. }
  94. typedef struct {
  95. uint8_t report_id;
  96. uint16_t usage;
  97. } __attribute__ ((packed)) report_extra_t;
  98. static void send_system(uint16_t data)
  99. {
  100. static uint16_t last_data = 0;
  101. if (data == last_data) return;
  102. last_data = data;
  103. report_extra_t report = {
  104. .report_id = REPORT_ID_SYSTEM,
  105. .usage = data
  106. };
  107. if (usbInterruptIsReady3()) {
  108. usbSetInterrupt3((void *)&report, sizeof(report));
  109. }
  110. }
  111. static void send_consumer(uint16_t data)
  112. {
  113. static uint16_t last_data = 0;
  114. if (data == last_data) return;
  115. last_data = data;
  116. report_extra_t report = {
  117. .report_id = REPORT_ID_CONSUMER,
  118. .usage = data
  119. };
  120. if (usbInterruptIsReady3()) {
  121. usbSetInterrupt3((void *)&report, sizeof(report));
  122. }
  123. }
  124. /*------------------------------------------------------------------*
  125. * Request from host *
  126. *------------------------------------------------------------------*/
  127. static struct {
  128. uint16_t len;
  129. enum {
  130. NONE,
  131. SET_LED
  132. } kind;
  133. } last_req;
  134. usbMsgLen_t usbFunctionSetup(uchar data[8])
  135. {
  136. usbRequest_t *rq = (void *)data;
  137. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
  138. if(rq->bRequest == USBRQ_HID_GET_REPORT){
  139. debug("GET_REPORT:");
  140. /* we only have one report type, so don't look at wValue */
  141. usbMsgPtr = (void *)keyboard_report;
  142. return sizeof(*keyboard_report);
  143. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  144. debug("GET_IDLE: ");
  145. //debug_hex(vusb_idle_rate);
  146. usbMsgPtr = &vusb_idle_rate;
  147. return 1;
  148. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  149. vusb_idle_rate = rq->wValue.bytes[1];
  150. debug("SET_IDLE: ");
  151. debug_hex(vusb_idle_rate);
  152. }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
  153. debug("SET_REPORT: ");
  154. // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
  155. if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
  156. debug("SET_LED: ");
  157. last_req.kind = SET_LED;
  158. last_req.len = rq->wLength.word;
  159. }
  160. return USB_NO_MSG; // to get data in usbFunctionWrite
  161. } else {
  162. debug("UNKNOWN:");
  163. }
  164. }else{
  165. debug("VENDOR:");
  166. /* no vendor specific requests implemented */
  167. }
  168. debug("\n");
  169. return 0; /* default for not implemented requests: return no data back to host */
  170. }
  171. uchar usbFunctionWrite(uchar *data, uchar len)
  172. {
  173. if (last_req.len == 0) {
  174. return -1;
  175. }
  176. switch (last_req.kind) {
  177. case SET_LED:
  178. debug("SET_LED: ");
  179. debug_hex(data[0]);
  180. debug("\n");
  181. vusb_keyboard_leds = data[0];
  182. last_req.len = 0;
  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. 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. 0x25, 0xFF, // 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. 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. 0x25, 0xb7, // 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. /*
  318. * Descriptor for compite device: Keyboard + Mouse
  319. *
  320. * contains: device, interface, HID and endpoint descriptors
  321. */
  322. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  323. PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
  324. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  325. USBDESCR_CONFIG, /* descriptor type */
  326. 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
  327. //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
  328. /* total length of data returned (including inlined descriptors) */
  329. 2, /* number of interfaces in this configuration */
  330. 1, /* index of this configuration */
  331. 0, /* configuration name string index */
  332. #if USB_CFG_IS_SELF_POWERED
  333. (1 << 7) | USBATTR_SELFPOWER, /* attributes */
  334. #else
  335. (1 << 7), /* attributes */
  336. #endif
  337. USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
  338. /*
  339. * Keyboard interface
  340. */
  341. /* Interface descriptor */
  342. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  343. USBDESCR_INTERFACE, /* descriptor type */
  344. 0, /* index of this interface */
  345. 0, /* alternate setting for this interface */
  346. USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
  347. USB_CFG_INTERFACE_CLASS,
  348. USB_CFG_INTERFACE_SUBCLASS,
  349. USB_CFG_INTERFACE_PROTOCOL,
  350. 0, /* string index for interface */
  351. /* HID descriptor */
  352. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  353. USBDESCR_HID, /* descriptor type: HID */
  354. 0x01, 0x01, /* BCD representation of HID version */
  355. 0x00, /* target country code */
  356. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  357. 0x22, /* descriptor type: report */
  358. sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
  359. /* Endpoint descriptor */
  360. #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  361. 7, /* sizeof(usbDescrEndpoint) */
  362. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  363. (char)0x81, /* IN endpoint number 1 */
  364. 0x03, /* attrib: Interrupt endpoint */
  365. 8, 0, /* maximum packet size */
  366. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  367. #endif
  368. /*
  369. * Mouse interface
  370. */
  371. /* Interface descriptor */
  372. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  373. USBDESCR_INTERFACE, /* descriptor type */
  374. 1, /* index of this interface */
  375. 0, /* alternate setting for this interface */
  376. USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
  377. 0x03, /* CLASS: HID */
  378. 0, /* SUBCLASS: none */
  379. 0, /* PROTOCOL: none */
  380. 0, /* string index for interface */
  381. /* HID descriptor */
  382. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  383. USBDESCR_HID, /* descriptor type: HID */
  384. 0x01, 0x01, /* BCD representation of HID version */
  385. 0x00, /* target country code */
  386. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  387. 0x22, /* descriptor type: report */
  388. sizeof(mouse_hid_report), 0, /* total length of report descriptor */
  389. #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
  390. /* Endpoint descriptor */
  391. 7, /* sizeof(usbDescrEndpoint) */
  392. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  393. (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
  394. 0x03, /* attrib: Interrupt endpoint */
  395. 8, 0, /* maximum packet size */
  396. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  397. #endif
  398. };
  399. #endif
  400. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
  401. {
  402. usbMsgLen_t len = 0;
  403. /*
  404. debug("usbFunctionDescriptor: ");
  405. debug_hex(rq->bmRequestType); debug(" ");
  406. debug_hex(rq->bRequest); debug(" ");
  407. debug_hex16(rq->wValue.word); debug(" ");
  408. debug_hex16(rq->wIndex.word); debug(" ");
  409. debug_hex16(rq->wLength.word); debug("\n");
  410. */
  411. switch (rq->wValue.bytes[1]) {
  412. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  413. case USBDESCR_CONFIG:
  414. usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
  415. len = sizeof(usbDescriptorConfiguration);
  416. break;
  417. #endif
  418. case USBDESCR_HID:
  419. switch (rq->wValue.bytes[0]) {
  420. case 0:
  421. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
  422. len = 9;
  423. break;
  424. case 1:
  425. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
  426. len = 9;
  427. break;
  428. }
  429. break;
  430. case USBDESCR_HID_REPORT:
  431. /* interface index */
  432. switch (rq->wIndex.word) {
  433. case 0:
  434. usbMsgPtr = keyboard_hid_report;
  435. len = sizeof(keyboard_hid_report);
  436. break;
  437. case 1:
  438. usbMsgPtr = mouse_hid_report;
  439. len = sizeof(mouse_hid_report);
  440. break;
  441. }
  442. break;
  443. }
  444. //debug("desc len: "); debug_hex(len); debug("\n");
  445. return len;
  446. }