2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 extern bool usb_started; /* flag for the started/stopped USB status */
20 static bool asynch_allowed;
22 int usb_disable_asynch(int disable)
24 int old_value = asynch_allowed;
26 asynch_allowed = !disable;
30 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
31 int length, int interval)
33 struct udevice *bus = udev->controller_dev;
34 struct dm_usb_ops *ops = usb_get_ops(bus);
39 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
42 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
43 void *buffer, int length, struct devrequest *setup)
45 struct udevice *bus = udev->controller_dev;
46 struct dm_usb_ops *ops = usb_get_ops(bus);
51 return ops->control(bus, udev, pipe, buffer, length, setup);
54 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
57 struct udevice *bus = udev->controller_dev;
58 struct dm_usb_ops *ops = usb_get_ops(bus);
63 return ops->bulk(bus, udev, pipe, buffer, length);
66 int usb_alloc_device(struct usb_device *udev)
68 struct udevice *bus = udev->controller_dev;
69 struct dm_usb_ops *ops = usb_get_ops(bus);
71 /* This is only requird by some controllers - current XHCI */
72 if (!ops->alloc_device)
75 return ops->alloc_device(bus, udev);
84 /* De-activate any devices that have been activated */
85 ret = uclass_get(UCLASS_USB, &uc);
88 uclass_foreach_dev(bus, uc) {
89 ret = device_remove(bus);
101 static int usb_scan_bus(struct udevice *bus, bool recurse)
103 struct usb_bus_priv *priv;
107 priv = dev_get_uclass_priv(bus);
109 assert(recurse); /* TODO: Support non-recusive */
111 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
115 return priv->next_addr;
120 int controllers_initialized = 0;
129 ret = uclass_get(UCLASS_USB, &uc);
133 uclass_foreach_dev(bus, uc) {
134 /* init low_level 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++;
145 if (ret) { /* Other error. */
146 printf("probe failed, error %d\n", ret);
150 * lowlevel init is OK, now scan the bus for devices
151 * i.e. search HUBs and configure them
153 controllers_initialized++;
154 printf("scanning bus %d for devices... ", bus->seq);
156 ret = usb_scan_bus(bus, true);
158 printf("failed, error %d\n", ret);
160 printf("No USB Device found\n");
162 printf("%d USB Device(s) found\n", ret);
167 /* if we were not able to find at least one working bus, bail out */
169 printf("No controllers found\n");
170 else if (controllers_initialized == 0)
171 printf("USB error: all controllers failed lowlevel init\n");
173 return usb_started ? 0 : -1;
176 int usb_reset_root_port(void)
181 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
183 struct usb_device *udev;
186 if (!device_active(parent))
188 udev = dev_get_parentdata(parent);
189 if (udev->devnum == devnum)
192 for (device_find_first_child(parent, &dev);
194 device_find_next_child(&dev)) {
195 udev = find_child_devnum(dev, devnum);
203 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
206 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
208 device_find_first_child(bus, &hub);
209 if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
210 return find_child_devnum(hub, devnum);
215 int usb_post_bind(struct udevice *dev)
217 /* Scan the bus for devices */
218 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
221 int usb_port_reset(struct usb_device *parent, int portnr)
223 unsigned short portstatus;
226 debug("%s: start\n", __func__);
229 /* reset the port for the second time */
231 debug("%s: reset %d\n", __func__, portnr - 1);
232 ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus);
234 printf("\n Couldn't reset port %i\n", portnr);
238 debug("%s: reset root\n", __func__);
239 usb_reset_root_port();
245 int usb_legacy_port_reset(struct usb_device *parent, int portnr)
247 return usb_port_reset(parent, portnr);
250 int usb_scan_device(struct udevice *parent, int port,
251 enum usb_device_speed speed, struct udevice **devp)
254 bool created = false;
255 struct usb_dev_platdata *plat;
256 struct usb_bus_priv *priv;
257 struct usb_device *parent_udev;
259 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
260 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
263 memset(udev, '\0', sizeof(*udev));
264 ret = usb_get_bus(parent, &udev->controller_dev);
267 priv = dev_get_uclass_priv(udev->controller_dev);
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.
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.
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
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.
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.
299 udev->devnum = priv->next_addr + 1;
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);
308 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
309 debug("** usb_find_child returns %d\n", ret);
311 /* TODO: Find a suitable driver and create the device */
315 int usb_child_post_bind(struct udevice *dev)
317 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
318 const void *blob = gd->fdt_blob;
321 if (dev->of_offset == -1)
324 /* We only support matching a few things */
325 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
327 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
328 plat->id.bDeviceClass = val;
330 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
332 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
333 plat->id.bInterfaceClass = val;
339 int usb_get_bus(struct udevice *dev, struct udevice **busp)
344 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
347 /* By design this cannot happen */
349 debug("USB HUB '%s' does not have a controller\n", dev->name);
357 int usb_child_pre_probe(struct udevice *dev)
360 struct usb_device *udev = dev_get_parentdata(dev);
361 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
364 ret = usb_get_bus(dev, &bus);
367 udev->controller_dev = bus;
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);
375 ret = usb_select_config(udev);
382 UCLASS_DRIVER(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),