2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 /* We only support up to 8 */
16 #define SANDBOX_NUM_PORTS 4
18 struct sandbox_hub_platdata {
19 struct usb_dev_platdata plat;
20 int port; /* Port number (numbered from 0) */
24 STRING_MANUFACTURER = 1,
31 static struct usb_string hub_strings[] = {
32 {STRING_MANUFACTURER, "sandbox"},
33 {STRING_PRODUCT, "hub"},
34 {STRING_SERIAL, "2345"},
38 static struct usb_device_descriptor hub_device_desc = {
39 .bLength = sizeof(hub_device_desc),
40 .bDescriptorType = USB_DT_DEVICE,
42 .bcdUSB = __constant_cpu_to_le16(0x0200),
44 .bDeviceClass = USB_CLASS_HUB,
48 .idVendor = __constant_cpu_to_le16(0x1234),
49 .idProduct = __constant_cpu_to_le16(0x5678),
50 .iManufacturer = STRING_MANUFACTURER,
51 .iProduct = STRING_PRODUCT,
52 .iSerialNumber = STRING_SERIAL,
53 .bNumConfigurations = 1,
56 static struct usb_config_descriptor hub_config1 = {
57 .bLength = sizeof(hub_config1),
58 .bDescriptorType = USB_DT_CONFIG,
60 /* wTotalLength is set up by usb-emul-uclass */
62 .bConfigurationValue = 0,
64 .bmAttributes = 1 << 7,
68 static struct usb_interface_descriptor hub_interface0 = {
69 .bLength = sizeof(hub_interface0),
70 .bDescriptorType = USB_DT_INTERFACE,
72 .bInterfaceNumber = 0,
73 .bAlternateSetting = 0,
75 .bInterfaceClass = USB_CLASS_HUB,
76 .bInterfaceSubClass = 0,
77 .bInterfaceProtocol = US_PR_CB,
81 static struct usb_endpoint_descriptor hub_endpoint0_in = {
82 .bLength = USB_DT_ENDPOINT_SIZE,
83 .bDescriptorType = USB_DT_ENDPOINT,
85 .bEndpointAddress = 1 | USB_DIR_IN,
86 .bmAttributes = USB_ENDPOINT_XFER_INT,
87 .wMaxPacketSize = __constant_cpu_to_le16(1024),
91 static struct usb_hub_descriptor hub_desc = {
92 .bLength = sizeof(hub_desc),
93 .bDescriptorType = USB_DT_HUB,
94 .bNbrPorts = SANDBOX_NUM_PORTS,
95 .wHubCharacteristics = __constant_cpu_to_le16(1 << 0 | 1 << 3 |
98 .bHubContrCurrent = 5,
101 /* all ports removeable */
102 .DeviceRemovable = {0, 0xff}
105 #if SANDBOX_NUM_PORTS > 8
106 #error "This code sets up an incorrect mask"
110 static void *hub_desc_list[] = {
119 struct sandbox_hub_priv {
120 int status[SANDBOX_NUM_PORTS];
121 int change[SANDBOX_NUM_PORTS];
124 static struct udevice *hub_find_device(struct udevice *hub, int port,
125 enum usb_device_speed *speed)
128 struct usb_generic_descriptor **gen_desc;
129 struct usb_device_descriptor **dev_desc;
131 for (device_find_first_child(hub, &dev);
133 device_find_next_child(&dev)) {
134 struct sandbox_hub_platdata *plat;
136 plat = dev_get_parent_platdata(dev);
137 if (plat->port == port) {
138 gen_desc = plat->plat.desc_list;
139 gen_desc = usb_emul_find_descriptor(gen_desc,
141 dev_desc = (struct usb_device_descriptor **)gen_desc;
143 switch (le16_to_cpu((*dev_desc)->bcdUSB)) {
145 *speed = USB_SPEED_LOW;
148 *speed = USB_SPEED_FULL;
152 *speed = USB_SPEED_HIGH;
163 static int clrset_post_state(struct udevice *hub, int port, int clear, int set)
165 struct sandbox_hub_priv *priv = dev_get_priv(hub);
166 int *status = &priv->status[port];
167 int *change = &priv->change[port];
170 if ((clear | set) & USB_PORT_STAT_POWER) {
171 enum usb_device_speed speed;
172 struct udevice *dev = hub_find_device(hub, port, &speed);
175 if (set & USB_PORT_STAT_POWER) {
176 ret = device_probe(dev);
177 debug("%s: %s: power on, probed, ret=%d\n",
178 __func__, dev->name, ret);
180 set |= USB_PORT_STAT_CONNECTION |
181 USB_PORT_STAT_ENABLE;
182 if (speed == USB_SPEED_LOW)
183 set |= USB_PORT_STAT_LOW_SPEED;
184 else if (speed == USB_SPEED_HIGH)
185 set |= USB_PORT_STAT_HIGH_SPEED;
188 } else if (clear & USB_PORT_STAT_POWER) {
189 debug("%s: %s: power off, removed, ret=%d\n",
190 __func__, dev->name, ret);
191 ret = device_remove(dev, DM_REMOVE_NORMAL);
192 clear |= USB_PORT_STAT_CONNECTION;
196 *change |= *status & clear;
197 *change |= ~*status & set;
199 *status = (*status & ~clear) | set;
204 static int sandbox_hub_submit_control_msg(struct udevice *bus,
205 struct usb_device *udev,
207 void *buffer, int length,
208 struct devrequest *setup)
210 struct sandbox_hub_priv *priv = dev_get_priv(bus);
213 if (pipe == usb_rcvctrlpipe(udev, 0)) {
214 switch (setup->requesttype) {
215 case USB_RT_HUB | USB_DIR_IN:
216 switch (setup->request) {
217 case USB_REQ_GET_STATUS: {
218 struct usb_hub_status *hubsts = buffer;
220 hubsts->wHubStatus = 0;
221 hubsts->wHubChange = 0;
223 udev->act_len = sizeof(*hubsts);
227 debug("%s: rx ctl requesttype=%x, request=%x\n",
228 __func__, setup->requesttype,
232 case USB_RT_PORT | USB_DIR_IN:
233 switch (setup->request) {
234 case USB_REQ_GET_STATUS: {
235 struct usb_port_status *portsts = buffer;
238 port = (setup->index & USB_HUB_PORT_MASK) - 1;
239 portsts->wPortStatus = priv->status[port];
240 portsts->wPortChange = priv->change[port];
242 udev->act_len = sizeof(*portsts);
247 debug("%s: rx ctl requesttype=%x, request=%x\n",
248 __func__, setup->requesttype, setup->request);
251 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
252 switch (setup->requesttype) {
254 switch (setup->request) {
255 case USB_REQ_SET_FEATURE: {
258 port = (setup->index & USB_HUB_PORT_MASK) - 1;
259 debug("set feature port=%x, feature=%x\n",
261 if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
262 ret = clrset_post_state(bus, port, 0,
265 debug(" ** Invalid feature\n");
269 case USB_REQ_CLEAR_FEATURE: {
272 port = (setup->index & USB_HUB_PORT_MASK) - 1;
273 debug("clear feature port=%x, feature=%x\n",
275 if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
276 ret = clrset_post_state(bus, port,
277 1 << setup->value, 0);
279 priv->change[port] &= 1 <<
286 debug("%s: tx ctl requesttype=%x, request=%x\n",
287 __func__, setup->requesttype,
292 debug("%s: tx ctl requesttype=%x, request=%x\n",
293 __func__, setup->requesttype, setup->request);
297 debug("pipe=%lx\n", pipe);
302 static int sandbox_hub_bind(struct udevice *dev)
304 return usb_emul_setup_device(dev, hub_strings, hub_desc_list);
307 static int sandbox_child_post_bind(struct udevice *dev)
309 struct sandbox_hub_platdata *plat = dev_get_parent_platdata(dev);
310 struct usb_emul_platdata *emul = dev_get_uclass_platdata(dev);
312 plat->port = dev_read_u32_default(dev, "reg", -1);
313 emul->port1 = plat->port + 1;
318 static const struct dm_usb_ops sandbox_usb_hub_ops = {
319 .control = sandbox_hub_submit_control_msg,
322 static const struct udevice_id sandbox_usb_hub_ids[] = {
323 { .compatible = "sandbox,usb-hub" },
327 U_BOOT_DRIVER(usb_sandbox_hub) = {
328 .name = "usb_sandbox_hub",
329 .id = UCLASS_USB_EMUL,
330 .of_match = sandbox_usb_hub_ids,
331 .bind = sandbox_hub_bind,
332 .ops = &sandbox_usb_hub_ops,
333 .priv_auto_alloc_size = sizeof(struct sandbox_hub_priv),
334 .per_child_platdata_auto_alloc_size =
335 sizeof(struct sandbox_hub_platdata),
336 .child_post_bind = sandbox_child_post_bind,