Keyboard firmwares for Atmel AVR and Cortex-M
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.

host.c 17KB

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