• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * driver.c - centralized device driver management
4  *
5  * Copyright (c) 2002-3 Patrick Mochel
6  * Copyright (c) 2002-3 Open Source Development Labs
7  * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
8  * Copyright (c) 2007 Novell Inc.
9  */
10 
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/sysfs.h>
17 #include "base.h"
18 
next_device(struct klist_iter * i)19 static struct device *next_device(struct klist_iter *i)
20 {
21 	struct klist_node *n = klist_next(i);
22 	struct device *dev = NULL;
23 	struct device_private *dev_prv;
24 
25 	if (n) {
26 		dev_prv = to_device_private_driver(n);
27 		dev = dev_prv->device;
28 	}
29 	return dev;
30 }
31 
32 /**
33  * driver_set_override() - Helper to set or clear driver override.
34  * @dev: Device to change
35  * @override: Address of string to change (e.g. &device->driver_override);
36  *            The contents will be freed and hold newly allocated override.
37  * @s: NUL-terminated string, new driver name to force a match, pass empty
38  *     string to clear it ("" or "\n", where the latter is only for sysfs
39  *     interface).
40  * @len: length of @s
41  *
42  * Helper to set or clear driver override in a device, intended for the cases
43  * when the driver_override field is allocated by driver/bus code.
44  *
45  * Returns: 0 on success or a negative error code on failure.
46  */
driver_set_override(struct device * dev,const char ** override,const char * s,size_t len)47 int driver_set_override(struct device *dev, const char **override,
48 			const char *s, size_t len)
49 {
50 	const char *new, *old;
51 	char *cp;
52 
53 	if (!override || !s)
54 		return -EINVAL;
55 
56 	/*
57 	 * The stored value will be used in sysfs show callback (sysfs_emit()),
58 	 * which has a length limit of PAGE_SIZE and adds a trailing newline.
59 	 * Thus we can store one character less to avoid truncation during sysfs
60 	 * show.
61 	 */
62 	if (len >= (PAGE_SIZE - 1))
63 		return -EINVAL;
64 
65 	if (!len) {
66 		/* Empty string passed - clear override */
67 		device_lock(dev);
68 		old = *override;
69 		*override = NULL;
70 		device_unlock(dev);
71 		kfree(old);
72 
73 		return 0;
74 	}
75 
76 	cp = strnchr(s, len, '\n');
77 	if (cp)
78 		len = cp - s;
79 
80 	new = kstrndup(s, len, GFP_KERNEL);
81 	if (!new)
82 		return -ENOMEM;
83 
84 	device_lock(dev);
85 	old = *override;
86 	if (cp != s) {
87 		*override = new;
88 	} else {
89 		/* "\n" passed - clear override */
90 		kfree(new);
91 		*override = NULL;
92 	}
93 	device_unlock(dev);
94 
95 	kfree(old);
96 
97 	return 0;
98 }
99 EXPORT_SYMBOL_GPL(driver_set_override);
100 
101 /**
102  * driver_for_each_device - Iterator for devices bound to a driver.
103  * @drv: Driver we're iterating.
104  * @start: Device to begin with
105  * @data: Data to pass to the callback.
106  * @fn: Function to call for each device.
107  *
108  * Iterate over the @drv's list of devices calling @fn for each one.
109  */
driver_for_each_device(struct device_driver * drv,struct device * start,void * data,int (* fn)(struct device *,void *))110 int driver_for_each_device(struct device_driver *drv, struct device *start,
111 			   void *data, int (*fn)(struct device *, void *))
112 {
113 	struct klist_iter i;
114 	struct device *dev;
115 	int error = 0;
116 
117 	if (!drv)
118 		return -EINVAL;
119 
120 	klist_iter_init_node(&drv->p->klist_devices, &i,
121 			     start ? &start->p->knode_driver : NULL);
122 	while (!error && (dev = next_device(&i)))
123 		error = fn(dev, data);
124 	klist_iter_exit(&i);
125 	return error;
126 }
127 EXPORT_SYMBOL_GPL(driver_for_each_device);
128 
129 /**
130  * driver_find_device - device iterator for locating a particular device.
131  * @drv: The device's driver
132  * @start: Device to begin with
133  * @data: Data to pass to match function
134  * @match: Callback function to check device
135  *
136  * This is similar to the driver_for_each_device() function above, but
137  * it returns a reference to a device that is 'found' for later use, as
138  * determined by the @match callback.
139  *
140  * The callback should return 0 if the device doesn't match and non-zero
141  * if it does.  If the callback returns non-zero, this function will
142  * return to the caller and not iterate over any more devices.
143  */
driver_find_device(struct device_driver * drv,struct device * start,const void * data,int (* match)(struct device * dev,const void * data))144 struct device *driver_find_device(struct device_driver *drv,
145 				  struct device *start, const void *data,
146 				  int (*match)(struct device *dev, const void *data))
147 {
148 	struct klist_iter i;
149 	struct device *dev;
150 
151 	if (!drv || !drv->p)
152 		return NULL;
153 
154 	klist_iter_init_node(&drv->p->klist_devices, &i,
155 			     (start ? &start->p->knode_driver : NULL));
156 	while ((dev = next_device(&i)))
157 		if (match(dev, data) && get_device(dev))
158 			break;
159 	klist_iter_exit(&i);
160 	return dev;
161 }
162 EXPORT_SYMBOL_GPL(driver_find_device);
163 
164 /**
165  * driver_create_file - create sysfs file for driver.
166  * @drv: driver.
167  * @attr: driver attribute descriptor.
168  */
driver_create_file(struct device_driver * drv,const struct driver_attribute * attr)169 int driver_create_file(struct device_driver *drv,
170 		       const struct driver_attribute *attr)
171 {
172 	int error;
173 
174 	if (drv)
175 		error = sysfs_create_file(&drv->p->kobj, &attr->attr);
176 	else
177 		error = -EINVAL;
178 	return error;
179 }
180 EXPORT_SYMBOL_GPL(driver_create_file);
181 
182 /**
183  * driver_remove_file - remove sysfs file for driver.
184  * @drv: driver.
185  * @attr: driver attribute descriptor.
186  */
driver_remove_file(struct device_driver * drv,const struct driver_attribute * attr)187 void driver_remove_file(struct device_driver *drv,
188 			const struct driver_attribute *attr)
189 {
190 	if (drv)
191 		sysfs_remove_file(&drv->p->kobj, &attr->attr);
192 }
193 EXPORT_SYMBOL_GPL(driver_remove_file);
194 
driver_add_groups(struct device_driver * drv,const struct attribute_group ** groups)195 int driver_add_groups(struct device_driver *drv,
196 		      const struct attribute_group **groups)
197 {
198 	return sysfs_create_groups(&drv->p->kobj, groups);
199 }
200 
driver_remove_groups(struct device_driver * drv,const struct attribute_group ** groups)201 void driver_remove_groups(struct device_driver *drv,
202 			  const struct attribute_group **groups)
203 {
204 	sysfs_remove_groups(&drv->p->kobj, groups);
205 }
206 
207 /**
208  * driver_register - register driver with bus
209  * @drv: driver to register
210  *
211  * We pass off most of the work to the bus_add_driver() call,
212  * since most of the things we have to do deal with the bus
213  * structures.
214  */
driver_register(struct device_driver * drv)215 int driver_register(struct device_driver *drv)
216 {
217 	int ret;
218 	struct device_driver *other;
219 
220 	if (!drv->bus->p) {
221 		pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
222 			   drv->name, drv->bus->name);
223 		return -EINVAL;
224 	}
225 
226 	if ((drv->bus->probe && drv->probe) ||
227 	    (drv->bus->remove && drv->remove) ||
228 	    (drv->bus->shutdown && drv->shutdown))
229 		printk(KERN_WARNING "Driver '%s' needs updating - please use "
230 			"bus_type methods\n", drv->name);
231 
232 	other = driver_find(drv->name, drv->bus);
233 	if (other) {
234 		printk(KERN_ERR "Error: Driver '%s' is already registered, "
235 			"aborting...\n", drv->name);
236 		return -EBUSY;
237 	}
238 
239 	ret = bus_add_driver(drv);
240 	if (ret)
241 		return ret;
242 	ret = driver_add_groups(drv, drv->groups);
243 	if (ret) {
244 		bus_remove_driver(drv);
245 		return ret;
246 	}
247 	kobject_uevent(&drv->p->kobj, KOBJ_ADD);
248 
249 	return ret;
250 }
251 EXPORT_SYMBOL_GPL(driver_register);
252 
253 /**
254  * driver_unregister - remove driver from system.
255  * @drv: driver.
256  *
257  * Again, we pass off most of the work to the bus-level call.
258  */
driver_unregister(struct device_driver * drv)259 void driver_unregister(struct device_driver *drv)
260 {
261 	if (!drv || !drv->p) {
262 		WARN(1, "Unexpected driver unregister!\n");
263 		return;
264 	}
265 	driver_remove_groups(drv, drv->groups);
266 	bus_remove_driver(drv);
267 }
268 EXPORT_SYMBOL_GPL(driver_unregister);
269 
270 /**
271  * driver_find - locate driver on a bus by its name.
272  * @name: name of the driver.
273  * @bus: bus to scan for the driver.
274  *
275  * Call kset_find_obj() to iterate over list of drivers on
276  * a bus to find driver by name. Return driver if found.
277  *
278  * This routine provides no locking to prevent the driver it returns
279  * from being unregistered or unloaded while the caller is using it.
280  * The caller is responsible for preventing this.
281  */
driver_find(const char * name,struct bus_type * bus)282 struct device_driver *driver_find(const char *name, struct bus_type *bus)
283 {
284 	struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
285 	struct driver_private *priv;
286 
287 	if (k) {
288 		/* Drop reference added by kset_find_obj() */
289 		kobject_put(k);
290 		priv = to_driver(k);
291 		return priv->driver;
292 	}
293 	return NULL;
294 }
295 EXPORT_SYMBOL_GPL(driver_find);
296