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