]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
5db43992 SG |
2 | /* |
3 | * (C) Copyright 2015 Google, Inc | |
4 | * Written by Simon Glass <[email protected]> | |
5db43992 SG |
5 | */ |
6 | ||
5db43992 | 7 | #include <dm.h> |
f7ae49fc | 8 | #include <log.h> |
5db43992 SG |
9 | #include <usb.h> |
10 | #include <dm/device-internal.h> | |
11 | ||
5db43992 | 12 | /* We only support up to 8 */ |
80438a6c | 13 | #define SANDBOX_NUM_PORTS 4 |
5db43992 | 14 | |
8a8d24bd SG |
15 | struct sandbox_hub_plat { |
16 | struct usb_dev_plat plat; | |
5db43992 SG |
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"}, | |
093f2dce | 32 | {}, |
5db43992 SG |
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, | |
337fc7e6 BM |
96 | { |
97 | { | |
98 | /* all ports removeable */ | |
99 | .DeviceRemovable = {0, 0xff} | |
100 | } | |
101 | } | |
5db43992 SG |
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 | ||
d7771f0c BM |
121 | static struct udevice *hub_find_device(struct udevice *hub, int port, |
122 | enum usb_device_speed *speed) | |
5db43992 SG |
123 | { |
124 | struct udevice *dev; | |
d7771f0c BM |
125 | struct usb_generic_descriptor **gen_desc; |
126 | struct usb_device_descriptor **dev_desc; | |
5db43992 SG |
127 | |
128 | for (device_find_first_child(hub, &dev); | |
129 | dev; | |
130 | device_find_next_child(&dev)) { | |
8a8d24bd | 131 | struct sandbox_hub_plat *plat; |
5db43992 | 132 | |
caa4daa2 | 133 | plat = dev_get_parent_plat(dev); |
d7771f0c BM |
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 | ||
5db43992 | 153 | return dev; |
d7771f0c | 154 | } |
5db43992 SG |
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) { | |
d7771f0c BM |
168 | enum usb_device_speed speed; |
169 | struct udevice *dev = hub_find_device(hub, port, &speed); | |
5db43992 SG |
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; | |
d7771f0c BM |
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; | |
5db43992 SG |
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); | |
706865af | 188 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
5db43992 SG |
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; | |
42175a70 | 222 | } |
5db43992 | 223 | } |
42175a70 | 224 | break; |
5db43992 SG |
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; | |
42175a70 | 237 | } |
5db43992 | 238 | } |
5db43992 SG |
239 | break; |
240 | } | |
42175a70 HS |
241 | debug("%s: rx ctl requesttype=%x, request=%x\n", |
242 | __func__, setup->requesttype, setup->request); | |
5db43992 SG |
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; | |
42175a70 | 260 | } |
5db43992 SG |
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; | |
42175a70 | 276 | } |
5db43992 | 277 | } |
5db43992 | 278 | } |
42175a70 HS |
279 | debug("%s: tx ctl requesttype=%x, request=%x\n", |
280 | __func__, setup->requesttype, setup->request); | |
5db43992 SG |
281 | } |
282 | debug("pipe=%lx\n", pipe); | |
283 | ||
284 | return -EIO; | |
285 | } | |
286 | ||
287 | static int sandbox_hub_bind(struct udevice *dev) | |
288 | { | |
98b639fc | 289 | return usb_emul_setup_device(dev, hub_strings, hub_desc_list); |
5db43992 SG |
290 | } |
291 | ||
292 | static int sandbox_child_post_bind(struct udevice *dev) | |
293 | { | |
8a8d24bd SG |
294 | struct sandbox_hub_plat *plat = dev_get_parent_plat(dev); |
295 | struct usb_emul_plat *emul = dev_get_uclass_plat(dev); | |
5db43992 | 296 | |
a1e4adee | 297 | plat->port = dev_read_u32_default(dev, "reg", -1); |
84aa8536 | 298 | emul->port1 = plat->port + 1; |
5db43992 SG |
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, | |
41575d8e | 318 | .priv_auto = sizeof(struct sandbox_hub_priv), |
8a8d24bd | 319 | .per_child_plat_auto = sizeof(struct sandbox_hub_plat), |
5db43992 SG |
320 | .child_post_bind = sandbox_child_post_bind, |
321 | }; |