Commit 0566e240 authored by Simon Glass's avatar Simon Glass
Browse files

dm: usb: Allow USB drivers to be declared and auto-probed



USB devices in U-Boot are currently probed only after all devices have
been enumerated. Each type of device is probed by custom code, e.g.:

- USB storage
- Keyboard
- Ethernet

With driver model this approach doesn't work very well. We could build
a picture of the bus and then go back and add the devices later, but
this means that the data structures are incomplete for quite a while.
It also does not follow the model of being able to bind a device when we
discover it.

We would prefer to have devices automatically be bound as the device is
enumerated. This allows us to attach drivers to particular USB classes
or product/vendor IDs. This is the method used by Linux.

Add the required #defines from Linux, a way of declaring a USB driver and
the logic to locate the correct driver given the USB device's descriptors.
Signed-off-by: default avatarSimon Glass <sjg@chromium.org>
Reviewed-by: default avatarMarek Vasut <marex@denx.de>
parent 46b01797
......@@ -2,6 +2,8 @@
* (C) Copyright 2015 Google, Inc
* Written by Simon Glass <sjg@chromium.org>
*
* usb_match_device() modified from Linux kernel v4.0.
*
* SPDX-License-Identifier: GPL-2.0+
*/
......@@ -247,6 +249,179 @@ int usb_legacy_port_reset(struct usb_device *parent, int portnr)
return usb_port_reset(parent, portnr);
}
/* returns 0 if no match, 1 if match */
int usb_match_device(const struct usb_device_descriptor *desc,
const struct usb_device_id *id)
{
if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
id->idVendor != le16_to_cpu(desc->idVendor))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
id->idProduct != le16_to_cpu(desc->idProduct))
return 0;
/* No need to test id->bcdDevice_lo != 0, since 0 is never
greater than any unsigned number. */
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
(id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
(id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
(id->bDeviceClass != desc->bDeviceClass))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
(id->bDeviceSubClass != desc->bDeviceSubClass))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
(id->bDeviceProtocol != desc->bDeviceProtocol))
return 0;
return 1;
}
/* returns 0 if no match, 1 if match */
int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
const struct usb_interface_descriptor *int_desc,
const struct usb_device_id *id)
{
/* The interface class, subclass, protocol and number should never be
* checked for a match if the device class is Vendor Specific,
* unless the match record specifies the Vendor ID. */
if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
!(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
(id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
USB_DEVICE_ID_MATCH_INT_SUBCLASS |
USB_DEVICE_ID_MATCH_INT_PROTOCOL |
USB_DEVICE_ID_MATCH_INT_NUMBER)))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
(id->bInterfaceClass != int_desc->bInterfaceClass))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
(id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
(id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
return 0;
if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
(id->bInterfaceNumber != int_desc->bInterfaceNumber))
return 0;
return 1;
}
/* returns 0 if no match, 1 if match */
int usb_match_one_id(struct usb_device_descriptor *desc,
struct usb_interface_descriptor *int_desc,
const struct usb_device_id *id)
{
if (!usb_match_device(desc, id))
return 0;
return usb_match_one_id_intf(desc, int_desc, id);
}
/**
* usb_find_and_bind_driver() - Find and bind the right USB driver
*
* This only looks at certain fields in the descriptor.
*/
static int usb_find_and_bind_driver(struct udevice *parent,
struct usb_device_descriptor *desc,
struct usb_interface_descriptor *iface,
int bus_seq, int devnum,
struct udevice **devp)
{
struct usb_driver_entry *start, *entry;
int n_ents;
int ret;
char name[30], *str;
*devp = NULL;
debug("%s: Searching for driver\n", __func__);
start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
for (entry = start; entry != start + n_ents; entry++) {
const struct usb_device_id *id;
struct udevice *dev;
const struct driver *drv;
struct usb_dev_platdata *plat;
for (id = entry->match; id->match_flags; id++) {
if (!usb_match_one_id(desc, iface, id))
continue;
drv = entry->driver;
/*
* We could pass the descriptor to the driver as
* platdata (instead of NULL) and allow its bind()
* method to return -ENOENT if it doesn't support this
* device. That way we could continue the search to
* find another driver. For now this doesn't seem
* necesssary, so just bind the first match.
*/
ret = device_bind(parent, drv, drv->name, NULL, -1,
&dev);
if (ret)
goto error;
debug("%s: Match found: %s\n", __func__, drv->name);
dev->driver_data = id->driver_info;
plat = dev_get_parent_platdata(dev);
plat->id = *id;
*devp = dev;
return 0;
}
}
ret = -ENOENT;
error:
debug("%s: No match found: %d\n", __func__, ret);
return ret;
}
/**
* usb_find_child() - Find an existing device which matches our needs
*
*
*/
static int usb_find_child(struct udevice *parent,
struct usb_device_descriptor *desc,
struct usb_interface_descriptor *iface,
struct udevice **devp)
{
struct udevice *dev;
*devp = NULL;
for (device_find_first_child(parent, &dev);
dev;
device_find_next_child(&dev)) {
struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
/* If this device is already in use, skip it */
if (device_active(dev))
continue;
debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
if (usb_match_one_id(desc, iface, &plat->id)) {
*devp = dev;
return 0;
}
}
return -ENOENT;
}
int usb_scan_device(struct udevice *parent, int port,
enum usb_device_speed speed, struct udevice **devp)
{
......@@ -307,9 +482,36 @@ int usb_scan_device(struct udevice *parent, int port,
return ret;
ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
debug("** usb_find_child returns %d\n", ret);
if (ret) {
if (ret != -ENOENT)
return ret;
ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
udev->controller_dev->seq,
udev->devnum, &dev);
if (ret)
return ret;
created = true;
}
plat = dev_get_parent_platdata(dev);
debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
plat->devnum = udev->devnum;
plat->speed = udev->speed;
plat->slot_id = udev->slot_id;
plat->portnr = port;
debug("** device '%s': stashing slot_id=%d\n", dev->name,
plat->slot_id);
priv->next_addr++;
ret = device_probe(dev);
if (ret) {
debug("%s: Device '%s' probe failed\n", __func__, dev->name);
priv->next_addr--;
if (created)
device_unbind(dev);
return ret;
}
*devp = dev;
/* TODO: Find a suitable driver and create the device */
return -ENOENT;
return 0;
}
int usb_child_post_bind(struct udevice *dev)
......
......@@ -412,6 +412,113 @@ int usb_set_interface(struct usb_device *dev, int interface, int alternate);
((usb_pipeendpoint(pipe) * 2) - \
(usb_pipein(pipe) ? 0 : 1))
/**
* struct usb_device_id - identifies USB devices for probing and hotplugging
* @match_flags: Bit mask controlling which of the other fields are used to
* match against new devices. Any field except for driver_info may be
* used, although some only make sense in conjunction with other fields.
* This is usually set by a USB_DEVICE_*() macro, which sets all
* other fields in this structure except for driver_info.
* @idVendor: USB vendor ID for a device; numbers are assigned
* by the USB forum to its members.
* @idProduct: Vendor-assigned product ID.
* @bcdDevice_lo: Low end of range of vendor-assigned product version numbers.
* This is also used to identify individual product versions, for
* a range consisting of a single device.
* @bcdDevice_hi: High end of version number range. The range of product
* versions is inclusive.
* @bDeviceClass: Class of device; numbers are assigned
* by the USB forum. Products may choose to implement classes,
* or be vendor-specific. Device classes specify behavior of all
* the interfaces on a device.
* @bDeviceSubClass: Subclass of device; associated with bDeviceClass.
* @bDeviceProtocol: Protocol of device; associated with bDeviceClass.
* @bInterfaceClass: Class of interface; numbers are assigned
* by the USB forum. Products may choose to implement classes,
* or be vendor-specific. Interface classes specify behavior only
* of a given interface; other interfaces may support other classes.
* @bInterfaceSubClass: Subclass of interface; associated with bInterfaceClass.
* @bInterfaceProtocol: Protocol of interface; associated with bInterfaceClass.
* @bInterfaceNumber: Number of interface; composite devices may use
* fixed interface numbers to differentiate between vendor-specific
* interfaces.
* @driver_info: Holds information used by the driver. Usually it holds
* a pointer to a descriptor understood by the driver, or perhaps
* device flags.
*
* In most cases, drivers will create a table of device IDs by using
* USB_DEVICE(), or similar macros designed for that purpose.
* They will then export it to userspace using MODULE_DEVICE_TABLE(),
* and provide it to the USB core through their usb_driver structure.
*
* See the usb_match_id() function for information about how matches are
* performed. Briefly, you will normally use one of several macros to help
* construct these entries. Each entry you provide will either identify
* one or more specific products, or will identify a class of products
* which have agreed to behave the same. You should put the more specific
* matches towards the beginning of your table, so that driver_info can
* record quirks of specific products.
*/
struct usb_device_id {
/* which fields to match against? */
u16 match_flags;
/* Used for product specific matches; range is inclusive */
u16 idVendor;
u16 idProduct;
u16 bcdDevice_lo;
u16 bcdDevice_hi;
/* Used for device class matches */
u8 bDeviceClass;
u8 bDeviceSubClass;
u8 bDeviceProtocol;
/* Used for interface class matches */
u8 bInterfaceClass;
u8 bInterfaceSubClass;
u8 bInterfaceProtocol;
/* Used for vendor-specific interface matches */
u8 bInterfaceNumber;
/* not matched against */
ulong driver_info;
};
/* Some useful macros to use to create struct usb_device_id */
#define USB_DEVICE_ID_MATCH_VENDOR 0x0001
#define USB_DEVICE_ID_MATCH_PRODUCT 0x0002
#define USB_DEVICE_ID_MATCH_DEV_LO 0x0004
#define USB_DEVICE_ID_MATCH_DEV_HI 0x0008
#define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010
#define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020
#define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040
#define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080
#define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100
#define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200
#define USB_DEVICE_ID_MATCH_INT_NUMBER 0x0400
/* Match anything, indicates this is a valid entry even if everything is 0 */
#define USB_DEVICE_ID_MATCH_NONE 0x0800
#define USB_DEVICE_ID_MATCH_ALL 0x07ff
/**
* struct usb_driver_entry - Matches a driver to its usb_device_ids
* @compatible: Compatible string
* @data: Data for this compatible string
*/
struct usb_driver_entry {
struct driver *driver;
const struct usb_device_id *match;
};
#define USB_DEVICE(__name, __match) \
ll_entry_declare(struct usb_driver_entry, __name, usb_driver_entry) = {\
.driver = llsym(struct driver, __name, driver), \
.match = __match, \
}
/*************************************************************************
* Hub Stuff
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment