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