blob: b76cc69f11067801e85f830573d098561eb97727 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * driver.c - centralized device driver management
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -08006 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/string.h>
17#include "base.h"
18
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080019static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org94e7b1c2005-03-21 12:25:36 -080020{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080021 struct klist_node *n = klist_next(i);
Greg Kroah-Hartman93e746d2008-12-16 12:25:49 -080022 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;
mochel@digitalimplant.org94e7b1c2005-03-21 12:25:36 -080030}
31
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080032/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080033 * driver_for_each_device - Iterator for devices bound to a driver.
34 * @drv: Driver we're iterating.
35 * @start: Device to begin with
36 * @data: Data to pass to the callback.
37 * @fn: Function to call for each device.
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080038 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080039 * Iterate over the @drv's list of devices calling @fn for each one.
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080040 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080041int driver_for_each_device(struct device_driver *drv, struct device *start,
42 void *data, int (*fn)(struct device *, void *))
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080043{
mochel@digitalimplant.org94e7b1c2005-03-21 12:25:36 -080044 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080045 struct device *dev;
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080046 int error = 0;
47
mochel@digitalimplant.org94e7b1c2005-03-21 12:25:36 -080048 if (!drv)
49 return -EINVAL;
50
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080051 klist_iter_init_node(&drv->p->klist_devices, &i,
Greg Kroah-Hartman93e746d2008-12-16 12:25:49 -080052 start ? &start->p->knode_driver : NULL);
mochel@digitalimplant.org94e7b1c2005-03-21 12:25:36 -080053 while ((dev = next_device(&i)) && !error)
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080054 error = fn(dev, data);
mochel@digitalimplant.org94e7b1c2005-03-21 12:25:36 -080055 klist_iter_exit(&i);
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080056 return error;
57}
gregkh@suse.de126eddf2005-03-22 12:17:13 -080058EXPORT_SYMBOL_GPL(driver_for_each_device);
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/**
Cornelia Huck0edb5862005-06-22 16:59:51 +020061 * driver_find_device - device iterator for locating a particular device.
Randy Dunlapc41455f2005-10-23 11:59:14 -070062 * @drv: The device's driver
Cornelia Huck0edb5862005-06-22 16:59:51 +020063 * @start: Device to begin with
64 * @data: Data to pass to match function
65 * @match: Callback function to check device
66 *
67 * This is similar to the driver_for_each_device() function above, but
68 * it returns a reference to a device that is 'found' for later use, as
69 * determined by the @match callback.
70 *
71 * The callback should return 0 if the device doesn't match and non-zero
72 * if it does. If the callback returns non-zero, this function will
73 * return to the caller and not iterate over any more devices.
74 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080075struct device *driver_find_device(struct device_driver *drv,
76 struct device *start, void *data,
77 int (*match)(struct device *dev, void *data))
Cornelia Huck0edb5862005-06-22 16:59:51 +020078{
79 struct klist_iter i;
80 struct device *dev;
81
82 if (!drv)
83 return NULL;
84
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080085 klist_iter_init_node(&drv->p->klist_devices, &i,
Greg Kroah-Hartman93e746d2008-12-16 12:25:49 -080086 (start ? &start->p->knode_driver : NULL));
Cornelia Huck0edb5862005-06-22 16:59:51 +020087 while ((dev = next_device(&i)))
88 if (match(dev, data) && get_device(dev))
89 break;
90 klist_iter_exit(&i);
91 return dev;
92}
93EXPORT_SYMBOL_GPL(driver_find_device);
94
95/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080096 * driver_create_file - create sysfs file for driver.
97 * @drv: driver.
98 * @attr: driver attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800100int driver_create_file(struct device_driver *drv,
101 struct driver_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 int error;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100104 if (drv)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800105 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
Cornelia Huck0c98b192008-01-31 10:39:38 +0100106 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 error = -EINVAL;
108 return error;
109}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800110EXPORT_SYMBOL_GPL(driver_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800113 * driver_remove_file - remove sysfs file for driver.
114 * @drv: driver.
115 * @attr: driver attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800117void driver_remove_file(struct device_driver *drv,
118 struct driver_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100120 if (drv)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800121 sysfs_remove_file(&drv->p->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800123EXPORT_SYMBOL_GPL(driver_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/**
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400126 * driver_add_kobj - add a kobject below the specified driver
Randy Dunlap71996772008-02-18 13:09:03 -0800127 * @drv: requesting device driver
128 * @kobj: kobject to add below this driver
129 * @fmt: format string that names the kobject
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400130 *
131 * You really don't want to do this, this is only here due to one looney
132 * iseries driver, go poke those developers if you are annoyed about
133 * this...
134 */
135int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
136 const char *fmt, ...)
137{
138 va_list args;
139 char *name;
Jesper Juhld4783762008-03-28 14:16:12 -0700140 int ret;
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400141
142 va_start(args, fmt);
143 name = kvasprintf(GFP_KERNEL, fmt, args);
144 va_end(args);
145
146 if (!name)
147 return -ENOMEM;
148
Jesper Juhld4783762008-03-28 14:16:12 -0700149 ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
150 kfree(name);
151 return ret;
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400152}
153EXPORT_SYMBOL_GPL(driver_add_kobj);
154
155/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800156 * get_driver - increment driver reference count.
157 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800159struct device_driver *get_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800161 if (drv) {
162 struct driver_private *priv;
163 struct kobject *kobj;
164
165 kobj = kobject_get(&drv->p->kobj);
166 priv = to_driver(kobj);
167 return priv->driver;
168 }
169 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800171EXPORT_SYMBOL_GPL(get_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800174 * put_driver - decrement driver's refcount.
175 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800177void put_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800179 kobject_put(&drv->p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800181EXPORT_SYMBOL_GPL(put_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Cornelia Huck57c74532007-12-05 12:50:23 +0100183static int driver_add_groups(struct device_driver *drv,
184 struct attribute_group **groups)
185{
186 int error = 0;
187 int i;
188
189 if (groups) {
190 for (i = 0; groups[i]; i++) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800191 error = sysfs_create_group(&drv->p->kobj, groups[i]);
Cornelia Huck57c74532007-12-05 12:50:23 +0100192 if (error) {
193 while (--i >= 0)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800194 sysfs_remove_group(&drv->p->kobj,
Cornelia Huck57c74532007-12-05 12:50:23 +0100195 groups[i]);
196 break;
197 }
198 }
199 }
200 return error;
201}
202
203static void driver_remove_groups(struct device_driver *drv,
204 struct attribute_group **groups)
205{
206 int i;
207
208 if (groups)
209 for (i = 0; groups[i]; i++)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800210 sysfs_remove_group(&drv->p->kobj, groups[i]);
Cornelia Huck57c74532007-12-05 12:50:23 +0100211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800214 * driver_register - register driver with bus
215 * @drv: driver to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800217 * We pass off most of the work to the bus_add_driver() call,
218 * since most of the things we have to do deal with the bus
219 * structures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800221int driver_register(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Cornelia Huck57c74532007-12-05 12:50:23 +0100223 int ret;
Stas Sergeev16dc42e2008-04-26 19:52:35 +0400224 struct device_driver *other;
Cornelia Huck57c74532007-12-05 12:50:23 +0100225
Russell King594c8282006-01-05 14:29:51 +0000226 if ((drv->bus->probe && drv->probe) ||
227 (drv->bus->remove && drv->remove) ||
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800228 (drv->bus->shutdown && drv->shutdown))
229 printk(KERN_WARNING "Driver '%s' needs updating - please use "
230 "bus_type methods\n", drv->name);
Stas Sergeev16dc42e2008-04-26 19:52:35 +0400231
232 other = driver_find(drv->name, drv->bus);
233 if (other) {
234 put_driver(other);
235 printk(KERN_ERR "Error: Driver '%s' is already registered, "
236 "aborting...\n", drv->name);
237 return -EEXIST;
238 }
239
Cornelia Huck57c74532007-12-05 12:50:23 +0100240 ret = bus_add_driver(drv);
241 if (ret)
242 return ret;
243 ret = driver_add_groups(drv, drv->groups);
244 if (ret)
245 bus_remove_driver(drv);
246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800248EXPORT_SYMBOL_GPL(driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800251 * driver_unregister - remove driver from system.
252 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800254 * Again, we pass off most of the work to the bus-level call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800256void driver_unregister(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Cornelia Huck57c74532007-12-05 12:50:23 +0100258 driver_remove_groups(drv, drv->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 bus_remove_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800261EXPORT_SYMBOL_GPL(driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800264 * driver_find - locate driver on a bus by its name.
265 * @name: name of the driver.
266 * @bus: bus to scan for the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800268 * Call kset_find_obj() to iterate over list of drivers on
269 * a bus to find driver by name. Return driver if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800271 * Note that kset_find_obj increments driver's reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273struct device_driver *driver_find(const char *name, struct bus_type *bus)
274{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700275 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800276 struct driver_private *priv;
277
278 if (k) {
279 priv = to_driver(k);
280 return priv->driver;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return NULL;
283}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284EXPORT_SYMBOL_GPL(driver_find);