usb-hid.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. #include "usb-desc.h"
  29. #include "qemu-timer.h"
  30. /* HID interface requests */
  31. #define GET_REPORT 0xa101
  32. #define GET_IDLE 0xa102
  33. #define GET_PROTOCOL 0xa103
  34. #define SET_REPORT 0x2109
  35. #define SET_IDLE 0x210a
  36. #define SET_PROTOCOL 0x210b
  37. /* HID descriptor types */
  38. #define USB_DT_HID 0x21
  39. #define USB_DT_REPORT 0x22
  40. #define USB_DT_PHY 0x23
  41. #define USB_MOUSE 1
  42. #define USB_TABLET 2
  43. #define USB_KEYBOARD 3
  44. typedef struct USBPointerEvent {
  45. int32_t xdx, ydy; /* relative iff it's a mouse, otherwise absolute */
  46. int32_t dz, buttons_state;
  47. } USBPointerEvent;
  48. #define QUEUE_LENGTH 16 /* should be enough for a triple-click */
  49. #define QUEUE_MASK (QUEUE_LENGTH-1u)
  50. #define QUEUE_INCR(v) ((v)++, (v) &= QUEUE_MASK)
  51. typedef struct USBMouseState {
  52. USBPointerEvent queue[QUEUE_LENGTH];
  53. int mouse_grabbed;
  54. QEMUPutMouseEntry *eh_entry;
  55. } USBMouseState;
  56. typedef struct USBKeyboardState {
  57. uint32_t keycodes[QUEUE_LENGTH];
  58. uint16_t modifiers;
  59. uint8_t leds;
  60. uint8_t key[16];
  61. int32_t keys;
  62. } USBKeyboardState;
  63. typedef struct USBHIDState {
  64. USBDevice dev;
  65. union {
  66. USBMouseState ptr;
  67. USBKeyboardState kbd;
  68. };
  69. uint32_t head; /* index into circular queue */
  70. uint32_t n;
  71. int kind;
  72. int32_t protocol;
  73. uint8_t idle;
  74. int64_t next_idle_clock;
  75. int changed;
  76. void *datain_opaque;
  77. void (*datain)(void *);
  78. } USBHIDState;
  79. enum {
  80. STR_MANUFACTURER = 1,
  81. STR_PRODUCT_MOUSE,
  82. STR_PRODUCT_TABLET,
  83. STR_PRODUCT_KEYBOARD,
  84. STR_SERIALNUMBER,
  85. STR_CONFIG_MOUSE,
  86. STR_CONFIG_TABLET,
  87. STR_CONFIG_KEYBOARD,
  88. };
  89. static const USBDescStrings desc_strings = {
  90. [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
  91. [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
  92. [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
  93. [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
  94. [STR_SERIALNUMBER] = "42", /* == remote wakeup works */
  95. [STR_CONFIG_MOUSE] = "HID Mouse",
  96. [STR_CONFIG_TABLET] = "HID Tablet",
  97. [STR_CONFIG_KEYBOARD] = "HID Keyboard",
  98. };
  99. static const USBDescIface desc_iface_mouse = {
  100. .bInterfaceNumber = 0,
  101. .bNumEndpoints = 1,
  102. .bInterfaceClass = USB_CLASS_HID,
  103. .bInterfaceSubClass = 0x01, /* boot */
  104. .bInterfaceProtocol = 0x02,
  105. .ndesc = 1,
  106. .descs = (USBDescOther[]) {
  107. {
  108. /* HID descriptor */
  109. .data = (uint8_t[]) {
  110. 0x09, /* u8 bLength */
  111. USB_DT_HID, /* u8 bDescriptorType */
  112. 0x01, 0x00, /* u16 HID_class */
  113. 0x00, /* u8 country_code */
  114. 0x01, /* u8 num_descriptors */
  115. USB_DT_REPORT, /* u8 type: Report */
  116. 52, 0, /* u16 len */
  117. },
  118. },
  119. },
  120. .eps = (USBDescEndpoint[]) {
  121. {
  122. .bEndpointAddress = USB_DIR_IN | 0x01,
  123. .bmAttributes = USB_ENDPOINT_XFER_INT,
  124. .wMaxPacketSize = 4,
  125. .bInterval = 0x0a,
  126. },
  127. },
  128. };
  129. static const USBDescIface desc_iface_tablet = {
  130. .bInterfaceNumber = 0,
  131. .bNumEndpoints = 1,
  132. .bInterfaceClass = USB_CLASS_HID,
  133. .bInterfaceProtocol = 0x02,
  134. .ndesc = 1,
  135. .descs = (USBDescOther[]) {
  136. {
  137. /* HID descriptor */
  138. .data = (uint8_t[]) {
  139. 0x09, /* u8 bLength */
  140. USB_DT_HID, /* u8 bDescriptorType */
  141. 0x01, 0x00, /* u16 HID_class */
  142. 0x00, /* u8 country_code */
  143. 0x01, /* u8 num_descriptors */
  144. USB_DT_REPORT, /* u8 type: Report */
  145. 74, 0, /* u16 len */
  146. },
  147. },
  148. },
  149. .eps = (USBDescEndpoint[]) {
  150. {
  151. .bEndpointAddress = USB_DIR_IN | 0x01,
  152. .bmAttributes = USB_ENDPOINT_XFER_INT,
  153. .wMaxPacketSize = 8,
  154. .bInterval = 0x0a,
  155. },
  156. },
  157. };
  158. static const USBDescIface desc_iface_keyboard = {
  159. .bInterfaceNumber = 0,
  160. .bNumEndpoints = 1,
  161. .bInterfaceClass = USB_CLASS_HID,
  162. .bInterfaceSubClass = 0x01, /* boot */
  163. .bInterfaceProtocol = 0x01, /* keyboard */
  164. .ndesc = 1,
  165. .descs = (USBDescOther[]) {
  166. {
  167. /* HID descriptor */
  168. .data = (uint8_t[]) {
  169. 0x09, /* u8 bLength */
  170. USB_DT_HID, /* u8 bDescriptorType */
  171. 0x11, 0x01, /* u16 HID_class */
  172. 0x00, /* u8 country_code */
  173. 0x01, /* u8 num_descriptors */
  174. USB_DT_REPORT, /* u8 type: Report */
  175. 0x3f, 0, /* u16 len */
  176. },
  177. },
  178. },
  179. .eps = (USBDescEndpoint[]) {
  180. {
  181. .bEndpointAddress = USB_DIR_IN | 0x01,
  182. .bmAttributes = USB_ENDPOINT_XFER_INT,
  183. .wMaxPacketSize = 8,
  184. .bInterval = 0x0a,
  185. },
  186. },
  187. };
  188. static const USBDescDevice desc_device_mouse = {
  189. .bcdUSB = 0x0100,
  190. .bMaxPacketSize0 = 8,
  191. .bNumConfigurations = 1,
  192. .confs = (USBDescConfig[]) {
  193. {
  194. .bNumInterfaces = 1,
  195. .bConfigurationValue = 1,
  196. .iConfiguration = STR_CONFIG_MOUSE,
  197. .bmAttributes = 0xa0,
  198. .bMaxPower = 50,
  199. .nif = 1,
  200. .ifs = &desc_iface_mouse,
  201. },
  202. },
  203. };
  204. static const USBDescDevice desc_device_tablet = {
  205. .bcdUSB = 0x0100,
  206. .bMaxPacketSize0 = 8,
  207. .bNumConfigurations = 1,
  208. .confs = (USBDescConfig[]) {
  209. {
  210. .bNumInterfaces = 1,
  211. .bConfigurationValue = 1,
  212. .iConfiguration = STR_CONFIG_TABLET,
  213. .bmAttributes = 0xa0,
  214. .bMaxPower = 50,
  215. .nif = 1,
  216. .ifs = &desc_iface_tablet,
  217. },
  218. },
  219. };
  220. static const USBDescDevice desc_device_keyboard = {
  221. .bcdUSB = 0x0100,
  222. .bMaxPacketSize0 = 8,
  223. .bNumConfigurations = 1,
  224. .confs = (USBDescConfig[]) {
  225. {
  226. .bNumInterfaces = 1,
  227. .bConfigurationValue = 1,
  228. .iConfiguration = STR_CONFIG_KEYBOARD,
  229. .bmAttributes = 0xa0,
  230. .bMaxPower = 50,
  231. .nif = 1,
  232. .ifs = &desc_iface_keyboard,
  233. },
  234. },
  235. };
  236. static const USBDesc desc_mouse = {
  237. .id = {
  238. .idVendor = 0x0627,
  239. .idProduct = 0x0001,
  240. .bcdDevice = 0,
  241. .iManufacturer = STR_MANUFACTURER,
  242. .iProduct = STR_PRODUCT_MOUSE,
  243. .iSerialNumber = STR_SERIALNUMBER,
  244. },
  245. .full = &desc_device_mouse,
  246. .str = desc_strings,
  247. };
  248. static const USBDesc desc_tablet = {
  249. .id = {
  250. .idVendor = 0x0627,
  251. .idProduct = 0x0001,
  252. .bcdDevice = 0,
  253. .iManufacturer = STR_MANUFACTURER,
  254. .iProduct = STR_PRODUCT_TABLET,
  255. .iSerialNumber = STR_SERIALNUMBER,
  256. },
  257. .full = &desc_device_tablet,
  258. .str = desc_strings,
  259. };
  260. static const USBDesc desc_keyboard = {
  261. .id = {
  262. .idVendor = 0x0627,
  263. .idProduct = 0x0001,
  264. .bcdDevice = 0,
  265. .iManufacturer = STR_MANUFACTURER,
  266. .iProduct = STR_PRODUCT_KEYBOARD,
  267. .iSerialNumber = STR_SERIALNUMBER,
  268. },
  269. .full = &desc_device_keyboard,
  270. .str = desc_strings,
  271. };
  272. static const uint8_t qemu_mouse_hid_report_descriptor[] = {
  273. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  274. 0x09, 0x02, /* Usage (Mouse) */
  275. 0xa1, 0x01, /* Collection (Application) */
  276. 0x09, 0x01, /* Usage (Pointer) */
  277. 0xa1, 0x00, /* Collection (Physical) */
  278. 0x05, 0x09, /* Usage Page (Button) */
  279. 0x19, 0x01, /* Usage Minimum (1) */
  280. 0x29, 0x03, /* Usage Maximum (3) */
  281. 0x15, 0x00, /* Logical Minimum (0) */
  282. 0x25, 0x01, /* Logical Maximum (1) */
  283. 0x95, 0x03, /* Report Count (3) */
  284. 0x75, 0x01, /* Report Size (1) */
  285. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  286. 0x95, 0x01, /* Report Count (1) */
  287. 0x75, 0x05, /* Report Size (5) */
  288. 0x81, 0x01, /* Input (Constant) */
  289. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  290. 0x09, 0x30, /* Usage (X) */
  291. 0x09, 0x31, /* Usage (Y) */
  292. 0x09, 0x38, /* Usage (Wheel) */
  293. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  294. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  295. 0x75, 0x08, /* Report Size (8) */
  296. 0x95, 0x03, /* Report Count (3) */
  297. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  298. 0xc0, /* End Collection */
  299. 0xc0, /* End Collection */
  300. };
  301. static const uint8_t qemu_tablet_hid_report_descriptor[] = {
  302. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  303. 0x09, 0x01, /* Usage (Pointer) */
  304. 0xa1, 0x01, /* Collection (Application) */
  305. 0x09, 0x01, /* Usage (Pointer) */
  306. 0xa1, 0x00, /* Collection (Physical) */
  307. 0x05, 0x09, /* Usage Page (Button) */
  308. 0x19, 0x01, /* Usage Minimum (1) */
  309. 0x29, 0x03, /* Usage Maximum (3) */
  310. 0x15, 0x00, /* Logical Minimum (0) */
  311. 0x25, 0x01, /* Logical Maximum (1) */
  312. 0x95, 0x03, /* Report Count (3) */
  313. 0x75, 0x01, /* Report Size (1) */
  314. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  315. 0x95, 0x01, /* Report Count (1) */
  316. 0x75, 0x05, /* Report Size (5) */
  317. 0x81, 0x01, /* Input (Constant) */
  318. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  319. 0x09, 0x30, /* Usage (X) */
  320. 0x09, 0x31, /* Usage (Y) */
  321. 0x15, 0x00, /* Logical Minimum (0) */
  322. 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
  323. 0x35, 0x00, /* Physical Minimum (0) */
  324. 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
  325. 0x75, 0x10, /* Report Size (16) */
  326. 0x95, 0x02, /* Report Count (2) */
  327. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  328. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  329. 0x09, 0x38, /* Usage (Wheel) */
  330. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  331. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  332. 0x35, 0x00, /* Physical Minimum (same as logical) */
  333. 0x45, 0x00, /* Physical Maximum (same as logical) */
  334. 0x75, 0x08, /* Report Size (8) */
  335. 0x95, 0x01, /* Report Count (1) */
  336. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  337. 0xc0, /* End Collection */
  338. 0xc0, /* End Collection */
  339. };
  340. static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
  341. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  342. 0x09, 0x06, /* Usage (Keyboard) */
  343. 0xa1, 0x01, /* Collection (Application) */
  344. 0x75, 0x01, /* Report Size (1) */
  345. 0x95, 0x08, /* Report Count (8) */
  346. 0x05, 0x07, /* Usage Page (Key Codes) */
  347. 0x19, 0xe0, /* Usage Minimum (224) */
  348. 0x29, 0xe7, /* Usage Maximum (231) */
  349. 0x15, 0x00, /* Logical Minimum (0) */
  350. 0x25, 0x01, /* Logical Maximum (1) */
  351. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  352. 0x95, 0x01, /* Report Count (1) */
  353. 0x75, 0x08, /* Report Size (8) */
  354. 0x81, 0x01, /* Input (Constant) */
  355. 0x95, 0x05, /* Report Count (5) */
  356. 0x75, 0x01, /* Report Size (1) */
  357. 0x05, 0x08, /* Usage Page (LEDs) */
  358. 0x19, 0x01, /* Usage Minimum (1) */
  359. 0x29, 0x05, /* Usage Maximum (5) */
  360. 0x91, 0x02, /* Output (Data, Variable, Absolute) */
  361. 0x95, 0x01, /* Report Count (1) */
  362. 0x75, 0x03, /* Report Size (3) */
  363. 0x91, 0x01, /* Output (Constant) */
  364. 0x95, 0x06, /* Report Count (6) */
  365. 0x75, 0x08, /* Report Size (8) */
  366. 0x15, 0x00, /* Logical Minimum (0) */
  367. 0x25, 0xff, /* Logical Maximum (255) */
  368. 0x05, 0x07, /* Usage Page (Key Codes) */
  369. 0x19, 0x00, /* Usage Minimum (0) */
  370. 0x29, 0xff, /* Usage Maximum (255) */
  371. 0x81, 0x00, /* Input (Data, Array) */
  372. 0xc0, /* End Collection */
  373. };
  374. #define USB_HID_USAGE_ERROR_ROLLOVER 0x01
  375. #define USB_HID_USAGE_POSTFAIL 0x02
  376. #define USB_HID_USAGE_ERROR_UNDEFINED 0x03
  377. /* Indices are QEMU keycodes, values are from HID Usage Table. Indices
  378. * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d. */
  379. static const uint8_t usb_hid_usage_keys[0x100] = {
  380. 0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  381. 0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
  382. 0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
  383. 0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
  384. 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
  385. 0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
  386. 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
  387. 0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
  388. 0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
  389. 0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
  390. 0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44,
  391. 0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
  392. 0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
  393. 0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
  394. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  395. 0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65,
  396. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  397. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  398. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  399. 0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
  400. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  401. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  402. 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
  403. 0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  404. 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a,
  405. 0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
  406. 0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
  407. 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x00, 0x00,
  408. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  409. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  412. };
  413. static void usb_hid_changed(USBHIDState *hs)
  414. {
  415. hs->changed = 1;
  416. if (hs->datain)
  417. hs->datain(hs->datain_opaque);
  418. usb_wakeup(&hs->dev);
  419. }
  420. static void usb_pointer_event_clear(USBPointerEvent *e, int buttons) {
  421. e->xdx = e->ydy = e->dz = 0;
  422. e->buttons_state = buttons;
  423. }
  424. static void usb_pointer_event_combine(USBPointerEvent *e, int xyrel,
  425. int x1, int y1, int z1) {
  426. if (xyrel) {
  427. e->xdx += x1;
  428. e->ydy += y1;
  429. } else {
  430. e->xdx = x1;
  431. e->ydy = y1;
  432. }
  433. e->dz += z1;
  434. }
  435. static void usb_pointer_event(void *opaque,
  436. int x1, int y1, int z1, int buttons_state)
  437. {
  438. USBHIDState *hs = opaque;
  439. USBMouseState *s = &hs->ptr;
  440. unsigned use_slot = (hs->head + hs->n - 1) & QUEUE_MASK;
  441. unsigned previous_slot = (use_slot - 1) & QUEUE_MASK;
  442. /* We combine events where feasible to keep the queue small. We shouldn't
  443. * combine anything with the first event of a particular button state, as
  444. * that would change the location of the button state change. When the
  445. * queue is empty, a second event is needed because we don't know if
  446. * the first event changed the button state. */
  447. if (hs->n == QUEUE_LENGTH) {
  448. /* Queue full. Discard old button state, combine motion normally. */
  449. s->queue[use_slot].buttons_state = buttons_state;
  450. } else if (hs->n < 2 ||
  451. s->queue[use_slot].buttons_state != buttons_state ||
  452. s->queue[previous_slot].buttons_state != s->queue[use_slot].buttons_state) {
  453. /* Cannot or should not combine, so add an empty item to the queue. */
  454. QUEUE_INCR(use_slot);
  455. hs->n++;
  456. usb_pointer_event_clear(&s->queue[use_slot], buttons_state);
  457. }
  458. usb_pointer_event_combine(&s->queue[use_slot],
  459. hs->kind == USB_MOUSE,
  460. x1, y1, z1);
  461. usb_hid_changed(hs);
  462. }
  463. static void usb_keyboard_event(void *opaque, int keycode)
  464. {
  465. USBHIDState *hs = opaque;
  466. USBKeyboardState *s = &hs->kbd;
  467. int slot;
  468. if (hs->n == QUEUE_LENGTH) {
  469. fprintf(stderr, "usb-kbd: warning: key event queue full\n");
  470. return;
  471. }
  472. slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
  473. s->keycodes[slot] = keycode;
  474. usb_hid_changed(hs);
  475. }
  476. static void usb_keyboard_process_keycode(USBHIDState *hs)
  477. {
  478. USBKeyboardState *s = &hs->kbd;
  479. uint8_t hid_code, key;
  480. int i, keycode, slot;
  481. if (hs->n == 0) {
  482. return;
  483. }
  484. slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
  485. keycode = s->keycodes[slot];
  486. key = keycode & 0x7f;
  487. hid_code = usb_hid_usage_keys[key | ((s->modifiers >> 1) & (1 << 7))];
  488. s->modifiers &= ~(1 << 8);
  489. switch (hid_code) {
  490. case 0x00:
  491. return;
  492. case 0xe0:
  493. if (s->modifiers & (1 << 9)) {
  494. s->modifiers ^= 3 << 8;
  495. return;
  496. }
  497. case 0xe1 ... 0xe7:
  498. if (keycode & (1 << 7)) {
  499. s->modifiers &= ~(1 << (hid_code & 0x0f));
  500. return;
  501. }
  502. case 0xe8 ... 0xef:
  503. s->modifiers |= 1 << (hid_code & 0x0f);
  504. return;
  505. }
  506. if (keycode & (1 << 7)) {
  507. for (i = s->keys - 1; i >= 0; i --)
  508. if (s->key[i] == hid_code) {
  509. s->key[i] = s->key[-- s->keys];
  510. s->key[s->keys] = 0x00;
  511. break;
  512. }
  513. if (i < 0)
  514. return;
  515. } else {
  516. for (i = s->keys - 1; i >= 0; i --)
  517. if (s->key[i] == hid_code)
  518. break;
  519. if (i < 0) {
  520. if (s->keys < sizeof(s->key))
  521. s->key[s->keys ++] = hid_code;
  522. } else
  523. return;
  524. }
  525. }
  526. static inline int int_clamp(int val, int vmin, int vmax)
  527. {
  528. if (val < vmin)
  529. return vmin;
  530. else if (val > vmax)
  531. return vmax;
  532. else
  533. return val;
  534. }
  535. static int usb_pointer_poll(USBHIDState *hs, uint8_t *buf, int len)
  536. {
  537. int dx, dy, dz, b, l;
  538. int index;
  539. USBMouseState *s = &hs->ptr;
  540. USBPointerEvent *e;
  541. if (!s->mouse_grabbed) {
  542. qemu_activate_mouse_event_handler(s->eh_entry);
  543. s->mouse_grabbed = 1;
  544. }
  545. /* When the buffer is empty, return the last event. Relative
  546. movements will all be zero. */
  547. index = (hs->n ? hs->head : hs->head - 1);
  548. e = &s->queue[index & QUEUE_MASK];
  549. if (hs->kind == USB_MOUSE) {
  550. dx = int_clamp(e->xdx, -127, 127);
  551. dy = int_clamp(e->ydy, -127, 127);
  552. e->xdx -= dx;
  553. e->ydy -= dy;
  554. } else {
  555. dx = e->xdx;
  556. dy = e->ydy;
  557. }
  558. dz = int_clamp(e->dz, -127, 127);
  559. e->dz -= dz;
  560. b = 0;
  561. if (e->buttons_state & MOUSE_EVENT_LBUTTON)
  562. b |= 0x01;
  563. if (e->buttons_state & MOUSE_EVENT_RBUTTON)
  564. b |= 0x02;
  565. if (e->buttons_state & MOUSE_EVENT_MBUTTON)
  566. b |= 0x04;
  567. if (hs->n &&
  568. !e->dz &&
  569. (hs->kind == USB_TABLET || (!e->xdx && !e->ydy))) {
  570. /* that deals with this event */
  571. QUEUE_INCR(hs->head);
  572. hs->n--;
  573. }
  574. /* Appears we have to invert the wheel direction */
  575. dz = 0 - dz;
  576. l = 0;
  577. switch (hs->kind) {
  578. case USB_MOUSE:
  579. if (len > l)
  580. buf[l++] = b;
  581. if (len > l)
  582. buf[l++] = dx;
  583. if (len > l)
  584. buf[l++] = dy;
  585. if (len > l)
  586. buf[l++] = dz;
  587. break;
  588. case USB_TABLET:
  589. if (len > l)
  590. buf[l++] = b;
  591. if (len > l)
  592. buf[l++] = dx & 0xff;
  593. if (len > l)
  594. buf[l++] = dx >> 8;
  595. if (len > l)
  596. buf[l++] = dy & 0xff;
  597. if (len > l)
  598. buf[l++] = dy >> 8;
  599. if (len > l)
  600. buf[l++] = dz;
  601. break;
  602. default:
  603. abort();
  604. }
  605. return l;
  606. }
  607. static int usb_keyboard_poll(USBHIDState *hs, uint8_t *buf, int len)
  608. {
  609. USBKeyboardState *s = &hs->kbd;
  610. if (len < 2)
  611. return 0;
  612. usb_keyboard_process_keycode(hs);
  613. buf[0] = s->modifiers & 0xff;
  614. buf[1] = 0;
  615. if (s->keys > 6)
  616. memset(buf + 2, USB_HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
  617. else
  618. memcpy(buf + 2, s->key, MIN(8, len) - 2);
  619. return MIN(8, len);
  620. }
  621. static int usb_keyboard_write(USBKeyboardState *s, uint8_t *buf, int len)
  622. {
  623. if (len > 0) {
  624. int ledstate = 0;
  625. /* 0x01: Num Lock LED
  626. * 0x02: Caps Lock LED
  627. * 0x04: Scroll Lock LED
  628. * 0x08: Compose LED
  629. * 0x10: Kana LED */
  630. s->leds = buf[0];
  631. if (s->leds & 0x04)
  632. ledstate |= QEMU_SCROLL_LOCK_LED;
  633. if (s->leds & 0x01)
  634. ledstate |= QEMU_NUM_LOCK_LED;
  635. if (s->leds & 0x02)
  636. ledstate |= QEMU_CAPS_LOCK_LED;
  637. kbd_put_ledstate(ledstate);
  638. }
  639. return 0;
  640. }
  641. static void usb_mouse_handle_reset(USBDevice *dev)
  642. {
  643. USBHIDState *s = (USBHIDState *)dev;
  644. memset(s->ptr.queue, 0, sizeof (s->ptr.queue));
  645. s->head = 0;
  646. s->n = 0;
  647. s->protocol = 1;
  648. }
  649. static void usb_keyboard_handle_reset(USBDevice *dev)
  650. {
  651. USBHIDState *s = (USBHIDState *)dev;
  652. qemu_add_kbd_event_handler(usb_keyboard_event, s);
  653. memset(s->kbd.keycodes, 0, sizeof (s->kbd.keycodes));
  654. s->head = 0;
  655. s->n = 0;
  656. memset(s->kbd.key, 0, sizeof (s->kbd.key));
  657. s->kbd.keys = 0;
  658. s->protocol = 1;
  659. }
  660. static void usb_hid_set_next_idle(USBHIDState *s, int64_t curtime)
  661. {
  662. s->next_idle_clock = curtime + (get_ticks_per_sec() * s->idle * 4) / 1000;
  663. }
  664. static int usb_hid_handle_control(USBDevice *dev, USBPacket *p,
  665. int request, int value, int index, int length, uint8_t *data)
  666. {
  667. USBHIDState *s = (USBHIDState *)dev;
  668. int ret;
  669. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  670. if (ret >= 0) {
  671. return ret;
  672. }
  673. ret = 0;
  674. switch(request) {
  675. case DeviceRequest | USB_REQ_GET_INTERFACE:
  676. data[0] = 0;
  677. ret = 1;
  678. break;
  679. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  680. ret = 0;
  681. break;
  682. /* hid specific requests */
  683. case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
  684. switch(value >> 8) {
  685. case 0x22:
  686. if (s->kind == USB_MOUSE) {
  687. memcpy(data, qemu_mouse_hid_report_descriptor,
  688. sizeof(qemu_mouse_hid_report_descriptor));
  689. ret = sizeof(qemu_mouse_hid_report_descriptor);
  690. } else if (s->kind == USB_TABLET) {
  691. memcpy(data, qemu_tablet_hid_report_descriptor,
  692. sizeof(qemu_tablet_hid_report_descriptor));
  693. ret = sizeof(qemu_tablet_hid_report_descriptor);
  694. } else if (s->kind == USB_KEYBOARD) {
  695. memcpy(data, qemu_keyboard_hid_report_descriptor,
  696. sizeof(qemu_keyboard_hid_report_descriptor));
  697. ret = sizeof(qemu_keyboard_hid_report_descriptor);
  698. }
  699. break;
  700. default:
  701. goto fail;
  702. }
  703. break;
  704. case GET_REPORT:
  705. if (s->kind == USB_MOUSE || s->kind == USB_TABLET) {
  706. ret = usb_pointer_poll(s, data, length);
  707. } else if (s->kind == USB_KEYBOARD) {
  708. ret = usb_keyboard_poll(s, data, length);
  709. }
  710. s->changed = s->n > 0;
  711. break;
  712. case SET_REPORT:
  713. if (s->kind == USB_KEYBOARD)
  714. ret = usb_keyboard_write(&s->kbd, data, length);
  715. else
  716. goto fail;
  717. break;
  718. case GET_PROTOCOL:
  719. if (s->kind != USB_KEYBOARD && s->kind != USB_MOUSE)
  720. goto fail;
  721. ret = 1;
  722. data[0] = s->protocol;
  723. break;
  724. case SET_PROTOCOL:
  725. if (s->kind != USB_KEYBOARD && s->kind != USB_MOUSE)
  726. goto fail;
  727. ret = 0;
  728. s->protocol = value;
  729. break;
  730. case GET_IDLE:
  731. ret = 1;
  732. data[0] = s->idle;
  733. break;
  734. case SET_IDLE:
  735. s->idle = (uint8_t) (value >> 8);
  736. usb_hid_set_next_idle(s, qemu_get_clock_ns(vm_clock));
  737. ret = 0;
  738. break;
  739. default:
  740. fail:
  741. ret = USB_RET_STALL;
  742. break;
  743. }
  744. return ret;
  745. }
  746. static int usb_hid_handle_data(USBDevice *dev, USBPacket *p)
  747. {
  748. USBHIDState *s = (USBHIDState *)dev;
  749. int ret = 0;
  750. switch(p->pid) {
  751. case USB_TOKEN_IN:
  752. if (p->devep == 1) {
  753. int64_t curtime = qemu_get_clock_ns(vm_clock);
  754. if (!s->changed && (!s->idle || s->next_idle_clock - curtime > 0))
  755. return USB_RET_NAK;
  756. usb_hid_set_next_idle(s, curtime);
  757. if (s->kind == USB_MOUSE || s->kind == USB_TABLET) {
  758. ret = usb_pointer_poll(s, p->data, p->len);
  759. }
  760. else if (s->kind == USB_KEYBOARD) {
  761. ret = usb_keyboard_poll(s, p->data, p->len);
  762. }
  763. s->changed = s->n > 0;
  764. } else {
  765. goto fail;
  766. }
  767. break;
  768. case USB_TOKEN_OUT:
  769. default:
  770. fail:
  771. ret = USB_RET_STALL;
  772. break;
  773. }
  774. return ret;
  775. }
  776. static void usb_hid_handle_destroy(USBDevice *dev)
  777. {
  778. USBHIDState *s = (USBHIDState *)dev;
  779. switch(s->kind) {
  780. case USB_KEYBOARD:
  781. qemu_remove_kbd_event_handler();
  782. break;
  783. default:
  784. qemu_remove_mouse_event_handler(s->ptr.eh_entry);
  785. }
  786. }
  787. static int usb_hid_initfn(USBDevice *dev, int kind)
  788. {
  789. USBHIDState *s = DO_UPCAST(USBHIDState, dev, dev);
  790. usb_desc_init(dev);
  791. s->kind = kind;
  792. if (s->kind == USB_MOUSE) {
  793. s->ptr.eh_entry = qemu_add_mouse_event_handler(usb_pointer_event, s,
  794. 0, "QEMU USB Mouse");
  795. } else if (s->kind == USB_TABLET) {
  796. s->ptr.eh_entry = qemu_add_mouse_event_handler(usb_pointer_event, s,
  797. 1, "QEMU USB Tablet");
  798. }
  799. /* Force poll routine to be run and grab input the first time. */
  800. s->changed = 1;
  801. return 0;
  802. }
  803. static int usb_tablet_initfn(USBDevice *dev)
  804. {
  805. return usb_hid_initfn(dev, USB_TABLET);
  806. }
  807. static int usb_mouse_initfn(USBDevice *dev)
  808. {
  809. return usb_hid_initfn(dev, USB_MOUSE);
  810. }
  811. static int usb_keyboard_initfn(USBDevice *dev)
  812. {
  813. return usb_hid_initfn(dev, USB_KEYBOARD);
  814. }
  815. void usb_hid_datain_cb(USBDevice *dev, void *opaque, void (*datain)(void *))
  816. {
  817. USBHIDState *s = (USBHIDState *)dev;
  818. s->datain_opaque = opaque;
  819. s->datain = datain;
  820. }
  821. static int usb_hid_post_load(void *opaque, int version_id)
  822. {
  823. USBHIDState *s = opaque;
  824. if (s->idle) {
  825. usb_hid_set_next_idle(s, qemu_get_clock_ns(vm_clock));
  826. }
  827. return 0;
  828. }
  829. static const VMStateDescription vmstate_usb_ptr_queue = {
  830. .name = "usb-ptr-queue",
  831. .version_id = 1,
  832. .minimum_version_id = 1,
  833. .fields = (VMStateField []) {
  834. VMSTATE_INT32(xdx, USBPointerEvent),
  835. VMSTATE_INT32(ydy, USBPointerEvent),
  836. VMSTATE_INT32(dz, USBPointerEvent),
  837. VMSTATE_INT32(buttons_state, USBPointerEvent),
  838. VMSTATE_END_OF_LIST()
  839. }
  840. };
  841. static const VMStateDescription vmstate_usb_ptr = {
  842. .name = "usb-ptr",
  843. .version_id = 1,
  844. .minimum_version_id = 1,
  845. .post_load = usb_hid_post_load,
  846. .fields = (VMStateField []) {
  847. VMSTATE_USB_DEVICE(dev, USBHIDState),
  848. VMSTATE_STRUCT_ARRAY(ptr.queue, USBHIDState, QUEUE_LENGTH, 0,
  849. vmstate_usb_ptr_queue, USBPointerEvent),
  850. VMSTATE_UINT32(head, USBHIDState),
  851. VMSTATE_UINT32(n, USBHIDState),
  852. VMSTATE_INT32(protocol, USBHIDState),
  853. VMSTATE_UINT8(idle, USBHIDState),
  854. VMSTATE_END_OF_LIST()
  855. }
  856. };
  857. static const VMStateDescription vmstate_usb_kbd = {
  858. .name = "usb-kbd",
  859. .version_id = 1,
  860. .minimum_version_id = 1,
  861. .post_load = usb_hid_post_load,
  862. .fields = (VMStateField []) {
  863. VMSTATE_USB_DEVICE(dev, USBHIDState),
  864. VMSTATE_UINT32_ARRAY(kbd.keycodes, USBHIDState, QUEUE_LENGTH),
  865. VMSTATE_UINT32(head, USBHIDState),
  866. VMSTATE_UINT32(n, USBHIDState),
  867. VMSTATE_UINT16(kbd.modifiers, USBHIDState),
  868. VMSTATE_UINT8(kbd.leds, USBHIDState),
  869. VMSTATE_UINT8_ARRAY(kbd.key, USBHIDState, 16),
  870. VMSTATE_INT32(kbd.keys, USBHIDState),
  871. VMSTATE_INT32(protocol, USBHIDState),
  872. VMSTATE_UINT8(idle, USBHIDState),
  873. VMSTATE_END_OF_LIST()
  874. }
  875. };
  876. static struct USBDeviceInfo hid_info[] = {
  877. {
  878. .product_desc = "QEMU USB Tablet",
  879. .qdev.name = "usb-tablet",
  880. .usbdevice_name = "tablet",
  881. .qdev.size = sizeof(USBHIDState),
  882. .qdev.vmsd = &vmstate_usb_ptr,
  883. .usb_desc = &desc_tablet,
  884. .init = usb_tablet_initfn,
  885. .handle_packet = usb_generic_handle_packet,
  886. .handle_reset = usb_mouse_handle_reset,
  887. .handle_control = usb_hid_handle_control,
  888. .handle_data = usb_hid_handle_data,
  889. .handle_destroy = usb_hid_handle_destroy,
  890. },{
  891. .product_desc = "QEMU USB Mouse",
  892. .qdev.name = "usb-mouse",
  893. .usbdevice_name = "mouse",
  894. .qdev.size = sizeof(USBHIDState),
  895. .qdev.vmsd = &vmstate_usb_ptr,
  896. .usb_desc = &desc_mouse,
  897. .init = usb_mouse_initfn,
  898. .handle_packet = usb_generic_handle_packet,
  899. .handle_reset = usb_mouse_handle_reset,
  900. .handle_control = usb_hid_handle_control,
  901. .handle_data = usb_hid_handle_data,
  902. .handle_destroy = usb_hid_handle_destroy,
  903. },{
  904. .product_desc = "QEMU USB Keyboard",
  905. .qdev.name = "usb-kbd",
  906. .usbdevice_name = "keyboard",
  907. .qdev.size = sizeof(USBHIDState),
  908. .qdev.vmsd = &vmstate_usb_kbd,
  909. .usb_desc = &desc_keyboard,
  910. .init = usb_keyboard_initfn,
  911. .handle_packet = usb_generic_handle_packet,
  912. .handle_reset = usb_keyboard_handle_reset,
  913. .handle_control = usb_hid_handle_control,
  914. .handle_data = usb_hid_handle_data,
  915. .handle_destroy = usb_hid_handle_destroy,
  916. },{
  917. /* end of list */
  918. }
  919. };
  920. static void usb_hid_register_devices(void)
  921. {
  922. usb_qdev_register_many(hid_info);
  923. }
  924. device_init(usb_hid_register_devices)