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.

пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
пре 13 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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, 0x02, // USAGE (Mouse)
  234. 0xa1, 0x02, // COLLECTION (Logical)
  235. 0x09, 0x01, // USAGE (Pointer)
  236. 0xa1, 0x00, // COLLECTION (Physical)
  237. // ------------------------------ Buttons
  238. 0x05, 0x09, // USAGE_PAGE (Button)
  239. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  240. 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
  241. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  242. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  243. 0x75, 0x01, // REPORT_SIZE (1)
  244. 0x95, 0x05, // REPORT_COUNT (5)
  245. 0x81, 0x02, // INPUT (Data,Var,Abs)
  246. // ------------------------------ Padding
  247. 0x75, 0x03, // REPORT_SIZE (3)
  248. 0x95, 0x01, // REPORT_COUNT (1)
  249. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  250. // ------------------------------ X,Y position
  251. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  252. 0x09, 0x30, // USAGE (X)
  253. 0x09, 0x31, // USAGE (Y)
  254. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  255. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  256. 0x75, 0x08, // REPORT_SIZE (8)
  257. 0x95, 0x02, // REPORT_COUNT (2)
  258. 0x81, 0x06, // INPUT (Data,Var,Rel)
  259. 0xa1, 0x02, // COLLECTION (Logical)
  260. // ------------------------------ Vertical wheel res multiplier
  261. 0x09, 0x48, // USAGE (Resolution Multiplier)
  262. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  263. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  264. 0x35, 0x01, // PHYSICAL_MINIMUM (1)
  265. 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
  266. 0x75, 0x02, // REPORT_SIZE (2)
  267. 0x95, 0x01, // REPORT_COUNT (1)
  268. 0xa4, // PUSH
  269. 0xb1, 0x02, // FEATURE (Data,Var,Abs)
  270. // ------------------------------ Vertical wheel
  271. 0x09, 0x38, // USAGE (Wheel)
  272. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  273. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  274. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  275. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  276. 0x75, 0x08, // REPORT_SIZE (8)
  277. 0x81, 0x06, // INPUT (Data,Var,Rel)
  278. 0xc0, // END_COLLECTION
  279. 0xa1, 0x02, // COLLECTION (Logical)
  280. // ------------------------------ Horizontal wheel res multiplier
  281. 0x09, 0x48, // USAGE (Resolution Multiplier)
  282. 0xb4, // POP
  283. 0xb1, 0x02, // FEATURE (Data,Var,Abs)
  284. // ------------------------------ Padding for Feature report
  285. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  286. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  287. 0x75, 0x04, // REPORT_SIZE (4)
  288. 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
  289. // ------------------------------ Horizontal wheel
  290. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  291. 0x0a, 0x38, 0x02, // USAGE (AC Pan)
  292. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  293. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  294. 0x75, 0x08, // REPORT_SIZE (8)
  295. 0x81, 0x06, // INPUT (Data,Var,Rel)
  296. 0xc0, // END_COLLECTION
  297. 0xc0, // END_COLLECTION
  298. 0xc0, // END_COLLECTION
  299. 0xc0 // END_COLLECTION
  300. };
  301. /*
  302. * Descriptor for compite device: Keyboard + Mouse
  303. *
  304. * contains: device, interface, HID and endpoint descriptors
  305. */
  306. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  307. PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
  308. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  309. USBDESCR_CONFIG, /* descriptor type */
  310. 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
  311. //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
  312. /* total length of data returned (including inlined descriptors) */
  313. 2, /* number of interfaces in this configuration */
  314. 1, /* index of this configuration */
  315. 0, /* configuration name string index */
  316. #if USB_CFG_IS_SELF_POWERED
  317. (1 << 7) | USBATTR_SELFPOWER, /* attributes */
  318. #else
  319. (1 << 7), /* attributes */
  320. #endif
  321. USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
  322. /*
  323. * Keyboard interface
  324. */
  325. /* Interface descriptor */
  326. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  327. USBDESCR_INTERFACE, /* descriptor type */
  328. 0, /* index of this interface */
  329. 0, /* alternate setting for this interface */
  330. USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
  331. USB_CFG_INTERFACE_CLASS,
  332. USB_CFG_INTERFACE_SUBCLASS,
  333. USB_CFG_INTERFACE_PROTOCOL,
  334. 0, /* string index for interface */
  335. /* HID descriptor */
  336. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  337. USBDESCR_HID, /* descriptor type: HID */
  338. 0x01, 0x01, /* BCD representation of HID version */
  339. 0x00, /* target country code */
  340. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  341. 0x22, /* descriptor type: report */
  342. sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
  343. /* Endpoint descriptor */
  344. #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  345. 7, /* sizeof(usbDescrEndpoint) */
  346. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  347. (char)0x81, /* IN endpoint number 1 */
  348. 0x03, /* attrib: Interrupt endpoint */
  349. 8, 0, /* maximum packet size */
  350. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  351. #endif
  352. /*
  353. * Mouse interface
  354. */
  355. /* Interface descriptor */
  356. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  357. USBDESCR_INTERFACE, /* descriptor type */
  358. 1, /* index of this interface */
  359. 0, /* alternate setting for this interface */
  360. USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
  361. 0x03, /* CLASS: HID */
  362. 0, /* SUBCLASS: none */
  363. 0, /* PROTOCOL: none */
  364. 0, /* string index for interface */
  365. /* HID descriptor */
  366. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  367. USBDESCR_HID, /* descriptor type: HID */
  368. 0x01, 0x01, /* BCD representation of HID version */
  369. 0x00, /* target country code */
  370. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  371. 0x22, /* descriptor type: report */
  372. sizeof(mouse_hid_report), 0, /* total length of report descriptor */
  373. #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
  374. /* Endpoint descriptor */
  375. 7, /* sizeof(usbDescrEndpoint) */
  376. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  377. (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
  378. 0x03, /* attrib: Interrupt endpoint */
  379. 8, 0, /* maximum packet size */
  380. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  381. #endif
  382. };
  383. #endif
  384. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
  385. {
  386. usbMsgLen_t len = 0;
  387. debug("usbFunctionDescriptor: ");
  388. debug_hex(rq->bmRequestType); debug(" ");
  389. debug_hex(rq->bRequest); debug(" ");
  390. debug_hex16(rq->wValue.word); debug(" ");
  391. debug_hex16(rq->wIndex.word); debug(" ");
  392. debug_hex16(rq->wLength.word); debug("\n");
  393. switch (rq->wValue.bytes[1]) {
  394. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  395. case USBDESCR_CONFIG:
  396. usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
  397. len = sizeof(usbDescriptorConfiguration);
  398. break;
  399. #endif
  400. case USBDESCR_HID:
  401. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 18);
  402. len = 9;
  403. break;
  404. case USBDESCR_HID_REPORT:
  405. /* interface index */
  406. switch (rq->wIndex.word) {
  407. case 0:
  408. usbMsgPtr = keyboard_hid_report;
  409. len = sizeof(keyboard_hid_report);
  410. break;
  411. case 1:
  412. usbMsgPtr = mouse_hid_report;
  413. len = sizeof(mouse_hid_report);
  414. break;
  415. }
  416. break;
  417. }
  418. debug("desc len: "); debug_hex(len); debug("\n");
  419. return len;
  420. }