blob: 1e2bda780e48268eb38b834f7078644689b5dc7b [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.org94e7b1c52005-03-21 12:25:36 -080020{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080021 struct klist_node *n = klist_next(i);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080022 return n ? container_of(n, struct device, knode_driver) : NULL;
23}
24
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080025/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080026 * 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.
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080031 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080032 * Iterate over the @drv's list of devices calling @fn for each one.
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080033 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080034int driver_for_each_device(struct device_driver *drv, struct device *start,
35 void *data, int (*fn)(struct device *, void *))
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080036{
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080037 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080038 struct device *dev;
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080039 int error = 0;
40
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080041 if (!drv)
42 return -EINVAL;
43
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080044 klist_iter_init_node(&drv->p->klist_devices, &i,
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080045 start ? &start->knode_driver : NULL);
46 while ((dev = next_device(&i)) && !error)
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080047 error = fn(dev, data);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080048 klist_iter_exit(&i);
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080049 return error;
50}
gregkh@suse.de126eddf2005-03-22 12:17:13 -080051EXPORT_SYMBOL_GPL(driver_for_each_device);
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/**
Cornelia Huck0edb5862005-06-22 16:59:51 +020054 * driver_find_device - device iterator for locating a particular device.
Randy Dunlapc41455f2005-10-23 11:59:14 -070055 * @drv: The device's driver
Cornelia Huck0edb5862005-06-22 16:59:51 +020056 * @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 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080068struct device *driver_find_device(struct device_driver *drv,
69 struct device *start, void *data,
70 int (*match)(struct device *dev, void *data))
Cornelia Huck0edb5862005-06-22 16:59:51 +020071{
72 struct klist_iter i;
73 struct device *dev;
74
75 if (!drv)
76 return NULL;
77
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080078 klist_iter_init_node(&drv->p->klist_devices, &i,
Cornelia Huck0edb5862005-06-22 16:59:51 +020079 (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}
86EXPORT_SYMBOL_GPL(driver_find_device);
87
88/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080089 * driver_create_file - create sysfs file for driver.
90 * @drv: driver.
91 * @attr: driver attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080093int driver_create_file(struct device_driver *drv,
94 struct driver_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int error;
Cornelia Huck0c98b192008-01-31 10:39:38 +010097 if (drv)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080098 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
Cornelia Huck0c98b192008-01-31 10:39:38 +010099 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 error = -EINVAL;
101 return error;
102}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800103EXPORT_SYMBOL_GPL(driver_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106 * driver_remove_file - remove sysfs file for driver.
107 * @drv: driver.
108 * @attr: driver attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800110void driver_remove_file(struct device_driver *drv,
111 struct driver_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100113 if (drv)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800114 sysfs_remove_file(&drv->p->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800116EXPORT_SYMBOL_GPL(driver_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/**
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400119 * driver_add_kobj - add a kobject below the specified driver
Randy Dunlap71996772008-02-18 13:09:03 -0800120 * @drv: requesting device driver
121 * @kobj: kobject to add below this driver
122 * @fmt: format string that names the kobject
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400123 *
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 */
128int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
129 const char *fmt, ...)
130{
131 va_list args;
132 char *name;
Jesper Juhld4783762008-03-28 14:16:12 -0700133 int ret;
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400134
135 va_start(args, fmt);
136 name = kvasprintf(GFP_KERNEL, fmt, args);
137 va_end(args);
138
139 if (!name)
140 return -ENOMEM;
141
Jesper Juhld4783762008-03-28 14:16:12 -0700142 ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
143 kfree(name);
144 return ret;
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400145}
146EXPORT_SYMBOL_GPL(driver_add_kobj);
147
148/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800149 * get_driver - increment driver reference count.
150 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800152struct device_driver *get_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800154 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800164EXPORT_SYMBOL_GPL(get_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800167 * put_driver - decrement driver's refcount.
168 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800170void put_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800172 kobject_put(&drv->p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800174EXPORT_SYMBOL_GPL(put_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Cornelia Huck57c74532007-12-05 12:50:23 +0100176static 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++) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800184 error = sysfs_create_group(&drv->p->kobj, groups[i]);
Cornelia Huck57c74532007-12-05 12:50:23 +0100185 if (error) {
186 while (--i >= 0)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800187 sysfs_remove_group(&drv->p->kobj,
Cornelia Huck57c74532007-12-05 12:50:23 +0100188 groups[i]);
189 break;
190 }
191 }
192 }
193 return error;
194}
195
196static 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++)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800203 sysfs_remove_group(&drv->p->kobj, groups[i]);
Cornelia Huck57c74532007-12-05 12:50:23 +0100204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800207 * driver_register - register driver with bus
208 * @drv: driver to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800210 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800214int driver_register(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Cornelia Huck57c74532007-12-05 12:50:23 +0100216 int ret;
Stas Sergeev16dc42e2008-04-26 19:52:35 +0400217 struct device_driver *other;
Cornelia Huck57c74532007-12-05 12:50:23 +0100218
Russell King594c8282006-01-05 14:29:51 +0000219 if ((drv->bus->probe && drv->probe) ||
220 (drv->bus->remove && drv->remove) ||
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800221 (drv->bus->shutdown && drv->shutdown))
222 printk(KERN_WARNING "Driver '%s' needs updating - please use "
223 "bus_type methods\n", drv->name);
Stas Sergeev16dc42e2008-04-26 19:52:35 +0400224
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
Cornelia Huck57c74532007-12-05 12:50:23 +0100233 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800241EXPORT_SYMBOL_GPL(driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800244 * driver_unregister - remove driver from system.
245 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800247 * Again, we pass off most of the work to the bus-level call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800249void driver_unregister(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Cornelia Huck57c74532007-12-05 12:50:23 +0100251 driver_remove_groups(drv, drv->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 bus_remove_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800254EXPORT_SYMBOL_GPL(driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800257 * driver_find - locate driver on a bus by its name.
258 * @name: name of the driver.
259 * @bus: bus to scan for the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800261 * Call kset_find_obj() to iterate over list of drivers on
262 * a bus to find driver by name. Return driver if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800264 * Note that kset_find_obj increments driver's reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266struct device_driver *driver_find(const char *name, struct bus_type *bus)
267{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700268 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800269 struct driver_private *priv;
270
271 if (k) {
272 priv = to_driver(k);
273 return priv->driver;
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return NULL;
276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277EXPORT_SYMBOL_GPL(driver_find);