25
#include "qemu/osdep.h"
26
#include "qapi/error.h"
27
#include "qemu/timer.h"
29
#include "hw/qdev-properties.h"
31
#include "migration/vmstate.h"
33
#include "qemu/error-report.h"
34
#include "qemu/module.h"
35
#include "qom/object.h"
39
typedef struct USBHubPort {
50
QEMUTimer *port_timer;
51
USBHubPort ports[MAX_PORTS];
54
#define TYPE_USB_HUB "usb-hub"
55
OBJECT_DECLARE_SIMPLE_TYPE(USBHubState, USB_HUB)
57
#define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
58
#define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
59
#define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
60
#define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
61
#define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
62
#define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
63
#define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
65
#define PORT_STAT_CONNECTION 0x0001
66
#define PORT_STAT_ENABLE 0x0002
67
#define PORT_STAT_SUSPEND 0x0004
68
#define PORT_STAT_OVERCURRENT 0x0008
69
#define PORT_STAT_RESET 0x0010
70
#define PORT_STAT_POWER 0x0100
71
#define PORT_STAT_LOW_SPEED 0x0200
72
#define PORT_STAT_HIGH_SPEED 0x0400
73
#define PORT_STAT_TEST 0x0800
74
#define PORT_STAT_INDICATOR 0x1000
76
#define PORT_STAT_C_CONNECTION 0x0001
77
#define PORT_STAT_C_ENABLE 0x0002
78
#define PORT_STAT_C_SUSPEND 0x0004
79
#define PORT_STAT_C_OVERCURRENT 0x0008
80
#define PORT_STAT_C_RESET 0x0010
82
#define PORT_CONNECTION 0
85
#define PORT_OVERCURRENT 3
88
#define PORT_LOWSPEED 9
89
#define PORT_HIGHSPEED 10
90
#define PORT_C_CONNECTION 16
91
#define PORT_C_ENABLE 17
92
#define PORT_C_SUSPEND 18
93
#define PORT_C_OVERCURRENT 19
94
#define PORT_C_RESET 20
96
#define PORT_INDICATOR 22
101
STR_MANUFACTURER = 1,
106
static const USBDescStrings desc_strings = {
107
[STR_MANUFACTURER] = "QEMU",
108
[STR_PRODUCT] = "QEMU USB Hub",
109
[STR_SERIALNUMBER] = "314159",
112
static const USBDescIface desc_iface_hub = {
113
.bInterfaceNumber = 0,
115
.bInterfaceClass = USB_CLASS_HUB,
116
.eps = (USBDescEndpoint[]) {
118
.bEndpointAddress = USB_DIR_IN | 0x01,
119
.bmAttributes = USB_ENDPOINT_XFER_INT,
120
.wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
126
static const USBDescDevice desc_device_hub = {
128
.bDeviceClass = USB_CLASS_HUB,
129
.bMaxPacketSize0 = 8,
130
.bNumConfigurations = 1,
131
.confs = (USBDescConfig[]) {
134
.bConfigurationValue = 1,
135
.bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
138
.ifs = &desc_iface_hub,
143
static const USBDesc desc_hub = {
148
.iManufacturer = STR_MANUFACTURER,
149
.iProduct = STR_PRODUCT,
150
.iSerialNumber = STR_SERIALNUMBER,
152
.full = &desc_device_hub,
156
static const uint8_t qemu_hub_hub_descriptor[] =
169
static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
174
port->wPortChange |= status;
180
static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
182
if (port->wPortStatus & status) {
185
port->wPortStatus |= status;
186
return usb_hub_port_change(port, status);
189
static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
191
if (!(port->wPortStatus & status)) {
194
port->wPortStatus &= ~status;
195
return usb_hub_port_change(port, status);
198
static bool usb_hub_port_update(USBHubPort *port)
202
if (port->port.dev && port->port.dev->attached) {
203
notify = usb_hub_port_set(port, PORT_STAT_CONNECTION);
204
if (port->port.dev->speed == USB_SPEED_LOW) {
205
usb_hub_port_set(port, PORT_STAT_LOW_SPEED);
207
usb_hub_port_clear(port, PORT_STAT_LOW_SPEED);
213
static void usb_hub_port_update_timer(void *opaque)
215
USBHubState *s = opaque;
219
for (i = 0; i < s->num_ports; i++) {
220
notify |= usb_hub_port_update(&s->ports[i]);
223
usb_wakeup(s->intr, 0);
227
static void usb_hub_attach(USBPort *port1)
229
USBHubState *s = port1->opaque;
230
USBHubPort *port = &s->ports[port1->index];
232
trace_usb_hub_attach(s->dev.addr, port1->index + 1);
233
usb_hub_port_update(port);
234
usb_wakeup(s->intr, 0);
237
static void usb_hub_detach(USBPort *port1)
239
USBHubState *s = port1->opaque;
240
USBHubPort *port = &s->ports[port1->index];
242
trace_usb_hub_detach(s->dev.addr, port1->index + 1);
243
usb_wakeup(s->intr, 0);
246
s->dev.port->ops->child_detach(s->dev.port, port1->dev);
248
usb_hub_port_clear(port, PORT_STAT_CONNECTION);
249
usb_hub_port_clear(port, PORT_STAT_ENABLE);
250
usb_hub_port_clear(port, PORT_STAT_SUSPEND);
251
usb_wakeup(s->intr, 0);
254
static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
256
USBHubState *s = port1->opaque;
259
s->dev.port->ops->child_detach(s->dev.port, child);
262
static void usb_hub_wakeup(USBPort *port1)
264
USBHubState *s = port1->opaque;
265
USBHubPort *port = &s->ports[port1->index];
267
if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
268
usb_wakeup(s->intr, 0);
272
static void usb_hub_complete(USBPort *port, USBPacket *packet)
274
USBHubState *s = port->opaque;
286
s->dev.port->ops->complete(s->dev.port, packet);
289
static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
291
USBHubState *s = USB_HUB(dev);
293
USBDevice *downstream;
296
for (i = 0; i < s->num_ports; i++) {
298
if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
301
downstream = usb_find_device(&port->port, addr);
302
if (downstream != NULL) {
309
static void usb_hub_handle_reset(USBDevice *dev)
311
USBHubState *s = USB_HUB(dev);
315
trace_usb_hub_reset(s->dev.addr);
316
for (i = 0; i < s->num_ports; i++) {
318
port->wPortStatus = 0;
319
port->wPortChange = 0;
320
usb_hub_port_set(port, PORT_STAT_POWER);
321
usb_hub_port_update(port);
325
static const char *feature_name(int feature)
327
static const char *name[] = {
328
[PORT_CONNECTION] = "connection",
329
[PORT_ENABLE] = "enable",
330
[PORT_SUSPEND] = "suspend",
331
[PORT_OVERCURRENT] = "overcurrent",
332
[PORT_RESET] = "reset",
333
[PORT_POWER] = "power",
334
[PORT_LOWSPEED] = "lowspeed",
335
[PORT_HIGHSPEED] = "highspeed",
336
[PORT_C_CONNECTION] = "change-connection",
337
[PORT_C_ENABLE] = "change-enable",
338
[PORT_C_SUSPEND] = "change-suspend",
339
[PORT_C_OVERCURRENT] = "change-overcurrent",
340
[PORT_C_RESET] = "change-reset",
341
[PORT_TEST] = "test",
342
[PORT_INDICATOR] = "indicator",
344
if (feature < 0 || feature >= ARRAY_SIZE(name)) {
347
return name[feature] ?: "?";
350
static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
351
int request, int value, int index, int length, uint8_t *data)
353
USBHubState *s = (USBHubState *)dev;
356
trace_usb_hub_control(s->dev.addr, request, value, index, length);
358
ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
364
case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
365
if (value == 0 && index != 0x81) {
375
p->actual_length = 4;
379
unsigned int n = index - 1;
381
if (n >= s->num_ports) {
385
trace_usb_hub_get_port_status(s->dev.addr, index,
388
data[0] = port->wPortStatus;
389
data[1] = port->wPortStatus >> 8;
390
data[2] = port->wPortChange;
391
data[3] = port->wPortChange >> 8;
392
p->actual_length = 4;
396
case ClearHubFeature:
397
if (value != 0 && value != 1) {
403
unsigned int n = index - 1;
407
trace_usb_hub_set_port_feature(s->dev.addr, index,
408
feature_name(value));
410
if (n >= s->num_ports) {
414
pdev = port->port.dev;
417
port->wPortStatus |= PORT_STAT_SUSPEND;
420
usb_hub_port_set(port, PORT_STAT_RESET);
421
usb_hub_port_clear(port, PORT_STAT_RESET);
422
if (pdev && pdev->attached) {
423
usb_device_reset(pdev);
424
usb_hub_port_set(port, PORT_STAT_ENABLE);
426
usb_wakeup(s->intr, 0);
430
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
431
usb_hub_port_set(port, PORT_STAT_POWER);
432
timer_mod(s->port_timer, now + 5000000);
440
case ClearPortFeature:
442
unsigned int n = index - 1;
445
trace_usb_hub_clear_port_feature(s->dev.addr, index,
446
feature_name(value));
448
if (n >= s->num_ports) {
454
port->wPortStatus &= ~PORT_STAT_ENABLE;
457
port->wPortChange &= ~PORT_STAT_C_ENABLE;
460
usb_hub_port_clear(port, PORT_STAT_SUSPEND);
463
port->wPortChange &= ~PORT_STAT_C_SUSPEND;
465
case PORT_C_CONNECTION:
466
port->wPortChange &= ~PORT_STAT_C_CONNECTION;
468
case PORT_C_OVERCURRENT:
469
port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
472
port->wPortChange &= ~PORT_STAT_C_RESET;
476
usb_hub_port_clear(port, PORT_STAT_POWER);
477
usb_hub_port_clear(port, PORT_STAT_CONNECTION);
478
usb_hub_port_clear(port, PORT_STAT_ENABLE);
479
usb_hub_port_clear(port, PORT_STAT_SUSPEND);
480
port->wPortChange = 0;
487
case GetHubDescriptor:
489
unsigned int n, limit, var_hub_size = 0;
490
memcpy(data, qemu_hub_hub_descriptor,
491
sizeof(qemu_hub_hub_descriptor));
492
data[2] = s->num_ports;
500
limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
501
for (n = 7; n < limit; n++) {
507
limit = limit + DIV_ROUND_UP(s->num_ports, 8);
508
for (;n < limit; n++) {
513
p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
514
data[0] = p->actual_length;
519
p->status = USB_RET_STALL;
524
static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
526
USBHubState *s = (USBHubState *)dev;
530
if (p->ep->nr == 1) {
535
n = DIV_ROUND_UP(s->num_ports + 1, 8);
536
if (p->iov.size == 1) {
538
} else if (n > p->iov.size) {
539
p->status = USB_RET_BABBLE;
543
for (i = 0; i < s->num_ports; i++) {
545
if (port->wPortChange)
546
status |= (1 << (i + 1));
549
trace_usb_hub_status_report(s->dev.addr, status);
550
for(i = 0; i < n; i++) {
551
buf[i] = status >> (8 * i);
553
usb_packet_copy(p, buf, n);
555
p->status = USB_RET_NAK;
564
p->status = USB_RET_STALL;
569
static void usb_hub_unrealize(USBDevice *dev)
571
USBHubState *s = (USBHubState *)dev;
574
for (i = 0; i < s->num_ports; i++) {
575
usb_unregister_port(usb_bus_from_device(dev),
579
timer_free(s->port_timer);
582
static USBPortOps usb_hub_port_ops = {
583
.attach = usb_hub_attach,
584
.detach = usb_hub_detach,
585
.child_detach = usb_hub_child_detach,
586
.wakeup = usb_hub_wakeup,
587
.complete = usb_hub_complete,
590
static void usb_hub_realize(USBDevice *dev, Error **errp)
592
USBHubState *s = USB_HUB(dev);
596
if (s->num_ports < 1 || s->num_ports > MAX_PORTS) {
597
error_setg(errp, "num_ports (%d) out of range (1..%d)",
598
s->num_ports, MAX_PORTS);
602
if (dev->port->hubcount == 5) {
603
error_setg(errp, "usb hub chain too deep");
607
usb_desc_create_serial(dev);
609
s->port_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
610
usb_hub_port_update_timer, s);
611
s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
612
for (i = 0; i < s->num_ports; i++) {
614
usb_register_port(usb_bus_from_device(dev),
615
&port->port, s, i, &usb_hub_port_ops,
616
USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
617
usb_port_location(&port->port, dev->port, i+1);
619
usb_hub_handle_reset(dev);
622
static const VMStateDescription vmstate_usb_hub_port = {
623
.name = "usb-hub-port",
625
.minimum_version_id = 1,
626
.fields = (const VMStateField[]) {
627
VMSTATE_UINT16(wPortStatus, USBHubPort),
628
VMSTATE_UINT16(wPortChange, USBHubPort),
629
VMSTATE_END_OF_LIST()
633
static bool usb_hub_port_timer_needed(void *opaque)
635
USBHubState *s = opaque;
637
return s->port_power;
640
static const VMStateDescription vmstate_usb_hub_port_timer = {
641
.name = "usb-hub/port-timer",
643
.minimum_version_id = 1,
644
.needed = usb_hub_port_timer_needed,
645
.fields = (const VMStateField[]) {
646
VMSTATE_TIMER_PTR(port_timer, USBHubState),
647
VMSTATE_END_OF_LIST()
651
static const VMStateDescription vmstate_usb_hub = {
654
.minimum_version_id = 1,
655
.fields = (const VMStateField[]) {
656
VMSTATE_USB_DEVICE(dev, USBHubState),
657
VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0,
658
vmstate_usb_hub_port, USBHubPort),
659
VMSTATE_END_OF_LIST()
661
.subsections = (const VMStateDescription * const []) {
662
&vmstate_usb_hub_port_timer,
667
static Property usb_hub_properties[] = {
668
DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8),
669
DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false),
670
DEFINE_PROP_END_OF_LIST(),
673
static void usb_hub_class_initfn(ObjectClass *klass, void *data)
675
DeviceClass *dc = DEVICE_CLASS(klass);
676
USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
678
uc->realize = usb_hub_realize;
679
uc->product_desc = "QEMU USB Hub";
680
uc->usb_desc = &desc_hub;
681
uc->find_device = usb_hub_find_device;
682
uc->handle_reset = usb_hub_handle_reset;
683
uc->handle_control = usb_hub_handle_control;
684
uc->handle_data = usb_hub_handle_data;
685
uc->unrealize = usb_hub_unrealize;
686
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
688
dc->vmsd = &vmstate_usb_hub;
689
device_class_set_props(dc, usb_hub_properties);
692
static const TypeInfo hub_info = {
693
.name = TYPE_USB_HUB,
694
.parent = TYPE_USB_DEVICE,
695
.instance_size = sizeof(USBHubState),
696
.class_init = usb_hub_class_initfn,
699
static void usb_hub_register_types(void)
701
type_register_static(&hub_info);
704
type_init(usb_hub_register_types)