blob: e35813d772222666419489233a9a507627d5c060 [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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydelle532b2e2016-01-26 18:17:12 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +020027#include "qemu/timer.h"
Gerd Hoffmann529f8f92012-03-23 15:42:58 +010028#include "trace.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020029#include "hw/qdev-properties.h"
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +010030#include "hw/usb.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020031#include "migration/vmstate.h"
Michael S. Tsirkin463581a2018-05-03 22:50:48 +030032#include "desc.h"
Gerd Hoffmannc24e4aa2013-03-20 11:40:02 +010033#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020034#include "qemu/module.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040035#include "qom/object.h"
bellard72899af2006-04-24 21:18:20 +000036
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +020037#define MAX_PORTS 8
bellard72899af2006-04-24 21:18:20 +000038
39typedef struct USBHubPort {
40 USBPort port;
41 uint16_t wPortStatus;
42 uint16_t wPortChange;
43} USBHubPort;
44
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040045struct USBHubState {
bellard72899af2006-04-24 21:18:20 +000046 USBDevice dev;
Gerd Hoffmann7567b512012-01-17 13:25:46 +010047 USBEndpoint *intr;
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +020048 uint32_t num_ports;
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +020049 bool port_power;
50 QEMUTimer *port_timer;
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +020051 USBHubPort ports[MAX_PORTS];
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040052};
bellard72899af2006-04-24 21:18:20 +000053
Gongleie81b13a2015-05-06 20:55:27 +080054#define TYPE_USB_HUB "usb-hub"
Eduardo Habkost80633962020-09-16 14:25:19 -040055OBJECT_DECLARE_SIMPLE_TYPE(USBHubState, USB_HUB)
Gongleie81b13a2015-05-06 20:55:27 +080056
bellard72899af2006-04-24 21:18:20 +000057#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)
64
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
75
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
81
82#define PORT_CONNECTION 0
83#define PORT_ENABLE 1
84#define PORT_SUSPEND 2
85#define PORT_OVERCURRENT 3
86#define PORT_RESET 4
87#define PORT_POWER 8
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
95#define PORT_TEST 21
96#define PORT_INDICATOR 22
97
98/* same as Linux kernel root hubs */
99
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100100enum {
101 STR_MANUFACTURER = 1,
102 STR_PRODUCT,
103 STR_SERIALNUMBER,
104};
105
106static const USBDescStrings desc_strings = {
Crístian Viana93bfef42012-05-30 00:35:51 -0300107 [STR_MANUFACTURER] = "QEMU",
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100108 [STR_PRODUCT] = "QEMU USB Hub",
109 [STR_SERIALNUMBER] = "314159",
110};
111
112static const USBDescIface desc_iface_hub = {
113 .bInterfaceNumber = 0,
114 .bNumEndpoints = 1,
115 .bInterfaceClass = USB_CLASS_HUB,
116 .eps = (USBDescEndpoint[]) {
117 {
118 .bEndpointAddress = USB_DIR_IN | 0x01,
119 .bmAttributes = USB_ENDPOINT_XFER_INT,
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200120 .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8),
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100121 .bInterval = 0xff,
122 },
123 }
124};
125
126static const USBDescDevice desc_device_hub = {
127 .bcdUSB = 0x0110,
128 .bDeviceClass = USB_CLASS_HUB,
129 .bMaxPacketSize0 = 8,
130 .bNumConfigurations = 1,
131 .confs = (USBDescConfig[]) {
132 {
133 .bNumInterfaces = 1,
134 .bConfigurationValue = 1,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200135 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
136 USB_CFG_ATT_WAKEUP,
Brad Hardsadd75082011-04-03 15:33:19 +1000137 .nif = 1,
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100138 .ifs = &desc_iface_hub,
139 },
140 },
141};
142
143static const USBDesc desc_hub = {
144 .id = {
Roy Tamdb803582011-09-15 11:25:47 +0800145 .idVendor = 0x0409,
146 .idProduct = 0x55aa,
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100147 .bcdDevice = 0x0101,
148 .iManufacturer = STR_MANUFACTURER,
149 .iProduct = STR_PRODUCT,
150 .iSerialNumber = STR_SERIALNUMBER,
151 },
152 .full = &desc_device_hub,
153 .str = desc_strings,
154};
155
bellard72899af2006-04-24 21:18:20 +0000156static const uint8_t qemu_hub_hub_descriptor[] =
157{
Paolo Bonzini7d374352018-12-13 23:37:37 +0100158 0x00, /* u8 bLength; patched in later */
159 0x29, /* u8 bDescriptorType; Hub-descriptor */
160 0x00, /* u8 bNbrPorts; (patched later) */
161 0x0a, /* u16 wHubCharacteristics; */
162 0x00, /* (per-port OC, no power switching) */
163 0x01, /* u8 bPwrOn2pwrGood; 2ms */
164 0x00 /* u8 bHubContrCurrent; 0 mA */
bellard985d1742006-04-30 21:53:59 +0000165
166 /* DeviceRemovable and PortPwrCtrlMask patched in later */
bellard72899af2006-04-24 21:18:20 +0000167};
168
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200169static bool usb_hub_port_change(USBHubPort *port, uint16_t status)
170{
171 bool notify = false;
172
173 if (status & 0x1f) {
174 port->wPortChange |= status;
175 notify = true;
176 }
177 return notify;
178}
179
180static bool usb_hub_port_set(USBHubPort *port, uint16_t status)
181{
182 if (port->wPortStatus & status) {
183 return false;
184 }
185 port->wPortStatus |= status;
186 return usb_hub_port_change(port, status);
187}
188
189static bool usb_hub_port_clear(USBHubPort *port, uint16_t status)
190{
191 if (!(port->wPortStatus & status)) {
192 return false;
193 }
194 port->wPortStatus &= ~status;
195 return usb_hub_port_change(port, status);
196}
197
Gerd Hoffmann638ac2d2019-05-24 09:03:09 +0200198static bool usb_hub_port_update(USBHubPort *port)
199{
200 bool notify = false;
201
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);
206 } else {
207 usb_hub_port_clear(port, PORT_STAT_LOW_SPEED);
208 }
209 }
210 return notify;
211}
212
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200213static void usb_hub_port_update_timer(void *opaque)
214{
215 USBHubState *s = opaque;
216 bool notify = false;
217 int i;
218
219 for (i = 0; i < s->num_ports; i++) {
220 notify |= usb_hub_port_update(&s->ports[i]);
221 }
222 if (notify) {
223 usb_wakeup(s->intr, 0);
224 }
225}
226
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100227static void usb_hub_attach(USBPort *port1)
bellard72899af2006-04-24 21:18:20 +0000228{
229 USBHubState *s = port1->opaque;
230 USBHubPort *port = &s->ports[port1->index];
ths3b46e622007-09-17 08:09:54 +0000231
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100232 trace_usb_hub_attach(s->dev.addr, port1->index + 1);
Gerd Hoffmann638ac2d2019-05-24 09:03:09 +0200233 usb_hub_port_update(port);
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100234 usb_wakeup(s->intr, 0);
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100235}
236
237static void usb_hub_detach(USBPort *port1)
238{
239 USBHubState *s = port1->opaque;
240 USBHubPort *port = &s->ports[port1->index];
241
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100242 trace_usb_hub_detach(s->dev.addr, port1->index + 1);
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100243 usb_wakeup(s->intr, 0);
Gerd Hoffmannbe35cbb2011-11-22 13:20:14 +0100244
Hans de Goede4706ab62011-06-24 12:31:11 +0200245 /* Let upstream know the device on this port is gone */
246 s->dev.port->ops->child_detach(s->dev.port, port1->dev);
247
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200248 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);
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100251 usb_wakeup(s->intr, 0);
bellard72899af2006-04-24 21:18:20 +0000252}
253
Hans de Goede4706ab62011-06-24 12:31:11 +0200254static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
255{
256 USBHubState *s = port1->opaque;
257
258 /* Pass along upstream */
259 s->dev.port->ops->child_detach(s->dev.port, child);
260}
261
Hans de Goeded47e59b2011-06-21 11:52:28 +0200262static void usb_hub_wakeup(USBPort *port1)
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100263{
Hans de Goeded47e59b2011-06-21 11:52:28 +0200264 USBHubState *s = port1->opaque;
265 USBHubPort *port = &s->ports[port1->index];
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100266
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200267 if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) {
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100268 usb_wakeup(s->intr, 0);
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100269 }
270}
271
Hans de Goeded47e59b2011-06-21 11:52:28 +0200272static void usb_hub_complete(USBPort *port, USBPacket *packet)
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100273{
Hans de Goeded47e59b2011-06-21 11:52:28 +0200274 USBHubState *s = port->opaque;
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100275
276 /*
277 * Just pass it along upstream for now.
278 *
Gerd Hoffmann80cf7cf2011-10-13 12:52:47 +0200279 * If we ever implement usb 2.0 split transactions this will
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100280 * become a little more complicated ...
Gerd Hoffmann80cf7cf2011-10-13 12:52:47 +0200281 *
282 * Can't use usb_packet_complete() here because packet->owner is
283 * cleared already, go call the ->complete() callback directly
284 * instead.
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100285 */
Gerd Hoffmann80cf7cf2011-10-13 12:52:47 +0200286 s->dev.port->ops->complete(s->dev.port, packet);
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100287}
288
Gerd Hoffmann06c75082012-01-10 17:08:13 +0100289static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
290{
Gongleie81b13a2015-05-06 20:55:27 +0800291 USBHubState *s = USB_HUB(dev);
Gerd Hoffmann06c75082012-01-10 17:08:13 +0100292 USBHubPort *port;
293 USBDevice *downstream;
294 int i;
295
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200296 for (i = 0; i < s->num_ports; i++) {
Gerd Hoffmann06c75082012-01-10 17:08:13 +0100297 port = &s->ports[i];
298 if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
299 continue;
300 }
301 downstream = usb_find_device(&port->port, addr);
302 if (downstream != NULL) {
303 return downstream;
304 }
305 }
306 return NULL;
307}
308
bellard059809e2006-07-19 18:06:15 +0000309static void usb_hub_handle_reset(USBDevice *dev)
bellard72899af2006-04-24 21:18:20 +0000310{
Gongleie81b13a2015-05-06 20:55:27 +0800311 USBHubState *s = USB_HUB(dev);
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100312 USBHubPort *port;
313 int i;
314
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100315 trace_usb_hub_reset(s->dev.addr);
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200316 for (i = 0; i < s->num_ports; i++) {
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100317 port = s->ports + i;
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200318 port->wPortStatus = 0;
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100319 port->wPortChange = 0;
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200320 usb_hub_port_set(port, PORT_STAT_POWER);
Gerd Hoffmann638ac2d2019-05-24 09:03:09 +0200321 usb_hub_port_update(port);
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100322 }
bellard72899af2006-04-24 21:18:20 +0000323}
324
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100325static const char *feature_name(int feature)
326{
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",
Gerd Hoffmannbdb88a82019-05-24 09:03:06 +0200336 [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",
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100341 [PORT_TEST] = "test",
342 [PORT_INDICATOR] = "indicator",
343 };
344 if (feature < 0 || feature >= ARRAY_SIZE(name)) {
345 return "?";
346 }
347 return name[feature] ?: "?";
348}
349
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100350static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
Hans de Goede007fd622011-02-02 16:33:13 +0100351 int request, int value, int index, int length, uint8_t *data)
bellard72899af2006-04-24 21:18:20 +0000352{
353 USBHubState *s = (USBHubState *)dev;
354 int ret;
355
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100356 trace_usb_hub_control(s->dev.addr, request, value, index, length);
357
Hans de Goede007fd622011-02-02 16:33:13 +0100358 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100359 if (ret >= 0) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100360 return;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100361 }
362
bellard72899af2006-04-24 21:18:20 +0000363 switch(request) {
bellard985d1742006-04-30 21:53:59 +0000364 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
365 if (value == 0 && index != 0x81) { /* clear ep halt */
366 goto fail;
367 }
bellard985d1742006-04-30 21:53:59 +0000368 break;
bellard72899af2006-04-24 21:18:20 +0000369 /* usb specific requests */
370 case GetHubStatus:
371 data[0] = 0;
372 data[1] = 0;
373 data[2] = 0;
374 data[3] = 0;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100375 p->actual_length = 4;
bellard72899af2006-04-24 21:18:20 +0000376 break;
377 case GetPortStatus:
378 {
379 unsigned int n = index - 1;
380 USBHubPort *port;
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200381 if (n >= s->num_ports) {
bellard72899af2006-04-24 21:18:20 +0000382 goto fail;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100383 }
bellard72899af2006-04-24 21:18:20 +0000384 port = &s->ports[n];
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100385 trace_usb_hub_get_port_status(s->dev.addr, index,
386 port->wPortStatus,
387 port->wPortChange);
bellard72899af2006-04-24 21:18:20 +0000388 data[0] = port->wPortStatus;
389 data[1] = port->wPortStatus >> 8;
390 data[2] = port->wPortChange;
391 data[3] = port->wPortChange >> 8;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100392 p->actual_length = 4;
bellard72899af2006-04-24 21:18:20 +0000393 }
394 break;
395 case SetHubFeature:
396 case ClearHubFeature:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100397 if (value != 0 && value != 1) {
bellard72899af2006-04-24 21:18:20 +0000398 goto fail;
399 }
bellard72899af2006-04-24 21:18:20 +0000400 break;
401 case SetPortFeature:
402 {
403 unsigned int n = index - 1;
404 USBHubPort *port;
405 USBDevice *dev;
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100406
407 trace_usb_hub_set_port_feature(s->dev.addr, index,
408 feature_name(value));
409
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200410 if (n >= s->num_ports) {
bellard72899af2006-04-24 21:18:20 +0000411 goto fail;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100412 }
bellard72899af2006-04-24 21:18:20 +0000413 port = &s->ports[n];
414 dev = port->port.dev;
415 switch(value) {
416 case PORT_SUSPEND:
417 port->wPortStatus |= PORT_STAT_SUSPEND;
418 break;
419 case PORT_RESET:
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200420 usb_hub_port_set(port, PORT_STAT_RESET);
421 usb_hub_port_clear(port, PORT_STAT_RESET);
Gerd Hoffmann3393bc12011-09-15 09:20:02 +0200422 if (dev && dev->attached) {
Gerd Hoffmannd28f4e22012-01-06 15:23:10 +0100423 usb_device_reset(dev);
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200424 usb_hub_port_set(port, PORT_STAT_ENABLE);
bellard72899af2006-04-24 21:18:20 +0000425 }
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200426 usb_wakeup(s->intr, 0);
bellard72899af2006-04-24 21:18:20 +0000427 break;
428 case PORT_POWER:
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200429 if (s->port_power) {
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); /* 5 ms */
433 }
bellard72899af2006-04-24 21:18:20 +0000434 break;
435 default:
436 goto fail;
437 }
bellard72899af2006-04-24 21:18:20 +0000438 }
439 break;
440 case ClearPortFeature:
441 {
442 unsigned int n = index - 1;
443 USBHubPort *port;
Blue Swirld4c4e6f2010-04-25 18:23:04 +0000444
Gerd Hoffmann529f8f92012-03-23 15:42:58 +0100445 trace_usb_hub_clear_port_feature(s->dev.addr, index,
446 feature_name(value));
447
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200448 if (n >= s->num_ports) {
bellard72899af2006-04-24 21:18:20 +0000449 goto fail;
Gerd Hoffmann062651c2010-11-26 13:13:22 +0100450 }
bellard72899af2006-04-24 21:18:20 +0000451 port = &s->ports[n];
bellard72899af2006-04-24 21:18:20 +0000452 switch(value) {
453 case PORT_ENABLE:
454 port->wPortStatus &= ~PORT_STAT_ENABLE;
455 break;
456 case PORT_C_ENABLE:
457 port->wPortChange &= ~PORT_STAT_C_ENABLE;
458 break;
459 case PORT_SUSPEND:
Gerd Hoffmann868a4202019-05-24 09:03:08 +0200460 usb_hub_port_clear(port, PORT_STAT_SUSPEND);
bellard72899af2006-04-24 21:18:20 +0000461 break;
462 case PORT_C_SUSPEND:
463 port->wPortChange &= ~PORT_STAT_C_SUSPEND;
464 break;
465 case PORT_C_CONNECTION:
466 port->wPortChange &= ~PORT_STAT_C_CONNECTION;
467 break;
468 case PORT_C_OVERCURRENT:
469 port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
470 break;
471 case PORT_C_RESET:
472 port->wPortChange &= ~PORT_STAT_C_RESET;
473 break;
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200474 case PORT_POWER:
475 if (s->port_power) {
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;
481 }
bellard72899af2006-04-24 21:18:20 +0000482 default:
483 goto fail;
484 }
bellard72899af2006-04-24 21:18:20 +0000485 }
486 break;
487 case GetHubDescriptor:
bellard985d1742006-04-30 21:53:59 +0000488 {
489 unsigned int n, limit, var_hub_size = 0;
ths5fafdf22007-09-16 21:08:06 +0000490 memcpy(data, qemu_hub_hub_descriptor,
bellard985d1742006-04-30 21:53:59 +0000491 sizeof(qemu_hub_hub_descriptor));
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200492 data[2] = s->num_ports;
bellard985d1742006-04-30 21:53:59 +0000493
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200494 if (s->port_power) {
495 data[3] &= ~0x03;
496 data[3] |= 0x01;
497 }
498
bellard985d1742006-04-30 21:53:59 +0000499 /* fill DeviceRemovable bits */
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200500 limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7;
bellard985d1742006-04-30 21:53:59 +0000501 for (n = 7; n < limit; n++) {
502 data[n] = 0x00;
503 var_hub_size++;
504 }
505
506 /* fill PortPwrCtrlMask bits */
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200507 limit = limit + DIV_ROUND_UP(s->num_ports, 8);
bellard985d1742006-04-30 21:53:59 +0000508 for (;n < limit; n++) {
509 data[n] = 0xff;
510 var_hub_size++;
511 }
512
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100513 p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
514 data[0] = p->actual_length;
bellard985d1742006-04-30 21:53:59 +0000515 break;
516 }
bellard72899af2006-04-24 21:18:20 +0000517 default:
518 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100519 p->status = USB_RET_STALL;
bellard72899af2006-04-24 21:18:20 +0000520 break;
521 }
bellard72899af2006-04-24 21:18:20 +0000522}
523
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100524static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
bellard72899af2006-04-24 21:18:20 +0000525{
526 USBHubState *s = (USBHubState *)dev;
bellard72899af2006-04-24 21:18:20 +0000527
pbrook4d611c92006-08-12 01:04:27 +0000528 switch(p->pid) {
bellard72899af2006-04-24 21:18:20 +0000529 case USB_TOKEN_IN:
Gerd Hoffmann079d0b72012-01-12 13:23:01 +0100530 if (p->ep->nr == 1) {
bellard72899af2006-04-24 21:18:20 +0000531 USBHubPort *port;
532 unsigned int status;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200533 uint8_t buf[4];
bellard72899af2006-04-24 21:18:20 +0000534 int i, n;
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200535 n = DIV_ROUND_UP(s->num_ports + 1, 8);
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200536 if (p->iov.size == 1) { /* FreeBSD workaround */
bellard985d1742006-04-30 21:53:59 +0000537 n = 1;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200538 } else if (n > p->iov.size) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100539 p->status = USB_RET_BABBLE;
540 return;
bellard985d1742006-04-30 21:53:59 +0000541 }
bellard72899af2006-04-24 21:18:20 +0000542 status = 0;
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200543 for (i = 0; i < s->num_ports; i++) {
bellard72899af2006-04-24 21:18:20 +0000544 port = &s->ports[i];
Gerd Hoffmannbdebd6e2013-08-27 17:00:04 +0200545 if (port->wPortChange)
bellard72899af2006-04-24 21:18:20 +0000546 status |= (1 << (i + 1));
547 }
548 if (status != 0) {
Gerd Hoffmannb8cbc132013-08-27 16:59:37 +0200549 trace_usb_hub_status_report(s->dev.addr, status);
bellard72899af2006-04-24 21:18:20 +0000550 for(i = 0; i < n; i++) {
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200551 buf[i] = status >> (8 * i);
bellard72899af2006-04-24 21:18:20 +0000552 }
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200553 usb_packet_copy(p, buf, n);
bellard72899af2006-04-24 21:18:20 +0000554 } else {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100555 p->status = USB_RET_NAK; /* usb11 11.13.1 */
bellard72899af2006-04-24 21:18:20 +0000556 }
557 } else {
558 goto fail;
559 }
560 break;
561 case USB_TOKEN_OUT:
562 default:
563 fail:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100564 p->status = USB_RET_STALL;
bellard72899af2006-04-24 21:18:20 +0000565 break;
566 }
bellard72899af2006-04-24 21:18:20 +0000567}
568
Markus Armbrusterb69c3c22020-05-05 17:29:24 +0200569static void usb_hub_unrealize(USBDevice *dev)
bellard059809e2006-07-19 18:06:15 +0000570{
571 USBHubState *s = (USBHubState *)dev;
Gerd Hoffmanna8e662b2009-09-25 21:42:39 +0200572 int i;
bellard059809e2006-07-19 18:06:15 +0000573
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200574 for (i = 0; i < s->num_ports; i++) {
Gerd Hoffmanna8e662b2009-09-25 21:42:39 +0200575 usb_unregister_port(usb_bus_from_device(dev),
576 &s->ports[i].port);
577 }
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200578
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200579 timer_free(s->port_timer);
bellard059809e2006-07-19 18:06:15 +0000580}
581
Gerd Hoffmann0d86d2b2010-12-01 11:08:44 +0100582static USBPortOps usb_hub_port_ops = {
583 .attach = usb_hub_attach,
Gerd Hoffmann618c1692010-12-01 11:27:05 +0100584 .detach = usb_hub_detach,
Hans de Goede4706ab62011-06-24 12:31:11 +0200585 .child_detach = usb_hub_child_detach,
Gerd Hoffmann34239c72010-12-15 12:26:59 +0100586 .wakeup = usb_hub_wakeup,
Gerd Hoffmann13a9a0d2010-12-16 17:03:44 +0100587 .complete = usb_hub_complete,
Gerd Hoffmann0d86d2b2010-12-01 11:08:44 +0100588};
589
Gongleif3f8c452014-09-19 14:48:28 +0800590static void usb_hub_realize(USBDevice *dev, Error **errp)
bellard72899af2006-04-24 21:18:20 +0000591{
Gongleie81b13a2015-05-06 20:55:27 +0800592 USBHubState *s = USB_HUB(dev);
bellard72899af2006-04-24 21:18:20 +0000593 USBHubPort *port;
594 int i;
595
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200596 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);
599 return;
600 }
601
Gerd Hoffmannc24e4aa2013-03-20 11:40:02 +0100602 if (dev->port->hubcount == 5) {
Gongleif3f8c452014-09-19 14:48:28 +0800603 error_setg(errp, "usb hub chain too deep");
604 return;
Gerd Hoffmannc24e4aa2013-03-20 11:40:02 +0100605 }
606
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +0200607 usb_desc_create_serial(dev);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100608 usb_desc_init(dev);
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200609 s->port_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
610 usb_hub_port_update_timer, s);
Gerd Hoffmann7567b512012-01-17 13:25:46 +0100611 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200612 for (i = 0; i < s->num_ports; i++) {
bellard72899af2006-04-24 21:18:20 +0000613 port = &s->ports[i];
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200614 usb_register_port(usb_bus_from_device(dev),
Gerd Hoffmannace13182011-01-12 11:34:50 +0100615 &port->port, s, i, &usb_hub_port_ops,
Gerd Hoffmann843d4e02010-12-03 17:30:13 +0100616 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
Gerd Hoffmann891fb2c2011-09-01 13:56:37 +0200617 usb_port_location(&port->port, dev->port, i+1);
bellard72899af2006-04-24 21:18:20 +0000618 }
Gerd Hoffmann20d183b2011-11-23 13:31:08 +0100619 usb_hub_handle_reset(dev);
bellard72899af2006-04-24 21:18:20 +0000620}
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200621
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100622static const VMStateDescription vmstate_usb_hub_port = {
623 .name = "usb-hub-port",
624 .version_id = 1,
625 .minimum_version_id = 1,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200626 .fields = (VMStateField[]) {
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100627 VMSTATE_UINT16(wPortStatus, USBHubPort),
628 VMSTATE_UINT16(wPortChange, USBHubPort),
629 VMSTATE_END_OF_LIST()
630 }
631};
632
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200633static bool usb_hub_port_timer_needed(void *opaque)
634{
635 USBHubState *s = opaque;
636
637 return s->port_power;
638}
639
640static const VMStateDescription vmstate_usb_hub_port_timer = {
641 .name = "usb-hub/port-timer",
642 .version_id = 1,
643 .minimum_version_id = 1,
644 .needed = usb_hub_port_timer_needed,
645 .fields = (VMStateField[]) {
646 VMSTATE_TIMER_PTR(port_timer, USBHubState),
647 VMSTATE_END_OF_LIST()
648 },
649};
650
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100651static const VMStateDescription vmstate_usb_hub = {
652 .name = "usb-hub",
653 .version_id = 1,
654 .minimum_version_id = 1,
Juan Quintela6e3d6522014-04-16 13:31:26 +0200655 .fields = (VMStateField[]) {
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100656 VMSTATE_USB_DEVICE(dev, USBHubState),
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200657 VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0,
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100658 vmstate_usb_hub_port, USBHubPort),
659 VMSTATE_END_OF_LIST()
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200660 },
661 .subsections = (const VMStateDescription * []) {
662 &vmstate_usb_hub_port_timer,
663 NULL
Gerd Hoffmannd1550092010-12-15 12:45:24 +0100664 }
665};
666
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200667static Property usb_hub_properties[] = {
668 DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8),
Gerd Hoffmann1cc403e2019-05-24 09:03:10 +0200669 DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false),
Gerd Hoffmann9d84bb02019-05-24 09:03:07 +0200670 DEFINE_PROP_END_OF_LIST(),
671};
672
Anthony Liguori39bffca2011-12-07 21:34:16 -0600673static void usb_hub_class_initfn(ObjectClass *klass, void *data)
Anthony Liguori62aed762011-12-15 14:53:10 -0600674{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600675 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600676 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
677
Gongleif3f8c452014-09-19 14:48:28 +0800678 uc->realize = usb_hub_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600679 uc->product_desc = "QEMU USB Hub";
680 uc->usb_desc = &desc_hub;
Gerd Hoffmann06c75082012-01-10 17:08:13 +0100681 uc->find_device = usb_hub_find_device;
Anthony Liguori62aed762011-12-15 14:53:10 -0600682 uc->handle_reset = usb_hub_handle_reset;
683 uc->handle_control = usb_hub_handle_control;
684 uc->handle_data = usb_hub_handle_data;
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400685 uc->unrealize = usb_hub_unrealize;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300686 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600687 dc->fw_name = "hub";
688 dc->vmsd = &vmstate_usb_hub;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400689 device_class_set_props(dc, usb_hub_properties);
Anthony Liguori62aed762011-12-15 14:53:10 -0600690}
691
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100692static const TypeInfo hub_info = {
Gongleie81b13a2015-05-06 20:55:27 +0800693 .name = TYPE_USB_HUB,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600694 .parent = TYPE_USB_DEVICE,
695 .instance_size = sizeof(USBHubState),
696 .class_init = usb_hub_class_initfn,
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200697};
698
Andreas Färber83f7d432012-02-09 15:20:55 +0100699static void usb_hub_register_types(void)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200700{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600701 type_register_static(&hub_info);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200702}
Andreas Färber83f7d432012-02-09 15:20:55 +0100703
704type_init(usb_hub_register_types)