00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <linux/device.h>
00014 #include <linux/err.h>
00015 #include <linux/init.h>
00016 #include <linux/module.h>
00017 #include <linux/slab.h>
00018 #include <linux/string.h>
00019 #include <linux/kdev_t.h>
00020 #include <linux/notifier.h>
00021 #include <linux/genhd.h>
00022 #include <linux/kallsyms.h>
00023 #include <linux/semaphore.h>
00024 #include <linux/mutex.h>
00025
00026 #include "base.h"
00027 #include "power/power.h"
00028
00029 int (*platform_notify)(struct device *dev) = NULL;
00030 int (*platform_notify_remove)(struct device *dev) = NULL;
00031 static struct kobject *dev_kobj;
00032 struct kobject *sysfs_dev_char_kobj;
00033 struct kobject *sysfs_dev_block_kobj;
00034
00035 #ifdef DDE_LINUX
00036 #include "local.h"
00037 #endif
00038
00039 #if defined(CONFIG_BLOCK) && !defined(DDE_LINUX)
00040 static inline int device_is_not_partition(struct device *dev)
00041 {
00042 return !(dev->type == &part_type);
00043 }
00044 #else
00045 static inline int device_is_not_partition(struct device *dev)
00046 {
00047 return 1;
00048 }
00049 #endif
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 const char *dev_driver_string(const struct device *dev)
00061 {
00062 return dev->driver ? dev->driver->name :
00063 (dev->bus ? dev->bus->name :
00064 (dev->class ? dev->class->name : ""));
00065 }
00066 EXPORT_SYMBOL(dev_driver_string);
00067
00068 #define to_dev(obj) container_of(obj, struct device, kobj)
00069 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
00070
00071 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
00072 char *buf)
00073 {
00074 struct device_attribute *dev_attr = to_dev_attr(attr);
00075 struct device *dev = to_dev(kobj);
00076 ssize_t ret = -EIO;
00077
00078 if (dev_attr->show)
00079 ret = dev_attr->show(dev, dev_attr, buf);
00080 if (ret >= (ssize_t)PAGE_SIZE) {
00081 print_symbol("dev_attr_show: %s returned bad count\n",
00082 (unsigned long)dev_attr->show);
00083 }
00084 return ret;
00085 }
00086
00087 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
00088 const char *buf, size_t count)
00089 {
00090 struct device_attribute *dev_attr = to_dev_attr(attr);
00091 struct device *dev = to_dev(kobj);
00092 ssize_t ret = -EIO;
00093
00094 if (dev_attr->store)
00095 ret = dev_attr->store(dev, dev_attr, buf, count);
00096 return ret;
00097 }
00098
00099 static struct sysfs_ops dev_sysfs_ops = {
00100 .show = dev_attr_show,
00101 .store = dev_attr_store,
00102 };
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 static void device_release(struct kobject *kobj)
00114 {
00115 struct device *dev = to_dev(kobj);
00116
00117 if (dev->release)
00118 dev->release(dev);
00119 else if (dev->type && dev->type->release)
00120 dev->type->release(dev);
00121 else if (dev->class && dev->class->dev_release)
00122 dev->class->dev_release(dev);
00123 else
00124 WARN(1, KERN_ERR "Device '%s' does not have a release() "
00125 "function, it is broken and must be fixed.\n",
00126 dev_name(dev));
00127 }
00128
00129 static struct kobj_type device_ktype = {
00130 .release = device_release,
00131 .sysfs_ops = &dev_sysfs_ops,
00132 };
00133
00134
00135 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
00136 {
00137 struct kobj_type *ktype = get_ktype(kobj);
00138
00139 if (ktype == &device_ktype) {
00140 struct device *dev = to_dev(kobj);
00141 if (dev->uevent_suppress)
00142 return 0;
00143 if (dev->bus)
00144 return 1;
00145 if (dev->class)
00146 return 1;
00147 }
00148 return 0;
00149 }
00150
00151 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
00152 {
00153 struct device *dev = to_dev(kobj);
00154
00155 if (dev->bus)
00156 return dev->bus->name;
00157 if (dev->class)
00158 return dev->class->name;
00159 return NULL;
00160 }
00161
00162 static int dev_uevent(struct kset *kset, struct kobject *kobj,
00163 struct kobj_uevent_env *env)
00164 {
00165 struct device *dev = to_dev(kobj);
00166 int retval = 0;
00167
00168 #ifndef DDE_LINUX
00169
00170 if (MAJOR(dev->devt)) {
00171 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
00172 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
00173 }
00174
00175 if (dev->type && dev->type->name)
00176 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
00177
00178 if (dev->driver)
00179 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
00180
00181 #ifdef CONFIG_SYSFS_DEPRECATED
00182 if (dev->class) {
00183 struct device *parent = dev->parent;
00184
00185
00186 while (parent && !parent->bus)
00187 parent = parent->parent;
00188 if (parent && parent->bus) {
00189 const char *path;
00190
00191 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
00192 if (path) {
00193 add_uevent_var(env, "PHYSDEVPATH=%s", path);
00194 kfree(path);
00195 }
00196
00197 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
00198
00199 if (parent->driver)
00200 add_uevent_var(env, "PHYSDEVDRIVER=%s",
00201 parent->driver->name);
00202 }
00203 } else if (dev->bus) {
00204 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
00205
00206 if (dev->driver)
00207 add_uevent_var(env, "PHYSDEVDRIVER=%s",
00208 dev->driver->name);
00209 }
00210 #endif
00211
00212
00213 if (dev->bus && dev->bus->uevent) {
00214 retval = dev->bus->uevent(dev, env);
00215 if (retval)
00216 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
00217 dev_name(dev), __func__, retval);
00218 }
00219
00220
00221 if (dev->class && dev->class->dev_uevent) {
00222 retval = dev->class->dev_uevent(dev, env);
00223 if (retval)
00224 pr_debug("device: '%s': %s: class uevent() "
00225 "returned %d\n", dev_name(dev),
00226 __func__, retval);
00227 }
00228
00229
00230 if (dev->type && dev->type->uevent) {
00231 retval = dev->type->uevent(dev, env);
00232 if (retval)
00233 pr_debug("device: '%s': %s: dev_type uevent() "
00234 "returned %d\n", dev_name(dev),
00235 __func__, retval);
00236 }
00237 #endif
00238
00239 return retval;
00240 }
00241
00242 static struct kset_uevent_ops device_uevent_ops = {
00243 .filter = dev_uevent_filter,
00244 .name = dev_uevent_name,
00245 .uevent = dev_uevent,
00246 };
00247
00248 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
00249 char *buf)
00250 {
00251 struct kobject *top_kobj;
00252 struct kset *kset;
00253 struct kobj_uevent_env *env = NULL;
00254 int i;
00255 size_t count = 0;
00256 int retval;
00257
00258
00259 top_kobj = &dev->kobj;
00260 while (!top_kobj->kset && top_kobj->parent)
00261 top_kobj = top_kobj->parent;
00262 if (!top_kobj->kset)
00263 goto out;
00264
00265 kset = top_kobj->kset;
00266 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
00267 goto out;
00268
00269
00270 if (kset->uevent_ops && kset->uevent_ops->filter)
00271 if (!kset->uevent_ops->filter(kset, &dev->kobj))
00272 goto out;
00273
00274 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
00275 if (!env)
00276 return -ENOMEM;
00277
00278
00279 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
00280 if (retval)
00281 goto out;
00282
00283
00284 for (i = 0; i < env->envp_idx; i++)
00285 count += sprintf(&buf[count], "%s\n", env->envp[i]);
00286 out:
00287 kfree(env);
00288 return count;
00289 }
00290
00291 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
00292 const char *buf, size_t count)
00293 {
00294 enum kobject_action action;
00295
00296 if (kobject_action_type(buf, count, &action) == 0) {
00297 kobject_uevent(&dev->kobj, action);
00298 goto out;
00299 }
00300
00301 dev_err(dev, "uevent: unsupported action-string; this will "
00302 "be ignored in a future kernel version\n");
00303 kobject_uevent(&dev->kobj, KOBJ_ADD);
00304 out:
00305 return count;
00306 }
00307
00308 static struct device_attribute uevent_attr =
00309 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
00310
00311 static int device_add_attributes(struct device *dev,
00312 struct device_attribute *attrs)
00313 {
00314 int error = 0;
00315 int i;
00316
00317 if (attrs) {
00318 for (i = 0; attr_name(attrs[i]); i++) {
00319 error = device_create_file(dev, &attrs[i]);
00320 if (error)
00321 break;
00322 }
00323 if (error)
00324 while (--i >= 0)
00325 device_remove_file(dev, &attrs[i]);
00326 }
00327 return error;
00328 }
00329
00330 static void device_remove_attributes(struct device *dev,
00331 struct device_attribute *attrs)
00332 {
00333 int i;
00334
00335 if (attrs)
00336 for (i = 0; attr_name(attrs[i]); i++)
00337 device_remove_file(dev, &attrs[i]);
00338 }
00339
00340 static int device_add_groups(struct device *dev,
00341 struct attribute_group **groups)
00342 {
00343 int error = 0;
00344 int i;
00345
00346 if (groups) {
00347 for (i = 0; groups[i]; i++) {
00348 error = sysfs_create_group(&dev->kobj, groups[i]);
00349 if (error) {
00350 while (--i >= 0)
00351 sysfs_remove_group(&dev->kobj,
00352 groups[i]);
00353 break;
00354 }
00355 }
00356 }
00357 return error;
00358 }
00359
00360 static void device_remove_groups(struct device *dev,
00361 struct attribute_group **groups)
00362 {
00363 int i;
00364
00365 if (groups)
00366 for (i = 0; groups[i]; i++)
00367 sysfs_remove_group(&dev->kobj, groups[i]);
00368 }
00369
00370 static int device_add_attrs(struct device *dev)
00371 {
00372 struct class *class = dev->class;
00373 struct device_type *type = dev->type;
00374 int error;
00375
00376 if (class) {
00377 error = device_add_attributes(dev, class->dev_attrs);
00378 if (error)
00379 return error;
00380 }
00381
00382 if (type) {
00383 error = device_add_groups(dev, type->groups);
00384 if (error)
00385 goto err_remove_class_attrs;
00386 }
00387
00388 error = device_add_groups(dev, dev->groups);
00389 if (error)
00390 goto err_remove_type_groups;
00391
00392 return 0;
00393
00394 err_remove_type_groups:
00395 if (type)
00396 device_remove_groups(dev, type->groups);
00397 err_remove_class_attrs:
00398 if (class)
00399 device_remove_attributes(dev, class->dev_attrs);
00400
00401 return error;
00402 }
00403
00404 static void device_remove_attrs(struct device *dev)
00405 {
00406 struct class *class = dev->class;
00407 struct device_type *type = dev->type;
00408
00409 device_remove_groups(dev, dev->groups);
00410
00411 if (type)
00412 device_remove_groups(dev, type->groups);
00413
00414 if (class)
00415 device_remove_attributes(dev, class->dev_attrs);
00416 }
00417
00418
00419 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
00420 char *buf)
00421 {
00422 return print_dev_t(buf, dev->devt);
00423 }
00424
00425 static struct device_attribute devt_attr =
00426 __ATTR(dev, S_IRUGO, show_dev, NULL);
00427
00428
00429 struct kset *devices_kset;
00430
00431
00432
00433
00434
00435
00436 int device_create_file(struct device *dev, struct device_attribute *attr)
00437 {
00438 int error = 0;
00439 if (dev)
00440 error = sysfs_create_file(&dev->kobj, &attr->attr);
00441 return error;
00442 }
00443
00444
00445
00446
00447
00448
00449 void device_remove_file(struct device *dev, struct device_attribute *attr)
00450 {
00451 if (dev)
00452 sysfs_remove_file(&dev->kobj, &attr->attr);
00453 }
00454
00455
00456
00457
00458
00459
00460 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
00461 {
00462 int error = -EINVAL;
00463 if (dev)
00464 error = sysfs_create_bin_file(&dev->kobj, attr);
00465 return error;
00466 }
00467 EXPORT_SYMBOL_GPL(device_create_bin_file);
00468
00469
00470
00471
00472
00473
00474 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
00475 {
00476 if (dev)
00477 sysfs_remove_bin_file(&dev->kobj, attr);
00478 }
00479 EXPORT_SYMBOL_GPL(device_remove_bin_file);
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506 int device_schedule_callback_owner(struct device *dev,
00507 void (*func)(struct device *), struct module *owner)
00508 {
00509 return sysfs_schedule_callback(&dev->kobj,
00510 (void (*)(void *)) func, dev, owner);
00511 }
00512 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
00513
00514 static void klist_children_get(struct klist_node *n)
00515 {
00516 struct device *dev = container_of(n, struct device, knode_parent);
00517
00518 get_device(dev);
00519 }
00520
00521 static void klist_children_put(struct klist_node *n)
00522 {
00523 struct device *dev = container_of(n, struct device, knode_parent);
00524
00525 put_device(dev);
00526 }
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 void device_initialize(struct device *dev)
00544 {
00545 dev->kobj.kset = devices_kset;
00546 kobject_init(&dev->kobj, &device_ktype);
00547 klist_init(&dev->klist_children, klist_children_get,
00548 klist_children_put);
00549 INIT_LIST_HEAD(&dev->dma_pools);
00550 init_MUTEX(&dev->sem);
00551 spin_lock_init(&dev->devres_lock);
00552 INIT_LIST_HEAD(&dev->devres_head);
00553 device_init_wakeup(dev, 0);
00554 device_pm_init(dev);
00555 set_dev_node(dev, -1);
00556 }
00557
00558 #ifdef CONFIG_SYSFS_DEPRECATED
00559 static struct kobject *get_device_parent(struct device *dev,
00560 struct device *parent)
00561 {
00562
00563 if (dev->class && (!parent || parent->class != dev->class))
00564 return &dev->class->p->class_subsys.kobj;
00565
00566 else if (parent)
00567 return &parent->kobj;
00568
00569 return NULL;
00570 }
00571
00572 static inline void cleanup_device_parent(struct device *dev) {}
00573 static inline void cleanup_glue_dir(struct device *dev,
00574 struct kobject *glue_dir) {}
00575 #else
00576 static struct kobject *virtual_device_parent(struct device *dev)
00577 {
00578 static struct kobject *virtual_dir = NULL;
00579
00580 if (!virtual_dir)
00581 virtual_dir = kobject_create_and_add("virtual",
00582 &devices_kset->kobj);
00583
00584 return virtual_dir;
00585 }
00586
00587 static struct kobject *get_device_parent(struct device *dev,
00588 struct device *parent)
00589 {
00590 int retval;
00591
00592 if (dev->class) {
00593 struct kobject *kobj = NULL;
00594 struct kobject *parent_kobj;
00595 struct kobject *k;
00596
00597
00598
00599
00600
00601
00602 if (parent == NULL)
00603 parent_kobj = virtual_device_parent(dev);
00604 else if (parent->class)
00605 return &parent->kobj;
00606 else
00607 parent_kobj = &parent->kobj;
00608
00609
00610 spin_lock(&dev->class->p->class_dirs.list_lock);
00611 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
00612 if (k->parent == parent_kobj) {
00613 kobj = kobject_get(k);
00614 break;
00615 }
00616 spin_unlock(&dev->class->p->class_dirs.list_lock);
00617 if (kobj)
00618 return kobj;
00619
00620
00621 k = kobject_create();
00622 if (!k)
00623 return NULL;
00624 k->kset = &dev->class->p->class_dirs;
00625 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
00626 if (retval < 0) {
00627 kobject_put(k);
00628 return NULL;
00629 }
00630
00631 return k;
00632 }
00633
00634 if (parent)
00635 return &parent->kobj;
00636 return NULL;
00637 }
00638
00639 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
00640 {
00641
00642 if (!glue_dir || !dev->class ||
00643 glue_dir->kset != &dev->class->p->class_dirs)
00644 return;
00645
00646 kobject_put(glue_dir);
00647 }
00648
00649 static void cleanup_device_parent(struct device *dev)
00650 {
00651 cleanup_glue_dir(dev, dev->kobj.parent);
00652 }
00653 #endif
00654
00655 static void setup_parent(struct device *dev, struct device *parent)
00656 {
00657 struct kobject *kobj;
00658 kobj = get_device_parent(dev, parent);
00659 if (kobj)
00660 dev->kobj.parent = kobj;
00661 }
00662
00663 static int device_add_class_symlinks(struct device *dev)
00664 {
00665 int error;
00666
00667 if (!dev->class)
00668 return 0;
00669
00670 error = sysfs_create_link(&dev->kobj,
00671 &dev->class->p->class_subsys.kobj,
00672 "subsystem");
00673 if (error)
00674 goto out;
00675
00676 #ifdef CONFIG_SYSFS_DEPRECATED
00677
00678 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
00679 device_is_not_partition(dev)) {
00680 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
00681 &dev->kobj, dev_name(dev));
00682 if (error)
00683 goto out_subsys;
00684 }
00685
00686 if (dev->parent && device_is_not_partition(dev)) {
00687 struct device *parent = dev->parent;
00688 char *class_name;
00689
00690
00691
00692
00693
00694 while (parent->class && !parent->bus && parent->parent)
00695 parent = parent->parent;
00696
00697 error = sysfs_create_link(&dev->kobj,
00698 &parent->kobj,
00699 "device");
00700 if (error)
00701 goto out_busid;
00702
00703 class_name = make_class_name(dev->class->name,
00704 &dev->kobj);
00705 if (class_name)
00706 error = sysfs_create_link(&dev->parent->kobj,
00707 &dev->kobj, class_name);
00708 kfree(class_name);
00709 if (error)
00710 goto out_device;
00711 }
00712 return 0;
00713
00714 out_device:
00715 if (dev->parent && device_is_not_partition(dev))
00716 sysfs_remove_link(&dev->kobj, "device");
00717 out_busid
00718 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
00719 device_is_not_partition(dev))
00720 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
00721 dev_name(dev));
00722 #else
00723
00724 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
00725 &dev->kobj, dev_name(dev));
00726 if (error)
00727 goto out_subsys;
00728
00729 if (dev->parent && device_is_not_partition(dev)) {
00730 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
00731 "device");
00732 if (error)
00733 goto out_busid;
00734 }
00735 return 0;
00736
00737 out_busid:
00738 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
00739 #endif
00740
00741 out_subsys:
00742 sysfs_remove_link(&dev->kobj, "subsystem");
00743 out:
00744 return error;
00745 }
00746
00747 static void device_remove_class_symlinks(struct device *dev)
00748 {
00749 if (!dev->class)
00750 return;
00751
00752 #ifdef CONFIG_SYSFS_DEPRECATED
00753 if (dev->parent && device_is_not_partition(dev)) {
00754 char *class_name;
00755
00756 class_name = make_class_name(dev->class->name, &dev->kobj);
00757 if (class_name) {
00758 sysfs_remove_link(&dev->parent->kobj, class_name);
00759 kfree(class_name);
00760 }
00761 sysfs_remove_link(&dev->kobj, "device");
00762 }
00763
00764 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
00765 device_is_not_partition(dev))
00766 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
00767 dev_name(dev));
00768 #else
00769 if (dev->parent && device_is_not_partition(dev))
00770 sysfs_remove_link(&dev->kobj, "device");
00771
00772 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
00773 #endif
00774
00775 sysfs_remove_link(&dev->kobj, "subsystem");
00776 }
00777
00778
00779
00780
00781
00782
00783 int dev_set_name(struct device *dev, const char *fmt, ...)
00784 {
00785 va_list vargs;
00786 char *s;
00787
00788 va_start(vargs, fmt);
00789 vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
00790 va_end(vargs);
00791
00792
00793 while ((s = strchr(dev->bus_id, '/')))
00794 *s = '!';
00795
00796 return 0;
00797 }
00798 EXPORT_SYMBOL_GPL(dev_set_name);
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 static struct kobject *device_to_dev_kobj(struct device *dev)
00812 {
00813 struct kobject *kobj;
00814
00815 if (dev->class)
00816 kobj = dev->class->dev_kobj;
00817 else
00818 kobj = sysfs_dev_char_kobj;
00819
00820 return kobj;
00821 }
00822
00823 static int device_create_sys_dev_entry(struct device *dev)
00824 {
00825 struct kobject *kobj = device_to_dev_kobj(dev);
00826 int error = 0;
00827 char devt_str[15];
00828
00829 if (kobj) {
00830 format_dev_t(devt_str, dev->devt);
00831 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
00832 }
00833
00834 return error;
00835 }
00836
00837 static void device_remove_sys_dev_entry(struct device *dev)
00838 {
00839 struct kobject *kobj = device_to_dev_kobj(dev);
00840 char devt_str[15];
00841
00842 if (kobj) {
00843 format_dev_t(devt_str, dev->devt);
00844 sysfs_remove_link(kobj, devt_str);
00845 }
00846 }
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 int device_add(struct device *dev)
00864 {
00865 struct device *parent = NULL;
00866 struct class_interface *class_intf;
00867 int error = -EINVAL;
00868
00869 dev = get_device(dev);
00870 if (!dev)
00871 goto done;
00872
00873
00874
00875 if (dev->init_name)
00876 dev_set_name(dev, "%s", dev->init_name);
00877
00878 if (!strlen(dev->bus_id))
00879 goto done;
00880
00881 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
00882
00883 parent = get_device(dev->parent);
00884 setup_parent(dev, parent);
00885
00886
00887 if (parent)
00888 set_dev_node(dev, dev_to_node(parent));
00889
00890
00891 error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
00892 if (error)
00893 goto Error;
00894
00895
00896 if (platform_notify)
00897 platform_notify(dev);
00898
00899 error = device_create_file(dev, &uevent_attr);
00900 if (error)
00901 goto attrError;
00902
00903 if (MAJOR(dev->devt)) {
00904 error = device_create_file(dev, &devt_attr);
00905 if (error)
00906 goto ueventattrError;
00907
00908 error = device_create_sys_dev_entry(dev);
00909 if (error)
00910 goto devtattrError;
00911 }
00912
00913 error = device_add_class_symlinks(dev);
00914 if (error)
00915 goto SymlinkError;
00916 error = device_add_attrs(dev);
00917 if (error)
00918 goto AttrsError;
00919 error = bus_add_device(dev);
00920 if (error)
00921 goto BusError;
00922 error = dpm_sysfs_add(dev);
00923 if (error)
00924 goto DPMError;
00925 device_pm_add(dev);
00926
00927
00928
00929
00930 if (dev->bus)
00931 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
00932 BUS_NOTIFY_ADD_DEVICE, dev);
00933
00934 kobject_uevent(&dev->kobj, KOBJ_ADD);
00935 bus_attach_device(dev);
00936 if (parent)
00937 klist_add_tail(&dev->knode_parent, &parent->klist_children);
00938
00939 if (dev->class) {
00940 mutex_lock(&dev->class->p->class_mutex);
00941
00942 klist_add_tail(&dev->knode_class,
00943 &dev->class->p->class_devices);
00944
00945
00946 list_for_each_entry(class_intf,
00947 &dev->class->p->class_interfaces, node)
00948 if (class_intf->add_dev)
00949 class_intf->add_dev(dev, class_intf);
00950 mutex_unlock(&dev->class->p->class_mutex);
00951 }
00952 done:
00953 put_device(dev);
00954 return error;
00955 DPMError:
00956 bus_remove_device(dev);
00957 BusError:
00958 device_remove_attrs(dev);
00959 AttrsError:
00960 device_remove_class_symlinks(dev);
00961 SymlinkError:
00962 if (MAJOR(dev->devt))
00963 device_remove_sys_dev_entry(dev);
00964 devtattrError:
00965 if (MAJOR(dev->devt))
00966 device_remove_file(dev, &devt_attr);
00967 ueventattrError:
00968 device_remove_file(dev, &uevent_attr);
00969 attrError:
00970 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
00971 kobject_del(&dev->kobj);
00972 Error:
00973 cleanup_device_parent(dev);
00974 if (parent)
00975 put_device(parent);
00976 goto done;
00977 }
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994 int device_register(struct device *dev)
00995 {
00996 device_initialize(dev);
00997 return device_add(dev);
00998 }
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008 struct device *get_device(struct device *dev)
01009 {
01010 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
01011 }
01012
01013
01014
01015
01016
01017 void put_device(struct device *dev)
01018 {
01019
01020 if (dev)
01021 kobject_put(&dev->kobj);
01022 }
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037 void device_del(struct device *dev)
01038 {
01039 struct device *parent = dev->parent;
01040 struct class_interface *class_intf;
01041
01042
01043
01044
01045 if (dev->bus)
01046 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
01047 BUS_NOTIFY_DEL_DEVICE, dev);
01048 device_pm_remove(dev);
01049 dpm_sysfs_remove(dev);
01050 if (parent)
01051 klist_del(&dev->knode_parent);
01052 if (MAJOR(dev->devt)) {
01053 device_remove_sys_dev_entry(dev);
01054 device_remove_file(dev, &devt_attr);
01055 }
01056 if (dev->class) {
01057 device_remove_class_symlinks(dev);
01058
01059 mutex_lock(&dev->class->p->class_mutex);
01060
01061 list_for_each_entry(class_intf,
01062 &dev->class->p->class_interfaces, node)
01063 if (class_intf->remove_dev)
01064 class_intf->remove_dev(dev, class_intf);
01065
01066 klist_del(&dev->knode_class);
01067 mutex_unlock(&dev->class->p->class_mutex);
01068 }
01069 device_remove_file(dev, &uevent_attr);
01070 device_remove_attrs(dev);
01071 bus_remove_device(dev);
01072
01073
01074
01075
01076
01077
01078 devres_release_all(dev);
01079
01080
01081
01082
01083 if (platform_notify_remove)
01084 platform_notify_remove(dev);
01085 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
01086 cleanup_device_parent(dev);
01087 kobject_del(&dev->kobj);
01088 put_device(parent);
01089 }
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102 void device_unregister(struct device *dev)
01103 {
01104 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
01105 device_del(dev);
01106 put_device(dev);
01107 }
01108
01109 static struct device *next_device(struct klist_iter *i)
01110 {
01111 struct klist_node *n = klist_next(i);
01112 return n ? container_of(n, struct device, knode_parent) : NULL;
01113 }
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127 int device_for_each_child(struct device *parent, void *data,
01128 int (*fn)(struct device *dev, void *data))
01129 {
01130 struct klist_iter i;
01131 struct device *child;
01132 int error = 0;
01133
01134 klist_iter_init(&parent->klist_children, &i);
01135 while ((child = next_device(&i)) && !error)
01136 error = fn(child, data);
01137 klist_iter_exit(&i);
01138 return error;
01139 }
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156 struct device *device_find_child(struct device *parent, void *data,
01157 int (*match)(struct device *dev, void *data))
01158 {
01159 struct klist_iter i;
01160 struct device *child;
01161
01162 if (!parent)
01163 return NULL;
01164
01165 klist_iter_init(&parent->klist_children, &i);
01166 while ((child = next_device(&i)))
01167 if (match(child, data) && get_device(child))
01168 break;
01169 klist_iter_exit(&i);
01170 return child;
01171 }
01172
01173 int __init devices_init(void)
01174 {
01175 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
01176 if (!devices_kset)
01177 return -ENOMEM;
01178 dev_kobj = kobject_create_and_add("dev", NULL);
01179 if (!dev_kobj)
01180 goto dev_kobj_err;
01181 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
01182 if (!sysfs_dev_block_kobj)
01183 goto block_kobj_err;
01184 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
01185 if (!sysfs_dev_char_kobj)
01186 goto char_kobj_err;
01187
01188 return 0;
01189
01190 char_kobj_err:
01191 kobject_put(sysfs_dev_block_kobj);
01192 block_kobj_err:
01193 kobject_put(dev_kobj);
01194 dev_kobj_err:
01195 kset_unregister(devices_kset);
01196 return -ENOMEM;
01197 }
01198
01199 EXPORT_SYMBOL_GPL(device_for_each_child);
01200 EXPORT_SYMBOL_GPL(device_find_child);
01201
01202 EXPORT_SYMBOL_GPL(device_initialize);
01203 EXPORT_SYMBOL_GPL(device_add);
01204 EXPORT_SYMBOL_GPL(device_register);
01205
01206 EXPORT_SYMBOL_GPL(device_del);
01207 EXPORT_SYMBOL_GPL(device_unregister);
01208 EXPORT_SYMBOL_GPL(get_device);
01209 EXPORT_SYMBOL_GPL(put_device);
01210
01211 EXPORT_SYMBOL_GPL(device_create_file);
01212 EXPORT_SYMBOL_GPL(device_remove_file);
01213
01214 struct root_device
01215 {
01216 struct device dev;
01217 struct module *owner;
01218 };
01219
01220 #define to_root_device(dev) container_of(dev, struct root_device, dev)
01221
01222 static void root_device_release(struct device *dev)
01223 {
01224 kfree(to_root_device(dev));
01225 }
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247 struct device *__root_device_register(const char *name, struct module *owner)
01248 {
01249 struct root_device *root;
01250 int err = -ENOMEM;
01251
01252 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
01253 if (!root)
01254 return ERR_PTR(err);
01255
01256 err = dev_set_name(&root->dev, name);
01257 if (err) {
01258 kfree(root);
01259 return ERR_PTR(err);
01260 }
01261
01262 root->dev.release = root_device_release;
01263
01264 err = device_register(&root->dev);
01265 if (err) {
01266 put_device(&root->dev);
01267 return ERR_PTR(err);
01268 }
01269
01270 #ifdef CONFIG_MODULE
01271 if (owner) {
01272 struct module_kobject *mk = &owner->mkobj;
01273
01274 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
01275 if (err) {
01276 device_unregister(&root->dev);
01277 return ERR_PTR(err);
01278 }
01279 root->owner = owner;
01280 }
01281 #endif
01282
01283 return &root->dev;
01284 }
01285 EXPORT_SYMBOL_GPL(__root_device_register);
01286
01287
01288
01289
01290
01291
01292
01293
01294 void root_device_unregister(struct device *dev)
01295 {
01296 struct root_device *root = to_root_device(dev);
01297
01298 if (root->owner)
01299 sysfs_remove_link(&root->dev.kobj, "module");
01300
01301 device_unregister(dev);
01302 }
01303 EXPORT_SYMBOL_GPL(root_device_unregister);
01304
01305
01306 static void device_create_release(struct device *dev)
01307 {
01308 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
01309 kfree(dev);
01310 }
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335 struct device *device_create_vargs(struct class *class, struct device *parent,
01336 dev_t devt, void *drvdata, const char *fmt,
01337 va_list args)
01338 {
01339 struct device *dev = NULL;
01340 int retval = -ENODEV;
01341
01342 if (class == NULL || IS_ERR(class))
01343 goto error;
01344
01345 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
01346 if (!dev) {
01347 retval = -ENOMEM;
01348 goto error;
01349 }
01350
01351 dev->devt = devt;
01352 dev->class = class;
01353 dev->parent = parent;
01354 dev->release = device_create_release;
01355 dev_set_drvdata(dev, drvdata);
01356
01357 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
01358 retval = device_register(dev);
01359 if (retval)
01360 goto error;
01361
01362 return dev;
01363
01364 error:
01365 put_device(dev);
01366 return ERR_PTR(retval);
01367 }
01368 EXPORT_SYMBOL_GPL(device_create_vargs);
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392 struct device *device_create(struct class *class, struct device *parent,
01393 dev_t devt, void *drvdata, const char *fmt, ...)
01394 {
01395 va_list vargs;
01396 struct device *dev;
01397
01398 va_start(vargs, fmt);
01399 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
01400 va_end(vargs);
01401 return dev;
01402 }
01403 EXPORT_SYMBOL_GPL(device_create);
01404
01405 static int __match_devt(struct device *dev, void *data)
01406 {
01407 dev_t *devt = data;
01408
01409 return dev->devt == *devt;
01410 }
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420 void device_destroy(struct class *class, dev_t devt)
01421 {
01422 struct device *dev;
01423
01424 dev = class_find_device(class, NULL, &devt, __match_devt);
01425 if (dev) {
01426 put_device(dev);
01427 device_unregister(dev);
01428 }
01429 }
01430 EXPORT_SYMBOL_GPL(device_destroy);
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442 int device_rename(struct device *dev, char *new_name)
01443 {
01444 char *old_class_name = NULL;
01445 char *new_class_name = NULL;
01446 char *old_device_name = NULL;
01447 int error;
01448
01449 dev = get_device(dev);
01450 if (!dev)
01451 return -EINVAL;
01452
01453 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
01454 __func__, new_name);
01455
01456 #ifdef CONFIG_SYSFS_DEPRECATED
01457 if ((dev->class) && (dev->parent))
01458 old_class_name = make_class_name(dev->class->name, &dev->kobj);
01459 #endif
01460
01461 old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
01462 if (!old_device_name) {
01463 error = -ENOMEM;
01464 goto out;
01465 }
01466 strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
01467 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
01468
01469 error = kobject_rename(&dev->kobj, new_name);
01470 if (error) {
01471 strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
01472 goto out;
01473 }
01474
01475 #ifdef CONFIG_SYSFS_DEPRECATED
01476 if (old_class_name) {
01477 new_class_name = make_class_name(dev->class->name, &dev->kobj);
01478 if (new_class_name) {
01479 error = sysfs_create_link_nowarn(&dev->parent->kobj,
01480 &dev->kobj,
01481 new_class_name);
01482 if (error)
01483 goto out;
01484 sysfs_remove_link(&dev->parent->kobj, old_class_name);
01485 }
01486 }
01487 #else
01488 if (dev->class) {
01489 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
01490 &dev->kobj, dev_name(dev));
01491 if (error)
01492 goto out;
01493 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
01494 old_device_name);
01495 }
01496 #endif
01497
01498 out:
01499 put_device(dev);
01500
01501 kfree(new_class_name);
01502 kfree(old_class_name);
01503 kfree(old_device_name);
01504
01505 return error;
01506 }
01507 EXPORT_SYMBOL_GPL(device_rename);
01508
01509 static int device_move_class_links(struct device *dev,
01510 struct device *old_parent,
01511 struct device *new_parent)
01512 {
01513 int error = 0;
01514 #ifdef CONFIG_SYSFS_DEPRECATED
01515 char *class_name;
01516
01517 class_name = make_class_name(dev->class->name, &dev->kobj);
01518 if (!class_name) {
01519 error = -ENOMEM;
01520 goto out;
01521 }
01522 if (old_parent) {
01523 sysfs_remove_link(&dev->kobj, "device");
01524 sysfs_remove_link(&old_parent->kobj, class_name);
01525 }
01526 if (new_parent) {
01527 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
01528 "device");
01529 if (error)
01530 goto out;
01531 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
01532 class_name);
01533 if (error)
01534 sysfs_remove_link(&dev->kobj, "device");
01535 } else
01536 error = 0;
01537 out:
01538 kfree(class_name);
01539 return error;
01540 #else
01541 if (old_parent)
01542 sysfs_remove_link(&dev->kobj, "device");
01543 if (new_parent)
01544 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
01545 "device");
01546 return error;
01547 #endif
01548 }
01549
01550
01551
01552
01553
01554
01555 int device_move(struct device *dev, struct device *new_parent)
01556 {
01557 int error;
01558 struct device *old_parent;
01559 struct kobject *new_parent_kobj;
01560
01561 dev = get_device(dev);
01562 if (!dev)
01563 return -EINVAL;
01564
01565 new_parent = get_device(new_parent);
01566 new_parent_kobj = get_device_parent(dev, new_parent);
01567
01568 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
01569 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
01570 error = kobject_move(&dev->kobj, new_parent_kobj);
01571 if (error) {
01572 cleanup_glue_dir(dev, new_parent_kobj);
01573 put_device(new_parent);
01574 goto out;
01575 }
01576 old_parent = dev->parent;
01577 dev->parent = new_parent;
01578 if (old_parent)
01579 klist_remove(&dev->knode_parent);
01580 if (new_parent) {
01581 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
01582 set_dev_node(dev, dev_to_node(new_parent));
01583 }
01584
01585 if (!dev->class)
01586 goto out_put;
01587 error = device_move_class_links(dev, old_parent, new_parent);
01588 if (error) {
01589
01590 device_move_class_links(dev, new_parent, old_parent);
01591 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
01592 if (new_parent)
01593 klist_remove(&dev->knode_parent);
01594 dev->parent = old_parent;
01595 if (old_parent) {
01596 klist_add_tail(&dev->knode_parent,
01597 &old_parent->klist_children);
01598 set_dev_node(dev, dev_to_node(old_parent));
01599 }
01600 }
01601 cleanup_glue_dir(dev, new_parent_kobj);
01602 put_device(new_parent);
01603 goto out;
01604 }
01605 out_put:
01606 put_device(old_parent);
01607 out:
01608 put_device(dev);
01609 return error;
01610 }
01611 EXPORT_SYMBOL_GPL(device_move);
01612
01613
01614
01615
01616 void device_shutdown(void)
01617 {
01618 struct device *dev, *devn;
01619
01620 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
01621 kobj.entry) {
01622 if (dev->bus && dev->bus->shutdown) {
01623 dev_dbg(dev, "shutdown\n");
01624 dev->bus->shutdown(dev);
01625 } else if (dev->driver && dev->driver->shutdown) {
01626 dev_dbg(dev, "shutdown\n");
01627 dev->driver->shutdown(dev);
01628 }
01629 }
01630 kobject_put(sysfs_dev_char_kobj);
01631 kobject_put(sysfs_dev_block_kobj);
01632 kobject_put(dev_kobj);
01633 }