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

drivers/base: Introduce device_match_t for device finding APIs

There are several drivers/base APIs for finding a specific device, and
they currently use the following good type for the @match parameter:
int (*match)(struct device *dev, const void *data)

Since these operations do not modify the caller-provided @*data, this
type is worthy of a dedicated typedef:
typedef int (*device_match_t)(struct device *dev, const void *data)

Advantages of using device_match_t:
- Shorter API declarations and definitions
- Prevent further APIs from using a bad type for @match

So introduce device_match_t and apply it to the existing
(bus|class|driver|auxiliary)_find_device() APIs.

Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
Link: https://lore.kernel.org/r/20240813-dev_match_api-v3-1-6c6878a99b9f@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Zijun Hu and committed by
Greg Kroah-Hartman
b45ed06f f0e5311a

+11 -10
+1 -1
drivers/base/auxiliary.c
··· 352 352 */ 353 353 struct auxiliary_device *auxiliary_find_device(struct device *start, 354 354 const void *data, 355 - int (*match)(struct device *dev, const void *data)) 355 + device_match_t match) 356 356 { 357 357 struct device *dev; 358 358
+1 -1
drivers/base/bus.c
··· 391 391 */ 392 392 struct device *bus_find_device(const struct bus_type *bus, 393 393 struct device *start, const void *data, 394 - int (*match)(struct device *dev, const void *data)) 394 + device_match_t match) 395 395 { 396 396 struct subsys_private *sp = bus_to_subsys(bus); 397 397 struct klist_iter i;
+1 -2
drivers/base/class.c
··· 433 433 * code. There's no locking restriction. 434 434 */ 435 435 struct device *class_find_device(const struct class *class, const struct device *start, 436 - const void *data, 437 - int (*match)(struct device *, const void *)) 436 + const void *data, device_match_t match) 438 437 { 439 438 struct subsys_private *sp = class_to_subsys(class); 440 439 struct class_dev_iter iter;
+1 -1
drivers/base/driver.c
··· 150 150 */ 151 151 struct device *driver_find_device(const struct device_driver *drv, 152 152 struct device *start, const void *data, 153 - int (*match)(struct device *dev, const void *data)) 153 + device_match_t match) 154 154 { 155 155 struct klist_iter i; 156 156 struct device *dev;
+1 -1
include/linux/auxiliary_bus.h
··· 271 271 272 272 struct auxiliary_device *auxiliary_find_device(struct device *start, 273 273 const void *data, 274 - int (*match)(struct device *dev, const void *data)); 274 + device_match_t match); 275 275 276 276 #endif /* _AUXILIARY_BUS_H_ */
+4 -2
include/linux/device/bus.h
··· 126 126 int __must_check bus_create_file(const struct bus_type *bus, struct bus_attribute *attr); 127 127 void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr); 128 128 129 + /* Matching function type for drivers/base APIs to find a specific device */ 130 + typedef int (*device_match_t)(struct device *dev, const void *data); 131 + 129 132 /* Generic device matching functions that all busses can use to match with */ 130 133 int device_match_name(struct device *dev, const void *name); 131 134 int device_match_of_node(struct device *dev, const void *np); ··· 142 139 int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, 143 140 int (*fn)(struct device *dev, void *data)); 144 141 struct device *bus_find_device(const struct bus_type *bus, struct device *start, 145 - const void *data, 146 - int (*match)(struct device *dev, const void *data)); 142 + const void *data, device_match_t match); 147 143 /** 148 144 * bus_find_device_by_name - device iterator for locating a particular device 149 145 * of a specific name.
+1 -1
include/linux/device/class.h
··· 95 95 int class_for_each_device(const struct class *class, const struct device *start, void *data, 96 96 int (*fn)(struct device *dev, void *data)); 97 97 struct device *class_find_device(const struct class *class, const struct device *start, 98 - const void *data, int (*match)(struct device *, const void *)); 98 + const void *data, device_match_t match); 99 99 100 100 /** 101 101 * class_find_device_by_name - device iterator for locating a particular device
+1 -1
include/linux/device/driver.h
··· 157 157 void *data, int (*fn)(struct device *dev, void *)); 158 158 struct device *driver_find_device(const struct device_driver *drv, 159 159 struct device *start, const void *data, 160 - int (*match)(struct device *dev, const void *data)); 160 + device_match_t match); 161 161 162 162 /** 163 163 * driver_find_device_by_name - device iterator for locating a particular device