0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/async.h>
0012 #include <linux/device/bus.h>
0013 #include <linux/device.h>
0014 #include <linux/module.h>
0015 #include <linux/errno.h>
0016 #include <linux/slab.h>
0017 #include <linux/init.h>
0018 #include <linux/string.h>
0019 #include <linux/mutex.h>
0020 #include <linux/sysfs.h>
0021 #include "base.h"
0022 #include "power/power.h"
0023
0024
0025 static struct kset *system_kset;
0026
0027 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
0028
0029
0030
0031
0032
0033 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
0034
0035 #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
0036 struct driver_attribute driver_attr_##_name = \
0037 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
0038
0039 static int __must_check bus_rescan_devices_helper(struct device *dev,
0040 void *data);
0041
0042 static struct bus_type *bus_get(struct bus_type *bus)
0043 {
0044 if (bus) {
0045 kset_get(&bus->p->subsys);
0046 return bus;
0047 }
0048 return NULL;
0049 }
0050
0051 static void bus_put(struct bus_type *bus)
0052 {
0053 if (bus)
0054 kset_put(&bus->p->subsys);
0055 }
0056
0057 static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
0058 char *buf)
0059 {
0060 struct driver_attribute *drv_attr = to_drv_attr(attr);
0061 struct driver_private *drv_priv = to_driver(kobj);
0062 ssize_t ret = -EIO;
0063
0064 if (drv_attr->show)
0065 ret = drv_attr->show(drv_priv->driver, buf);
0066 return ret;
0067 }
0068
0069 static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
0070 const char *buf, size_t count)
0071 {
0072 struct driver_attribute *drv_attr = to_drv_attr(attr);
0073 struct driver_private *drv_priv = to_driver(kobj);
0074 ssize_t ret = -EIO;
0075
0076 if (drv_attr->store)
0077 ret = drv_attr->store(drv_priv->driver, buf, count);
0078 return ret;
0079 }
0080
0081 static const struct sysfs_ops driver_sysfs_ops = {
0082 .show = drv_attr_show,
0083 .store = drv_attr_store,
0084 };
0085
0086 static void driver_release(struct kobject *kobj)
0087 {
0088 struct driver_private *drv_priv = to_driver(kobj);
0089
0090 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
0091 kfree(drv_priv);
0092 }
0093
0094 static struct kobj_type driver_ktype = {
0095 .sysfs_ops = &driver_sysfs_ops,
0096 .release = driver_release,
0097 };
0098
0099
0100
0101
0102 static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
0103 char *buf)
0104 {
0105 struct bus_attribute *bus_attr = to_bus_attr(attr);
0106 struct subsys_private *subsys_priv = to_subsys_private(kobj);
0107 ssize_t ret = 0;
0108
0109 if (bus_attr->show)
0110 ret = bus_attr->show(subsys_priv->bus, buf);
0111 return ret;
0112 }
0113
0114 static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
0115 const char *buf, size_t count)
0116 {
0117 struct bus_attribute *bus_attr = to_bus_attr(attr);
0118 struct subsys_private *subsys_priv = to_subsys_private(kobj);
0119 ssize_t ret = 0;
0120
0121 if (bus_attr->store)
0122 ret = bus_attr->store(subsys_priv->bus, buf, count);
0123 return ret;
0124 }
0125
0126 static const struct sysfs_ops bus_sysfs_ops = {
0127 .show = bus_attr_show,
0128 .store = bus_attr_store,
0129 };
0130
0131 int bus_create_file(struct bus_type *bus, struct bus_attribute *attr)
0132 {
0133 int error;
0134 if (bus_get(bus)) {
0135 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
0136 bus_put(bus);
0137 } else
0138 error = -EINVAL;
0139 return error;
0140 }
0141 EXPORT_SYMBOL_GPL(bus_create_file);
0142
0143 void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr)
0144 {
0145 if (bus_get(bus)) {
0146 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
0147 bus_put(bus);
0148 }
0149 }
0150 EXPORT_SYMBOL_GPL(bus_remove_file);
0151
0152 static void bus_release(struct kobject *kobj)
0153 {
0154 struct subsys_private *priv = to_subsys_private(kobj);
0155 struct bus_type *bus = priv->bus;
0156
0157 kfree(priv);
0158 bus->p = NULL;
0159 }
0160
0161 static struct kobj_type bus_ktype = {
0162 .sysfs_ops = &bus_sysfs_ops,
0163 .release = bus_release,
0164 };
0165
0166 static int bus_uevent_filter(struct kobject *kobj)
0167 {
0168 const struct kobj_type *ktype = get_ktype(kobj);
0169
0170 if (ktype == &bus_ktype)
0171 return 1;
0172 return 0;
0173 }
0174
0175 static const struct kset_uevent_ops bus_uevent_ops = {
0176 .filter = bus_uevent_filter,
0177 };
0178
0179 static struct kset *bus_kset;
0180
0181
0182 static ssize_t unbind_store(struct device_driver *drv, const char *buf,
0183 size_t count)
0184 {
0185 struct bus_type *bus = bus_get(drv->bus);
0186 struct device *dev;
0187 int err = -ENODEV;
0188
0189 dev = bus_find_device_by_name(bus, NULL, buf);
0190 if (dev && dev->driver == drv) {
0191 device_driver_detach(dev);
0192 err = count;
0193 }
0194 put_device(dev);
0195 bus_put(bus);
0196 return err;
0197 }
0198 static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store);
0199
0200
0201
0202
0203
0204
0205 static ssize_t bind_store(struct device_driver *drv, const char *buf,
0206 size_t count)
0207 {
0208 struct bus_type *bus = bus_get(drv->bus);
0209 struct device *dev;
0210 int err = -ENODEV;
0211
0212 dev = bus_find_device_by_name(bus, NULL, buf);
0213 if (dev && driver_match_device(drv, dev)) {
0214 err = device_driver_attach(drv, dev);
0215 if (!err) {
0216
0217 err = count;
0218 }
0219 }
0220 put_device(dev);
0221 bus_put(bus);
0222 return err;
0223 }
0224 static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store);
0225
0226 static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
0227 {
0228 return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe);
0229 }
0230
0231 static ssize_t drivers_autoprobe_store(struct bus_type *bus,
0232 const char *buf, size_t count)
0233 {
0234 if (buf[0] == '0')
0235 bus->p->drivers_autoprobe = 0;
0236 else
0237 bus->p->drivers_autoprobe = 1;
0238 return count;
0239 }
0240
0241 static ssize_t drivers_probe_store(struct bus_type *bus,
0242 const char *buf, size_t count)
0243 {
0244 struct device *dev;
0245 int err = -EINVAL;
0246
0247 dev = bus_find_device_by_name(bus, NULL, buf);
0248 if (!dev)
0249 return -ENODEV;
0250 if (bus_rescan_devices_helper(dev, NULL) == 0)
0251 err = count;
0252 put_device(dev);
0253 return err;
0254 }
0255
0256 static struct device *next_device(struct klist_iter *i)
0257 {
0258 struct klist_node *n = klist_next(i);
0259 struct device *dev = NULL;
0260 struct device_private *dev_prv;
0261
0262 if (n) {
0263 dev_prv = to_device_private_bus(n);
0264 dev = dev_prv->device;
0265 }
0266 return dev;
0267 }
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 int bus_for_each_dev(struct bus_type *bus, struct device *start,
0289 void *data, int (*fn)(struct device *, void *))
0290 {
0291 struct klist_iter i;
0292 struct device *dev;
0293 int error = 0;
0294
0295 if (!bus || !bus->p)
0296 return -EINVAL;
0297
0298 klist_iter_init_node(&bus->p->klist_devices, &i,
0299 (start ? &start->p->knode_bus : NULL));
0300 while (!error && (dev = next_device(&i)))
0301 error = fn(dev, data);
0302 klist_iter_exit(&i);
0303 return error;
0304 }
0305 EXPORT_SYMBOL_GPL(bus_for_each_dev);
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 struct device *bus_find_device(struct bus_type *bus,
0323 struct device *start, const void *data,
0324 int (*match)(struct device *dev, const void *data))
0325 {
0326 struct klist_iter i;
0327 struct device *dev;
0328
0329 if (!bus || !bus->p)
0330 return NULL;
0331
0332 klist_iter_init_node(&bus->p->klist_devices, &i,
0333 (start ? &start->p->knode_bus : NULL));
0334 while ((dev = next_device(&i)))
0335 if (match(dev, data) && get_device(dev))
0336 break;
0337 klist_iter_exit(&i);
0338 return dev;
0339 }
0340 EXPORT_SYMBOL_GPL(bus_find_device);
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352 struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id,
0353 struct device *hint)
0354 {
0355 struct klist_iter i;
0356 struct device *dev;
0357
0358 if (!subsys)
0359 return NULL;
0360
0361 if (hint) {
0362 klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus);
0363 dev = next_device(&i);
0364 if (dev && dev->id == id && get_device(dev)) {
0365 klist_iter_exit(&i);
0366 return dev;
0367 }
0368 klist_iter_exit(&i);
0369 }
0370
0371 klist_iter_init_node(&subsys->p->klist_devices, &i, NULL);
0372 while ((dev = next_device(&i))) {
0373 if (dev->id == id && get_device(dev)) {
0374 klist_iter_exit(&i);
0375 return dev;
0376 }
0377 }
0378 klist_iter_exit(&i);
0379 return NULL;
0380 }
0381 EXPORT_SYMBOL_GPL(subsys_find_device_by_id);
0382
0383 static struct device_driver *next_driver(struct klist_iter *i)
0384 {
0385 struct klist_node *n = klist_next(i);
0386 struct driver_private *drv_priv;
0387
0388 if (n) {
0389 drv_priv = container_of(n, struct driver_private, knode_bus);
0390 return drv_priv->driver;
0391 }
0392 return NULL;
0393 }
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
0415 void *data, int (*fn)(struct device_driver *, void *))
0416 {
0417 struct klist_iter i;
0418 struct device_driver *drv;
0419 int error = 0;
0420
0421 if (!bus)
0422 return -EINVAL;
0423
0424 klist_iter_init_node(&bus->p->klist_drivers, &i,
0425 start ? &start->p->knode_bus : NULL);
0426 while ((drv = next_driver(&i)) && !error)
0427 error = fn(drv, data);
0428 klist_iter_exit(&i);
0429 return error;
0430 }
0431 EXPORT_SYMBOL_GPL(bus_for_each_drv);
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441 int bus_add_device(struct device *dev)
0442 {
0443 struct bus_type *bus = bus_get(dev->bus);
0444 int error = 0;
0445
0446 if (bus) {
0447 pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev));
0448 error = device_add_groups(dev, bus->dev_groups);
0449 if (error)
0450 goto out_put;
0451 error = sysfs_create_link(&bus->p->devices_kset->kobj,
0452 &dev->kobj, dev_name(dev));
0453 if (error)
0454 goto out_groups;
0455 error = sysfs_create_link(&dev->kobj,
0456 &dev->bus->p->subsys.kobj, "subsystem");
0457 if (error)
0458 goto out_subsys;
0459 klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);
0460 }
0461 return 0;
0462
0463 out_subsys:
0464 sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
0465 out_groups:
0466 device_remove_groups(dev, bus->dev_groups);
0467 out_put:
0468 bus_put(dev->bus);
0469 return error;
0470 }
0471
0472
0473
0474
0475
0476
0477
0478 void bus_probe_device(struct device *dev)
0479 {
0480 struct bus_type *bus = dev->bus;
0481 struct subsys_interface *sif;
0482
0483 if (!bus)
0484 return;
0485
0486 if (bus->p->drivers_autoprobe)
0487 device_initial_probe(dev);
0488
0489 mutex_lock(&bus->p->mutex);
0490 list_for_each_entry(sif, &bus->p->interfaces, node)
0491 if (sif->add_dev)
0492 sif->add_dev(dev, sif);
0493 mutex_unlock(&bus->p->mutex);
0494 }
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506 void bus_remove_device(struct device *dev)
0507 {
0508 struct bus_type *bus = dev->bus;
0509 struct subsys_interface *sif;
0510
0511 if (!bus)
0512 return;
0513
0514 mutex_lock(&bus->p->mutex);
0515 list_for_each_entry(sif, &bus->p->interfaces, node)
0516 if (sif->remove_dev)
0517 sif->remove_dev(dev, sif);
0518 mutex_unlock(&bus->p->mutex);
0519
0520 sysfs_remove_link(&dev->kobj, "subsystem");
0521 sysfs_remove_link(&dev->bus->p->devices_kset->kobj,
0522 dev_name(dev));
0523 device_remove_groups(dev, dev->bus->dev_groups);
0524 if (klist_node_attached(&dev->p->knode_bus))
0525 klist_del(&dev->p->knode_bus);
0526
0527 pr_debug("bus: '%s': remove device %s\n",
0528 dev->bus->name, dev_name(dev));
0529 device_release_driver(dev);
0530 bus_put(dev->bus);
0531 }
0532
0533 static int __must_check add_bind_files(struct device_driver *drv)
0534 {
0535 int ret;
0536
0537 ret = driver_create_file(drv, &driver_attr_unbind);
0538 if (ret == 0) {
0539 ret = driver_create_file(drv, &driver_attr_bind);
0540 if (ret)
0541 driver_remove_file(drv, &driver_attr_unbind);
0542 }
0543 return ret;
0544 }
0545
0546 static void remove_bind_files(struct device_driver *drv)
0547 {
0548 driver_remove_file(drv, &driver_attr_bind);
0549 driver_remove_file(drv, &driver_attr_unbind);
0550 }
0551
0552 static BUS_ATTR_WO(drivers_probe);
0553 static BUS_ATTR_RW(drivers_autoprobe);
0554
0555 static int add_probe_files(struct bus_type *bus)
0556 {
0557 int retval;
0558
0559 retval = bus_create_file(bus, &bus_attr_drivers_probe);
0560 if (retval)
0561 goto out;
0562
0563 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
0564 if (retval)
0565 bus_remove_file(bus, &bus_attr_drivers_probe);
0566 out:
0567 return retval;
0568 }
0569
0570 static void remove_probe_files(struct bus_type *bus)
0571 {
0572 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
0573 bus_remove_file(bus, &bus_attr_drivers_probe);
0574 }
0575
0576 static ssize_t uevent_store(struct device_driver *drv, const char *buf,
0577 size_t count)
0578 {
0579 int rc;
0580
0581 rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
0582 return rc ? rc : count;
0583 }
0584 static DRIVER_ATTR_WO(uevent);
0585
0586
0587
0588
0589
0590 int bus_add_driver(struct device_driver *drv)
0591 {
0592 struct bus_type *bus;
0593 struct driver_private *priv;
0594 int error = 0;
0595
0596 bus = bus_get(drv->bus);
0597 if (!bus)
0598 return -EINVAL;
0599
0600 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
0601
0602 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0603 if (!priv) {
0604 error = -ENOMEM;
0605 goto out_put_bus;
0606 }
0607 klist_init(&priv->klist_devices, NULL, NULL);
0608 priv->driver = drv;
0609 drv->p = priv;
0610 priv->kobj.kset = bus->p->drivers_kset;
0611 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
0612 "%s", drv->name);
0613 if (error)
0614 goto out_unregister;
0615
0616 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
0617 if (drv->bus->p->drivers_autoprobe) {
0618 error = driver_attach(drv);
0619 if (error)
0620 goto out_del_list;
0621 }
0622 module_add_driver(drv->owner, drv);
0623
0624 error = driver_create_file(drv, &driver_attr_uevent);
0625 if (error) {
0626 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
0627 __func__, drv->name);
0628 }
0629 error = driver_add_groups(drv, bus->drv_groups);
0630 if (error) {
0631
0632 printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
0633 __func__, drv->name);
0634 }
0635
0636 if (!drv->suppress_bind_attrs) {
0637 error = add_bind_files(drv);
0638 if (error) {
0639
0640 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
0641 __func__, drv->name);
0642 }
0643 }
0644
0645 return 0;
0646
0647 out_del_list:
0648 klist_del(&priv->knode_bus);
0649 out_unregister:
0650 kobject_put(&priv->kobj);
0651
0652 drv->p = NULL;
0653 out_put_bus:
0654 bus_put(bus);
0655 return error;
0656 }
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666 void bus_remove_driver(struct device_driver *drv)
0667 {
0668 if (!drv->bus)
0669 return;
0670
0671 if (!drv->suppress_bind_attrs)
0672 remove_bind_files(drv);
0673 driver_remove_groups(drv, drv->bus->drv_groups);
0674 driver_remove_file(drv, &driver_attr_uevent);
0675 klist_remove(&drv->p->knode_bus);
0676 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
0677 driver_detach(drv);
0678 module_remove_driver(drv);
0679 kobject_put(&drv->p->kobj);
0680 bus_put(drv->bus);
0681 }
0682
0683
0684 static int __must_check bus_rescan_devices_helper(struct device *dev,
0685 void *data)
0686 {
0687 int ret = 0;
0688
0689 if (!dev->driver) {
0690 if (dev->parent && dev->bus->need_parent_lock)
0691 device_lock(dev->parent);
0692 ret = device_attach(dev);
0693 if (dev->parent && dev->bus->need_parent_lock)
0694 device_unlock(dev->parent);
0695 }
0696 return ret < 0 ? ret : 0;
0697 }
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707 int bus_rescan_devices(struct bus_type *bus)
0708 {
0709 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
0710 }
0711 EXPORT_SYMBOL_GPL(bus_rescan_devices);
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722 int device_reprobe(struct device *dev)
0723 {
0724 if (dev->driver)
0725 device_driver_detach(dev);
0726 return bus_rescan_devices_helper(dev, NULL);
0727 }
0728 EXPORT_SYMBOL_GPL(device_reprobe);
0729
0730 static int bus_add_groups(struct bus_type *bus,
0731 const struct attribute_group **groups)
0732 {
0733 return sysfs_create_groups(&bus->p->subsys.kobj, groups);
0734 }
0735
0736 static void bus_remove_groups(struct bus_type *bus,
0737 const struct attribute_group **groups)
0738 {
0739 sysfs_remove_groups(&bus->p->subsys.kobj, groups);
0740 }
0741
0742 static void klist_devices_get(struct klist_node *n)
0743 {
0744 struct device_private *dev_prv = to_device_private_bus(n);
0745 struct device *dev = dev_prv->device;
0746
0747 get_device(dev);
0748 }
0749
0750 static void klist_devices_put(struct klist_node *n)
0751 {
0752 struct device_private *dev_prv = to_device_private_bus(n);
0753 struct device *dev = dev_prv->device;
0754
0755 put_device(dev);
0756 }
0757
0758 static ssize_t bus_uevent_store(struct bus_type *bus,
0759 const char *buf, size_t count)
0760 {
0761 int rc;
0762
0763 rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count);
0764 return rc ? rc : count;
0765 }
0766
0767
0768
0769
0770
0771
0772 static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL,
0773 bus_uevent_store);
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783 int bus_register(struct bus_type *bus)
0784 {
0785 int retval;
0786 struct subsys_private *priv;
0787 struct lock_class_key *key = &bus->lock_key;
0788
0789 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
0790 if (!priv)
0791 return -ENOMEM;
0792
0793 priv->bus = bus;
0794 bus->p = priv;
0795
0796 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
0797
0798 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
0799 if (retval)
0800 goto out;
0801
0802 priv->subsys.kobj.kset = bus_kset;
0803 priv->subsys.kobj.ktype = &bus_ktype;
0804 priv->drivers_autoprobe = 1;
0805
0806 retval = kset_register(&priv->subsys);
0807 if (retval)
0808 goto out;
0809
0810 retval = bus_create_file(bus, &bus_attr_uevent);
0811 if (retval)
0812 goto bus_uevent_fail;
0813
0814 priv->devices_kset = kset_create_and_add("devices", NULL,
0815 &priv->subsys.kobj);
0816 if (!priv->devices_kset) {
0817 retval = -ENOMEM;
0818 goto bus_devices_fail;
0819 }
0820
0821 priv->drivers_kset = kset_create_and_add("drivers", NULL,
0822 &priv->subsys.kobj);
0823 if (!priv->drivers_kset) {
0824 retval = -ENOMEM;
0825 goto bus_drivers_fail;
0826 }
0827
0828 INIT_LIST_HEAD(&priv->interfaces);
0829 __mutex_init(&priv->mutex, "subsys mutex", key);
0830 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
0831 klist_init(&priv->klist_drivers, NULL, NULL);
0832
0833 retval = add_probe_files(bus);
0834 if (retval)
0835 goto bus_probe_files_fail;
0836
0837 retval = bus_add_groups(bus, bus->bus_groups);
0838 if (retval)
0839 goto bus_groups_fail;
0840
0841 pr_debug("bus: '%s': registered\n", bus->name);
0842 return 0;
0843
0844 bus_groups_fail:
0845 remove_probe_files(bus);
0846 bus_probe_files_fail:
0847 kset_unregister(bus->p->drivers_kset);
0848 bus_drivers_fail:
0849 kset_unregister(bus->p->devices_kset);
0850 bus_devices_fail:
0851 bus_remove_file(bus, &bus_attr_uevent);
0852 bus_uevent_fail:
0853 kset_unregister(&bus->p->subsys);
0854 out:
0855 kfree(bus->p);
0856 bus->p = NULL;
0857 return retval;
0858 }
0859 EXPORT_SYMBOL_GPL(bus_register);
0860
0861
0862
0863
0864
0865
0866
0867
0868 void bus_unregister(struct bus_type *bus)
0869 {
0870 pr_debug("bus: '%s': unregistering\n", bus->name);
0871 if (bus->dev_root)
0872 device_unregister(bus->dev_root);
0873 bus_remove_groups(bus, bus->bus_groups);
0874 remove_probe_files(bus);
0875 kset_unregister(bus->p->drivers_kset);
0876 kset_unregister(bus->p->devices_kset);
0877 bus_remove_file(bus, &bus_attr_uevent);
0878 kset_unregister(&bus->p->subsys);
0879 }
0880 EXPORT_SYMBOL_GPL(bus_unregister);
0881
0882 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
0883 {
0884 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
0885 }
0886 EXPORT_SYMBOL_GPL(bus_register_notifier);
0887
0888 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
0889 {
0890 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
0891 }
0892 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
0893
0894 struct kset *bus_get_kset(struct bus_type *bus)
0895 {
0896 return &bus->p->subsys;
0897 }
0898 EXPORT_SYMBOL_GPL(bus_get_kset);
0899
0900 struct klist *bus_get_device_klist(struct bus_type *bus)
0901 {
0902 return &bus->p->klist_devices;
0903 }
0904 EXPORT_SYMBOL_GPL(bus_get_device_klist);
0905
0906
0907
0908
0909
0910
0911
0912
0913 static void device_insertion_sort_klist(struct device *a, struct list_head *list,
0914 int (*compare)(const struct device *a,
0915 const struct device *b))
0916 {
0917 struct klist_node *n;
0918 struct device_private *dev_prv;
0919 struct device *b;
0920
0921 list_for_each_entry(n, list, n_node) {
0922 dev_prv = to_device_private_bus(n);
0923 b = dev_prv->device;
0924 if (compare(a, b) <= 0) {
0925 list_move_tail(&a->p->knode_bus.n_node,
0926 &b->p->knode_bus.n_node);
0927 return;
0928 }
0929 }
0930 list_move_tail(&a->p->knode_bus.n_node, list);
0931 }
0932
0933 void bus_sort_breadthfirst(struct bus_type *bus,
0934 int (*compare)(const struct device *a,
0935 const struct device *b))
0936 {
0937 LIST_HEAD(sorted_devices);
0938 struct klist_node *n, *tmp;
0939 struct device_private *dev_prv;
0940 struct device *dev;
0941 struct klist *device_klist;
0942
0943 device_klist = bus_get_device_klist(bus);
0944
0945 spin_lock(&device_klist->k_lock);
0946 list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
0947 dev_prv = to_device_private_bus(n);
0948 dev = dev_prv->device;
0949 device_insertion_sort_klist(dev, &sorted_devices, compare);
0950 }
0951 list_splice(&sorted_devices, &device_klist->k_list);
0952 spin_unlock(&device_klist->k_lock);
0953 }
0954 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
0955
0956
0957
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968 void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
0969 struct device *start, const struct device_type *type)
0970 {
0971 struct klist_node *start_knode = NULL;
0972
0973 if (start)
0974 start_knode = &start->p->knode_bus;
0975 klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
0976 iter->type = type;
0977 }
0978 EXPORT_SYMBOL_GPL(subsys_dev_iter_init);
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
0993 {
0994 struct klist_node *knode;
0995 struct device *dev;
0996
0997 for (;;) {
0998 knode = klist_next(&iter->ki);
0999 if (!knode)
1000 return NULL;
1001 dev = to_device_private_bus(knode)->device;
1002 if (!iter->type || iter->type == dev->type)
1003 return dev;
1004 }
1005 }
1006 EXPORT_SYMBOL_GPL(subsys_dev_iter_next);
1007
1008
1009
1010
1011
1012
1013
1014
1015 void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1016 {
1017 klist_iter_exit(&iter->ki);
1018 }
1019 EXPORT_SYMBOL_GPL(subsys_dev_iter_exit);
1020
1021 int subsys_interface_register(struct subsys_interface *sif)
1022 {
1023 struct bus_type *subsys;
1024 struct subsys_dev_iter iter;
1025 struct device *dev;
1026
1027 if (!sif || !sif->subsys)
1028 return -ENODEV;
1029
1030 subsys = bus_get(sif->subsys);
1031 if (!subsys)
1032 return -EINVAL;
1033
1034 mutex_lock(&subsys->p->mutex);
1035 list_add_tail(&sif->node, &subsys->p->interfaces);
1036 if (sif->add_dev) {
1037 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1038 while ((dev = subsys_dev_iter_next(&iter)))
1039 sif->add_dev(dev, sif);
1040 subsys_dev_iter_exit(&iter);
1041 }
1042 mutex_unlock(&subsys->p->mutex);
1043
1044 return 0;
1045 }
1046 EXPORT_SYMBOL_GPL(subsys_interface_register);
1047
1048 void subsys_interface_unregister(struct subsys_interface *sif)
1049 {
1050 struct bus_type *subsys;
1051 struct subsys_dev_iter iter;
1052 struct device *dev;
1053
1054 if (!sif || !sif->subsys)
1055 return;
1056
1057 subsys = sif->subsys;
1058
1059 mutex_lock(&subsys->p->mutex);
1060 list_del_init(&sif->node);
1061 if (sif->remove_dev) {
1062 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1063 while ((dev = subsys_dev_iter_next(&iter)))
1064 sif->remove_dev(dev, sif);
1065 subsys_dev_iter_exit(&iter);
1066 }
1067 mutex_unlock(&subsys->p->mutex);
1068
1069 bus_put(subsys);
1070 }
1071 EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1072
1073 static void system_root_device_release(struct device *dev)
1074 {
1075 kfree(dev);
1076 }
1077
1078 static int subsys_register(struct bus_type *subsys,
1079 const struct attribute_group **groups,
1080 struct kobject *parent_of_root)
1081 {
1082 struct device *dev;
1083 int err;
1084
1085 err = bus_register(subsys);
1086 if (err < 0)
1087 return err;
1088
1089 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1090 if (!dev) {
1091 err = -ENOMEM;
1092 goto err_dev;
1093 }
1094
1095 err = dev_set_name(dev, "%s", subsys->name);
1096 if (err < 0)
1097 goto err_name;
1098
1099 dev->kobj.parent = parent_of_root;
1100 dev->groups = groups;
1101 dev->release = system_root_device_release;
1102
1103 err = device_register(dev);
1104 if (err < 0)
1105 goto err_dev_reg;
1106
1107 subsys->dev_root = dev;
1108 return 0;
1109
1110 err_dev_reg:
1111 put_device(dev);
1112 dev = NULL;
1113 err_name:
1114 kfree(dev);
1115 err_dev:
1116 bus_unregister(subsys);
1117 return err;
1118 }
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 int subsys_system_register(struct bus_type *subsys,
1139 const struct attribute_group **groups)
1140 {
1141 return subsys_register(subsys, groups, &system_kset->kobj);
1142 }
1143 EXPORT_SYMBOL_GPL(subsys_system_register);
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 int subsys_virtual_register(struct bus_type *subsys,
1157 const struct attribute_group **groups)
1158 {
1159 struct kobject *virtual_dir;
1160
1161 virtual_dir = virtual_device_parent(NULL);
1162 if (!virtual_dir)
1163 return -ENOMEM;
1164
1165 return subsys_register(subsys, groups, virtual_dir);
1166 }
1167 EXPORT_SYMBOL_GPL(subsys_virtual_register);
1168
1169 int __init buses_init(void)
1170 {
1171 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1172 if (!bus_kset)
1173 return -ENOMEM;
1174
1175 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1176 if (!system_kset)
1177 return -ENOMEM;
1178
1179 return 0;
1180 }