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