]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/usb/dev-hub.c
Move QOM typedefs and add missing includes
[thirdparty/qemu.git] / hw / usb / dev-hub.c
CommitLineData
72899afc
FB
1/*
2 * QEMU USB HUB emulation
3 *
4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
72899afc
FB
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 */
0b8fa32f 24
e532b2e0 25#include "qemu/osdep.h"
da34e65c 26#include "qapi/error.h"
1cc403eb 27#include "qemu/timer.h"
529f8f9f 28#include "trace.h"
a27bd6c7 29#include "hw/qdev-properties.h"
f1ae32a1 30#include "hw/usb.h"
d6454270 31#include "migration/vmstate.h"
463581a8 32#include "desc.h"
c24e4aac 33#include "qemu/error-report.h"
0b8fa32f 34#include "qemu/module.h"
db1015e9 35#include "qom/object.h"
72899afc 36
9d84bb00 37#define MAX_PORTS 8
72899afc
FB
38
39typedef struct USBHubPort {
40 USBPort port;
41 uint16_t wPortStatus;
42 uint16_t wPortChange;
43} USBHubPort;
44
db1015e9 45struct USBHubState {
72899afc 46 USBDevice dev;
7567b51f 47 USBEndpoint *intr;
9d84bb00 48 uint32_t num_ports;
1cc403eb
GH
49 bool port_power;
50 QEMUTimer *port_timer;
9d84bb00 51 USBHubPort ports[MAX_PORTS];
db1015e9
EH
52};
53typedef struct USBHubState USBHubState;
72899afc 54
e81b13ad
GA
55#define TYPE_USB_HUB "usb-hub"
56#define USB_HUB(obj) OBJECT_CHECK(USBHubState, (obj), TYPE_USB_HUB)
57
72899afc
FB
58#define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
59#define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
60#define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
61#define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
62#define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
63#define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
64#define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
65
66#define PORT_STAT_CONNECTION 0x0001
67#define PORT_STAT_ENABLE 0x0002
68#define PORT_STAT_SUSPEND 0x0004
69#define PORT_STAT_OVERCURRENT 0x0008
70#define PORT_STAT_RESET 0x0010
71#define PORT_STAT_POWER 0x0100
72#define PORT_STAT_LOW_SPEED 0x0200
73#define PORT_STAT_HIGH_SPEED 0x0400
74#define PORT_STAT_TEST 0x0800
75#define PORT_STAT_INDICATOR 0x1000
76
77#define PORT_STAT_C_CONNECTION 0x0001
78#define PORT_STAT_C_ENABLE 0x0002
79#define PORT_STAT_C_SUSPEND 0x0004
80#define PORT_STAT_C_OVERCURRENT 0x0008
81#define PORT_STAT_C_RESET 0x0010
82
83#define PORT_CONNECTION 0
84#define PORT_ENABLE 1
85#define PORT_SUSPEND 2
86#define PORT_OVERCURRENT 3
87#define PORT_RESET 4
88#define PORT_POWER 8
89#define PORT_LOWSPEED 9
90#define PORT_HIGHSPEED 10
91#define PORT_C_CONNECTION 16
92#define PORT_C_ENABLE 17
93#define PORT_C_SUSPEND 18
94#define PORT_C_OVERCURRENT 19
95#define PORT_C_RESET 20
96#define PORT_TEST 21
97#define PORT_INDICATOR 22
98
99/* same as Linux kernel root hubs */
100
062651c7
GH
101enum {
102 STR_MANUFACTURER = 1,
103 STR_PRODUCT,
104 STR_SERIALNUMBER,
105};
106
107static const USBDescStrings desc_strings = {
93bfef4c 108 [STR_MANUFACTURER] = "QEMU",
062651c7
GH
109 [STR_PRODUCT] = "QEMU USB Hub",
110 [STR_SERIALNUMBER] = "314159",
111};
112
113static const USBDescIface desc_iface_hub = {
114 .bInterfaceNumber = 0,
115 .bNumEndpoints = 1,
116 .bInterfaceClass = USB_CLASS_HUB,
117 .eps = (USBDescEndpoint[]) {
118 {
119 .bEndpointAddress = USB_DIR_IN | 0x01,
120 .bmAttributes = USB_ENDPOINT_XFER_INT,
9d84bb00 121 .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
062651c7
GH
122 .bInterval = 0xff,
123 },
124 }
125};
126
127static const USBDescDevice desc_device_hub = {
128 .bcdUSB = 0x0110,
129 .bDeviceClass = USB_CLASS_HUB,
130 .bMaxPacketSize0 = 8,
131 .bNumConfigurations = 1,
132 .confs = (USBDescConfig[]) {
133 {
134 .bNumInterfaces = 1,
135 .bConfigurationValue = 1,
bd93976a
PK
136 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
137 USB_CFG_ATT_WAKEUP,
add75088 138 .nif = 1,
062651c7
GH
139 .ifs = &desc_iface_hub,
140 },
141 },
142};
143
144static const USBDesc desc_hub = {
145 .id = {
db80358a
RT
146 .idVendor = 0x0409,
147 .idProduct = 0x55aa,
062651c7
GH
148 .bcdDevice = 0x0101,
149 .iManufacturer = STR_MANUFACTURER,
150 .iProduct = STR_PRODUCT,
151 .iSerialNumber = STR_SERIALNUMBER,
152 },
153 .full = &desc_device_hub,
154 .str = desc_strings,
155};
156
72899afc
FB
157static const uint8_t qemu_hub_hub_descriptor[] =
158{
7d37435b
PB
159 0x00, /* u8 bLength; patched in later */
160 0x29, /* u8 bDescriptorType; Hub-descriptor */
161 0x00, /* u8 bNbrPorts; (patched later) */
162 0x0a, /* u16 wHubCharacteristics; */
163 0x00, /* (per-port OC, no power switching) */
164 0x01, /* u8 bPwrOn2pwrGood; 2ms */
165 0x00 /* u8 bHubContrCurrent; 0 mA */
985d1742
FB
166
167 /* DeviceRemovable and PortPwrCtrlMask patched in later */
72899afc
FB
168};
169
868a4203
GH
170static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
171{
172 bool notify = false;
173
174 if (status & 0x1f) {
175 port->wPortChange |= status;
176 notify = true;
177 }
178 return notify;
179}
180
181static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
182{
183 if (port->wPortStatus & status) {
184 return false;
185 }
186 port->wPortStatus |= status;
187 return usb_hub_port_change(port, status);
188}
189
190static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
191{
192 if (!(port->wPortStatus & status)) {
193 return false;
194 }
195 port->wPortStatus &= ~status;
196 return usb_hub_port_change(port, status);
197}
198
638ac2d8
GH
199static bool usb_hub_port_update(USBHubPort *port)
200{
201 bool notify = false;
202
203 if (port->port.dev && port->port.dev->attached) {
204 notify = usb_hub_port_set(port, PORT_STAT_CONNECTION);
205 if (port->port.dev->speed == USB_SPEED_LOW) {
206 usb_hub_port_set(port, PORT_STAT_LOW_SPEED);
207 } else {
208 usb_hub_port_clear(port, PORT_STAT_LOW_SPEED);
209 }
210 }
211 return notify;
212}
213
1cc403eb
GH
214static void usb_hub_port_update_timer(void *opaque)
215{
216 USBHubState *s = opaque;
217 bool notify = false;
218 int i;
219
220 for (i = 0; i < s->num_ports; i++) {
221 notify |= usb_hub_port_update(&s->ports[i]);
222 }
223 if (notify) {
224 usb_wakeup(s->intr, 0);
225 }
226}
227
618c169b 228static void usb_hub_attach(USBPort *port1)
72899afc
FB
229{
230 USBHubState *s = port1->opaque;
231 USBHubPort *port = &s->ports[port1->index];
3b46e624 232
529f8f9f 233 trace_usb_hub_attach(s->dev.addr, port1->index + 1);
638ac2d8 234 usb_hub_port_update(port);
8550a02d 235 usb_wakeup(s->intr, 0);
618c169b
GH
236}
237
238static void usb_hub_detach(USBPort *port1)
239{
240 USBHubState *s = port1->opaque;
241 USBHubPort *port = &s->ports[port1->index];
242
529f8f9f 243 trace_usb_hub_detach(s->dev.addr, port1->index + 1);
8550a02d 244 usb_wakeup(s->intr, 0);
be35cbbc 245
4706ab6c
HG
246 /* Let upstream know the device on this port is gone */
247 s->dev.port->ops->child_detach(s->dev.port, port1->dev);
248
868a4203
GH
249 usb_hub_port_clear(port, PORT_STAT_CONNECTION);
250 usb_hub_port_clear(port, PORT_STAT_ENABLE);
251 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
8550a02d 252 usb_wakeup(s->intr, 0);
72899afc
FB
253}
254
4706ab6c
HG
255static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
256{
257 USBHubState *s = port1->opaque;
258
259 /* Pass along upstream */
260 s->dev.port->ops->child_detach(s->dev.port, child);
261}
262
d47e59b8 263static void usb_hub_wakeup(USBPort *port1)
34239c7b 264{
d47e59b8
HG
265 USBHubState *s = port1->opaque;
266 USBHubPort *port = &s->ports[port1->index];
34239c7b 267
868a4203 268 if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
8550a02d 269 usb_wakeup(s->intr, 0);
34239c7b
GH
270 }
271}
272
d47e59b8 273static void usb_hub_complete(USBPort *port, USBPacket *packet)
13a9a0d3 274{
d47e59b8 275 USBHubState *s = port->opaque;
13a9a0d3
GH
276
277 /*
278 * Just pass it along upstream for now.
279 *
80cf7cf7 280 * If we ever implement usb 2.0 split transactions this will
13a9a0d3 281 * become a little more complicated ...
80cf7cf7
GH
282 *
283 * Can't use usb_packet_complete() here because packet->owner is
284 * cleared already, go call the ->complete() callback directly
285 * instead.
13a9a0d3 286 */
80cf7cf7 287 s->dev.port->ops->complete(s->dev.port, packet);
13a9a0d3
GH
288}
289
06c75088
GH
290static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
291{
e81b13ad 292 USBHubState *s = USB_HUB(dev);
06c75088
GH
293 USBHubPort *port;
294 USBDevice *downstream;
295 int i;
296
9d84bb00 297 for (i = 0; i < s->num_ports; i++) {
06c75088
GH
298 port = &s->ports[i];
299 if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
300 continue;
301 }
302 downstream = usb_find_device(&port->port, addr);
303 if (downstream != NULL) {
304 return downstream;
305 }
306 }
307 return NULL;
308}
309
059809e4 310static void usb_hub_handle_reset(USBDevice *dev)
72899afc 311{
e81b13ad 312 USBHubState *s = USB_HUB(dev);
20d183b6
GH
313 USBHubPort *port;
314 int i;
315
529f8f9f 316 trace_usb_hub_reset(s->dev.addr);
9d84bb00 317 for (i = 0; i < s->num_ports; i++) {
20d183b6 318 port = s->ports + i;
868a4203 319 port->wPortStatus = 0;
20d183b6 320 port->wPortChange = 0;
868a4203 321 usb_hub_port_set(port, PORT_STAT_POWER);
638ac2d8 322 usb_hub_port_update(port);
20d183b6 323 }
72899afc
FB
324}
325
529f8f9f
GH
326static const char *feature_name(int feature)
327{
328 static const char *name[] = {
329 [PORT_CONNECTION] = "connection",
330 [PORT_ENABLE] = "enable",
331 [PORT_SUSPEND] = "suspend",
332 [PORT_OVERCURRENT] = "overcurrent",
333 [PORT_RESET] = "reset",
334 [PORT_POWER] = "power",
335 [PORT_LOWSPEED] = "lowspeed",
336 [PORT_HIGHSPEED] = "highspeed",
bdb88a8e
GH
337 [PORT_C_CONNECTION] = "change-connection",
338 [PORT_C_ENABLE] = "change-enable",
339 [PORT_C_SUSPEND] = "change-suspend",
340 [PORT_C_OVERCURRENT] = "change-overcurrent",
341 [PORT_C_RESET] = "change-reset",
529f8f9f
GH
342 [PORT_TEST] = "test",
343 [PORT_INDICATOR] = "indicator",
344 };
345 if (feature < 0 || feature >= ARRAY_SIZE(name)) {
346 return "?";
347 }
348 return name[feature] ?: "?";
349}
350
9a77a0f5 351static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
007fd62f 352 int request, int value, int index, int length, uint8_t *data)
72899afc
FB
353{
354 USBHubState *s = (USBHubState *)dev;
355 int ret;
356
529f8f9f
GH
357 trace_usb_hub_control(s->dev.addr, request, value, index, length);
358
007fd62f 359 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
062651c7 360 if (ret >= 0) {
9a77a0f5 361 return;
062651c7
GH
362 }
363
72899afc 364 switch(request) {
985d1742
FB
365 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
366 if (value == 0 && index != 0x81) { /* clear ep halt */
367 goto fail;
368 }
985d1742 369 break;
72899afc
FB
370 /* usb specific requests */
371 case GetHubStatus:
372 data[0] = 0;
373 data[1] = 0;
374 data[2] = 0;
375 data[3] = 0;
9a77a0f5 376 p->actual_length = 4;
72899afc
FB
377 break;
378 case GetPortStatus:
379 {
380 unsigned int n = index - 1;
381 USBHubPort *port;
9d84bb00 382 if (n >= s->num_ports) {
72899afc 383 goto fail;
062651c7 384 }
72899afc 385 port = &s->ports[n];
529f8f9f
GH
386 trace_usb_hub_get_port_status(s->dev.addr, index,
387 port->wPortStatus,
388 port->wPortChange);
72899afc
FB
389 data[0] = port->wPortStatus;
390 data[1] = port->wPortStatus >> 8;
391 data[2] = port->wPortChange;
392 data[3] = port->wPortChange >> 8;
9a77a0f5 393 p->actual_length = 4;
72899afc
FB
394 }
395 break;
396 case SetHubFeature:
397 case ClearHubFeature:
9a77a0f5 398 if (value != 0 && value != 1) {
72899afc
FB
399 goto fail;
400 }
72899afc
FB
401 break;
402 case SetPortFeature:
403 {
404 unsigned int n = index - 1;
405 USBHubPort *port;
406 USBDevice *dev;
529f8f9f
GH
407
408 trace_usb_hub_set_port_feature(s->dev.addr, index,
409 feature_name(value));
410
9d84bb00 411 if (n >= s->num_ports) {
72899afc 412 goto fail;
062651c7 413 }
72899afc
FB
414 port = &s->ports[n];
415 dev = port->port.dev;
416 switch(value) {
417 case PORT_SUSPEND:
418 port->wPortStatus |= PORT_STAT_SUSPEND;
419 break;
420 case PORT_RESET:
868a4203
GH
421 usb_hub_port_set(port, PORT_STAT_RESET);
422 usb_hub_port_clear(port, PORT_STAT_RESET);
3393bc10 423 if (dev && dev->attached) {
d28f4e2d 424 usb_device_reset(dev);
868a4203 425 usb_hub_port_set(port, PORT_STAT_ENABLE);
72899afc 426 }
868a4203 427 usb_wakeup(s->intr, 0);
72899afc
FB
428 break;
429 case PORT_POWER:
1cc403eb
GH
430 if (s->port_power) {
431 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
432 usb_hub_port_set(port, PORT_STAT_POWER);
433 timer_mod(s->port_timer, now + 5000000); /* 5 ms */
434 }
72899afc
FB
435 break;
436 default:
437 goto fail;
438 }
72899afc
FB
439 }
440 break;
441 case ClearPortFeature:
442 {
443 unsigned int n = index - 1;
444 USBHubPort *port;
d4c4e6fd 445
529f8f9f
GH
446 trace_usb_hub_clear_port_feature(s->dev.addr, index,
447 feature_name(value));
448
9d84bb00 449 if (n >= s->num_ports) {
72899afc 450 goto fail;
062651c7 451 }
72899afc 452 port = &s->ports[n];
72899afc
FB
453 switch(value) {
454 case PORT_ENABLE:
455 port->wPortStatus &= ~PORT_STAT_ENABLE;
456 break;
457 case PORT_C_ENABLE:
458 port->wPortChange &= ~PORT_STAT_C_ENABLE;
459 break;
460 case PORT_SUSPEND:
868a4203 461 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
72899afc
FB
462 break;
463 case PORT_C_SUSPEND:
464 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
465 break;
466 case PORT_C_CONNECTION:
467 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
468 break;
469 case PORT_C_OVERCURRENT:
470 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
471 break;
472 case PORT_C_RESET:
473 port->wPortChange &= ~PORT_STAT_C_RESET;
474 break;
1cc403eb
GH
475 case PORT_POWER:
476 if (s->port_power) {
477 usb_hub_port_clear(port, PORT_STAT_POWER);
478 usb_hub_port_clear(port, PORT_STAT_CONNECTION);
479 usb_hub_port_clear(port, PORT_STAT_ENABLE);
480 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
481 port->wPortChange = 0;
482 }
72899afc
FB
483 default:
484 goto fail;
485 }
72899afc
FB
486 }
487 break;
488 case GetHubDescriptor:
985d1742
FB
489 {
490 unsigned int n, limit, var_hub_size = 0;
5fafdf24 491 memcpy(data, qemu_hub_hub_descriptor,
985d1742 492 sizeof(qemu_hub_hub_descriptor));
9d84bb00 493 data[2] = s->num_ports;
985d1742 494
1cc403eb
GH
495 if (s->port_power) {
496 data[3] &= ~0x03;
497 data[3] |= 0x01;
498 }
499
985d1742 500 /* fill DeviceRemovable bits */
9d84bb00 501 limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
985d1742
FB
502 for (n = 7; n < limit; n++) {
503 data[n] = 0x00;
504 var_hub_size++;
505 }
506
507 /* fill PortPwrCtrlMask bits */
9d84bb00 508 limit = limit + DIV_ROUND_UP(s->num_ports, 8);
985d1742
FB
509 for (;n < limit; n++) {
510 data[n] = 0xff;
511 var_hub_size++;
512 }
513
9a77a0f5
HG
514 p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
515 data[0] = p->actual_length;
985d1742
FB
516 break;
517 }
72899afc
FB
518 default:
519 fail:
9a77a0f5 520 p->status = USB_RET_STALL;
72899afc
FB
521 break;
522 }
72899afc
FB
523}
524
9a77a0f5 525static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
72899afc
FB
526{
527 USBHubState *s = (USBHubState *)dev;
72899afc 528
4d611c9a 529 switch(p->pid) {
72899afc 530 case USB_TOKEN_IN:
079d0b7f 531 if (p->ep->nr == 1) {
72899afc
FB
532 USBHubPort *port;
533 unsigned int status;
4f4321c1 534 uint8_t buf[4];
72899afc 535 int i, n;
9d84bb00 536 n = DIV_ROUND_UP(s->num_ports + 1, 8);
4f4321c1 537 if (p->iov.size == 1) { /* FreeBSD workaround */
985d1742 538 n = 1;
4f4321c1 539 } else if (n > p->iov.size) {
9a77a0f5
HG
540 p->status = USB_RET_BABBLE;
541 return;
985d1742 542 }
72899afc 543 status = 0;
9d84bb00 544 for (i = 0; i < s->num_ports; i++) {
72899afc 545 port = &s->ports[i];
bdebd6ee 546 if (port->wPortChange)
72899afc
FB
547 status |= (1 << (i + 1));
548 }
549 if (status != 0) {
b8cbc137 550 trace_usb_hub_status_report(s->dev.addr, status);
72899afc 551 for(i = 0; i < n; i++) {
4f4321c1 552 buf[i] = status >> (8 * i);
72899afc 553 }
4f4321c1 554 usb_packet_copy(p, buf, n);
72899afc 555 } else {
9a77a0f5 556 p->status = USB_RET_NAK; /* usb11 11.13.1 */
72899afc
FB
557 }
558 } else {
559 goto fail;
560 }
561 break;
562 case USB_TOKEN_OUT:
563 default:
564 fail:
9a77a0f5 565 p->status = USB_RET_STALL;
72899afc
FB
566 break;
567 }
72899afc
FB
568}
569
b69c3c21 570static void usb_hub_unrealize(USBDevice *dev)
059809e4
FB
571{
572 USBHubState *s = (USBHubState *)dev;
a8e662b5 573 int i;
059809e4 574
9d84bb00 575 for (i = 0; i < s->num_ports; i++) {
a8e662b5
GH
576 usb_unregister_port(usb_bus_from_device(dev),
577 &s->ports[i].port);
578 }
1cc403eb
GH
579
580 timer_del(s->port_timer);
581 timer_free(s->port_timer);
059809e4
FB
582}
583
0d86d2be
GH
584static USBPortOps usb_hub_port_ops = {
585 .attach = usb_hub_attach,
618c169b 586 .detach = usb_hub_detach,
4706ab6c 587 .child_detach = usb_hub_child_detach,
34239c7b 588 .wakeup = usb_hub_wakeup,
13a9a0d3 589 .complete = usb_hub_complete,
0d86d2be
GH
590};
591
f3f8c459 592static void usb_hub_realize(USBDevice *dev, Error **errp)
72899afc 593{
e81b13ad 594 USBHubState *s = USB_HUB(dev);
72899afc
FB
595 USBHubPort *port;
596 int i;
597
9d84bb00
GH
598 if (s->num_ports < 1 || s->num_ports > MAX_PORTS) {
599 error_setg(errp, "num_ports (%d) out of range (1..%d)",
600 s->num_ports, MAX_PORTS);
601 return;
602 }
603
c24e4aac 604 if (dev->port->hubcount == 5) {
f3f8c459
GA
605 error_setg(errp, "usb hub chain too deep");
606 return;
c24e4aac
GH
607 }
608
9d55d1ad 609 usb_desc_create_serial(dev);
a980a065 610 usb_desc_init(dev);
1cc403eb
GH
611 s->port_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
612 usb_hub_port_update_timer, s);
7567b51f 613 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
9d84bb00 614 for (i = 0; i < s->num_ports; i++) {
72899afc 615 port = &s->ports[i];
a5d2f727 616 usb_register_port(usb_bus_from_device(dev),
ace1318b 617 &port->port, s, i, &usb_hub_port_ops,
843d4e0c 618 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
891fb2cd 619 usb_port_location(&port->port, dev->port, i+1);
72899afc 620 }
20d183b6 621 usb_hub_handle_reset(dev);
806b6024
GH
622}
623
d1550090
GH
624static const VMStateDescription vmstate_usb_hub_port = {
625 .name = "usb-hub-port",
626 .version_id = 1,
627 .minimum_version_id = 1,
6e3d652a 628 .fields = (VMStateField[]) {
d1550090
GH
629 VMSTATE_UINT16(wPortStatus, USBHubPort),
630 VMSTATE_UINT16(wPortChange, USBHubPort),
631 VMSTATE_END_OF_LIST()
632 }
633};
634
1cc403eb
GH
635static bool usb_hub_port_timer_needed(void *opaque)
636{
637 USBHubState *s = opaque;
638
639 return s->port_power;
640}
641
642static const VMStateDescription vmstate_usb_hub_port_timer = {
643 .name = "usb-hub/port-timer",
644 .version_id = 1,
645 .minimum_version_id = 1,
646 .needed = usb_hub_port_timer_needed,
647 .fields = (VMStateField[]) {
648 VMSTATE_TIMER_PTR(port_timer, USBHubState),
649 VMSTATE_END_OF_LIST()
650 },
651};
652
d1550090
GH
653static const VMStateDescription vmstate_usb_hub = {
654 .name = "usb-hub",
655 .version_id = 1,
656 .minimum_version_id = 1,
6e3d652a 657 .fields = (VMStateField[]) {
d1550090 658 VMSTATE_USB_DEVICE(dev, USBHubState),
9d84bb00 659 VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0,
d1550090
GH
660 vmstate_usb_hub_port, USBHubPort),
661 VMSTATE_END_OF_LIST()
1cc403eb
GH
662 },
663 .subsections = (const VMStateDescription * []) {
664 &vmstate_usb_hub_port_timer,
665 NULL
d1550090
GH
666 }
667};
668
9d84bb00
GH
669static Property usb_hub_properties[] = {
670 DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8),
1cc403eb 671 DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false),
9d84bb00
GH
672 DEFINE_PROP_END_OF_LIST(),
673};
674
39bffca2 675static void usb_hub_class_initfn(ObjectClass *klass, void *data)
62aed765 676{
39bffca2 677 DeviceClass *dc = DEVICE_CLASS(klass);
62aed765
AL
678 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
679
f3f8c459 680 uc->realize = usb_hub_realize;
62aed765
AL
681 uc->product_desc = "QEMU USB Hub";
682 uc->usb_desc = &desc_hub;
06c75088 683 uc->find_device = usb_hub_find_device;
62aed765
AL
684 uc->handle_reset = usb_hub_handle_reset;
685 uc->handle_control = usb_hub_handle_control;
686 uc->handle_data = usb_hub_handle_data;
c4fe9700 687 uc->unrealize = usb_hub_unrealize;
125ee0ed 688 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
39bffca2
AL
689 dc->fw_name = "hub";
690 dc->vmsd = &vmstate_usb_hub;
4f67d30b 691 device_class_set_props(dc, usb_hub_properties);
62aed765
AL
692}
693
8c43a6f0 694static const TypeInfo hub_info = {
e81b13ad 695 .name = TYPE_USB_HUB,
39bffca2
AL
696 .parent = TYPE_USB_DEVICE,
697 .instance_size = sizeof(USBHubState),
698 .class_init = usb_hub_class_initfn,
806b6024
GH
699};
700
83f7d43a 701static void usb_hub_register_types(void)
806b6024 702{
39bffca2 703 type_register_static(&hub_info);
72899afc 704}
83f7d43a
AF
705
706type_init(usb_hub_register_types)