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.

host.c 16KB

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