blob: 752e30c305a2dc1ac5dabc62c413e8413b3de6c2 [file] [log] [blame]
bellard72899af2006-04-24 21:18:20 +00001/*
2 * QEMU USB HUB emulation
3 *
4 * Copyright (c) 2005 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard72899af2006-04-24 21:18:20 +00006 * 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 */
Peter Maydelle532b2e2016-01-26 18:17:12 +000024#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
pbrook87ecb682007-11-17 17:14:51 +000026#include "qemu-common.h"
Gerd Hoffmann529f8f92012-03-23 15:42:58 +010027#include "trace.h"
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +010028#include "hw/usb.h"
29#include "hw/usb/desc.h"
Gerd Hoffmannc24e4aa2013-03-20 11:40:02 +010030#include "qemu/error-report.h"
bellard72899af2006-04-24 21:18:20 +000031
Gerd Hoffmann062651c2010-11-26 13:13:22 +010032#define NUM_PORTS 8
bellard72899af2006-04-24 21:18:20 +000033
34typedef struct USBHubPort {
35 USBPort port;
36 uint16_t wPortStatus;
37 uint16_t wPortChange;
38} USBHubPort;
39
40typedef struct USBHubState {
41 USBDevice dev;
Gerd Hoffmann7567b512012-01-17 13:25:46 +010042 USBEndpoint *intr;
Gerd Hoffmann062651c2010-11-26 13:13:22 +010043 USBHubPort ports[NUM_PORTS];
bellard72899af2006-04-24 21:18:20 +000044} USBHubState;
45
Gongleie81b13a2015-05-06 20:55:27 +080046#define TYPE_USB_HUB "usb-hub"
47#define USB_HUB(obj) OBJECT_CHECK(USBHubState, (obj), TYPE_USB_HUB)
48
bellard72899af2006-04-24 21:18:20 +000049#define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
50#define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
51#define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
52#define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
53#define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
54#define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
55#define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
56
57#define PORT_STAT_CONNECTION 0x0001
58#define PORT_STAT_ENABLE 0x0002
59#define PORT_STAT_SUSPEND 0x0004
60#define PORT_STAT_OVERCURRENT 0x0008
61#define PORT_STAT_RESET 0x0010
62#define PORT_STAT_POWER 0x0100
63#define PORT_STAT_LOW_SPEED 0x0200
64#define PORT_STAT_HIGH_SPEED 0x0400
65#define PORT_STAT_TEST 0x0800
66#define PORT_STAT_INDICATOR 0x1000
67
68#define PORT_STAT_C_CONNECTION 0x0001
69#define PORT_STAT_C_ENABLE 0x0002
70#define PORT_STAT_C_SUSPEND 0x0004
71#define PORT_STAT_C_OVERCURRENT 0x0008
72#define PORT_STAT_C_RESET 0x0010
73
74#define PORT_CONNECTION 0
75#define PORT_ENABLE 1
76#define PORT_SUSPEND 2
77#define PORT_OVERCURRENT 3
78#define PORT_RESET 4
79#define PORT_POWER 8
80#define PORT_LOWSPEED 9
81#define PORT_HIGHSPEED 10
82#define PORT_C_CONNECTION 16
83#define PORT_C_ENABLE 17
84#define PORT_C_SUSPEND 18
85#define PORT_C_OVERCURRENT 19
86#define PORT_C_RESET 20
87#define PORT_TEST 21
88#define PORT_INDICATOR 22
89
90/* same as Linux kernel root hubs */
91
Gerd Hoffmann062651c2010-11-26 13:13:22 +010092enum {
93 STR_MANUFACTURER = 1,
94 STR_PRODUCT,
95 STR_SERIALNUMBER,
96};
97
98static const USBDescStrings desc_strings = {
Crístian Viana93bfef42012-05-30 00:35:51 -030099 [STR_MANUFACTURER] = "QEMU",
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100100 [STR_PRODUCT] = "QEMU USB Hub",
101 [STR_SERIALNUMBER] = "314159",
102};
103
104static const USBDescIface desc_iface_hub = {
105 .bInterfaceNumber = 0,
106 .bNumEndpoints = 1,
107 .bInterfaceClass = USB_CLASS_HUB,
108 .eps = (USBDescEndpoint[]) {
109 {
110 .bEndpointAddress = USB_DIR_IN | 0x01,
111 .bmAttributes = USB_ENDPOINT_XFER_INT,
Marc-André Lureau54ac85e2017-06-22 13:04:16 +0200112 .wMaxPacketSize = 1 + DIV_ROUND_UP(NUM_PORTS, 8),
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100113 .bInterval = 0xff,
114 },
115 }
116};
117
118static const USBDescDevice desc_device_hub = {
119 .bcdUSB = 0x0110,
120 .bDeviceClass = USB_CLASS_HUB,
121 .bMaxPacketSize0 = 8,
122 .bNumConfigurations = 1,
123 .confs = (USBDescConfig[]) {
124 {
125 .bNumInterfaces = 1,
126 .bConfigurationValue = 1,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200127 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
128 USB_CFG_ATT_WAKEUP,
Brad Hardsadd75082011-04-03 15:33:19 +1000129 .nif = 1,
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100130 .ifs = &desc_iface_hub,
131 },
132 },
133};
134
135static const USBDesc desc_hub = {
136 .id = {
Roy Tamdb803582011-09-15 11:25:47 +0800137 .idVendor = 0x0409,
138 .idProduct = 0x55aa,
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100139 .bcdDevice = 0x0101,
140 .iManufacturer = STR_MANUFACTURER,
141 .iProduct = STR_PRODUCT,
142 .iSerialNumber = STR_SERIALNUMBER,
143 },
144 .full = &desc_device_hub,
145 .str = desc_strings,
146};
147
bellard72899af2006-04-24 21:18:20 +0000148static const uint8_t qemu_hub_hub_descriptor[] =
149{
bellard67f36562006-05-05 20:05:35 +0000150 0x00, /* u8 bLength; patched in later */
bellard72899af2006-04-24 21:18:20 +0000151 0x29, /* u8 bDescriptorType; Hub-descriptor */
152 0x00, /* u8 bNbrPorts; (patched later) */
153 0x0a, /* u16 wHubCharacteristics; */
154 0x00, /* (per-port OC, no power switching) */
155 0x01, /* u8 bPwrOn2pwrGood; 2ms */
bellard985d1742006-04-30 21:53:59 +0000156 0x00 /* u8 bHubContrCurrent; 0 mA */
157
158 /* DeviceRemovable and PortPwrCtrlMask patched in later */
bellard72899af2006-04-24 21:18:20 +0000159};
160
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100161static void usb_hub_attach(USBPort *port1)
bellard72899af2006-04-24 21:18:20 +0000162{
163 USBHubState *s = port1->opaque;
164 USBHubPort *port = &s->ports[port1->index];
ths3b46e622007-09-17 08:09:54 +0000165
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100166 trace_usb_hub_attach(s->dev.addr, port1->index + 1);
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100167 port->wPortStatus |= PORT_STAT_CONNECTION;
168 port->wPortChange |= PORT_STAT_C_CONNECTION;
169 if (port->port.dev->speed == USB_SPEED_LOW) {
170 port->wPortStatus |= PORT_STAT_LOW_SPEED;
bellard72899af2006-04-24 21:18:20 +0000171 } else {
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100172 port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
173 }
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100174 usb_wakeup(s->intr, 0);
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100175}
176
177static void usb_hub_detach(USBPort *port1)
178{
179 USBHubState *s = port1->opaque;
180 USBHubPort *port = &s->ports[port1->index];
181
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100182 trace_usb_hub_detach(s->dev.addr, port1->index + 1);
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100183 usb_wakeup(s->intr, 0);
Gerd Hoffmannbe35cbb2011-11-22 13:20:14 +0100184
Hans de Goede4706ab62011-06-24 12:31:11 +0200185 /* Let upstream know the device on this port is gone */
186 s->dev.port->ops->child_detach(s->dev.port, port1->dev);
187
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100188 port->wPortStatus &= ~PORT_STAT_CONNECTION;
189 port->wPortChange |= PORT_STAT_C_CONNECTION;
190 if (port->wPortStatus & PORT_STAT_ENABLE) {
191 port->wPortStatus &= ~PORT_STAT_ENABLE;
192 port->wPortChange |= PORT_STAT_C_ENABLE;
bellard72899af2006-04-24 21:18:20 +0000193 }
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100194 usb_wakeup(s->intr, 0);
bellard72899af2006-04-24 21:18:20 +0000195}
196
Hans de Goede4706ab62011-06-24 12:31:11 +0200197static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
198{
199 USBHubState *s = port1->opaque;
200
201 /* Pass along upstream */
202 s->dev.port->ops->child_detach(s->dev.port, child);
203}
204
Hans de Goeded47e59b2011-06-21 11:52:28 +0200205static void usb_hub_wakeup(USBPort *port1)
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100206{
Hans de Goeded47e59b2011-06-21 11:52:28 +0200207 USBHubState *s = port1->opaque;
208 USBHubPort *port = &s->ports[port1->index];
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100209
210 if (port->wPortStatus & PORT_STAT_SUSPEND) {
Ladi Prosek66849dc2017-05-11 14:53:14 +0200211 port->wPortStatus &= ~PORT_STAT_SUSPEND;
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100212 port->wPortChange |= PORT_STAT_C_SUSPEND;
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100213 usb_wakeup(s->intr, 0);
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100214 }
215}
216
Hans de Goeded47e59b2011-06-21 11:52:28 +0200217static void usb_hub_complete(USBPort *port, USBPacket *packet)
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100218{
Hans de Goeded47e59b2011-06-21 11:52:28 +0200219 USBHubState *s = port->opaque;
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100220
221 /*
222 * Just pass it along upstream for now.
223 *
Gerd Hoffmann80cf7cf2011-10-13 12:52:47 +0200224 * If we ever implement usb 2.0 split transactions this will
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100225 * become a little more complicated ...
Gerd Hoffmann80cf7cf2011-10-13 12:52:47 +0200226 *
227 * Can't use usb_packet_complete() here because packet->owner is
228 * cleared already, go call the ->complete() callback directly
229 * instead.
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100230 */
Gerd Hoffmann80cf7cf2011-10-13 12:52:47 +0200231 s->dev.port->ops->complete(s->dev.port, packet);
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100232}
233
Gerd Hoffmann06c75082012-01-10 17:08:13 +0100234static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
235{
Gongleie81b13a2015-05-06 20:55:27 +0800236 USBHubState *s = USB_HUB(dev);
Gerd Hoffmann06c75082012-01-10 17:08:13 +0100237 USBHubPort *port;
238 USBDevice *downstream;
239 int i;
240
241 for (i = 0; i < NUM_PORTS; i++) {
242 port = &s->ports[i];
243 if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
244 continue;
245 }
246 downstream = usb_find_device(&port->port, addr);
247 if (downstream != NULL) {
248 return downstream;
249 }
250 }
251 return NULL;
252}
253
bellard059809e2006-07-19 18:06:15 +0000254static void usb_hub_handle_reset(USBDevice *dev)
bellard72899af2006-04-24 21:18:20 +0000255{
Gongleie81b13a2015-05-06 20:55:27 +0800256 USBHubState *s = USB_HUB(dev);
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100257 USBHubPort *port;
258 int i;
259
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100260 trace_usb_hub_reset(s->dev.addr);
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100261 for (i = 0; i < NUM_PORTS; i++) {
262 port = s->ports + i;
263 port->wPortStatus = PORT_STAT_POWER;
264 port->wPortChange = 0;
265 if (port->port.dev && port->port.dev->attached) {
266 port->wPortStatus |= PORT_STAT_CONNECTION;
267 port->wPortChange |= PORT_STAT_C_CONNECTION;
268 if (port->port.dev->speed == USB_SPEED_LOW) {
269 port->wPortStatus |= PORT_STAT_LOW_SPEED;
270 }
271 }
272 }
bellard72899af2006-04-24 21:18:20 +0000273}
274
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100275static const char *feature_name(int feature)
276{
277 static const char *name[] = {
278 [PORT_CONNECTION] = "connection",
279 [PORT_ENABLE] = "enable",
280 [PORT_SUSPEND] = "suspend",
281 [PORT_OVERCURRENT] = "overcurrent",
282 [PORT_RESET] = "reset",
283 [PORT_POWER] = "power",
284 [PORT_LOWSPEED] = "lowspeed",
285 [PORT_HIGHSPEED] = "highspeed",
286 [PORT_C_CONNECTION] = "change connection",
287 [PORT_C_ENABLE] = "change enable",
288 [PORT_C_SUSPEND] = "change suspend",
289 [PORT_C_OVERCURRENT] = "change overcurrent",
290 [PORT_C_RESET] = "change reset",
291 [PORT_TEST] = "test",
292 [PORT_INDICATOR] = "indicator",
293 };
294 if (feature < 0 || feature >= ARRAY_SIZE(name)) {
295 return "?";
296 }
297 return name[feature] ?: "?";
298}
299
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100300static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
Hans de Goede007fd622011-02-02 16:33:13 +0100301 int request, int value, int index, int length, uint8_t *data)
bellard72899af2006-04-24 21:18:20 +0000302{
303 USBHubState *s = (USBHubState *)dev;
304 int ret;
305
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100306 trace_usb_hub_control(s->dev.addr, request, value, index, length);
307
Hans de Goede007fd622011-02-02 16:33:13 +0100308 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100309 if (ret >= 0) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100310 return;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100311 }
312
bellard72899af2006-04-24 21:18:20 +0000313 switch(request) {
bellard985d1742006-04-30 21:53:59 +0000314 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
315 if (value == 0 && index != 0x81) { /* clear ep halt */
316 goto fail;
317 }
bellard985d1742006-04-30 21:53:59 +0000318 break;
bellard72899af2006-04-24 21:18:20 +0000319 /* usb specific requests */
320 case GetHubStatus:
321 data[0] = 0;
322 data[1] = 0;
323 data[2] = 0;
324 data[3] = 0;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100325 p->actual_length = 4;
bellard72899af2006-04-24 21:18:20 +0000326 break;
327 case GetPortStatus:
328 {
329 unsigned int n = index - 1;
330 USBHubPort *port;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100331 if (n >= NUM_PORTS) {
bellard72899af2006-04-24 21:18:20 +0000332 goto fail;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100333 }
bellard72899af2006-04-24 21:18:20 +0000334 port = &s->ports[n];
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100335 trace_usb_hub_get_port_status(s->dev.addr, index,
336 port->wPortStatus,
337 port->wPortChange);
bellard72899af2006-04-24 21:18:20 +0000338 data[0] = port->wPortStatus;
339 data[1] = port->wPortStatus >> 8;
340 data[2] = port->wPortChange;
341 data[3] = port->wPortChange >> 8;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100342 p->actual_length = 4;
bellard72899af2006-04-24 21:18:20 +0000343 }
344 break;
345 case SetHubFeature:
346 case ClearHubFeature:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100347 if (value != 0 && value != 1) {
bellard72899af2006-04-24 21:18:20 +0000348 goto fail;
349 }
bellard72899af2006-04-24 21:18:20 +0000350 break;
351 case SetPortFeature:
352 {
353 unsigned int n = index - 1;
354 USBHubPort *port;
355 USBDevice *dev;
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100356
357 trace_usb_hub_set_port_feature(s->dev.addr, index,
358 feature_name(value));
359
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100360 if (n >= NUM_PORTS) {
bellard72899af2006-04-24 21:18:20 +0000361 goto fail;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100362 }
bellard72899af2006-04-24 21:18:20 +0000363 port = &s->ports[n];
364 dev = port->port.dev;
365 switch(value) {
366 case PORT_SUSPEND:
367 port->wPortStatus |= PORT_STAT_SUSPEND;
368 break;
369 case PORT_RESET:
Gerd Hoffmann3393bc12011-09-15 09:20:02 +0200370 if (dev && dev->attached) {
Gerd Hoffmannd28f4e22012-01-06 15:23:10 +0100371 usb_device_reset(dev);
bellard72899af2006-04-24 21:18:20 +0000372 port->wPortChange |= PORT_STAT_C_RESET;
373 /* set enable bit */
bellard72899af2006-04-24 21:18:20 +0000374 port->wPortStatus |= PORT_STAT_ENABLE;
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100375 usb_wakeup(s->intr, 0);
bellard72899af2006-04-24 21:18:20 +0000376 }
377 break;
378 case PORT_POWER:
379 break;
380 default:
381 goto fail;
382 }
bellard72899af2006-04-24 21:18:20 +0000383 }
384 break;
385 case ClearPortFeature:
386 {
387 unsigned int n = index - 1;
388 USBHubPort *port;
Blue Swirld4c4e6f2010-04-25 18:23:04 +0000389
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100390 trace_usb_hub_clear_port_feature(s->dev.addr, index,
391 feature_name(value));
392
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100393 if (n >= NUM_PORTS) {
bellard72899af2006-04-24 21:18:20 +0000394 goto fail;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100395 }
bellard72899af2006-04-24 21:18:20 +0000396 port = &s->ports[n];
bellard72899af2006-04-24 21:18:20 +0000397 switch(value) {
398 case PORT_ENABLE:
399 port->wPortStatus &= ~PORT_STAT_ENABLE;
400 break;
401 case PORT_C_ENABLE:
402 port->wPortChange &= ~PORT_STAT_C_ENABLE;
403 break;
404 case PORT_SUSPEND:
Ladi Prosek6361bbc2017-05-22 14:33:25 +0200405 if (port->wPortStatus & PORT_STAT_SUSPEND) {
406 port->wPortStatus &= ~PORT_STAT_SUSPEND;
407
408 /*
409 * USB Spec rev2.0 11.24.2.7.2.3 C_PORT_SUSPEND
410 * "This bit is set on the following transitions:
411 * - On transition from the Resuming state to the
412 * SendEOP [sic] state"
413 *
414 * Note that this includes both remote wake-up and
415 * explicit ClearPortFeature(PORT_SUSPEND).
416 */
417 port->wPortChange |= PORT_STAT_C_SUSPEND;
418 }
bellard72899af2006-04-24 21:18:20 +0000419 break;
420 case PORT_C_SUSPEND:
421 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
422 break;
423 case PORT_C_CONNECTION:
424 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
425 break;
426 case PORT_C_OVERCURRENT:
427 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
428 break;
429 case PORT_C_RESET:
430 port->wPortChange &= ~PORT_STAT_C_RESET;
431 break;
432 default:
433 goto fail;
434 }
bellard72899af2006-04-24 21:18:20 +0000435 }
436 break;
437 case GetHubDescriptor:
bellard985d1742006-04-30 21:53:59 +0000438 {
439 unsigned int n, limit, var_hub_size = 0;
ths5fafdf22007-09-16 21:08:06 +0000440 memcpy(data, qemu_hub_hub_descriptor,
bellard985d1742006-04-30 21:53:59 +0000441 sizeof(qemu_hub_hub_descriptor));
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100442 data[2] = NUM_PORTS;
bellard985d1742006-04-30 21:53:59 +0000443
444 /* fill DeviceRemovable bits */
Marc-André Lureau54ac85e2017-06-22 13:04:16 +0200445 limit = DIV_ROUND_UP(NUM_PORTS + 1, 8) + 7;
bellard985d1742006-04-30 21:53:59 +0000446 for (n = 7; n < limit; n++) {
447 data[n] = 0x00;
448 var_hub_size++;
449 }
450
451 /* fill PortPwrCtrlMask bits */
Marc-André Lureau54ac85e2017-06-22 13:04:16 +0200452 limit = limit + DIV_ROUND_UP(NUM_PORTS, 8);
bellard985d1742006-04-30 21:53:59 +0000453 for (;n < limit; n++) {
454 data[n] = 0xff;
455 var_hub_size++;
456 }
457
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100458 p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
459 data[0] = p->actual_length;
bellard985d1742006-04-30 21:53:59 +0000460 break;
461 }
bellard72899af2006-04-24 21:18:20 +0000462 default:
463 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100464 p->status = USB_RET_STALL;
bellard72899af2006-04-24 21:18:20 +0000465 break;
466 }
bellard72899af2006-04-24 21:18:20 +0000467}
468
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100469static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
bellard72899af2006-04-24 21:18:20 +0000470{
471 USBHubState *s = (USBHubState *)dev;
bellard72899af2006-04-24 21:18:20 +0000472
pbrook4d611c92006-08-12 01:04:27 +0000473 switch(p->pid) {
bellard72899af2006-04-24 21:18:20 +0000474 case USB_TOKEN_IN:
Gerd Hoffmann079d0b72012-01-12 13:23:01 +0100475 if (p->ep->nr == 1) {
bellard72899af2006-04-24 21:18:20 +0000476 USBHubPort *port;
477 unsigned int status;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200478 uint8_t buf[4];
bellard72899af2006-04-24 21:18:20 +0000479 int i, n;
Marc-André Lureau54ac85e2017-06-22 13:04:16 +0200480 n = DIV_ROUND_UP(NUM_PORTS + 1, 8);
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200481 if (p->iov.size == 1) { /* FreeBSD workaround */
bellard985d1742006-04-30 21:53:59 +0000482 n = 1;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200483 } else if (n > p->iov.size) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100484 p->status = USB_RET_BABBLE;
485 return;
bellard985d1742006-04-30 21:53:59 +0000486 }
bellard72899af2006-04-24 21:18:20 +0000487 status = 0;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100488 for(i = 0; i < NUM_PORTS; i++) {
bellard72899af2006-04-24 21:18:20 +0000489 port = &s->ports[i];
Gerd Hoffmannbdebd6e2013-08-27 17:00:04 +0200490 if (port->wPortChange)
bellard72899af2006-04-24 21:18:20 +0000491 status |= (1 << (i + 1));
492 }
493 if (status != 0) {
Gerd Hoffmannb8cbc132013-08-27 16:59:37 +0200494 trace_usb_hub_status_report(s->dev.addr, status);
bellard72899af2006-04-24 21:18:20 +0000495 for(i = 0; i < n; i++) {
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200496 buf[i] = status >> (8 * i);
bellard72899af2006-04-24 21:18:20 +0000497 }
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200498 usb_packet_copy(p, buf, n);
bellard72899af2006-04-24 21:18:20 +0000499 } else {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100500 p->status = USB_RET_NAK; /* usb11 11.13.1 */
bellard72899af2006-04-24 21:18:20 +0000501 }
502 } else {
503 goto fail;
504 }
505 break;
506 case USB_TOKEN_OUT:
507 default:
508 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100509 p->status = USB_RET_STALL;
bellard72899af2006-04-24 21:18:20 +0000510 break;
511 }
bellard72899af2006-04-24 21:18:20 +0000512}
513
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400514static void usb_hub_unrealize(USBDevice *dev, Error **errp)
bellard059809e2006-07-19 18:06:15 +0000515{
516 USBHubState *s = (USBHubState *)dev;
Gerd Hoffmanna8e662b2009-09-25 21:42:39 +0200517 int i;
bellard059809e2006-07-19 18:06:15 +0000518
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100519 for (i = 0; i < NUM_PORTS; i++) {
Gerd Hoffmanna8e662b2009-09-25 21:42:39 +0200520 usb_unregister_port(usb_bus_from_device(dev),
521 &s->ports[i].port);
522 }
bellard059809e2006-07-19 18:06:15 +0000523}
524
Gerd Hoffmann0d86d2b2010-12-01 11:08:44 +0100525static USBPortOps usb_hub_port_ops = {
526 .attach = usb_hub_attach,
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100527 .detach = usb_hub_detach,
Hans de Goede4706ab62011-06-24 12:31:11 +0200528 .child_detach = usb_hub_child_detach,
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100529 .wakeup = usb_hub_wakeup,
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100530 .complete = usb_hub_complete,
Gerd Hoffmann0d86d2b2010-12-01 11:08:44 +0100531};
532
Gongleif3f8c452014-09-19 14:48:28 +0800533static void usb_hub_realize(USBDevice *dev, Error **errp)
bellard72899af2006-04-24 21:18:20 +0000534{
Gongleie81b13a2015-05-06 20:55:27 +0800535 USBHubState *s = USB_HUB(dev);
bellard72899af2006-04-24 21:18:20 +0000536 USBHubPort *port;
537 int i;
538
Gerd Hoffmannc24e4aa2013-03-20 11:40:02 +0100539 if (dev->port->hubcount == 5) {
Gongleif3f8c452014-09-19 14:48:28 +0800540 error_setg(errp, "usb hub chain too deep");
541 return;
Gerd Hoffmannc24e4aa2013-03-20 11:40:02 +0100542 }
543
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +0200544 usb_desc_create_serial(dev);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100545 usb_desc_init(dev);
Gerd Hoffmann7567b512012-01-17 13:25:46 +0100546 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100547 for (i = 0; i < NUM_PORTS; i++) {
bellard72899af2006-04-24 21:18:20 +0000548 port = &s->ports[i];
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200549 usb_register_port(usb_bus_from_device(dev),
Gerd Hoffmannace13182011-01-12 11:34:50 +0100550 &port->port, s, i, &usb_hub_port_ops,
Gerd Hoffmann843d4e02010-12-03 17:30:13 +0100551 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
Gerd Hoffmann891fb2c2011-09-01 13:56:37 +0200552 usb_port_location(&port->port, dev->port, i+1);
bellard72899af2006-04-24 21:18:20 +0000553 }
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100554 usb_hub_handle_reset(dev);
bellard72899af2006-04-24 21:18:20 +0000555}
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200556
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100557static const VMStateDescription vmstate_usb_hub_port = {
558 .name = "usb-hub-port",
559 .version_id = 1,
560 .minimum_version_id = 1,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200561 .fields = (VMStateField[]) {
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100562 VMSTATE_UINT16(wPortStatus, USBHubPort),
563 VMSTATE_UINT16(wPortChange, USBHubPort),
564 VMSTATE_END_OF_LIST()
565 }
566};
567
568static const VMStateDescription vmstate_usb_hub = {
569 .name = "usb-hub",
570 .version_id = 1,
571 .minimum_version_id = 1,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200572 .fields = (VMStateField[]) {
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100573 VMSTATE_USB_DEVICE(dev, USBHubState),
574 VMSTATE_STRUCT_ARRAY(ports, USBHubState, NUM_PORTS, 0,
575 vmstate_usb_hub_port, USBHubPort),
576 VMSTATE_END_OF_LIST()
577 }
578};
579
Anthony Liguori39bffca2011-12-07 21:34:16 -0600580static void usb_hub_class_initfn(ObjectClass *klass, void *data)
Anthony Liguori62aed762011-12-15 14:53:10 -0600581{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600582 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600583 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
584
Gongleif3f8c452014-09-19 14:48:28 +0800585 uc->realize = usb_hub_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600586 uc->product_desc = "QEMU USB Hub";
587 uc->usb_desc = &desc_hub;
Gerd Hoffmann06c75082012-01-10 17:08:13 +0100588 uc->find_device = usb_hub_find_device;
Anthony Liguori62aed762011-12-15 14:53:10 -0600589 uc->handle_reset = usb_hub_handle_reset;
590 uc->handle_control = usb_hub_handle_control;
591 uc->handle_data = usb_hub_handle_data;
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400592 uc->unrealize = usb_hub_unrealize;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300593 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600594 dc->fw_name = "hub";
595 dc->vmsd = &vmstate_usb_hub;
Anthony Liguori62aed762011-12-15 14:53:10 -0600596}
597
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100598static const TypeInfo hub_info = {
Gongleie81b13a2015-05-06 20:55:27 +0800599 .name = TYPE_USB_HUB,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600600 .parent = TYPE_USB_DEVICE,
601 .instance_size = sizeof(USBHubState),
602 .class_init = usb_hub_class_initfn,
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200603};
604
Andreas Färber83f7d432012-02-09 15:20:55 +0100605static void usb_hub_register_types(void)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200606{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600607 type_register_static(&hub_info);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200608}
Andreas Färber83f7d432012-02-09 15:20:55 +0100609
610type_init(usb_hub_register_types)