Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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