usb-hid.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * QEMU USB HID devices
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. * Copyright (c) 2007 OpenMoko, Inc. (andrew@openedhand.com)
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "hw.h"
  26. #include "console.h"
  27. #include "usb.h"
  28. /* HID interface requests */
  29. #define GET_REPORT 0xa101
  30. #define GET_IDLE 0xa102
  31. #define GET_PROTOCOL 0xa103
  32. #define SET_REPORT 0x2109
  33. #define SET_IDLE 0x210a
  34. #define SET_PROTOCOL 0x210b
  35. /* HID descriptor types */
  36. #define USB_DT_HID 0x21
  37. #define USB_DT_REPORT 0x22
  38. #define USB_DT_PHY 0x23
  39. #define USB_MOUSE 1
  40. #define USB_TABLET 2
  41. #define USB_KEYBOARD 3
  42. typedef struct USBMouseState {
  43. int dx, dy, dz, buttons_state;
  44. int x, y;
  45. int mouse_grabbed;
  46. QEMUPutMouseEntry *eh_entry;
  47. } USBMouseState;
  48. typedef struct USBKeyboardState {
  49. uint16_t modifiers;
  50. uint8_t leds;
  51. uint8_t key[16];
  52. int keys;
  53. } USBKeyboardState;
  54. typedef struct USBHIDState {
  55. USBDevice dev;
  56. union {
  57. USBMouseState ptr;
  58. USBKeyboardState kbd;
  59. };
  60. int kind;
  61. int protocol;
  62. uint8_t idle;
  63. int changed;
  64. void *datain_opaque;
  65. void (*datain)(void *);
  66. } USBHIDState;
  67. /* mostly the same values as the Bochs USB Mouse device */
  68. static const uint8_t qemu_mouse_dev_descriptor[] = {
  69. 0x12, /* u8 bLength; */
  70. 0x01, /* u8 bDescriptorType; Device */
  71. 0x00, 0x01, /* u16 bcdUSB; v1.0 */
  72. 0x00, /* u8 bDeviceClass; */
  73. 0x00, /* u8 bDeviceSubClass; */
  74. 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
  75. 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
  76. 0x27, 0x06, /* u16 idVendor; */
  77. 0x01, 0x00, /* u16 idProduct; */
  78. 0x00, 0x00, /* u16 bcdDevice */
  79. 0x03, /* u8 iManufacturer; */
  80. 0x02, /* u8 iProduct; */
  81. 0x01, /* u8 iSerialNumber; */
  82. 0x01 /* u8 bNumConfigurations; */
  83. };
  84. static const uint8_t qemu_mouse_config_descriptor[] = {
  85. /* one configuration */
  86. 0x09, /* u8 bLength; */
  87. 0x02, /* u8 bDescriptorType; Configuration */
  88. 0x22, 0x00, /* u16 wTotalLength; */
  89. 0x01, /* u8 bNumInterfaces; (1) */
  90. 0x01, /* u8 bConfigurationValue; */
  91. 0x04, /* u8 iConfiguration; */
  92. 0xa0, /* u8 bmAttributes;
  93. Bit 7: must be set,
  94. 6: Self-powered,
  95. 5: Remote wakeup,
  96. 4..0: resvd */
  97. 50, /* u8 MaxPower; */
  98. /* USB 1.1:
  99. * USB 2.0, single TT organization (mandatory):
  100. * one interface, protocol 0
  101. *
  102. * USB 2.0, multiple TT organization (optional):
  103. * two interfaces, protocols 1 (like single TT)
  104. * and 2 (multiple TT mode) ... config is
  105. * sometimes settable
  106. * NOT IMPLEMENTED
  107. */
  108. /* one interface */
  109. 0x09, /* u8 if_bLength; */
  110. 0x04, /* u8 if_bDescriptorType; Interface */
  111. 0x00, /* u8 if_bInterfaceNumber; */
  112. 0x00, /* u8 if_bAlternateSetting; */
  113. 0x01, /* u8 if_bNumEndpoints; */
  114. 0x03, /* u8 if_bInterfaceClass; */
  115. 0x01, /* u8 if_bInterfaceSubClass; */
  116. 0x02, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  117. 0x07, /* u8 if_iInterface; */
  118. /* HID descriptor */
  119. 0x09, /* u8 bLength; */
  120. 0x21, /* u8 bDescriptorType; */
  121. 0x01, 0x00, /* u16 HID_class */
  122. 0x00, /* u8 country_code */
  123. 0x01, /* u8 num_descriptors */
  124. 0x22, /* u8 type; Report */
  125. 52, 0, /* u16 len */
  126. /* one endpoint (status change endpoint) */
  127. 0x07, /* u8 ep_bLength; */
  128. 0x05, /* u8 ep_bDescriptorType; Endpoint */
  129. 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
  130. 0x03, /* u8 ep_bmAttributes; Interrupt */
  131. 0x04, 0x00, /* u16 ep_wMaxPacketSize; */
  132. 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  133. };
  134. static const uint8_t qemu_tablet_config_descriptor[] = {
  135. /* one configuration */
  136. 0x09, /* u8 bLength; */
  137. 0x02, /* u8 bDescriptorType; Configuration */
  138. 0x22, 0x00, /* u16 wTotalLength; */
  139. 0x01, /* u8 bNumInterfaces; (1) */
  140. 0x01, /* u8 bConfigurationValue; */
  141. 0x05, /* u8 iConfiguration; */
  142. 0xa0, /* u8 bmAttributes;
  143. Bit 7: must be set,
  144. 6: Self-powered,
  145. 5: Remote wakeup,
  146. 4..0: resvd */
  147. 50, /* u8 MaxPower; */
  148. /* USB 1.1:
  149. * USB 2.0, single TT organization (mandatory):
  150. * one interface, protocol 0
  151. *
  152. * USB 2.0, multiple TT organization (optional):
  153. * two interfaces, protocols 1 (like single TT)
  154. * and 2 (multiple TT mode) ... config is
  155. * sometimes settable
  156. * NOT IMPLEMENTED
  157. */
  158. /* one interface */
  159. 0x09, /* u8 if_bLength; */
  160. 0x04, /* u8 if_bDescriptorType; Interface */
  161. 0x00, /* u8 if_bInterfaceNumber; */
  162. 0x00, /* u8 if_bAlternateSetting; */
  163. 0x01, /* u8 if_bNumEndpoints; */
  164. 0x03, /* u8 if_bInterfaceClass; */
  165. 0x01, /* u8 if_bInterfaceSubClass; */
  166. 0x02, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  167. 0x07, /* u8 if_iInterface; */
  168. /* HID descriptor */
  169. 0x09, /* u8 bLength; */
  170. 0x21, /* u8 bDescriptorType; */
  171. 0x01, 0x00, /* u16 HID_class */
  172. 0x00, /* u8 country_code */
  173. 0x01, /* u8 num_descriptors */
  174. 0x22, /* u8 type; Report */
  175. 74, 0, /* u16 len */
  176. /* one endpoint (status change endpoint) */
  177. 0x07, /* u8 ep_bLength; */
  178. 0x05, /* u8 ep_bDescriptorType; Endpoint */
  179. 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
  180. 0x03, /* u8 ep_bmAttributes; Interrupt */
  181. 0x08, 0x00, /* u16 ep_wMaxPacketSize; */
  182. 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  183. };
  184. static const uint8_t qemu_keyboard_config_descriptor[] = {
  185. /* one configuration */
  186. 0x09, /* u8 bLength; */
  187. USB_DT_CONFIG, /* u8 bDescriptorType; Configuration */
  188. 0x22, 0x00, /* u16 wTotalLength; */
  189. 0x01, /* u8 bNumInterfaces; (1) */
  190. 0x01, /* u8 bConfigurationValue; */
  191. 0x06, /* u8 iConfiguration; */
  192. 0xa0, /* u8 bmAttributes;
  193. Bit 7: must be set,
  194. 6: Self-powered,
  195. 5: Remote wakeup,
  196. 4..0: resvd */
  197. 0x32, /* u8 MaxPower; */
  198. /* USB 1.1:
  199. * USB 2.0, single TT organization (mandatory):
  200. * one interface, protocol 0
  201. *
  202. * USB 2.0, multiple TT organization (optional):
  203. * two interfaces, protocols 1 (like single TT)
  204. * and 2 (multiple TT mode) ... config is
  205. * sometimes settable
  206. * NOT IMPLEMENTED
  207. */
  208. /* one interface */
  209. 0x09, /* u8 if_bLength; */
  210. USB_DT_INTERFACE, /* u8 if_bDescriptorType; Interface */
  211. 0x00, /* u8 if_bInterfaceNumber; */
  212. 0x00, /* u8 if_bAlternateSetting; */
  213. 0x01, /* u8 if_bNumEndpoints; */
  214. 0x03, /* u8 if_bInterfaceClass; HID */
  215. 0x01, /* u8 if_bInterfaceSubClass; Boot */
  216. 0x01, /* u8 if_bInterfaceProtocol; Keyboard */
  217. 0x07, /* u8 if_iInterface; */
  218. /* HID descriptor */
  219. 0x09, /* u8 bLength; */
  220. USB_DT_HID, /* u8 bDescriptorType; */
  221. 0x11, 0x01, /* u16 HID_class */
  222. 0x00, /* u8 country_code */
  223. 0x01, /* u8 num_descriptors */
  224. USB_DT_REPORT, /* u8 type; Report */
  225. 0x3f, 0x00, /* u16 len */
  226. /* one endpoint (status change endpoint) */
  227. 0x07, /* u8 ep_bLength; */
  228. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; Endpoint */
  229. USB_DIR_IN | 0x01, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
  230. 0x03, /* u8 ep_bmAttributes; Interrupt */
  231. 0x08, 0x00, /* u16 ep_wMaxPacketSize; */
  232. 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  233. };
  234. static const uint8_t qemu_mouse_hid_report_descriptor[] = {
  235. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  236. 0x09, 0x02, /* Usage (Mouse) */
  237. 0xa1, 0x01, /* Collection (Application) */
  238. 0x09, 0x01, /* Usage (Pointer) */
  239. 0xa1, 0x00, /* Collection (Physical) */
  240. 0x05, 0x09, /* Usage Page (Button) */
  241. 0x19, 0x01, /* Usage Minimum (1) */
  242. 0x29, 0x03, /* Usage Maximum (3) */
  243. 0x15, 0x00, /* Logical Minimum (0) */
  244. 0x25, 0x01, /* Logical Maximum (1) */
  245. 0x95, 0x03, /* Report Count (3) */
  246. 0x75, 0x01, /* Report Size (1) */
  247. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  248. 0x95, 0x01, /* Report Count (1) */
  249. 0x75, 0x05, /* Report Size (5) */
  250. 0x81, 0x01, /* Input (Constant) */
  251. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  252. 0x09, 0x30, /* Usage (X) */
  253. 0x09, 0x31, /* Usage (Y) */
  254. 0x09, 0x38, /* Usage (Wheel) */
  255. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  256. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  257. 0x75, 0x08, /* Report Size (8) */
  258. 0x95, 0x03, /* Report Count (3) */
  259. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  260. 0xc0, /* End Collection */
  261. 0xc0, /* End Collection */
  262. };
  263. static const uint8_t qemu_tablet_hid_report_descriptor[] = {
  264. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  265. 0x09, 0x01, /* Usage (Pointer) */
  266. 0xa1, 0x01, /* Collection (Application) */
  267. 0x09, 0x01, /* Usage (Pointer) */
  268. 0xa1, 0x00, /* Collection (Physical) */
  269. 0x05, 0x09, /* Usage Page (Button) */
  270. 0x19, 0x01, /* Usage Minimum (1) */
  271. 0x29, 0x03, /* Usage Maximum (3) */
  272. 0x15, 0x00, /* Logical Minimum (0) */
  273. 0x25, 0x01, /* Logical Maximum (1) */
  274. 0x95, 0x03, /* Report Count (3) */
  275. 0x75, 0x01, /* Report Size (1) */
  276. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  277. 0x95, 0x01, /* Report Count (1) */
  278. 0x75, 0x05, /* Report Size (5) */
  279. 0x81, 0x01, /* Input (Constant) */
  280. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  281. 0x09, 0x30, /* Usage (X) */
  282. 0x09, 0x31, /* Usage (Y) */
  283. 0x15, 0x00, /* Logical Minimum (0) */
  284. 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
  285. 0x35, 0x00, /* Physical Minimum (0) */
  286. 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
  287. 0x75, 0x10, /* Report Size (16) */
  288. 0x95, 0x02, /* Report Count (2) */
  289. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  290. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  291. 0x09, 0x38, /* Usage (Wheel) */
  292. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  293. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  294. 0x35, 0x00, /* Physical Minimum (same as logical) */
  295. 0x45, 0x00, /* Physical Maximum (same as logical) */
  296. 0x75, 0x08, /* Report Size (8) */
  297. 0x95, 0x01, /* Report Count (1) */
  298. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  299. 0xc0, /* End Collection */
  300. 0xc0, /* End Collection */
  301. };
  302. static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
  303. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  304. 0x09, 0x06, /* Usage (Keyboard) */
  305. 0xa1, 0x01, /* Collection (Application) */
  306. 0x75, 0x01, /* Report Size (1) */
  307. 0x95, 0x08, /* Report Count (8) */
  308. 0x05, 0x07, /* Usage Page (Key Codes) */
  309. 0x19, 0xe0, /* Usage Minimum (224) */
  310. 0x29, 0xe7, /* Usage Maximum (231) */
  311. 0x15, 0x00, /* Logical Minimum (0) */
  312. 0x25, 0x01, /* Logical Maximum (1) */
  313. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  314. 0x95, 0x01, /* Report Count (1) */
  315. 0x75, 0x08, /* Report Size (8) */
  316. 0x81, 0x01, /* Input (Constant) */
  317. 0x95, 0x05, /* Report Count (5) */
  318. 0x75, 0x01, /* Report Size (1) */
  319. 0x05, 0x08, /* Usage Page (LEDs) */
  320. 0x19, 0x01, /* Usage Minimum (1) */
  321. 0x29, 0x05, /* Usage Maximum (5) */
  322. 0x91, 0x02, /* Output (Data, Variable, Absolute) */
  323. 0x95, 0x01, /* Report Count (1) */
  324. 0x75, 0x03, /* Report Size (3) */
  325. 0x91, 0x01, /* Output (Constant) */
  326. 0x95, 0x06, /* Report Count (6) */
  327. 0x75, 0x08, /* Report Size (8) */
  328. 0x15, 0x00, /* Logical Minimum (0) */
  329. 0x25, 0xff, /* Logical Maximum (255) */
  330. 0x05, 0x07, /* Usage Page (Key Codes) */
  331. 0x19, 0x00, /* Usage Minimum (0) */
  332. 0x29, 0xff, /* Usage Maximum (255) */
  333. 0x81, 0x00, /* Input (Data, Array) */
  334. 0xc0, /* End Collection */
  335. };
  336. #define USB_HID_USAGE_ERROR_ROLLOVER 0x01
  337. #define USB_HID_USAGE_POSTFAIL 0x02
  338. #define USB_HID_USAGE_ERROR_UNDEFINED 0x03
  339. /* Indices are QEMU keycodes, values are from HID Usage Table. Indices
  340. * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d. */
  341. static const uint8_t usb_hid_usage_keys[0x100] = {
  342. 0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  343. 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
  344. 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
  345. 0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
  346. 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
  347. 0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
  348. 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
  349. 0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
  350. 0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
  351. 0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
  352. 0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44,
  353. 0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  354. 0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
  355. 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
  356. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  357. 0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65,
  358. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  359. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  361. 0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
  362. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  363. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  364. 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
  365. 0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  366. 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a,
  367. 0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
  368. 0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
  369. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  370. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  371. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  372. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  373. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  374. };
  375. static void usb_hid_changed(USBHIDState *hs)
  376. {
  377. hs->changed = 1;
  378. if (hs->datain)
  379. hs->datain(hs->datain_opaque);
  380. }
  381. static void usb_mouse_event(void *opaque,
  382. int dx1, int dy1, int dz1, int buttons_state)
  383. {
  384. USBHIDState *hs = opaque;
  385. USBMouseState *s = &hs->ptr;
  386. s->dx += dx1;
  387. s->dy += dy1;
  388. s->dz += dz1;
  389. s->buttons_state = buttons_state;
  390. usb_hid_changed(hs);
  391. }
  392. static void usb_tablet_event(void *opaque,
  393. int x, int y, int dz, int buttons_state)
  394. {
  395. USBHIDState *hs = opaque;
  396. USBMouseState *s = &hs->ptr;
  397. s->x = x;
  398. s->y = y;
  399. s->dz += dz;
  400. s->buttons_state = buttons_state;
  401. usb_hid_changed(hs);
  402. }
  403. static void usb_keyboard_event(void *opaque, int keycode)
  404. {
  405. USBHIDState *hs = opaque;
  406. USBKeyboardState *s = &hs->kbd;
  407. uint8_t hid_code, key;
  408. int i;
  409. key = keycode & 0x7f;
  410. hid_code = usb_hid_usage_keys[key | ((s->modifiers >> 1) & (1 << 7))];
  411. s->modifiers &= ~(1 << 8);
  412. switch (hid_code) {
  413. case 0x00:
  414. return;
  415. case 0xe0:
  416. if (s->modifiers & (1 << 9)) {
  417. s->modifiers ^= 3 << 8;
  418. return;
  419. }
  420. case 0xe1 ... 0xe7:
  421. if (keycode & (1 << 7)) {
  422. s->modifiers &= ~(1 << (hid_code & 0x0f));
  423. return;
  424. }
  425. case 0xe8 ... 0xef:
  426. s->modifiers |= 1 << (hid_code & 0x0f);
  427. return;
  428. }
  429. if (keycode & (1 << 7)) {
  430. for (i = s->keys - 1; i >= 0; i --)
  431. if (s->key[i] == hid_code) {
  432. s->key[i] = s->key[-- s->keys];
  433. s->key[s->keys] = 0x00;
  434. usb_hid_changed(hs);
  435. break;
  436. }
  437. if (i < 0)
  438. return;
  439. } else {
  440. for (i = s->keys - 1; i >= 0; i --)
  441. if (s->key[i] == hid_code)
  442. break;
  443. if (i < 0) {
  444. if (s->keys < sizeof(s->key))
  445. s->key[s->keys ++] = hid_code;
  446. } else
  447. return;
  448. }
  449. usb_hid_changed(hs);
  450. }
  451. static inline int int_clamp(int val, int vmin, int vmax)
  452. {
  453. if (val < vmin)
  454. return vmin;
  455. else if (val > vmax)
  456. return vmax;
  457. else
  458. return val;
  459. }
  460. static int usb_mouse_poll(USBHIDState *hs, uint8_t *buf, int len)
  461. {
  462. int dx, dy, dz, b, l;
  463. USBMouseState *s = &hs->ptr;
  464. if (!s->mouse_grabbed) {
  465. s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, hs,
  466. 0, "QEMU USB Mouse");
  467. s->mouse_grabbed = 1;
  468. }
  469. dx = int_clamp(s->dx, -127, 127);
  470. dy = int_clamp(s->dy, -127, 127);
  471. dz = int_clamp(s->dz, -127, 127);
  472. s->dx -= dx;
  473. s->dy -= dy;
  474. s->dz -= dz;
  475. /* Appears we have to invert the wheel direction */
  476. dz = 0 - dz;
  477. b = 0;
  478. if (s->buttons_state & MOUSE_EVENT_LBUTTON)
  479. b |= 0x01;
  480. if (s->buttons_state & MOUSE_EVENT_RBUTTON)
  481. b |= 0x02;
  482. if (s->buttons_state & MOUSE_EVENT_MBUTTON)
  483. b |= 0x04;
  484. l = 0;
  485. if (len > l)
  486. buf[l ++] = b;
  487. if (len > l)
  488. buf[l ++] = dx;
  489. if (len > l)
  490. buf[l ++] = dy;
  491. if (len > l)
  492. buf[l ++] = dz;
  493. return l;
  494. }
  495. static int usb_tablet_poll(USBHIDState *hs, uint8_t *buf, int len)
  496. {
  497. int dz, b, l;
  498. USBMouseState *s = &hs->ptr;
  499. if (!s->mouse_grabbed) {
  500. s->eh_entry = qemu_add_mouse_event_handler(usb_tablet_event, hs,
  501. 1, "QEMU USB Tablet");
  502. s->mouse_grabbed = 1;
  503. }
  504. dz = int_clamp(s->dz, -127, 127);
  505. s->dz -= dz;
  506. /* Appears we have to invert the wheel direction */
  507. dz = 0 - dz;
  508. b = 0;
  509. if (s->buttons_state & MOUSE_EVENT_LBUTTON)
  510. b |= 0x01;
  511. if (s->buttons_state & MOUSE_EVENT_RBUTTON)
  512. b |= 0x02;
  513. if (s->buttons_state & MOUSE_EVENT_MBUTTON)
  514. b |= 0x04;
  515. buf[0] = b;
  516. buf[1] = s->x & 0xff;
  517. buf[2] = s->x >> 8;
  518. buf[3] = s->y & 0xff;
  519. buf[4] = s->y >> 8;
  520. buf[5] = dz;
  521. l = 6;
  522. return l;
  523. }
  524. static int usb_keyboard_poll(USBKeyboardState *s, uint8_t *buf, int len)
  525. {
  526. if (len < 2)
  527. return 0;
  528. buf[0] = s->modifiers & 0xff;
  529. buf[1] = 0;
  530. if (s->keys > 6)
  531. memset(buf + 2, USB_HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
  532. else
  533. memcpy(buf + 2, s->key, MIN(8, len) - 2);
  534. return MIN(8, len);
  535. }
  536. static int usb_keyboard_write(USBKeyboardState *s, uint8_t *buf, int len)
  537. {
  538. if (len > 0) {
  539. /* 0x01: Num Lock LED
  540. * 0x02: Caps Lock LED
  541. * 0x04: Scroll Lock LED
  542. * 0x08: Compose LED
  543. * 0x10: Kana LED */
  544. s->leds = buf[0];
  545. }
  546. return 0;
  547. }
  548. static void usb_mouse_handle_reset(USBDevice *dev)
  549. {
  550. USBHIDState *s = (USBHIDState *)dev;
  551. s->ptr.dx = 0;
  552. s->ptr.dy = 0;
  553. s->ptr.dz = 0;
  554. s->ptr.x = 0;
  555. s->ptr.y = 0;
  556. s->ptr.buttons_state = 0;
  557. s->protocol = 1;
  558. }
  559. static void usb_keyboard_handle_reset(USBDevice *dev)
  560. {
  561. USBHIDState *s = (USBHIDState *)dev;
  562. qemu_add_kbd_event_handler(usb_keyboard_event, s);
  563. s->protocol = 1;
  564. }
  565. static int usb_hid_handle_control(USBDevice *dev, int request, int value,
  566. int index, int length, uint8_t *data)
  567. {
  568. USBHIDState *s = (USBHIDState *)dev;
  569. int ret = 0;
  570. switch(request) {
  571. case DeviceRequest | USB_REQ_GET_STATUS:
  572. data[0] = (1 << USB_DEVICE_SELF_POWERED) |
  573. (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
  574. data[1] = 0x00;
  575. ret = 2;
  576. break;
  577. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  578. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  579. dev->remote_wakeup = 0;
  580. } else {
  581. goto fail;
  582. }
  583. ret = 0;
  584. break;
  585. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  586. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  587. dev->remote_wakeup = 1;
  588. } else {
  589. goto fail;
  590. }
  591. ret = 0;
  592. break;
  593. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  594. dev->addr = value;
  595. ret = 0;
  596. break;
  597. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  598. switch(value >> 8) {
  599. case USB_DT_DEVICE:
  600. memcpy(data, qemu_mouse_dev_descriptor,
  601. sizeof(qemu_mouse_dev_descriptor));
  602. ret = sizeof(qemu_mouse_dev_descriptor);
  603. break;
  604. case USB_DT_CONFIG:
  605. if (s->kind == USB_MOUSE) {
  606. memcpy(data, qemu_mouse_config_descriptor,
  607. sizeof(qemu_mouse_config_descriptor));
  608. ret = sizeof(qemu_mouse_config_descriptor);
  609. } else if (s->kind == USB_TABLET) {
  610. memcpy(data, qemu_tablet_config_descriptor,
  611. sizeof(qemu_tablet_config_descriptor));
  612. ret = sizeof(qemu_tablet_config_descriptor);
  613. } else if (s->kind == USB_KEYBOARD) {
  614. memcpy(data, qemu_keyboard_config_descriptor,
  615. sizeof(qemu_keyboard_config_descriptor));
  616. ret = sizeof(qemu_keyboard_config_descriptor);
  617. }
  618. break;
  619. case USB_DT_STRING:
  620. switch(value & 0xff) {
  621. case 0:
  622. /* language ids */
  623. data[0] = 4;
  624. data[1] = 3;
  625. data[2] = 0x09;
  626. data[3] = 0x04;
  627. ret = 4;
  628. break;
  629. case 1:
  630. /* serial number */
  631. ret = set_usb_string(data, "1");
  632. break;
  633. case 2:
  634. /* product description */
  635. ret = set_usb_string(data, s->dev.devname);
  636. break;
  637. case 3:
  638. /* vendor description */
  639. ret = set_usb_string(data, "QEMU " QEMU_VERSION);
  640. break;
  641. case 4:
  642. ret = set_usb_string(data, "HID Mouse");
  643. break;
  644. case 5:
  645. ret = set_usb_string(data, "HID Tablet");
  646. break;
  647. case 6:
  648. ret = set_usb_string(data, "HID Keyboard");
  649. break;
  650. case 7:
  651. ret = set_usb_string(data, "Endpoint1 Interrupt Pipe");
  652. break;
  653. default:
  654. goto fail;
  655. }
  656. break;
  657. default:
  658. goto fail;
  659. }
  660. break;
  661. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  662. data[0] = 1;
  663. ret = 1;
  664. break;
  665. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  666. ret = 0;
  667. break;
  668. case DeviceRequest | USB_REQ_GET_INTERFACE:
  669. data[0] = 0;
  670. ret = 1;
  671. break;
  672. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  673. ret = 0;
  674. break;
  675. /* hid specific requests */
  676. case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
  677. switch(value >> 8) {
  678. case 0x22:
  679. if (s->kind == USB_MOUSE) {
  680. memcpy(data, qemu_mouse_hid_report_descriptor,
  681. sizeof(qemu_mouse_hid_report_descriptor));
  682. ret = sizeof(qemu_mouse_hid_report_descriptor);
  683. } else if (s->kind == USB_TABLET) {
  684. memcpy(data, qemu_tablet_hid_report_descriptor,
  685. sizeof(qemu_tablet_hid_report_descriptor));
  686. ret = sizeof(qemu_tablet_hid_report_descriptor);
  687. } else if (s->kind == USB_KEYBOARD) {
  688. memcpy(data, qemu_keyboard_hid_report_descriptor,
  689. sizeof(qemu_keyboard_hid_report_descriptor));
  690. ret = sizeof(qemu_keyboard_hid_report_descriptor);
  691. }
  692. break;
  693. default:
  694. goto fail;
  695. }
  696. break;
  697. case GET_REPORT:
  698. if (s->kind == USB_MOUSE)
  699. ret = usb_mouse_poll(s, data, length);
  700. else if (s->kind == USB_TABLET)
  701. ret = usb_tablet_poll(s, data, length);
  702. else if (s->kind == USB_KEYBOARD)
  703. ret = usb_keyboard_poll(&s->kbd, data, length);
  704. break;
  705. case SET_REPORT:
  706. if (s->kind == USB_KEYBOARD)
  707. ret = usb_keyboard_write(&s->kbd, data, length);
  708. else
  709. goto fail;
  710. break;
  711. case GET_PROTOCOL:
  712. if (s->kind != USB_KEYBOARD)
  713. goto fail;
  714. ret = 1;
  715. data[0] = s->protocol;
  716. break;
  717. case SET_PROTOCOL:
  718. if (s->kind != USB_KEYBOARD)
  719. goto fail;
  720. ret = 0;
  721. s->protocol = value;
  722. break;
  723. case GET_IDLE:
  724. ret = 1;
  725. data[0] = s->idle;
  726. break;
  727. case SET_IDLE:
  728. s->idle = (uint8_t) (value >> 8);
  729. ret = 0;
  730. break;
  731. default:
  732. fail:
  733. ret = USB_RET_STALL;
  734. break;
  735. }
  736. return ret;
  737. }
  738. static int usb_hid_handle_data(USBDevice *dev, USBPacket *p)
  739. {
  740. USBHIDState *s = (USBHIDState *)dev;
  741. int ret = 0;
  742. switch(p->pid) {
  743. case USB_TOKEN_IN:
  744. if (p->devep == 1) {
  745. /* TODO: Implement finite idle delays. */
  746. if (!(s->changed || s->idle))
  747. return USB_RET_NAK;
  748. s->changed = 0;
  749. if (s->kind == USB_MOUSE)
  750. ret = usb_mouse_poll(s, p->data, p->len);
  751. else if (s->kind == USB_TABLET)
  752. ret = usb_tablet_poll(s, p->data, p->len);
  753. else if (s->kind == USB_KEYBOARD)
  754. ret = usb_keyboard_poll(&s->kbd, p->data, p->len);
  755. } else {
  756. goto fail;
  757. }
  758. break;
  759. case USB_TOKEN_OUT:
  760. default:
  761. fail:
  762. ret = USB_RET_STALL;
  763. break;
  764. }
  765. return ret;
  766. }
  767. static void usb_hid_handle_destroy(USBDevice *dev)
  768. {
  769. USBHIDState *s = (USBHIDState *)dev;
  770. if (s->kind != USB_KEYBOARD)
  771. qemu_remove_mouse_event_handler(s->ptr.eh_entry);
  772. /* TODO: else */
  773. qemu_free(s);
  774. }
  775. USBDevice *usb_tablet_init(void)
  776. {
  777. USBHIDState *s;
  778. s = qemu_mallocz(sizeof(USBHIDState));
  779. s->dev.speed = USB_SPEED_FULL;
  780. s->dev.handle_packet = usb_generic_handle_packet;
  781. s->dev.handle_reset = usb_mouse_handle_reset;
  782. s->dev.handle_control = usb_hid_handle_control;
  783. s->dev.handle_data = usb_hid_handle_data;
  784. s->dev.handle_destroy = usb_hid_handle_destroy;
  785. s->kind = USB_TABLET;
  786. /* Force poll routine to be run and grab input the first time. */
  787. s->changed = 1;
  788. pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Tablet");
  789. return (USBDevice *)s;
  790. }
  791. USBDevice *usb_mouse_init(void)
  792. {
  793. USBHIDState *s;
  794. s = qemu_mallocz(sizeof(USBHIDState));
  795. s->dev.speed = USB_SPEED_FULL;
  796. s->dev.handle_packet = usb_generic_handle_packet;
  797. s->dev.handle_reset = usb_mouse_handle_reset;
  798. s->dev.handle_control = usb_hid_handle_control;
  799. s->dev.handle_data = usb_hid_handle_data;
  800. s->dev.handle_destroy = usb_hid_handle_destroy;
  801. s->kind = USB_MOUSE;
  802. /* Force poll routine to be run and grab input the first time. */
  803. s->changed = 1;
  804. pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Mouse");
  805. return (USBDevice *)s;
  806. }
  807. USBDevice *usb_keyboard_init(void)
  808. {
  809. USBHIDState *s;
  810. s = qemu_mallocz(sizeof(USBHIDState));
  811. s->dev.speed = USB_SPEED_FULL;
  812. s->dev.handle_packet = usb_generic_handle_packet;
  813. s->dev.handle_reset = usb_keyboard_handle_reset;
  814. s->dev.handle_control = usb_hid_handle_control;
  815. s->dev.handle_data = usb_hid_handle_data;
  816. s->dev.handle_destroy = usb_hid_handle_destroy;
  817. s->kind = USB_KEYBOARD;
  818. pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Keyboard");
  819. return (USBDevice *) s;
  820. }
  821. void usb_hid_datain_cb(USBDevice *dev, void *opaque, void (*datain)(void *))
  822. {
  823. USBHIDState *s = (USBHIDState *)dev;
  824. s->datain_opaque = opaque;
  825. s->datain = datain;
  826. }