usb-hid.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. #include "hid.h"
  31. /* HID interface requests */
  32. #define GET_REPORT 0xa101
  33. #define GET_IDLE 0xa102
  34. #define GET_PROTOCOL 0xa103
  35. #define SET_REPORT 0x2109
  36. #define SET_IDLE 0x210a
  37. #define SET_PROTOCOL 0x210b
  38. /* HID descriptor types */
  39. #define USB_DT_HID 0x21
  40. #define USB_DT_REPORT 0x22
  41. #define USB_DT_PHY 0x23
  42. typedef struct USBHIDState {
  43. USBDevice dev;
  44. HIDState hid;
  45. } USBHIDState;
  46. enum {
  47. STR_MANUFACTURER = 1,
  48. STR_PRODUCT_MOUSE,
  49. STR_PRODUCT_TABLET,
  50. STR_PRODUCT_KEYBOARD,
  51. STR_SERIALNUMBER,
  52. STR_CONFIG_MOUSE,
  53. STR_CONFIG_TABLET,
  54. STR_CONFIG_KEYBOARD,
  55. };
  56. static const USBDescStrings desc_strings = {
  57. [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
  58. [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
  59. [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
  60. [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
  61. [STR_SERIALNUMBER] = "42", /* == remote wakeup works */
  62. [STR_CONFIG_MOUSE] = "HID Mouse",
  63. [STR_CONFIG_TABLET] = "HID Tablet",
  64. [STR_CONFIG_KEYBOARD] = "HID Keyboard",
  65. };
  66. static const USBDescIface desc_iface_mouse = {
  67. .bInterfaceNumber = 0,
  68. .bNumEndpoints = 1,
  69. .bInterfaceClass = USB_CLASS_HID,
  70. .bInterfaceSubClass = 0x01, /* boot */
  71. .bInterfaceProtocol = 0x02,
  72. .ndesc = 1,
  73. .descs = (USBDescOther[]) {
  74. {
  75. /* HID descriptor */
  76. .data = (uint8_t[]) {
  77. 0x09, /* u8 bLength */
  78. USB_DT_HID, /* u8 bDescriptorType */
  79. 0x01, 0x00, /* u16 HID_class */
  80. 0x00, /* u8 country_code */
  81. 0x01, /* u8 num_descriptors */
  82. USB_DT_REPORT, /* u8 type: Report */
  83. 52, 0, /* u16 len */
  84. },
  85. },
  86. },
  87. .eps = (USBDescEndpoint[]) {
  88. {
  89. .bEndpointAddress = USB_DIR_IN | 0x01,
  90. .bmAttributes = USB_ENDPOINT_XFER_INT,
  91. .wMaxPacketSize = 4,
  92. .bInterval = 0x0a,
  93. },
  94. },
  95. };
  96. static const USBDescIface desc_iface_tablet = {
  97. .bInterfaceNumber = 0,
  98. .bNumEndpoints = 1,
  99. .bInterfaceClass = USB_CLASS_HID,
  100. .bInterfaceProtocol = 0x02,
  101. .ndesc = 1,
  102. .descs = (USBDescOther[]) {
  103. {
  104. /* HID descriptor */
  105. .data = (uint8_t[]) {
  106. 0x09, /* u8 bLength */
  107. USB_DT_HID, /* u8 bDescriptorType */
  108. 0x01, 0x00, /* u16 HID_class */
  109. 0x00, /* u8 country_code */
  110. 0x01, /* u8 num_descriptors */
  111. USB_DT_REPORT, /* u8 type: Report */
  112. 74, 0, /* u16 len */
  113. },
  114. },
  115. },
  116. .eps = (USBDescEndpoint[]) {
  117. {
  118. .bEndpointAddress = USB_DIR_IN | 0x01,
  119. .bmAttributes = USB_ENDPOINT_XFER_INT,
  120. .wMaxPacketSize = 8,
  121. .bInterval = 0x0a,
  122. },
  123. },
  124. };
  125. static const USBDescIface desc_iface_keyboard = {
  126. .bInterfaceNumber = 0,
  127. .bNumEndpoints = 1,
  128. .bInterfaceClass = USB_CLASS_HID,
  129. .bInterfaceSubClass = 0x01, /* boot */
  130. .bInterfaceProtocol = 0x01, /* keyboard */
  131. .ndesc = 1,
  132. .descs = (USBDescOther[]) {
  133. {
  134. /* HID descriptor */
  135. .data = (uint8_t[]) {
  136. 0x09, /* u8 bLength */
  137. USB_DT_HID, /* u8 bDescriptorType */
  138. 0x11, 0x01, /* u16 HID_class */
  139. 0x00, /* u8 country_code */
  140. 0x01, /* u8 num_descriptors */
  141. USB_DT_REPORT, /* u8 type: Report */
  142. 0x3f, 0, /* u16 len */
  143. },
  144. },
  145. },
  146. .eps = (USBDescEndpoint[]) {
  147. {
  148. .bEndpointAddress = USB_DIR_IN | 0x01,
  149. .bmAttributes = USB_ENDPOINT_XFER_INT,
  150. .wMaxPacketSize = 8,
  151. .bInterval = 0x0a,
  152. },
  153. },
  154. };
  155. static const USBDescDevice desc_device_mouse = {
  156. .bcdUSB = 0x0100,
  157. .bMaxPacketSize0 = 8,
  158. .bNumConfigurations = 1,
  159. .confs = (USBDescConfig[]) {
  160. {
  161. .bNumInterfaces = 1,
  162. .bConfigurationValue = 1,
  163. .iConfiguration = STR_CONFIG_MOUSE,
  164. .bmAttributes = 0xa0,
  165. .bMaxPower = 50,
  166. .nif = 1,
  167. .ifs = &desc_iface_mouse,
  168. },
  169. },
  170. };
  171. static const USBDescDevice desc_device_tablet = {
  172. .bcdUSB = 0x0100,
  173. .bMaxPacketSize0 = 8,
  174. .bNumConfigurations = 1,
  175. .confs = (USBDescConfig[]) {
  176. {
  177. .bNumInterfaces = 1,
  178. .bConfigurationValue = 1,
  179. .iConfiguration = STR_CONFIG_TABLET,
  180. .bmAttributes = 0xa0,
  181. .bMaxPower = 50,
  182. .nif = 1,
  183. .ifs = &desc_iface_tablet,
  184. },
  185. },
  186. };
  187. static const USBDescDevice desc_device_keyboard = {
  188. .bcdUSB = 0x0100,
  189. .bMaxPacketSize0 = 8,
  190. .bNumConfigurations = 1,
  191. .confs = (USBDescConfig[]) {
  192. {
  193. .bNumInterfaces = 1,
  194. .bConfigurationValue = 1,
  195. .iConfiguration = STR_CONFIG_KEYBOARD,
  196. .bmAttributes = 0xa0,
  197. .bMaxPower = 50,
  198. .nif = 1,
  199. .ifs = &desc_iface_keyboard,
  200. },
  201. },
  202. };
  203. static const USBDesc desc_mouse = {
  204. .id = {
  205. .idVendor = 0x0627,
  206. .idProduct = 0x0001,
  207. .bcdDevice = 0,
  208. .iManufacturer = STR_MANUFACTURER,
  209. .iProduct = STR_PRODUCT_MOUSE,
  210. .iSerialNumber = STR_SERIALNUMBER,
  211. },
  212. .full = &desc_device_mouse,
  213. .str = desc_strings,
  214. };
  215. static const USBDesc desc_tablet = {
  216. .id = {
  217. .idVendor = 0x0627,
  218. .idProduct = 0x0001,
  219. .bcdDevice = 0,
  220. .iManufacturer = STR_MANUFACTURER,
  221. .iProduct = STR_PRODUCT_TABLET,
  222. .iSerialNumber = STR_SERIALNUMBER,
  223. },
  224. .full = &desc_device_tablet,
  225. .str = desc_strings,
  226. };
  227. static const USBDesc desc_keyboard = {
  228. .id = {
  229. .idVendor = 0x0627,
  230. .idProduct = 0x0001,
  231. .bcdDevice = 0,
  232. .iManufacturer = STR_MANUFACTURER,
  233. .iProduct = STR_PRODUCT_KEYBOARD,
  234. .iSerialNumber = STR_SERIALNUMBER,
  235. },
  236. .full = &desc_device_keyboard,
  237. .str = desc_strings,
  238. };
  239. static const uint8_t qemu_mouse_hid_report_descriptor[] = {
  240. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  241. 0x09, 0x02, /* Usage (Mouse) */
  242. 0xa1, 0x01, /* Collection (Application) */
  243. 0x09, 0x01, /* Usage (Pointer) */
  244. 0xa1, 0x00, /* Collection (Physical) */
  245. 0x05, 0x09, /* Usage Page (Button) */
  246. 0x19, 0x01, /* Usage Minimum (1) */
  247. 0x29, 0x03, /* Usage Maximum (3) */
  248. 0x15, 0x00, /* Logical Minimum (0) */
  249. 0x25, 0x01, /* Logical Maximum (1) */
  250. 0x95, 0x03, /* Report Count (3) */
  251. 0x75, 0x01, /* Report Size (1) */
  252. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  253. 0x95, 0x01, /* Report Count (1) */
  254. 0x75, 0x05, /* Report Size (5) */
  255. 0x81, 0x01, /* Input (Constant) */
  256. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  257. 0x09, 0x30, /* Usage (X) */
  258. 0x09, 0x31, /* Usage (Y) */
  259. 0x09, 0x38, /* Usage (Wheel) */
  260. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  261. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  262. 0x75, 0x08, /* Report Size (8) */
  263. 0x95, 0x03, /* Report Count (3) */
  264. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  265. 0xc0, /* End Collection */
  266. 0xc0, /* End Collection */
  267. };
  268. static const uint8_t qemu_tablet_hid_report_descriptor[] = {
  269. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  270. 0x09, 0x01, /* Usage (Pointer) */
  271. 0xa1, 0x01, /* Collection (Application) */
  272. 0x09, 0x01, /* Usage (Pointer) */
  273. 0xa1, 0x00, /* Collection (Physical) */
  274. 0x05, 0x09, /* Usage Page (Button) */
  275. 0x19, 0x01, /* Usage Minimum (1) */
  276. 0x29, 0x03, /* Usage Maximum (3) */
  277. 0x15, 0x00, /* Logical Minimum (0) */
  278. 0x25, 0x01, /* Logical Maximum (1) */
  279. 0x95, 0x03, /* Report Count (3) */
  280. 0x75, 0x01, /* Report Size (1) */
  281. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  282. 0x95, 0x01, /* Report Count (1) */
  283. 0x75, 0x05, /* Report Size (5) */
  284. 0x81, 0x01, /* Input (Constant) */
  285. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  286. 0x09, 0x30, /* Usage (X) */
  287. 0x09, 0x31, /* Usage (Y) */
  288. 0x15, 0x00, /* Logical Minimum (0) */
  289. 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
  290. 0x35, 0x00, /* Physical Minimum (0) */
  291. 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
  292. 0x75, 0x10, /* Report Size (16) */
  293. 0x95, 0x02, /* Report Count (2) */
  294. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  295. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  296. 0x09, 0x38, /* Usage (Wheel) */
  297. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  298. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  299. 0x35, 0x00, /* Physical Minimum (same as logical) */
  300. 0x45, 0x00, /* Physical Maximum (same as logical) */
  301. 0x75, 0x08, /* Report Size (8) */
  302. 0x95, 0x01, /* Report Count (1) */
  303. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  304. 0xc0, /* End Collection */
  305. 0xc0, /* End Collection */
  306. };
  307. static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
  308. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  309. 0x09, 0x06, /* Usage (Keyboard) */
  310. 0xa1, 0x01, /* Collection (Application) */
  311. 0x75, 0x01, /* Report Size (1) */
  312. 0x95, 0x08, /* Report Count (8) */
  313. 0x05, 0x07, /* Usage Page (Key Codes) */
  314. 0x19, 0xe0, /* Usage Minimum (224) */
  315. 0x29, 0xe7, /* Usage Maximum (231) */
  316. 0x15, 0x00, /* Logical Minimum (0) */
  317. 0x25, 0x01, /* Logical Maximum (1) */
  318. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  319. 0x95, 0x01, /* Report Count (1) */
  320. 0x75, 0x08, /* Report Size (8) */
  321. 0x81, 0x01, /* Input (Constant) */
  322. 0x95, 0x05, /* Report Count (5) */
  323. 0x75, 0x01, /* Report Size (1) */
  324. 0x05, 0x08, /* Usage Page (LEDs) */
  325. 0x19, 0x01, /* Usage Minimum (1) */
  326. 0x29, 0x05, /* Usage Maximum (5) */
  327. 0x91, 0x02, /* Output (Data, Variable, Absolute) */
  328. 0x95, 0x01, /* Report Count (1) */
  329. 0x75, 0x03, /* Report Size (3) */
  330. 0x91, 0x01, /* Output (Constant) */
  331. 0x95, 0x06, /* Report Count (6) */
  332. 0x75, 0x08, /* Report Size (8) */
  333. 0x15, 0x00, /* Logical Minimum (0) */
  334. 0x25, 0xff, /* Logical Maximum (255) */
  335. 0x05, 0x07, /* Usage Page (Key Codes) */
  336. 0x19, 0x00, /* Usage Minimum (0) */
  337. 0x29, 0xff, /* Usage Maximum (255) */
  338. 0x81, 0x00, /* Input (Data, Array) */
  339. 0xc0, /* End Collection */
  340. };
  341. static void usb_hid_changed(HIDState *hs)
  342. {
  343. USBHIDState *us = container_of(hs, USBHIDState, hid);
  344. usb_wakeup(&us->dev);
  345. }
  346. static void usb_hid_handle_reset(USBDevice *dev)
  347. {
  348. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  349. hid_reset(&us->hid);
  350. }
  351. static int usb_hid_handle_control(USBDevice *dev, USBPacket *p,
  352. int request, int value, int index, int length, uint8_t *data)
  353. {
  354. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  355. HIDState *hs = &us->hid;
  356. int ret;
  357. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  358. if (ret >= 0) {
  359. return ret;
  360. }
  361. ret = 0;
  362. switch (request) {
  363. case DeviceRequest | USB_REQ_GET_INTERFACE:
  364. data[0] = 0;
  365. ret = 1;
  366. break;
  367. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  368. ret = 0;
  369. break;
  370. /* hid specific requests */
  371. case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
  372. switch (value >> 8) {
  373. case 0x22:
  374. if (hs->kind == HID_MOUSE) {
  375. memcpy(data, qemu_mouse_hid_report_descriptor,
  376. sizeof(qemu_mouse_hid_report_descriptor));
  377. ret = sizeof(qemu_mouse_hid_report_descriptor);
  378. } else if (hs->kind == HID_TABLET) {
  379. memcpy(data, qemu_tablet_hid_report_descriptor,
  380. sizeof(qemu_tablet_hid_report_descriptor));
  381. ret = sizeof(qemu_tablet_hid_report_descriptor);
  382. } else if (hs->kind == HID_KEYBOARD) {
  383. memcpy(data, qemu_keyboard_hid_report_descriptor,
  384. sizeof(qemu_keyboard_hid_report_descriptor));
  385. ret = sizeof(qemu_keyboard_hid_report_descriptor);
  386. }
  387. break;
  388. default:
  389. goto fail;
  390. }
  391. break;
  392. case GET_REPORT:
  393. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  394. ret = hid_pointer_poll(hs, data, length);
  395. } else if (hs->kind == HID_KEYBOARD) {
  396. ret = hid_keyboard_poll(hs, data, length);
  397. }
  398. break;
  399. case SET_REPORT:
  400. if (hs->kind == HID_KEYBOARD) {
  401. ret = hid_keyboard_write(hs, data, length);
  402. } else {
  403. goto fail;
  404. }
  405. break;
  406. case GET_PROTOCOL:
  407. if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
  408. goto fail;
  409. }
  410. ret = 1;
  411. data[0] = hs->protocol;
  412. break;
  413. case SET_PROTOCOL:
  414. if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
  415. goto fail;
  416. }
  417. ret = 0;
  418. hs->protocol = value;
  419. break;
  420. case GET_IDLE:
  421. ret = 1;
  422. data[0] = hs->idle;
  423. break;
  424. case SET_IDLE:
  425. hs->idle = (uint8_t) (value >> 8);
  426. hid_set_next_idle(hs, qemu_get_clock_ns(vm_clock));
  427. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  428. hid_pointer_activate(hs);
  429. }
  430. ret = 0;
  431. break;
  432. default:
  433. fail:
  434. ret = USB_RET_STALL;
  435. break;
  436. }
  437. return ret;
  438. }
  439. static int usb_hid_handle_data(USBDevice *dev, USBPacket *p)
  440. {
  441. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  442. HIDState *hs = &us->hid;
  443. uint8_t buf[p->iov.size];
  444. int ret = 0;
  445. switch (p->pid) {
  446. case USB_TOKEN_IN:
  447. if (p->devep == 1) {
  448. int64_t curtime = qemu_get_clock_ns(vm_clock);
  449. if (!hid_has_events(hs) &&
  450. (!hs->idle || hs->next_idle_clock - curtime > 0)) {
  451. return USB_RET_NAK;
  452. }
  453. hid_set_next_idle(hs, curtime);
  454. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  455. ret = hid_pointer_poll(hs, buf, p->iov.size);
  456. } else if (hs->kind == HID_KEYBOARD) {
  457. ret = hid_keyboard_poll(hs, buf, p->iov.size);
  458. }
  459. usb_packet_copy(p, buf, ret);
  460. } else {
  461. goto fail;
  462. }
  463. break;
  464. case USB_TOKEN_OUT:
  465. default:
  466. fail:
  467. ret = USB_RET_STALL;
  468. break;
  469. }
  470. return ret;
  471. }
  472. static void usb_hid_handle_destroy(USBDevice *dev)
  473. {
  474. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  475. hid_free(&us->hid);
  476. }
  477. static int usb_hid_initfn(USBDevice *dev, int kind)
  478. {
  479. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  480. usb_desc_init(dev);
  481. hid_init(&us->hid, kind, usb_hid_changed);
  482. return 0;
  483. }
  484. static int usb_tablet_initfn(USBDevice *dev)
  485. {
  486. return usb_hid_initfn(dev, HID_TABLET);
  487. }
  488. static int usb_mouse_initfn(USBDevice *dev)
  489. {
  490. return usb_hid_initfn(dev, HID_MOUSE);
  491. }
  492. static int usb_keyboard_initfn(USBDevice *dev)
  493. {
  494. return usb_hid_initfn(dev, HID_KEYBOARD);
  495. }
  496. static int usb_ptr_post_load(void *opaque, int version_id)
  497. {
  498. USBHIDState *s = opaque;
  499. if (s->dev.remote_wakeup) {
  500. hid_pointer_activate(&s->hid);
  501. }
  502. return 0;
  503. }
  504. static const VMStateDescription vmstate_usb_ptr = {
  505. .name = "usb-ptr",
  506. .version_id = 1,
  507. .minimum_version_id = 1,
  508. .post_load = usb_ptr_post_load,
  509. .fields = (VMStateField []) {
  510. VMSTATE_USB_DEVICE(dev, USBHIDState),
  511. VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
  512. VMSTATE_END_OF_LIST()
  513. }
  514. };
  515. static const VMStateDescription vmstate_usb_kbd = {
  516. .name = "usb-kbd",
  517. .version_id = 1,
  518. .minimum_version_id = 1,
  519. .fields = (VMStateField []) {
  520. VMSTATE_USB_DEVICE(dev, USBHIDState),
  521. VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
  522. VMSTATE_END_OF_LIST()
  523. }
  524. };
  525. static struct USBDeviceInfo hid_info[] = {
  526. {
  527. .product_desc = "QEMU USB Tablet",
  528. .qdev.name = "usb-tablet",
  529. .usbdevice_name = "tablet",
  530. .qdev.size = sizeof(USBHIDState),
  531. .qdev.vmsd = &vmstate_usb_ptr,
  532. .usb_desc = &desc_tablet,
  533. .init = usb_tablet_initfn,
  534. .handle_packet = usb_generic_handle_packet,
  535. .handle_reset = usb_hid_handle_reset,
  536. .handle_control = usb_hid_handle_control,
  537. .handle_data = usb_hid_handle_data,
  538. .handle_destroy = usb_hid_handle_destroy,
  539. },{
  540. .product_desc = "QEMU USB Mouse",
  541. .qdev.name = "usb-mouse",
  542. .usbdevice_name = "mouse",
  543. .qdev.size = sizeof(USBHIDState),
  544. .qdev.vmsd = &vmstate_usb_ptr,
  545. .usb_desc = &desc_mouse,
  546. .init = usb_mouse_initfn,
  547. .handle_packet = usb_generic_handle_packet,
  548. .handle_reset = usb_hid_handle_reset,
  549. .handle_control = usb_hid_handle_control,
  550. .handle_data = usb_hid_handle_data,
  551. .handle_destroy = usb_hid_handle_destroy,
  552. },{
  553. .product_desc = "QEMU USB Keyboard",
  554. .qdev.name = "usb-kbd",
  555. .usbdevice_name = "keyboard",
  556. .qdev.size = sizeof(USBHIDState),
  557. .qdev.vmsd = &vmstate_usb_kbd,
  558. .usb_desc = &desc_keyboard,
  559. .init = usb_keyboard_initfn,
  560. .handle_packet = usb_generic_handle_packet,
  561. .handle_reset = usb_hid_handle_reset,
  562. .handle_control = usb_hid_handle_control,
  563. .handle_data = usb_hid_handle_data,
  564. .handle_destroy = usb_hid_handle_destroy,
  565. },{
  566. /* end of list */
  567. }
  568. };
  569. static void usb_hid_register_devices(void)
  570. {
  571. usb_qdev_register_many(hid_info);
  572. }
  573. device_init(usb_hid_register_devices)