2
0

dev-hub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * QEMU USB HUB emulation
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "qemu/timer.h"
  27. #include "trace.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/usb.h"
  30. #include "migration/vmstate.h"
  31. #include "desc.h"
  32. #include "qemu/error-report.h"
  33. #include "qemu/module.h"
  34. #include "qom/object.h"
  35. #define MAX_PORTS 8
  36. typedef struct USBHubPort {
  37. USBPort port;
  38. uint16_t wPortStatus;
  39. uint16_t wPortChange;
  40. } USBHubPort;
  41. struct USBHubState {
  42. USBDevice dev;
  43. USBEndpoint *intr;
  44. uint32_t num_ports;
  45. bool port_power;
  46. QEMUTimer *port_timer;
  47. USBHubPort ports[MAX_PORTS];
  48. };
  49. #define TYPE_USB_HUB "usb-hub"
  50. OBJECT_DECLARE_SIMPLE_TYPE(USBHubState, USB_HUB)
  51. #define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
  52. #define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
  53. #define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
  54. #define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
  55. #define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
  56. #define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
  57. #define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
  58. #define PORT_STAT_CONNECTION 0x0001
  59. #define PORT_STAT_ENABLE 0x0002
  60. #define PORT_STAT_SUSPEND 0x0004
  61. #define PORT_STAT_OVERCURRENT 0x0008
  62. #define PORT_STAT_RESET 0x0010
  63. #define PORT_STAT_POWER 0x0100
  64. #define PORT_STAT_LOW_SPEED 0x0200
  65. #define PORT_STAT_HIGH_SPEED 0x0400
  66. #define PORT_STAT_TEST 0x0800
  67. #define PORT_STAT_INDICATOR 0x1000
  68. #define PORT_STAT_C_CONNECTION 0x0001
  69. #define PORT_STAT_C_ENABLE 0x0002
  70. #define PORT_STAT_C_SUSPEND 0x0004
  71. #define PORT_STAT_C_OVERCURRENT 0x0008
  72. #define PORT_STAT_C_RESET 0x0010
  73. #define PORT_CONNECTION 0
  74. #define PORT_ENABLE 1
  75. #define PORT_SUSPEND 2
  76. #define PORT_OVERCURRENT 3
  77. #define PORT_RESET 4
  78. #define PORT_POWER 8
  79. #define PORT_LOWSPEED 9
  80. #define PORT_HIGHSPEED 10
  81. #define PORT_C_CONNECTION 16
  82. #define PORT_C_ENABLE 17
  83. #define PORT_C_SUSPEND 18
  84. #define PORT_C_OVERCURRENT 19
  85. #define PORT_C_RESET 20
  86. #define PORT_TEST 21
  87. #define PORT_INDICATOR 22
  88. /* same as Linux kernel root hubs */
  89. enum {
  90. STR_MANUFACTURER = 1,
  91. STR_PRODUCT,
  92. STR_SERIALNUMBER,
  93. };
  94. static const USBDescStrings desc_strings = {
  95. [STR_MANUFACTURER] = "QEMU",
  96. [STR_PRODUCT] = "QEMU USB Hub",
  97. [STR_SERIALNUMBER] = "314159",
  98. };
  99. static const USBDescIface desc_iface_hub = {
  100. .bInterfaceNumber = 0,
  101. .bNumEndpoints = 1,
  102. .bInterfaceClass = USB_CLASS_HUB,
  103. .eps = (USBDescEndpoint[]) {
  104. {
  105. .bEndpointAddress = USB_DIR_IN | 0x01,
  106. .bmAttributes = USB_ENDPOINT_XFER_INT,
  107. .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
  108. .bInterval = 0xff,
  109. },
  110. }
  111. };
  112. static const USBDescDevice desc_device_hub = {
  113. .bcdUSB = 0x0110,
  114. .bDeviceClass = USB_CLASS_HUB,
  115. .bMaxPacketSize0 = 8,
  116. .bNumConfigurations = 1,
  117. .confs = (USBDescConfig[]) {
  118. {
  119. .bNumInterfaces = 1,
  120. .bConfigurationValue = 1,
  121. .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
  122. USB_CFG_ATT_WAKEUP,
  123. .nif = 1,
  124. .ifs = &desc_iface_hub,
  125. },
  126. },
  127. };
  128. static const USBDesc desc_hub = {
  129. .id = {
  130. .idVendor = 0x0409,
  131. .idProduct = 0x55aa,
  132. .bcdDevice = 0x0101,
  133. .iManufacturer = STR_MANUFACTURER,
  134. .iProduct = STR_PRODUCT,
  135. .iSerialNumber = STR_SERIALNUMBER,
  136. },
  137. .full = &desc_device_hub,
  138. .str = desc_strings,
  139. };
  140. static const uint8_t qemu_hub_hub_descriptor[] =
  141. {
  142. 0x00, /* u8 bLength; patched in later */
  143. 0x29, /* u8 bDescriptorType; Hub-descriptor */
  144. 0x00, /* u8 bNbrPorts; (patched later) */
  145. 0x0a, /* u16 wHubCharacteristics; */
  146. 0x00, /* (per-port OC, no power switching) */
  147. 0x01, /* u8 bPwrOn2pwrGood; 2ms */
  148. 0x00 /* u8 bHubContrCurrent; 0 mA */
  149. /* DeviceRemovable and PortPwrCtrlMask patched in later */
  150. };
  151. static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
  152. {
  153. bool notify = false;
  154. if (status & 0x1f) {
  155. port->wPortChange |= status;
  156. notify = true;
  157. }
  158. return notify;
  159. }
  160. static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
  161. {
  162. if (port->wPortStatus & status) {
  163. return false;
  164. }
  165. port->wPortStatus |= status;
  166. return usb_hub_port_change(port, status);
  167. }
  168. static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
  169. {
  170. if (!(port->wPortStatus & status)) {
  171. return false;
  172. }
  173. port->wPortStatus &= ~status;
  174. return usb_hub_port_change(port, status);
  175. }
  176. static bool usb_hub_port_update(USBHubPort *port)
  177. {
  178. bool notify = false;
  179. if (port->port.dev && port->port.dev->attached) {
  180. notify = usb_hub_port_set(port, PORT_STAT_CONNECTION);
  181. if (port->port.dev->speed == USB_SPEED_LOW) {
  182. usb_hub_port_set(port, PORT_STAT_LOW_SPEED);
  183. } else {
  184. usb_hub_port_clear(port, PORT_STAT_LOW_SPEED);
  185. }
  186. }
  187. return notify;
  188. }
  189. static void usb_hub_port_update_timer(void *opaque)
  190. {
  191. USBHubState *s = opaque;
  192. bool notify = false;
  193. int i;
  194. for (i = 0; i < s->num_ports; i++) {
  195. notify |= usb_hub_port_update(&s->ports[i]);
  196. }
  197. if (notify) {
  198. usb_wakeup(s->intr, 0);
  199. }
  200. }
  201. static void usb_hub_attach(USBPort *port1)
  202. {
  203. USBHubState *s = port1->opaque;
  204. USBHubPort *port = &s->ports[port1->index];
  205. trace_usb_hub_attach(s->dev.addr, port1->index + 1);
  206. usb_hub_port_update(port);
  207. usb_wakeup(s->intr, 0);
  208. }
  209. static void usb_hub_detach(USBPort *port1)
  210. {
  211. USBHubState *s = port1->opaque;
  212. USBHubPort *port = &s->ports[port1->index];
  213. trace_usb_hub_detach(s->dev.addr, port1->index + 1);
  214. usb_wakeup(s->intr, 0);
  215. /* Let upstream know the device on this port is gone */
  216. s->dev.port->ops->child_detach(s->dev.port, port1->dev);
  217. usb_hub_port_clear(port, PORT_STAT_CONNECTION);
  218. usb_hub_port_clear(port, PORT_STAT_ENABLE);
  219. usb_hub_port_clear(port, PORT_STAT_SUSPEND);
  220. usb_wakeup(s->intr, 0);
  221. }
  222. static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
  223. {
  224. USBHubState *s = port1->opaque;
  225. /* Pass along upstream */
  226. s->dev.port->ops->child_detach(s->dev.port, child);
  227. }
  228. static void usb_hub_wakeup(USBPort *port1)
  229. {
  230. USBHubState *s = port1->opaque;
  231. USBHubPort *port = &s->ports[port1->index];
  232. if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
  233. usb_wakeup(s->intr, 0);
  234. }
  235. }
  236. static void usb_hub_complete(USBPort *port, USBPacket *packet)
  237. {
  238. USBHubState *s = port->opaque;
  239. /*
  240. * Just pass it along upstream for now.
  241. *
  242. * If we ever implement usb 2.0 split transactions this will
  243. * become a little more complicated ...
  244. *
  245. * Can't use usb_packet_complete() here because packet->owner is
  246. * cleared already, go call the ->complete() callback directly
  247. * instead.
  248. */
  249. s->dev.port->ops->complete(s->dev.port, packet);
  250. }
  251. static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
  252. {
  253. USBHubState *s = USB_HUB(dev);
  254. USBHubPort *port;
  255. USBDevice *downstream;
  256. int i;
  257. for (i = 0; i < s->num_ports; i++) {
  258. port = &s->ports[i];
  259. if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
  260. continue;
  261. }
  262. downstream = usb_find_device(&port->port, addr);
  263. if (downstream != NULL) {
  264. return downstream;
  265. }
  266. }
  267. return NULL;
  268. }
  269. static void usb_hub_handle_reset(USBDevice *dev)
  270. {
  271. USBHubState *s = USB_HUB(dev);
  272. USBHubPort *port;
  273. int i;
  274. trace_usb_hub_reset(s->dev.addr);
  275. for (i = 0; i < s->num_ports; i++) {
  276. port = s->ports + i;
  277. port->wPortStatus = 0;
  278. port->wPortChange = 0;
  279. usb_hub_port_set(port, PORT_STAT_POWER);
  280. usb_hub_port_update(port);
  281. }
  282. }
  283. static const char *feature_name(int feature)
  284. {
  285. static const char *name[] = {
  286. [PORT_CONNECTION] = "connection",
  287. [PORT_ENABLE] = "enable",
  288. [PORT_SUSPEND] = "suspend",
  289. [PORT_OVERCURRENT] = "overcurrent",
  290. [PORT_RESET] = "reset",
  291. [PORT_POWER] = "power",
  292. [PORT_LOWSPEED] = "lowspeed",
  293. [PORT_HIGHSPEED] = "highspeed",
  294. [PORT_C_CONNECTION] = "change-connection",
  295. [PORT_C_ENABLE] = "change-enable",
  296. [PORT_C_SUSPEND] = "change-suspend",
  297. [PORT_C_OVERCURRENT] = "change-overcurrent",
  298. [PORT_C_RESET] = "change-reset",
  299. [PORT_TEST] = "test",
  300. [PORT_INDICATOR] = "indicator",
  301. };
  302. if (feature < 0 || feature >= ARRAY_SIZE(name)) {
  303. return "?";
  304. }
  305. return name[feature] ?: "?";
  306. }
  307. static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
  308. int request, int value, int index, int length, uint8_t *data)
  309. {
  310. USBHubState *s = (USBHubState *)dev;
  311. int ret;
  312. trace_usb_hub_control(s->dev.addr, request, value, index, length);
  313. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  314. if (ret >= 0) {
  315. return;
  316. }
  317. switch(request) {
  318. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  319. if (value == 0 && index != 0x81) { /* clear ep halt */
  320. goto fail;
  321. }
  322. break;
  323. /* usb specific requests */
  324. case GetHubStatus:
  325. data[0] = 0;
  326. data[1] = 0;
  327. data[2] = 0;
  328. data[3] = 0;
  329. p->actual_length = 4;
  330. break;
  331. case GetPortStatus:
  332. {
  333. unsigned int n = index - 1;
  334. USBHubPort *port;
  335. if (n >= s->num_ports) {
  336. goto fail;
  337. }
  338. port = &s->ports[n];
  339. trace_usb_hub_get_port_status(s->dev.addr, index,
  340. port->wPortStatus,
  341. port->wPortChange);
  342. data[0] = port->wPortStatus;
  343. data[1] = port->wPortStatus >> 8;
  344. data[2] = port->wPortChange;
  345. data[3] = port->wPortChange >> 8;
  346. p->actual_length = 4;
  347. }
  348. break;
  349. case SetHubFeature:
  350. case ClearHubFeature:
  351. if (value != 0 && value != 1) {
  352. goto fail;
  353. }
  354. break;
  355. case SetPortFeature:
  356. {
  357. unsigned int n = index - 1;
  358. USBHubPort *port;
  359. USBDevice *dev;
  360. trace_usb_hub_set_port_feature(s->dev.addr, index,
  361. feature_name(value));
  362. if (n >= s->num_ports) {
  363. goto fail;
  364. }
  365. port = &s->ports[n];
  366. dev = port->port.dev;
  367. switch(value) {
  368. case PORT_SUSPEND:
  369. port->wPortStatus |= PORT_STAT_SUSPEND;
  370. break;
  371. case PORT_RESET:
  372. usb_hub_port_set(port, PORT_STAT_RESET);
  373. usb_hub_port_clear(port, PORT_STAT_RESET);
  374. if (dev && dev->attached) {
  375. usb_device_reset(dev);
  376. usb_hub_port_set(port, PORT_STAT_ENABLE);
  377. }
  378. usb_wakeup(s->intr, 0);
  379. break;
  380. case PORT_POWER:
  381. if (s->port_power) {
  382. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  383. usb_hub_port_set(port, PORT_STAT_POWER);
  384. timer_mod(s->port_timer, now + 5000000); /* 5 ms */
  385. }
  386. break;
  387. default:
  388. goto fail;
  389. }
  390. }
  391. break;
  392. case ClearPortFeature:
  393. {
  394. unsigned int n = index - 1;
  395. USBHubPort *port;
  396. trace_usb_hub_clear_port_feature(s->dev.addr, index,
  397. feature_name(value));
  398. if (n >= s->num_ports) {
  399. goto fail;
  400. }
  401. port = &s->ports[n];
  402. switch(value) {
  403. case PORT_ENABLE:
  404. port->wPortStatus &= ~PORT_STAT_ENABLE;
  405. break;
  406. case PORT_C_ENABLE:
  407. port->wPortChange &= ~PORT_STAT_C_ENABLE;
  408. break;
  409. case PORT_SUSPEND:
  410. usb_hub_port_clear(port, PORT_STAT_SUSPEND);
  411. break;
  412. case PORT_C_SUSPEND:
  413. port->wPortChange &= ~PORT_STAT_C_SUSPEND;
  414. break;
  415. case PORT_C_CONNECTION:
  416. port->wPortChange &= ~PORT_STAT_C_CONNECTION;
  417. break;
  418. case PORT_C_OVERCURRENT:
  419. port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
  420. break;
  421. case PORT_C_RESET:
  422. port->wPortChange &= ~PORT_STAT_C_RESET;
  423. break;
  424. case PORT_POWER:
  425. if (s->port_power) {
  426. usb_hub_port_clear(port, PORT_STAT_POWER);
  427. usb_hub_port_clear(port, PORT_STAT_CONNECTION);
  428. usb_hub_port_clear(port, PORT_STAT_ENABLE);
  429. usb_hub_port_clear(port, PORT_STAT_SUSPEND);
  430. port->wPortChange = 0;
  431. }
  432. default:
  433. goto fail;
  434. }
  435. }
  436. break;
  437. case GetHubDescriptor:
  438. {
  439. unsigned int n, limit, var_hub_size = 0;
  440. memcpy(data, qemu_hub_hub_descriptor,
  441. sizeof(qemu_hub_hub_descriptor));
  442. data[2] = s->num_ports;
  443. if (s->port_power) {
  444. data[3] &= ~0x03;
  445. data[3] |= 0x01;
  446. }
  447. /* fill DeviceRemovable bits */
  448. limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
  449. for (n = 7; n < limit; n++) {
  450. data[n] = 0x00;
  451. var_hub_size++;
  452. }
  453. /* fill PortPwrCtrlMask bits */
  454. limit = limit + DIV_ROUND_UP(s->num_ports, 8);
  455. for (;n < limit; n++) {
  456. data[n] = 0xff;
  457. var_hub_size++;
  458. }
  459. p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
  460. data[0] = p->actual_length;
  461. break;
  462. }
  463. default:
  464. fail:
  465. p->status = USB_RET_STALL;
  466. break;
  467. }
  468. }
  469. static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
  470. {
  471. USBHubState *s = (USBHubState *)dev;
  472. switch(p->pid) {
  473. case USB_TOKEN_IN:
  474. if (p->ep->nr == 1) {
  475. USBHubPort *port;
  476. unsigned int status;
  477. uint8_t buf[4];
  478. int i, n;
  479. n = DIV_ROUND_UP(s->num_ports + 1, 8);
  480. if (p->iov.size == 1) { /* FreeBSD workaround */
  481. n = 1;
  482. } else if (n > p->iov.size) {
  483. p->status = USB_RET_BABBLE;
  484. return;
  485. }
  486. status = 0;
  487. for (i = 0; i < s->num_ports; i++) {
  488. port = &s->ports[i];
  489. if (port->wPortChange)
  490. status |= (1 << (i + 1));
  491. }
  492. if (status != 0) {
  493. trace_usb_hub_status_report(s->dev.addr, status);
  494. for(i = 0; i < n; i++) {
  495. buf[i] = status >> (8 * i);
  496. }
  497. usb_packet_copy(p, buf, n);
  498. } else {
  499. p->status = USB_RET_NAK; /* usb11 11.13.1 */
  500. }
  501. } else {
  502. goto fail;
  503. }
  504. break;
  505. case USB_TOKEN_OUT:
  506. default:
  507. fail:
  508. p->status = USB_RET_STALL;
  509. break;
  510. }
  511. }
  512. static void usb_hub_unrealize(USBDevice *dev)
  513. {
  514. USBHubState *s = (USBHubState *)dev;
  515. int i;
  516. for (i = 0; i < s->num_ports; i++) {
  517. usb_unregister_port(usb_bus_from_device(dev),
  518. &s->ports[i].port);
  519. }
  520. timer_free(s->port_timer);
  521. }
  522. static USBPortOps usb_hub_port_ops = {
  523. .attach = usb_hub_attach,
  524. .detach = usb_hub_detach,
  525. .child_detach = usb_hub_child_detach,
  526. .wakeup = usb_hub_wakeup,
  527. .complete = usb_hub_complete,
  528. };
  529. static void usb_hub_realize(USBDevice *dev, Error **errp)
  530. {
  531. USBHubState *s = USB_HUB(dev);
  532. USBHubPort *port;
  533. int i;
  534. if (s->num_ports < 1 || s->num_ports > MAX_PORTS) {
  535. error_setg(errp, "num_ports (%d) out of range (1..%d)",
  536. s->num_ports, MAX_PORTS);
  537. return;
  538. }
  539. if (dev->port->hubcount == 5) {
  540. error_setg(errp, "usb hub chain too deep");
  541. return;
  542. }
  543. usb_desc_create_serial(dev);
  544. usb_desc_init(dev);
  545. s->port_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  546. usb_hub_port_update_timer, s);
  547. s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
  548. for (i = 0; i < s->num_ports; i++) {
  549. port = &s->ports[i];
  550. usb_register_port(usb_bus_from_device(dev),
  551. &port->port, s, i, &usb_hub_port_ops,
  552. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  553. usb_port_location(&port->port, dev->port, i+1);
  554. }
  555. usb_hub_handle_reset(dev);
  556. }
  557. static const VMStateDescription vmstate_usb_hub_port = {
  558. .name = "usb-hub-port",
  559. .version_id = 1,
  560. .minimum_version_id = 1,
  561. .fields = (VMStateField[]) {
  562. VMSTATE_UINT16(wPortStatus, USBHubPort),
  563. VMSTATE_UINT16(wPortChange, USBHubPort),
  564. VMSTATE_END_OF_LIST()
  565. }
  566. };
  567. static bool usb_hub_port_timer_needed(void *opaque)
  568. {
  569. USBHubState *s = opaque;
  570. return s->port_power;
  571. }
  572. static const VMStateDescription vmstate_usb_hub_port_timer = {
  573. .name = "usb-hub/port-timer",
  574. .version_id = 1,
  575. .minimum_version_id = 1,
  576. .needed = usb_hub_port_timer_needed,
  577. .fields = (VMStateField[]) {
  578. VMSTATE_TIMER_PTR(port_timer, USBHubState),
  579. VMSTATE_END_OF_LIST()
  580. },
  581. };
  582. static const VMStateDescription vmstate_usb_hub = {
  583. .name = "usb-hub",
  584. .version_id = 1,
  585. .minimum_version_id = 1,
  586. .fields = (VMStateField[]) {
  587. VMSTATE_USB_DEVICE(dev, USBHubState),
  588. VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0,
  589. vmstate_usb_hub_port, USBHubPort),
  590. VMSTATE_END_OF_LIST()
  591. },
  592. .subsections = (const VMStateDescription * []) {
  593. &vmstate_usb_hub_port_timer,
  594. NULL
  595. }
  596. };
  597. static Property usb_hub_properties[] = {
  598. DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8),
  599. DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false),
  600. DEFINE_PROP_END_OF_LIST(),
  601. };
  602. static void usb_hub_class_initfn(ObjectClass *klass, void *data)
  603. {
  604. DeviceClass *dc = DEVICE_CLASS(klass);
  605. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  606. uc->realize = usb_hub_realize;
  607. uc->product_desc = "QEMU USB Hub";
  608. uc->usb_desc = &desc_hub;
  609. uc->find_device = usb_hub_find_device;
  610. uc->handle_reset = usb_hub_handle_reset;
  611. uc->handle_control = usb_hub_handle_control;
  612. uc->handle_data = usb_hub_handle_data;
  613. uc->unrealize = usb_hub_unrealize;
  614. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  615. dc->fw_name = "hub";
  616. dc->vmsd = &vmstate_usb_hub;
  617. device_class_set_props(dc, usb_hub_properties);
  618. }
  619. static const TypeInfo hub_info = {
  620. .name = TYPE_USB_HUB,
  621. .parent = TYPE_USB_DEVICE,
  622. .instance_size = sizeof(USBHubState),
  623. .class_init = usb_hub_class_initfn,
  624. };
  625. static void usb_hub_register_types(void)
  626. {
  627. type_register_static(&hub_info);
  628. }
  629. type_init(usb_hub_register_types)