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

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