dev-hid.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/input/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 = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
  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 = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
  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 = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
  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 = USB_CFG_ATT_ONE | USB_CFG_ATT_WAKEUP,
  244. .bMaxPower = 50,
  245. .nif = 1,
  246. .ifs = &desc_iface_keyboard,
  247. },
  248. },
  249. };
  250. static const USBDescMSOS desc_msos_suspend = {
  251. .SelectiveSuspendEnabled = true,
  252. };
  253. static const USBDesc desc_mouse = {
  254. .id = {
  255. .idVendor = 0x0627,
  256. .idProduct = 0x0001,
  257. .bcdDevice = 0,
  258. .iManufacturer = STR_MANUFACTURER,
  259. .iProduct = STR_PRODUCT_MOUSE,
  260. .iSerialNumber = STR_SERIALNUMBER,
  261. },
  262. .full = &desc_device_mouse,
  263. .str = desc_strings,
  264. .msos = &desc_msos_suspend,
  265. };
  266. static const USBDesc desc_tablet = {
  267. .id = {
  268. .idVendor = 0x0627,
  269. .idProduct = 0x0001,
  270. .bcdDevice = 0,
  271. .iManufacturer = STR_MANUFACTURER,
  272. .iProduct = STR_PRODUCT_TABLET,
  273. .iSerialNumber = STR_SERIALNUMBER,
  274. },
  275. .full = &desc_device_tablet,
  276. .str = desc_strings,
  277. .msos = &desc_msos_suspend,
  278. };
  279. static const USBDesc desc_tablet2 = {
  280. .id = {
  281. .idVendor = 0x0627,
  282. .idProduct = 0x0001,
  283. .bcdDevice = 0,
  284. .iManufacturer = STR_MANUFACTURER,
  285. .iProduct = STR_PRODUCT_TABLET,
  286. .iSerialNumber = STR_SERIALNUMBER,
  287. },
  288. .full = &desc_device_tablet,
  289. .high = &desc_device_tablet2,
  290. .str = desc_strings,
  291. .msos = &desc_msos_suspend,
  292. };
  293. static const USBDesc desc_keyboard = {
  294. .id = {
  295. .idVendor = 0x0627,
  296. .idProduct = 0x0001,
  297. .bcdDevice = 0,
  298. .iManufacturer = STR_MANUFACTURER,
  299. .iProduct = STR_PRODUCT_KEYBOARD,
  300. .iSerialNumber = STR_SERIALNUMBER,
  301. },
  302. .full = &desc_device_keyboard,
  303. .str = desc_strings,
  304. .msos = &desc_msos_suspend,
  305. };
  306. static const uint8_t qemu_mouse_hid_report_descriptor[] = {
  307. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  308. 0x09, 0x02, /* Usage (Mouse) */
  309. 0xa1, 0x01, /* Collection (Application) */
  310. 0x09, 0x01, /* Usage (Pointer) */
  311. 0xa1, 0x00, /* Collection (Physical) */
  312. 0x05, 0x09, /* Usage Page (Button) */
  313. 0x19, 0x01, /* Usage Minimum (1) */
  314. 0x29, 0x03, /* Usage Maximum (3) */
  315. 0x15, 0x00, /* Logical Minimum (0) */
  316. 0x25, 0x01, /* Logical Maximum (1) */
  317. 0x95, 0x03, /* Report Count (3) */
  318. 0x75, 0x01, /* Report Size (1) */
  319. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  320. 0x95, 0x01, /* Report Count (1) */
  321. 0x75, 0x05, /* Report Size (5) */
  322. 0x81, 0x01, /* Input (Constant) */
  323. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  324. 0x09, 0x30, /* Usage (X) */
  325. 0x09, 0x31, /* Usage (Y) */
  326. 0x09, 0x38, /* Usage (Wheel) */
  327. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  328. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  329. 0x75, 0x08, /* Report Size (8) */
  330. 0x95, 0x03, /* Report Count (3) */
  331. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  332. 0xc0, /* End Collection */
  333. 0xc0, /* End Collection */
  334. };
  335. static const uint8_t qemu_tablet_hid_report_descriptor[] = {
  336. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  337. 0x09, 0x01, /* Usage (Pointer) */
  338. 0xa1, 0x01, /* Collection (Application) */
  339. 0x09, 0x01, /* Usage (Pointer) */
  340. 0xa1, 0x00, /* Collection (Physical) */
  341. 0x05, 0x09, /* Usage Page (Button) */
  342. 0x19, 0x01, /* Usage Minimum (1) */
  343. 0x29, 0x03, /* Usage Maximum (3) */
  344. 0x15, 0x00, /* Logical Minimum (0) */
  345. 0x25, 0x01, /* Logical Maximum (1) */
  346. 0x95, 0x03, /* Report Count (3) */
  347. 0x75, 0x01, /* Report Size (1) */
  348. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  349. 0x95, 0x01, /* Report Count (1) */
  350. 0x75, 0x05, /* Report Size (5) */
  351. 0x81, 0x01, /* Input (Constant) */
  352. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  353. 0x09, 0x30, /* Usage (X) */
  354. 0x09, 0x31, /* Usage (Y) */
  355. 0x15, 0x00, /* Logical Minimum (0) */
  356. 0x26, 0xff, 0x7f, /* Logical Maximum (0x7fff) */
  357. 0x35, 0x00, /* Physical Minimum (0) */
  358. 0x46, 0xff, 0x7f, /* Physical Maximum (0x7fff) */
  359. 0x75, 0x10, /* Report Size (16) */
  360. 0x95, 0x02, /* Report Count (2) */
  361. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  362. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  363. 0x09, 0x38, /* Usage (Wheel) */
  364. 0x15, 0x81, /* Logical Minimum (-0x7f) */
  365. 0x25, 0x7f, /* Logical Maximum (0x7f) */
  366. 0x35, 0x00, /* Physical Minimum (same as logical) */
  367. 0x45, 0x00, /* Physical Maximum (same as logical) */
  368. 0x75, 0x08, /* Report Size (8) */
  369. 0x95, 0x01, /* Report Count (1) */
  370. 0x81, 0x06, /* Input (Data, Variable, Relative) */
  371. 0xc0, /* End Collection */
  372. 0xc0, /* End Collection */
  373. };
  374. static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
  375. 0x05, 0x01, /* Usage Page (Generic Desktop) */
  376. 0x09, 0x06, /* Usage (Keyboard) */
  377. 0xa1, 0x01, /* Collection (Application) */
  378. 0x75, 0x01, /* Report Size (1) */
  379. 0x95, 0x08, /* Report Count (8) */
  380. 0x05, 0x07, /* Usage Page (Key Codes) */
  381. 0x19, 0xe0, /* Usage Minimum (224) */
  382. 0x29, 0xe7, /* Usage Maximum (231) */
  383. 0x15, 0x00, /* Logical Minimum (0) */
  384. 0x25, 0x01, /* Logical Maximum (1) */
  385. 0x81, 0x02, /* Input (Data, Variable, Absolute) */
  386. 0x95, 0x01, /* Report Count (1) */
  387. 0x75, 0x08, /* Report Size (8) */
  388. 0x81, 0x01, /* Input (Constant) */
  389. 0x95, 0x05, /* Report Count (5) */
  390. 0x75, 0x01, /* Report Size (1) */
  391. 0x05, 0x08, /* Usage Page (LEDs) */
  392. 0x19, 0x01, /* Usage Minimum (1) */
  393. 0x29, 0x05, /* Usage Maximum (5) */
  394. 0x91, 0x02, /* Output (Data, Variable, Absolute) */
  395. 0x95, 0x01, /* Report Count (1) */
  396. 0x75, 0x03, /* Report Size (3) */
  397. 0x91, 0x01, /* Output (Constant) */
  398. 0x95, 0x06, /* Report Count (6) */
  399. 0x75, 0x08, /* Report Size (8) */
  400. 0x15, 0x00, /* Logical Minimum (0) */
  401. 0x25, 0xff, /* Logical Maximum (255) */
  402. 0x05, 0x07, /* Usage Page (Key Codes) */
  403. 0x19, 0x00, /* Usage Minimum (0) */
  404. 0x29, 0xff, /* Usage Maximum (255) */
  405. 0x81, 0x00, /* Input (Data, Array) */
  406. 0xc0, /* End Collection */
  407. };
  408. static void usb_hid_changed(HIDState *hs)
  409. {
  410. USBHIDState *us = container_of(hs, USBHIDState, hid);
  411. usb_wakeup(us->intr, 0);
  412. }
  413. static void usb_hid_handle_reset(USBDevice *dev)
  414. {
  415. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  416. hid_reset(&us->hid);
  417. }
  418. static void usb_hid_handle_control(USBDevice *dev, USBPacket *p,
  419. int request, int value, int index, int length, uint8_t *data)
  420. {
  421. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  422. HIDState *hs = &us->hid;
  423. int ret;
  424. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  425. if (ret >= 0) {
  426. return;
  427. }
  428. switch (request) {
  429. /* hid specific requests */
  430. case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
  431. switch (value >> 8) {
  432. case 0x22:
  433. if (hs->kind == HID_MOUSE) {
  434. memcpy(data, qemu_mouse_hid_report_descriptor,
  435. sizeof(qemu_mouse_hid_report_descriptor));
  436. p->actual_length = sizeof(qemu_mouse_hid_report_descriptor);
  437. } else if (hs->kind == HID_TABLET) {
  438. memcpy(data, qemu_tablet_hid_report_descriptor,
  439. sizeof(qemu_tablet_hid_report_descriptor));
  440. p->actual_length = sizeof(qemu_tablet_hid_report_descriptor);
  441. } else if (hs->kind == HID_KEYBOARD) {
  442. memcpy(data, qemu_keyboard_hid_report_descriptor,
  443. sizeof(qemu_keyboard_hid_report_descriptor));
  444. p->actual_length = sizeof(qemu_keyboard_hid_report_descriptor);
  445. }
  446. break;
  447. default:
  448. goto fail;
  449. }
  450. break;
  451. case GET_REPORT:
  452. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  453. p->actual_length = hid_pointer_poll(hs, data, length);
  454. } else if (hs->kind == HID_KEYBOARD) {
  455. p->actual_length = hid_keyboard_poll(hs, data, length);
  456. }
  457. break;
  458. case SET_REPORT:
  459. if (hs->kind == HID_KEYBOARD) {
  460. p->actual_length = hid_keyboard_write(hs, data, length);
  461. } else {
  462. goto fail;
  463. }
  464. break;
  465. case GET_PROTOCOL:
  466. if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
  467. goto fail;
  468. }
  469. data[0] = hs->protocol;
  470. p->actual_length = 1;
  471. break;
  472. case SET_PROTOCOL:
  473. if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
  474. goto fail;
  475. }
  476. hs->protocol = value;
  477. break;
  478. case GET_IDLE:
  479. data[0] = hs->idle;
  480. p->actual_length = 1;
  481. break;
  482. case SET_IDLE:
  483. hs->idle = (uint8_t) (value >> 8);
  484. hid_set_next_idle(hs);
  485. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  486. hid_pointer_activate(hs);
  487. }
  488. break;
  489. default:
  490. fail:
  491. p->status = USB_RET_STALL;
  492. break;
  493. }
  494. }
  495. static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
  496. {
  497. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  498. HIDState *hs = &us->hid;
  499. uint8_t buf[p->iov.size];
  500. int len = 0;
  501. switch (p->pid) {
  502. case USB_TOKEN_IN:
  503. if (p->ep->nr == 1) {
  504. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  505. hid_pointer_activate(hs);
  506. }
  507. if (!hid_has_events(hs)) {
  508. p->status = USB_RET_NAK;
  509. return;
  510. }
  511. hid_set_next_idle(hs);
  512. if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
  513. len = hid_pointer_poll(hs, buf, p->iov.size);
  514. } else if (hs->kind == HID_KEYBOARD) {
  515. len = hid_keyboard_poll(hs, buf, p->iov.size);
  516. }
  517. usb_packet_copy(p, buf, len);
  518. } else {
  519. goto fail;
  520. }
  521. break;
  522. case USB_TOKEN_OUT:
  523. default:
  524. fail:
  525. p->status = USB_RET_STALL;
  526. break;
  527. }
  528. }
  529. static void usb_hid_handle_destroy(USBDevice *dev)
  530. {
  531. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  532. hid_free(&us->hid);
  533. }
  534. static int usb_hid_initfn(USBDevice *dev, int kind)
  535. {
  536. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  537. if (dev->serial) {
  538. usb_desc_set_string(dev, STR_SERIALNUMBER, dev->serial);
  539. }
  540. usb_desc_init(dev);
  541. us->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
  542. hid_init(&us->hid, kind, usb_hid_changed);
  543. return 0;
  544. }
  545. static int usb_tablet_initfn(USBDevice *dev)
  546. {
  547. USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
  548. switch (us->usb_version) {
  549. case 1:
  550. dev->usb_desc = &desc_tablet;
  551. break;
  552. case 2:
  553. dev->usb_desc = &desc_tablet2;
  554. break;
  555. default:
  556. error_report("Invalid usb version %d for usb-tabler (must be 1 or 2)",
  557. us->usb_version);
  558. return -1;
  559. }
  560. return usb_hid_initfn(dev, HID_TABLET);
  561. }
  562. static int usb_mouse_initfn(USBDevice *dev)
  563. {
  564. return usb_hid_initfn(dev, HID_MOUSE);
  565. }
  566. static int usb_keyboard_initfn(USBDevice *dev)
  567. {
  568. return usb_hid_initfn(dev, HID_KEYBOARD);
  569. }
  570. static int usb_ptr_post_load(void *opaque, int version_id)
  571. {
  572. USBHIDState *s = opaque;
  573. if (s->dev.remote_wakeup) {
  574. hid_pointer_activate(&s->hid);
  575. }
  576. return 0;
  577. }
  578. static const VMStateDescription vmstate_usb_ptr = {
  579. .name = "usb-ptr",
  580. .version_id = 1,
  581. .minimum_version_id = 1,
  582. .post_load = usb_ptr_post_load,
  583. .fields = (VMStateField []) {
  584. VMSTATE_USB_DEVICE(dev, USBHIDState),
  585. VMSTATE_HID_POINTER_DEVICE(hid, USBHIDState),
  586. VMSTATE_END_OF_LIST()
  587. }
  588. };
  589. static const VMStateDescription vmstate_usb_kbd = {
  590. .name = "usb-kbd",
  591. .version_id = 1,
  592. .minimum_version_id = 1,
  593. .fields = (VMStateField []) {
  594. VMSTATE_USB_DEVICE(dev, USBHIDState),
  595. VMSTATE_HID_KEYBOARD_DEVICE(hid, USBHIDState),
  596. VMSTATE_END_OF_LIST()
  597. }
  598. };
  599. static void usb_hid_class_initfn(ObjectClass *klass, void *data)
  600. {
  601. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  602. uc->handle_reset = usb_hid_handle_reset;
  603. uc->handle_control = usb_hid_handle_control;
  604. uc->handle_data = usb_hid_handle_data;
  605. uc->handle_destroy = usb_hid_handle_destroy;
  606. uc->handle_attach = usb_desc_attach;
  607. }
  608. static Property usb_tablet_properties[] = {
  609. DEFINE_PROP_UINT32("usb_version", USBHIDState, usb_version, 2),
  610. DEFINE_PROP_END_OF_LIST(),
  611. };
  612. static void usb_tablet_class_initfn(ObjectClass *klass, void *data)
  613. {
  614. DeviceClass *dc = DEVICE_CLASS(klass);
  615. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  616. usb_hid_class_initfn(klass, data);
  617. uc->init = usb_tablet_initfn;
  618. uc->product_desc = "QEMU USB Tablet";
  619. dc->vmsd = &vmstate_usb_ptr;
  620. dc->props = usb_tablet_properties;
  621. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  622. }
  623. static const TypeInfo usb_tablet_info = {
  624. .name = "usb-tablet",
  625. .parent = TYPE_USB_DEVICE,
  626. .instance_size = sizeof(USBHIDState),
  627. .class_init = usb_tablet_class_initfn,
  628. };
  629. static void usb_mouse_class_initfn(ObjectClass *klass, void *data)
  630. {
  631. DeviceClass *dc = DEVICE_CLASS(klass);
  632. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  633. usb_hid_class_initfn(klass, data);
  634. uc->init = usb_mouse_initfn;
  635. uc->product_desc = "QEMU USB Mouse";
  636. uc->usb_desc = &desc_mouse;
  637. dc->vmsd = &vmstate_usb_ptr;
  638. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  639. }
  640. static const TypeInfo usb_mouse_info = {
  641. .name = "usb-mouse",
  642. .parent = TYPE_USB_DEVICE,
  643. .instance_size = sizeof(USBHIDState),
  644. .class_init = usb_mouse_class_initfn,
  645. };
  646. static void usb_keyboard_class_initfn(ObjectClass *klass, void *data)
  647. {
  648. DeviceClass *dc = DEVICE_CLASS(klass);
  649. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  650. usb_hid_class_initfn(klass, data);
  651. uc->init = usb_keyboard_initfn;
  652. uc->product_desc = "QEMU USB Keyboard";
  653. uc->usb_desc = &desc_keyboard;
  654. dc->vmsd = &vmstate_usb_kbd;
  655. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  656. }
  657. static const TypeInfo usb_keyboard_info = {
  658. .name = "usb-kbd",
  659. .parent = TYPE_USB_DEVICE,
  660. .instance_size = sizeof(USBHIDState),
  661. .class_init = usb_keyboard_class_initfn,
  662. };
  663. static void usb_hid_register_types(void)
  664. {
  665. type_register_static(&usb_tablet_info);
  666. usb_legacy_register("usb-tablet", "tablet", NULL);
  667. type_register_static(&usb_mouse_info);
  668. usb_legacy_register("usb-mouse", "mouse", NULL);
  669. type_register_static(&usb_keyboard_info);
  670. usb_legacy_register("usb-kbd", "keyboard", NULL);
  671. }
  672. type_init(usb_hid_register_types)