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