bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU USB HUB emulation |
| 3 | * |
| 4 | * Copyright (c) 2005 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 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: |
| 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 Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 24 | |
Peter Maydell | e532b2e | 2016-01-26 18:17:12 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 27 | #include "qemu/timer.h" |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 28 | #include "trace.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 29 | #include "hw/qdev-properties.h" |
Gerd Hoffmann | f1ae32a | 2012-03-07 14:55:18 +0100 | [diff] [blame] | 30 | #include "hw/usb.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 31 | #include "migration/vmstate.h" |
Michael S. Tsirkin | 463581a | 2018-05-03 22:50:48 +0300 | [diff] [blame] | 32 | #include "desc.h" |
Gerd Hoffmann | c24e4aa | 2013-03-20 11:40:02 +0100 | [diff] [blame] | 33 | #include "qemu/error-report.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 34 | #include "qemu/module.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 35 | #include "qom/object.h" |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 36 | |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 37 | #define MAX_PORTS 8 |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 38 | |
| 39 | typedef struct USBHubPort { |
| 40 | USBPort port; |
| 41 | uint16_t wPortStatus; |
| 42 | uint16_t wPortChange; |
| 43 | } USBHubPort; |
| 44 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 45 | struct USBHubState { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 46 | USBDevice dev; |
Gerd Hoffmann | 7567b51 | 2012-01-17 13:25:46 +0100 | [diff] [blame] | 47 | USBEndpoint *intr; |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 48 | uint32_t num_ports; |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 49 | bool port_power; |
| 50 | QEMUTimer *port_timer; |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 51 | USBHubPort ports[MAX_PORTS]; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 52 | }; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 53 | |
Gonglei | e81b13a | 2015-05-06 20:55:27 +0800 | [diff] [blame] | 54 | #define TYPE_USB_HUB "usb-hub" |
Eduardo Habkost | 8063396 | 2020-09-16 14:25:19 -0400 | [diff] [blame] | 55 | OBJECT_DECLARE_SIMPLE_TYPE(USBHubState, USB_HUB) |
Gonglei | e81b13a | 2015-05-06 20:55:27 +0800 | [diff] [blame] | 56 | |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 57 | #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 Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 100 | enum { |
| 101 | STR_MANUFACTURER = 1, |
| 102 | STR_PRODUCT, |
| 103 | STR_SERIALNUMBER, |
| 104 | }; |
| 105 | |
| 106 | static const USBDescStrings desc_strings = { |
Crístian Viana | 93bfef4 | 2012-05-30 00:35:51 -0300 | [diff] [blame] | 107 | [STR_MANUFACTURER] = "QEMU", |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 108 | [STR_PRODUCT] = "QEMU USB Hub", |
| 109 | [STR_SERIALNUMBER] = "314159", |
| 110 | }; |
| 111 | |
| 112 | static 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 Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 120 | .wMaxPacketSize = 1 + DIV_ROUND_UP(MAX_PORTS, 8), |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 121 | .bInterval = 0xff, |
| 122 | }, |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | static 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 Koukousoulas | bd93976 | 2013-12-16 09:42:49 +0200 | [diff] [blame] | 135 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER | |
| 136 | USB_CFG_ATT_WAKEUP, |
Brad Hards | add7508 | 2011-04-03 15:33:19 +1000 | [diff] [blame] | 137 | .nif = 1, |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 138 | .ifs = &desc_iface_hub, |
| 139 | }, |
| 140 | }, |
| 141 | }; |
| 142 | |
| 143 | static const USBDesc desc_hub = { |
| 144 | .id = { |
Roy Tam | db80358 | 2011-09-15 11:25:47 +0800 | [diff] [blame] | 145 | .idVendor = 0x0409, |
| 146 | .idProduct = 0x55aa, |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 147 | .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 | |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 156 | static const uint8_t qemu_hub_hub_descriptor[] = |
| 157 | { |
Paolo Bonzini | 7d37435 | 2018-12-13 23:37:37 +0100 | [diff] [blame] | 158 | 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 */ |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 165 | |
| 166 | /* DeviceRemovable and PortPwrCtrlMask patched in later */ |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Gerd Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 169 | static 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 | |
| 180 | static 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 | |
| 189 | static 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 Hoffmann | 638ac2d | 2019-05-24 09:03:09 +0200 | [diff] [blame] | 198 | static 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 Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 213 | static 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 Hoffmann | 618c169 | 2010-12-01 11:27:05 +0100 | [diff] [blame] | 227 | static void usb_hub_attach(USBPort *port1) |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 228 | { |
| 229 | USBHubState *s = port1->opaque; |
| 230 | USBHubPort *port = &s->ports[port1->index]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 231 | |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 232 | trace_usb_hub_attach(s->dev.addr, port1->index + 1); |
Gerd Hoffmann | 638ac2d | 2019-05-24 09:03:09 +0200 | [diff] [blame] | 233 | usb_hub_port_update(port); |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 234 | usb_wakeup(s->intr, 0); |
Gerd Hoffmann | 618c169 | 2010-12-01 11:27:05 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void usb_hub_detach(USBPort *port1) |
| 238 | { |
| 239 | USBHubState *s = port1->opaque; |
| 240 | USBHubPort *port = &s->ports[port1->index]; |
| 241 | |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 242 | trace_usb_hub_detach(s->dev.addr, port1->index + 1); |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 243 | usb_wakeup(s->intr, 0); |
Gerd Hoffmann | be35cbb | 2011-11-22 13:20:14 +0100 | [diff] [blame] | 244 | |
Hans de Goede | 4706ab6 | 2011-06-24 12:31:11 +0200 | [diff] [blame] | 245 | /* Let upstream know the device on this port is gone */ |
| 246 | s->dev.port->ops->child_detach(s->dev.port, port1->dev); |
| 247 | |
Gerd Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 248 | 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 Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 251 | usb_wakeup(s->intr, 0); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Hans de Goede | 4706ab6 | 2011-06-24 12:31:11 +0200 | [diff] [blame] | 254 | static 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 Goede | d47e59b | 2011-06-21 11:52:28 +0200 | [diff] [blame] | 262 | static void usb_hub_wakeup(USBPort *port1) |
Gerd Hoffmann | 34239c7 | 2010-12-15 12:26:59 +0100 | [diff] [blame] | 263 | { |
Hans de Goede | d47e59b | 2011-06-21 11:52:28 +0200 | [diff] [blame] | 264 | USBHubState *s = port1->opaque; |
| 265 | USBHubPort *port = &s->ports[port1->index]; |
Gerd Hoffmann | 34239c7 | 2010-12-15 12:26:59 +0100 | [diff] [blame] | 266 | |
Gerd Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 267 | if (usb_hub_port_clear(port, PORT_STAT_SUSPEND)) { |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 268 | usb_wakeup(s->intr, 0); |
Gerd Hoffmann | 34239c7 | 2010-12-15 12:26:59 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Hans de Goede | d47e59b | 2011-06-21 11:52:28 +0200 | [diff] [blame] | 272 | static void usb_hub_complete(USBPort *port, USBPacket *packet) |
Gerd Hoffmann | 13a9a0d | 2010-12-16 17:03:44 +0100 | [diff] [blame] | 273 | { |
Hans de Goede | d47e59b | 2011-06-21 11:52:28 +0200 | [diff] [blame] | 274 | USBHubState *s = port->opaque; |
Gerd Hoffmann | 13a9a0d | 2010-12-16 17:03:44 +0100 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * Just pass it along upstream for now. |
| 278 | * |
Gerd Hoffmann | 80cf7cf | 2011-10-13 12:52:47 +0200 | [diff] [blame] | 279 | * If we ever implement usb 2.0 split transactions this will |
Gerd Hoffmann | 13a9a0d | 2010-12-16 17:03:44 +0100 | [diff] [blame] | 280 | * become a little more complicated ... |
Gerd Hoffmann | 80cf7cf | 2011-10-13 12:52:47 +0200 | [diff] [blame] | 281 | * |
| 282 | * Can't use usb_packet_complete() here because packet->owner is |
| 283 | * cleared already, go call the ->complete() callback directly |
| 284 | * instead. |
Gerd Hoffmann | 13a9a0d | 2010-12-16 17:03:44 +0100 | [diff] [blame] | 285 | */ |
Gerd Hoffmann | 80cf7cf | 2011-10-13 12:52:47 +0200 | [diff] [blame] | 286 | s->dev.port->ops->complete(s->dev.port, packet); |
Gerd Hoffmann | 13a9a0d | 2010-12-16 17:03:44 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Gerd Hoffmann | 06c7508 | 2012-01-10 17:08:13 +0100 | [diff] [blame] | 289 | static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr) |
| 290 | { |
Gonglei | e81b13a | 2015-05-06 20:55:27 +0800 | [diff] [blame] | 291 | USBHubState *s = USB_HUB(dev); |
Gerd Hoffmann | 06c7508 | 2012-01-10 17:08:13 +0100 | [diff] [blame] | 292 | USBHubPort *port; |
| 293 | USBDevice *downstream; |
| 294 | int i; |
| 295 | |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 296 | for (i = 0; i < s->num_ports; i++) { |
Gerd Hoffmann | 06c7508 | 2012-01-10 17:08:13 +0100 | [diff] [blame] | 297 | 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 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 309 | static void usb_hub_handle_reset(USBDevice *dev) |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 310 | { |
Gonglei | e81b13a | 2015-05-06 20:55:27 +0800 | [diff] [blame] | 311 | USBHubState *s = USB_HUB(dev); |
Gerd Hoffmann | 20d183b | 2011-11-23 13:31:08 +0100 | [diff] [blame] | 312 | USBHubPort *port; |
| 313 | int i; |
| 314 | |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 315 | trace_usb_hub_reset(s->dev.addr); |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 316 | for (i = 0; i < s->num_ports; i++) { |
Gerd Hoffmann | 20d183b | 2011-11-23 13:31:08 +0100 | [diff] [blame] | 317 | port = s->ports + i; |
Gerd Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 318 | port->wPortStatus = 0; |
Gerd Hoffmann | 20d183b | 2011-11-23 13:31:08 +0100 | [diff] [blame] | 319 | port->wPortChange = 0; |
Gerd Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 320 | usb_hub_port_set(port, PORT_STAT_POWER); |
Gerd Hoffmann | 638ac2d | 2019-05-24 09:03:09 +0200 | [diff] [blame] | 321 | usb_hub_port_update(port); |
Gerd Hoffmann | 20d183b | 2011-11-23 13:31:08 +0100 | [diff] [blame] | 322 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 325 | static 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 Hoffmann | bdb88a8 | 2019-05-24 09:03:06 +0200 | [diff] [blame] | 336 | [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 Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 341 | [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 Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 350 | static void usb_hub_handle_control(USBDevice *dev, USBPacket *p, |
Hans de Goede | 007fd62 | 2011-02-02 16:33:13 +0100 | [diff] [blame] | 351 | int request, int value, int index, int length, uint8_t *data) |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 352 | { |
| 353 | USBHubState *s = (USBHubState *)dev; |
| 354 | int ret; |
| 355 | |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 356 | trace_usb_hub_control(s->dev.addr, request, value, index, length); |
| 357 | |
Hans de Goede | 007fd62 | 2011-02-02 16:33:13 +0100 | [diff] [blame] | 358 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 359 | if (ret >= 0) { |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 360 | return; |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 361 | } |
| 362 | |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 363 | switch(request) { |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 364 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
| 365 | if (value == 0 && index != 0x81) { /* clear ep halt */ |
| 366 | goto fail; |
| 367 | } |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 368 | break; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 369 | /* usb specific requests */ |
| 370 | case GetHubStatus: |
| 371 | data[0] = 0; |
| 372 | data[1] = 0; |
| 373 | data[2] = 0; |
| 374 | data[3] = 0; |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 375 | p->actual_length = 4; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 376 | break; |
| 377 | case GetPortStatus: |
| 378 | { |
| 379 | unsigned int n = index - 1; |
| 380 | USBHubPort *port; |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 381 | if (n >= s->num_ports) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 382 | goto fail; |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 383 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 384 | port = &s->ports[n]; |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 385 | trace_usb_hub_get_port_status(s->dev.addr, index, |
| 386 | port->wPortStatus, |
| 387 | port->wPortChange); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 388 | data[0] = port->wPortStatus; |
| 389 | data[1] = port->wPortStatus >> 8; |
| 390 | data[2] = port->wPortChange; |
| 391 | data[3] = port->wPortChange >> 8; |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 392 | p->actual_length = 4; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 393 | } |
| 394 | break; |
| 395 | case SetHubFeature: |
| 396 | case ClearHubFeature: |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 397 | if (value != 0 && value != 1) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 398 | goto fail; |
| 399 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 400 | break; |
| 401 | case SetPortFeature: |
| 402 | { |
| 403 | unsigned int n = index - 1; |
| 404 | USBHubPort *port; |
| 405 | USBDevice *dev; |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 406 | |
| 407 | trace_usb_hub_set_port_feature(s->dev.addr, index, |
| 408 | feature_name(value)); |
| 409 | |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 410 | if (n >= s->num_ports) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 411 | goto fail; |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 412 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 413 | 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 Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 420 | usb_hub_port_set(port, PORT_STAT_RESET); |
| 421 | usb_hub_port_clear(port, PORT_STAT_RESET); |
Gerd Hoffmann | 3393bc1 | 2011-09-15 09:20:02 +0200 | [diff] [blame] | 422 | if (dev && dev->attached) { |
Gerd Hoffmann | d28f4e2 | 2012-01-06 15:23:10 +0100 | [diff] [blame] | 423 | usb_device_reset(dev); |
Gerd Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 424 | usb_hub_port_set(port, PORT_STAT_ENABLE); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 425 | } |
Gerd Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 426 | usb_wakeup(s->intr, 0); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 427 | break; |
| 428 | case PORT_POWER: |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 429 | 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 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 434 | break; |
| 435 | default: |
| 436 | goto fail; |
| 437 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 438 | } |
| 439 | break; |
| 440 | case ClearPortFeature: |
| 441 | { |
| 442 | unsigned int n = index - 1; |
| 443 | USBHubPort *port; |
Blue Swirl | d4c4e6f | 2010-04-25 18:23:04 +0000 | [diff] [blame] | 444 | |
Gerd Hoffmann | 529f8f9 | 2012-03-23 15:42:58 +0100 | [diff] [blame] | 445 | trace_usb_hub_clear_port_feature(s->dev.addr, index, |
| 446 | feature_name(value)); |
| 447 | |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 448 | if (n >= s->num_ports) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 449 | goto fail; |
Gerd Hoffmann | 062651c | 2010-11-26 13:13:22 +0100 | [diff] [blame] | 450 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 451 | port = &s->ports[n]; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 452 | 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 Hoffmann | 868a420 | 2019-05-24 09:03:08 +0200 | [diff] [blame] | 460 | usb_hub_port_clear(port, PORT_STAT_SUSPEND); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 461 | 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 Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 474 | 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 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 482 | default: |
| 483 | goto fail; |
| 484 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 485 | } |
| 486 | break; |
| 487 | case GetHubDescriptor: |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 488 | { |
| 489 | unsigned int n, limit, var_hub_size = 0; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 490 | memcpy(data, qemu_hub_hub_descriptor, |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 491 | sizeof(qemu_hub_hub_descriptor)); |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 492 | data[2] = s->num_ports; |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 493 | |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 494 | if (s->port_power) { |
| 495 | data[3] &= ~0x03; |
| 496 | data[3] |= 0x01; |
| 497 | } |
| 498 | |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 499 | /* fill DeviceRemovable bits */ |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 500 | limit = DIV_ROUND_UP(s->num_ports + 1, 8) + 7; |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 501 | for (n = 7; n < limit; n++) { |
| 502 | data[n] = 0x00; |
| 503 | var_hub_size++; |
| 504 | } |
| 505 | |
| 506 | /* fill PortPwrCtrlMask bits */ |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 507 | limit = limit + DIV_ROUND_UP(s->num_ports, 8); |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 508 | for (;n < limit; n++) { |
| 509 | data[n] = 0xff; |
| 510 | var_hub_size++; |
| 511 | } |
| 512 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 513 | p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size; |
| 514 | data[0] = p->actual_length; |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 517 | default: |
| 518 | fail: |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 519 | p->status = USB_RET_STALL; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 520 | break; |
| 521 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 524 | static void usb_hub_handle_data(USBDevice *dev, USBPacket *p) |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 525 | { |
| 526 | USBHubState *s = (USBHubState *)dev; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 527 | |
pbrook | 4d611c9 | 2006-08-12 01:04:27 +0000 | [diff] [blame] | 528 | switch(p->pid) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 529 | case USB_TOKEN_IN: |
Gerd Hoffmann | 079d0b7 | 2012-01-12 13:23:01 +0100 | [diff] [blame] | 530 | if (p->ep->nr == 1) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 531 | USBHubPort *port; |
| 532 | unsigned int status; |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 533 | uint8_t buf[4]; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 534 | int i, n; |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 535 | n = DIV_ROUND_UP(s->num_ports + 1, 8); |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 536 | if (p->iov.size == 1) { /* FreeBSD workaround */ |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 537 | n = 1; |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 538 | } else if (n > p->iov.size) { |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 539 | p->status = USB_RET_BABBLE; |
| 540 | return; |
bellard | 985d174 | 2006-04-30 21:53:59 +0000 | [diff] [blame] | 541 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 542 | status = 0; |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 543 | for (i = 0; i < s->num_ports; i++) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 544 | port = &s->ports[i]; |
Gerd Hoffmann | bdebd6e | 2013-08-27 17:00:04 +0200 | [diff] [blame] | 545 | if (port->wPortChange) |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 546 | status |= (1 << (i + 1)); |
| 547 | } |
| 548 | if (status != 0) { |
Gerd Hoffmann | b8cbc13 | 2013-08-27 16:59:37 +0200 | [diff] [blame] | 549 | trace_usb_hub_status_report(s->dev.addr, status); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 550 | for(i = 0; i < n; i++) { |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 551 | buf[i] = status >> (8 * i); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 552 | } |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 553 | usb_packet_copy(p, buf, n); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 554 | } else { |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 555 | p->status = USB_RET_NAK; /* usb11 11.13.1 */ |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 556 | } |
| 557 | } else { |
| 558 | goto fail; |
| 559 | } |
| 560 | break; |
| 561 | case USB_TOKEN_OUT: |
| 562 | default: |
| 563 | fail: |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 564 | p->status = USB_RET_STALL; |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 565 | break; |
| 566 | } |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 569 | static void usb_hub_unrealize(USBDevice *dev) |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 570 | { |
| 571 | USBHubState *s = (USBHubState *)dev; |
Gerd Hoffmann | a8e662b | 2009-09-25 21:42:39 +0200 | [diff] [blame] | 572 | int i; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 573 | |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 574 | for (i = 0; i < s->num_ports; i++) { |
Gerd Hoffmann | a8e662b | 2009-09-25 21:42:39 +0200 | [diff] [blame] | 575 | usb_unregister_port(usb_bus_from_device(dev), |
| 576 | &s->ports[i].port); |
| 577 | } |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 578 | |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 579 | timer_free(s->port_timer); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Gerd Hoffmann | 0d86d2b | 2010-12-01 11:08:44 +0100 | [diff] [blame] | 582 | static USBPortOps usb_hub_port_ops = { |
| 583 | .attach = usb_hub_attach, |
Gerd Hoffmann | 618c169 | 2010-12-01 11:27:05 +0100 | [diff] [blame] | 584 | .detach = usb_hub_detach, |
Hans de Goede | 4706ab6 | 2011-06-24 12:31:11 +0200 | [diff] [blame] | 585 | .child_detach = usb_hub_child_detach, |
Gerd Hoffmann | 34239c7 | 2010-12-15 12:26:59 +0100 | [diff] [blame] | 586 | .wakeup = usb_hub_wakeup, |
Gerd Hoffmann | 13a9a0d | 2010-12-16 17:03:44 +0100 | [diff] [blame] | 587 | .complete = usb_hub_complete, |
Gerd Hoffmann | 0d86d2b | 2010-12-01 11:08:44 +0100 | [diff] [blame] | 588 | }; |
| 589 | |
Gonglei | f3f8c45 | 2014-09-19 14:48:28 +0800 | [diff] [blame] | 590 | static void usb_hub_realize(USBDevice *dev, Error **errp) |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 591 | { |
Gonglei | e81b13a | 2015-05-06 20:55:27 +0800 | [diff] [blame] | 592 | USBHubState *s = USB_HUB(dev); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 593 | USBHubPort *port; |
| 594 | int i; |
| 595 | |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 596 | 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 Hoffmann | c24e4aa | 2013-03-20 11:40:02 +0100 | [diff] [blame] | 602 | if (dev->port->hubcount == 5) { |
Gonglei | f3f8c45 | 2014-09-19 14:48:28 +0800 | [diff] [blame] | 603 | error_setg(errp, "usb hub chain too deep"); |
| 604 | return; |
Gerd Hoffmann | c24e4aa | 2013-03-20 11:40:02 +0100 | [diff] [blame] | 605 | } |
| 606 | |
Gerd Hoffmann | 9d55d1a | 2012-04-20 12:33:30 +0200 | [diff] [blame] | 607 | usb_desc_create_serial(dev); |
Gerd Hoffmann | a980a06 | 2010-11-26 20:20:41 +0100 | [diff] [blame] | 608 | usb_desc_init(dev); |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 609 | s->port_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
| 610 | usb_hub_port_update_timer, s); |
Gerd Hoffmann | 7567b51 | 2012-01-17 13:25:46 +0100 | [diff] [blame] | 611 | s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1); |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 612 | for (i = 0; i < s->num_ports; i++) { |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 613 | port = &s->ports[i]; |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 614 | usb_register_port(usb_bus_from_device(dev), |
Gerd Hoffmann | ace1318 | 2011-01-12 11:34:50 +0100 | [diff] [blame] | 615 | &port->port, s, i, &usb_hub_port_ops, |
Gerd Hoffmann | 843d4e0 | 2010-12-03 17:30:13 +0100 | [diff] [blame] | 616 | USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); |
Gerd Hoffmann | 891fb2c | 2011-09-01 13:56:37 +0200 | [diff] [blame] | 617 | usb_port_location(&port->port, dev->port, i+1); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 618 | } |
Gerd Hoffmann | 20d183b | 2011-11-23 13:31:08 +0100 | [diff] [blame] | 619 | usb_hub_handle_reset(dev); |
bellard | 72899af | 2006-04-24 21:18:20 +0000 | [diff] [blame] | 620 | } |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 621 | |
Gerd Hoffmann | d155009 | 2010-12-15 12:45:24 +0100 | [diff] [blame] | 622 | static const VMStateDescription vmstate_usb_hub_port = { |
| 623 | .name = "usb-hub-port", |
| 624 | .version_id = 1, |
| 625 | .minimum_version_id = 1, |
Juan Quintela | 6e3d652 | 2014-04-16 13:31:26 +0200 | [diff] [blame] | 626 | .fields = (VMStateField[]) { |
Gerd Hoffmann | d155009 | 2010-12-15 12:45:24 +0100 | [diff] [blame] | 627 | VMSTATE_UINT16(wPortStatus, USBHubPort), |
| 628 | VMSTATE_UINT16(wPortChange, USBHubPort), |
| 629 | VMSTATE_END_OF_LIST() |
| 630 | } |
| 631 | }; |
| 632 | |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 633 | static bool usb_hub_port_timer_needed(void *opaque) |
| 634 | { |
| 635 | USBHubState *s = opaque; |
| 636 | |
| 637 | return s->port_power; |
| 638 | } |
| 639 | |
| 640 | static 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 Hoffmann | d155009 | 2010-12-15 12:45:24 +0100 | [diff] [blame] | 651 | static const VMStateDescription vmstate_usb_hub = { |
| 652 | .name = "usb-hub", |
| 653 | .version_id = 1, |
| 654 | .minimum_version_id = 1, |
Juan Quintela | 6e3d652 | 2014-04-16 13:31:26 +0200 | [diff] [blame] | 655 | .fields = (VMStateField[]) { |
Gerd Hoffmann | d155009 | 2010-12-15 12:45:24 +0100 | [diff] [blame] | 656 | VMSTATE_USB_DEVICE(dev, USBHubState), |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 657 | VMSTATE_STRUCT_ARRAY(ports, USBHubState, MAX_PORTS, 0, |
Gerd Hoffmann | d155009 | 2010-12-15 12:45:24 +0100 | [diff] [blame] | 658 | vmstate_usb_hub_port, USBHubPort), |
| 659 | VMSTATE_END_OF_LIST() |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 660 | }, |
| 661 | .subsections = (const VMStateDescription * []) { |
| 662 | &vmstate_usb_hub_port_timer, |
| 663 | NULL |
Gerd Hoffmann | d155009 | 2010-12-15 12:45:24 +0100 | [diff] [blame] | 664 | } |
| 665 | }; |
| 666 | |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 667 | static Property usb_hub_properties[] = { |
| 668 | DEFINE_PROP_UINT32("ports", USBHubState, num_ports, 8), |
Gerd Hoffmann | 1cc403e | 2019-05-24 09:03:10 +0200 | [diff] [blame] | 669 | DEFINE_PROP_BOOL("port-power", USBHubState, port_power, false), |
Gerd Hoffmann | 9d84bb0 | 2019-05-24 09:03:07 +0200 | [diff] [blame] | 670 | DEFINE_PROP_END_OF_LIST(), |
| 671 | }; |
| 672 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 673 | static void usb_hub_class_initfn(ObjectClass *klass, void *data) |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 674 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 675 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 676 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
| 677 | |
Gonglei | f3f8c45 | 2014-09-19 14:48:28 +0800 | [diff] [blame] | 678 | uc->realize = usb_hub_realize; |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 679 | uc->product_desc = "QEMU USB Hub"; |
| 680 | uc->usb_desc = &desc_hub; |
Gerd Hoffmann | 06c7508 | 2012-01-10 17:08:13 +0100 | [diff] [blame] | 681 | uc->find_device = usb_hub_find_device; |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 682 | 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é Lureau | c4fe970 | 2017-02-21 18:14:45 +0400 | [diff] [blame] | 685 | uc->unrealize = usb_hub_unrealize; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 686 | set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 687 | dc->fw_name = "hub"; |
| 688 | dc->vmsd = &vmstate_usb_hub; |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 689 | device_class_set_props(dc, usb_hub_properties); |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 690 | } |
| 691 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 692 | static const TypeInfo hub_info = { |
Gonglei | e81b13a | 2015-05-06 20:55:27 +0800 | [diff] [blame] | 693 | .name = TYPE_USB_HUB, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 694 | .parent = TYPE_USB_DEVICE, |
| 695 | .instance_size = sizeof(USBHubState), |
| 696 | .class_init = usb_hub_class_initfn, |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 697 | }; |
| 698 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 699 | static void usb_hub_register_types(void) |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 700 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 701 | type_register_static(&hub_info); |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 702 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 703 | |
| 704 | type_init(usb_hub_register_types) |