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"
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
];
53 typedef struct USBHubState USBHubState
;
55 #define TYPE_USB_HUB "usb-hub"
56 DECLARE_INSTANCE_CHECKER(USBHubState
, USB_HUB
,
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)
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
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
84 #define PORT_CONNECTION 0
86 #define PORT_SUSPEND 2
87 #define PORT_OVERCURRENT 3
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
98 #define PORT_INDICATOR 22
100 /* same as Linux kernel root hubs */
103 STR_MANUFACTURER
= 1,
108 static const USBDescStrings desc_strings
= {
109 [STR_MANUFACTURER
] = "QEMU",
110 [STR_PRODUCT
] = "QEMU USB Hub",
111 [STR_SERIALNUMBER
] = "314159",
114 static const USBDescIface desc_iface_hub
= {
115 .bInterfaceNumber
= 0,
117 .bInterfaceClass
= USB_CLASS_HUB
,
118 .eps
= (USBDescEndpoint
[]) {
120 .bEndpointAddress
= USB_DIR_IN
| 0x01,
121 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
122 .wMaxPacketSize
= 1 + DIV_ROUND_UP(MAX_PORTS
, 8),
128 static const USBDescDevice desc_device_hub
= {
130 .bDeviceClass
= USB_CLASS_HUB
,
131 .bMaxPacketSize0
= 8,
132 .bNumConfigurations
= 1,
133 .confs
= (USBDescConfig
[]) {
136 .bConfigurationValue
= 1,
137 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
|
140 .ifs
= &desc_iface_hub
,
145 static const USBDesc desc_hub
= {
150 .iManufacturer
= STR_MANUFACTURER
,
151 .iProduct
= STR_PRODUCT
,
152 .iSerialNumber
= STR_SERIALNUMBER
,
154 .full
= &desc_device_hub
,
158 static const uint8_t qemu_hub_hub_descriptor
[] =
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 */
168 /* DeviceRemovable and PortPwrCtrlMask patched in later */
171 static bool usb_hub_port_change(USBHubPort
*port
, uint16_t status
)
176 port
->wPortChange
|= status
;
182 static bool usb_hub_port_set(USBHubPort
*port
, uint16_t status
)
184 if (port
->wPortStatus
& status
) {
187 port
->wPortStatus
|= status
;
188 return usb_hub_port_change(port
, status
);
191 static bool usb_hub_port_clear(USBHubPort
*port
, uint16_t status
)
193 if (!(port
->wPortStatus
& status
)) {
196 port
->wPortStatus
&= ~status
;
197 return usb_hub_port_change(port
, status
);
200 static bool usb_hub_port_update(USBHubPort
*port
)
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
);
209 usb_hub_port_clear(port
, PORT_STAT_LOW_SPEED
);
215 static void usb_hub_port_update_timer(void *opaque
)
217 USBHubState
*s
= opaque
;
221 for (i
= 0; i
< s
->num_ports
; i
++) {
222 notify
|= usb_hub_port_update(&s
->ports
[i
]);
225 usb_wakeup(s
->intr
, 0);
229 static void usb_hub_attach(USBPort
*port1
)
231 USBHubState
*s
= port1
->opaque
;
232 USBHubPort
*port
= &s
->ports
[port1
->index
];
234 trace_usb_hub_attach(s
->dev
.addr
, port1
->index
+ 1);
235 usb_hub_port_update(port
);
236 usb_wakeup(s
->intr
, 0);
239 static void usb_hub_detach(USBPort
*port1
)
241 USBHubState
*s
= port1
->opaque
;
242 USBHubPort
*port
= &s
->ports
[port1
->index
];
244 trace_usb_hub_detach(s
->dev
.addr
, port1
->index
+ 1);
245 usb_wakeup(s
->intr
, 0);
247 /* Let upstream know the device on this port is gone */
248 s
->dev
.port
->ops
->child_detach(s
->dev
.port
, port1
->dev
);
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
);
253 usb_wakeup(s
->intr
, 0);
256 static void usb_hub_child_detach(USBPort
*port1
, USBDevice
*child
)
258 USBHubState
*s
= port1
->opaque
;
260 /* Pass along upstream */
261 s
->dev
.port
->ops
->child_detach(s
->dev
.port
, child
);
264 static void usb_hub_wakeup(USBPort
*port1
)
266 USBHubState
*s
= port1
->opaque
;
267 USBHubPort
*port
= &s
->ports
[port1
->index
];
269 if (usb_hub_port_clear(port
, PORT_STAT_SUSPEND
)) {
270 usb_wakeup(s
->intr
, 0);
274 static void usb_hub_complete(USBPort
*port
, USBPacket
*packet
)
276 USBHubState
*s
= port
->opaque
;
279 * Just pass it along upstream for now.
281 * If we ever implement usb 2.0 split transactions this will
282 * become a little more complicated ...
284 * Can't use usb_packet_complete() here because packet->owner is
285 * cleared already, go call the ->complete() callback directly
288 s
->dev
.port
->ops
->complete(s
->dev
.port
, packet
);
291 static USBDevice
*usb_hub_find_device(USBDevice
*dev
, uint8_t addr
)
293 USBHubState
*s
= USB_HUB(dev
);
295 USBDevice
*downstream
;
298 for (i
= 0; i
< s
->num_ports
; i
++) {
300 if (!(port
->wPortStatus
& PORT_STAT_ENABLE
)) {
303 downstream
= usb_find_device(&port
->port
, addr
);
304 if (downstream
!= NULL
) {
311 static void usb_hub_handle_reset(USBDevice
*dev
)
313 USBHubState
*s
= USB_HUB(dev
);
317 trace_usb_hub_reset(s
->dev
.addr
);
318 for (i
= 0; i
< s
->num_ports
; i
++) {
320 port
->wPortStatus
= 0;
321 port
->wPortChange
= 0;
322 usb_hub_port_set(port
, PORT_STAT_POWER
);
323 usb_hub_port_update(port
);
327 static const char *feature_name(int feature
)
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",
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",
343 [PORT_TEST
] = "test",
344 [PORT_INDICATOR
] = "indicator",
346 if (feature
< 0 || feature
>= ARRAY_SIZE(name
)) {
349 return name
[feature
] ?: "?";
352 static void usb_hub_handle_control(USBDevice
*dev
, USBPacket
*p
,
353 int request
, int value
, int index
, int length
, uint8_t *data
)
355 USBHubState
*s
= (USBHubState
*)dev
;
358 trace_usb_hub_control(s
->dev
.addr
, request
, value
, index
, length
);
360 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
366 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
367 if (value
== 0 && index
!= 0x81) { /* clear ep halt */
371 /* usb specific requests */
377 p
->actual_length
= 4;
381 unsigned int n
= index
- 1;
383 if (n
>= s
->num_ports
) {
387 trace_usb_hub_get_port_status(s
->dev
.addr
, index
,
390 data
[0] = port
->wPortStatus
;
391 data
[1] = port
->wPortStatus
>> 8;
392 data
[2] = port
->wPortChange
;
393 data
[3] = port
->wPortChange
>> 8;
394 p
->actual_length
= 4;
398 case ClearHubFeature
:
399 if (value
!= 0 && value
!= 1) {
405 unsigned int n
= index
- 1;
409 trace_usb_hub_set_port_feature(s
->dev
.addr
, index
,
410 feature_name(value
));
412 if (n
>= s
->num_ports
) {
416 dev
= port
->port
.dev
;
419 port
->wPortStatus
|= PORT_STAT_SUSPEND
;
422 usb_hub_port_set(port
, PORT_STAT_RESET
);
423 usb_hub_port_clear(port
, PORT_STAT_RESET
);
424 if (dev
&& dev
->attached
) {
425 usb_device_reset(dev
);
426 usb_hub_port_set(port
, PORT_STAT_ENABLE
);
428 usb_wakeup(s
->intr
, 0);
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 */
442 case ClearPortFeature
:
444 unsigned int n
= index
- 1;
447 trace_usb_hub_clear_port_feature(s
->dev
.addr
, index
,
448 feature_name(value
));
450 if (n
>= s
->num_ports
) {
456 port
->wPortStatus
&= ~PORT_STAT_ENABLE
;
459 port
->wPortChange
&= ~PORT_STAT_C_ENABLE
;
462 usb_hub_port_clear(port
, PORT_STAT_SUSPEND
);
465 port
->wPortChange
&= ~PORT_STAT_C_SUSPEND
;
467 case PORT_C_CONNECTION
:
468 port
->wPortChange
&= ~PORT_STAT_C_CONNECTION
;
470 case PORT_C_OVERCURRENT
:
471 port
->wPortChange
&= ~PORT_STAT_C_OVERCURRENT
;
474 port
->wPortChange
&= ~PORT_STAT_C_RESET
;
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;
489 case GetHubDescriptor
:
491 unsigned int n
, limit
, var_hub_size
= 0;
492 memcpy(data
, qemu_hub_hub_descriptor
,
493 sizeof(qemu_hub_hub_descriptor
));
494 data
[2] = s
->num_ports
;
501 /* fill DeviceRemovable bits */
502 limit
= DIV_ROUND_UP(s
->num_ports
+ 1, 8) + 7;
503 for (n
= 7; n
< limit
; n
++) {
508 /* fill PortPwrCtrlMask bits */
509 limit
= limit
+ DIV_ROUND_UP(s
->num_ports
, 8);
510 for (;n
< limit
; n
++) {
515 p
->actual_length
= sizeof(qemu_hub_hub_descriptor
) + var_hub_size
;
516 data
[0] = p
->actual_length
;
521 p
->status
= USB_RET_STALL
;
526 static void usb_hub_handle_data(USBDevice
*dev
, USBPacket
*p
)
528 USBHubState
*s
= (USBHubState
*)dev
;
532 if (p
->ep
->nr
== 1) {
537 n
= DIV_ROUND_UP(s
->num_ports
+ 1, 8);
538 if (p
->iov
.size
== 1) { /* FreeBSD workaround */
540 } else if (n
> p
->iov
.size
) {
541 p
->status
= USB_RET_BABBLE
;
545 for (i
= 0; i
< s
->num_ports
; i
++) {
547 if (port
->wPortChange
)
548 status
|= (1 << (i
+ 1));
551 trace_usb_hub_status_report(s
->dev
.addr
, status
);
552 for(i
= 0; i
< n
; i
++) {
553 buf
[i
] = status
>> (8 * i
);
555 usb_packet_copy(p
, buf
, n
);
557 p
->status
= USB_RET_NAK
; /* usb11 11.13.1 */
566 p
->status
= USB_RET_STALL
;
571 static void usb_hub_unrealize(USBDevice
*dev
)
573 USBHubState
*s
= (USBHubState
*)dev
;
576 for (i
= 0; i
< s
->num_ports
; i
++) {
577 usb_unregister_port(usb_bus_from_device(dev
),
581 timer_del(s
->port_timer
);
582 timer_free(s
->port_timer
);
585 static USBPortOps usb_hub_port_ops
= {
586 .attach
= usb_hub_attach
,
587 .detach
= usb_hub_detach
,
588 .child_detach
= usb_hub_child_detach
,
589 .wakeup
= usb_hub_wakeup
,
590 .complete
= usb_hub_complete
,
593 static void usb_hub_realize(USBDevice
*dev
, Error
**errp
)
595 USBHubState
*s
= USB_HUB(dev
);
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
);
605 if (dev
->port
->hubcount
== 5) {
606 error_setg(errp
, "usb hub chain too deep");
610 usb_desc_create_serial(dev
);
612 s
->port_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
613 usb_hub_port_update_timer
, s
);
614 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
615 for (i
= 0; i
< s
->num_ports
; i
++) {
617 usb_register_port(usb_bus_from_device(dev
),
618 &port
->port
, s
, i
, &usb_hub_port_ops
,
619 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
);
620 usb_port_location(&port
->port
, dev
->port
, i
+1);
622 usb_hub_handle_reset(dev
);
625 static const VMStateDescription vmstate_usb_hub_port
= {
626 .name
= "usb-hub-port",
628 .minimum_version_id
= 1,
629 .fields
= (VMStateField
[]) {
630 VMSTATE_UINT16(wPortStatus
, USBHubPort
),
631 VMSTATE_UINT16(wPortChange
, USBHubPort
),
632 VMSTATE_END_OF_LIST()
636 static bool usb_hub_port_timer_needed(void *opaque
)
638 USBHubState
*s
= opaque
;
640 return s
->port_power
;
643 static const VMStateDescription vmstate_usb_hub_port_timer
= {
644 .name
= "usb-hub/port-timer",
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()
654 static const VMStateDescription vmstate_usb_hub
= {
657 .minimum_version_id
= 1,
658 .fields
= (VMStateField
[]) {
659 VMSTATE_USB_DEVICE(dev
, USBHubState
),
660 VMSTATE_STRUCT_ARRAY(ports
, USBHubState
, MAX_PORTS
, 0,
661 vmstate_usb_hub_port
, USBHubPort
),
662 VMSTATE_END_OF_LIST()
664 .subsections
= (const VMStateDescription
* []) {
665 &vmstate_usb_hub_port_timer
,
670 static Property usb_hub_properties
[] = {
671 DEFINE_PROP_UINT32("ports", USBHubState
, num_ports
, 8),
672 DEFINE_PROP_BOOL("port-power", USBHubState
, port_power
, false),
673 DEFINE_PROP_END_OF_LIST(),
676 static void usb_hub_class_initfn(ObjectClass
*klass
, void *data
)
678 DeviceClass
*dc
= DEVICE_CLASS(klass
);
679 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
681 uc
->realize
= usb_hub_realize
;
682 uc
->product_desc
= "QEMU USB Hub";
683 uc
->usb_desc
= &desc_hub
;
684 uc
->find_device
= usb_hub_find_device
;
685 uc
->handle_reset
= usb_hub_handle_reset
;
686 uc
->handle_control
= usb_hub_handle_control
;
687 uc
->handle_data
= usb_hub_handle_data
;
688 uc
->unrealize
= usb_hub_unrealize
;
689 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
691 dc
->vmsd
= &vmstate_usb_hub
;
692 device_class_set_props(dc
, usb_hub_properties
);
695 static const TypeInfo hub_info
= {
696 .name
= TYPE_USB_HUB
,
697 .parent
= TYPE_USB_DEVICE
,
698 .instance_size
= sizeof(USBHubState
),
699 .class_init
= usb_hub_class_initfn
,
702 static void usb_hub_register_types(void)
704 type_register_static(&hub_info
);
707 type_init(usb_hub_register_types
)