2 * QEMU USB HUB emulation
4 * Copyright (c) 2005 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/timer.h"
31 #include "qemu/error-report.h"
32 #include "qemu/module.h"
36 typedef struct USBHubPort {
42 typedef struct USBHubState {
47 QEMUTimer *port_timer;
48 USBHubPort ports[MAX_PORTS];
51 #define TYPE_USB_HUB "usb-hub"
52 #define USB_HUB(obj) OBJECT_CHECK(USBHubState, (obj), TYPE_USB_HUB)
54 #define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
55 #define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
56 #define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
57 #define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
58 #define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
59 #define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
60 #define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
62 #define PORT_STAT_CONNECTION 0x0001
63 #define PORT_STAT_ENABLE 0x0002
64 #define PORT_STAT_SUSPEND 0x0004
65 #define PORT_STAT_OVERCURRENT 0x0008
66 #define PORT_STAT_RESET 0x0010
67 #define PORT_STAT_POWER 0x0100
68 #define PORT_STAT_LOW_SPEED 0x0200
69 #define PORT_STAT_HIGH_SPEED 0x0400
70 #define PORT_STAT_TEST 0x0800
71 #define PORT_STAT_INDICATOR 0x1000
73 #define PORT_STAT_C_CONNECTION 0x0001
74 #define PORT_STAT_C_ENABLE 0x0002
75 #define PORT_STAT_C_SUSPEND 0x0004
76 #define PORT_STAT_C_OVERCURRENT 0x0008
77 #define PORT_STAT_C_RESET 0x0010
79 #define PORT_CONNECTION 0
81 #define PORT_SUSPEND 2
82 #define PORT_OVERCURRENT 3
85 #define PORT_LOWSPEED 9
86 #define PORT_HIGHSPEED 10
87 #define PORT_C_CONNECTION 16
88 #define PORT_C_ENABLE 17
89 #define PORT_C_SUSPEND 18
90 #define PORT_C_OVERCURRENT 19
91 #define PORT_C_RESET 20
93 #define PORT_INDICATOR 22
95 /* same as Linux kernel root hubs */
103 static const USBDescStrings desc_strings = {
104 [STR_MANUFACTURER] = "QEMU",
105 [STR_PRODUCT] = "QEMU USB Hub",
106 [STR_SERIALNUMBER] = "314159",
109 static const USBDescIface desc_iface_hub = {
110 .bInterfaceNumber = 0,
112 .bInterfaceClass = USB_CLASS_HUB,
113 .eps = (USBDescEndpoint[]) {
115 .bEndpointAddress = USB_DIR_IN | 0x01,
116 .bmAttributes = USB_ENDPOINT_XFER_INT,
117 .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
123 static const USBDescDevice desc_device_hub = {
125 .bDeviceClass = USB_CLASS_HUB,
126 .bMaxPacketSize0 = 8,
127 .bNumConfigurations = 1,
128 .confs = (USBDescConfig[]) {
131 .bConfigurationValue = 1,
132 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
135 .ifs = &desc_iface_hub,
140 static const USBDesc desc_hub = {
145 .iManufacturer = STR_MANUFACTURER,
146 .iProduct = STR_PRODUCT,
147 .iSerialNumber = STR_SERIALNUMBER,
149 .full = &desc_device_hub,
153 static const uint8_t qemu_hub_hub_descriptor[] =
155 0x00, /* u8 bLength; patched in later */
156 0x29, /* u8 bDescriptorType; Hub-descriptor */
157 0x00, /* u8 bNbrPorts; (patched later) */
158 0x0a, /* u16 wHubCharacteristics; */
159 0x00, /* (per-port OC, no power switching) */
160 0x01, /* u8 bPwrOn2pwrGood; 2ms */
161 0x00 /* u8 bHubContrCurrent; 0 mA */
163 /* DeviceRemovable and PortPwrCtrlMask patched in later */
166 static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
171 port->wPortChange |= status;
177 static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
179 if (port->wPortStatus & status) {
182 port->wPortStatus |= status;
183 return usb_hub_port_change(port, status);
186 static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
188 if (!(port->wPortStatus & status)) {
191 port->wPortStatus &= ~status;
192 return usb_hub_port_change(port, status);
195 static bool usb_hub_port_update(USBHubPort *port)
199 if (port->port.dev && port->port.dev->attached) {
200 notify = usb_hub_port_set(port, PORT_STAT_CONNECTION);
201 if (port->port.dev->speed == USB_SPEED_LOW) {
202 usb_hub_port_set(port, PORT_STAT_LOW_SPEED);
204 usb_hub_port_clear(port, PORT_STAT_LOW_SPEED);
210 static void usb_hub_port_update_timer(void *opaque)
212 USBHubState *s = opaque;
216 for (i = 0; i < s->num_ports; i++) {
217 notify |= usb_hub_port_update(&s->ports[i]);
220 usb_wakeup(s->intr, 0);
224 static void usb_hub_attach(USBPort *port1)
226 USBHubState *s = port1->opaque;
227 USBHubPort *port = &s->ports[port1->index];
229 trace_usb_hub_attach(s->dev.addr, port1->index + 1);
230 usb_hub_port_update(port);
231 usb_wakeup(s->intr, 0);
234 static void usb_hub_detach(USBPort *port1)
236 USBHubState *s = port1->opaque;
237 USBHubPort *port = &s->ports[port1->index];
239 trace_usb_hub_detach(s->dev.addr, port1->index + 1);
240 usb_wakeup(s->intr, 0);
242 /* Let upstream know the device on this port is gone */
243 s->dev.port->ops->child_detach(s->dev.port, port1->dev);
245 usb_hub_port_clear(port, PORT_STAT_CONNECTION);
246 usb_hub_port_clear(port, PORT_STAT_ENABLE);
247 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
248 usb_wakeup(s->intr, 0);
251 static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
253 USBHubState *s = port1->opaque;
255 /* Pass along upstream */
256 s->dev.port->ops->child_detach(s->dev.port, child);
259 static void usb_hub_wakeup(USBPort *port1)
261 USBHubState *s = port1->opaque;
262 USBHubPort *port = &s->ports[port1->index];
264 if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
265 usb_wakeup(s->intr, 0);
269 static void usb_hub_complete(USBPort *port, USBPacket *packet)
271 USBHubState *s = port->opaque;
274 * Just pass it along upstream for now.
276 * If we ever implement usb 2.0 split transactions this will
277 * become a little more complicated ...
279 * Can't use usb_packet_complete() here because packet->owner is
280 * cleared already, go call the ->complete() callback directly
283 s->dev.port->ops->complete(s->dev.port, packet);
286 static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
288 USBHubState *s = USB_HUB(dev);
290 USBDevice *downstream;
293 for (i = 0; i < s->num_ports; i++) {
295 if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
298 downstream = usb_find_device(&port->port, addr);
299 if (downstream != NULL) {
306 static void usb_hub_handle_reset(USBDevice *dev)
308 USBHubState *s = USB_HUB(dev);
312 trace_usb_hub_reset(s->dev.addr);
313 for (i = 0; i < s->num_ports; i++) {
315 port->wPortStatus = 0;
316 port->wPortChange = 0;
317 usb_hub_port_set(port, PORT_STAT_POWER);
318 usb_hub_port_update(port);
322 static const char *feature_name(int feature)
324 static const char *name[] = {
325 [PORT_CONNECTION] = "connection",
326 [PORT_ENABLE] = "enable",
327 [PORT_SUSPEND] = "suspend",
328 [PORT_OVERCURRENT] = "overcurrent",
329 [PORT_RESET] = "reset",
330 [PORT_POWER] = "power",
331 [PORT_LOWSPEED] = "lowspeed",
332 [PORT_HIGHSPEED] = "highspeed",
333 [PORT_C_CONNECTION] = "change-connection",
334 [PORT_C_ENABLE] = "change-enable",
335 [PORT_C_SUSPEND] = "change-suspend",
336 [PORT_C_OVERCURRENT] = "change-overcurrent",
337 [PORT_C_RESET] = "change-reset",
338 [PORT_TEST] = "test",
339 [PORT_INDICATOR] = "indicator",
341 if (feature < 0 || feature >= ARRAY_SIZE(name)) {
344 return name[feature] ?: "?";
347 static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
348 int request, int value, int index, int length, uint8_t *data)
350 USBHubState *s = (USBHubState *)dev;
353 trace_usb_hub_control(s->dev.addr, request, value, index, length);
355 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
361 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
362 if (value == 0 && index != 0x81) { /* clear ep halt */
366 /* usb specific requests */
372 p->actual_length = 4;
376 unsigned int n = index - 1;
378 if (n >= s->num_ports) {
382 trace_usb_hub_get_port_status(s->dev.addr, index,
385 data[0] = port->wPortStatus;
386 data[1] = port->wPortStatus >> 8;
387 data[2] = port->wPortChange;
388 data[3] = port->wPortChange >> 8;
389 p->actual_length = 4;
393 case ClearHubFeature:
394 if (value != 0 && value != 1) {
400 unsigned int n = index - 1;
404 trace_usb_hub_set_port_feature(s->dev.addr, index,
405 feature_name(value));
407 if (n >= s->num_ports) {
411 dev = port->port.dev;
414 port->wPortStatus |= PORT_STAT_SUSPEND;
417 usb_hub_port_set(port, PORT_STAT_RESET);
418 usb_hub_port_clear(port, PORT_STAT_RESET);
419 if (dev && dev->attached) {
420 usb_device_reset(dev);
421 usb_hub_port_set(port, PORT_STAT_ENABLE);
423 usb_wakeup(s->intr, 0);
427 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
428 usb_hub_port_set(port, PORT_STAT_POWER);
429 timer_mod(s->port_timer, now + 5000000); /* 5 ms */
437 case ClearPortFeature:
439 unsigned int n = index - 1;
442 trace_usb_hub_clear_port_feature(s->dev.addr, index,
443 feature_name(value));
445 if (n >= s->num_ports) {
451 port->wPortStatus &= ~PORT_STAT_ENABLE;
454 port->wPortChange &= ~PORT_STAT_C_ENABLE;
457 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
460 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
462 case PORT_C_CONNECTION:
463 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
465 case PORT_C_OVERCURRENT:
466 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
469 port->wPortChange &= ~PORT_STAT_C_RESET;
473 usb_hub_port_clear(port, PORT_STAT_POWER);
474 usb_hub_port_clear(port, PORT_STAT_CONNECTION);
475 usb_hub_port_clear(port, PORT_STAT_ENABLE);
476 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
477 port->wPortChange = 0;
484 case GetHubDescriptor:
486 unsigned int n, limit, var_hub_size = 0;
487 memcpy(data, qemu_hub_hub_descriptor,
488 sizeof(qemu_hub_hub_descriptor));
489 data[2] = s->num_ports;
496 /* fill DeviceRemovable bits */
497 limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
498 for (n = 7; n < limit; n++) {
503 /* fill PortPwrCtrlMask bits */
504 limit = limit + DIV_ROUND_UP(s->num_ports, 8);
505 for (;n < limit; n++) {
510 p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
511 data[0] = p->actual_length;
516 p->status = USB_RET_STALL;
521 static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
523 USBHubState *s = (USBHubState *)dev;
527 if (p->ep->nr == 1) {
532 n = DIV_ROUND_UP(s->num_ports + 1, 8);
533 if (p->iov.size == 1) { /* FreeBSD workaround */
535 } else if (n > p->iov.size) {
536 p->status = USB_RET_BABBLE;
540 for (i = 0; i < s->num_ports; i++) {
542 if (port->wPortChange)
543 status |= (1 << (i + 1));
546 trace_usb_hub_status_report(s->dev.addr, status);
547 for(i = 0; i < n; i++) {
548 buf[i] = status >> (8 * i);
550 usb_packet_copy(p, buf, n);
552 p->status = USB_RET_NAK; /* usb11 11.13.1 */
561 p->status = USB_RET_STALL;
566 static void usb_hub_unrealize(USBDevice *dev, Error **errp)
568 USBHubState *s = (USBHubState *)dev;
571 for (i = 0; i < s->num_ports; i++) {
572 usb_unregister_port(usb_bus_from_device(dev),
576 timer_del(s->port_timer);
577 timer_free(s->port_timer);
580 static USBPortOps usb_hub_port_ops = {
581 .attach = usb_hub_attach,
582 .detach = usb_hub_detach,
583 .child_detach = usb_hub_child_detach,
584 .wakeup = usb_hub_wakeup,
585 .complete = usb_hub_complete,
588 static void usb_hub_realize(USBDevice *dev, Error **errp)
590 USBHubState *s = USB_HUB(dev);
594 if (s->num_ports < 1 || s->num_ports > MAX_PORTS) {
595 error_setg(errp, "num_ports (%d) out of range (1..%d)",
596 s->num_ports, MAX_PORTS);
600 if (dev->port->hubcount == 5) {
601 error_setg(errp, "usb hub chain too deep");
605 usb_desc_create_serial(dev);
607 s->port_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
608 usb_hub_port_update_timer, s);
609 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
610 for (i = 0; i < s->num_ports; i++) {
612 usb_register_port(usb_bus_from_device(dev),
613 &port->port, s, i, &usb_hub_port_ops,
614 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
615 usb_port_location(&port->port, dev->port, i+1);
617 usb_hub_handle_reset(dev);
620 static const VMStateDescription vmstate_usb_hub_port = {
621 .name = "usb-hub-port",
623 .minimum_version_id = 1,
624 .fields = (VMStateField[]) {
625 VMSTATE_UINT16(wPortStatus, USBHubPort),
626 VMSTATE_UINT16(wPortChange, USBHubPort),
627 VMSTATE_END_OF_LIST()
631 static bool usb_hub_port_timer_needed(void *opaque)
633 USBHubState *s = opaque;
635 return s->port_power;
638 static const VMStateDescription vmstate_usb_hub_port_timer = {
639 .name = "usb-hub/port-timer",
641 .minimum_version_id = 1,
642 .needed = usb_hub_port_timer_needed,
643 .fields = (VMStateField[]) {
644 VMSTATE_TIMER_PTR(port_timer, USBHubState),
645 VMSTATE_END_OF_LIST()
649 static const VMStateDescription vmstate_usb_hub = {
652 .minimum_version_id = 1,
653 .fields = (VMStateField[]) {
654 VMSTATE_USB_DEVICE(dev, USBHubState),
655 VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0,
656 vmstate_usb_hub_port, USBHubPort),
657 VMSTATE_END_OF_LIST()
659 .subsections = (const VMStateDescription * []) {
660 &vmstate_usb_hub_port_timer,
665 static Property usb_hub_properties[] = {
666 DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8),
667 DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false),
668 DEFINE_PROP_END_OF_LIST(),
671 static void usb_hub_class_initfn(ObjectClass *klass, void *data)
673 DeviceClass *dc = DEVICE_CLASS(klass);
674 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
676 uc->realize = usb_hub_realize;
677 uc->product_desc = "QEMU USB Hub";
678 uc->usb_desc = &desc_hub;
679 uc->find_device = usb_hub_find_device;
680 uc->handle_reset = usb_hub_handle_reset;
681 uc->handle_control = usb_hub_handle_control;
682 uc->handle_data = usb_hub_handle_data;
683 uc->unrealize = usb_hub_unrealize;
684 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
686 dc->vmsd = &vmstate_usb_hub;
687 dc->props = usb_hub_properties;
690 static const TypeInfo hub_info = {
691 .name = TYPE_USB_HUB,
692 .parent = TYPE_USB_DEVICE,
693 .instance_size = sizeof(USBHubState),
694 .class_init = usb_hub_class_initfn,
697 static void usb_hub_register_types(void)
699 type_register_static(&hub_info);
702 type_init(usb_hub_register_types)