summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSuzuki K Poulose <suzuki.poulose@arm.com>2019-07-23 18:18:32 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2019-07-30 07:07:41 -0400
commit6cda08a20dbde45b021091230c8a359fa08c5103 (patch)
tree6017a1b2e4a83b7fd330d404691e33da0df2726a
parent5f9e832c137075045d15cd6899ab0505cfb2ca4b (diff)
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>
-rw-r--r--drivers/base/bus.c24
-rw-r--r--drivers/base/core.c6
-rw-r--r--drivers/hwtracing/stm/core.c9
-rw-r--r--drivers/leds/led-class.c9
-rw-r--r--drivers/rtc/interface.c11
-rw-r--r--drivers/s390/cio/ccwgroup.c10
-rw-r--r--drivers/s390/cio/device.c15
-rw-r--r--drivers/s390/crypto/zcrypt_api.c11
-rw-r--r--drivers/usb/roles/class.c8
-rw-r--r--drivers/usb/typec/class.c8
-rw-r--r--include/linux/device.h42
-rw-r--r--net/ieee802154/core.c7
12 files changed, 54 insertions, 106 deletions
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index df3cac739813..a1d1e8256324 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -342,30 +342,6 @@ struct device *bus_find_device(struct bus_type *bus,
342} 342}
343EXPORT_SYMBOL_GPL(bus_find_device); 343EXPORT_SYMBOL_GPL(bus_find_device);
344 344
345static 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 */
362struct 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}
367EXPORT_SYMBOL_GPL(bus_find_device_by_name);
368
369/** 345/**
370 * subsys_find_device_by_id - find a device with a specific enumeration number 346 * subsys_find_device_by_id - find a device with a specific enumeration number
371 * @subsys: subsystem 347 * @subsys: subsystem
diff --git a/drivers/base/core.c b/drivers/base/core.c
index da84a73f2ba6..fb83647d685a 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -3357,6 +3357,12 @@ void device_set_of_node_from_dev(struct device *dev, const struct device *dev2)
3357} 3357}
3358EXPORT_SYMBOL_GPL(device_set_of_node_from_dev); 3358EXPORT_SYMBOL_GPL(device_set_of_node_from_dev);
3359 3359
3360int device_match_name(struct device *dev, const void *name)
3361{
3362 return sysfs_streq(dev_name(dev), name);
3363}
3364EXPORT_SYMBOL_GPL(device_match_name);
3365
3360int device_match_of_node(struct device *dev, const void *np) 3366int device_match_of_node(struct device *dev, const void *np)
3361{ 3367{
3362 return dev->of_node == np; 3368 return dev->of_node == np;
diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c
index e55b902560de..2b6bd42632e8 100644
--- a/drivers/hwtracing/stm/core.c
+++ b/drivers/hwtracing/stm/core.c
@@ -89,13 +89,6 @@ static struct class stm_class = {
89 .dev_groups = stm_groups, 89 .dev_groups = stm_groups,
90}; 90};
91 91
92static 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 * stm_find_device() - find stm device by name 93 * stm_find_device() - find stm device by name
101 * @buf: character buffer containing the name 94 * @buf: character buffer containing the name
@@ -116,7 +109,7 @@ struct stm_device *stm_find_device(const char *buf)
116 if (!stm_core_up) 109 if (!stm_core_up)
117 return NULL; 110 return NULL;
118 111
119 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match); 112 dev = class_find_device_by_name(&stm_class, buf);
120 if (!dev) 113 if (!dev)
121 return NULL; 114 return NULL;
122 115
diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 4793e77808e2..d54c8e4d8954 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -213,13 +213,6 @@ static int led_resume(struct device *dev)
213 213
214static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume); 214static SIMPLE_DEV_PM_OPS(leds_class_dev_pm_ops, led_suspend, led_resume);
215 215
216static 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
223static int led_classdev_next_name(const char *init_name, char *name, 216static int led_classdev_next_name(const char *init_name, char *name,
224 size_t len) 217 size_t len)
225{ 218{
@@ -230,7 +223,7 @@ static int led_classdev_next_name(const char *init_name, char *name,
230 strlcpy(name, init_name, len); 223 strlcpy(name, init_name, len);
231 224
232 while ((ret < len) && 225 while ((ret < len) &&
233 (dev = class_find_device(leds_class, NULL, name, match_name))) { 226 (dev = class_find_device_by_name(leds_class, name))) {
234 put_device(dev); 227 put_device(dev);
235 ret = snprintf(name, len, "%s_%u", init_name, ++i); 228 ret = snprintf(name, len, "%s_%u", init_name, ++i);
236 } 229 }
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 72b7ddc43116..c93ef33b01d3 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -663,21 +663,12 @@ void rtc_update_irq(struct rtc_device *rtc,
663} 663}
664EXPORT_SYMBOL_GPL(rtc_update_irq); 664EXPORT_SYMBOL_GPL(rtc_update_irq);
665 665
666static 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
675struct rtc_device *rtc_class_open(const char *name) 666struct rtc_device *rtc_class_open(const char *name)
676{ 667{
677 struct device *dev; 668 struct device *dev;
678 struct rtc_device *rtc = NULL; 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 if (dev) 672 if (dev)
682 rtc = to_rtc_device(dev); 673 rtc = to_rtc_device(dev);
683 674
diff --git a/drivers/s390/cio/ccwgroup.c b/drivers/s390/cio/ccwgroup.c
index c522e9313c50..d843e362c167 100644
--- a/drivers/s390/cio/ccwgroup.c
+++ b/drivers/s390/cio/ccwgroup.c
@@ -608,13 +608,6 @@ void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
608} 608}
609EXPORT_SYMBOL(ccwgroup_driver_unregister); 609EXPORT_SYMBOL(ccwgroup_driver_unregister);
610 610
611static 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 * get_ccwgroupdev_by_busid() - obtain device from a bus id 612 * get_ccwgroupdev_by_busid() - obtain device from a bus id
620 * @gdrv: driver the device is owned by 613 * @gdrv: driver the device is owned by
@@ -631,8 +624,7 @@ struct ccwgroup_device *get_ccwgroupdev_by_busid(struct ccwgroup_driver *gdrv,
631{ 624{
632 struct device *dev; 625 struct device *dev;
633 626
634 dev = driver_find_device(&gdrv->driver, NULL, bus_id, 627 dev = driver_find_device_by_name(&gdrv->driver, bus_id);
635 __ccwgroupdev_check_busid);
636 628
637 return dev ? to_ccwgroupdev(dev) : NULL; 629 return dev ? to_ccwgroupdev(dev) : NULL;
638} 630}
diff --git a/drivers/s390/cio/device.c b/drivers/s390/cio/device.c
index c421899be20f..131430bd48d9 100644
--- a/drivers/s390/cio/device.c
+++ b/drivers/s390/cio/device.c
@@ -1695,18 +1695,6 @@ int ccw_device_force_console(struct ccw_device *cdev)
1695EXPORT_SYMBOL_GPL(ccw_device_force_console); 1695EXPORT_SYMBOL_GPL(ccw_device_force_console);
1696#endif 1696#endif
1697 1697
1698/*
1699 * get ccw_device matching the busid, but only if owned by cdrv
1700 */
1701static 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 * get_ccwdev_by_busid() - obtain device from a bus id 1699 * get_ccwdev_by_busid() - obtain device from a bus id
1712 * @cdrv: driver the device is owned by 1700 * @cdrv: driver the device is owned by
@@ -1723,8 +1711,7 @@ struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1723{ 1711{
1724 struct device *dev; 1712 struct device *dev;
1725 1713
1726 dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id, 1714 dev = driver_find_device_by_name(&cdrv->driver, bus_id);
1727 __ccwdev_check_busid);
1728 1715
1729 return dev ? to_ccwdev(dev) : NULL; 1716 return dev ? to_ccwdev(dev) : NULL;
1730} 1717}
diff --git a/drivers/s390/crypto/zcrypt_api.c b/drivers/s390/crypto/zcrypt_api.c
index 1058b4b5cc1e..38a5a47b8c9c 100644
--- a/drivers/s390/crypto/zcrypt_api.c
+++ b/drivers/s390/crypto/zcrypt_api.c
@@ -133,12 +133,6 @@ struct zcdn_device {
133static int zcdn_create(const char *name); 133static int zcdn_create(const char *name);
134static int zcdn_destroy(const char *name); 134static int zcdn_destroy(const char *name);
135 135
136/* helper function, matches the name for find_zcdndev_by_name() */
137static int __match_zcdn_name(struct device *dev, const void *data)
138{
139 return strcmp(dev_name(dev), (const char *)data) == 0;
140}
141
142/* helper function, matches the devt value for find_zcdndev_by_devt() */ 136/* helper function, matches the devt value for find_zcdndev_by_devt() */
143static int __match_zcdn_devt(struct device *dev, const void *data) 137static int __match_zcdn_devt(struct device *dev, const void *data)
144{ 138{
@@ -152,10 +146,7 @@ static int __match_zcdn_devt(struct device *dev, const void *data)
152 */ 146 */
153static inline struct zcdn_device *find_zcdndev_by_name(const char *name) 147static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
154{ 148{
155 struct device *dev = 149 struct device *dev = class_find_device_by_name(zcrypt_class, name);
156 class_find_device(zcrypt_class, NULL,
157 (void *) name,
158 __match_zcdn_name);
159 150
160 return dev ? to_zcdn_dev(dev) : NULL; 151 return dev ? to_zcdn_dev(dev) : NULL;
161} 152}
diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
index 86defca6623e..c8efe60e2465 100644
--- a/drivers/usb/roles/class.c
+++ b/drivers/usb/roles/class.c
@@ -90,11 +90,6 @@ static int switch_fwnode_match(struct device *dev, const void *fwnode)
90 return dev_fwnode(dev) == fwnode; 90 return dev_fwnode(dev) == fwnode;
91} 91}
92 92
93static int switch_name_match(struct device *dev, const void *name)
94{
95 return !strcmp((const char *)name, dev_name(dev));
96}
97
98static void *usb_role_switch_match(struct device_connection *con, int ep, 93static void *usb_role_switch_match(struct device_connection *con, int ep,
99 void *data) 94 void *data)
100{ 95{
@@ -107,8 +102,7 @@ static void *usb_role_switch_match(struct device_connection *con, int ep,
107 dev = class_find_device(role_class, NULL, con->fwnode, 102 dev = class_find_device(role_class, NULL, con->fwnode,
108 switch_fwnode_match); 103 switch_fwnode_match);
109 } else { 104 } else {
110 dev = class_find_device(role_class, NULL, con->endpoint[ep], 105 dev = class_find_device_by_name(role_class, con->endpoint[ep]);
111 switch_name_match);
112 } 106 }
113 107
114 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); 108 return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index a18285a990a8..9b0d15b487e5 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -210,11 +210,6 @@ static int typec_port_fwnode_match(struct device *dev, const void *fwnode)
210 return dev_fwnode(dev) == fwnode; 210 return dev_fwnode(dev) == fwnode;
211} 211}
212 212
213static int typec_port_name_match(struct device *dev, const void *name)
214{
215 return !strcmp((const char *)name, dev_name(dev));
216}
217
218static void *typec_port_match(struct device_connection *con, int ep, void *data) 213static void *typec_port_match(struct device_connection *con, int ep, void *data)
219{ 214{
220 struct device *dev; 215 struct device *dev;
@@ -227,8 +222,7 @@ static void *typec_port_match(struct device_connection *con, int ep, void *data)
227 return class_find_device(typec_class, NULL, con->fwnode, 222 return class_find_device(typec_class, NULL, con->fwnode,
228 typec_port_fwnode_match); 223 typec_port_fwnode_match);
229 224
230 dev = class_find_device(typec_class, NULL, con->endpoint[ep], 225 dev = class_find_device_by_name(typec_class, con->endpoint[ep]);
231 typec_port_name_match);
232 226
233 return dev ? dev : ERR_PTR(-EPROBE_DEFER); 227 return dev ? dev : ERR_PTR(-EPROBE_DEFER);
234} 228}
diff --git a/include/linux/device.h b/include/linux/device.h
index c330b75c6c57..3ba376b8b456 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -164,6 +164,7 @@ void subsys_dev_iter_init(struct subsys_dev_iter *iter,
164struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter); 164struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
165void subsys_dev_iter_exit(struct subsys_dev_iter *iter); 165void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
166 166
167int device_match_name(struct device *dev, const void *name);
167int device_match_of_node(struct device *dev, const void *np); 168int device_match_of_node(struct device *dev, const void *np);
168 169
169int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, 170int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
@@ -171,9 +172,20 @@ int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
171struct device *bus_find_device(struct bus_type *bus, struct device *start, 172struct device *bus_find_device(struct bus_type *bus, struct device *start,
172 const void *data, 173 const void *data,
173 int (*match)(struct device *dev, const void *data)); 174 int (*match)(struct device *dev, const void *data));
174struct device *bus_find_device_by_name(struct bus_type *bus, 175/**
175 struct device *start, 176 * bus_find_device_by_name - device iterator for locating a particular device
176 const char *name); 177 * of a specific name.
178 * @bus: bus type
179 * @start: Device to begin with
180 * @name: name of the device to match
181 */
182static inline struct device *bus_find_device_by_name(struct bus_type *bus,
183 struct device *start,
184 const char *name)
185{
186 return bus_find_device(bus, start, name, device_match_name);
187}
188
177struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id, 189struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
178 struct device *hint); 190 struct device *hint);
179int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 191int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
@@ -342,6 +354,18 @@ struct device *driver_find_device(struct device_driver *drv,
342 struct device *start, const void *data, 354 struct device *start, const void *data,
343 int (*match)(struct device *dev, const void *data)); 355 int (*match)(struct device *dev, const void *data));
344 356
357/**
358 * driver_find_device_by_name - device iterator for locating a particular device
359 * of a specific name.
360 * @driver: the driver we're iterating
361 * @name: name of the device to match
362 */
363static inline struct device *driver_find_device_by_name(struct device_driver *drv,
364 const char *name)
365{
366 return driver_find_device(drv, NULL, name, device_match_name);
367}
368
345void driver_deferred_probe_add(struct device *dev); 369void driver_deferred_probe_add(struct device *dev);
346int driver_deferred_probe_check_state(struct device *dev); 370int driver_deferred_probe_check_state(struct device *dev);
347int driver_deferred_probe_check_state_continue(struct device *dev); 371int driver_deferred_probe_check_state_continue(struct device *dev);
@@ -471,6 +495,18 @@ extern struct device *class_find_device(struct class *class,
471 struct device *start, const void *data, 495 struct device *start, const void *data,
472 int (*match)(struct device *, const void *)); 496 int (*match)(struct device *, const void *));
473 497
498/**
499 * class_find_device_by_name - device iterator for locating a particular device
500 * of a specific name.
501 * @class: class type
502 * @name: name of the device to match
503 */
504static inline struct device *class_find_device_by_name(struct class *class,
505 const char *name)
506{
507 return class_find_device(class, NULL, name, device_match_name);
508}
509
474struct class_attribute { 510struct class_attribute {
475 struct attribute attr; 511 struct attribute attr;
476 ssize_t (*show)(struct class *class, struct class_attribute *attr, 512 ssize_t (*show)(struct class *class, struct class_attribute *attr,
diff --git a/net/ieee802154/core.c b/net/ieee802154/core.c
index 60b7ac56a1f5..de259b5170ab 100644
--- a/net/ieee802154/core.c
+++ b/net/ieee802154/core.c
@@ -23,11 +23,6 @@
23LIST_HEAD(cfg802154_rdev_list); 23LIST_HEAD(cfg802154_rdev_list);
24int cfg802154_rdev_list_generation; 24int cfg802154_rdev_list_generation;
25 25
26static int wpan_phy_match(struct device *dev, const void *data)
27{
28 return !strcmp(dev_name(dev), (const char *)data);
29}
30
31struct wpan_phy *wpan_phy_find(const char *str) 26struct wpan_phy *wpan_phy_find(const char *str)
32{ 27{
33 struct device *dev; 28 struct device *dev;
@@ -35,7 +30,7 @@ struct wpan_phy *wpan_phy_find(const char *str)
35 if (WARN_ON(!str)) 30 if (WARN_ON(!str))
36 return NULL; 31 return NULL;
37 32
38 dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match); 33 dev = class_find_device_by_name(&wpan_phy_class, str);
39 if (!dev) 34 if (!dev)
40 return NULL; 35 return NULL;
41 36