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_vusb.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "usbdrv.h"
  2. #include "usbconfig.h"
  3. #include "keyboard.h"
  4. #include "print.h"
  5. #include "host.h"
  6. #include "host_vusb.h"
  7. #define KBUF_SIZE 16
  8. static report_keyboard_t kbuf[KBUF_SIZE];
  9. static uint8_t kbuf_head = 0;
  10. static uint8_t kbuf_tail = 0;
  11. void host_vusb_keyboard_send()
  12. {
  13. while (usbInterruptIsReady() && kbuf_head != kbuf_tail) {
  14. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  15. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  16. }
  17. /*
  18. if (kbuf_head != kbuf_tail) {
  19. if (usbInterruptIsReady()) {
  20. usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
  21. kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
  22. }
  23. }
  24. */
  25. }
  26. void host_keyboard_send(report_keyboard_t *report)
  27. {
  28. uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
  29. if (next != kbuf_tail) {
  30. kbuf[kbuf_head] = *report;
  31. kbuf_head = next;
  32. print("kbuf: "); phex(kbuf_head); phex(kbuf_tail); print("\n");
  33. } else {
  34. print("kbuf: full\n");
  35. // hmm...
  36. /*
  37. matrix_init();
  38. kbuf_head = 0;
  39. kbuf_tail = 0;
  40. */
  41. }
  42. }
  43. void host_mouse_send(report_mouse_t *report)
  44. {
  45. if (usbInterruptIsReady3()) {
  46. usbSetInterrupt3((void *)report, sizeof(*report));
  47. } else {
  48. print("Int3 not ready\n");
  49. }
  50. }
  51. static struct {
  52. uint16_t len;
  53. enum {
  54. NONE,
  55. SET_LED
  56. } kind;
  57. } last_req;
  58. uint8_t host_keyboard_led = 0;
  59. static uchar idleRate;
  60. usbMsgLen_t usbFunctionSetup(uchar data[8])
  61. {
  62. usbRequest_t *rq = (void *)data;
  63. //print("Setup: ");
  64. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
  65. /*
  66. print("CLASS: ");
  67. phex(rq->bRequest); print(" ");
  68. phex16(rq->wValue.word); print(" ");
  69. phex16(rq->wIndex.word); print(" ");
  70. phex16(rq->wLength.word); print(" ");
  71. */
  72. if(rq->bRequest == USBRQ_HID_GET_REPORT){
  73. print(" GET_REPORT");
  74. /* we only have one report type, so don't look at wValue */
  75. usbMsgPtr = (void *)keyboard_report;
  76. return sizeof(*keyboard_report);
  77. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  78. print(" GET_IDLE: ");
  79. phex(idleRate);
  80. usbMsgPtr = &idleRate;
  81. return 1;
  82. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  83. idleRate = rq->wValue.bytes[1];
  84. print(" SET_IDLE: ");
  85. phex(idleRate);
  86. }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
  87. //print(" SET_REPORT: ");
  88. if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
  89. last_req.kind = SET_LED;
  90. last_req.len = rq->wLength.word;
  91. }
  92. return USB_NO_MSG; // to get data in usbFunctionWrite
  93. }
  94. print("\n");
  95. }else{
  96. print("VENDOR\n");
  97. /* no vendor specific requests implemented */
  98. }
  99. return 0; /* default for not implemented requests: return no data back to host */
  100. }
  101. uchar usbFunctionWrite(uchar *data, uchar len)
  102. {
  103. if (last_req.len == 0) {
  104. return -1;
  105. }
  106. switch (last_req.kind) {
  107. case SET_LED:
  108. //print("SET_LED\n");
  109. host_keyboard_led = data[0];
  110. last_req.len = 0;
  111. return 1;
  112. break;
  113. case NONE:
  114. default:
  115. return -1;
  116. break;
  117. }
  118. return 1;
  119. }
  120. PROGMEM uchar keyboard_hid_report[] = {
  121. 0x05, 0x01, // Usage Page (Generic Desktop),
  122. 0x09, 0x06, // Usage (Keyboard),
  123. 0xA1, 0x01, // Collection (Application),
  124. 0x75, 0x01, // Report Size (1),
  125. 0x95, 0x08, // Report Count (8),
  126. 0x05, 0x07, // Usage Page (Key Codes),
  127. 0x19, 0xE0, // Usage Minimum (224),
  128. 0x29, 0xE7, // Usage Maximum (231),
  129. 0x15, 0x00, // Logical Minimum (0),
  130. 0x25, 0x01, // Logical Maximum (1),
  131. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  132. 0x95, 0x01, // Report Count (1),
  133. 0x75, 0x08, // Report Size (8),
  134. 0x81, 0x03, // Input (Constant), ;Reserved byte
  135. 0x95, 0x05, // Report Count (5),
  136. 0x75, 0x01, // Report Size (1),
  137. 0x05, 0x08, // Usage Page (LEDs),
  138. 0x19, 0x01, // Usage Minimum (1),
  139. 0x29, 0x05, // Usage Maximum (5),
  140. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  141. 0x95, 0x01, // Report Count (1),
  142. 0x75, 0x03, // Report Size (3),
  143. 0x91, 0x03, // Output (Constant), ;LED report padding
  144. 0x95, 0x06, // Report Count (6),
  145. 0x75, 0x08, // Report Size (8),
  146. 0x15, 0x00, // Logical Minimum (0),
  147. 0x25, 0xFF, // Logical Maximum(255),
  148. 0x05, 0x07, // Usage Page (Key Codes),
  149. 0x19, 0x00, // Usage Minimum (0),
  150. 0x29, 0xFF, // Usage Maximum (255),
  151. 0x81, 0x00, // Input (Data, Array),
  152. 0xc0 // End Collection
  153. };
  154. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  155. // http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
  156. // http://www.keil.com/forum/15671/
  157. // http://www.microsoft.com/whdc/device/input/wheel.mspx
  158. PROGMEM uchar mouse_hid_report[] = {
  159. /* from HID 1.11 spec example */
  160. 0x05, 0x01, // Usage Page (Generic Desktop),
  161. 0x09, 0x02, // Usage (Mouse),
  162. 0xA1, 0x01, // Collection (Application),
  163. 0x09, 0x01, // Usage (Pointer),
  164. 0xA1, 0x00, // Collection (Physical),
  165. 0x05, 0x09, // Usage Page (Buttons),
  166. 0x19, 0x01, // Usage Minimum (01),
  167. 0x29, 0x03, // Usage Maximun (03),
  168. 0x15, 0x00, // Logical Minimum (0),
  169. 0x25, 0x01, // Logical Maximum (1),
  170. 0x95, 0x03, // Report Count (3),
  171. 0x75, 0x01, // Report Size (1),
  172. 0x81, 0x02, // Input (Data, Variable, Absolute), ;3 button bits
  173. 0x95, 0x01, // Report Count (1),
  174. 0x75, 0x05, // Report Size (5),
  175. 0x81, 0x01, // Input (Constant), ;5 bit padding
  176. 0x05, 0x01, // Usage Page (Generic Desktop),
  177. 0x09, 0x30, // Usage (X),
  178. 0x09, 0x31, // Usage (Y),
  179. 0x15, 0x81, // Logical Minimum (-127),
  180. 0x25, 0x7F, // Logical Maximum (127),
  181. 0x75, 0x08, // Report Size (8),
  182. 0x95, 0x02, // Report Count (2),
  183. 0x81, 0x06, // Input (Data, Variable, Relative), ;2 position bytes (X & Y)
  184. 0xC0, // End Collection,
  185. 0xC0, // End Collection
  186. /*
  187. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  188. 0x09, 0x02, // USAGE (Mouse)
  189. 0xa1, 0x01, // COLLECTION (Application)
  190. 0x09, 0x02, // USAGE (Mouse)
  191. 0xa1, 0x02, // COLLECTION (Logical)
  192. 0x09, 0x01, // USAGE (Pointer)
  193. 0xa1, 0x00, // COLLECTION (Physical)
  194. // ------------------------------ Buttons
  195. 0x05, 0x09, // USAGE_PAGE (Button)
  196. 0x19, 0x01, // USAGE_MINIMUM (Button 1)
  197. 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
  198. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  199. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  200. 0x75, 0x01, // REPORT_SIZE (1)
  201. 0x95, 0x05, // REPORT_COUNT (5)
  202. 0x81, 0x02, // INPUT (Data,Var,Abs)
  203. // ------------------------------ Padding
  204. 0x75, 0x03, // REPORT_SIZE (3)
  205. 0x95, 0x01, // REPORT_COUNT (1)
  206. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  207. // ------------------------------ X,Y position
  208. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  209. 0x09, 0x30, // USAGE (X)
  210. 0x09, 0x31, // USAGE (Y)
  211. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  212. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  213. 0x75, 0x08, // REPORT_SIZE (8)
  214. 0x95, 0x02, // REPORT_COUNT (2)
  215. 0x81, 0x06, // INPUT (Data,Var,Rel)
  216. 0xa1, 0x02, // COLLECTION (Logical)
  217. // ------------------------------ Vertical wheel res multiplier
  218. 0x09, 0x48, // USAGE (Resolution Multiplier)
  219. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  220. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  221. 0x35, 0x01, // PHYSICAL_MINIMUM (1)
  222. 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
  223. 0x75, 0x02, // REPORT_SIZE (2)
  224. 0x95, 0x01, // REPORT_COUNT (1)
  225. 0xa4, // PUSH
  226. 0xb1, 0x02, // FEATURE (Data,Var,Abs)
  227. // ------------------------------ Vertical wheel
  228. 0x09, 0x38, // USAGE (Wheel)
  229. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  230. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  231. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  232. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  233. 0x75, 0x08, // REPORT_SIZE (8)
  234. 0x81, 0x06, // INPUT (Data,Var,Rel)
  235. 0xc0, // END_COLLECTION
  236. 0xa1, 0x02, // COLLECTION (Logical)
  237. // ------------------------------ Horizontal wheel res multiplier
  238. 0x09, 0x48, // USAGE (Resolution Multiplier)
  239. 0xb4, // POP
  240. 0xb1, 0x02, // FEATURE (Data,Var,Abs)
  241. // ------------------------------ Padding for Feature report
  242. 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
  243. 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
  244. 0x75, 0x04, // REPORT_SIZE (4)
  245. 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
  246. // ------------------------------ Horizontal wheel
  247. 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
  248. 0x0a, 0x38, 0x02, // USAGE (AC Pan)
  249. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  250. 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
  251. 0x75, 0x08, // REPORT_SIZE (8)
  252. 0x81, 0x06, // INPUT (Data,Var,Rel)
  253. 0xc0, // END_COLLECTION
  254. 0xc0, // END_COLLECTION
  255. 0xc0, // END_COLLECTION
  256. 0xc0 // END_COLLECTION
  257. */
  258. };
  259. /* Descriptor for compite device: Keyboard + Mouse */
  260. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  261. PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
  262. 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
  263. USBDESCR_CONFIG, /* descriptor type */
  264. 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
  265. //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
  266. /* total length of data returned (including inlined descriptors) */
  267. 2, /* number of interfaces in this configuration */
  268. 1, /* index of this configuration */
  269. 0, /* configuration name string index */
  270. #if USB_CFG_IS_SELF_POWERED
  271. (1 << 7) | USBATTR_SELFPOWER, /* attributes */
  272. #else
  273. (1 << 7), /* attributes */
  274. #endif
  275. USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
  276. /*
  277. * Keyboard interface
  278. */
  279. /* Interface descriptor */
  280. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  281. USBDESCR_INTERFACE, /* descriptor type */
  282. 0, /* index of this interface */
  283. 0, /* alternate setting for this interface */
  284. USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
  285. USB_CFG_INTERFACE_CLASS,
  286. USB_CFG_INTERFACE_SUBCLASS,
  287. USB_CFG_INTERFACE_PROTOCOL,
  288. 0, /* string index for interface */
  289. /* HID descriptor */
  290. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  291. USBDESCR_HID, /* descriptor type: HID */
  292. 0x01, 0x01, /* BCD representation of HID version */
  293. 0x00, /* target country code */
  294. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  295. 0x22, /* descriptor type: report */
  296. sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
  297. /* Endpoint descriptor */
  298. #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
  299. 7, /* sizeof(usbDescrEndpoint) */
  300. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  301. (char)0x81, /* IN endpoint number 1 */
  302. 0x03, /* attrib: Interrupt endpoint */
  303. 8, 0, /* maximum packet size */
  304. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  305. #endif
  306. /*
  307. * Mouse interface
  308. */
  309. /* Interface descriptor */
  310. 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
  311. USBDESCR_INTERFACE, /* descriptor type */
  312. 1, /* index of this interface */
  313. 0, /* alternate setting for this interface */
  314. USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
  315. 0x03, /* CLASS: HID */
  316. 0, /* SUBCLASS: none */
  317. 0, /* PROTOCOL: none */
  318. 0, /* string index for interface */
  319. /* HID descriptor */
  320. 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
  321. USBDESCR_HID, /* descriptor type: HID */
  322. 0x01, 0x01, /* BCD representation of HID version */
  323. 0x00, /* target country code */
  324. 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
  325. 0x22, /* descriptor type: report */
  326. sizeof(mouse_hid_report), 0, /* total length of report descriptor */
  327. #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
  328. /* Endpoint descriptor */
  329. 7, /* sizeof(usbDescrEndpoint) */
  330. USBDESCR_ENDPOINT, /* descriptor type = endpoint */
  331. (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
  332. 0x03, /* attrib: Interrupt endpoint */
  333. 8, 0, /* maximum packet size */
  334. USB_CFG_INTR_POLL_INTERVAL, /* in ms */
  335. #endif
  336. };
  337. #endif
  338. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
  339. {
  340. usbMsgLen_t len = 0;
  341. print("usbFunctionDescriptor: ");
  342. phex(rq->bmRequestType); print(" ");
  343. phex(rq->bRequest); print(" ");
  344. phex16(rq->wValue.word); print(" ");
  345. phex16(rq->wIndex.word); print(" ");
  346. phex16(rq->wLength.word); print("\n");
  347. switch (rq->wValue.bytes[1]) {
  348. #if USB_CFG_DESCR_PROPS_CONFIGURATION
  349. case USBDESCR_CONFIG:
  350. usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
  351. len = sizeof(usbDescriptorConfiguration);
  352. break;
  353. #endif
  354. case USBDESCR_HID:
  355. usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 18);
  356. len = 9;
  357. break;
  358. case USBDESCR_HID_REPORT:
  359. /* interface index */
  360. switch (rq->wIndex.word) {
  361. case 0:
  362. usbMsgPtr = keyboard_hid_report;
  363. len = sizeof(keyboard_hid_report);
  364. break;
  365. case 1:
  366. usbMsgPtr = mouse_hid_report;
  367. len = sizeof(mouse_hid_report);
  368. break;
  369. }
  370. break;
  371. }
  372. print("desc len: "); phex(len); print("\n");
  373. return len;
  374. }