blob: 22dcd14316041b87b1de90fdcbf6e0d3f67a0d16 [file] [log] [blame]
Simon Glass9b82eeb2015-03-25 12:21:59 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <usb.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
14#include <dm/root.h>
15#include <dm/uclass-internal.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19extern bool usb_started; /* flag for the started/stopped USB status */
20static bool asynch_allowed;
21
22int usb_disable_asynch(int disable)
23{
24 int old_value = asynch_allowed;
25
26 asynch_allowed = !disable;
27 return old_value;
28}
29
30int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
31 int length, int interval)
32{
33 struct udevice *bus = udev->controller_dev;
34 struct dm_usb_ops *ops = usb_get_ops(bus);
35
36 if (!ops->interrupt)
37 return -ENOSYS;
38
39 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
40}
41
42int submit_control_msg(struct usb_device *udev, unsigned long pipe,
43 void *buffer, int length, struct devrequest *setup)
44{
45 struct udevice *bus = udev->controller_dev;
46 struct dm_usb_ops *ops = usb_get_ops(bus);
47
48 if (!ops->control)
49 return -ENOSYS;
50
51 return ops->control(bus, udev, pipe, buffer, length, setup);
52}
53
54int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
55 int length)
56{
57 struct udevice *bus = udev->controller_dev;
58 struct dm_usb_ops *ops = usb_get_ops(bus);
59
60 if (!ops->bulk)
61 return -ENOSYS;
62
63 return ops->bulk(bus, udev, pipe, buffer, length);
64}
65
66int usb_alloc_device(struct usb_device *udev)
67{
68 struct udevice *bus = udev->controller_dev;
69 struct dm_usb_ops *ops = usb_get_ops(bus);
70
71 /* This is only requird by some controllers - current XHCI */
72 if (!ops->alloc_device)
73 return 0;
74
75 return ops->alloc_device(bus, udev);
76}
77
78int usb_stop(void)
79{
80 struct udevice *bus;
81 struct uclass *uc;
82 int err = 0, ret;
83
84 /* De-activate any devices that have been activated */
85 ret = uclass_get(UCLASS_USB, &uc);
86 if (ret)
87 return ret;
88 uclass_foreach_dev(bus, uc) {
89 ret = device_remove(bus);
90 if (ret && !err)
91 err = ret;
92 }
93
94 usb_stor_reset();
95 usb_hub_reset();
96 usb_started = 0;
97
98 return err;
99}
100
101static int usb_scan_bus(struct udevice *bus, bool recurse)
102{
103 struct usb_bus_priv *priv;
104 struct udevice *dev;
105 int ret;
106
107 priv = dev_get_uclass_priv(bus);
108
109 assert(recurse); /* TODO: Support non-recusive */
110
111 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
112 if (ret)
113 return ret;
114
115 return priv->next_addr;
116}
117
118int usb_init(void)
119{
120 int controllers_initialized = 0;
121 struct udevice *bus;
122 struct uclass *uc;
123 int count = 0;
124 int ret;
125
126 asynch_allowed = 1;
127 usb_hub_reset();
128
129 ret = uclass_get(UCLASS_USB, &uc);
130 if (ret)
131 return ret;
132
133 uclass_foreach_dev(bus, uc) {
134 /* init low_level USB */
135 count++;
136 printf("USB");
137 printf("%d: ", bus->seq);
138 ret = device_probe(bus);
139 if (ret == -ENODEV) { /* No such device. */
140 puts("Port not available.\n");
141 controllers_initialized++;
142 continue;
143 }
144
145 if (ret) { /* Other error. */
146 printf("probe failed, error %d\n", ret);
147 continue;
148 }
149 /*
150 * lowlevel init is OK, now scan the bus for devices
151 * i.e. search HUBs and configure them
152 */
153 controllers_initialized++;
154 printf("scanning bus %d for devices... ", bus->seq);
155 debug("\n");
156 ret = usb_scan_bus(bus, true);
157 if (ret < 0)
158 printf("failed, error %d\n", ret);
159 else if (!ret)
160 printf("No USB Device found\n");
161 else
162 printf("%d USB Device(s) found\n", ret);
163 usb_started = true;
164 }
165
166 debug("scan end\n");
167 /* if we were not able to find at least one working bus, bail out */
168 if (!count)
169 printf("No controllers found\n");
170 else if (controllers_initialized == 0)
171 printf("USB error: all controllers failed lowlevel init\n");
172
173 return usb_started ? 0 : -1;
174}
175
176int usb_reset_root_port(void)
177{
178 return -ENOSYS;
179}
180
181static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
182{
183 struct usb_device *udev;
184 struct udevice *dev;
185
186 if (!device_active(parent))
187 return NULL;
188 udev = dev_get_parentdata(parent);
189 if (udev->devnum == devnum)
190 return udev;
191
192 for (device_find_first_child(parent, &dev);
193 dev;
194 device_find_next_child(&dev)) {
195 udev = find_child_devnum(dev, devnum);
196 if (udev)
197 return udev;
198 }
199
200 return NULL;
201}
202
203struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
204{
205 struct udevice *hub;
206 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
207
208 device_find_first_child(bus, &hub);
209 if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
210 return find_child_devnum(hub, devnum);
211
212 return NULL;
213}
214
215int usb_post_bind(struct udevice *dev)
216{
217 /* Scan the bus for devices */
218 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
219}
220
221int usb_port_reset(struct usb_device *parent, int portnr)
222{
223 unsigned short portstatus;
224 int ret;
225
226 debug("%s: start\n", __func__);
227
228 if (parent) {
229 /* reset the port for the second time */
230 assert(portnr > 0);
231 debug("%s: reset %d\n", __func__, portnr - 1);
232 ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus);
233 if (ret < 0) {
234 printf("\n Couldn't reset port %i\n", portnr);
235 return ret;
236 }
237 } else {
238 debug("%s: reset root\n", __func__);
239 usb_reset_root_port();
240 }
241
242 return 0;
243}
244
245int usb_legacy_port_reset(struct usb_device *parent, int portnr)
246{
247 return usb_port_reset(parent, portnr);
248}
249
250int usb_scan_device(struct udevice *parent, int port,
251 enum usb_device_speed speed, struct udevice **devp)
252{
253 struct udevice *dev;
254 bool created = false;
255 struct usb_dev_platdata *plat;
256 struct usb_bus_priv *priv;
257 struct usb_device *parent_udev;
258 int ret;
259 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
260 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
261
262 *devp = NULL;
263 memset(udev, '\0', sizeof(*udev));
264 ret = usb_get_bus(parent, &udev->controller_dev);
265 if (ret)
266 return ret;
267 priv = dev_get_uclass_priv(udev->controller_dev);
268
269 /*
270 * Somewhat nasty, this. We create a local device and use the normal
271 * USB stack to read its descriptor. Then we know what type of device
272 * to create for real.
273 *
274 * udev->dev is set to the parent, since we don't have a real device
275 * yet. The USB stack should not access udev.dev anyway, except perhaps
276 * to find the controller, and the controller will either be @parent,
277 * or some parent of @parent.
278 *
279 * Another option might be to create the device as a generic USB
280 * device, then morph it into the correct one when we know what it
281 * should be. This means that a generic USB device would morph into
282 * a network controller, or a USB flash stick, for example. However,
283 * we don't support such morphing and it isn't clear that it would
284 * be easy to do.
285 *
286 * Yet another option is to split out the USB stack parts of udev
287 * into something like a 'struct urb' (as Linux does) which can exist
288 * independently of any device. This feels cleaner, but calls for quite
289 * a big change to the USB stack.
290 *
291 * For now, the approach is to set up an empty udev, read its
292 * descriptor and assign it an address, then bind a real device and
293 * stash the resulting information into the device's parent
294 * platform data. Then when we probe it, usb_child_pre_probe() is called
295 * and it will pull the information out of the stash.
296 */
297 udev->dev = parent;
298 udev->speed = speed;
299 udev->devnum = priv->next_addr + 1;
300 udev->portnr = port;
301 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
302 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
303 dev_get_parentdata(parent) : NULL;
304 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
305 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
306 if (ret)
307 return ret;
308 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
309 debug("** usb_find_child returns %d\n", ret);
310
311 /* TODO: Find a suitable driver and create the device */
312 return -ENOENT;
313}
314
315int usb_child_post_bind(struct udevice *dev)
316{
317 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
318 const void *blob = gd->fdt_blob;
319 int val;
320
321 if (dev->of_offset == -1)
322 return 0;
323
324 /* We only support matching a few things */
325 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
326 if (val != -1) {
327 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
328 plat->id.bDeviceClass = val;
329 }
330 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
331 if (val != -1) {
332 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
333 plat->id.bInterfaceClass = val;
334 }
335
336 return 0;
337}
338
339int usb_get_bus(struct udevice *dev, struct udevice **busp)
340{
341 struct udevice *bus;
342
343 *busp = NULL;
344 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
345 bus = bus->parent;
346 if (!bus) {
347 /* By design this cannot happen */
348 assert(bus);
349 debug("USB HUB '%s' does not have a controller\n", dev->name);
350 return -EXDEV;
351 }
352 *busp = bus;
353
354 return 0;
355}
356
357int usb_child_pre_probe(struct udevice *dev)
358{
359 struct udevice *bus;
360 struct usb_device *udev = dev_get_parentdata(dev);
361 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
362 int ret;
363
364 ret = usb_get_bus(dev, &bus);
365 if (ret)
366 return ret;
367 udev->controller_dev = bus;
368 udev->dev = dev;
369 udev->devnum = plat->devnum;
370 udev->slot_id = plat->slot_id;
371 udev->portnr = plat->portnr;
372 udev->speed = plat->speed;
373 debug("** device '%s': getting slot_id=%d\n", dev->name, plat->slot_id);
374
375 ret = usb_select_config(udev);
376 if (ret)
377 return ret;
378
379 return 0;
380}
381
382UCLASS_DRIVER(usb) = {
383 .id = UCLASS_USB,
384 .name = "usb",
385 .flags = DM_UC_FLAG_SEQ_ALIAS,
386 .post_bind = usb_post_bind,
387 .per_child_auto_alloc_size = sizeof(struct usb_device),
388 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
389 .child_post_bind = usb_child_post_bind,
390 .child_pre_probe = usb_child_pre_probe,
391 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
392};