Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drivers: Introduce device lookup variants by name

Add a helper to match the device name for device lookup. Also
reuse this generic exported helper for the existing bus_find_device_by_name().
and add similar variants for driver/class.

Cc: Alessandro Zummo <a.zummo@towertech.it>
Cc: Alexander Aring <alex.aring@gmail.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Dan Murphy <dmurphy@ti.com>
Cc: Harald Freudenberger <freude@linux.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Jacek Anaszewski <jacek.anaszewski@gmail.com>
Cc: Lee Jones <lee.jones@linaro.org>
Cc: linux-leds@vger.kernel.org
Cc: linux-rtc@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: linux-wpan@vger.kernel.org
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Peter Oberparleiter <oberpar@linux.ibm.com>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Stefan Schmidt <stefan@datenfreihafen.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20190723221838.12024-2-suzuki.poulose@arm.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Suzuki K Poulose and committed by
Greg Kroah-Hartman
6cda08a2 5f9e832c

+54 -106
-24
drivers/base/bus.c
··· 342 342 } 343 343 EXPORT_SYMBOL_GPL(bus_find_device); 344 344 345 - static int match_name(struct device *dev, const void *data) 346 - { 347 - const char *name = data; 348 - 349 - return sysfs_streq(name, dev_name(dev)); 350 - } 351 - 352 - /** 353 - * bus_find_device_by_name - device iterator for locating a particular device of a specific name 354 - * @bus: bus type 355 - * @start: Device to begin with 356 - * @name: name of the device to match 357 - * 358 - * This is similar to the bus_find_device() function above, but it handles 359 - * searching by a name automatically, no need to write another strcmp matching 360 - * function. 361 - */ 362 - struct device *bus_find_device_by_name(struct bus_type *bus, 363 - struct device *start, const char *name) 364 - { 365 - return bus_find_device(bus, start, (void *)name, match_name); 366 - } 367 - EXPORT_SYMBOL_GPL(bus_find_device_by_name); 368 - 369 345 /** 370 346 * subsys_find_device_by_id - find a device with a specific enumeration number 371 347 * @subsys: subsystem
+6
drivers/base/core.c
··· 3357 3357 } 3358 3358 EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); 3359 3359 3360 + int device_match_name(struct device *dev, const void *name) 3361 + { 3362 + return sysfs_streq(dev_name(dev), name); 3363 + } 3364 + EXPORT_SYMBOL_GPL(device_match_name); 3365 + 3360 3366 int device_match_of_node(struct device *dev, const void *np) 3361 3367 { 3362 3368 return dev->of_node == np;
+1 -8
drivers/hwtracing/stm/core.c
··· 89 89 .dev_groups = stm_groups, 90 90 }; 91 91 92 - static int stm_dev_match(struct device *dev, const void *data) 93 - { 94 - const char *name = data; 95 - 96 - return sysfs_streq(name, dev_name(dev)); 97 - } 98 - 99 92 /** 100 93 * stm_find_device() - find stm device by name 101 94 * @buf: character buffer containing the name ··· 109 116 if (!stm_core_up) 110 117 return NULL; 111 118 112 - dev = class_find_device(&stm_class, NULL, buf, stm_dev_match); 119 + dev = class_find_device_by_name(&stm_class, buf); 113 120 if (!dev) 114 121 return NULL; 115 122
+1 -8
drivers/leds/led-class.c
··· 213 213 214 214 static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume); 215 215 216 - static int match_name(struct device *dev, const void *data) 217 - { 218 - if (!dev_name(dev)) 219 - return 0; 220 - return !strcmp(dev_name(dev), (char *)data); 221 - } 222 - 223 216 static int led_classdev_next_name(const char *init_name, char *name, 224 217 size_t len) 225 218 { ··· 223 230 strlcpy(name, init_name, len); 224 231 225 232 while ((ret < len) && 226 - (dev = class_find_device(leds_class, NULL, name, match_name))) { 233 + (dev = class_find_device_by_name(leds_class, name))) { 227 234 put_device(dev); 228 235 ret = snprintf(name, len, "%s_%u", init_name, ++i); 229 236 }
+1 -10
drivers/rtc/interface.c
··· 663 663 } 664 664 EXPORT_SYMBOL_GPL(rtc_update_irq); 665 665 666 - static int __rtc_match(struct device *dev, const void *data) 667 - { 668 - const char *name = data; 669 - 670 - if (strcmp(dev_name(dev), name) == 0) 671 - return 1; 672 - return 0; 673 - } 674 - 675 666 struct rtc_device *rtc_class_open(const char *name) 676 667 { 677 668 struct device *dev; 678 669 struct rtc_device *rtc = NULL; 679 670 680 - dev = class_find_device(rtc_class, NULL, name, __rtc_match); 671 + dev = class_find_device_by_name(rtc_class, name); 681 672 if (dev) 682 673 rtc = to_rtc_device(dev); 683 674
+1 -9
drivers/s390/cio/ccwgroup.c
··· 608 608 } 609 609 EXPORT_SYMBOL(ccwgroup_driver_unregister); 610 610 611 - static int __ccwgroupdev_check_busid(struct device *dev, const void *id) 612 - { 613 - const char *bus_id = id; 614 - 615 - return (strcmp(bus_id, dev_name(dev)) == 0); 616 - } 617 - 618 611 /** 619 612 * get_ccwgroupdev_by_busid() - obtain device from a bus id 620 613 * @gdrv: driver the device is owned by ··· 624 631 { 625 632 struct device *dev; 626 633 627 - dev = driver_find_device(&gdrv->driver, NULL, bus_id, 628 - __ccwgroupdev_check_busid); 634 + dev = driver_find_device_by_name(&gdrv->driver, bus_id); 629 635 630 636 return dev ? to_ccwgroupdev(dev) : NULL; 631 637 }
+1 -14
drivers/s390/cio/device.c
··· 1695 1695 EXPORT_SYMBOL_GPL(ccw_device_force_console); 1696 1696 #endif 1697 1697 1698 - /* 1699 - * get ccw_device matching the busid, but only if owned by cdrv 1700 - */ 1701 - static int 1702 - __ccwdev_check_busid(struct device *dev, const void *id) 1703 - { 1704 - const char *bus_id = id; 1705 - 1706 - return (strcmp(bus_id, dev_name(dev)) == 0); 1707 - } 1708 - 1709 - 1710 1698 /** 1711 1699 * get_ccwdev_by_busid() - obtain device from a bus id 1712 1700 * @cdrv: driver the device is owned by ··· 1711 1723 { 1712 1724 struct device *dev; 1713 1725 1714 - dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id, 1715 - __ccwdev_check_busid); 1726 + dev = driver_find_device_by_name(&cdrv->driver, bus_id); 1716 1727 1717 1728 return dev ? to_ccwdev(dev) : NULL; 1718 1729 }
+1 -10
drivers/s390/crypto/zcrypt_api.c
··· 133 133 static int zcdn_create(const char *name); 134 134 static int zcdn_destroy(const char *name); 135 135 136 - /* helper function, matches the name for find_zcdndev_by_name() */ 137 - static int __match_zcdn_name(struct device *dev, const void *data) 138 - { 139 - return strcmp(dev_name(dev), (const char *)data) == 0; 140 - } 141 - 142 136 /* helper function, matches the devt value for find_zcdndev_by_devt() */ 143 137 static int __match_zcdn_devt(struct device *dev, const void *data) 144 138 { ··· 146 152 */ 147 153 static inline struct zcdn_device *find_zcdndev_by_name(const char *name) 148 154 { 149 - struct device *dev = 150 - class_find_device(zcrypt_class, NULL, 151 - (void *) name, 152 - __match_zcdn_name); 155 + struct device *dev = class_find_device_by_name(zcrypt_class, name); 153 156 154 157 return dev ? to_zcdn_dev(dev) : NULL; 155 158 }
+1 -7
drivers/usb/roles/class.c
··· 90 90 return dev_fwnode(dev) == fwnode; 91 91 } 92 92 93 - static int switch_name_match(struct device *dev, const void *name) 94 - { 95 - return !strcmp((const char *)name, dev_name(dev)); 96 - } 97 - 98 93 static void *usb_role_switch_match(struct device_connection *con, int ep, 99 94 void *data) 100 95 { ··· 102 107 dev = class_find_device(role_class, NULL, con->fwnode, 103 108 switch_fwnode_match); 104 109 } else { 105 - dev = class_find_device(role_class, NULL, con->endpoint[ep], 106 - switch_name_match); 110 + dev = class_find_device_by_name(role_class, con->endpoint[ep]); 107 111 } 108 112 109 113 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
+1 -7
drivers/usb/typec/class.c
··· 210 210 return dev_fwnode(dev) == fwnode; 211 211 } 212 212 213 - static int typec_port_name_match(struct device *dev, const void *name) 214 - { 215 - return !strcmp((const char *)name, dev_name(dev)); 216 - } 217 - 218 213 static void *typec_port_match(struct device_connection *con, int ep, void *data) 219 214 { 220 215 struct device *dev; ··· 222 227 return class_find_device(typec_class, NULL, con->fwnode, 223 228 typec_port_fwnode_match); 224 229 225 - dev = class_find_device(typec_class, NULL, con->endpoint[ep], 226 - typec_port_name_match); 230 + dev = class_find_device_by_name(typec_class, con->endpoint[ep]); 227 231 228 232 return dev ? dev : ERR_PTR(-EPROBE_DEFER); 229 233 }
+39 -3
include/linux/device.h
··· 164 164 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter); 165 165 void subsys_dev_iter_exit(struct subsys_dev_iter *iter); 166 166 167 + int device_match_name(struct device *dev, const void *name); 167 168 int device_match_of_node(struct device *dev, const void *np); 168 169 169 170 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, ··· 172 171 struct device *bus_find_device(struct bus_type *bus, struct device *start, 173 172 const void *data, 174 173 int (*match)(struct device *dev, const void *data)); 175 - struct device *bus_find_device_by_name(struct bus_type *bus, 176 - struct device *start, 177 - const char *name); 174 + /** 175 + * bus_find_device_by_name - device iterator for locating a particular device 176 + * of a specific name. 177 + * @bus: bus type 178 + * @start: Device to begin with 179 + * @name: name of the device to match 180 + */ 181 + static inline struct device *bus_find_device_by_name(struct bus_type *bus, 182 + struct device *start, 183 + const char *name) 184 + { 185 + return bus_find_device(bus, start, name, device_match_name); 186 + } 187 + 178 188 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id, 179 189 struct device *hint); 180 190 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, ··· 354 342 struct device *start, const void *data, 355 343 int (*match)(struct device *dev, const void *data)); 356 344 345 + /** 346 + * driver_find_device_by_name - device iterator for locating a particular device 347 + * of a specific name. 348 + * @driver: the driver we're iterating 349 + * @name: name of the device to match 350 + */ 351 + static inline struct device *driver_find_device_by_name(struct device_driver *drv, 352 + const char *name) 353 + { 354 + return driver_find_device(drv, NULL, name, device_match_name); 355 + } 356 + 357 357 void driver_deferred_probe_add(struct device *dev); 358 358 int driver_deferred_probe_check_state(struct device *dev); 359 359 int driver_deferred_probe_check_state_continue(struct device *dev); ··· 494 470 extern struct device *class_find_device(struct class *class, 495 471 struct device *start, const void *data, 496 472 int (*match)(struct device *, const void *)); 473 + 474 + /** 475 + * class_find_device_by_name - device iterator for locating a particular device 476 + * of a specific name. 477 + * @class: class type 478 + * @name: name of the device to match 479 + */ 480 + static inline struct device *class_find_device_by_name(struct class *class, 481 + const char *name) 482 + { 483 + return class_find_device(class, NULL, name, device_match_name); 484 + } 497 485 498 486 struct class_attribute { 499 487 struct attribute attr;
+1 -6
net/ieee802154/core.c
··· 23 23 LIST_HEAD(cfg802154_rdev_list); 24 24 int cfg802154_rdev_list_generation; 25 25 26 - static int wpan_phy_match(struct device *dev, const void *data) 27 - { 28 - return !strcmp(dev_name(dev), (const char *)data); 29 - } 30 - 31 26 struct wpan_phy *wpan_phy_find(const char *str) 32 27 { 33 28 struct device *dev; ··· 30 35 if (WARN_ON(!str)) 31 36 return NULL; 32 37 33 - dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match); 38 + dev = class_find_device_by_name(&wpan_phy_class, str); 34 39 if (!dev) 35 40 return NULL; 36 41