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