dev-hub.c 20 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. #define MAX_PORTS 8
  35. typedef struct USBHubPort {
  36. USBPort port;
  37. uint16_t wPortStatus;
  38. uint16_t wPortChange;
  39. } USBHubPort;
  40. typedef struct USBHubState {
  41. USBDevice dev;
  42. USBEndpoint *intr;
  43. uint32_t num_ports;
  44. bool port_power;
  45. QEMUTimer *port_timer;
  46. USBHubPort ports[MAX_PORTS];
  47. } USBHubState;
  48. #define TYPE_USB_HUB "usb-hub"
  49. #define USB_HUB(obj) OBJECT_CHECK(USBHubState, (obj), TYPE_USB_HUB)
  50. #define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
  51. #define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
  52. #define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
  53. #define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
  54. #define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
  55. #define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
  56. #define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
  57. #define PORT_STAT_CONNECTION 0x0001
  58. #define PORT_STAT_ENABLE 0x0002
  59. #define PORT_STAT_SUSPEND 0x0004
  60. #define PORT_STAT_OVERCURRENT 0x0008
  61. #define PORT_STAT_RESET 0x0010
  62. #define PORT_STAT_POWER 0x0100
  63. #define PORT_STAT_LOW_SPEED 0x0200
  64. #define PORT_STAT_HIGH_SPEED 0x0400
  65. #define PORT_STAT_TEST 0x0800
  66. #define PORT_STAT_INDICATOR 0x1000
  67. #define PORT_STAT_C_CONNECTION 0x0001
  68. #define PORT_STAT_C_ENABLE 0x0002
  69. #define PORT_STAT_C_SUSPEND 0x0004
  70. #define PORT_STAT_C_OVERCURRENT 0x0008
  71. #define PORT_STAT_C_RESET 0x0010
  72. #define PORT_CONNECTION 0
  73. #define PORT_ENABLE 1
  74. #define PORT_SUSPEND 2
  75. #define PORT_OVERCURRENT 3
  76. #define PORT_RESET 4
  77. #define PORT_POWER 8
  78. #define PORT_LOWSPEED 9
  79. #define PORT_HIGHSPEED 10
  80. #define PORT_C_CONNECTION 16
  81. #define PORT_C_ENABLE 17
  82. #define PORT_C_SUSPEND 18
  83. #define PORT_C_OVERCURRENT 19
  84. #define PORT_C_RESET 20
  85. #define PORT_TEST 21
  86. #define PORT_INDICATOR 22
  87. /* same as Linux kernel root hubs */
  88. enum {
  89. STR_MANUFACTURER = 1,
  90. STR_PRODUCT,
  91. STR_SERIALNUMBER,
  92. };
  93. static const USBDescStrings desc_strings = {
  94. [STR_MANUFACTURER] = "QEMU",
  95. [STR_PRODUCT] = "QEMU USB Hub",
  96. [STR_SERIALNUMBER] = "314159",
  97. };
  98. static const USBDescIface desc_iface_hub = {
  99. .bInterfaceNumber = 0,
  100. .bNumEndpoints = 1,
  101. .bInterfaceClass = USB_CLASS_HUB,
  102. .eps = (USBDescEndpoint[]) {
  103. {
  104. .bEndpointAddress = USB_DIR_IN | 0x01,
  105. .bmAttributes = USB_ENDPOINT_XFER_INT,
  106. .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
  107. .bInterval = 0xff,
  108. },
  109. }
  110. };
  111. static const USBDescDevice desc_device_hub = {
  112. .bcdUSB = 0x0110,
  113. .bDeviceClass = USB_CLASS_HUB,
  114. .bMaxPacketSize0 = 8,
  115. .bNumConfigurations = 1,
  116. .confs = (USBDescConfig[]) {
  117. {
  118. .bNumInterfaces = 1,
  119. .bConfigurationValue = 1,
  120. .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
  121. USB_CFG_ATT_WAKEUP,
  122. .nif = 1,
  123. .ifs = &desc_iface_hub,
  124. },
  125. },
  126. };
  127. static const USBDesc desc_hub = {
  128. .id = {
  129. .idVendor = 0x0409,
  130. .idProduct = 0x55aa,
  131. .bcdDevice = 0x0101,
  132. .iManufacturer = STR_MANUFACTURER,
  133. .iProduct = STR_PRODUCT,
  134. .iSerialNumber = STR_SERIALNUMBER,
  135. },
  136. .full = &desc_device_hub,
  137. .str = desc_strings,
  138. };
  139. static const uint8_t qemu_hub_hub_descriptor[] =
  140. {
  141. 0x00, /* u8 bLength; patched in later */
  142. 0x29, /* u8 bDescriptorType; Hub-descriptor */
  143. 0x00, /* u8 bNbrPorts; (patched later) */
  144. 0x0a, /* u16 wHubCharacteristics; */
  145. 0x00, /* (per-port OC, no power switching) */
  146. 0x01, /* u8 bPwrOn2pwrGood; 2ms */
  147. 0x00 /* u8 bHubContrCurrent; 0 mA */
  148. /* DeviceRemovable and PortPwrCtrlMask patched in later */
  149. };
  150. static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
  151. {
  152. bool notify = false;
  153. if (status & 0x1f) {
  154. port->wPortChange |= status;
  155. notify = true;
  156. }
  157. return notify;
  158. }
  159. static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
  160. {
  161. if (port->wPortStatus & status) {
  162. return false;
  163. }
  164. port->wPortStatus |= status;
  165. return usb_hub_port_change(port, status);
  166. }
  167. static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
  168. {
  169. if (!(port->wPortStatus & status)) {
  170. return false;
  171. }
  172. port->wPortStatus &= ~status;
  173. return usb_hub_port_change(port, status);
  174. }
  175. static bool usb_hub_port_update(USBHubPort *port)
  176. {
  177. bool notify = false;
  178. if (port->port.dev && port->port.dev->attached) {
  179. notify = usb_hub_port_set(port, PORT_STAT_CONNECTION);
  180. if (port->port.dev->speed == USB_SPEED_LOW) {
  181. usb_hub_port_set(port, PORT_STAT_LOW_SPEED);
  182. } else {
  183. usb_hub_port_clear(port, PORT_STAT_LOW_SPEED);
  184. }
  185. }
  186. return notify;
  187. }
  188. static void usb_hub_port_update_timer(void *opaque)
  189. {
  190. USBHubState *s = opaque;
  191. bool notify = false;
  192. int i;
  193. for (i = 0; i < s->num_ports; i++) {
  194. notify |= usb_hub_port_update(&s->ports[i]);
  195. }
  196. if (notify) {
  197. usb_wakeup(s->intr, 0);
  198. }
  199. }
  200. static void usb_hub_attach(USBPort *port1)
  201. {
  202. USBHubState *s = port1->opaque;
  203. USBHubPort *port = &s->ports[port1->index];
  204. trace_usb_hub_attach(s->dev.addr, port1->index + 1);
  205. usb_hub_port_update(port);
  206. usb_wakeup(s->intr, 0);
  207. }
  208. static void usb_hub_detach(USBPort *port1)
  209. {
  210. USBHubState *s = port1->opaque;
  211. USBHubPort *port = &s->ports[port1->index];
  212. trace_usb_hub_detach(s->dev.addr, port1->index + 1);
  213. usb_wakeup(s->intr, 0);
  214. /* Let upstream know the device on this port is gone */
  215. s->dev.port->ops->child_detach(s->dev.port, port1->dev);
  216. usb_hub_port_clear(port, PORT_STAT_CONNECTION);
  217. usb_hub_port_clear(port, PORT_STAT_ENABLE);
  218. usb_hub_port_clear(port, PORT_STAT_SUSPEND);
  219. usb_wakeup(s->intr, 0);
  220. }
  221. static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
  222. {
  223. USBHubState *s = port1->opaque;
  224. /* Pass along upstream */
  225. s->dev.port->ops->child_detach(s->dev.port, child);
  226. }
  227. static void usb_hub_wakeup(USBPort *port1)
  228. {
  229. USBHubState *s = port1->opaque;
  230. USBHubPort *port = &s->ports[port1->index];
  231. if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
  232. usb_wakeup(s->intr, 0);
  233. }
  234. }
  235. static void usb_hub_complete(USBPort *port, USBPacket *packet)
  236. {
  237. USBHubState *s = port->opaque;
  238. /*
  239. * Just pass it along upstream for now.
  240. *
  241. * If we ever implement usb 2.0 split transactions this will
  242. * become a little more complicated ...
  243. *
  244. * Can't use usb_packet_complete() here because packet->owner is
  245. * cleared already, go call the ->complete() callback directly
  246. * instead.
  247. */
  248. s->dev.port->ops->complete(s->dev.port, packet);
  249. }
  250. static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
  251. {
  252. USBHubState *s = USB_HUB(dev);
  253. USBHubPort *port;
  254. USBDevice *downstream;
  255. int i;
  256. for (i = 0; i < s->num_ports; i++) {
  257. port = &s->ports[i];
  258. if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
  259. continue;
  260. }
  261. downstream = usb_find_device(&port->port, addr);
  262. if (downstream != NULL) {
  263. return downstream;
  264. }
  265. }
  266. return NULL;
  267. }
  268. static void usb_hub_handle_reset(USBDevice *dev)
  269. {
  270. USBHubState *s = USB_HUB(dev);
  271. USBHubPort *port;
  272. int i;
  273. trace_usb_hub_reset(s->dev.addr);
  274. for (i = 0; i < s->num_ports; i++) {
  275. port = s->ports + i;
  276. port->wPortStatus = 0;
  277. port->wPortChange = 0;
  278. usb_hub_port_set(port, PORT_STAT_POWER);
  279. usb_hub_port_update(port);
  280. }
  281. }
  282. static const char *feature_name(int feature)
  283. {
  284. static const char *name[] = {
  285. [PORT_CONNECTION] = "connection",
  286. [PORT_ENABLE] = "enable",
  287. [PORT_SUSPEND] = "suspend",
  288. [PORT_OVERCURRENT] = "overcurrent",
  289. [PORT_RESET] = "reset",
  290. [PORT_POWER] = "power",
  291. [PORT_LOWSPEED] = "lowspeed",
  292. [PORT_HIGHSPEED] = "highspeed",
  293. [PORT_C_CONNECTION] = "change-connection",
  294. [PORT_C_ENABLE] = "change-enable",
  295. [PORT_C_SUSPEND] = "change-suspend",
  296. [PORT_C_OVERCURRENT] = "change-overcurrent",
  297. [PORT_C_RESET] = "change-reset",
  298. [PORT_TEST] = "test",
  299. [PORT_INDICATOR] = "indicator",
  300. };
  301. if (feature < 0 || feature >= ARRAY_SIZE(name)) {
  302. return "?";
  303. }
  304. return name[feature] ?: "?";
  305. }
  306. static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
  307. int request, int value, int index, int length, uint8_t *data)
  308. {
  309. USBHubState *s = (USBHubState *)dev;
  310. int ret;
  311. trace_usb_hub_control(s->dev.addr, request, value, index, length);
  312. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  313. if (ret >= 0) {
  314. return;
  315. }
  316. switch(request) {
  317. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  318. if (value == 0 && index != 0x81) { /* clear ep halt */
  319. goto fail;
  320. }
  321. break;
  322. /* usb specific requests */
  323. case GetHubStatus:
  324. data[0] = 0;
  325. data[1] = 0;
  326. data[2] = 0;
  327. data[3] = 0;
  328. p->actual_length = 4;
  329. break;
  330. case GetPortStatus:
  331. {
  332. unsigned int n = index - 1;
  333. USBHubPort *port;
  334. if (n >= s->num_ports) {
  335. goto fail;
  336. }
  337. port = &s->ports[n];
  338. trace_usb_hub_get_port_status(s->dev.addr, index,
  339. port->wPortStatus,
  340. port->wPortChange);
  341. data[0] = port->wPortStatus;
  342. data[1] = port->wPortStatus >> 8;
  343. data[2] = port->wPortChange;
  344. data[3] = port->wPortChange >> 8;
  345. p->actual_length = 4;
  346. }
  347. break;
  348. case SetHubFeature:
  349. case ClearHubFeature:
  350. if (value != 0 && value != 1) {
  351. goto fail;
  352. }
  353. break;
  354. case SetPortFeature:
  355. {
  356. unsigned int n = index - 1;
  357. USBHubPort *port;
  358. USBDevice *dev;
  359. trace_usb_hub_set_port_feature(s->dev.addr, index,
  360. feature_name(value));
  361. if (n >= s->num_ports) {
  362. goto fail;
  363. }
  364. port = &s->ports[n];
  365. dev = port->port.dev;
  366. switch(value) {
  367. case PORT_SUSPEND:
  368. port->wPortStatus |= PORT_STAT_SUSPEND;
  369. break;
  370. case PORT_RESET:
  371. usb_hub_port_set(port, PORT_STAT_RESET);
  372. usb_hub_port_clear(port, PORT_STAT_RESET);
  373. if (dev && dev->attached) {
  374. usb_device_reset(dev);
  375. usb_hub_port_set(port, PORT_STAT_ENABLE);
  376. }
  377. usb_wakeup(s->intr, 0);
  378. break;
  379. case PORT_POWER:
  380. if (s->port_power) {
  381. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  382. usb_hub_port_set(port, PORT_STAT_POWER);
  383. timer_mod(s->port_timer, now + 5000000); /* 5 ms */
  384. }
  385. break;
  386. default:
  387. goto fail;
  388. }
  389. }
  390. break;
  391. case ClearPortFeature:
  392. {
  393. unsigned int n = index - 1;
  394. USBHubPort *port;
  395. trace_usb_hub_clear_port_feature(s->dev.addr, index,
  396. feature_name(value));
  397. if (n >= s->num_ports) {
  398. goto fail;
  399. }
  400. port = &s->ports[n];
  401. switch(value) {
  402. case PORT_ENABLE:
  403. port->wPortStatus &= ~PORT_STAT_ENABLE;
  404. break;
  405. case PORT_C_ENABLE:
  406. port->wPortChange &= ~PORT_STAT_C_ENABLE;
  407. break;
  408. case PORT_SUSPEND:
  409. usb_hub_port_clear(port, PORT_STAT_SUSPEND);
  410. break;
  411. case PORT_C_SUSPEND:
  412. port->wPortChange &= ~PORT_STAT_C_SUSPEND;
  413. break;
  414. case PORT_C_CONNECTION:
  415. port->wPortChange &= ~PORT_STAT_C_CONNECTION;
  416. break;
  417. case PORT_C_OVERCURRENT:
  418. port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
  419. break;
  420. case PORT_C_RESET:
  421. port->wPortChange &= ~PORT_STAT_C_RESET;
  422. break;
  423. case PORT_POWER:
  424. if (s->port_power) {
  425. usb_hub_port_clear(port, PORT_STAT_POWER);
  426. usb_hub_port_clear(port, PORT_STAT_CONNECTION);
  427. usb_hub_port_clear(port, PORT_STAT_ENABLE);
  428. usb_hub_port_clear(port, PORT_STAT_SUSPEND);
  429. port->wPortChange = 0;
  430. }
  431. default:
  432. goto fail;
  433. }
  434. }
  435. break;
  436. case GetHubDescriptor:
  437. {
  438. unsigned int n, limit, var_hub_size = 0;
  439. memcpy(data, qemu_hub_hub_descriptor,
  440. sizeof(qemu_hub_hub_descriptor));
  441. data[2] = s->num_ports;
  442. if (s->port_power) {
  443. data[3] &= ~0x03;
  444. data[3] |= 0x01;
  445. }
  446. /* fill DeviceRemovable bits */
  447. limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
  448. for (n = 7; n < limit; n++) {
  449. data[n] = 0x00;
  450. var_hub_size++;
  451. }
  452. /* fill PortPwrCtrlMask bits */
  453. limit = limit + DIV_ROUND_UP(s->num_ports, 8);
  454. for (;n < limit; n++) {
  455. data[n] = 0xff;
  456. var_hub_size++;
  457. }
  458. p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
  459. data[0] = p->actual_length;
  460. break;
  461. }
  462. default:
  463. fail:
  464. p->status = USB_RET_STALL;
  465. break;
  466. }
  467. }
  468. static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
  469. {
  470. USBHubState *s = (USBHubState *)dev;
  471. switch(p->pid) {
  472. case USB_TOKEN_IN:
  473. if (p->ep->nr == 1) {
  474. USBHubPort *port;
  475. unsigned int status;
  476. uint8_t buf[4];
  477. int i, n;
  478. n = DIV_ROUND_UP(s->num_ports + 1, 8);
  479. if (p->iov.size == 1) { /* FreeBSD workaround */
  480. n = 1;
  481. } else if (n > p->iov.size) {
  482. p->status = USB_RET_BABBLE;
  483. return;
  484. }
  485. status = 0;
  486. for (i = 0; i < s->num_ports; i++) {
  487. port = &s->ports[i];
  488. if (port->wPortChange)
  489. status |= (1 << (i + 1));
  490. }
  491. if (status != 0) {
  492. trace_usb_hub_status_report(s->dev.addr, status);
  493. for(i = 0; i < n; i++) {
  494. buf[i] = status >> (8 * i);
  495. }
  496. usb_packet_copy(p, buf, n);
  497. } else {
  498. p->status = USB_RET_NAK; /* usb11 11.13.1 */
  499. }
  500. } else {
  501. goto fail;
  502. }
  503. break;
  504. case USB_TOKEN_OUT:
  505. default:
  506. fail:
  507. p->status = USB_RET_STALL;
  508. break;
  509. }
  510. }
  511. static void usb_hub_unrealize(USBDevice *dev)
  512. {
  513. USBHubState *s = (USBHubState *)dev;
  514. int i;
  515. for (i = 0; i < s->num_ports; i++) {
  516. usb_unregister_port(usb_bus_from_device(dev),
  517. &s->ports[i].port);
  518. }
  519. timer_del(s->port_timer);
  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)