[PATCH] driver core: Add the ability to unbind drivers to devices from userspace
[safe/jmp/linux-2.6] / drivers / base / bus.c
1 /*
2  * bus.c - bus driver management
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  *
7  * This file is released under the GPLv2
8  *
9  */
10
11 #include <linux/config.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include "base.h"
18 #include "power/power.h"
19
20 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
21 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
22
23 /*
24  * sysfs bindings for drivers
25  */
26
27 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
28 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
29
30
31 static ssize_t
32 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
33 {
34         struct driver_attribute * drv_attr = to_drv_attr(attr);
35         struct device_driver * drv = to_driver(kobj);
36         ssize_t ret = -EIO;
37
38         if (drv_attr->show)
39                 ret = drv_attr->show(drv, buf);
40         return ret;
41 }
42
43 static ssize_t
44 drv_attr_store(struct kobject * kobj, struct attribute * attr,
45                const char * buf, size_t count)
46 {
47         struct driver_attribute * drv_attr = to_drv_attr(attr);
48         struct device_driver * drv = to_driver(kobj);
49         ssize_t ret = -EIO;
50
51         if (drv_attr->store)
52                 ret = drv_attr->store(drv, buf, count);
53         return ret;
54 }
55
56 static struct sysfs_ops driver_sysfs_ops = {
57         .show   = drv_attr_show,
58         .store  = drv_attr_store,
59 };
60
61
62 static void driver_release(struct kobject * kobj)
63 {
64         struct device_driver * drv = to_driver(kobj);
65         complete(&drv->unloaded);
66 }
67
68 static struct kobj_type ktype_driver = {
69         .sysfs_ops      = &driver_sysfs_ops,
70         .release        = driver_release,
71 };
72
73
74 /*
75  * sysfs bindings for buses
76  */
77
78
79 static ssize_t
80 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
81 {
82         struct bus_attribute * bus_attr = to_bus_attr(attr);
83         struct bus_type * bus = to_bus(kobj);
84         ssize_t ret = 0;
85
86         if (bus_attr->show)
87                 ret = bus_attr->show(bus, buf);
88         return ret;
89 }
90
91 static ssize_t
92 bus_attr_store(struct kobject * kobj, struct attribute * attr,
93                const char * buf, size_t count)
94 {
95         struct bus_attribute * bus_attr = to_bus_attr(attr);
96         struct bus_type * bus = to_bus(kobj);
97         ssize_t ret = 0;
98
99         if (bus_attr->store)
100                 ret = bus_attr->store(bus, buf, count);
101         return ret;
102 }
103
104 static struct sysfs_ops bus_sysfs_ops = {
105         .show   = bus_attr_show,
106         .store  = bus_attr_store,
107 };
108
109 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
110 {
111         int error;
112         if (get_bus(bus)) {
113                 error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
114                 put_bus(bus);
115         } else
116                 error = -EINVAL;
117         return error;
118 }
119
120 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
121 {
122         if (get_bus(bus)) {
123                 sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
124                 put_bus(bus);
125         }
126 }
127
128 static struct kobj_type ktype_bus = {
129         .sysfs_ops      = &bus_sysfs_ops,
130
131 };
132
133 decl_subsys(bus, &ktype_bus, NULL);
134
135
136 /* Manually detach a device from it's associated driver. */
137 static int driver_helper(struct device *dev, void *data)
138 {
139         const char *name = data;
140
141         if (strcmp(name, dev->bus_id) == 0)
142                 return 1;
143         return 0;
144 }
145
146 static ssize_t driver_unbind(struct device_driver *drv,
147                              const char *buf, size_t count)
148 {
149         struct bus_type *bus = get_bus(drv->bus);
150         struct device *dev;
151         int err = -ENODEV;
152
153         dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
154         if ((dev) &&
155             (dev->driver == drv)) {
156                 device_release_driver(dev);
157                 err = count;
158         }
159         return err;
160 }
161 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
162
163
164 static struct device * next_device(struct klist_iter * i)
165 {
166         struct klist_node * n = klist_next(i);
167         return n ? container_of(n, struct device, knode_bus) : NULL;
168 }
169
170 /**
171  *      bus_for_each_dev - device iterator.
172  *      @bus:   bus type.
173  *      @start: device to start iterating from.
174  *      @data:  data for the callback.
175  *      @fn:    function to be called for each device.
176  *
177  *      Iterate over @bus's list of devices, and call @fn for each,
178  *      passing it @data. If @start is not NULL, we use that device to
179  *      begin iterating from.
180  *
181  *      We check the return of @fn each time. If it returns anything
182  *      other than 0, we break out and return that value.
183  *
184  *      NOTE: The device that returns a non-zero value is not retained
185  *      in any way, nor is its refcount incremented. If the caller needs
186  *      to retain this data, it should do, and increment the reference
187  *      count in the supplied callback.
188  */
189
190 int bus_for_each_dev(struct bus_type * bus, struct device * start,
191                      void * data, int (*fn)(struct device *, void *))
192 {
193         struct klist_iter i;
194         struct device * dev;
195         int error = 0;
196
197         if (!bus)
198                 return -EINVAL;
199
200         klist_iter_init_node(&bus->klist_devices, &i,
201                              (start ? &start->knode_bus : NULL));
202         while ((dev = next_device(&i)) && !error)
203                 error = fn(dev, data);
204         klist_iter_exit(&i);
205         return error;
206 }
207
208 /**
209  * bus_find_device - device iterator for locating a particular device.
210  * @bus: bus type
211  * @start: Device to begin with
212  * @data: Data to pass to match function
213  * @match: Callback function to check device
214  *
215  * This is similar to the bus_for_each_dev() function above, but it
216  * returns a reference to a device that is 'found' for later use, as
217  * determined by the @match callback.
218  *
219  * The callback should return 0 if the device doesn't match and non-zero
220  * if it does.  If the callback returns non-zero, this function will
221  * return to the caller and not iterate over any more devices.
222  */
223 struct device * bus_find_device(struct bus_type *bus,
224                                 struct device *start, void *data,
225                                 int (*match)(struct device *, void *))
226 {
227         struct klist_iter i;
228         struct device *dev;
229
230         if (!bus)
231                 return NULL;
232
233         klist_iter_init_node(&bus->klist_devices, &i,
234                              (start ? &start->knode_bus : NULL));
235         while ((dev = next_device(&i)))
236                 if (match(dev, data) && get_device(dev))
237                         break;
238         klist_iter_exit(&i);
239         return dev;
240 }
241
242
243 static struct device_driver * next_driver(struct klist_iter * i)
244 {
245         struct klist_node * n = klist_next(i);
246         return n ? container_of(n, struct device_driver, knode_bus) : NULL;
247 }
248
249 /**
250  *      bus_for_each_drv - driver iterator
251  *      @bus:   bus we're dealing with.
252  *      @start: driver to start iterating on.
253  *      @data:  data to pass to the callback.
254  *      @fn:    function to call for each driver.
255  *
256  *      This is nearly identical to the device iterator above.
257  *      We iterate over each driver that belongs to @bus, and call
258  *      @fn for each. If @fn returns anything but 0, we break out
259  *      and return it. If @start is not NULL, we use it as the head
260  *      of the list.
261  *
262  *      NOTE: we don't return the driver that returns a non-zero
263  *      value, nor do we leave the reference count incremented for that
264  *      driver. If the caller needs to know that info, it must set it
265  *      in the callback. It must also be sure to increment the refcount
266  *      so it doesn't disappear before returning to the caller.
267  */
268
269 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
270                      void * data, int (*fn)(struct device_driver *, void *))
271 {
272         struct klist_iter i;
273         struct device_driver * drv;
274         int error = 0;
275
276         if (!bus)
277                 return -EINVAL;
278
279         klist_iter_init_node(&bus->klist_drivers, &i,
280                              start ? &start->knode_bus : NULL);
281         while ((drv = next_driver(&i)) && !error)
282                 error = fn(drv, data);
283         klist_iter_exit(&i);
284         return error;
285 }
286
287 static int device_add_attrs(struct bus_type * bus, struct device * dev)
288 {
289         int error = 0;
290         int i;
291
292         if (bus->dev_attrs) {
293                 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
294                         error = device_create_file(dev,&bus->dev_attrs[i]);
295                         if (error)
296                                 goto Err;
297                 }
298         }
299  Done:
300         return error;
301  Err:
302         while (--i >= 0)
303                 device_remove_file(dev,&bus->dev_attrs[i]);
304         goto Done;
305 }
306
307
308 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
309 {
310         int i;
311
312         if (bus->dev_attrs) {
313                 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
314                         device_remove_file(dev,&bus->dev_attrs[i]);
315         }
316 }
317
318
319 /**
320  *      bus_add_device - add device to bus
321  *      @dev:   device being added
322  *
323  *      - Add the device to its bus's list of devices.
324  *      - Try to attach to driver.
325  *      - Create link to device's physical location.
326  */
327 int bus_add_device(struct device * dev)
328 {
329         struct bus_type * bus = get_bus(dev->bus);
330         int error = 0;
331
332         if (bus) {
333                 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
334                 device_attach(dev);
335                 klist_add_tail(&bus->klist_devices, &dev->knode_bus);
336                 error = device_add_attrs(bus, dev);
337                 if (!error) {
338                         sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
339                         sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
340                 }
341         }
342         return error;
343 }
344
345 /**
346  *      bus_remove_device - remove device from bus
347  *      @dev:   device to be removed
348  *
349  *      - Remove symlink from bus's directory.
350  *      - Delete device from bus's list.
351  *      - Detach from its driver.
352  *      - Drop reference taken in bus_add_device().
353  */
354 void bus_remove_device(struct device * dev)
355 {
356         if (dev->bus) {
357                 sysfs_remove_link(&dev->kobj, "bus");
358                 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
359                 device_remove_attrs(dev->bus, dev);
360                 klist_remove(&dev->knode_bus);
361                 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
362                 device_release_driver(dev);
363                 put_bus(dev->bus);
364         }
365 }
366
367 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
368 {
369         int error = 0;
370         int i;
371
372         if (bus->drv_attrs) {
373                 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
374                         error = driver_create_file(drv, &bus->drv_attrs[i]);
375                         if (error)
376                                 goto Err;
377                 }
378         }
379  Done:
380         return error;
381  Err:
382         while (--i >= 0)
383                 driver_remove_file(drv, &bus->drv_attrs[i]);
384         goto Done;
385 }
386
387
388 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
389 {
390         int i;
391
392         if (bus->drv_attrs) {
393                 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
394                         driver_remove_file(drv, &bus->drv_attrs[i]);
395         }
396 }
397
398
399 /**
400  *      bus_add_driver - Add a driver to the bus.
401  *      @drv:   driver.
402  *
403  */
404 int bus_add_driver(struct device_driver * drv)
405 {
406         struct bus_type * bus = get_bus(drv->bus);
407         int error = 0;
408
409         if (bus) {
410                 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
411                 error = kobject_set_name(&drv->kobj, "%s", drv->name);
412                 if (error) {
413                         put_bus(bus);
414                         return error;
415                 }
416                 drv->kobj.kset = &bus->drivers;
417                 if ((error = kobject_register(&drv->kobj))) {
418                         put_bus(bus);
419                         return error;
420                 }
421
422                 driver_attach(drv);
423                 klist_add_tail(&bus->klist_drivers, &drv->knode_bus);
424                 module_add_driver(drv->owner, drv);
425
426                 driver_add_attrs(bus, drv);
427                 driver_create_file(drv, &driver_attr_unbind);
428         }
429         return error;
430 }
431
432
433 /**
434  *      bus_remove_driver - delete driver from bus's knowledge.
435  *      @drv:   driver.
436  *
437  *      Detach the driver from the devices it controls, and remove
438  *      it from its bus's list of drivers. Finally, we drop the reference
439  *      to the bus we took in bus_add_driver().
440  */
441
442 void bus_remove_driver(struct device_driver * drv)
443 {
444         if (drv->bus) {
445                 driver_remove_file(drv, &driver_attr_unbind);
446                 driver_remove_attrs(drv->bus, drv);
447                 klist_remove(&drv->knode_bus);
448                 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
449                 driver_detach(drv);
450                 module_remove_driver(drv);
451                 kobject_unregister(&drv->kobj);
452                 put_bus(drv->bus);
453         }
454 }
455
456
457 /* Helper for bus_rescan_devices's iter */
458 static int bus_rescan_devices_helper(struct device *dev, void *data)
459 {
460         int *count = data;
461
462         if (!dev->driver && (device_attach(dev) > 0))
463                 (*count)++;
464
465         return 0;
466 }
467
468
469 /**
470  *      bus_rescan_devices - rescan devices on the bus for possible drivers
471  *      @bus:   the bus to scan.
472  *
473  *      This function will look for devices on the bus with no driver
474  *      attached and rescan it against existing drivers to see if it
475  *      matches any. Calls device_attach(). Returns the number of devices
476  *      that were sucessfully bound to a driver.
477  */
478 int bus_rescan_devices(struct bus_type * bus)
479 {
480         int count = 0;
481
482         bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
483
484         return count;
485 }
486
487
488 struct bus_type * get_bus(struct bus_type * bus)
489 {
490         return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
491 }
492
493 void put_bus(struct bus_type * bus)
494 {
495         subsys_put(&bus->subsys);
496 }
497
498
499 /**
500  *      find_bus - locate bus by name.
501  *      @name:  name of bus.
502  *
503  *      Call kset_find_obj() to iterate over list of buses to
504  *      find a bus by name. Return bus if found.
505  *
506  *      Note that kset_find_obj increments bus' reference count.
507  */
508
509 struct bus_type * find_bus(char * name)
510 {
511         struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
512         return k ? to_bus(k) : NULL;
513 }
514
515
516 /**
517  *      bus_add_attrs - Add default attributes for this bus.
518  *      @bus:   Bus that has just been registered.
519  */
520
521 static int bus_add_attrs(struct bus_type * bus)
522 {
523         int error = 0;
524         int i;
525
526         if (bus->bus_attrs) {
527                 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
528                         if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
529                                 goto Err;
530                 }
531         }
532  Done:
533         return error;
534  Err:
535         while (--i >= 0)
536                 bus_remove_file(bus,&bus->bus_attrs[i]);
537         goto Done;
538 }
539
540 static void bus_remove_attrs(struct bus_type * bus)
541 {
542         int i;
543
544         if (bus->bus_attrs) {
545                 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
546                         bus_remove_file(bus,&bus->bus_attrs[i]);
547         }
548 }
549
550 /**
551  *      bus_register - register a bus with the system.
552  *      @bus:   bus.
553  *
554  *      Once we have that, we registered the bus with the kobject
555  *      infrastructure, then register the children subsystems it has:
556  *      the devices and drivers that belong to the bus.
557  */
558 int bus_register(struct bus_type * bus)
559 {
560         int retval;
561
562         retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
563         if (retval)
564                 goto out;
565
566         subsys_set_kset(bus, bus_subsys);
567         retval = subsystem_register(&bus->subsys);
568         if (retval)
569                 goto out;
570
571         kobject_set_name(&bus->devices.kobj, "devices");
572         bus->devices.subsys = &bus->subsys;
573         retval = kset_register(&bus->devices);
574         if (retval)
575                 goto bus_devices_fail;
576
577         kobject_set_name(&bus->drivers.kobj, "drivers");
578         bus->drivers.subsys = &bus->subsys;
579         bus->drivers.ktype = &ktype_driver;
580         retval = kset_register(&bus->drivers);
581         if (retval)
582                 goto bus_drivers_fail;
583
584         klist_init(&bus->klist_devices);
585         klist_init(&bus->klist_drivers);
586         bus_add_attrs(bus);
587
588         pr_debug("bus type '%s' registered\n", bus->name);
589         return 0;
590
591 bus_drivers_fail:
592         kset_unregister(&bus->devices);
593 bus_devices_fail:
594         subsystem_unregister(&bus->subsys);
595 out:
596         return retval;
597 }
598
599
600 /**
601  *      bus_unregister - remove a bus from the system
602  *      @bus:   bus.
603  *
604  *      Unregister the child subsystems and the bus itself.
605  *      Finally, we call put_bus() to release the refcount
606  */
607 void bus_unregister(struct bus_type * bus)
608 {
609         pr_debug("bus %s: unregistering\n", bus->name);
610         bus_remove_attrs(bus);
611         kset_unregister(&bus->drivers);
612         kset_unregister(&bus->devices);
613         subsystem_unregister(&bus->subsys);
614 }
615
616 int __init buses_init(void)
617 {
618         return subsystem_register(&bus_subsys);
619 }
620
621
622 EXPORT_SYMBOL_GPL(bus_for_each_dev);
623 EXPORT_SYMBOL_GPL(bus_find_device);
624 EXPORT_SYMBOL_GPL(bus_for_each_drv);
625
626 EXPORT_SYMBOL_GPL(bus_add_device);
627 EXPORT_SYMBOL_GPL(bus_remove_device);
628 EXPORT_SYMBOL_GPL(bus_register);
629 EXPORT_SYMBOL_GPL(bus_unregister);
630 EXPORT_SYMBOL_GPL(bus_rescan_devices);
631 EXPORT_SYMBOL_GPL(get_bus);
632 EXPORT_SYMBOL_GPL(put_bus);
633 EXPORT_SYMBOL_GPL(find_bus);
634
635 EXPORT_SYMBOL_GPL(bus_create_file);
636 EXPORT_SYMBOL_GPL(bus_remove_file);