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.

vusb.c 18KB

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