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