To make porting Linux drivers easier, let's import some bus lookup functions that are going to be used in later commits.
Signed-off-by: Ahmad Fatoum <a.fat...@barebox.org> --- drivers/base/bus.c | 56 ++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 22 --------------- include/linux/device/bus.h | 38 ++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 22 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index fde27dd008e8..8edd8cb01fae 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -95,3 +95,59 @@ int device_match_of_modalias(struct device *dev, const struct driver *drv) return false; } + +static struct device *__bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, + int (*fn)(struct device *dev, void *data), int *result) +{ + struct device *dev; + int ret; + + bus_for_each_device(bus, dev) { + if (start) { + if (dev == start) + start = NULL; + continue; + } + + ret = fn(dev, data); + if (ret) { + if (result) + *result = ret; + return dev; + } + } + + return NULL; +} + +int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, + int (*fn)(struct device *dev, void *data)) +{ + int ret = 0; + __bus_for_each_dev(bus, start, data, fn, &ret); + return ret; +} + +struct check_match_data { + device_match_t match; +}; + +struct device *bus_find_device(const struct bus_type *bus, struct device *start, + const void *data, device_match_t match) +{ + return __bus_for_each_dev(bus, start, (void *)data, + (int (*)(struct device *dev, void *data))match, + NULL); +} + +int device_match_name(struct device *dev, const void *name) +{ + return !strcmp(dev_name(dev), name); +} +EXPORT_SYMBOL_GPL(device_match_name); + +int device_match_of_node(struct device *dev, const void *np) +{ + return np && dev->of_node == np; +} +EXPORT_SYMBOL_GPL(device_match_of_node); diff --git a/include/linux/device.h b/include/linux/device.h index d4098b85239f..e3934b505fe0 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -52,28 +52,6 @@ static inline void __iomem *devm_ioremap(struct device *dev, return IOMEM(start); } -static inline int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, - int (*fn)(struct device *dev, void *data)) -{ - struct device *dev; - int ret; - - bus_for_each_device(bus, dev) { - if (start) { - if (dev == start) - start = NULL; - continue; - } - - ret = fn(dev, data); - if (ret) - return ret; - } - - return 0; -} - - /** * dev_set_drvdata - set driver private data for device * @dev: device instance diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index 8837ce3c2ed2..2364645bad4a 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -37,6 +37,44 @@ extern struct class bus_class; */ #define bus_for_each_driver(bus, drv) list_for_each_entry(drv, &(bus)->driver_list, bus_list) +int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, + int (*fn)(struct device *dev, void *data)); + +/* Generic device matching functions that all busses can use to match with */ +int device_match_name(struct device *dev, const void *name); +int device_match_of_node(struct device *dev, const void *np); + +/* Matching function type for drivers/base APIs to find a specific device */ +typedef int (*device_match_t)(struct device *dev, const void *data); + +struct device *bus_find_device(const struct bus_type *bus, struct device *start, + const void *data, device_match_t match); +/** + * bus_find_device_by_name - device iterator for locating a particular device + * of a specific name. + * @bus: bus type + * @start: Device to begin with + * @name: name of the device to match + */ +static inline struct device *bus_find_device_by_name(const struct bus_type *bus, + struct device *start, + const char *name) +{ + return bus_find_device(bus, start, name, device_match_name); +} + +/** + * bus_find_device_by_of_node : device iterator for locating a particular device + * matching the of_node. + * @bus: bus type + * @np: of_node of the device to match. + */ +static inline struct device * +bus_find_device_by_of_node(const struct bus_type *bus, const struct device_node *np) +{ + return bus_find_device(bus, NULL, np, device_match_of_node); +} + extern struct bus_type platform_bus; #endif -- 2.39.5