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
24 #include "qemu-common.h"
32 typedef struct USBHubPort {
38 typedef struct USBHubState {
41 USBHubPort ports[MAX_PORTS];
44 static void usb_hub_save_state(QEMUFile *f, void *opaque)
46 USBHubState *s = (USBHubState *)opaque;
49 qemu_put_sbe32(f, s->nb_ports);
50 for (i = 0; i < s->nb_ports; i++) {
51 qemu_put_be16(f, s->ports[i].wPortStatus);
52 qemu_put_be16(f, s->ports[i].wPortChange);
56 static int usb_hub_load_state(QEMUFile *f, void *opaque, int version_id)
58 USBHubState *s = (USBHubState *)opaque;
64 if (qemu_get_sbe32(f) != s->nb_ports)
67 for (i = 0; i < s->nb_ports; i++) {
68 s->ports[i].wPortStatus = qemu_get_be16(f);
69 s->ports[i].wPortChange = qemu_get_be16(f);
75 #define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
76 #define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
77 #define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
78 #define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
79 #define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
80 #define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
81 #define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
83 #define PORT_STAT_CONNECTION 0x0001
84 #define PORT_STAT_ENABLE 0x0002
85 #define PORT_STAT_SUSPEND 0x0004
86 #define PORT_STAT_OVERCURRENT 0x0008
87 #define PORT_STAT_RESET 0x0010
88 #define PORT_STAT_POWER 0x0100
89 #define PORT_STAT_LOW_SPEED 0x0200
90 #define PORT_STAT_HIGH_SPEED 0x0400
91 #define PORT_STAT_TEST 0x0800
92 #define PORT_STAT_INDICATOR 0x1000
94 #define PORT_STAT_C_CONNECTION 0x0001
95 #define PORT_STAT_C_ENABLE 0x0002
96 #define PORT_STAT_C_SUSPEND 0x0004
97 #define PORT_STAT_C_OVERCURRENT 0x0008
98 #define PORT_STAT_C_RESET 0x0010
100 #define PORT_CONNECTION 0
101 #define PORT_ENABLE 1
102 #define PORT_SUSPEND 2
103 #define PORT_OVERCURRENT 3
106 #define PORT_LOWSPEED 9
107 #define PORT_HIGHSPEED 10
108 #define PORT_C_CONNECTION 16
109 #define PORT_C_ENABLE 17
110 #define PORT_C_SUSPEND 18
111 #define PORT_C_OVERCURRENT 19
112 #define PORT_C_RESET 20
114 #define PORT_INDICATOR 22
116 /* same as Linux kernel root hubs */
118 static const uint8_t qemu_hub_dev_descriptor[] = {
119 0x12, /* u8 bLength; */
120 0x01, /* u8 bDescriptorType; Device */
121 0x10, 0x01, /* u16 bcdUSB; v1.1 */
123 0x09, /* u8 bDeviceClass; HUB_CLASSCODE */
124 0x00, /* u8 bDeviceSubClass; */
125 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
126 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
128 0x00, 0x00, /* u16 idVendor; */
129 0x00, 0x00, /* u16 idProduct; */
130 0x01, 0x01, /* u16 bcdDevice */
132 0x03, /* u8 iManufacturer; */
133 0x02, /* u8 iProduct; */
134 0x01, /* u8 iSerialNumber; */
135 0x01 /* u8 bNumConfigurations; */
138 /* XXX: patch interrupt size */
139 static const uint8_t qemu_hub_config_descriptor[] = {
141 /* one configuration */
142 0x09, /* u8 bLength; */
143 0x02, /* u8 bDescriptorType; Configuration */
144 0x19, 0x00, /* u16 wTotalLength; */
145 0x01, /* u8 bNumInterfaces; (1) */
146 0x01, /* u8 bConfigurationValue; */
147 0x00, /* u8 iConfiguration; */
148 0xe0, /* u8 bmAttributes;
153 0x00, /* u8 MaxPower; */
156 * USB 2.0, single TT organization (mandatory):
157 * one interface, protocol 0
159 * USB 2.0, multiple TT organization (optional):
160 * two interfaces, protocols 1 (like single TT)
161 * and 2 (multiple TT mode) ... config is
167 0x09, /* u8 if_bLength; */
168 0x04, /* u8 if_bDescriptorType; Interface */
169 0x00, /* u8 if_bInterfaceNumber; */
170 0x00, /* u8 if_bAlternateSetting; */
171 0x01, /* u8 if_bNumEndpoints; */
172 0x09, /* u8 if_bInterfaceClass; HUB_CLASSCODE */
173 0x00, /* u8 if_bInterfaceSubClass; */
174 0x00, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
175 0x00, /* u8 if_iInterface; */
177 /* one endpoint (status change endpoint) */
178 0x07, /* u8 ep_bLength; */
179 0x05, /* u8 ep_bDescriptorType; Endpoint */
180 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
181 0x03, /* u8 ep_bmAttributes; Interrupt */
182 0x02, 0x00, /* u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
183 0xff /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
186 static const uint8_t qemu_hub_hub_descriptor[] =
188 0x00, /* u8 bLength; patched in later */
189 0x29, /* u8 bDescriptorType; Hub-descriptor */
190 0x00, /* u8 bNbrPorts; (patched later) */
191 0x0a, /* u16 wHubCharacteristics; */
192 0x00, /* (per-port OC, no power switching) */
193 0x01, /* u8 bPwrOn2pwrGood; 2ms */
194 0x00 /* u8 bHubContrCurrent; 0 mA */
196 /* DeviceRemovable and PortPwrCtrlMask patched in later */
199 static void usb_hub_attach(USBPort *port1, USBDevice *dev)
201 USBHubState *s = port1->opaque;
202 USBHubPort *port = &s->ports[port1->index];
206 usb_attach(port1, NULL);
208 port->wPortStatus |= PORT_STAT_CONNECTION;
209 port->wPortChange |= PORT_STAT_C_CONNECTION;
210 if (dev->speed == USB_SPEED_LOW)
211 port->wPortStatus |= PORT_STAT_LOW_SPEED;
213 port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
214 port->port.dev = dev;
215 /* send the attach message */
216 usb_send_msg(dev, USB_MSG_ATTACH);
218 dev = port->port.dev;
220 port->wPortStatus &= ~PORT_STAT_CONNECTION;
221 port->wPortChange |= PORT_STAT_C_CONNECTION;
222 if (port->wPortStatus & PORT_STAT_ENABLE) {
223 port->wPortStatus &= ~PORT_STAT_ENABLE;
224 port->wPortChange |= PORT_STAT_C_ENABLE;
226 /* send the detach message */
227 usb_send_msg(dev, USB_MSG_DETACH);
228 port->port.dev = NULL;
233 static void usb_hub_handle_reset(USBDevice *dev)
238 static int usb_hub_handle_control(USBDevice *dev, int request, int value,
239 int index, int length, uint8_t *data)
241 USBHubState *s = (USBHubState *)dev;
245 case DeviceRequest | USB_REQ_GET_STATUS:
246 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
247 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
251 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
252 if (value == USB_DEVICE_REMOTE_WAKEUP) {
253 dev->remote_wakeup = 0;
259 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
260 if (value == 0 && index != 0x81) { /* clear ep halt */
265 case DeviceOutRequest | USB_REQ_SET_FEATURE:
266 if (value == USB_DEVICE_REMOTE_WAKEUP) {
267 dev->remote_wakeup = 1;
273 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
277 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
280 memcpy(data, qemu_hub_dev_descriptor,
281 sizeof(qemu_hub_dev_descriptor));
282 ret = sizeof(qemu_hub_dev_descriptor);
285 memcpy(data, qemu_hub_config_descriptor,
286 sizeof(qemu_hub_config_descriptor));
288 /* status change endpoint size based on number
290 data[22] = (s->nb_ports + 1 + 7) / 8;
292 ret = sizeof(qemu_hub_config_descriptor);
295 switch(value & 0xff) {
306 ret = set_usb_string(data, "314159");
309 /* product description */
310 ret = set_usb_string(data, "QEMU USB Hub");
313 /* vendor description */
314 ret = set_usb_string(data, "QEMU " QEMU_VERSION);
324 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
328 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
331 case DeviceRequest | USB_REQ_GET_INTERFACE:
335 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
338 /* usb specific requests */
348 unsigned int n = index - 1;
350 if (n >= s->nb_ports)
353 data[0] = port->wPortStatus;
354 data[1] = port->wPortStatus >> 8;
355 data[2] = port->wPortChange;
356 data[3] = port->wPortChange >> 8;
361 case ClearHubFeature:
362 if (value == 0 || value == 1) {
370 unsigned int n = index - 1;
373 if (n >= s->nb_ports)
376 dev = port->port.dev;
379 port->wPortStatus |= PORT_STAT_SUSPEND;
383 usb_send_msg(dev, USB_MSG_RESET);
384 port->wPortChange |= PORT_STAT_C_RESET;
386 port->wPortStatus |= PORT_STAT_ENABLE;
397 case ClearPortFeature:
399 unsigned int n = index - 1;
402 if (n >= s->nb_ports)
405 dev = port->port.dev;
408 port->wPortStatus &= ~PORT_STAT_ENABLE;
411 port->wPortChange &= ~PORT_STAT_C_ENABLE;
414 port->wPortStatus &= ~PORT_STAT_SUSPEND;
417 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
419 case PORT_C_CONNECTION:
420 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
422 case PORT_C_OVERCURRENT:
423 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
426 port->wPortChange &= ~PORT_STAT_C_RESET;
434 case GetHubDescriptor:
436 unsigned int n, limit, var_hub_size = 0;
437 memcpy(data, qemu_hub_hub_descriptor,
438 sizeof(qemu_hub_hub_descriptor));
439 data[2] = s->nb_ports;
441 /* fill DeviceRemovable bits */
442 limit = ((s->nb_ports + 1 + 7) / 8) + 7;
443 for (n = 7; n < limit; n++) {
448 /* fill PortPwrCtrlMask bits */
449 limit = limit + ((s->nb_ports + 7) / 8);
450 for (;n < limit; n++) {
455 ret = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
467 static int usb_hub_handle_data(USBDevice *dev, USBPacket *p)
469 USBHubState *s = (USBHubState *)dev;
478 n = (s->nb_ports + 1 + 7) / 8;
479 if (p->len == 1) { /* FreeBSD workaround */
481 } else if (n > p->len) {
482 return USB_RET_BABBLE;
485 for(i = 0; i < s->nb_ports; i++) {
487 if (port->wPortChange)
488 status |= (1 << (i + 1));
491 for(i = 0; i < n; i++) {
492 p->data[i] = status >> (8 * i);
496 ret = USB_RET_NAK; /* usb11 11.13.1 */
511 static int usb_hub_broadcast_packet(USBHubState *s, USBPacket *p)
517 for(i = 0; i < s->nb_ports; i++) {
519 dev = port->port.dev;
520 if (dev && (port->wPortStatus & PORT_STAT_ENABLE)) {
521 ret = dev->handle_packet(dev, p);
522 if (ret != USB_RET_NODEV) {
527 return USB_RET_NODEV;
530 static int usb_hub_handle_packet(USBDevice *dev, USBPacket *p)
532 USBHubState *s = (USBHubState *)dev;
534 #if defined(DEBUG) && 0
535 printf("usb_hub: pid=0x%x\n", pid);
537 if (dev->state == USB_STATE_DEFAULT &&
539 p->devaddr != dev->addr &&
540 (p->pid == USB_TOKEN_SETUP ||
541 p->pid == USB_TOKEN_OUT ||
542 p->pid == USB_TOKEN_IN)) {
543 /* broadcast the packet to the devices */
544 return usb_hub_broadcast_packet(s, p);
546 return usb_generic_handle_packet(dev, p);
549 static void usb_hub_handle_destroy(USBDevice *dev)
551 USBHubState *s = (USBHubState *)dev;
556 USBDevice *usb_hub_init(int nb_ports)
562 if (nb_ports > MAX_PORTS)
564 s = qemu_mallocz(sizeof(USBHubState));
565 s->dev.speed = USB_SPEED_FULL;
566 s->dev.handle_packet = usb_hub_handle_packet;
568 /* generic USB device init */
569 s->dev.handle_reset = usb_hub_handle_reset;
570 s->dev.handle_control = usb_hub_handle_control;
571 s->dev.handle_data = usb_hub_handle_data;
572 s->dev.handle_destroy = usb_hub_handle_destroy;
574 pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Hub");
576 s->nb_ports = nb_ports;
577 for(i = 0; i < s->nb_ports; i++) {
579 qemu_register_usb_port(&port->port, s, i, usb_hub_attach);
580 port->wPortStatus = PORT_STAT_POWER;
581 port->wPortChange = 0;
584 register_savevm("usb hub", -1, 0,
585 usb_hub_save_state, usb_hub_load_state, s);
587 return (USBDevice *)s;