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

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