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

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