1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
10 #include <dm/device-internal.h>
12 /* We only support up to 8 */
13 #define SANDBOX_NUM_PORTS 4
15 struct sandbox_hub_plat {
16 struct usb_dev_plat plat;
17 int port; /* Port number (numbered from 0) */
21 STRING_MANUFACTURER = 1,
28 static struct usb_string hub_strings[] = {
29 {STRING_MANUFACTURER, "sandbox"},
30 {STRING_PRODUCT, "hub"},
31 {STRING_SERIAL, "2345"},
35 static struct usb_device_descriptor hub_device_desc = {
36 .bLength = sizeof(hub_device_desc),
37 .bDescriptorType = USB_DT_DEVICE,
39 .bcdUSB = __constant_cpu_to_le16(0x0200),
41 .bDeviceClass = USB_CLASS_HUB,
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,
53 static struct usb_config_descriptor hub_config1 = {
54 .bLength = sizeof(hub_config1),
55 .bDescriptorType = USB_DT_CONFIG,
57 /* wTotalLength is set up by usb-emul-uclass */
59 .bConfigurationValue = 0,
61 .bmAttributes = 1 << 7,
65 static struct usb_interface_descriptor hub_interface0 = {
66 .bLength = sizeof(hub_interface0),
67 .bDescriptorType = USB_DT_INTERFACE,
69 .bInterfaceNumber = 0,
70 .bAlternateSetting = 0,
72 .bInterfaceClass = USB_CLASS_HUB,
73 .bInterfaceSubClass = 0,
74 .bInterfaceProtocol = US_PR_CB,
78 static struct usb_endpoint_descriptor hub_endpoint0_in = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
82 .bEndpointAddress = 1 | USB_DIR_IN,
83 .bmAttributes = USB_ENDPOINT_XFER_INT,
84 .wMaxPacketSize = __constant_cpu_to_le16(1024),
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 |
95 .bHubContrCurrent = 5,
98 /* all ports removeable */
99 .DeviceRemovable = {0, 0xff}
102 #if SANDBOX_NUM_PORTS > 8
103 #error "This code sets up an incorrect mask"
107 static void *hub_desc_list[] = {
116 struct sandbox_hub_priv {
117 int status[SANDBOX_NUM_PORTS];
118 int change[SANDBOX_NUM_PORTS];
121 static struct udevice *hub_find_device(struct udevice *hub, int port,
122 enum usb_device_speed *speed)
125 struct usb_generic_descriptor **gen_desc;
126 struct usb_device_descriptor **dev_desc;
128 for (device_find_first_child(hub, &dev);
130 device_find_next_child(&dev)) {
131 struct sandbox_hub_plat *plat;
133 plat = dev_get_parent_plat(dev);
134 if (plat->port == port) {
135 gen_desc = plat->plat.desc_list;
136 gen_desc = usb_emul_find_descriptor(gen_desc,
138 dev_desc = (struct usb_device_descriptor **)gen_desc;
140 switch (le16_to_cpu((*dev_desc)->bcdUSB)) {
142 *speed = USB_SPEED_LOW;
145 *speed = USB_SPEED_FULL;
149 *speed = USB_SPEED_HIGH;
160 static int clrset_post_state(struct udevice *hub, int port, int clear, int set)
162 struct sandbox_hub_priv *priv = dev_get_priv(hub);
163 int *status = &priv->status[port];
164 int *change = &priv->change[port];
167 if ((clear | set) & USB_PORT_STAT_POWER) {
168 enum usb_device_speed speed;
169 struct udevice *dev = hub_find_device(hub, port, &speed);
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);
177 set |= USB_PORT_STAT_CONNECTION |
178 USB_PORT_STAT_ENABLE;
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;
185 } else if (clear & USB_PORT_STAT_POWER) {
186 debug("%s: %s: power off, removed, ret=%d\n",
187 __func__, dev->name, ret);
188 ret = device_remove(dev, DM_REMOVE_NORMAL);
189 clear |= USB_PORT_STAT_CONNECTION;
193 *change |= *status & clear;
194 *change |= ~*status & set;
196 *status = (*status & ~clear) | set;
201 static int sandbox_hub_submit_control_msg(struct udevice *bus,
202 struct usb_device *udev,
204 void *buffer, int length,
205 struct devrequest *setup)
207 struct sandbox_hub_priv *priv = dev_get_priv(bus);
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;
217 hubsts->wHubStatus = 0;
218 hubsts->wHubChange = 0;
220 udev->act_len = sizeof(*hubsts);
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;
231 port = (setup->index & USB_HUB_PORT_MASK) - 1;
232 portsts->wPortStatus = priv->status[port];
233 portsts->wPortChange = priv->change[port];
235 udev->act_len = sizeof(*portsts);
241 debug("%s: rx ctl requesttype=%x, request=%x\n",
242 __func__, setup->requesttype, setup->request);
243 } else if (pipe == usb_sndctrlpipe(udev, 0)) {
244 switch (setup->requesttype) {
246 switch (setup->request) {
247 case USB_REQ_SET_FEATURE: {
250 port = (setup->index & USB_HUB_PORT_MASK) - 1;
251 debug("set feature port=%x, feature=%x\n",
253 if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
254 ret = clrset_post_state(bus, port, 0,
257 debug(" ** Invalid feature\n");
261 case USB_REQ_CLEAR_FEATURE: {
264 port = (setup->index & USB_HUB_PORT_MASK) - 1;
265 debug("clear feature port=%x, feature=%x\n",
267 if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
268 ret = clrset_post_state(bus, port,
269 1 << setup->value, 0);
271 priv->change[port] &= 1 <<
279 debug("%s: tx ctl requesttype=%x, request=%x\n",
280 __func__, setup->requesttype, setup->request);
282 debug("pipe=%lx\n", pipe);
287 static int sandbox_hub_bind(struct udevice *dev)
289 return usb_emul_setup_device(dev, hub_strings, hub_desc_list);
292 static int sandbox_child_post_bind(struct udevice *dev)
294 struct sandbox_hub_plat *plat = dev_get_parent_plat(dev);
295 struct usb_emul_plat *emul = dev_get_uclass_plat(dev);
297 plat->port = dev_read_u32_default(dev, "reg", -1);
298 emul->port1 = plat->port + 1;
303 static const struct dm_usb_ops sandbox_usb_hub_ops = {
304 .control = sandbox_hub_submit_control_msg,
307 static const struct udevice_id sandbox_usb_hub_ids[] = {
308 { .compatible = "sandbox,usb-hub" },
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,
318 .priv_auto = sizeof(struct sandbox_hub_priv),
319 .per_child_plat_auto = sizeof(struct sandbox_hub_plat),
320 .child_post_bind = sandbox_child_post_bind,