Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

host_vusb.c 15KB

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