Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 10 | #include <usb.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 13 | /* We only support up to 8 */ |
Simon Glass | ac33899 | 2015-11-08 23:47:54 -0700 | [diff] [blame] | 14 | #define SANDBOX_NUM_PORTS 4 |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 15 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 16 | struct sandbox_hub_plat { |
| 17 | struct usb_dev_plat plat; |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 18 | int port; /* Port number (numbered from 0) */ |
| 19 | }; |
| 20 | |
| 21 | enum { |
| 22 | STRING_MANUFACTURER = 1, |
| 23 | STRING_PRODUCT, |
| 24 | STRING_SERIAL, |
| 25 | |
| 26 | STRING_count, |
| 27 | }; |
| 28 | |
| 29 | static struct usb_string hub_strings[] = { |
| 30 | {STRING_MANUFACTURER, "sandbox"}, |
| 31 | {STRING_PRODUCT, "hub"}, |
| 32 | {STRING_SERIAL, "2345"}, |
Simon Glass | f5823c0 | 2015-04-19 07:20:59 -0600 | [diff] [blame] | 33 | {}, |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static struct usb_device_descriptor hub_device_desc = { |
| 37 | .bLength = sizeof(hub_device_desc), |
| 38 | .bDescriptorType = USB_DT_DEVICE, |
| 39 | |
| 40 | .bcdUSB = __constant_cpu_to_le16(0x0200), |
| 41 | |
| 42 | .bDeviceClass = USB_CLASS_HUB, |
| 43 | .bDeviceSubClass = 0, |
| 44 | .bDeviceProtocol = 0, |
| 45 | |
| 46 | .idVendor = __constant_cpu_to_le16(0x1234), |
| 47 | .idProduct = __constant_cpu_to_le16(0x5678), |
| 48 | .iManufacturer = STRING_MANUFACTURER, |
| 49 | .iProduct = STRING_PRODUCT, |
| 50 | .iSerialNumber = STRING_SERIAL, |
| 51 | .bNumConfigurations = 1, |
| 52 | }; |
| 53 | |
| 54 | static struct usb_config_descriptor hub_config1 = { |
| 55 | .bLength = sizeof(hub_config1), |
| 56 | .bDescriptorType = USB_DT_CONFIG, |
| 57 | |
| 58 | /* wTotalLength is set up by usb-emul-uclass */ |
| 59 | .bNumInterfaces = 1, |
| 60 | .bConfigurationValue = 0, |
| 61 | .iConfiguration = 0, |
| 62 | .bmAttributes = 1 << 7, |
| 63 | .bMaxPower = 50, |
| 64 | }; |
| 65 | |
| 66 | static struct usb_interface_descriptor hub_interface0 = { |
| 67 | .bLength = sizeof(hub_interface0), |
| 68 | .bDescriptorType = USB_DT_INTERFACE, |
| 69 | |
| 70 | .bInterfaceNumber = 0, |
| 71 | .bAlternateSetting = 0, |
| 72 | .bNumEndpoints = 1, |
| 73 | .bInterfaceClass = USB_CLASS_HUB, |
| 74 | .bInterfaceSubClass = 0, |
| 75 | .bInterfaceProtocol = US_PR_CB, |
| 76 | .iInterface = 0, |
| 77 | }; |
| 78 | |
| 79 | static struct usb_endpoint_descriptor hub_endpoint0_in = { |
| 80 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 81 | .bDescriptorType = USB_DT_ENDPOINT, |
| 82 | |
| 83 | .bEndpointAddress = 1 | USB_DIR_IN, |
| 84 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 85 | .wMaxPacketSize = __constant_cpu_to_le16(1024), |
| 86 | .bInterval = 0, |
| 87 | }; |
| 88 | |
| 89 | static struct usb_hub_descriptor hub_desc = { |
| 90 | .bLength = sizeof(hub_desc), |
| 91 | .bDescriptorType = USB_DT_HUB, |
| 92 | .bNbrPorts = SANDBOX_NUM_PORTS, |
| 93 | .wHubCharacteristics = __constant_cpu_to_le16(1 << 0 | 1 << 3 | |
| 94 | 1 << 7), |
| 95 | .bPwrOn2PwrGood = 2, |
| 96 | .bHubContrCurrent = 5, |
Bin Meng | 0d66b3a | 2017-07-19 21:50:00 +0800 | [diff] [blame] | 97 | { |
| 98 | { |
| 99 | /* all ports removeable */ |
| 100 | .DeviceRemovable = {0, 0xff} |
| 101 | } |
| 102 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 103 | #if SANDBOX_NUM_PORTS > 8 |
| 104 | #error "This code sets up an incorrect mask" |
| 105 | #endif |
| 106 | }; |
| 107 | |
| 108 | static void *hub_desc_list[] = { |
| 109 | &hub_device_desc, |
| 110 | &hub_config1, |
| 111 | &hub_interface0, |
| 112 | &hub_endpoint0_in, |
| 113 | &hub_desc, |
| 114 | NULL, |
| 115 | }; |
| 116 | |
| 117 | struct sandbox_hub_priv { |
| 118 | int status[SANDBOX_NUM_PORTS]; |
| 119 | int change[SANDBOX_NUM_PORTS]; |
| 120 | }; |
| 121 | |
Bin Meng | 83436d5 | 2017-10-01 06:19:41 -0700 | [diff] [blame] | 122 | static struct udevice *hub_find_device(struct udevice *hub, int port, |
| 123 | enum usb_device_speed *speed) |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 124 | { |
| 125 | struct udevice *dev; |
Bin Meng | 83436d5 | 2017-10-01 06:19:41 -0700 | [diff] [blame] | 126 | struct usb_generic_descriptor **gen_desc; |
| 127 | struct usb_device_descriptor **dev_desc; |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 128 | |
| 129 | for (device_find_first_child(hub, &dev); |
| 130 | dev; |
| 131 | device_find_next_child(&dev)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 132 | struct sandbox_hub_plat *plat; |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 133 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 134 | plat = dev_get_parent_plat(dev); |
Bin Meng | 83436d5 | 2017-10-01 06:19:41 -0700 | [diff] [blame] | 135 | if (plat->port == port) { |
| 136 | gen_desc = plat->plat.desc_list; |
| 137 | gen_desc = usb_emul_find_descriptor(gen_desc, |
| 138 | USB_DT_DEVICE, 0); |
| 139 | dev_desc = (struct usb_device_descriptor **)gen_desc; |
| 140 | |
| 141 | switch (le16_to_cpu((*dev_desc)->bcdUSB)) { |
| 142 | case 0x0100: |
| 143 | *speed = USB_SPEED_LOW; |
| 144 | break; |
| 145 | case 0x0101: |
| 146 | *speed = USB_SPEED_FULL; |
| 147 | break; |
| 148 | case 0x0200: |
| 149 | default: |
| 150 | *speed = USB_SPEED_HIGH; |
| 151 | break; |
| 152 | } |
| 153 | |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 154 | return dev; |
Bin Meng | 83436d5 | 2017-10-01 06:19:41 -0700 | [diff] [blame] | 155 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | static int clrset_post_state(struct udevice *hub, int port, int clear, int set) |
| 162 | { |
| 163 | struct sandbox_hub_priv *priv = dev_get_priv(hub); |
| 164 | int *status = &priv->status[port]; |
| 165 | int *change = &priv->change[port]; |
| 166 | int ret = 0; |
| 167 | |
| 168 | if ((clear | set) & USB_PORT_STAT_POWER) { |
Bin Meng | 83436d5 | 2017-10-01 06:19:41 -0700 | [diff] [blame] | 169 | enum usb_device_speed speed; |
| 170 | struct udevice *dev = hub_find_device(hub, port, &speed); |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 171 | |
| 172 | if (dev) { |
| 173 | if (set & USB_PORT_STAT_POWER) { |
| 174 | ret = device_probe(dev); |
| 175 | debug("%s: %s: power on, probed, ret=%d\n", |
| 176 | __func__, dev->name, ret); |
| 177 | if (!ret) { |
| 178 | set |= USB_PORT_STAT_CONNECTION | |
| 179 | USB_PORT_STAT_ENABLE; |
Bin Meng | 83436d5 | 2017-10-01 06:19:41 -0700 | [diff] [blame] | 180 | if (speed == USB_SPEED_LOW) |
| 181 | set |= USB_PORT_STAT_LOW_SPEED; |
| 182 | else if (speed == USB_SPEED_HIGH) |
| 183 | set |= USB_PORT_STAT_HIGH_SPEED; |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | } else if (clear & USB_PORT_STAT_POWER) { |
| 187 | debug("%s: %s: power off, removed, ret=%d\n", |
| 188 | __func__, dev->name, ret); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 189 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 190 | clear |= USB_PORT_STAT_CONNECTION; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | *change |= *status & clear; |
| 195 | *change |= ~*status & set; |
| 196 | *change &= 0x1f; |
| 197 | *status = (*status & ~clear) | set; |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static int sandbox_hub_submit_control_msg(struct udevice *bus, |
| 203 | struct usb_device *udev, |
| 204 | unsigned long pipe, |
| 205 | void *buffer, int length, |
| 206 | struct devrequest *setup) |
| 207 | { |
| 208 | struct sandbox_hub_priv *priv = dev_get_priv(bus); |
| 209 | int ret = 0; |
| 210 | |
| 211 | if (pipe == usb_rcvctrlpipe(udev, 0)) { |
| 212 | switch (setup->requesttype) { |
| 213 | case USB_RT_HUB | USB_DIR_IN: |
| 214 | switch (setup->request) { |
| 215 | case USB_REQ_GET_STATUS: { |
| 216 | struct usb_hub_status *hubsts = buffer; |
| 217 | |
| 218 | hubsts->wHubStatus = 0; |
| 219 | hubsts->wHubChange = 0; |
| 220 | udev->status = 0; |
| 221 | udev->act_len = sizeof(*hubsts); |
| 222 | return 0; |
Heinrich Schuchardt | 00eaa7c | 2023-04-01 08:57:33 +0200 | [diff] [blame] | 223 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 224 | } |
Heinrich Schuchardt | 00eaa7c | 2023-04-01 08:57:33 +0200 | [diff] [blame] | 225 | break; |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 226 | case USB_RT_PORT | USB_DIR_IN: |
| 227 | switch (setup->request) { |
| 228 | case USB_REQ_GET_STATUS: { |
| 229 | struct usb_port_status *portsts = buffer; |
| 230 | int port; |
| 231 | |
| 232 | port = (setup->index & USB_HUB_PORT_MASK) - 1; |
| 233 | portsts->wPortStatus = priv->status[port]; |
| 234 | portsts->wPortChange = priv->change[port]; |
| 235 | udev->status = 0; |
| 236 | udev->act_len = sizeof(*portsts); |
| 237 | return 0; |
Heinrich Schuchardt | 00eaa7c | 2023-04-01 08:57:33 +0200 | [diff] [blame] | 238 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 239 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 240 | break; |
| 241 | } |
Heinrich Schuchardt | 00eaa7c | 2023-04-01 08:57:33 +0200 | [diff] [blame] | 242 | debug("%s: rx ctl requesttype=%x, request=%x\n", |
| 243 | __func__, setup->requesttype, setup->request); |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 244 | } else if (pipe == usb_sndctrlpipe(udev, 0)) { |
| 245 | switch (setup->requesttype) { |
| 246 | case USB_RT_PORT: |
| 247 | switch (setup->request) { |
| 248 | case USB_REQ_SET_FEATURE: { |
| 249 | int port; |
| 250 | |
| 251 | port = (setup->index & USB_HUB_PORT_MASK) - 1; |
| 252 | debug("set feature port=%x, feature=%x\n", |
| 253 | port, setup->value); |
| 254 | if (setup->value < USB_PORT_FEAT_C_CONNECTION) { |
| 255 | ret = clrset_post_state(bus, port, 0, |
| 256 | 1 << setup->value); |
| 257 | } else { |
| 258 | debug(" ** Invalid feature\n"); |
| 259 | } |
| 260 | return ret; |
Heinrich Schuchardt | 00eaa7c | 2023-04-01 08:57:33 +0200 | [diff] [blame] | 261 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 262 | case USB_REQ_CLEAR_FEATURE: { |
| 263 | int port; |
| 264 | |
| 265 | port = (setup->index & USB_HUB_PORT_MASK) - 1; |
| 266 | debug("clear feature port=%x, feature=%x\n", |
| 267 | port, setup->value); |
| 268 | if (setup->value < USB_PORT_FEAT_C_CONNECTION) { |
| 269 | ret = clrset_post_state(bus, port, |
| 270 | 1 << setup->value, 0); |
| 271 | } else { |
| 272 | priv->change[port] &= 1 << |
| 273 | (setup->value - 16); |
| 274 | } |
| 275 | udev->status = 0; |
| 276 | return 0; |
Heinrich Schuchardt | 00eaa7c | 2023-04-01 08:57:33 +0200 | [diff] [blame] | 277 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 278 | } |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 279 | } |
Heinrich Schuchardt | 00eaa7c | 2023-04-01 08:57:33 +0200 | [diff] [blame] | 280 | debug("%s: tx ctl requesttype=%x, request=%x\n", |
| 281 | __func__, setup->requesttype, setup->request); |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 282 | } |
| 283 | debug("pipe=%lx\n", pipe); |
| 284 | |
| 285 | return -EIO; |
| 286 | } |
| 287 | |
| 288 | static int sandbox_hub_bind(struct udevice *dev) |
| 289 | { |
Bin Meng | d502cf1 | 2017-10-01 06:19:36 -0700 | [diff] [blame] | 290 | return usb_emul_setup_device(dev, hub_strings, hub_desc_list); |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static int sandbox_child_post_bind(struct udevice *dev) |
| 294 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 295 | struct sandbox_hub_plat *plat = dev_get_parent_plat(dev); |
| 296 | struct usb_emul_plat *emul = dev_get_uclass_plat(dev); |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 297 | |
Simon Glass | 82bf00e | 2017-05-18 20:09:39 -0600 | [diff] [blame] | 298 | plat->port = dev_read_u32_default(dev, "reg", -1); |
Bin Meng | f196644 | 2017-10-01 06:19:39 -0700 | [diff] [blame] | 299 | emul->port1 = plat->port + 1; |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static const struct dm_usb_ops sandbox_usb_hub_ops = { |
| 305 | .control = sandbox_hub_submit_control_msg, |
| 306 | }; |
| 307 | |
| 308 | static const struct udevice_id sandbox_usb_hub_ids[] = { |
| 309 | { .compatible = "sandbox,usb-hub" }, |
| 310 | { } |
| 311 | }; |
| 312 | |
| 313 | U_BOOT_DRIVER(usb_sandbox_hub) = { |
| 314 | .name = "usb_sandbox_hub", |
| 315 | .id = UCLASS_USB_EMUL, |
| 316 | .of_match = sandbox_usb_hub_ids, |
| 317 | .bind = sandbox_hub_bind, |
| 318 | .ops = &sandbox_usb_hub_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 319 | .priv_auto = sizeof(struct sandbox_hub_priv), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 320 | .per_child_plat_auto = sizeof(struct sandbox_hub_plat), |
Simon Glass | fb09d95 | 2015-03-25 12:22:40 -0600 | [diff] [blame] | 321 | .child_post_bind = sandbox_child_post_bind, |
| 322 | }; |