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