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.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

vusb.c 18KB

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