aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/base')
-rw-r--r--drivers/base/Makefile2
-rw-r--r--drivers/base/base.h2
-rw-r--r--drivers/base/bus.c182
-rw-r--r--drivers/base/dd.c200
4 files changed, 203 insertions, 183 deletions
diff --git a/drivers/base/Makefile b/drivers/base/Makefile
index 771f6c80e824..66d9c4643fc1 100644
--- a/drivers/base/Makefile
+++ b/drivers/base/Makefile
@@ -1,6 +1,6 @@
1# Makefile for the Linux device tree 1# Makefile for the Linux device tree
2 2
3obj-y := core.o sys.o bus.o \ 3obj-y := core.o sys.o bus.o dd.o \
4 driver.o class.o platform.o \ 4 driver.o class.o platform.o \
5 cpu.o firmware.o init.o map.o dmapool.o \ 5 cpu.o firmware.o init.o map.o dmapool.o \
6 attribute_container.o transport_class.o 6 attribute_container.o transport_class.o
diff --git a/drivers/base/base.h b/drivers/base/base.h
index 8d1e8bd48632..645f62692920 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -4,6 +4,8 @@ extern void bus_remove_device(struct device * dev);
4extern int bus_add_driver(struct device_driver *); 4extern int bus_add_driver(struct device_driver *);
5extern void bus_remove_driver(struct device_driver *); 5extern void bus_remove_driver(struct device_driver *);
6 6
7extern void driver_detach(struct device_driver * drv);
8
7static inline struct class_device *to_class_dev(struct kobject *obj) 9static inline struct class_device *to_class_dev(struct kobject *obj)
8{ 10{
9 return container_of(obj, struct class_device, kobj); 11 return container_of(obj, struct class_device, kobj);
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index aa27f76d28cd..1b05c1399e37 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -18,7 +18,6 @@
18#include "power/power.h" 18#include "power/power.h"
19 19
20#define to_dev(node) container_of(node, struct device, bus_list) 20#define to_dev(node) container_of(node, struct device, bus_list)
21#define to_drv(node) container_of(node, struct device_driver, kobj.entry)
22 21
23#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) 22#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
24#define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj) 23#define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
@@ -243,181 +242,6 @@ int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
243 return ret; 242 return ret;
244} 243}
245 244
246/**
247 * device_bind_driver - bind a driver to one device.
248 * @dev: device.
249 *
250 * Allow manual attachment of a driver to a device.
251 * Caller must have already set @dev->driver.
252 *
253 * Note that this does not modify the bus reference count
254 * nor take the bus's rwsem. Please verify those are accounted
255 * for before calling this. (It is ok to call with no other effort
256 * from a driver's probe() method.)
257 */
258
259void device_bind_driver(struct device * dev)
260{
261 pr_debug("bound device '%s' to driver '%s'\n",
262 dev->bus_id, dev->driver->name);
263 list_add_tail(&dev->driver_list, &dev->driver->devices);
264 sysfs_create_link(&dev->driver->kobj, &dev->kobj,
265 kobject_name(&dev->kobj));
266 sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
267}
268
269
270/**
271 * driver_probe_device - attempt to bind device & driver.
272 * @drv: driver.
273 * @dev: device.
274 *
275 * First, we call the bus's match function, if one present, which
276 * should compare the device IDs the driver supports with the
277 * device IDs of the device. Note we don't do this ourselves
278 * because we don't know the format of the ID structures, nor what
279 * is to be considered a match and what is not.
280 *
281 * If we find a match, we call @drv->probe(@dev) if it exists, and
282 * call device_bind_driver() above.
283 */
284int driver_probe_device(struct device_driver * drv, struct device * dev)
285{
286 int error = 0;
287
288 if (drv->bus->match && !drv->bus->match(dev, drv))
289 return -ENODEV;
290
291 down(&dev->sem);
292 dev->driver = drv;
293 if (drv->probe) {
294 error = drv->probe(dev);
295 if (error) {
296 dev->driver = NULL;
297 up(&dev->sem);
298 return error;
299 }
300 }
301 up(&dev->sem);
302 device_bind_driver(dev);
303 return 0;
304}
305
306
307/**
308 * device_attach - try to attach device to a driver.
309 * @dev: device.
310 *
311 * Walk the list of drivers that the bus has and call
312 * driver_probe_device() for each pair. If a compatible
313 * pair is found, break out and return.
314 */
315int device_attach(struct device * dev)
316{
317 struct bus_type * bus = dev->bus;
318 struct list_head * entry;
319 int error;
320
321 if (dev->driver) {
322 device_bind_driver(dev);
323 return 1;
324 }
325
326 if (bus->match) {
327 list_for_each(entry, &bus->drivers.list) {
328 struct device_driver * drv = to_drv(entry);
329 error = driver_probe_device(drv, dev);
330 if (!error)
331 /* success, driver matched */
332 return 1;
333 if (error != -ENODEV && error != -ENXIO)
334 /* driver matched but the probe failed */
335 printk(KERN_WARNING
336 "%s: probe of %s failed with error %d\n",
337 drv->name, dev->bus_id, error);
338 }
339 }
340
341 return 0;
342}
343
344
345/**
346 * driver_attach - try to bind driver to devices.
347 * @drv: driver.
348 *
349 * Walk the list of devices that the bus has on it and try to
350 * match the driver with each one. If driver_probe_device()
351 * returns 0 and the @dev->driver is set, we've found a
352 * compatible pair.
353 *
354 * Note that we ignore the -ENODEV error from driver_probe_device(),
355 * since it's perfectly valid for a driver not to bind to any devices.
356 */
357void driver_attach(struct device_driver * drv)
358{
359 struct bus_type * bus = drv->bus;
360 struct list_head * entry;
361 int error;
362
363 if (!bus->match)
364 return;
365
366 list_for_each(entry, &bus->devices.list) {
367 struct device * dev = container_of(entry, struct device, bus_list);
368 if (!dev->driver) {
369 error = driver_probe_device(drv, dev);
370 if (error && (error != -ENODEV))
371 /* driver matched but the probe failed */
372 printk(KERN_WARNING
373 "%s: probe of %s failed with error %d\n",
374 drv->name, dev->bus_id, error);
375 }
376 }
377}
378
379
380/**
381 * device_release_driver - manually detach device from driver.
382 * @dev: device.
383 *
384 * Manually detach device from driver.
385 * Note that this is called without incrementing the bus
386 * reference count nor taking the bus's rwsem. Be sure that
387 * those are accounted for before calling this function.
388 */
389
390void device_release_driver(struct device * dev)
391{
392 struct device_driver * drv;
393
394 down(&dev->sem);
395 drv = dev->driver;
396 if (drv) {
397 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
398 sysfs_remove_link(&dev->kobj, "driver");
399 list_del_init(&dev->driver_list);
400 if (drv->remove)
401 drv->remove(dev);
402 dev->driver = NULL;
403 }
404 up(&dev->sem);
405}
406
407
408/**
409 * driver_detach - detach driver from all devices it controls.
410 * @drv: driver.
411 */
412
413static void driver_detach(struct device_driver * drv)
414{
415 while (!list_empty(&drv->devices)) {
416 struct device * dev = container_of(drv->devices.next, struct device, driver_list);
417 device_release_driver(dev);
418 }
419}
420
421static int device_add_attrs(struct bus_type * bus, struct device * dev) 245static int device_add_attrs(struct bus_type * bus, struct device * dev)
422{ 246{
423 int error = 0; 247 int error = 0;
@@ -757,12 +581,6 @@ int __init buses_init(void)
757EXPORT_SYMBOL_GPL(bus_for_each_dev); 581EXPORT_SYMBOL_GPL(bus_for_each_dev);
758EXPORT_SYMBOL_GPL(bus_for_each_drv); 582EXPORT_SYMBOL_GPL(bus_for_each_drv);
759 583
760EXPORT_SYMBOL_GPL(driver_probe_device);
761EXPORT_SYMBOL_GPL(device_bind_driver);
762EXPORT_SYMBOL_GPL(device_release_driver);
763EXPORT_SYMBOL_GPL(device_attach);
764EXPORT_SYMBOL_GPL(driver_attach);
765
766EXPORT_SYMBOL_GPL(bus_add_device); 584EXPORT_SYMBOL_GPL(bus_add_device);
767EXPORT_SYMBOL_GPL(bus_remove_device); 585EXPORT_SYMBOL_GPL(bus_remove_device);
768EXPORT_SYMBOL_GPL(bus_register); 586EXPORT_SYMBOL_GPL(bus_register);
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
new file mode 100644
index 000000000000..b709b1e0cb2a
--- /dev/null
+++ b/drivers/base/dd.c
@@ -0,0 +1,200 @@
1/*
2 * drivers/base/dd.c - The core device/driver interactions.
3 *
4 * This file contains the (sometimes tricky) code that controls the
5 * interactions between devices and drivers, which primarily includes
6 * driver binding and unbinding.
7 *
8 * All of this code used to exist in drivers/base/bus.c, but was
9 * relocated to here in the name of compartmentalization (since it wasn't
10 * strictly code just for the 'struct bus_type'.
11 *
12 * Copyright (c) 2002-5 Patrick Mochel
13 * Copyright (c) 2002-3 Open Source Development Labs
14 *
15 * This file is released under the GPLv2
16 */
17
18#include <linux/device.h>
19#include <linux/module.h>
20
21#include "base.h"
22#include "power/power.h"
23
24#define to_drv(node) container_of(node, struct device_driver, kobj.entry)
25
26
27/**
28 * device_bind_driver - bind a driver to one device.
29 * @dev: device.
30 *
31 * Allow manual attachment of a driver to a device.
32 * Caller must have already set @dev->driver.
33 *
34 * Note that this does not modify the bus reference count
35 * nor take the bus's rwsem. Please verify those are accounted
36 * for before calling this. (It is ok to call with no other effort
37 * from a driver's probe() method.)
38 */
39void device_bind_driver(struct device * dev)
40{
41 pr_debug("bound device '%s' to driver '%s'\n",
42 dev->bus_id, dev->driver->name);
43 list_add_tail(&dev->driver_list, &dev->driver->devices);
44 sysfs_create_link(&dev->driver->kobj, &dev->kobj,
45 kobject_name(&dev->kobj));
46 sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
47}
48
49/**
50 * driver_probe_device - attempt to bind device & driver.
51 * @drv: driver.
52 * @dev: device.
53 *
54 * First, we call the bus's match function, if one present, which
55 * should compare the device IDs the driver supports with the
56 * device IDs of the device. Note we don't do this ourselves
57 * because we don't know the format of the ID structures, nor what
58 * is to be considered a match and what is not.
59 *
60 * If we find a match, we call @drv->probe(@dev) if it exists, and
61 * call device_bind_driver() above.
62 */
63int driver_probe_device(struct device_driver * drv, struct device * dev)
64{
65 int error = 0;
66
67 if (drv->bus->match && !drv->bus->match(dev, drv))
68 return -ENODEV;
69
70 down(&dev->sem);
71 dev->driver = drv;
72 if (drv->probe) {
73 error = drv->probe(dev);
74 if (error) {
75 dev->driver = NULL;
76 up(&dev->sem);
77 return error;
78 }
79 }
80 up(&dev->sem);
81 device_bind_driver(dev);
82 return 0;
83}
84
85/**
86 * device_attach - try to attach device to a driver.
87 * @dev: device.
88 *
89 * Walk the list of drivers that the bus has and call
90 * driver_probe_device() for each pair. If a compatible
91 * pair is found, break out and return.
92 */
93int device_attach(struct device * dev)
94{
95 struct bus_type * bus = dev->bus;
96 struct list_head * entry;
97 int error;
98
99 if (dev->driver) {
100 device_bind_driver(dev);
101 return 1;
102 }
103
104 if (bus->match) {
105 list_for_each(entry, &bus->drivers.list) {
106 struct device_driver * drv = to_drv(entry);
107 error = driver_probe_device(drv, dev);
108 if (!error)
109 /* success, driver matched */
110 return 1;
111 if (error != -ENODEV && error != -ENXIO)
112 /* driver matched but the probe failed */
113 printk(KERN_WARNING
114 "%s: probe of %s failed with error %d\n",
115 drv->name, dev->bus_id, error);
116 }
117 }
118
119 return 0;
120}
121
122/**
123 * driver_attach - try to bind driver to devices.
124 * @drv: driver.
125 *
126 * Walk the list of devices that the bus has on it and try to
127 * match the driver with each one. If driver_probe_device()
128 * returns 0 and the @dev->driver is set, we've found a
129 * compatible pair.
130 *
131 * Note that we ignore the -ENODEV error from driver_probe_device(),
132 * since it's perfectly valid for a driver not to bind to any devices.
133 */
134void driver_attach(struct device_driver * drv)
135{
136 struct bus_type * bus = drv->bus;
137 struct list_head * entry;
138 int error;
139
140 if (!bus->match)
141 return;
142
143 list_for_each(entry, &bus->devices.list) {
144 struct device * dev = container_of(entry, struct device, bus_list);
145 if (!dev->driver) {
146 error = driver_probe_device(drv, dev);
147 if (error && (error != -ENODEV))
148 /* driver matched but the probe failed */
149 printk(KERN_WARNING
150 "%s: probe of %s failed with error %d\n",
151 drv->name, dev->bus_id, error);
152 }
153 }
154}
155
156/**
157 * device_release_driver - manually detach device from driver.
158 * @dev: device.
159 *
160 * Manually detach device from driver.
161 * Note that this is called without incrementing the bus
162 * reference count nor taking the bus's rwsem. Be sure that
163 * those are accounted for before calling this function.
164 */
165void device_release_driver(struct device * dev)
166{
167 struct device_driver * drv;
168
169 down(&dev->sem);
170 drv = dev->driver;
171 if (drv) {
172 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
173 sysfs_remove_link(&dev->kobj, "driver");
174 list_del_init(&dev->driver_list);
175 if (drv->remove)
176 drv->remove(dev);
177 dev->driver = NULL;
178 }
179 up(&dev->sem);
180}
181
182/**
183 * driver_detach - detach driver from all devices it controls.
184 * @drv: driver.
185 */
186void driver_detach(struct device_driver * drv)
187{
188 while (!list_empty(&drv->devices)) {
189 struct device * dev = container_of(drv->devices.next, struct device, driver_list);
190 device_release_driver(dev);
191 }
192}
193
194
195EXPORT_SYMBOL_GPL(driver_probe_device);
196EXPORT_SYMBOL_GPL(device_bind_driver);
197EXPORT_SYMBOL_GPL(device_release_driver);
198EXPORT_SYMBOL_GPL(device_attach);
199EXPORT_SYMBOL_GPL(driver_attach);
200