2 * driver.c - centralized device driver management
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
11 #include <linux/config.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
18 #define to_dev(node) container_of(node,struct device,driver_list)
19 #define to_drv(obj) container_of(obj,struct device_driver,kobj)
22 * driver_create_file - create sysfs file for driver.
24 * @attr: driver attribute descriptor.
27 int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
30 if (get_driver(drv)) {
31 error = sysfs_create_file(&drv->kobj,&attr->attr);
40 * driver_remove_file - remove sysfs file for driver.
42 * @attr: driver attribute descriptor.
45 void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
47 if (get_driver(drv)) {
48 sysfs_remove_file(&drv->kobj,&attr->attr);
55 * get_driver - increment driver reference count.
58 struct device_driver * get_driver(struct device_driver * drv)
60 return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
65 * put_driver - decrement driver's refcount.
68 void put_driver(struct device_driver * drv)
70 kobject_put(&drv->kobj);
75 * driver_register - register driver with bus
76 * @drv: driver to register
78 * We pass off most of the work to the bus_add_driver() call,
79 * since most of the things we have to do deal with the bus
82 * The one interesting aspect is that we initialize @drv->unload_sem
83 * to a locked state here. It will be unlocked when the driver
84 * reference count reaches 0.
86 int driver_register(struct device_driver * drv)
88 INIT_LIST_HEAD(&drv->devices);
89 init_MUTEX_LOCKED(&drv->unload_sem);
90 return bus_add_driver(drv);
95 * driver_unregister - remove driver from system.
98 * Again, we pass off most of the work to the bus-level call.
100 * Though, once that is done, we attempt to take @drv->unload_sem.
101 * This will block until the driver refcount reaches 0, and it is
102 * released. Only modular drivers will call this function, and we
103 * have to guarantee that it won't complete, letting the driver
104 * unload until all references are gone.
107 void driver_unregister(struct device_driver * drv)
109 bus_remove_driver(drv);
110 down(&drv->unload_sem);
111 up(&drv->unload_sem);
114 EXPORT_SYMBOL(driver_register);
115 EXPORT_SYMBOL(driver_unregister);
116 EXPORT_SYMBOL(get_driver);
117 EXPORT_SYMBOL(put_driver);
119 EXPORT_SYMBOL(driver_create_file);
120 EXPORT_SYMBOL(driver_remove_file);