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