GNU Linux-libre 4.4.283-gnu1
[releases.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/async.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/string.h>
20 #include <linux/mutex.h>
21 #include <linux/sysfs.h>
22 #include "base.h"
23 #include "power/power.h"
24
25 /* /sys/devices/system */
26 static struct kset *system_kset;
27
28 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
29
30 /*
31  * sysfs bindings for drivers
32  */
33
34 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
35
36 #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
37         struct driver_attribute driver_attr_##_name =           \
38                 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
39
40 static int __must_check bus_rescan_devices_helper(struct device *dev,
41                                                 void *data);
42
43 static struct bus_type *bus_get(struct bus_type *bus)
44 {
45         if (bus) {
46                 kset_get(&bus->p->subsys);
47                 return bus;
48         }
49         return NULL;
50 }
51
52 static void bus_put(struct bus_type *bus)
53 {
54         if (bus)
55                 kset_put(&bus->p->subsys);
56 }
57
58 static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
59                              char *buf)
60 {
61         struct driver_attribute *drv_attr = to_drv_attr(attr);
62         struct driver_private *drv_priv = to_driver(kobj);
63         ssize_t ret = -EIO;
64
65         if (drv_attr->show)
66                 ret = drv_attr->show(drv_priv->driver, buf);
67         return ret;
68 }
69
70 static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
71                               const char *buf, size_t count)
72 {
73         struct driver_attribute *drv_attr = to_drv_attr(attr);
74         struct driver_private *drv_priv = to_driver(kobj);
75         ssize_t ret = -EIO;
76
77         if (drv_attr->store)
78                 ret = drv_attr->store(drv_priv->driver, buf, count);
79         return ret;
80 }
81
82 static const struct sysfs_ops driver_sysfs_ops = {
83         .show   = drv_attr_show,
84         .store  = drv_attr_store,
85 };
86
87 static void driver_release(struct kobject *kobj)
88 {
89         struct driver_private *drv_priv = to_driver(kobj);
90
91         pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
92         kfree(drv_priv);
93 }
94
95 static struct kobj_type driver_ktype = {
96         .sysfs_ops      = &driver_sysfs_ops,
97         .release        = driver_release,
98 };
99
100 /*
101  * sysfs bindings for buses
102  */
103 static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
104                              char *buf)
105 {
106         struct bus_attribute *bus_attr = to_bus_attr(attr);
107         struct subsys_private *subsys_priv = to_subsys_private(kobj);
108         ssize_t ret = 0;
109
110         if (bus_attr->show)
111                 ret = bus_attr->show(subsys_priv->bus, buf);
112         return ret;
113 }
114
115 static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
116                               const char *buf, size_t count)
117 {
118         struct bus_attribute *bus_attr = to_bus_attr(attr);
119         struct subsys_private *subsys_priv = to_subsys_private(kobj);
120         ssize_t ret = 0;
121
122         if (bus_attr->store)
123                 ret = bus_attr->store(subsys_priv->bus, buf, count);
124         return ret;
125 }
126
127 static const struct sysfs_ops bus_sysfs_ops = {
128         .show   = bus_attr_show,
129         .store  = bus_attr_store,
130 };
131
132 int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
133 {
134         int error;
135         if (bus_get(bus)) {
136                 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
137                 bus_put(bus);
138         } else
139                 error = -EINVAL;
140         return error;
141 }
142 EXPORT_SYMBOL_GPL(bus_create_file);
143
144 void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
145 {
146         if (bus_get(bus)) {
147                 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
148                 bus_put(bus);
149         }
150 }
151 EXPORT_SYMBOL_GPL(bus_remove_file);
152
153 static void bus_release(struct kobject *kobj)
154 {
155         struct subsys_private *priv =
156                 container_of(kobj, typeof(*priv), subsys.kobj);
157         struct bus_type *bus = priv->bus;
158
159         kfree(priv);
160         bus->p = NULL;
161 }
162
163 static struct kobj_type bus_ktype = {
164         .sysfs_ops      = &bus_sysfs_ops,
165         .release        = bus_release,
166 };
167
168 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
169 {
170         struct kobj_type *ktype = get_ktype(kobj);
171
172         if (ktype == &bus_ktype)
173                 return 1;
174         return 0;
175 }
176
177 static const struct kset_uevent_ops bus_uevent_ops = {
178         .filter = bus_uevent_filter,
179 };
180
181 static struct kset *bus_kset;
182
183 /* Manually detach a device from its associated driver. */
184 static ssize_t unbind_store(struct device_driver *drv, const char *buf,
185                             size_t count)
186 {
187         struct bus_type *bus = bus_get(drv->bus);
188         struct device *dev;
189         int err = -ENODEV;
190
191         dev = bus_find_device_by_name(bus, NULL, buf);
192         if (dev && dev->driver == drv) {
193                 if (dev->parent)        /* Needed for USB */
194                         device_lock(dev->parent);
195                 device_release_driver(dev);
196                 if (dev->parent)
197                         device_unlock(dev->parent);
198                 err = count;
199         }
200         put_device(dev);
201         bus_put(bus);
202         return err;
203 }
204 static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, S_IWUSR, NULL, unbind_store);
205
206 /*
207  * Manually attach a device to a driver.
208  * Note: the driver must want to bind to the device,
209  * it is not possible to override the driver's id table.
210  */
211 static ssize_t bind_store(struct device_driver *drv, const char *buf,
212                           size_t count)
213 {
214         struct bus_type *bus = bus_get(drv->bus);
215         struct device *dev;
216         int err = -ENODEV;
217
218         dev = bus_find_device_by_name(bus, NULL, buf);
219         if (dev && dev->driver == NULL && driver_match_device(drv, dev)) {
220                 if (dev->parent)        /* Needed for USB */
221                         device_lock(dev->parent);
222                 device_lock(dev);
223                 err = driver_probe_device(drv, dev);
224                 device_unlock(dev);
225                 if (dev->parent)
226                         device_unlock(dev->parent);
227
228                 if (err > 0) {
229                         /* success */
230                         err = count;
231                 } else if (err == 0) {
232                         /* driver didn't accept device */
233                         err = -ENODEV;
234                 }
235         }
236         put_device(dev);
237         bus_put(bus);
238         return err;
239 }
240 static DRIVER_ATTR_IGNORE_LOCKDEP(bind, S_IWUSR, NULL, bind_store);
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         int err = -EINVAL;
262
263         dev = bus_find_device_by_name(bus, NULL, buf);
264         if (!dev)
265                 return -ENODEV;
266         if (bus_rescan_devices_helper(dev, NULL) == 0)
267                 err = count;
268         put_device(dev);
269         return err;
270 }
271
272 static struct device *next_device(struct klist_iter *i)
273 {
274         struct klist_node *n = klist_next(i);
275         struct device *dev = NULL;
276         struct device_private *dev_prv;
277
278         if (n) {
279                 dev_prv = to_device_private_bus(n);
280                 dev = dev_prv->device;
281         }
282         return dev;
283 }
284
285 /**
286  * bus_for_each_dev - device iterator.
287  * @bus: bus type.
288  * @start: device to start iterating from.
289  * @data: data for the callback.
290  * @fn: function to be called for each device.
291  *
292  * Iterate over @bus's list of devices, and call @fn for each,
293  * passing it @data. If @start is not NULL, we use that device to
294  * begin iterating from.
295  *
296  * We check the return of @fn each time. If it returns anything
297  * other than 0, we break out and return that value.
298  *
299  * NOTE: The device that returns a non-zero value is not retained
300  * in any way, nor is its refcount incremented. If the caller needs
301  * to retain this data, it should do so, and increment the reference
302  * count in the supplied callback.
303  */
304 int bus_for_each_dev(struct bus_type *bus, struct device *start,
305                      void *data, int (*fn)(struct device *, void *))
306 {
307         struct klist_iter i;
308         struct device *dev;
309         int error = 0;
310
311         if (!bus || !bus->p)
312                 return -EINVAL;
313
314         klist_iter_init_node(&bus->p->klist_devices, &i,
315                              (start ? &start->p->knode_bus : NULL));
316         while ((dev = next_device(&i)) && !error)
317                 error = fn(dev, data);
318         klist_iter_exit(&i);
319         return error;
320 }
321 EXPORT_SYMBOL_GPL(bus_for_each_dev);
322
323 /**
324  * bus_find_device - device iterator for locating a particular device.
325  * @bus: bus type
326  * @start: Device to begin with
327  * @data: Data to pass to match function
328  * @match: Callback function to check device
329  *
330  * This is similar to the bus_for_each_dev() function above, but it
331  * returns a reference to a device that is 'found' for later use, as
332  * determined by the @match callback.
333  *
334  * The callback should return 0 if the device doesn't match and non-zero
335  * if it does.  If the callback returns non-zero, this function will
336  * return to the caller and not iterate over any more devices.
337  */
338 struct device *bus_find_device(struct bus_type *bus,
339                                struct device *start, void *data,
340                                int (*match)(struct device *dev, void *data))
341 {
342         struct klist_iter i;
343         struct device *dev;
344
345         if (!bus || !bus->p)
346                 return NULL;
347
348         klist_iter_init_node(&bus->p->klist_devices, &i,
349                              (start ? &start->p->knode_bus : NULL));
350         while ((dev = next_device(&i)))
351                 if (match(dev, data) && get_device(dev))
352                         break;
353         klist_iter_exit(&i);
354         return dev;
355 }
356 EXPORT_SYMBOL_GPL(bus_find_device);
357
358 static int match_name(struct device *dev, void *data)
359 {
360         const char *name = data;
361
362         return sysfs_streq(name, dev_name(dev));
363 }
364
365 /**
366  * bus_find_device_by_name - device iterator for locating a particular device of a specific name
367  * @bus: bus type
368  * @start: Device to begin with
369  * @name: name of the device to match
370  *
371  * This is similar to the bus_find_device() function above, but it handles
372  * searching by a name automatically, no need to write another strcmp matching
373  * function.
374  */
375 struct device *bus_find_device_by_name(struct bus_type *bus,
376                                        struct device *start, const char *name)
377 {
378         return bus_find_device(bus, start, (void *)name, match_name);
379 }
380 EXPORT_SYMBOL_GPL(bus_find_device_by_name);
381
382 /**
383  * subsys_find_device_by_id - find a device with a specific enumeration number
384  * @subsys: subsystem
385  * @id: index 'id' in struct device
386  * @hint: device to check first
387  *
388  * Check the hint's next object and if it is a match return it directly,
389  * otherwise, fall back to a full list search. Either way a reference for
390  * the returned object is taken.
391  */
392 struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
393                                         struct device *hint)
394 {
395         struct klist_iter i;
396         struct device *dev;
397
398         if (!subsys)
399                 return NULL;
400
401         if (hint) {
402                 klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
403                 dev = next_device(&i);
404                 if (dev && dev->id == id && get_device(dev)) {
405                         klist_iter_exit(&i);
406                         return dev;
407                 }
408                 klist_iter_exit(&i);
409         }
410
411         klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
412         while ((dev = next_device(&i))) {
413                 if (dev->id == id && get_device(dev)) {
414                         klist_iter_exit(&i);
415                         return dev;
416                 }
417         }
418         klist_iter_exit(&i);
419         return NULL;
420 }
421 EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
422
423 static struct device_driver *next_driver(struct klist_iter *i)
424 {
425         struct klist_node *n = klist_next(i);
426         struct driver_private *drv_priv;
427
428         if (n) {
429                 drv_priv = container_of(n, struct driver_private, knode_bus);
430                 return drv_priv->driver;
431         }
432         return NULL;
433 }
434
435 /**
436  * bus_for_each_drv - driver iterator
437  * @bus: bus we're dealing with.
438  * @start: driver to start iterating on.
439  * @data: data to pass to the callback.
440  * @fn: function to call for each driver.
441  *
442  * This is nearly identical to the device iterator above.
443  * We iterate over each driver that belongs to @bus, and call
444  * @fn for each. If @fn returns anything but 0, we break out
445  * and return it. If @start is not NULL, we use it as the head
446  * of the list.
447  *
448  * NOTE: we don't return the driver that returns a non-zero
449  * value, nor do we leave the reference count incremented for that
450  * driver. If the caller needs to know that info, it must set it
451  * in the callback. It must also be sure to increment the refcount
452  * so it doesn't disappear before returning to the caller.
453  */
454 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
455                      void *data, int (*fn)(struct device_driver *, void *))
456 {
457         struct klist_iter i;
458         struct device_driver *drv;
459         int error = 0;
460
461         if (!bus)
462                 return -EINVAL;
463
464         klist_iter_init_node(&bus->p->klist_drivers, &i,
465                              start ? &start->p->knode_bus : NULL);
466         while ((drv = next_driver(&i)) && !error)
467                 error = fn(drv, data);
468         klist_iter_exit(&i);
469         return error;
470 }
471 EXPORT_SYMBOL_GPL(bus_for_each_drv);
472
473 static int device_add_attrs(struct bus_type *bus, struct device *dev)
474 {
475         int error = 0;
476         int i;
477
478         if (!bus->dev_attrs)
479                 return 0;
480
481         for (i = 0; bus->dev_attrs[i].attr.name; i++) {
482                 error = device_create_file(dev, &bus->dev_attrs[i]);
483                 if (error) {
484                         while (--i >= 0)
485                                 device_remove_file(dev, &bus->dev_attrs[i]);
486                         break;
487                 }
488         }
489         return error;
490 }
491
492 static void device_remove_attrs(struct bus_type *bus, struct device *dev)
493 {
494         int i;
495
496         if (bus->dev_attrs) {
497                 for (i = 0; bus->dev_attrs[i].attr.name; i++)
498                         device_remove_file(dev, &bus->dev_attrs[i]);
499         }
500 }
501
502 /**
503  * bus_add_device - add device to bus
504  * @dev: device being added
505  *
506  * - Add device's bus attributes.
507  * - Create links to device's bus.
508  * - Add the device to its bus's list of devices.
509  */
510 int bus_add_device(struct device *dev)
511 {
512         struct bus_type *bus = bus_get(dev->bus);
513         int error = 0;
514
515         if (bus) {
516                 pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
517                 error = device_add_attrs(bus, dev);
518                 if (error)
519                         goto out_put;
520                 error = device_add_groups(dev, bus->dev_groups);
521                 if (error)
522                         goto out_id;
523                 error = sysfs_create_link(&bus->p->devices_kset->kobj,
524                                                 &dev->kobj, dev_name(dev));
525                 if (error)
526                         goto out_groups;
527                 error = sysfs_create_link(&dev->kobj,
528                                 &dev->bus->p->subsys.kobj, "subsystem");
529                 if (error)
530                         goto out_subsys;
531                 klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
532         }
533         return 0;
534
535 out_subsys:
536         sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
537 out_groups:
538         device_remove_groups(dev, bus->dev_groups);
539 out_id:
540         device_remove_attrs(bus, dev);
541 out_put:
542         bus_put(dev->bus);
543         return error;
544 }
545
546 /**
547  * bus_probe_device - probe drivers for a new device
548  * @dev: device to probe
549  *
550  * - Automatically probe for a driver if the bus allows it.
551  */
552 void bus_probe_device(struct device *dev)
553 {
554         struct bus_type *bus = dev->bus;
555         struct subsys_interface *sif;
556
557         if (!bus)
558                 return;
559
560         if (bus->p->drivers_autoprobe)
561                 device_initial_probe(dev);
562
563         mutex_lock(&bus->p->mutex);
564         list_for_each_entry(sif, &bus->p->interfaces, node)
565                 if (sif->add_dev)
566                         sif->add_dev(dev, sif);
567         mutex_unlock(&bus->p->mutex);
568 }
569
570 /**
571  * bus_remove_device - remove device from bus
572  * @dev: device to be removed
573  *
574  * - Remove device from all interfaces.
575  * - Remove symlink from bus' directory.
576  * - Delete device from bus's list.
577  * - Detach from its driver.
578  * - Drop reference taken in bus_add_device().
579  */
580 void bus_remove_device(struct device *dev)
581 {
582         struct bus_type *bus = dev->bus;
583         struct subsys_interface *sif;
584
585         if (!bus)
586                 return;
587
588         mutex_lock(&bus->p->mutex);
589         list_for_each_entry(sif, &bus->p->interfaces, node)
590                 if (sif->remove_dev)
591                         sif->remove_dev(dev, sif);
592         mutex_unlock(&bus->p->mutex);
593
594         sysfs_remove_link(&dev->kobj, "subsystem");
595         sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
596                           dev_name(dev));
597         device_remove_attrs(dev->bus, dev);
598         device_remove_groups(dev, dev->bus->dev_groups);
599         if (klist_node_attached(&dev->p->knode_bus))
600                 klist_del(&dev->p->knode_bus);
601
602         pr_debug("bus: '%s': remove device %s\n",
603                  dev->bus->name, dev_name(dev));
604         device_release_driver(dev);
605         bus_put(dev->bus);
606 }
607
608 static int __must_check add_bind_files(struct device_driver *drv)
609 {
610         int ret;
611
612         ret = driver_create_file(drv, &driver_attr_unbind);
613         if (ret == 0) {
614                 ret = driver_create_file(drv, &driver_attr_bind);
615                 if (ret)
616                         driver_remove_file(drv, &driver_attr_unbind);
617         }
618         return ret;
619 }
620
621 static void remove_bind_files(struct device_driver *drv)
622 {
623         driver_remove_file(drv, &driver_attr_bind);
624         driver_remove_file(drv, &driver_attr_unbind);
625 }
626
627 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
628 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
629                 show_drivers_autoprobe, store_drivers_autoprobe);
630
631 static int add_probe_files(struct bus_type *bus)
632 {
633         int retval;
634
635         retval = bus_create_file(bus, &bus_attr_drivers_probe);
636         if (retval)
637                 goto out;
638
639         retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
640         if (retval)
641                 bus_remove_file(bus, &bus_attr_drivers_probe);
642 out:
643         return retval;
644 }
645
646 static void remove_probe_files(struct bus_type *bus)
647 {
648         bus_remove_file(bus, &bus_attr_drivers_autoprobe);
649         bus_remove_file(bus, &bus_attr_drivers_probe);
650 }
651
652 static ssize_t uevent_store(struct device_driver *drv, const char *buf,
653                             size_t count)
654 {
655         enum kobject_action action;
656
657         if (kobject_action_type(buf, count, &action) == 0)
658                 kobject_uevent(&drv->p->kobj, action);
659         return count;
660 }
661 static DRIVER_ATTR_WO(uevent);
662
663 static void driver_attach_async(void *_drv, async_cookie_t cookie)
664 {
665         struct device_driver *drv = _drv;
666         int ret;
667
668         ret = driver_attach(drv);
669
670         pr_debug("bus: '%s': driver %s async attach completed: %d\n",
671                  drv->bus->name, drv->name, ret);
672 }
673
674 /**
675  * bus_add_driver - Add a driver to the bus.
676  * @drv: driver.
677  */
678 int bus_add_driver(struct device_driver *drv)
679 {
680         struct bus_type *bus;
681         struct driver_private *priv;
682         int error = 0;
683
684         bus = bus_get(drv->bus);
685         if (!bus)
686                 return -EINVAL;
687
688         pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
689
690         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
691         if (!priv) {
692                 error = -ENOMEM;
693                 goto out_put_bus;
694         }
695         klist_init(&priv->klist_devices, NULL, NULL);
696         priv->driver = drv;
697         drv->p = priv;
698         priv->kobj.kset = bus->p->drivers_kset;
699         error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
700                                      "%s", drv->name);
701         if (error)
702                 goto out_unregister;
703
704         klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
705         if (drv->bus->p->drivers_autoprobe) {
706                 if (driver_allows_async_probing(drv)) {
707                         pr_debug("bus: '%s': probing driver %s asynchronously\n",
708                                 drv->bus->name, drv->name);
709                         async_schedule(driver_attach_async, drv);
710                 } else {
711                         error = driver_attach(drv);
712                         if (error)
713                                 goto out_unregister;
714                 }
715         }
716         module_add_driver(drv->owner, drv);
717
718         error = driver_create_file(drv, &driver_attr_uevent);
719         if (error) {
720                 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
721                         __func__, drv->name);
722         }
723         error = driver_add_groups(drv, bus->drv_groups);
724         if (error) {
725                 /* How the hell do we get out of this pickle? Give up */
726                 printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
727                         __func__, drv->name);
728         }
729
730         if (!drv->suppress_bind_attrs) {
731                 error = add_bind_files(drv);
732                 if (error) {
733                         /* Ditto */
734                         printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
735                                 __func__, drv->name);
736                 }
737         }
738
739         return 0;
740
741 out_unregister:
742         kobject_put(&priv->kobj);
743         /* drv->p is freed in driver_release()  */
744         drv->p = NULL;
745 out_put_bus:
746         bus_put(bus);
747         return error;
748 }
749
750 /**
751  * bus_remove_driver - delete driver from bus's knowledge.
752  * @drv: driver.
753  *
754  * Detach the driver from the devices it controls, and remove
755  * it from its bus's list of drivers. Finally, we drop the reference
756  * to the bus we took in bus_add_driver().
757  */
758 void bus_remove_driver(struct device_driver *drv)
759 {
760         if (!drv->bus)
761                 return;
762
763         if (!drv->suppress_bind_attrs)
764                 remove_bind_files(drv);
765         driver_remove_groups(drv, drv->bus->drv_groups);
766         driver_remove_file(drv, &driver_attr_uevent);
767         klist_remove(&drv->p->knode_bus);
768         pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
769         driver_detach(drv);
770         module_remove_driver(drv);
771         kobject_put(&drv->p->kobj);
772         bus_put(drv->bus);
773 }
774
775 /* Helper for bus_rescan_devices's iter */
776 static int __must_check bus_rescan_devices_helper(struct device *dev,
777                                                   void *data)
778 {
779         int ret = 0;
780
781         if (!dev->driver) {
782                 if (dev->parent)        /* Needed for USB */
783                         device_lock(dev->parent);
784                 ret = device_attach(dev);
785                 if (dev->parent)
786                         device_unlock(dev->parent);
787         }
788         return ret < 0 ? ret : 0;
789 }
790
791 /**
792  * bus_rescan_devices - rescan devices on the bus for possible drivers
793  * @bus: the bus to scan.
794  *
795  * This function will look for devices on the bus with no driver
796  * attached and rescan it against existing drivers to see if it matches
797  * any by calling device_attach() for the unbound devices.
798  */
799 int bus_rescan_devices(struct bus_type *bus)
800 {
801         return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
802 }
803 EXPORT_SYMBOL_GPL(bus_rescan_devices);
804
805 /**
806  * device_reprobe - remove driver for a device and probe for a new driver
807  * @dev: the device to reprobe
808  *
809  * This function detaches the attached driver (if any) for the given
810  * device and restarts the driver probing process.  It is intended
811  * to use if probing criteria changed during a devices lifetime and
812  * driver attachment should change accordingly.
813  */
814 int device_reprobe(struct device *dev)
815 {
816         if (dev->driver) {
817                 if (dev->parent)        /* Needed for USB */
818                         device_lock(dev->parent);
819                 device_release_driver(dev);
820                 if (dev->parent)
821                         device_unlock(dev->parent);
822         }
823         return bus_rescan_devices_helper(dev, NULL);
824 }
825 EXPORT_SYMBOL_GPL(device_reprobe);
826
827 /**
828  * find_bus - locate bus by name.
829  * @name: name of bus.
830  *
831  * Call kset_find_obj() to iterate over list of buses to
832  * find a bus by name. Return bus if found.
833  *
834  * Note that kset_find_obj increments bus' reference count.
835  */
836 #if 0
837 struct bus_type *find_bus(char *name)
838 {
839         struct kobject *k = kset_find_obj(bus_kset, name);
840         return k ? to_bus(k) : NULL;
841 }
842 #endif  /*  0  */
843
844 static int bus_add_groups(struct bus_type *bus,
845                           const struct attribute_group **groups)
846 {
847         return sysfs_create_groups(&bus->p->subsys.kobj, groups);
848 }
849
850 static void bus_remove_groups(struct bus_type *bus,
851                               const struct attribute_group **groups)
852 {
853         sysfs_remove_groups(&bus->p->subsys.kobj, groups);
854 }
855
856 static void klist_devices_get(struct klist_node *n)
857 {
858         struct device_private *dev_prv = to_device_private_bus(n);
859         struct device *dev = dev_prv->device;
860
861         get_device(dev);
862 }
863
864 static void klist_devices_put(struct klist_node *n)
865 {
866         struct device_private *dev_prv = to_device_private_bus(n);
867         struct device *dev = dev_prv->device;
868
869         put_device(dev);
870 }
871
872 static ssize_t bus_uevent_store(struct bus_type *bus,
873                                 const char *buf, size_t count)
874 {
875         enum kobject_action action;
876
877         if (kobject_action_type(buf, count, &action) == 0)
878                 kobject_uevent(&bus->p->subsys.kobj, action);
879         return count;
880 }
881 static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
882
883 /**
884  * bus_register - register a driver-core subsystem
885  * @bus: bus to register
886  *
887  * Once we have that, we register the bus with the kobject
888  * infrastructure, then register the children subsystems it has:
889  * the devices and drivers that belong to the subsystem.
890  */
891 int bus_register(struct bus_type *bus)
892 {
893         int retval;
894         struct subsys_private *priv;
895         struct lock_class_key *key = &bus->lock_key;
896
897         priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
898         if (!priv)
899                 return -ENOMEM;
900
901         priv->bus = bus;
902         bus->p = priv;
903
904         BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
905
906         retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
907         if (retval)
908                 goto out;
909
910         priv->subsys.kobj.kset = bus_kset;
911         priv->subsys.kobj.ktype = &bus_ktype;
912         priv->drivers_autoprobe = 1;
913
914         retval = kset_register(&priv->subsys);
915         if (retval)
916                 goto out;
917
918         retval = bus_create_file(bus, &bus_attr_uevent);
919         if (retval)
920                 goto bus_uevent_fail;
921
922         priv->devices_kset = kset_create_and_add("devices", NULL,
923                                                  &priv->subsys.kobj);
924         if (!priv->devices_kset) {
925                 retval = -ENOMEM;
926                 goto bus_devices_fail;
927         }
928
929         priv->drivers_kset = kset_create_and_add("drivers", NULL,
930                                                  &priv->subsys.kobj);
931         if (!priv->drivers_kset) {
932                 retval = -ENOMEM;
933                 goto bus_drivers_fail;
934         }
935
936         INIT_LIST_HEAD(&priv->interfaces);
937         __mutex_init(&priv->mutex, "subsys mutex", key);
938         klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
939         klist_init(&priv->klist_drivers, NULL, NULL);
940
941         retval = add_probe_files(bus);
942         if (retval)
943                 goto bus_probe_files_fail;
944
945         retval = bus_add_groups(bus, bus->bus_groups);
946         if (retval)
947                 goto bus_groups_fail;
948
949         pr_debug("bus: '%s': registered\n", bus->name);
950         return 0;
951
952 bus_groups_fail:
953         remove_probe_files(bus);
954 bus_probe_files_fail:
955         kset_unregister(bus->p->drivers_kset);
956 bus_drivers_fail:
957         kset_unregister(bus->p->devices_kset);
958 bus_devices_fail:
959         bus_remove_file(bus, &bus_attr_uevent);
960 bus_uevent_fail:
961         kset_unregister(&bus->p->subsys);
962 out:
963         kfree(bus->p);
964         bus->p = NULL;
965         return retval;
966 }
967 EXPORT_SYMBOL_GPL(bus_register);
968
969 /**
970  * bus_unregister - remove a bus from the system
971  * @bus: bus.
972  *
973  * Unregister the child subsystems and the bus itself.
974  * Finally, we call bus_put() to release the refcount
975  */
976 void bus_unregister(struct bus_type *bus)
977 {
978         pr_debug("bus: '%s': unregistering\n", bus->name);
979         if (bus->dev_root)
980                 device_unregister(bus->dev_root);
981         bus_remove_groups(bus, bus->bus_groups);
982         remove_probe_files(bus);
983         kset_unregister(bus->p->drivers_kset);
984         kset_unregister(bus->p->devices_kset);
985         bus_remove_file(bus, &bus_attr_uevent);
986         kset_unregister(&bus->p->subsys);
987 }
988 EXPORT_SYMBOL_GPL(bus_unregister);
989
990 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
991 {
992         return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
993 }
994 EXPORT_SYMBOL_GPL(bus_register_notifier);
995
996 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
997 {
998         return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
999 }
1000 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
1001
1002 struct kset *bus_get_kset(struct bus_type *bus)
1003 {
1004         return &bus->p->subsys;
1005 }
1006 EXPORT_SYMBOL_GPL(bus_get_kset);
1007
1008 struct klist *bus_get_device_klist(struct bus_type *bus)
1009 {
1010         return &bus->p->klist_devices;
1011 }
1012 EXPORT_SYMBOL_GPL(bus_get_device_klist);
1013
1014 /*
1015  * Yes, this forcibly breaks the klist abstraction temporarily.  It
1016  * just wants to sort the klist, not change reference counts and
1017  * take/drop locks rapidly in the process.  It does all this while
1018  * holding the lock for the list, so objects can't otherwise be
1019  * added/removed while we're swizzling.
1020  */
1021 static void device_insertion_sort_klist(struct device *a, struct list_head *list,
1022                                         int (*compare)(const struct device *a,
1023                                                         const struct device *b))
1024 {
1025         struct list_head *pos;
1026         struct klist_node *n;
1027         struct device_private *dev_prv;
1028         struct device *b;
1029
1030         list_for_each(pos, list) {
1031                 n = container_of(pos, struct klist_node, n_node);
1032                 dev_prv = to_device_private_bus(n);
1033                 b = dev_prv->device;
1034                 if (compare(a, b) <= 0) {
1035                         list_move_tail(&a->p->knode_bus.n_node,
1036                                        &b->p->knode_bus.n_node);
1037                         return;
1038                 }
1039         }
1040         list_move_tail(&a->p->knode_bus.n_node, list);
1041 }
1042
1043 void bus_sort_breadthfirst(struct bus_type *bus,
1044                            int (*compare)(const struct device *a,
1045                                           const struct device *b))
1046 {
1047         LIST_HEAD(sorted_devices);
1048         struct list_head *pos, *tmp;
1049         struct klist_node *n;
1050         struct device_private *dev_prv;
1051         struct device *dev;
1052         struct klist *device_klist;
1053
1054         device_klist = bus_get_device_klist(bus);
1055
1056         spin_lock(&device_klist->k_lock);
1057         list_for_each_safe(pos, tmp, &device_klist->k_list) {
1058                 n = container_of(pos, struct klist_node, n_node);
1059                 dev_prv = to_device_private_bus(n);
1060                 dev = dev_prv->device;
1061                 device_insertion_sort_klist(dev, &sorted_devices, compare);
1062         }
1063         list_splice(&sorted_devices, &device_klist->k_list);
1064         spin_unlock(&device_klist->k_lock);
1065 }
1066 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
1067
1068 /**
1069  * subsys_dev_iter_init - initialize subsys device iterator
1070  * @iter: subsys iterator to initialize
1071  * @subsys: the subsys we wanna iterate over
1072  * @start: the device to start iterating from, if any
1073  * @type: device_type of the devices to iterate over, NULL for all
1074  *
1075  * Initialize subsys iterator @iter such that it iterates over devices
1076  * of @subsys.  If @start is set, the list iteration will start there,
1077  * otherwise if it is NULL, the iteration starts at the beginning of
1078  * the list.
1079  */
1080 void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1081                           struct device *start, const struct device_type *type)
1082 {
1083         struct klist_node *start_knode = NULL;
1084
1085         if (start)
1086                 start_knode = &start->p->knode_bus;
1087         klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1088         iter->type = type;
1089 }
1090 EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
1091
1092 /**
1093  * subsys_dev_iter_next - iterate to the next device
1094  * @iter: subsys iterator to proceed
1095  *
1096  * Proceed @iter to the next device and return it.  Returns NULL if
1097  * iteration is complete.
1098  *
1099  * The returned device is referenced and won't be released till
1100  * iterator is proceed to the next device or exited.  The caller is
1101  * free to do whatever it wants to do with the device including
1102  * calling back into subsys code.
1103  */
1104 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1105 {
1106         struct klist_node *knode;
1107         struct device *dev;
1108
1109         for (;;) {
1110                 knode = klist_next(&iter->ki);
1111                 if (!knode)
1112                         return NULL;
1113                 dev = container_of(knode, struct device_private, knode_bus)->device;
1114                 if (!iter->type || iter->type == dev->type)
1115                         return dev;
1116         }
1117 }
1118 EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1119
1120 /**
1121  * subsys_dev_iter_exit - finish iteration
1122  * @iter: subsys iterator to finish
1123  *
1124  * Finish an iteration.  Always call this function after iteration is
1125  * complete whether the iteration ran till the end or not.
1126  */
1127 void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1128 {
1129         klist_iter_exit(&iter->ki);
1130 }
1131 EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1132
1133 int subsys_interface_register(struct subsys_interface *sif)
1134 {
1135         struct bus_type *subsys;
1136         struct subsys_dev_iter iter;
1137         struct device *dev;
1138
1139         if (!sif || !sif->subsys)
1140                 return -ENODEV;
1141
1142         subsys = bus_get(sif->subsys);
1143         if (!subsys)
1144                 return -EINVAL;
1145
1146         mutex_lock(&subsys->p->mutex);
1147         list_add_tail(&sif->node, &subsys->p->interfaces);
1148         if (sif->add_dev) {
1149                 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1150                 while ((dev = subsys_dev_iter_next(&iter)))
1151                         sif->add_dev(dev, sif);
1152                 subsys_dev_iter_exit(&iter);
1153         }
1154         mutex_unlock(&subsys->p->mutex);
1155
1156         return 0;
1157 }
1158 EXPORT_SYMBOL_GPL(subsys_interface_register);
1159
1160 void subsys_interface_unregister(struct subsys_interface *sif)
1161 {
1162         struct bus_type *subsys;
1163         struct subsys_dev_iter iter;
1164         struct device *dev;
1165
1166         if (!sif || !sif->subsys)
1167                 return;
1168
1169         subsys = sif->subsys;
1170
1171         mutex_lock(&subsys->p->mutex);
1172         list_del_init(&sif->node);
1173         if (sif->remove_dev) {
1174                 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1175                 while ((dev = subsys_dev_iter_next(&iter)))
1176                         sif->remove_dev(dev, sif);
1177                 subsys_dev_iter_exit(&iter);
1178         }
1179         mutex_unlock(&subsys->p->mutex);
1180
1181         bus_put(subsys);
1182 }
1183 EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1184
1185 static void system_root_device_release(struct device *dev)
1186 {
1187         kfree(dev);
1188 }
1189
1190 static int subsys_register(struct bus_type *subsys,
1191                            const struct attribute_group **groups,
1192                            struct kobject *parent_of_root)
1193 {
1194         struct device *dev;
1195         int err;
1196
1197         err = bus_register(subsys);
1198         if (err < 0)
1199                 return err;
1200
1201         dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1202         if (!dev) {
1203                 err = -ENOMEM;
1204                 goto err_dev;
1205         }
1206
1207         err = dev_set_name(dev, "%s", subsys->name);
1208         if (err < 0)
1209                 goto err_name;
1210
1211         dev->kobj.parent = parent_of_root;
1212         dev->groups = groups;
1213         dev->release = system_root_device_release;
1214
1215         err = device_register(dev);
1216         if (err < 0)
1217                 goto err_dev_reg;
1218
1219         subsys->dev_root = dev;
1220         return 0;
1221
1222 err_dev_reg:
1223         put_device(dev);
1224         dev = NULL;
1225 err_name:
1226         kfree(dev);
1227 err_dev:
1228         bus_unregister(subsys);
1229         return err;
1230 }
1231
1232 /**
1233  * subsys_system_register - register a subsystem at /sys/devices/system/
1234  * @subsys: system subsystem
1235  * @groups: default attributes for the root device
1236  *
1237  * All 'system' subsystems have a /sys/devices/system/<name> root device
1238  * with the name of the subsystem. The root device can carry subsystem-
1239  * wide attributes. All registered devices are below this single root
1240  * device and are named after the subsystem with a simple enumeration
1241  * number appended. The registered devices are not explicitly named;
1242  * only 'id' in the device needs to be set.
1243  *
1244  * Do not use this interface for anything new, it exists for compatibility
1245  * with bad ideas only. New subsystems should use plain subsystems; and
1246  * add the subsystem-wide attributes should be added to the subsystem
1247  * directory itself and not some create fake root-device placed in
1248  * /sys/devices/system/<name>.
1249  */
1250 int subsys_system_register(struct bus_type *subsys,
1251                            const struct attribute_group **groups)
1252 {
1253         return subsys_register(subsys, groups, &system_kset->kobj);
1254 }
1255 EXPORT_SYMBOL_GPL(subsys_system_register);
1256
1257 /**
1258  * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1259  * @subsys: virtual subsystem
1260  * @groups: default attributes for the root device
1261  *
1262  * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1263  * with the name of the subystem.  The root device can carry subsystem-wide
1264  * attributes.  All registered devices are below this single root device.
1265  * There's no restriction on device naming.  This is for kernel software
1266  * constructs which need sysfs interface.
1267  */
1268 int subsys_virtual_register(struct bus_type *subsys,
1269                             const struct attribute_group **groups)
1270 {
1271         struct kobject *virtual_dir;
1272
1273         virtual_dir = virtual_device_parent(NULL);
1274         if (!virtual_dir)
1275                 return -ENOMEM;
1276
1277         return subsys_register(subsys, groups, virtual_dir);
1278 }
1279 EXPORT_SYMBOL_GPL(subsys_virtual_register);
1280
1281 int __init buses_init(void)
1282 {
1283         bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1284         if (!bus_kset)
1285                 return -ENOMEM;
1286
1287         system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1288         if (!system_kset)
1289                 return -ENOMEM;
1290
1291         return 0;
1292 }