Keyboard firmwares for Atmel AVR and Cortex-M
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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