blob: 2ef5acf4368b9e8deef4440d865a87cef8d0b0a6 [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
19#define to_dev(node) container_of(node, struct device, driver_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080021
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080022static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080023{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080024 struct klist_node *n = klist_next(i);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080025 return n ? container_of(n, struct device, knode_driver) : NULL;
26}
27
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080028/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080029 * driver_for_each_device - Iterator for devices bound to a driver.
30 * @drv: Driver we're iterating.
31 * @start: Device to begin with
32 * @data: Data to pass to the callback.
33 * @fn: Function to call for each device.
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080034 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080035 * Iterate over the @drv's list of devices calling @fn for each one.
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080036 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080037int driver_for_each_device(struct device_driver *drv, struct device *start,
38 void *data, int (*fn)(struct device *, void *))
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080039{
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080040 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080041 struct device *dev;
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080042 int error = 0;
43
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080044 if (!drv)
45 return -EINVAL;
46
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080047 klist_iter_init_node(&drv->p->klist_devices, &i,
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080048 start ? &start->knode_driver : NULL);
49 while ((dev = next_device(&i)) && !error)
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080050 error = fn(dev, data);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080051 klist_iter_exit(&i);
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080052 return error;
53}
gregkh@suse.de126eddf2005-03-22 12:17:13 -080054EXPORT_SYMBOL_GPL(driver_for_each_device);
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/**
Cornelia Huck0edb5862005-06-22 16:59:51 +020057 * driver_find_device - device iterator for locating a particular device.
Randy Dunlapc41455f2005-10-23 11:59:14 -070058 * @drv: The device's driver
Cornelia Huck0edb5862005-06-22 16:59:51 +020059 * @start: Device to begin with
60 * @data: Data to pass to match function
61 * @match: Callback function to check device
62 *
63 * This is similar to the driver_for_each_device() function above, but
64 * it returns a reference to a device that is 'found' for later use, as
65 * determined by the @match callback.
66 *
67 * The callback should return 0 if the device doesn't match and non-zero
68 * if it does. If the callback returns non-zero, this function will
69 * return to the caller and not iterate over any more devices.
70 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080071struct device *driver_find_device(struct device_driver *drv,
72 struct device *start, void *data,
73 int (*match)(struct device *dev, void *data))
Cornelia Huck0edb5862005-06-22 16:59:51 +020074{
75 struct klist_iter i;
76 struct device *dev;
77
78 if (!drv)
79 return NULL;
80
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080081 klist_iter_init_node(&drv->p->klist_devices, &i,
Cornelia Huck0edb5862005-06-22 16:59:51 +020082 (start ? &start->knode_driver : NULL));
83 while ((dev = next_device(&i)))
84 if (match(dev, data) && get_device(dev))
85 break;
86 klist_iter_exit(&i);
87 return dev;
88}
89EXPORT_SYMBOL_GPL(driver_find_device);
90
91/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080092 * driver_create_file - create sysfs file for driver.
93 * @drv: driver.
94 * @attr: driver attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080096int driver_create_file(struct device_driver *drv,
97 struct driver_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int error;
Cornelia Huck0c98b192008-01-31 10:39:38 +0100100 if (drv)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800101 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
Cornelia Huck0c98b192008-01-31 10:39:38 +0100102 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 error = -EINVAL;
104 return error;
105}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800106EXPORT_SYMBOL_GPL(driver_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800109 * driver_remove_file - remove sysfs file for driver.
110 * @drv: driver.
111 * @attr: driver attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800113void driver_remove_file(struct device_driver *drv,
114 struct driver_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Cornelia Huck0c98b192008-01-31 10:39:38 +0100116 if (drv)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800117 sysfs_remove_file(&drv->p->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800119EXPORT_SYMBOL_GPL(driver_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/**
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400122 * driver_add_kobj - add a kobject below the specified driver
Randy Dunlap71996772008-02-18 13:09:03 -0800123 * @drv: requesting device driver
124 * @kobj: kobject to add below this driver
125 * @fmt: format string that names the kobject
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400126 *
127 * You really don't want to do this, this is only here due to one looney
128 * iseries driver, go poke those developers if you are annoyed about
129 * this...
130 */
131int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
132 const char *fmt, ...)
133{
134 va_list args;
135 char *name;
Jesper Juhld4783762008-03-28 14:16:12 -0700136 int ret;
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400137
138 va_start(args, fmt);
139 name = kvasprintf(GFP_KERNEL, fmt, args);
140 va_end(args);
141
142 if (!name)
143 return -ENOMEM;
144
Jesper Juhld4783762008-03-28 14:16:12 -0700145 ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
146 kfree(name);
147 return ret;
Greg Kroah-Hartmancbe9c592007-12-19 15:54:39 -0400148}
149EXPORT_SYMBOL_GPL(driver_add_kobj);
150
151/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800152 * get_driver - increment driver reference count.
153 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800155struct device_driver *get_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800157 if (drv) {
158 struct driver_private *priv;
159 struct kobject *kobj;
160
161 kobj = kobject_get(&drv->p->kobj);
162 priv = to_driver(kobj);
163 return priv->driver;
164 }
165 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800167EXPORT_SYMBOL_GPL(get_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800170 * put_driver - decrement driver's refcount.
171 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800173void put_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800175 kobject_put(&drv->p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800177EXPORT_SYMBOL_GPL(put_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Cornelia Huck57c74532007-12-05 12:50:23 +0100179static int driver_add_groups(struct device_driver *drv,
180 struct attribute_group **groups)
181{
182 int error = 0;
183 int i;
184
185 if (groups) {
186 for (i = 0; groups[i]; i++) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800187 error = sysfs_create_group(&drv->p->kobj, groups[i]);
Cornelia Huck57c74532007-12-05 12:50:23 +0100188 if (error) {
189 while (--i >= 0)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800190 sysfs_remove_group(&drv->p->kobj,
Cornelia Huck57c74532007-12-05 12:50:23 +0100191 groups[i]);
192 break;
193 }
194 }
195 }
196 return error;
197}
198
199static void driver_remove_groups(struct device_driver *drv,
200 struct attribute_group **groups)
201{
202 int i;
203
204 if (groups)
205 for (i = 0; groups[i]; i++)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800206 sysfs_remove_group(&drv->p->kobj, groups[i]);
Cornelia Huck57c74532007-12-05 12:50:23 +0100207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800210 * driver_register - register driver with bus
211 * @drv: driver to register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800213 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800217int driver_register(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Cornelia Huck57c74532007-12-05 12:50:23 +0100219 int ret;
Stas Sergeev16dc42e2008-04-26 19:52:35 +0400220 struct device_driver *other;
Cornelia Huck57c74532007-12-05 12:50:23 +0100221
Russell King594c8282006-01-05 14:29:51 +0000222 if ((drv->bus->probe && drv->probe) ||
223 (drv->bus->remove && drv->remove) ||
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800224 (drv->bus->shutdown && drv->shutdown))
225 printk(KERN_WARNING "Driver '%s' needs updating - please use "
226 "bus_type methods\n", drv->name);
Stas Sergeev16dc42e2008-04-26 19:52:35 +0400227
228 other = driver_find(drv->name, drv->bus);
229 if (other) {
230 put_driver(other);
231 printk(KERN_ERR "Error: Driver '%s' is already registered, "
232 "aborting...\n", drv->name);
233 return -EEXIST;
234 }
235
Cornelia Huck57c74532007-12-05 12:50:23 +0100236 ret = bus_add_driver(drv);
237 if (ret)
238 return ret;
239 ret = driver_add_groups(drv, drv->groups);
240 if (ret)
241 bus_remove_driver(drv);
242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800244EXPORT_SYMBOL_GPL(driver_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800247 * driver_unregister - remove driver from system.
248 * @drv: driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800250 * Again, we pass off most of the work to the bus-level call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800252void driver_unregister(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Cornelia Huck57c74532007-12-05 12:50:23 +0100254 driver_remove_groups(drv, drv->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 bus_remove_driver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800257EXPORT_SYMBOL_GPL(driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800260 * driver_find - locate driver on a bus by its name.
261 * @name: name of the driver.
262 * @bus: bus to scan for the driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800264 * Call kset_find_obj() to iterate over list of drivers on
265 * a bus to find driver by name. Return driver if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800267 * Note that kset_find_obj increments driver's reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269struct device_driver *driver_find(const char *name, struct bus_type *bus)
270{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700271 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800272 struct driver_private *priv;
273
274 if (k) {
275 priv = to_driver(k);
276 return priv->driver;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return NULL;
279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280EXPORT_SYMBOL_GPL(driver_find);