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 18KB

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