xref: /openbmc/linux/drivers/base/driver.c (revision 384740dc)
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/string.h>
17 #include "base.h"
18 
19 static struct device *next_device(struct klist_iter *i)
20 {
21 	struct klist_node *n = klist_next(i);
22 	return n ? container_of(n, struct device, knode_driver) : NULL;
23 }
24 
25 /**
26  * driver_for_each_device - Iterator for devices bound to a driver.
27  * @drv: Driver we're iterating.
28  * @start: Device to begin with
29  * @data: Data to pass to the callback.
30  * @fn: Function to call for each device.
31  *
32  * Iterate over the @drv's list of devices calling @fn for each one.
33  */
34 int driver_for_each_device(struct device_driver *drv, struct device *start,
35 			   void *data, int (*fn)(struct device *, void *))
36 {
37 	struct klist_iter i;
38 	struct device *dev;
39 	int error = 0;
40 
41 	if (!drv)
42 		return -EINVAL;
43 
44 	klist_iter_init_node(&drv->p->klist_devices, &i,
45 			     start ? &start->knode_driver : NULL);
46 	while ((dev = next_device(&i)) && !error)
47 		error = fn(dev, data);
48 	klist_iter_exit(&i);
49 	return error;
50 }
51 EXPORT_SYMBOL_GPL(driver_for_each_device);
52 
53 /**
54  * driver_find_device - device iterator for locating a particular device.
55  * @drv: The device's driver
56  * @start: Device to begin with
57  * @data: Data to pass to match function
58  * @match: Callback function to check device
59  *
60  * This is similar to the driver_for_each_device() function above, but
61  * it returns a reference to a device that is 'found' for later use, as
62  * determined by the @match callback.
63  *
64  * The callback should return 0 if the device doesn't match and non-zero
65  * if it does.  If the callback returns non-zero, this function will
66  * return to the caller and not iterate over any more devices.
67  */
68 struct device *driver_find_device(struct device_driver *drv,
69 				  struct device *start, void *data,
70 				  int (*match)(struct device *dev, void *data))
71 {
72 	struct klist_iter i;
73 	struct device *dev;
74 
75 	if (!drv)
76 		return NULL;
77 
78 	klist_iter_init_node(&drv->p->klist_devices, &i,
79 			     (start ? &start->knode_driver : NULL));
80 	while ((dev = next_device(&i)))
81 		if (match(dev, data) && get_device(dev))
82 			break;
83 	klist_iter_exit(&i);
84 	return dev;
85 }
86 EXPORT_SYMBOL_GPL(driver_find_device);
87 
88 /**
89  * driver_create_file - create sysfs file for driver.
90  * @drv: driver.
91  * @attr: driver attribute descriptor.
92  */
93 int driver_create_file(struct device_driver *drv,
94 		       struct driver_attribute *attr)
95 {
96 	int error;
97 	if (drv)
98 		error = sysfs_create_file(&drv->p->kobj, &attr->attr);
99 	else
100 		error = -EINVAL;
101 	return error;
102 }
103 EXPORT_SYMBOL_GPL(driver_create_file);
104 
105 /**
106  * driver_remove_file - remove sysfs file for driver.
107  * @drv: driver.
108  * @attr: driver attribute descriptor.
109  */
110 void driver_remove_file(struct device_driver *drv,
111 			struct driver_attribute *attr)
112 {
113 	if (drv)
114 		sysfs_remove_file(&drv->p->kobj, &attr->attr);
115 }
116 EXPORT_SYMBOL_GPL(driver_remove_file);
117 
118 /**
119  * driver_add_kobj - add a kobject below the specified driver
120  * @drv: requesting device driver
121  * @kobj: kobject to add below this driver
122  * @fmt: format string that names the kobject
123  *
124  * You really don't want to do this, this is only here due to one looney
125  * iseries driver, go poke those developers if you are annoyed about
126  * this...
127  */
128 int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
129 		    const char *fmt, ...)
130 {
131 	va_list args;
132 	char *name;
133 	int ret;
134 
135 	va_start(args, fmt);
136 	name = kvasprintf(GFP_KERNEL, fmt, args);
137 	va_end(args);
138 
139 	if (!name)
140 		return -ENOMEM;
141 
142 	ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
143 	kfree(name);
144 	return ret;
145 }
146 EXPORT_SYMBOL_GPL(driver_add_kobj);
147 
148 /**
149  * get_driver - increment driver reference count.
150  * @drv: driver.
151  */
152 struct device_driver *get_driver(struct device_driver *drv)
153 {
154 	if (drv) {
155 		struct driver_private *priv;
156 		struct kobject *kobj;
157 
158 		kobj = kobject_get(&drv->p->kobj);
159 		priv = to_driver(kobj);
160 		return priv->driver;
161 	}
162 	return NULL;
163 }
164 EXPORT_SYMBOL_GPL(get_driver);
165 
166 /**
167  * put_driver - decrement driver's refcount.
168  * @drv: driver.
169  */
170 void put_driver(struct device_driver *drv)
171 {
172 	kobject_put(&drv->p->kobj);
173 }
174 EXPORT_SYMBOL_GPL(put_driver);
175 
176 static int driver_add_groups(struct device_driver *drv,
177 			     struct attribute_group **groups)
178 {
179 	int error = 0;
180 	int i;
181 
182 	if (groups) {
183 		for (i = 0; groups[i]; i++) {
184 			error = sysfs_create_group(&drv->p->kobj, groups[i]);
185 			if (error) {
186 				while (--i >= 0)
187 					sysfs_remove_group(&drv->p->kobj,
188 							   groups[i]);
189 				break;
190 			}
191 		}
192 	}
193 	return error;
194 }
195 
196 static void driver_remove_groups(struct device_driver *drv,
197 				 struct attribute_group **groups)
198 {
199 	int i;
200 
201 	if (groups)
202 		for (i = 0; groups[i]; i++)
203 			sysfs_remove_group(&drv->p->kobj, groups[i]);
204 }
205 
206 /**
207  * driver_register - register driver with bus
208  * @drv: driver to register
209  *
210  * We pass off most of the work to the bus_add_driver() call,
211  * since most of the things we have to do deal with the bus
212  * structures.
213  */
214 int driver_register(struct device_driver *drv)
215 {
216 	int ret;
217 	struct device_driver *other;
218 
219 	if ((drv->bus->probe && drv->probe) ||
220 	    (drv->bus->remove && drv->remove) ||
221 	    (drv->bus->shutdown && drv->shutdown))
222 		printk(KERN_WARNING "Driver '%s' needs updating - please use "
223 			"bus_type methods\n", drv->name);
224 
225 	other = driver_find(drv->name, drv->bus);
226 	if (other) {
227 		put_driver(other);
228 		printk(KERN_ERR "Error: Driver '%s' is already registered, "
229 			"aborting...\n", drv->name);
230 		return -EEXIST;
231 	}
232 
233 	ret = bus_add_driver(drv);
234 	if (ret)
235 		return ret;
236 	ret = driver_add_groups(drv, drv->groups);
237 	if (ret)
238 		bus_remove_driver(drv);
239 	return ret;
240 }
241 EXPORT_SYMBOL_GPL(driver_register);
242 
243 /**
244  * driver_unregister - remove driver from system.
245  * @drv: driver.
246  *
247  * Again, we pass off most of the work to the bus-level call.
248  */
249 void driver_unregister(struct device_driver *drv)
250 {
251 	driver_remove_groups(drv, drv->groups);
252 	bus_remove_driver(drv);
253 }
254 EXPORT_SYMBOL_GPL(driver_unregister);
255 
256 /**
257  * driver_find - locate driver on a bus by its name.
258  * @name: name of the driver.
259  * @bus: bus to scan for the driver.
260  *
261  * Call kset_find_obj() to iterate over list of drivers on
262  * a bus to find driver by name. Return driver if found.
263  *
264  * Note that kset_find_obj increments driver's reference count.
265  */
266 struct device_driver *driver_find(const char *name, struct bus_type *bus)
267 {
268 	struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
269 	struct driver_private *priv;
270 
271 	if (k) {
272 		priv = to_driver(k);
273 		return priv->driver;
274 	}
275 	return NULL;
276 }
277 EXPORT_SYMBOL_GPL(driver_find);
278