dev-hid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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/hw.h"
  26. #include "ui/console.h"
  27. #include "hw/usb.h"
  28. #include "hw/usb/desc.h"
  29. #include "qemu/timer.h"
  30. #include "hw/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. USBEndpoint *intr;
  45. HIDState hid;
  46. uint32_t usb_version;
  47. } USBHIDState;
  48. enum {
  49. STR_MANUFACTURER = 1,
  50. STR_PRODUCT_MOUSE,
  51. STR_PRODUCT_TABLET,
  52. STR_PRODUCT_KEYBOARD,
  53. STR_SERIALNUMBER,
  54. STR_CONFIG_MOUSE,
  55. STR_CONFIG_TABLET,
  56. STR_CONFIG_KEYBOARD,
  57. };
  58. static const USBDescStrings desc_strings = {
  59. [STR_MANUFACTURER] = "QEMU",
  60. [STR_PRODUCT_MOUSE] = "QEMU USB Mouse",
  61. [STR_PRODUCT_TABLET] = "QEMU USB Tablet",
  62. [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
  63. [STR_SERIALNUMBER] = "42", /* == remote wakeup works */
  64. [STR_CONFIG_MOUSE] = "HID Mouse",
  65. [STR_CONFIG_TABLET] = "HID Tablet",
  66. [STR_CONFIG_KEYBOARD] = "HID Keyboard",
  67. };
  68. static const USBDescIface desc_iface_mouse = {
  69. .bInterfaceNumber = 0,
  70. .bNumEndpoints = 1,
  71. .bInterfaceClass = USB_CLASS_HID,
  72. .bInterfaceSubClass = 0x01, /* boot */
  73. .bInterfaceProtocol = 0x02,
  74. .ndesc = 1,
  75. .descs = (USBDescOther[]) {
  76. {
  77. /* HID descriptor */
  78. .data = (uint8_t[]) {
  79. 0x09, /* u8 bLength */
  80. USB_DT_HID, /* u8 bDescriptorType */
  81. 0x01, 0x00, /* u16 HID_class */
  82. 0x00, /* u8 country_code */
  83. 0x01, /* u8 num_descriptors */
  84. USB_DT_REPORT, /* u8 type: Report */
  85. 52, 0, /* u16 len */
  86. },
  87. },
  88. },
  89. .eps = (USBDescEndpoint[]) {
  90. {
  91. .bEndpointAddress = USB_DIR_IN | 0x01,
  92. .bmAttributes = USB_ENDPOINT_XFER_INT,
  93. .wMaxPacketSize = 4,
  94. .bInterval = 0x0a,
  95. },
  96. },
  97. };
  98. static const USBDescIface desc_iface_tablet = {
  99. .bInterfaceNumber = 0,
  100. .bNumEndpoints = 1,
  101. .bInterfaceClass = USB_CLASS_HID,
  102. .bInterfaceProtocol = 0x02,
  103. .ndesc = 1,
  104. .descs = (USBDescOther[]) {
  105. {
  106. /* HID descriptor */
  107. .data = (uint8_t[]) {
  108. 0x09, /* u8 bLength */
  109. USB_DT_HID, /* u8 bDescriptorType */
  110. 0x01, 0x00, /* u16 HID_class */
  111. 0x00, /* u8 country_code */
  112. 0x01, /* u8 num_descriptors */
  113. USB_DT_REPORT, /* u8 type: Report */
  114. 74, 0, /* u16 len */
  115. },
  116. },
  117. },
  118. .eps = (USBDescEndpoint[]) {
  119. {
  120. .bEndpointAddress = USB_DIR_IN | 0x01,
  121. .bmAttributes = USB_ENDPOINT_XFER_INT,
  122. .wMaxPacketSize = 8,
  123. .bInterval = 0x0a,
  124. },
  125. },
  126. };
  127. static const USBDescIface desc_iface_tablet2 = {
  128. .bInterfaceNumber = 0,
  129. .bNumEndpoints = 1,
  130. .bInterfaceClass = USB_CLASS_HID,
  131. .bInterfaceProtocol = 0x02,
  132. .ndesc = 1,
  133. .descs = (USBDescOther[]) {
  134. {
  135. /* HID descriptor */
  136. .data = (uint8_t[]) {
  137. 0x09, /* u8 bLength */
  138. USB_DT_HID, /* u8 bDescriptorType */
  139. 0x01, 0x00, /* u16 HID_class */
  140. 0x00, /* u8 country_code */
  141. 0x01, /* u8 num_descriptors */
  142. USB_DT_REPORT, /* u8 type: Report */
  143. 74, 0, /* u16 len */
  144. },
  145. },
  146. },
  147. .eps = (USBDescEndpoint[]) {
  148. {
  149. .bEndpointAddress = USB_DIR_IN | 0x01,
  150. .bmAttributes = USB_ENDPOINT_XFER_INT,
  151. .wMaxPacketSize = 8,
  152. .bInterval = 4, /* 2 ^ (4-1) * 125 usecs = 1 ms */
  153. },
  154. },
  155. };
  156. static const USBDescIface desc_iface_keyboard = {
  157. .bInterfaceNumber = 0,
  158. .bNumEndpoints = 1,
  159. .bInterfaceClass = USB_CLASS_HID,
  160. .bInterfaceSubClass = 0x01, /* boot */
  161. .bInterfaceProtocol = 0x01, /* keyboard */
  162. .ndesc = 1,
  163. .descs = (USBDescOther[]) {
  164. {
  165. /* HID descriptor */
  166. .data = (uint8_t[]) {
  167. 0x09, /* u8 bLength */
  168. USB_DT_HID, /* u8 bDescriptorType */
  169. 0x11, 0x01, /* u16 HID_class */
  170. 0x00, /* u8 country_code */
  171. 0x01, /* u8 num_descriptors */
  172. USB_DT_REPORT, /* u8 type: Report */
  173. 0x3f, 0, /* u16 len */
  174. },
  175. },
  176. },
  177. .eps = (USBDescEndpoint[]) {
  178. {
  179. .bEndpointAddress = USB_DIR_IN | 0x01,
  180. .bmAttributes = USB_ENDPOINT_XFER_INT,
  181. .wMaxPacketSize = 8,
  182. .bInterval = 0x0a,
  183. },
  184. },
  185. };
  186. static const USBDescDevice desc_device_mouse = {
  187. .bcdUSB = 0x0100,
  188. .bMaxPacketSize0 = 8,
  189. .bNumConfigurations = 1,
  190. .confs = (USBDescConfig[]) {
  191. {
  192. .bNumInterfaces = 1,
  193. .bConfigurationValue = 1,
  194. .iConfiguration = STR_CONFIG_MOUSE,
  195. .bmAttributes = 0xa0,
  196. .bMaxPower = 50,
  197. .nif = 1,
  198. .ifs = &desc_iface_mouse,
  199. },
  200. },
  201. };
  202. static const USBDescDevice desc_device_tablet = {
  203. .bcdUSB = 0x0100,
  204. .bMaxPacketSize0 = 8,
  205. .bNumConfigurations = 1,
  206. .confs = (USBDescConfig[]) {
  207. {
  208. .bNumInterfaces = 1,
  209. .bConfigurationValue = 1,
  210. .iConfiguration = STR_CONFIG_TABLET,
  211. .bmAttributes = 0xa0,
  212. .bMaxPower = 50,
  213. .nif = 1,
  214. .ifs = &desc_iface_tablet,
  215. },
  216. },
  217. };
  218. static const USBDescDevice desc_device_tablet2 = {
  219. .bcdUSB = 0x0200,
  220. .bMaxPacketSize0 = 64,
  221. .bNumConfigurations = 1,
  222. .confs = (USBDescConfig[]) {
  223. {
  224. .bNumInterfaces = 1,
  225. .bConfigurationValue = 1,
  226. .iConfiguration = STR_CONFIG_TABLET,
  227. .bmAttributes = 0x80,
  228. .bMaxPower = 50,
  229. .nif = 1,
  230. .ifs = &desc_iface_tablet2,
  231. },
  232. },
  233. };
  234. static const USBDescDevice desc_device_keyboard = {
  235. .bcdUSB = 0x0100,
  236. .bMaxPacketSize0 = 8,
  237. .bNumConfigurations = 1,
  238. .confs = (USBDescConfig[]) {
  239. {
  240. .bNumInterfaces = 1,
  241. .bConfigurationValue = 1,
  242. .iConfiguration = STR_CONFIG_KEYBOARD,
  243. .bmAttributes = 0xa0,
  244. .bMaxPower = 50,
  245. .nif = 1,
  246. .ifs = &desc_iface_keyboard,
  247. },
  248. },
  249. };
  250. static const USBDesc desc_mouse = {
  251. .id = {
  252. .idVendor = 0x0627,
  253. .idProduct = 0x0001,
  254. .bcdDevice = 0,
  255. .iManufacturer = STR_MANUFACTURER,
  256. .iProduct = STR_PRODUCT_MOUSE,
  257. .iSerialNumber = STR_SERIALNUMBER,
  258. },
  259. .full = &desc_device_mouse,
  260. .str = desc_strings,
  261. };
  262. static const USBDesc desc_tablet = {
  263. .id = {
  264. .idVendor = 0x0627,
  265. .idProduct = 0x0001,
  266. .bcdDevice = 0,
  267. .iManufacturer = STR_MANUFACTURER,
  268. .iProduct = STR_PRODUCT_TABLET,
  269. .iSerialNumber = STR_SERIALNUMBER,
  270. },
  271. .full = &desc_device_tablet,
  272. .str = desc_strings,
  273. };
  274. static const USBDesc desc_tablet2 = {
  275. .id = {
  276. .idVendor = 0x0627,
  277. .idProduct = 0x0001,
  278. .bcdDevice = 0,
  279. .iManufacturer = STR_MANUFACTURER,
  280. .iProduct = STR_PRODUCT_TABLET,
  281. .iSerialNumber = STR_SERIALNUMBER,
  282. },
  283. .full = &desc_device_tablet,
  284. .high = &desc_device_tablet2,
  285. .str = desc_strings,
  286. };
  287. static const USBDesc desc_keyboard = {
  288. .id = {
  289. .idVendor = 0x0627,
  290. .idProduct = 0x0001,
  291. .bcdDevice = 0,
  292. .iManufacturer = STR_MANUFACTURER,
  293. .iProduct = STR_PRODUCT_KEYBOARD,
  294. .iSerialNumber = STR_SERIALNUMBER,
  295. },
  296. .full = &desc_device_keyboard,
  297. .str = desc_strings,
  298. };
  299. static const uint8_t qemu_mouse_hid_report_descriptor[] = {
  300. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  301. 0x09, 0x02, /* Usage (Mouse) */
  302. 0xa1, 0x01, /* Collection (Application) */
  303. 0x09, 0x01, /* Usage (Pointer) */
  304. 0xa1, 0x00, /* Collection (Physical) */
  305. 0x05, 0x09, /* Usage Page (Button) */
  306. 0x19, 0x01, /* Usage Minimum (1) */
  307. 0x29, 0x03, /* Usage Maximum (3) */
  308. 0x15, 0x00, /* Logical Minimum (0) */
  309. 0x25, 0x01, /* Logical Maximum (1) */
  310. 0x95, 0x03, /* Report Count (3) */
  311. 0x75, 0x01, /* Report Size (1) */
  312. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  313. 0x95, 0x01, /* Report Count (1) */
  314. 0x75, 0x05, /* Report Size (5) */
  315. 0x81, 0x01, /* Input (Constant) */
  316. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  317. 0x09, 0x30, /* Usage (X) */
  318. 0x09, 0x31, /* Usage (Y) */
  319. 0x09, 0x38, /* Usage (Wheel) */
  320. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  321. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  322. 0x75, 0x08, /* Report Size (8) */
  323. 0x95, 0x03, /* Report Count (3) */
  324. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  325. 0xc0, /* End Collection */
  326. 0xc0, /* End Collection */
  327. };
  328. static const uint8_t qemu_tablet_hid_report_descriptor[] = {
  329. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  330. 0x09, 0x01, /* Usage (Pointer) */
  331. 0xa1, 0x01, /* Collection (Application) */
  332. 0x09, 0x01, /* Usage (Pointer) */
  333. 0xa1, 0x00, /* Collection (Physical) */
  334. 0x05, 0x09, /* Usage Page (Button) */
  335. 0x19, 0x01, /* Usage Minimum (1) */
  336. 0x29, 0x03, /* Usage Maximum (3) */
  337. 0x15, 0x00, /* Logical Minimum (0) */
  338. 0x25, 0x01, /* Logical Maximum (1) */
  339. 0x95, 0x03, /* Report Count (3) */
  340. 0x75, 0x01, /* Report Size (1) */
  341. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  342. 0x95, 0x01, /* Report Count (1) */
  343. 0x75, 0x05, /* Report Size (5) */
  344. 0x81, 0x01, /* Input (Constant) */
  345. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  346. 0x09, 0x30, /* Usage (X) */
  347. 0x09, 0x31, /* Usage (Y) */
  348. 0x15, 0x00, /* Logical Minimum (0) */
  349. 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
  350. 0x35, 0x00, /* Physical Minimum (0) */
  351. 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
  352. 0x75, 0x10, /* Report Size (16) */
  353. 0x95, 0x02, /* Report Count (2) */
  354. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  355. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  356. 0x09, 0x38, /* Usage (Wheel) */
  357. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  358. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  359. 0x35, 0x00, /* Physical Minimum (same as logical) */
  360. 0x45, 0x00, /* Physical Maximum (same as logical) */
  361. 0x75, 0x08, /* Report Size (8) */
  362. 0x95, 0x01, /* Report Count (1) */
  363. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  364. 0xc0, /* End Collection */
  365. 0xc0, /* End Collection */
  366. };
  367. static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
  368. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  369. 0x09, 0x06, /* Usage (Keyboard) */
  370. 0xa1, 0x01, /* Collection (Application) */
  371. 0x75, 0x01, /* Report Size (1) */
  372. 0x95, 0x08, /* Report Count (8) */
  373. 0x05, 0x07, /* Usage Page (Key Codes) */
  374. 0x19, 0xe0, /* Usage Minimum (224) */
  375. 0x29, 0xe7, /* Usage Maximum (231) */
  376. 0x15, 0x00, /* Logical Minimum (0) */
  377. 0x25, 0x01, /* Logical Maximum (1) */
  378. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  379. 0x95, 0x01, /* Report Count (1) */
  380. 0x75, 0x08, /* Report Size (8) */
  381. 0x81, 0x01, /* Input (Constant) */
  382. 0x95, 0x05, /* Report Count (5) */
  383. 0x75, 0x01, /* Report Size (1) */
  384. 0x05, 0x08, /* Usage Page (LEDs) */
  385. 0x19, 0x01, /* Usage Minimum (1) */
  386. 0x29, 0x05, /* Usage Maximum (5) */
  387. 0x91, 0x02, /* Output (Data, Variable, Absolute) */
  388. 0x95, 0x01, /* Report Count (1) */
  389. 0x75, 0x03, /* Report Size (3) */
  390. 0x91, 0x01, /* Output (Constant) */
  391. 0x95, 0x06, /* Report Count (6) */
  392. 0x75, 0x08, /* Report Size (8) */
  393. 0x15, 0x00, /* Logical Minimum (0) */
  394. 0x25, 0xff, /* Logical Maximum (255) */
  395. 0x05, 0x07, /* Usage Page (Key Codes) */
  396. 0x19, 0x00, /* Usage Minimum (0) */
  397. 0x29, 0xff, /* Usage Maximum (255) */
  398. 0x81, 0x00, /* Input (Data, Array) */
  399. 0xc0, /* End Collection */
  400. };
  401. static void usb_hid_changed(HIDState *hs)
  402. {
  403. USBHIDState *us = container_of(hs, USBHIDState, hid);
  404. usb_wakeup(us->intr);
  405. }
  406. static void usb_hid_handle_reset(USBDevice *dev)
  407. {
  408. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  409. hid_reset(&us->hid);
  410. }
  411. static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
  412. int request, int value, int index, int length, uint8_t *data)
  413. {
  414. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  415. HIDState *hs = &us->hid;
  416. int ret;
  417. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  418. if (ret >= 0) {
  419. return;
  420. }
  421. switch (request) {
  422. /* hid specific requests */
  423. case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
  424. switch (value >> 8) {
  425. case 0x22:
  426. if (hs->kind == HID_MOUSE) {
  427. memcpy(data, qemu_mouse_hid_report_descriptor,
  428. sizeof(qemu_mouse_hid_report_descriptor));
  429. p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
  430. } else if (hs->kind == HID_TABLET) {
  431. memcpy(data, qemu_tablet_hid_report_descriptor,
  432. sizeof(qemu_tablet_hid_report_descriptor));
  433. p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
  434. } else if (hs->kind == HID_KEYBOARD) {
  435. memcpy(data, qemu_keyboard_hid_report_descriptor,
  436. sizeof(qemu_keyboard_hid_report_descriptor));
  437. p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
  438. }
  439. break;
  440. default:
  441. goto fail;
  442. }
  443. break;
  444. case GET_REPORT:
  445. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  446. p->actual_length = hid_pointer_poll(hs, data, length);
  447. } else if (hs->kind == HID_KEYBOARD) {
  448. p->actual_length = hid_keyboard_poll(hs, data, length);
  449. }
  450. break;
  451. case SET_REPORT:
  452. if (hs->kind == HID_KEYBOARD) {
  453. p->actual_length = hid_keyboard_write(hs, data, length);
  454. } else {
  455. goto fail;
  456. }
  457. break;
  458. case GET_PROTOCOL:
  459. if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
  460. goto fail;
  461. }
  462. data[0] = hs->protocol;
  463. p->actual_length = 1;
  464. break;
  465. case SET_PROTOCOL:
  466. if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
  467. goto fail;
  468. }
  469. hs->protocol = value;
  470. break;
  471. case GET_IDLE:
  472. data[0] = hs->idle;
  473. p->actual_length = 1;
  474. break;
  475. case SET_IDLE:
  476. hs->idle = (uint8_t) (value >> 8);
  477. hid_set_next_idle(hs);
  478. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  479. hid_pointer_activate(hs);
  480. }
  481. break;
  482. default:
  483. fail:
  484. p->status = USB_RET_STALL;
  485. break;
  486. }
  487. }
  488. static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
  489. {
  490. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  491. HIDState *hs = &us->hid;
  492. uint8_t buf[p->iov.size];
  493. int len = 0;
  494. switch (p->pid) {
  495. case USB_TOKEN_IN:
  496. if (p->ep->nr == 1) {
  497. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  498. hid_pointer_activate(hs);
  499. }
  500. if (!hid_has_events(hs)) {
  501. p->status = USB_RET_NAK;
  502. return;
  503. }
  504. hid_set_next_idle(hs);
  505. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  506. len = hid_pointer_poll(hs, buf, p->iov.size);
  507. } else if (hs->kind == HID_KEYBOARD) {
  508. len = hid_keyboard_poll(hs, buf, p->iov.size);
  509. }
  510. usb_packet_copy(p, buf, len);
  511. } else {
  512. goto fail;
  513. }
  514. break;
  515. case USB_TOKEN_OUT:
  516. default:
  517. fail:
  518. p->status = USB_RET_STALL;
  519. break;
  520. }
  521. }
  522. static void usb_hid_handle_destroy(USBDevice *dev)
  523. {
  524. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  525. hid_free(&us->hid);
  526. }
  527. static int usb_hid_initfn(USBDevice *dev, int kind)
  528. {
  529. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  530. usb_desc_init(dev);
  531. us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
  532. hid_init(&us->hid, kind, usb_hid_changed);
  533. return 0;
  534. }
  535. static int usb_tablet_initfn(USBDevice *dev)
  536. {
  537. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  538. switch (us->usb_version) {
  539. case 1:
  540. dev->usb_desc = &desc_tablet;
  541. break;
  542. case 2:
  543. dev->usb_desc = &desc_tablet2;
  544. break;
  545. default:
  546. error_report("Invalid usb version %d for usb-tabler (must be 1 or 2)",
  547. us->usb_version);
  548. return -1;
  549. }
  550. return usb_hid_initfn(dev, HID_TABLET);
  551. }
  552. static int usb_mouse_initfn(USBDevice *dev)
  553. {
  554. return usb_hid_initfn(dev, HID_MOUSE);
  555. }
  556. static int usb_keyboard_initfn(USBDevice *dev)
  557. {
  558. return usb_hid_initfn(dev, HID_KEYBOARD);
  559. }
  560. static int usb_ptr_post_load(void *opaque, int version_id)
  561. {
  562. USBHIDState *s = opaque;
  563. if (s->dev.remote_wakeup) {
  564. hid_pointer_activate(&s->hid);
  565. }
  566. return 0;
  567. }
  568. static const VMStateDescription vmstate_usb_ptr = {
  569. .name = "usb-ptr",
  570. .version_id = 1,
  571. .minimum_version_id = 1,
  572. .post_load = usb_ptr_post_load,
  573. .fields = (VMStateField []) {
  574. VMSTATE_USB_DEVICE(dev, USBHIDState),
  575. VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
  576. VMSTATE_END_OF_LIST()
  577. }
  578. };
  579. static const VMStateDescription vmstate_usb_kbd = {
  580. .name = "usb-kbd",
  581. .version_id = 1,
  582. .minimum_version_id = 1,
  583. .fields = (VMStateField []) {
  584. VMSTATE_USB_DEVICE(dev, USBHIDState),
  585. VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
  586. VMSTATE_END_OF_LIST()
  587. }
  588. };
  589. static void usb_hid_class_initfn(ObjectClass *klass, void *data)
  590. {
  591. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  592. uc->handle_reset = usb_hid_handle_reset;
  593. uc->handle_control = usb_hid_handle_control;
  594. uc->handle_data = usb_hid_handle_data;
  595. uc->handle_destroy = usb_hid_handle_destroy;
  596. uc->handle_attach = usb_desc_attach;
  597. }
  598. static Property usb_tablet_properties[] = {
  599. DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
  600. DEFINE_PROP_END_OF_LIST(),
  601. };
  602. static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
  603. {
  604. DeviceClass *dc = DEVICE_CLASS(klass);
  605. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  606. usb_hid_class_initfn(klass, data);
  607. uc->init = usb_tablet_initfn;
  608. uc->product_desc = "QEMU USB Tablet";
  609. dc->vmsd = &vmstate_usb_ptr;
  610. dc->props = usb_tablet_properties;
  611. }
  612. static const TypeInfo usb_tablet_info = {
  613. .name = "usb-tablet",
  614. .parent = TYPE_USB_DEVICE,
  615. .instance_size = sizeof(USBHIDState),
  616. .class_init = usb_tablet_class_initfn,
  617. };
  618. static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
  619. {
  620. DeviceClass *dc = DEVICE_CLASS(klass);
  621. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  622. usb_hid_class_initfn(klass, data);
  623. uc->init = usb_mouse_initfn;
  624. uc->product_desc = "QEMU USB Mouse";
  625. uc->usb_desc = &desc_mouse;
  626. dc->vmsd = &vmstate_usb_ptr;
  627. }
  628. static const TypeInfo usb_mouse_info = {
  629. .name = "usb-mouse",
  630. .parent = TYPE_USB_DEVICE,
  631. .instance_size = sizeof(USBHIDState),
  632. .class_init = usb_mouse_class_initfn,
  633. };
  634. static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
  635. {
  636. DeviceClass *dc = DEVICE_CLASS(klass);
  637. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  638. usb_hid_class_initfn(klass, data);
  639. uc->init = usb_keyboard_initfn;
  640. uc->product_desc = "QEMU USB Keyboard";
  641. uc->usb_desc = &desc_keyboard;
  642. dc->vmsd = &vmstate_usb_kbd;
  643. }
  644. static const TypeInfo usb_keyboard_info = {
  645. .name = "usb-kbd",
  646. .parent = TYPE_USB_DEVICE,
  647. .instance_size = sizeof(USBHIDState),
  648. .class_init = usb_keyboard_class_initfn,
  649. };
  650. static void usb_hid_register_types(void)
  651. {
  652. type_register_static(&usb_tablet_info);
  653. usb_legacy_register("usb-tablet", "tablet", NULL);
  654. type_register_static(&usb_mouse_info);
  655. usb_legacy_register("usb-mouse", "mouse", NULL);
  656. type_register_static(&usb_keyboard_info);
  657. usb_legacy_register("usb-kbd", "keyboard", NULL);
  658. }
  659. type_init(usb_hid_register_types)