driver core: remove fields from struct bus_type
[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/device.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16 #include "base.h"
17 #include "power/power.h"
18
19 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
20 #define to_bus(obj) container_of(obj, struct bus_type_private, subsys.kobj)
21
22 /*
23  * sysfs bindings for drivers
24  */
25
26 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
27 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
28
29
30 static int __must_check bus_rescan_devices_helper(struct device *dev,
31                                                 void *data);
32
33 static struct bus_type *bus_get(struct bus_type *bus)
34 {
35         if (bus) {
36                 kset_get(&bus->p->subsys);
37                 return bus;
38         }
39         return NULL;
40 }
41
42 static void bus_put(struct bus_type *bus)
43 {
44         if (bus)
45                 kset_put(&bus->p->subsys);
46 }
47
48 static ssize_t
49 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
50 {
51         struct driver_attribute * drv_attr = to_drv_attr(attr);
52         struct device_driver * drv = to_driver(kobj);
53         ssize_t ret = -EIO;
54
55         if (drv_attr->show)
56                 ret = drv_attr->show(drv, buf);
57         return ret;
58 }
59
60 static ssize_t
61 drv_attr_store(struct kobject * kobj, struct attribute * attr,
62                const char * buf, size_t count)
63 {
64         struct driver_attribute * drv_attr = to_drv_attr(attr);
65         struct device_driver * drv = to_driver(kobj);
66         ssize_t ret = -EIO;
67
68         if (drv_attr->store)
69                 ret = drv_attr->store(drv, buf, count);
70         return ret;
71 }
72
73 static struct sysfs_ops driver_sysfs_ops = {
74         .show   = drv_attr_show,
75         .store  = drv_attr_store,
76 };
77
78
79 static void driver_release(struct kobject * kobj)
80 {
81         /*
82          * Yes this is an empty release function, it is this way because struct
83          * device is always a static object, not a dynamic one.  Yes, this is
84          * not nice and bad, but remember, drivers are code, reference counted
85          * by the module count, not a device, which is really data.  And yes,
86          * in the future I do want to have all drivers be created dynamically,
87          * and am working toward that goal, but it will take a bit longer...
88          *
89          * But do not let this example give _anyone_ the idea that they can
90          * create a release function without any code in it at all, to do that
91          * is almost always wrong.  If you have any questions about this,
92          * please send an email to <greg@kroah.com>
93          */
94 }
95
96 static struct kobj_type driver_ktype = {
97         .sysfs_ops      = &driver_sysfs_ops,
98         .release        = driver_release,
99 };
100
101
102 /*
103  * sysfs bindings for buses
104  */
105
106
107 static ssize_t
108 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
109 {
110         struct bus_attribute * bus_attr = to_bus_attr(attr);
111         struct bus_type_private *bus_priv = to_bus(kobj);
112         ssize_t ret = 0;
113
114         if (bus_attr->show)
115                 ret = bus_attr->show(bus_priv->bus, buf);
116         return ret;
117 }
118
119 static ssize_t
120 bus_attr_store(struct kobject * kobj, struct attribute * attr,
121                const char * buf, size_t count)
122 {
123         struct bus_attribute * bus_attr = to_bus_attr(attr);
124         struct bus_type_private *bus_priv = to_bus(kobj);
125         ssize_t ret = 0;
126
127         if (bus_attr->store)
128                 ret = bus_attr->store(bus_priv->bus, buf, count);
129         return ret;
130 }
131
132 static struct sysfs_ops bus_sysfs_ops = {
133         .show   = bus_attr_show,
134         .store  = bus_attr_store,
135 };
136
137 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
138 {
139         int error;
140         if (bus_get(bus)) {
141                 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
142                 bus_put(bus);
143         } else
144                 error = -EINVAL;
145         return error;
146 }
147
148 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
149 {
150         if (bus_get(bus)) {
151                 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
152                 bus_put(bus);
153         }
154 }
155
156 static struct kobj_type bus_ktype = {
157         .sysfs_ops      = &bus_sysfs_ops,
158 };
159
160 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
161 {
162         struct kobj_type *ktype = get_ktype(kobj);
163
164         if (ktype == &bus_ktype)
165                 return 1;
166         return 0;
167 }
168
169 static struct kset_uevent_ops bus_uevent_ops = {
170         .filter = bus_uevent_filter,
171 };
172
173 static struct kset *bus_kset;
174
175
176 #ifdef CONFIG_HOTPLUG
177 /* Manually detach a device from its associated driver. */
178 static int driver_helper(struct device *dev, void *data)
179 {
180         const char *name = data;
181
182         if (strcmp(name, dev->bus_id) == 0)
183                 return 1;
184         return 0;
185 }
186
187 static ssize_t driver_unbind(struct device_driver *drv,
188                              const char *buf, size_t count)
189 {
190         struct bus_type *bus = bus_get(drv->bus);
191         struct device *dev;
192         int err = -ENODEV;
193
194         dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
195         if (dev && dev->driver == drv) {
196                 if (dev->parent)        /* Needed for USB */
197                         down(&dev->parent->sem);
198                 device_release_driver(dev);
199                 if (dev->parent)
200                         up(&dev->parent->sem);
201                 err = count;
202         }
203         put_device(dev);
204         bus_put(bus);
205         return err;
206 }
207 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
208
209 /*
210  * Manually attach a device to a driver.
211  * Note: the driver must want to bind to the device,
212  * it is not possible to override the driver's id table.
213  */
214 static ssize_t driver_bind(struct device_driver *drv,
215                            const char *buf, size_t count)
216 {
217         struct bus_type *bus = bus_get(drv->bus);
218         struct device *dev;
219         int err = -ENODEV;
220
221         dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
222         if (dev && dev->driver == NULL) {
223                 if (dev->parent)        /* Needed for USB */
224                         down(&dev->parent->sem);
225                 down(&dev->sem);
226                 err = driver_probe_device(drv, dev);
227                 up(&dev->sem);
228                 if (dev->parent)
229                         up(&dev->parent->sem);
230
231                 if (err > 0)            /* success */
232                         err = count;
233                 else if (err == 0)      /* driver didn't accept device */
234                         err = -ENODEV;
235         }
236         put_device(dev);
237         bus_put(bus);
238         return err;
239 }
240 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
241
242 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
243 {
244         return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
245 }
246
247 static ssize_t store_drivers_autoprobe(struct bus_type *bus,
248                                        const char *buf, size_t count)
249 {
250         if (buf[0] == '0')
251                 bus->p->drivers_autoprobe = 0;
252         else
253                 bus->p->drivers_autoprobe = 1;
254         return count;
255 }
256
257 static ssize_t store_drivers_probe(struct bus_type *bus,
258                                    const char *buf, size_t count)
259 {
260         struct device *dev;
261
262         dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
263         if (!dev)
264                 return -ENODEV;
265         if (bus_rescan_devices_helper(dev, NULL) != 0)
266                 return -EINVAL;
267         return count;
268 }
269 #endif
270
271 static struct device * next_device(struct klist_iter * i)
272 {
273         struct klist_node * n = klist_next(i);
274         return n ? container_of(n, struct device, knode_bus) : NULL;
275 }
276
277 /**
278  *      bus_for_each_dev - device iterator.
279  *      @bus:   bus type.
280  *      @start: device to start iterating from.
281  *      @data:  data for the callback.
282  *      @fn:    function to be called for each device.
283  *
284  *      Iterate over @bus's list of devices, and call @fn for each,
285  *      passing it @data. If @start is not NULL, we use that device to
286  *      begin iterating from.
287  *
288  *      We check the return of @fn each time. If it returns anything
289  *      other than 0, we break out and return that value.
290  *
291  *      NOTE: The device that returns a non-zero value is not retained
292  *      in any way, nor is its refcount incremented. If the caller needs
293  *      to retain this data, it should do, and increment the reference
294  *      count in the supplied callback.
295  */
296
297 int bus_for_each_dev(struct bus_type * bus, struct device * start,
298                      void * data, int (*fn)(struct device *, void *))
299 {
300         struct klist_iter i;
301         struct device * dev;
302         int error = 0;
303
304         if (!bus)
305                 return -EINVAL;
306
307         klist_iter_init_node(&bus->p->klist_devices, &i,
308                              (start ? &start->knode_bus : NULL));
309         while ((dev = next_device(&i)) && !error)
310                 error = fn(dev, data);
311         klist_iter_exit(&i);
312         return error;
313 }
314
315 /**
316  * bus_find_device - device iterator for locating a particular device.
317  * @bus: bus type
318  * @start: Device to begin with
319  * @data: Data to pass to match function
320  * @match: Callback function to check device
321  *
322  * This is similar to the bus_for_each_dev() function above, but it
323  * returns a reference to a device that is 'found' for later use, as
324  * determined by the @match callback.
325  *
326  * The callback should return 0 if the device doesn't match and non-zero
327  * if it does.  If the callback returns non-zero, this function will
328  * return to the caller and not iterate over any more devices.
329  */
330 struct device * bus_find_device(struct bus_type *bus,
331                                 struct device *start, void *data,
332                                 int (*match)(struct device *, void *))
333 {
334         struct klist_iter i;
335         struct device *dev;
336
337         if (!bus)
338                 return NULL;
339
340         klist_iter_init_node(&bus->p->klist_devices, &i,
341                              (start ? &start->knode_bus : NULL));
342         while ((dev = next_device(&i)))
343                 if (match(dev, data) && get_device(dev))
344                         break;
345         klist_iter_exit(&i);
346         return dev;
347 }
348
349
350 static struct device_driver * next_driver(struct klist_iter * i)
351 {
352         struct klist_node * n = klist_next(i);
353         return n ? container_of(n, struct device_driver, knode_bus) : NULL;
354 }
355
356 /**
357  *      bus_for_each_drv - driver iterator
358  *      @bus:   bus we're dealing with.
359  *      @start: driver to start iterating on.
360  *      @data:  data to pass to the callback.
361  *      @fn:    function to call for each driver.
362  *
363  *      This is nearly identical to the device iterator above.
364  *      We iterate over each driver that belongs to @bus, and call
365  *      @fn for each. If @fn returns anything but 0, we break out
366  *      and return it. If @start is not NULL, we use it as the head
367  *      of the list.
368  *
369  *      NOTE: we don't return the driver that returns a non-zero
370  *      value, nor do we leave the reference count incremented for that
371  *      driver. If the caller needs to know that info, it must set it
372  *      in the callback. It must also be sure to increment the refcount
373  *      so it doesn't disappear before returning to the caller.
374  */
375
376 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
377                      void * data, int (*fn)(struct device_driver *, void *))
378 {
379         struct klist_iter i;
380         struct device_driver * drv;
381         int error = 0;
382
383         if (!bus)
384                 return -EINVAL;
385
386         klist_iter_init_node(&bus->p->klist_drivers, &i,
387                              start ? &start->knode_bus : NULL);
388         while ((drv = next_driver(&i)) && !error)
389                 error = fn(drv, data);
390         klist_iter_exit(&i);
391         return error;
392 }
393
394 static int device_add_attrs(struct bus_type *bus, struct device *dev)
395 {
396         int error = 0;
397         int i;
398
399         if (!bus->dev_attrs)
400                 return 0;
401
402         for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
403                 error = device_create_file(dev,&bus->dev_attrs[i]);
404                 if (error) {
405                         while (--i >= 0)
406                                 device_remove_file(dev, &bus->dev_attrs[i]);
407                         break;
408                 }
409         }
410         return error;
411 }
412
413 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
414 {
415         int i;
416
417         if (bus->dev_attrs) {
418                 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
419                         device_remove_file(dev,&bus->dev_attrs[i]);
420         }
421 }
422
423 #ifdef CONFIG_SYSFS_DEPRECATED
424 static int make_deprecated_bus_links(struct device *dev)
425 {
426         return sysfs_create_link(&dev->kobj,
427                                  &dev->bus->p->subsys.kobj, "bus");
428 }
429
430 static void remove_deprecated_bus_links(struct device *dev)
431 {
432         sysfs_remove_link(&dev->kobj, "bus");
433 }
434 #else
435 static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
436 static inline void remove_deprecated_bus_links(struct device *dev) { }
437 #endif
438
439 /**
440  *      bus_add_device - add device to bus
441  *      @dev:   device being added
442  *
443  *      - Add the device to its bus's list of devices.
444  *      - Create link to device's bus.
445  */
446 int bus_add_device(struct device * dev)
447 {
448         struct bus_type * bus = bus_get(dev->bus);
449         int error = 0;
450
451         if (bus) {
452                 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
453                 error = device_add_attrs(bus, dev);
454                 if (error)
455                         goto out_put;
456                 error = sysfs_create_link(&bus->p->devices_kset->kobj,
457                                                 &dev->kobj, dev->bus_id);
458                 if (error)
459                         goto out_id;
460                 error = sysfs_create_link(&dev->kobj,
461                                 &dev->bus->p->subsys.kobj, "subsystem");
462                 if (error)
463                         goto out_subsys;
464                 error = make_deprecated_bus_links(dev);
465                 if (error)
466                         goto out_deprecated;
467         }
468         return 0;
469
470 out_deprecated:
471         sysfs_remove_link(&dev->kobj, "subsystem");
472 out_subsys:
473         sysfs_remove_link(&bus->p->devices_kset->kobj, dev->bus_id);
474 out_id:
475         device_remove_attrs(bus, dev);
476 out_put:
477         bus_put(dev->bus);
478         return error;
479 }
480
481 /**
482  *      bus_attach_device - add device to bus
483  *      @dev:   device tried to attach to a driver
484  *
485  *      - Add device to bus's list of devices.
486  *      - Try to attach to driver.
487  */
488 void bus_attach_device(struct device * dev)
489 {
490         struct bus_type *bus = dev->bus;
491         int ret = 0;
492
493         if (bus) {
494                 dev->is_registered = 1;
495                 if (bus->p->drivers_autoprobe)
496                         ret = device_attach(dev);
497                 WARN_ON(ret < 0);
498                 if (ret >= 0)
499                         klist_add_tail(&dev->knode_bus, &bus->p->klist_devices);
500                 else
501                         dev->is_registered = 0;
502         }
503 }
504
505 /**
506  *      bus_remove_device - remove device from bus
507  *      @dev:   device to be removed
508  *
509  *      - Remove symlink from bus's directory.
510  *      - Delete device from bus's list.
511  *      - Detach from its driver.
512  *      - Drop reference taken in bus_add_device().
513  */
514 void bus_remove_device(struct device * dev)
515 {
516         if (dev->bus) {
517                 sysfs_remove_link(&dev->kobj, "subsystem");
518                 remove_deprecated_bus_links(dev);
519                 sysfs_remove_link(&dev->bus->p->devices_kset->kobj, dev->bus_id);
520                 device_remove_attrs(dev->bus, dev);
521                 if (dev->is_registered) {
522                         dev->is_registered = 0;
523                         klist_del(&dev->knode_bus);
524                 }
525                 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
526                 device_release_driver(dev);
527                 bus_put(dev->bus);
528         }
529 }
530
531 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
532 {
533         int error = 0;
534         int i;
535
536         if (bus->drv_attrs) {
537                 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
538                         error = driver_create_file(drv, &bus->drv_attrs[i]);
539                         if (error)
540                                 goto Err;
541                 }
542         }
543  Done:
544         return error;
545  Err:
546         while (--i >= 0)
547                 driver_remove_file(drv, &bus->drv_attrs[i]);
548         goto Done;
549 }
550
551
552 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
553 {
554         int i;
555
556         if (bus->drv_attrs) {
557                 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
558                         driver_remove_file(drv, &bus->drv_attrs[i]);
559         }
560 }
561
562 #ifdef CONFIG_HOTPLUG
563 /*
564  * Thanks to drivers making their tables __devinit, we can't allow manual
565  * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
566  */
567 static int __must_check add_bind_files(struct device_driver *drv)
568 {
569         int ret;
570
571         ret = driver_create_file(drv, &driver_attr_unbind);
572         if (ret == 0) {
573                 ret = driver_create_file(drv, &driver_attr_bind);
574                 if (ret)
575                         driver_remove_file(drv, &driver_attr_unbind);
576         }
577         return ret;
578 }
579
580 static void remove_bind_files(struct device_driver *drv)
581 {
582         driver_remove_file(drv, &driver_attr_bind);
583         driver_remove_file(drv, &driver_attr_unbind);
584 }
585
586 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
587 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
588                 show_drivers_autoprobe, store_drivers_autoprobe);
589
590 static int add_probe_files(struct bus_type *bus)
591 {
592         int retval;
593
594         retval = bus_create_file(bus, &bus_attr_drivers_probe);
595         if (retval)
596                 goto out;
597
598         retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
599         if (retval)
600                 bus_remove_file(bus, &bus_attr_drivers_probe);
601 out:
602         return retval;
603 }
604
605 static void remove_probe_files(struct bus_type *bus)
606 {
607         bus_remove_file(bus, &bus_attr_drivers_autoprobe);
608         bus_remove_file(bus, &bus_attr_drivers_probe);
609 }
610 #else
611 static inline int add_bind_files(struct device_driver *drv) { return 0; }
612 static inline void remove_bind_files(struct device_driver *drv) {}
613 static inline int add_probe_files(struct bus_type *bus) { return 0; }
614 static inline void remove_probe_files(struct bus_type *bus) {}
615 #endif
616
617 static ssize_t driver_uevent_store(struct device_driver *drv,
618                                    const char *buf, size_t count)
619 {
620         enum kobject_action action;
621
622         if (kobject_action_type(buf, count, &action) == 0)
623                 kobject_uevent(&drv->kobj, action);
624         return count;
625 }
626 static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
627
628 /**
629  *      bus_add_driver - Add a driver to the bus.
630  *      @drv:   driver.
631  *
632  */
633 int bus_add_driver(struct device_driver *drv)
634 {
635         struct bus_type * bus = bus_get(drv->bus);
636         int error = 0;
637
638         if (!bus)
639                 return -EINVAL;
640
641         pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
642         error = kobject_set_name(&drv->kobj, "%s", drv->name);
643         if (error)
644                 goto out_put_bus;
645         drv->kobj.kset = bus->p->drivers_kset;
646         drv->kobj.ktype = &driver_ktype;
647         error = kobject_register(&drv->kobj);
648         if (error)
649                 goto out_put_bus;
650
651         if (drv->bus->p->drivers_autoprobe) {
652                 error = driver_attach(drv);
653                 if (error)
654                         goto out_unregister;
655         }
656         klist_add_tail(&drv->knode_bus, &bus->p->klist_drivers);
657         module_add_driver(drv->owner, drv);
658
659         error = driver_create_file(drv, &driver_attr_uevent);
660         if (error) {
661                 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
662                         __FUNCTION__, drv->name);
663         }
664         error = driver_add_attrs(bus, drv);
665         if (error) {
666                 /* How the hell do we get out of this pickle? Give up */
667                 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
668                         __FUNCTION__, drv->name);
669         }
670         error = add_bind_files(drv);
671         if (error) {
672                 /* Ditto */
673                 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
674                         __FUNCTION__, drv->name);
675         }
676
677         return error;
678 out_unregister:
679         kobject_unregister(&drv->kobj);
680 out_put_bus:
681         bus_put(bus);
682         return error;
683 }
684
685 /**
686  *      bus_remove_driver - delete driver from bus's knowledge.
687  *      @drv:   driver.
688  *
689  *      Detach the driver from the devices it controls, and remove
690  *      it from its bus's list of drivers. Finally, we drop the reference
691  *      to the bus we took in bus_add_driver().
692  */
693
694 void bus_remove_driver(struct device_driver * drv)
695 {
696         if (!drv->bus)
697                 return;
698
699         remove_bind_files(drv);
700         driver_remove_attrs(drv->bus, drv);
701         driver_remove_file(drv, &driver_attr_uevent);
702         klist_remove(&drv->knode_bus);
703         pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
704         driver_detach(drv);
705         module_remove_driver(drv);
706         kobject_unregister(&drv->kobj);
707         bus_put(drv->bus);
708 }
709
710
711 /* Helper for bus_rescan_devices's iter */
712 static int __must_check bus_rescan_devices_helper(struct device *dev,
713                                                 void *data)
714 {
715         int ret = 0;
716
717         if (!dev->driver) {
718                 if (dev->parent)        /* Needed for USB */
719                         down(&dev->parent->sem);
720                 ret = device_attach(dev);
721                 if (dev->parent)
722                         up(&dev->parent->sem);
723         }
724         return ret < 0 ? ret : 0;
725 }
726
727 /**
728  * bus_rescan_devices - rescan devices on the bus for possible drivers
729  * @bus: the bus to scan.
730  *
731  * This function will look for devices on the bus with no driver
732  * attached and rescan it against existing drivers to see if it matches
733  * any by calling device_attach() for the unbound devices.
734  */
735 int bus_rescan_devices(struct bus_type * bus)
736 {
737         return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
738 }
739
740 /**
741  * device_reprobe - remove driver for a device and probe for a new driver
742  * @dev: the device to reprobe
743  *
744  * This function detaches the attached driver (if any) for the given
745  * device and restarts the driver probing process.  It is intended
746  * to use if probing criteria changed during a devices lifetime and
747  * driver attachment should change accordingly.
748  */
749 int device_reprobe(struct device *dev)
750 {
751         if (dev->driver) {
752                 if (dev->parent)        /* Needed for USB */
753                         down(&dev->parent->sem);
754                 device_release_driver(dev);
755                 if (dev->parent)
756                         up(&dev->parent->sem);
757         }
758         return bus_rescan_devices_helper(dev, NULL);
759 }
760 EXPORT_SYMBOL_GPL(device_reprobe);
761
762 /**
763  *      find_bus - locate bus by name.
764  *      @name:  name of bus.
765  *
766  *      Call kset_find_obj() to iterate over list of buses to
767  *      find a bus by name. Return bus if found.
768  *
769  *      Note that kset_find_obj increments bus' reference count.
770  */
771 #if 0
772 struct bus_type * find_bus(char * name)
773 {
774         struct kobject * k = kset_find_obj(bus_kset, name);
775         return k ? to_bus(k) : NULL;
776 }
777 #endif  /*  0  */
778
779
780 /**
781  *      bus_add_attrs - Add default attributes for this bus.
782  *      @bus:   Bus that has just been registered.
783  */
784
785 static int bus_add_attrs(struct bus_type * bus)
786 {
787         int error = 0;
788         int i;
789
790         if (bus->bus_attrs) {
791                 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
792                         error = bus_create_file(bus,&bus->bus_attrs[i]);
793                         if (error)
794                                 goto Err;
795                 }
796         }
797  Done:
798         return error;
799  Err:
800         while (--i >= 0)
801                 bus_remove_file(bus,&bus->bus_attrs[i]);
802         goto Done;
803 }
804
805 static void bus_remove_attrs(struct bus_type * bus)
806 {
807         int i;
808
809         if (bus->bus_attrs) {
810                 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
811                         bus_remove_file(bus,&bus->bus_attrs[i]);
812         }
813 }
814
815 static void klist_devices_get(struct klist_node *n)
816 {
817         struct device *dev = container_of(n, struct device, knode_bus);
818
819         get_device(dev);
820 }
821
822 static void klist_devices_put(struct klist_node *n)
823 {
824         struct device *dev = container_of(n, struct device, knode_bus);
825
826         put_device(dev);
827 }
828
829 static ssize_t bus_uevent_store(struct bus_type *bus,
830                                 const char *buf, size_t count)
831 {
832         enum kobject_action action;
833
834         if (kobject_action_type(buf, count, &action) == 0)
835                 kobject_uevent(&bus->p->subsys.kobj, action);
836         return count;
837 }
838 static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
839
840 /**
841  *      bus_register - register a bus with the system.
842  *      @bus:   bus.
843  *
844  *      Once we have that, we registered the bus with the kobject
845  *      infrastructure, then register the children subsystems it has:
846  *      the devices and drivers that belong to the bus.
847  */
848 int bus_register(struct bus_type * bus)
849 {
850         int retval;
851         struct bus_type_private *priv;
852
853         priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
854         if (!priv)
855                 return -ENOMEM;
856
857         priv->bus = bus;
858         bus->p = priv;
859
860         BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
861
862         retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
863         if (retval)
864                 goto out;
865
866         priv->subsys.kobj.kset = bus_kset;
867         priv->subsys.kobj.ktype = &bus_ktype;
868         priv->drivers_autoprobe = 1;
869
870         retval = kset_register(&priv->subsys);
871         if (retval)
872                 goto out;
873
874         retval = bus_create_file(bus, &bus_attr_uevent);
875         if (retval)
876                 goto bus_uevent_fail;
877
878         priv->devices_kset = kset_create_and_add("devices", NULL,
879                                                  &priv->subsys.kobj);
880         if (!priv->devices_kset) {
881                 retval = -ENOMEM;
882                 goto bus_devices_fail;
883         }
884
885         priv->drivers_kset = kset_create_and_add("drivers", NULL,
886                                                  &priv->subsys.kobj);
887         if (!priv->drivers_kset) {
888                 retval = -ENOMEM;
889                 goto bus_drivers_fail;
890         }
891
892         klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
893         klist_init(&priv->klist_drivers, NULL, NULL);
894
895         retval = add_probe_files(bus);
896         if (retval)
897                 goto bus_probe_files_fail;
898
899         retval = bus_add_attrs(bus);
900         if (retval)
901                 goto bus_attrs_fail;
902
903         pr_debug("bus type '%s' registered\n", bus->name);
904         return 0;
905
906 bus_attrs_fail:
907         remove_probe_files(bus);
908 bus_probe_files_fail:
909         kset_unregister(bus->p->drivers_kset);
910 bus_drivers_fail:
911         kset_unregister(bus->p->devices_kset);
912 bus_devices_fail:
913         bus_remove_file(bus, &bus_attr_uevent);
914 bus_uevent_fail:
915         kset_unregister(&bus->p->subsys);
916         kfree(bus->p);
917 out:
918         return retval;
919 }
920
921 /**
922  *      bus_unregister - remove a bus from the system
923  *      @bus:   bus.
924  *
925  *      Unregister the child subsystems and the bus itself.
926  *      Finally, we call bus_put() to release the refcount
927  */
928 void bus_unregister(struct bus_type * bus)
929 {
930         pr_debug("bus %s: unregistering\n", bus->name);
931         bus_remove_attrs(bus);
932         remove_probe_files(bus);
933         kset_unregister(bus->p->drivers_kset);
934         kset_unregister(bus->p->devices_kset);
935         bus_remove_file(bus, &bus_attr_uevent);
936         kset_unregister(&bus->p->subsys);
937         kfree(bus->p);
938 }
939
940 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
941 {
942         return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
943 }
944 EXPORT_SYMBOL_GPL(bus_register_notifier);
945
946 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
947 {
948         return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
949 }
950 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
951
952 struct kset *bus_get_kset(struct bus_type *bus)
953 {
954         return &bus->p->subsys;
955 }
956 EXPORT_SYMBOL_GPL(bus_get_kset);
957
958 struct klist *bus_get_device_klist(struct bus_type *bus)
959 {
960         return &bus->p->klist_devices;
961 }
962 EXPORT_SYMBOL_GPL(bus_get_device_klist);
963
964 int __init buses_init(void)
965 {
966         bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
967         if (!bus_kset)
968                 return -ENOMEM;
969         return 0;
970 }
971
972
973 EXPORT_SYMBOL_GPL(bus_for_each_dev);
974 EXPORT_SYMBOL_GPL(bus_find_device);
975 EXPORT_SYMBOL_GPL(bus_for_each_drv);
976
977 EXPORT_SYMBOL_GPL(bus_register);
978 EXPORT_SYMBOL_GPL(bus_unregister);
979 EXPORT_SYMBOL_GPL(bus_rescan_devices);
980
981 EXPORT_SYMBOL_GPL(bus_create_file);
982 EXPORT_SYMBOL_GPL(bus_remove_file);