blob: 3b3e59f978f7d1b2f4bf2575b53d2b5e24d3e34c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassfb09d952015-03-25 12:22:40 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassfb09d952015-03-25 12:22:40 -06005 */
6
Simon Glassfb09d952015-03-25 12:22:40 -06007#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glassfb09d952015-03-25 12:22:40 -06009#include <usb.h>
10#include <dm/device-internal.h>
11
Simon Glassfb09d952015-03-25 12:22:40 -060012/* We only support up to 8 */
Simon Glassac338992015-11-08 23:47:54 -070013#define SANDBOX_NUM_PORTS 4
Simon Glassfb09d952015-03-25 12:22:40 -060014
Simon Glassb75b15b2020-12-03 16:55:23 -070015struct sandbox_hub_plat {
16 struct usb_dev_plat plat;
Simon Glassfb09d952015-03-25 12:22:40 -060017 int port; /* Port number (numbered from 0) */
18};
19
20enum {
21 STRING_MANUFACTURER = 1,
22 STRING_PRODUCT,
23 STRING_SERIAL,
24
25 STRING_count,
26};
27
28static struct usb_string hub_strings[] = {
29 {STRING_MANUFACTURER, "sandbox"},
30 {STRING_PRODUCT, "hub"},
31 {STRING_SERIAL, "2345"},
Simon Glassf5823c02015-04-19 07:20:59 -060032 {},
Simon Glassfb09d952015-03-25 12:22:40 -060033};
34
35static 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
53static 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
65static 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
78static 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
88static 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 Meng0d66b3a2017-07-19 21:50:00 +080096 {
97 {
98 /* all ports removeable */
99 .DeviceRemovable = {0, 0xff}
100 }
101 }
Simon Glassfb09d952015-03-25 12:22:40 -0600102#if SANDBOX_NUM_PORTS > 8
103#error "This code sets up an incorrect mask"
104#endif
105};
106
107static 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
116struct sandbox_hub_priv {
117 int status[SANDBOX_NUM_PORTS];
118 int change[SANDBOX_NUM_PORTS];
119};
120
Bin Meng83436d52017-10-01 06:19:41 -0700121static struct udevice *hub_find_device(struct udevice *hub, int port,
122 enum usb_device_speed *speed)
Simon Glassfb09d952015-03-25 12:22:40 -0600123{
124 struct udevice *dev;
Bin Meng83436d52017-10-01 06:19:41 -0700125 struct usb_generic_descriptor **gen_desc;
126 struct usb_device_descriptor **dev_desc;
Simon Glassfb09d952015-03-25 12:22:40 -0600127
128 for (device_find_first_child(hub, &dev);
129 dev;
130 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700131 struct sandbox_hub_plat *plat;
Simon Glassfb09d952015-03-25 12:22:40 -0600132
Simon Glass71fa5b42020-12-03 16:55:18 -0700133 plat = dev_get_parent_plat(dev);
Bin Meng83436d52017-10-01 06:19:41 -0700134 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 Glassfb09d952015-03-25 12:22:40 -0600153 return dev;
Bin Meng83436d52017-10-01 06:19:41 -0700154 }
Simon Glassfb09d952015-03-25 12:22:40 -0600155 }
156
157 return NULL;
158}
159
160static 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 Meng83436d52017-10-01 06:19:41 -0700168 enum usb_device_speed speed;
169 struct udevice *dev = hub_find_device(hub, port, &speed);
Simon Glassfb09d952015-03-25 12:22:40 -0600170
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 Meng83436d52017-10-01 06:19:41 -0700179 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 Glassfb09d952015-03-25 12:22:40 -0600183 }
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 Roese80b5bc92017-03-20 12:51:48 +0100188 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassfb09d952015-03-25 12:22:40 -0600189 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
201static 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 Schuchardt00eaa7c2023-04-01 08:57:33 +0200222 }
Simon Glassfb09d952015-03-25 12:22:40 -0600223 }
Heinrich Schuchardt00eaa7c2023-04-01 08:57:33 +0200224 break;
Simon Glassfb09d952015-03-25 12:22:40 -0600225 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 Schuchardt00eaa7c2023-04-01 08:57:33 +0200237 }
Simon Glassfb09d952015-03-25 12:22:40 -0600238 }
Simon Glassfb09d952015-03-25 12:22:40 -0600239 break;
240 }
Heinrich Schuchardt00eaa7c2023-04-01 08:57:33 +0200241 debug("%s: rx ctl requesttype=%x, request=%x\n",
242 __func__, setup->requesttype, setup->request);
Simon Glassfb09d952015-03-25 12:22:40 -0600243 } 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 Schuchardt00eaa7c2023-04-01 08:57:33 +0200260 }
Simon Glassfb09d952015-03-25 12:22:40 -0600261 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 Schuchardt00eaa7c2023-04-01 08:57:33 +0200276 }
Simon Glassfb09d952015-03-25 12:22:40 -0600277 }
Simon Glassfb09d952015-03-25 12:22:40 -0600278 }
Heinrich Schuchardt00eaa7c2023-04-01 08:57:33 +0200279 debug("%s: tx ctl requesttype=%x, request=%x\n",
280 __func__, setup->requesttype, setup->request);
Simon Glassfb09d952015-03-25 12:22:40 -0600281 }
282 debug("pipe=%lx\n", pipe);
283
284 return -EIO;
285}
286
287static int sandbox_hub_bind(struct udevice *dev)
288{
Bin Mengd502cf12017-10-01 06:19:36 -0700289 return usb_emul_setup_device(dev, hub_strings, hub_desc_list);
Simon Glassfb09d952015-03-25 12:22:40 -0600290}
291
292static int sandbox_child_post_bind(struct udevice *dev)
293{
Simon Glassb75b15b2020-12-03 16:55:23 -0700294 struct sandbox_hub_plat *plat = dev_get_parent_plat(dev);
295 struct usb_emul_plat *emul = dev_get_uclass_plat(dev);
Simon Glassfb09d952015-03-25 12:22:40 -0600296
Simon Glass82bf00e2017-05-18 20:09:39 -0600297 plat->port = dev_read_u32_default(dev, "reg", -1);
Bin Mengf1966442017-10-01 06:19:39 -0700298 emul->port1 = plat->port + 1;
Simon Glassfb09d952015-03-25 12:22:40 -0600299
300 return 0;
301}
302
303static const struct dm_usb_ops sandbox_usb_hub_ops = {
304 .control = sandbox_hub_submit_control_msg,
305};
306
307static const struct udevice_id sandbox_usb_hub_ids[] = {
308 { .compatible = "sandbox,usb-hub" },
309 { }
310};
311
312U_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 Glass8a2b47f2020-12-03 16:55:17 -0700318 .priv_auto = sizeof(struct sandbox_hub_priv),
Simon Glassb75b15b2020-12-03 16:55:23 -0700319 .per_child_plat_auto = sizeof(struct sandbox_hub_plat),
Simon Glassfb09d952015-03-25 12:22:40 -0600320 .child_post_bind = sandbox_child_post_bind,
321};