Linux 3.12.39
[linux/fpc-iii.git] / drivers / base / core.c
blob944fecd32e9fe97816b5fce7dd67ee0d5abadfab
1 /*
2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/kallsyms.h>
25 #include <linux/mutex.h>
26 #include <linux/async.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29 #include <linux/sysfs.h>
31 #include "base.h"
32 #include "power/power.h"
34 #ifdef CONFIG_SYSFS_DEPRECATED
35 #ifdef CONFIG_SYSFS_DEPRECATED_V2
36 long sysfs_deprecated = 1;
37 #else
38 long sysfs_deprecated = 0;
39 #endif
40 static int __init sysfs_deprecated_setup(char *arg)
42 return kstrtol(arg, 10, &sysfs_deprecated);
44 early_param("sysfs.deprecated", sysfs_deprecated_setup);
45 #endif
47 int (*platform_notify)(struct device *dev) = NULL;
48 int (*platform_notify_remove)(struct device *dev) = NULL;
49 static struct kobject *dev_kobj;
50 struct kobject *sysfs_dev_char_kobj;
51 struct kobject *sysfs_dev_block_kobj;
53 static DEFINE_MUTEX(device_hotplug_lock);
55 void lock_device_hotplug(void)
57 mutex_lock(&device_hotplug_lock);
60 void unlock_device_hotplug(void)
62 mutex_unlock(&device_hotplug_lock);
65 int lock_device_hotplug_sysfs(void)
67 if (mutex_trylock(&device_hotplug_lock))
68 return 0;
70 /* Avoid busy looping (5 ms of sleep should do). */
71 msleep(5);
72 return restart_syscall();
75 #ifdef CONFIG_BLOCK
76 static inline int device_is_not_partition(struct device *dev)
78 return !(dev->type == &part_type);
80 #else
81 static inline int device_is_not_partition(struct device *dev)
83 return 1;
85 #endif
87 /**
88 * dev_driver_string - Return a device's driver name, if at all possible
89 * @dev: struct device to get the name of
91 * Will return the device's driver's name if it is bound to a device. If
92 * the device is not bound to a driver, it will return the name of the bus
93 * it is attached to. If it is not attached to a bus either, an empty
94 * string will be returned.
96 const char *dev_driver_string(const struct device *dev)
98 struct device_driver *drv;
100 /* dev->driver can change to NULL underneath us because of unbinding,
101 * so be careful about accessing it. dev->bus and dev->class should
102 * never change once they are set, so they don't need special care.
104 drv = ACCESS_ONCE(dev->driver);
105 return drv ? drv->name :
106 (dev->bus ? dev->bus->name :
107 (dev->class ? dev->class->name : ""));
109 EXPORT_SYMBOL(dev_driver_string);
111 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
113 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
114 char *buf)
116 struct device_attribute *dev_attr = to_dev_attr(attr);
117 struct device *dev = kobj_to_dev(kobj);
118 ssize_t ret = -EIO;
120 if (dev_attr->show)
121 ret = dev_attr->show(dev, dev_attr, buf);
122 if (ret >= (ssize_t)PAGE_SIZE) {
123 print_symbol("dev_attr_show: %s returned bad count\n",
124 (unsigned long)dev_attr->show);
126 return ret;
129 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
130 const char *buf, size_t count)
132 struct device_attribute *dev_attr = to_dev_attr(attr);
133 struct device *dev = kobj_to_dev(kobj);
134 ssize_t ret = -EIO;
136 if (dev_attr->store)
137 ret = dev_attr->store(dev, dev_attr, buf, count);
138 return ret;
141 static const struct sysfs_ops dev_sysfs_ops = {
142 .show = dev_attr_show,
143 .store = dev_attr_store,
146 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
148 ssize_t device_store_ulong(struct device *dev,
149 struct device_attribute *attr,
150 const char *buf, size_t size)
152 struct dev_ext_attribute *ea = to_ext_attr(attr);
153 char *end;
154 unsigned long new = simple_strtoul(buf, &end, 0);
155 if (end == buf)
156 return -EINVAL;
157 *(unsigned long *)(ea->var) = new;
158 /* Always return full write size even if we didn't consume all */
159 return size;
161 EXPORT_SYMBOL_GPL(device_store_ulong);
163 ssize_t device_show_ulong(struct device *dev,
164 struct device_attribute *attr,
165 char *buf)
167 struct dev_ext_attribute *ea = to_ext_attr(attr);
168 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
170 EXPORT_SYMBOL_GPL(device_show_ulong);
172 ssize_t device_store_int(struct device *dev,
173 struct device_attribute *attr,
174 const char *buf, size_t size)
176 struct dev_ext_attribute *ea = to_ext_attr(attr);
177 char *end;
178 long new = simple_strtol(buf, &end, 0);
179 if (end == buf || new > INT_MAX || new < INT_MIN)
180 return -EINVAL;
181 *(int *)(ea->var) = new;
182 /* Always return full write size even if we didn't consume all */
183 return size;
185 EXPORT_SYMBOL_GPL(device_store_int);
187 ssize_t device_show_int(struct device *dev,
188 struct device_attribute *attr,
189 char *buf)
191 struct dev_ext_attribute *ea = to_ext_attr(attr);
193 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
195 EXPORT_SYMBOL_GPL(device_show_int);
197 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
198 const char *buf, size_t size)
200 struct dev_ext_attribute *ea = to_ext_attr(attr);
202 if (strtobool(buf, ea->var) < 0)
203 return -EINVAL;
205 return size;
207 EXPORT_SYMBOL_GPL(device_store_bool);
209 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
210 char *buf)
212 struct dev_ext_attribute *ea = to_ext_attr(attr);
214 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
216 EXPORT_SYMBOL_GPL(device_show_bool);
219 * device_release - free device structure.
220 * @kobj: device's kobject.
222 * This is called once the reference count for the object
223 * reaches 0. We forward the call to the device's release
224 * method, which should handle actually freeing the structure.
226 static void device_release(struct kobject *kobj)
228 struct device *dev = kobj_to_dev(kobj);
229 struct device_private *p = dev->p;
232 * Some platform devices are driven without driver attached
233 * and managed resources may have been acquired. Make sure
234 * all resources are released.
236 * Drivers still can add resources into device after device
237 * is deleted but alive, so release devres here to avoid
238 * possible memory leak.
240 devres_release_all(dev);
242 if (dev->release)
243 dev->release(dev);
244 else if (dev->type && dev->type->release)
245 dev->type->release(dev);
246 else if (dev->class && dev->class->dev_release)
247 dev->class->dev_release(dev);
248 else
249 WARN(1, KERN_ERR "Device '%s' does not have a release() "
250 "function, it is broken and must be fixed.\n",
251 dev_name(dev));
252 kfree(p);
255 static const void *device_namespace(struct kobject *kobj)
257 struct device *dev = kobj_to_dev(kobj);
258 const void *ns = NULL;
260 if (dev->class && dev->class->ns_type)
261 ns = dev->class->namespace(dev);
263 return ns;
266 static struct kobj_type device_ktype = {
267 .release = device_release,
268 .sysfs_ops = &dev_sysfs_ops,
269 .namespace = device_namespace,
273 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
275 struct kobj_type *ktype = get_ktype(kobj);
277 if (ktype == &device_ktype) {
278 struct device *dev = kobj_to_dev(kobj);
279 if (dev->bus)
280 return 1;
281 if (dev->class)
282 return 1;
284 return 0;
287 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
289 struct device *dev = kobj_to_dev(kobj);
291 if (dev->bus)
292 return dev->bus->name;
293 if (dev->class)
294 return dev->class->name;
295 return NULL;
298 static int dev_uevent(struct kset *kset, struct kobject *kobj,
299 struct kobj_uevent_env *env)
301 struct device *dev = kobj_to_dev(kobj);
302 int retval = 0;
304 /* add device node properties if present */
305 if (MAJOR(dev->devt)) {
306 const char *tmp;
307 const char *name;
308 umode_t mode = 0;
309 kuid_t uid = GLOBAL_ROOT_UID;
310 kgid_t gid = GLOBAL_ROOT_GID;
312 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
313 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
314 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
315 if (name) {
316 add_uevent_var(env, "DEVNAME=%s", name);
317 if (mode)
318 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
319 if (!uid_eq(uid, GLOBAL_ROOT_UID))
320 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
321 if (!gid_eq(gid, GLOBAL_ROOT_GID))
322 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
323 kfree(tmp);
327 if (dev->type && dev->type->name)
328 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
330 if (dev->driver)
331 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
333 /* Add common DT information about the device */
334 of_device_uevent(dev, env);
336 /* have the bus specific function add its stuff */
337 if (dev->bus && dev->bus->uevent) {
338 retval = dev->bus->uevent(dev, env);
339 if (retval)
340 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
341 dev_name(dev), __func__, retval);
344 /* have the class specific function add its stuff */
345 if (dev->class && dev->class->dev_uevent) {
346 retval = dev->class->dev_uevent(dev, env);
347 if (retval)
348 pr_debug("device: '%s': %s: class uevent() "
349 "returned %d\n", dev_name(dev),
350 __func__, retval);
353 /* have the device type specific function add its stuff */
354 if (dev->type && dev->type->uevent) {
355 retval = dev->type->uevent(dev, env);
356 if (retval)
357 pr_debug("device: '%s': %s: dev_type uevent() "
358 "returned %d\n", dev_name(dev),
359 __func__, retval);
362 return retval;
365 static const struct kset_uevent_ops device_uevent_ops = {
366 .filter = dev_uevent_filter,
367 .name = dev_uevent_name,
368 .uevent = dev_uevent,
371 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
372 char *buf)
374 struct kobject *top_kobj;
375 struct kset *kset;
376 struct kobj_uevent_env *env = NULL;
377 int i;
378 size_t count = 0;
379 int retval;
381 /* search the kset, the device belongs to */
382 top_kobj = &dev->kobj;
383 while (!top_kobj->kset && top_kobj->parent)
384 top_kobj = top_kobj->parent;
385 if (!top_kobj->kset)
386 goto out;
388 kset = top_kobj->kset;
389 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
390 goto out;
392 /* respect filter */
393 if (kset->uevent_ops && kset->uevent_ops->filter)
394 if (!kset->uevent_ops->filter(kset, &dev->kobj))
395 goto out;
397 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
398 if (!env)
399 return -ENOMEM;
401 /* let the kset specific function add its keys */
402 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
403 if (retval)
404 goto out;
406 /* copy keys to file */
407 for (i = 0; i < env->envp_idx; i++)
408 count += sprintf(&buf[count], "%s\n", env->envp[i]);
409 out:
410 kfree(env);
411 return count;
414 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
415 const char *buf, size_t count)
417 enum kobject_action action;
419 if (kobject_action_type(buf, count, &action) == 0)
420 kobject_uevent(&dev->kobj, action);
421 else
422 dev_err(dev, "uevent: unknown action-string\n");
423 return count;
425 static DEVICE_ATTR_RW(uevent);
427 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
428 char *buf)
430 bool val;
432 device_lock(dev);
433 val = !dev->offline;
434 device_unlock(dev);
435 return sprintf(buf, "%u\n", val);
438 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
439 const char *buf, size_t count)
441 bool val;
442 int ret;
444 ret = strtobool(buf, &val);
445 if (ret < 0)
446 return ret;
448 ret = lock_device_hotplug_sysfs();
449 if (ret)
450 return ret;
452 ret = val ? device_online(dev) : device_offline(dev);
453 unlock_device_hotplug();
454 return ret < 0 ? ret : count;
456 static DEVICE_ATTR_RW(online);
458 static int device_add_attributes(struct device *dev,
459 struct device_attribute *attrs)
461 int error = 0;
462 int i;
464 if (attrs) {
465 for (i = 0; attrs[i].attr.name; i++) {
466 error = device_create_file(dev, &attrs[i]);
467 if (error)
468 break;
470 if (error)
471 while (--i >= 0)
472 device_remove_file(dev, &attrs[i]);
474 return error;
477 static void device_remove_attributes(struct device *dev,
478 struct device_attribute *attrs)
480 int i;
482 if (attrs)
483 for (i = 0; attrs[i].attr.name; i++)
484 device_remove_file(dev, &attrs[i]);
487 static int device_add_bin_attributes(struct device *dev,
488 struct bin_attribute *attrs)
490 int error = 0;
491 int i;
493 if (attrs) {
494 for (i = 0; attrs[i].attr.name; i++) {
495 error = device_create_bin_file(dev, &attrs[i]);
496 if (error)
497 break;
499 if (error)
500 while (--i >= 0)
501 device_remove_bin_file(dev, &attrs[i]);
503 return error;
506 static void device_remove_bin_attributes(struct device *dev,
507 struct bin_attribute *attrs)
509 int i;
511 if (attrs)
512 for (i = 0; attrs[i].attr.name; i++)
513 device_remove_bin_file(dev, &attrs[i]);
516 int device_add_groups(struct device *dev, const struct attribute_group **groups)
518 return sysfs_create_groups(&dev->kobj, groups);
521 void device_remove_groups(struct device *dev,
522 const struct attribute_group **groups)
524 sysfs_remove_groups(&dev->kobj, groups);
527 static int device_add_attrs(struct device *dev)
529 struct class *class = dev->class;
530 const struct device_type *type = dev->type;
531 int error;
533 if (class) {
534 error = device_add_groups(dev, class->dev_groups);
535 if (error)
536 return error;
537 error = device_add_attributes(dev, class->dev_attrs);
538 if (error)
539 goto err_remove_class_groups;
540 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
541 if (error)
542 goto err_remove_class_attrs;
545 if (type) {
546 error = device_add_groups(dev, type->groups);
547 if (error)
548 goto err_remove_class_bin_attrs;
551 error = device_add_groups(dev, dev->groups);
552 if (error)
553 goto err_remove_type_groups;
555 if (device_supports_offline(dev) && !dev->offline_disabled) {
556 error = device_create_file(dev, &dev_attr_online);
557 if (error)
558 goto err_remove_type_groups;
561 return 0;
563 err_remove_type_groups:
564 if (type)
565 device_remove_groups(dev, type->groups);
566 err_remove_class_bin_attrs:
567 if (class)
568 device_remove_bin_attributes(dev, class->dev_bin_attrs);
569 err_remove_class_attrs:
570 if (class)
571 device_remove_attributes(dev, class->dev_attrs);
572 err_remove_class_groups:
573 if (class)
574 device_remove_groups(dev, class->dev_groups);
576 return error;
579 static void device_remove_attrs(struct device *dev)
581 struct class *class = dev->class;
582 const struct device_type *type = dev->type;
584 device_remove_file(dev, &dev_attr_online);
585 device_remove_groups(dev, dev->groups);
587 if (type)
588 device_remove_groups(dev, type->groups);
590 if (class) {
591 device_remove_attributes(dev, class->dev_attrs);
592 device_remove_bin_attributes(dev, class->dev_bin_attrs);
593 device_remove_groups(dev, class->dev_groups);
597 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
598 char *buf)
600 return print_dev_t(buf, dev->devt);
602 static DEVICE_ATTR_RO(dev);
604 /* /sys/devices/ */
605 struct kset *devices_kset;
608 * device_create_file - create sysfs attribute file for device.
609 * @dev: device.
610 * @attr: device attribute descriptor.
612 int device_create_file(struct device *dev,
613 const struct device_attribute *attr)
615 int error = 0;
617 if (dev) {
618 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
619 "Attribute %s: write permission without 'store'\n",
620 attr->attr.name);
621 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
622 "Attribute %s: read permission without 'show'\n",
623 attr->attr.name);
624 error = sysfs_create_file(&dev->kobj, &attr->attr);
627 return error;
629 EXPORT_SYMBOL_GPL(device_create_file);
632 * device_remove_file - remove sysfs attribute file.
633 * @dev: device.
634 * @attr: device attribute descriptor.
636 void device_remove_file(struct device *dev,
637 const struct device_attribute *attr)
639 if (dev)
640 sysfs_remove_file(&dev->kobj, &attr->attr);
642 EXPORT_SYMBOL_GPL(device_remove_file);
645 * device_create_bin_file - create sysfs binary attribute file for device.
646 * @dev: device.
647 * @attr: device binary attribute descriptor.
649 int device_create_bin_file(struct device *dev,
650 const struct bin_attribute *attr)
652 int error = -EINVAL;
653 if (dev)
654 error = sysfs_create_bin_file(&dev->kobj, attr);
655 return error;
657 EXPORT_SYMBOL_GPL(device_create_bin_file);
660 * device_remove_bin_file - remove sysfs binary attribute file
661 * @dev: device.
662 * @attr: device binary attribute descriptor.
664 void device_remove_bin_file(struct device *dev,
665 const struct bin_attribute *attr)
667 if (dev)
668 sysfs_remove_bin_file(&dev->kobj, attr);
670 EXPORT_SYMBOL_GPL(device_remove_bin_file);
673 * device_schedule_callback_owner - helper to schedule a callback for a device
674 * @dev: device.
675 * @func: callback function to invoke later.
676 * @owner: module owning the callback routine
678 * Attribute methods must not unregister themselves or their parent device
679 * (which would amount to the same thing). Attempts to do so will deadlock,
680 * since unregistration is mutually exclusive with driver callbacks.
682 * Instead methods can call this routine, which will attempt to allocate
683 * and schedule a workqueue request to call back @func with @dev as its
684 * argument in the workqueue's process context. @dev will be pinned until
685 * @func returns.
687 * This routine is usually called via the inline device_schedule_callback(),
688 * which automatically sets @owner to THIS_MODULE.
690 * Returns 0 if the request was submitted, -ENOMEM if storage could not
691 * be allocated, -ENODEV if a reference to @owner isn't available.
693 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
694 * underlying sysfs routine (since it is intended for use by attribute
695 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
697 int device_schedule_callback_owner(struct device *dev,
698 void (*func)(struct device *), struct module *owner)
700 return sysfs_schedule_callback(&dev->kobj,
701 (void (*)(void *)) func, dev, owner);
703 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
705 static void klist_children_get(struct klist_node *n)
707 struct device_private *p = to_device_private_parent(n);
708 struct device *dev = p->device;
710 get_device(dev);
713 static void klist_children_put(struct klist_node *n)
715 struct device_private *p = to_device_private_parent(n);
716 struct device *dev = p->device;
718 put_device(dev);
722 * device_initialize - init device structure.
723 * @dev: device.
725 * This prepares the device for use by other layers by initializing
726 * its fields.
727 * It is the first half of device_register(), if called by
728 * that function, though it can also be called separately, so one
729 * may use @dev's fields. In particular, get_device()/put_device()
730 * may be used for reference counting of @dev after calling this
731 * function.
733 * All fields in @dev must be initialized by the caller to 0, except
734 * for those explicitly set to some other value. The simplest
735 * approach is to use kzalloc() to allocate the structure containing
736 * @dev.
738 * NOTE: Use put_device() to give up your reference instead of freeing
739 * @dev directly once you have called this function.
741 void device_initialize(struct device *dev)
743 dev->kobj.kset = devices_kset;
744 kobject_init(&dev->kobj, &device_ktype);
745 INIT_LIST_HEAD(&dev->dma_pools);
746 mutex_init(&dev->mutex);
747 lockdep_set_novalidate_class(&dev->mutex);
748 spin_lock_init(&dev->devres_lock);
749 INIT_LIST_HEAD(&dev->devres_head);
750 device_pm_init(dev);
751 set_dev_node(dev, -1);
753 EXPORT_SYMBOL_GPL(device_initialize);
755 struct kobject *virtual_device_parent(struct device *dev)
757 static struct kobject *virtual_dir = NULL;
759 if (!virtual_dir)
760 virtual_dir = kobject_create_and_add("virtual",
761 &devices_kset->kobj);
763 return virtual_dir;
766 struct class_dir {
767 struct kobject kobj;
768 struct class *class;
771 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
773 static void class_dir_release(struct kobject *kobj)
775 struct class_dir *dir = to_class_dir(kobj);
776 kfree(dir);
779 static const
780 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
782 struct class_dir *dir = to_class_dir(kobj);
783 return dir->class->ns_type;
786 static struct kobj_type class_dir_ktype = {
787 .release = class_dir_release,
788 .sysfs_ops = &kobj_sysfs_ops,
789 .child_ns_type = class_dir_child_ns_type
792 static struct kobject *
793 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
795 struct class_dir *dir;
796 int retval;
798 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
799 if (!dir)
800 return NULL;
802 dir->class = class;
803 kobject_init(&dir->kobj, &class_dir_ktype);
805 dir->kobj.kset = &class->p->glue_dirs;
807 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
808 if (retval < 0) {
809 kobject_put(&dir->kobj);
810 return NULL;
812 return &dir->kobj;
815 static DEFINE_MUTEX(gdp_mutex);
817 static struct kobject *get_device_parent(struct device *dev,
818 struct device *parent)
820 if (dev->class) {
821 struct kobject *kobj = NULL;
822 struct kobject *parent_kobj;
823 struct kobject *k;
825 #ifdef CONFIG_BLOCK
826 /* block disks show up in /sys/block */
827 if (sysfs_deprecated && dev->class == &block_class) {
828 if (parent && parent->class == &block_class)
829 return &parent->kobj;
830 return &block_class.p->subsys.kobj;
832 #endif
835 * If we have no parent, we live in "virtual".
836 * Class-devices with a non class-device as parent, live
837 * in a "glue" directory to prevent namespace collisions.
839 if (parent == NULL)
840 parent_kobj = virtual_device_parent(dev);
841 else if (parent->class && !dev->class->ns_type)
842 return &parent->kobj;
843 else
844 parent_kobj = &parent->kobj;
846 mutex_lock(&gdp_mutex);
848 /* find our class-directory at the parent and reference it */
849 spin_lock(&dev->class->p->glue_dirs.list_lock);
850 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
851 if (k->parent == parent_kobj) {
852 kobj = kobject_get(k);
853 break;
855 spin_unlock(&dev->class->p->glue_dirs.list_lock);
856 if (kobj) {
857 mutex_unlock(&gdp_mutex);
858 return kobj;
861 /* or create a new class-directory at the parent device */
862 k = class_dir_create_and_add(dev->class, parent_kobj);
863 /* do not emit an uevent for this simple "glue" directory */
864 mutex_unlock(&gdp_mutex);
865 return k;
868 /* subsystems can specify a default root directory for their devices */
869 if (!parent && dev->bus && dev->bus->dev_root)
870 return &dev->bus->dev_root->kobj;
872 if (parent)
873 return &parent->kobj;
874 return NULL;
877 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
879 /* see if we live in a "glue" directory */
880 if (!glue_dir || !dev->class ||
881 glue_dir->kset != &dev->class->p->glue_dirs)
882 return;
884 mutex_lock(&gdp_mutex);
885 kobject_put(glue_dir);
886 mutex_unlock(&gdp_mutex);
889 static void cleanup_device_parent(struct device *dev)
891 cleanup_glue_dir(dev, dev->kobj.parent);
894 static int device_add_class_symlinks(struct device *dev)
896 int error;
898 if (!dev->class)
899 return 0;
901 error = sysfs_create_link(&dev->kobj,
902 &dev->class->p->subsys.kobj,
903 "subsystem");
904 if (error)
905 goto out;
907 if (dev->parent && device_is_not_partition(dev)) {
908 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
909 "device");
910 if (error)
911 goto out_subsys;
914 #ifdef CONFIG_BLOCK
915 /* /sys/block has directories and does not need symlinks */
916 if (sysfs_deprecated && dev->class == &block_class)
917 return 0;
918 #endif
920 /* link in the class directory pointing to the device */
921 error = sysfs_create_link(&dev->class->p->subsys.kobj,
922 &dev->kobj, dev_name(dev));
923 if (error)
924 goto out_device;
926 return 0;
928 out_device:
929 sysfs_remove_link(&dev->kobj, "device");
931 out_subsys:
932 sysfs_remove_link(&dev->kobj, "subsystem");
933 out:
934 return error;
937 static void device_remove_class_symlinks(struct device *dev)
939 if (!dev->class)
940 return;
942 if (dev->parent && device_is_not_partition(dev))
943 sysfs_remove_link(&dev->kobj, "device");
944 sysfs_remove_link(&dev->kobj, "subsystem");
945 #ifdef CONFIG_BLOCK
946 if (sysfs_deprecated && dev->class == &block_class)
947 return;
948 #endif
949 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
953 * dev_set_name - set a device name
954 * @dev: device
955 * @fmt: format string for the device's name
957 int dev_set_name(struct device *dev, const char *fmt, ...)
959 va_list vargs;
960 int err;
962 va_start(vargs, fmt);
963 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
964 va_end(vargs);
965 return err;
967 EXPORT_SYMBOL_GPL(dev_set_name);
970 * device_to_dev_kobj - select a /sys/dev/ directory for the device
971 * @dev: device
973 * By default we select char/ for new entries. Setting class->dev_obj
974 * to NULL prevents an entry from being created. class->dev_kobj must
975 * be set (or cleared) before any devices are registered to the class
976 * otherwise device_create_sys_dev_entry() and
977 * device_remove_sys_dev_entry() will disagree about the presence of
978 * the link.
980 static struct kobject *device_to_dev_kobj(struct device *dev)
982 struct kobject *kobj;
984 if (dev->class)
985 kobj = dev->class->dev_kobj;
986 else
987 kobj = sysfs_dev_char_kobj;
989 return kobj;
992 static int device_create_sys_dev_entry(struct device *dev)
994 struct kobject *kobj = device_to_dev_kobj(dev);
995 int error = 0;
996 char devt_str[15];
998 if (kobj) {
999 format_dev_t(devt_str, dev->devt);
1000 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1003 return error;
1006 static void device_remove_sys_dev_entry(struct device *dev)
1008 struct kobject *kobj = device_to_dev_kobj(dev);
1009 char devt_str[15];
1011 if (kobj) {
1012 format_dev_t(devt_str, dev->devt);
1013 sysfs_remove_link(kobj, devt_str);
1017 int device_private_init(struct device *dev)
1019 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1020 if (!dev->p)
1021 return -ENOMEM;
1022 dev->p->device = dev;
1023 klist_init(&dev->p->klist_children, klist_children_get,
1024 klist_children_put);
1025 INIT_LIST_HEAD(&dev->p->deferred_probe);
1026 return 0;
1030 * device_add - add device to device hierarchy.
1031 * @dev: device.
1033 * This is part 2 of device_register(), though may be called
1034 * separately _iff_ device_initialize() has been called separately.
1036 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1037 * to the global and sibling lists for the device, then
1038 * adds it to the other relevant subsystems of the driver model.
1040 * Do not call this routine or device_register() more than once for
1041 * any device structure. The driver model core is not designed to work
1042 * with devices that get unregistered and then spring back to life.
1043 * (Among other things, it's very hard to guarantee that all references
1044 * to the previous incarnation of @dev have been dropped.) Allocate
1045 * and register a fresh new struct device instead.
1047 * NOTE: _Never_ directly free @dev after calling this function, even
1048 * if it returned an error! Always use put_device() to give up your
1049 * reference instead.
1051 int device_add(struct device *dev)
1053 struct device *parent = NULL;
1054 struct kobject *kobj;
1055 struct class_interface *class_intf;
1056 int error = -EINVAL;
1058 dev = get_device(dev);
1059 if (!dev)
1060 goto done;
1062 if (!dev->p) {
1063 error = device_private_init(dev);
1064 if (error)
1065 goto done;
1069 * for statically allocated devices, which should all be converted
1070 * some day, we need to initialize the name. We prevent reading back
1071 * the name, and force the use of dev_name()
1073 if (dev->init_name) {
1074 dev_set_name(dev, "%s", dev->init_name);
1075 dev->init_name = NULL;
1078 /* subsystems can specify simple device enumeration */
1079 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1080 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1082 if (!dev_name(dev)) {
1083 error = -EINVAL;
1084 goto name_error;
1087 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1089 parent = get_device(dev->parent);
1090 kobj = get_device_parent(dev, parent);
1091 if (kobj)
1092 dev->kobj.parent = kobj;
1094 /* use parent numa_node */
1095 if (parent)
1096 set_dev_node(dev, dev_to_node(parent));
1098 /* first, register with generic layer. */
1099 /* we require the name to be set before, and pass NULL */
1100 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1101 if (error)
1102 goto Error;
1104 /* notify platform of device entry */
1105 if (platform_notify)
1106 platform_notify(dev);
1108 error = device_create_file(dev, &dev_attr_uevent);
1109 if (error)
1110 goto attrError;
1112 if (MAJOR(dev->devt)) {
1113 error = device_create_file(dev, &dev_attr_dev);
1114 if (error)
1115 goto ueventattrError;
1117 error = device_create_sys_dev_entry(dev);
1118 if (error)
1119 goto devtattrError;
1121 devtmpfs_create_node(dev);
1124 error = device_add_class_symlinks(dev);
1125 if (error)
1126 goto SymlinkError;
1127 error = device_add_attrs(dev);
1128 if (error)
1129 goto AttrsError;
1130 error = bus_add_device(dev);
1131 if (error)
1132 goto BusError;
1133 error = dpm_sysfs_add(dev);
1134 if (error)
1135 goto DPMError;
1136 device_pm_add(dev);
1138 /* Notify clients of device addition. This call must come
1139 * after dpm_sysfs_add() and before kobject_uevent().
1141 if (dev->bus)
1142 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1143 BUS_NOTIFY_ADD_DEVICE, dev);
1145 kobject_uevent(&dev->kobj, KOBJ_ADD);
1146 bus_probe_device(dev);
1147 if (parent)
1148 klist_add_tail(&dev->p->knode_parent,
1149 &parent->p->klist_children);
1151 if (dev->class) {
1152 mutex_lock(&dev->class->p->mutex);
1153 /* tie the class to the device */
1154 klist_add_tail(&dev->knode_class,
1155 &dev->class->p->klist_devices);
1157 /* notify any interfaces that the device is here */
1158 list_for_each_entry(class_intf,
1159 &dev->class->p->interfaces, node)
1160 if (class_intf->add_dev)
1161 class_intf->add_dev(dev, class_intf);
1162 mutex_unlock(&dev->class->p->mutex);
1164 done:
1165 put_device(dev);
1166 return error;
1167 DPMError:
1168 bus_remove_device(dev);
1169 BusError:
1170 device_remove_attrs(dev);
1171 AttrsError:
1172 device_remove_class_symlinks(dev);
1173 SymlinkError:
1174 if (MAJOR(dev->devt))
1175 devtmpfs_delete_node(dev);
1176 if (MAJOR(dev->devt))
1177 device_remove_sys_dev_entry(dev);
1178 devtattrError:
1179 if (MAJOR(dev->devt))
1180 device_remove_file(dev, &dev_attr_dev);
1181 ueventattrError:
1182 device_remove_file(dev, &dev_attr_uevent);
1183 attrError:
1184 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1185 kobject_del(&dev->kobj);
1186 Error:
1187 cleanup_device_parent(dev);
1188 if (parent)
1189 put_device(parent);
1190 name_error:
1191 kfree(dev->p);
1192 dev->p = NULL;
1193 goto done;
1195 EXPORT_SYMBOL_GPL(device_add);
1198 * device_register - register a device with the system.
1199 * @dev: pointer to the device structure
1201 * This happens in two clean steps - initialize the device
1202 * and add it to the system. The two steps can be called
1203 * separately, but this is the easiest and most common.
1204 * I.e. you should only call the two helpers separately if
1205 * have a clearly defined need to use and refcount the device
1206 * before it is added to the hierarchy.
1208 * For more information, see the kerneldoc for device_initialize()
1209 * and device_add().
1211 * NOTE: _Never_ directly free @dev after calling this function, even
1212 * if it returned an error! Always use put_device() to give up the
1213 * reference initialized in this function instead.
1215 int device_register(struct device *dev)
1217 device_initialize(dev);
1218 return device_add(dev);
1220 EXPORT_SYMBOL_GPL(device_register);
1223 * get_device - increment reference count for device.
1224 * @dev: device.
1226 * This simply forwards the call to kobject_get(), though
1227 * we do take care to provide for the case that we get a NULL
1228 * pointer passed in.
1230 struct device *get_device(struct device *dev)
1232 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1234 EXPORT_SYMBOL_GPL(get_device);
1237 * put_device - decrement reference count.
1238 * @dev: device in question.
1240 void put_device(struct device *dev)
1242 /* might_sleep(); */
1243 if (dev)
1244 kobject_put(&dev->kobj);
1246 EXPORT_SYMBOL_GPL(put_device);
1249 * device_del - delete device from system.
1250 * @dev: device.
1252 * This is the first part of the device unregistration
1253 * sequence. This removes the device from the lists we control
1254 * from here, has it removed from the other driver model
1255 * subsystems it was added to in device_add(), and removes it
1256 * from the kobject hierarchy.
1258 * NOTE: this should be called manually _iff_ device_add() was
1259 * also called manually.
1261 void device_del(struct device *dev)
1263 struct device *parent = dev->parent;
1264 struct class_interface *class_intf;
1266 /* Notify clients of device removal. This call must come
1267 * before dpm_sysfs_remove().
1269 if (dev->bus)
1270 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1271 BUS_NOTIFY_DEL_DEVICE, dev);
1272 dpm_sysfs_remove(dev);
1273 if (parent)
1274 klist_del(&dev->p->knode_parent);
1275 if (MAJOR(dev->devt)) {
1276 devtmpfs_delete_node(dev);
1277 device_remove_sys_dev_entry(dev);
1278 device_remove_file(dev, &dev_attr_dev);
1280 if (dev->class) {
1281 device_remove_class_symlinks(dev);
1283 mutex_lock(&dev->class->p->mutex);
1284 /* notify any interfaces that the device is now gone */
1285 list_for_each_entry(class_intf,
1286 &dev->class->p->interfaces, node)
1287 if (class_intf->remove_dev)
1288 class_intf->remove_dev(dev, class_intf);
1289 /* remove the device from the class list */
1290 klist_del(&dev->knode_class);
1291 mutex_unlock(&dev->class->p->mutex);
1293 device_remove_file(dev, &dev_attr_uevent);
1294 device_remove_attrs(dev);
1295 bus_remove_device(dev);
1296 device_pm_remove(dev);
1297 driver_deferred_probe_del(dev);
1299 /* Notify the platform of the removal, in case they
1300 * need to do anything...
1302 if (platform_notify_remove)
1303 platform_notify_remove(dev);
1304 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1305 cleanup_device_parent(dev);
1306 kobject_del(&dev->kobj);
1307 put_device(parent);
1309 EXPORT_SYMBOL_GPL(device_del);
1312 * device_unregister - unregister device from system.
1313 * @dev: device going away.
1315 * We do this in two parts, like we do device_register(). First,
1316 * we remove it from all the subsystems with device_del(), then
1317 * we decrement the reference count via put_device(). If that
1318 * is the final reference count, the device will be cleaned up
1319 * via device_release() above. Otherwise, the structure will
1320 * stick around until the final reference to the device is dropped.
1322 void device_unregister(struct device *dev)
1324 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1325 device_del(dev);
1326 put_device(dev);
1328 EXPORT_SYMBOL_GPL(device_unregister);
1330 static struct device *next_device(struct klist_iter *i)
1332 struct klist_node *n = klist_next(i);
1333 struct device *dev = NULL;
1334 struct device_private *p;
1336 if (n) {
1337 p = to_device_private_parent(n);
1338 dev = p->device;
1340 return dev;
1344 * device_get_devnode - path of device node file
1345 * @dev: device
1346 * @mode: returned file access mode
1347 * @uid: returned file owner
1348 * @gid: returned file group
1349 * @tmp: possibly allocated string
1351 * Return the relative path of a possible device node.
1352 * Non-default names may need to allocate a memory to compose
1353 * a name. This memory is returned in tmp and needs to be
1354 * freed by the caller.
1356 const char *device_get_devnode(struct device *dev,
1357 umode_t *mode, kuid_t *uid, kgid_t *gid,
1358 const char **tmp)
1360 char *s;
1362 *tmp = NULL;
1364 /* the device type may provide a specific name */
1365 if (dev->type && dev->type->devnode)
1366 *tmp = dev->type->devnode(dev, mode, uid, gid);
1367 if (*tmp)
1368 return *tmp;
1370 /* the class may provide a specific name */
1371 if (dev->class && dev->class->devnode)
1372 *tmp = dev->class->devnode(dev, mode);
1373 if (*tmp)
1374 return *tmp;
1376 /* return name without allocation, tmp == NULL */
1377 if (strchr(dev_name(dev), '!') == NULL)
1378 return dev_name(dev);
1380 /* replace '!' in the name with '/' */
1381 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1382 if (!*tmp)
1383 return NULL;
1384 while ((s = strchr(*tmp, '!')))
1385 s[0] = '/';
1386 return *tmp;
1390 * device_for_each_child - device child iterator.
1391 * @parent: parent struct device.
1392 * @fn: function to be called for each device.
1393 * @data: data for the callback.
1395 * Iterate over @parent's child devices, and call @fn for each,
1396 * passing it @data.
1398 * We check the return of @fn each time. If it returns anything
1399 * other than 0, we break out and return that value.
1401 int device_for_each_child(struct device *parent, void *data,
1402 int (*fn)(struct device *dev, void *data))
1404 struct klist_iter i;
1405 struct device *child;
1406 int error = 0;
1408 if (!parent->p)
1409 return 0;
1411 klist_iter_init(&parent->p->klist_children, &i);
1412 while ((child = next_device(&i)) && !error)
1413 error = fn(child, data);
1414 klist_iter_exit(&i);
1415 return error;
1417 EXPORT_SYMBOL_GPL(device_for_each_child);
1420 * device_find_child - device iterator for locating a particular device.
1421 * @parent: parent struct device
1422 * @match: Callback function to check device
1423 * @data: Data to pass to match function
1425 * This is similar to the device_for_each_child() function above, but it
1426 * returns a reference to a device that is 'found' for later use, as
1427 * determined by the @match callback.
1429 * The callback should return 0 if the device doesn't match and non-zero
1430 * if it does. If the callback returns non-zero and a reference to the
1431 * current device can be obtained, this function will return to the caller
1432 * and not iterate over any more devices.
1434 * NOTE: you will need to drop the reference with put_device() after use.
1436 struct device *device_find_child(struct device *parent, void *data,
1437 int (*match)(struct device *dev, void *data))
1439 struct klist_iter i;
1440 struct device *child;
1442 if (!parent)
1443 return NULL;
1445 klist_iter_init(&parent->p->klist_children, &i);
1446 while ((child = next_device(&i)))
1447 if (match(child, data) && get_device(child))
1448 break;
1449 klist_iter_exit(&i);
1450 return child;
1452 EXPORT_SYMBOL_GPL(device_find_child);
1454 int __init devices_init(void)
1456 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1457 if (!devices_kset)
1458 return -ENOMEM;
1459 dev_kobj = kobject_create_and_add("dev", NULL);
1460 if (!dev_kobj)
1461 goto dev_kobj_err;
1462 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1463 if (!sysfs_dev_block_kobj)
1464 goto block_kobj_err;
1465 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1466 if (!sysfs_dev_char_kobj)
1467 goto char_kobj_err;
1469 return 0;
1471 char_kobj_err:
1472 kobject_put(sysfs_dev_block_kobj);
1473 block_kobj_err:
1474 kobject_put(dev_kobj);
1475 dev_kobj_err:
1476 kset_unregister(devices_kset);
1477 return -ENOMEM;
1480 static int device_check_offline(struct device *dev, void *not_used)
1482 int ret;
1484 ret = device_for_each_child(dev, NULL, device_check_offline);
1485 if (ret)
1486 return ret;
1488 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1492 * device_offline - Prepare the device for hot-removal.
1493 * @dev: Device to be put offline.
1495 * Execute the device bus type's .offline() callback, if present, to prepare
1496 * the device for a subsequent hot-removal. If that succeeds, the device must
1497 * not be used until either it is removed or its bus type's .online() callback
1498 * is executed.
1500 * Call under device_hotplug_lock.
1502 int device_offline(struct device *dev)
1504 int ret;
1506 if (dev->offline_disabled)
1507 return -EPERM;
1509 ret = device_for_each_child(dev, NULL, device_check_offline);
1510 if (ret)
1511 return ret;
1513 device_lock(dev);
1514 if (device_supports_offline(dev)) {
1515 if (dev->offline) {
1516 ret = 1;
1517 } else {
1518 ret = dev->bus->offline(dev);
1519 if (!ret) {
1520 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1521 dev->offline = true;
1525 device_unlock(dev);
1527 return ret;
1531 * device_online - Put the device back online after successful device_offline().
1532 * @dev: Device to be put back online.
1534 * If device_offline() has been successfully executed for @dev, but the device
1535 * has not been removed subsequently, execute its bus type's .online() callback
1536 * to indicate that the device can be used again.
1538 * Call under device_hotplug_lock.
1540 int device_online(struct device *dev)
1542 int ret = 0;
1544 device_lock(dev);
1545 if (device_supports_offline(dev)) {
1546 if (dev->offline) {
1547 ret = dev->bus->online(dev);
1548 if (!ret) {
1549 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1550 dev->offline = false;
1552 } else {
1553 ret = 1;
1556 device_unlock(dev);
1558 return ret;
1561 struct root_device {
1562 struct device dev;
1563 struct module *owner;
1566 static inline struct root_device *to_root_device(struct device *d)
1568 return container_of(d, struct root_device, dev);
1571 static void root_device_release(struct device *dev)
1573 kfree(to_root_device(dev));
1577 * __root_device_register - allocate and register a root device
1578 * @name: root device name
1579 * @owner: owner module of the root device, usually THIS_MODULE
1581 * This function allocates a root device and registers it
1582 * using device_register(). In order to free the returned
1583 * device, use root_device_unregister().
1585 * Root devices are dummy devices which allow other devices
1586 * to be grouped under /sys/devices. Use this function to
1587 * allocate a root device and then use it as the parent of
1588 * any device which should appear under /sys/devices/{name}
1590 * The /sys/devices/{name} directory will also contain a
1591 * 'module' symlink which points to the @owner directory
1592 * in sysfs.
1594 * Returns &struct device pointer on success, or ERR_PTR() on error.
1596 * Note: You probably want to use root_device_register().
1598 struct device *__root_device_register(const char *name, struct module *owner)
1600 struct root_device *root;
1601 int err = -ENOMEM;
1603 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1604 if (!root)
1605 return ERR_PTR(err);
1607 err = dev_set_name(&root->dev, "%s", name);
1608 if (err) {
1609 kfree(root);
1610 return ERR_PTR(err);
1613 root->dev.release = root_device_release;
1615 err = device_register(&root->dev);
1616 if (err) {
1617 put_device(&root->dev);
1618 return ERR_PTR(err);
1621 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1622 if (owner) {
1623 struct module_kobject *mk = &owner->mkobj;
1625 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1626 if (err) {
1627 device_unregister(&root->dev);
1628 return ERR_PTR(err);
1630 root->owner = owner;
1632 #endif
1634 return &root->dev;
1636 EXPORT_SYMBOL_GPL(__root_device_register);
1639 * root_device_unregister - unregister and free a root device
1640 * @dev: device going away
1642 * This function unregisters and cleans up a device that was created by
1643 * root_device_register().
1645 void root_device_unregister(struct device *dev)
1647 struct root_device *root = to_root_device(dev);
1649 if (root->owner)
1650 sysfs_remove_link(&root->dev.kobj, "module");
1652 device_unregister(dev);
1654 EXPORT_SYMBOL_GPL(root_device_unregister);
1657 static void device_create_release(struct device *dev)
1659 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1660 kfree(dev);
1663 static struct device *
1664 device_create_groups_vargs(struct class *class, struct device *parent,
1665 dev_t devt, void *drvdata,
1666 const struct attribute_group **groups,
1667 const char *fmt, va_list args)
1669 struct device *dev = NULL;
1670 int retval = -ENODEV;
1672 if (class == NULL || IS_ERR(class))
1673 goto error;
1675 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1676 if (!dev) {
1677 retval = -ENOMEM;
1678 goto error;
1681 dev->devt = devt;
1682 dev->class = class;
1683 dev->parent = parent;
1684 dev->groups = groups;
1685 dev->release = device_create_release;
1686 dev_set_drvdata(dev, drvdata);
1688 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1689 if (retval)
1690 goto error;
1692 retval = device_register(dev);
1693 if (retval)
1694 goto error;
1696 return dev;
1698 error:
1699 put_device(dev);
1700 return ERR_PTR(retval);
1704 * device_create_vargs - creates a device and registers it with sysfs
1705 * @class: pointer to the struct class that this device should be registered to
1706 * @parent: pointer to the parent struct device of this new device, if any
1707 * @devt: the dev_t for the char device to be added
1708 * @drvdata: the data to be added to the device for callbacks
1709 * @fmt: string for the device's name
1710 * @args: va_list for the device's name
1712 * This function can be used by char device classes. A struct device
1713 * will be created in sysfs, registered to the specified class.
1715 * A "dev" file will be created, showing the dev_t for the device, if
1716 * the dev_t is not 0,0.
1717 * If a pointer to a parent struct device is passed in, the newly created
1718 * struct device will be a child of that device in sysfs.
1719 * The pointer to the struct device will be returned from the call.
1720 * Any further sysfs files that might be required can be created using this
1721 * pointer.
1723 * Returns &struct device pointer on success, or ERR_PTR() on error.
1725 * Note: the struct class passed to this function must have previously
1726 * been created with a call to class_create().
1728 struct device *device_create_vargs(struct class *class, struct device *parent,
1729 dev_t devt, void *drvdata, const char *fmt,
1730 va_list args)
1732 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1733 fmt, args);
1735 EXPORT_SYMBOL_GPL(device_create_vargs);
1738 * device_create - creates a device and registers it with sysfs
1739 * @class: pointer to the struct class that this device should be registered to
1740 * @parent: pointer to the parent struct device of this new device, if any
1741 * @devt: the dev_t for the char device to be added
1742 * @drvdata: the data to be added to the device for callbacks
1743 * @fmt: string for the device's name
1745 * This function can be used by char device classes. A struct device
1746 * will be created in sysfs, registered to the specified class.
1748 * A "dev" file will be created, showing the dev_t for the device, if
1749 * the dev_t is not 0,0.
1750 * If a pointer to a parent struct device is passed in, the newly created
1751 * struct device will be a child of that device in sysfs.
1752 * The pointer to the struct device will be returned from the call.
1753 * Any further sysfs files that might be required can be created using this
1754 * pointer.
1756 * Returns &struct device pointer on success, or ERR_PTR() on error.
1758 * Note: the struct class passed to this function must have previously
1759 * been created with a call to class_create().
1761 struct device *device_create(struct class *class, struct device *parent,
1762 dev_t devt, void *drvdata, const char *fmt, ...)
1764 va_list vargs;
1765 struct device *dev;
1767 va_start(vargs, fmt);
1768 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1769 va_end(vargs);
1770 return dev;
1772 EXPORT_SYMBOL_GPL(device_create);
1775 * device_create_with_groups - creates a device and registers it with sysfs
1776 * @class: pointer to the struct class that this device should be registered to
1777 * @parent: pointer to the parent struct device of this new device, if any
1778 * @devt: the dev_t for the char device to be added
1779 * @drvdata: the data to be added to the device for callbacks
1780 * @groups: NULL-terminated list of attribute groups to be created
1781 * @fmt: string for the device's name
1783 * This function can be used by char device classes. A struct device
1784 * will be created in sysfs, registered to the specified class.
1785 * Additional attributes specified in the groups parameter will also
1786 * be created automatically.
1788 * A "dev" file will be created, showing the dev_t for the device, if
1789 * the dev_t is not 0,0.
1790 * If a pointer to a parent struct device is passed in, the newly created
1791 * struct device will be a child of that device in sysfs.
1792 * The pointer to the struct device will be returned from the call.
1793 * Any further sysfs files that might be required can be created using this
1794 * pointer.
1796 * Returns &struct device pointer on success, or ERR_PTR() on error.
1798 * Note: the struct class passed to this function must have previously
1799 * been created with a call to class_create().
1801 struct device *device_create_with_groups(struct class *class,
1802 struct device *parent, dev_t devt,
1803 void *drvdata,
1804 const struct attribute_group **groups,
1805 const char *fmt, ...)
1807 va_list vargs;
1808 struct device *dev;
1810 va_start(vargs, fmt);
1811 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1812 fmt, vargs);
1813 va_end(vargs);
1814 return dev;
1816 EXPORT_SYMBOL_GPL(device_create_with_groups);
1818 static int __match_devt(struct device *dev, const void *data)
1820 const dev_t *devt = data;
1822 return dev->devt == *devt;
1826 * device_destroy - removes a device that was created with device_create()
1827 * @class: pointer to the struct class that this device was registered with
1828 * @devt: the dev_t of the device that was previously registered
1830 * This call unregisters and cleans up a device that was created with a
1831 * call to device_create().
1833 void device_destroy(struct class *class, dev_t devt)
1835 struct device *dev;
1837 dev = class_find_device(class, NULL, &devt, __match_devt);
1838 if (dev) {
1839 put_device(dev);
1840 device_unregister(dev);
1843 EXPORT_SYMBOL_GPL(device_destroy);
1846 * device_rename - renames a device
1847 * @dev: the pointer to the struct device to be renamed
1848 * @new_name: the new name of the device
1850 * It is the responsibility of the caller to provide mutual
1851 * exclusion between two different calls of device_rename
1852 * on the same device to ensure that new_name is valid and
1853 * won't conflict with other devices.
1855 * Note: Don't call this function. Currently, the networking layer calls this
1856 * function, but that will change. The following text from Kay Sievers offers
1857 * some insight:
1859 * Renaming devices is racy at many levels, symlinks and other stuff are not
1860 * replaced atomically, and you get a "move" uevent, but it's not easy to
1861 * connect the event to the old and new device. Device nodes are not renamed at
1862 * all, there isn't even support for that in the kernel now.
1864 * In the meantime, during renaming, your target name might be taken by another
1865 * driver, creating conflicts. Or the old name is taken directly after you
1866 * renamed it -- then you get events for the same DEVPATH, before you even see
1867 * the "move" event. It's just a mess, and nothing new should ever rely on
1868 * kernel device renaming. Besides that, it's not even implemented now for
1869 * other things than (driver-core wise very simple) network devices.
1871 * We are currently about to change network renaming in udev to completely
1872 * disallow renaming of devices in the same namespace as the kernel uses,
1873 * because we can't solve the problems properly, that arise with swapping names
1874 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1875 * be allowed to some other name than eth[0-9]*, for the aforementioned
1876 * reasons.
1878 * Make up a "real" name in the driver before you register anything, or add
1879 * some other attributes for userspace to find the device, or use udev to add
1880 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1881 * don't even want to get into that and try to implement the missing pieces in
1882 * the core. We really have other pieces to fix in the driver core mess. :)
1884 int device_rename(struct device *dev, const char *new_name)
1886 char *old_device_name = NULL;
1887 int error;
1889 dev = get_device(dev);
1890 if (!dev)
1891 return -EINVAL;
1893 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1894 __func__, new_name);
1896 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1897 if (!old_device_name) {
1898 error = -ENOMEM;
1899 goto out;
1902 if (dev->class) {
1903 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1904 &dev->kobj, old_device_name, new_name);
1905 if (error)
1906 goto out;
1909 error = kobject_rename(&dev->kobj, new_name);
1910 if (error)
1911 goto out;
1913 out:
1914 put_device(dev);
1916 kfree(old_device_name);
1918 return error;
1920 EXPORT_SYMBOL_GPL(device_rename);
1922 static int device_move_class_links(struct device *dev,
1923 struct device *old_parent,
1924 struct device *new_parent)
1926 int error = 0;
1928 if (old_parent)
1929 sysfs_remove_link(&dev->kobj, "device");
1930 if (new_parent)
1931 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1932 "device");
1933 return error;
1937 * device_move - moves a device to a new parent
1938 * @dev: the pointer to the struct device to be moved
1939 * @new_parent: the new parent of the device (can by NULL)
1940 * @dpm_order: how to reorder the dpm_list
1942 int device_move(struct device *dev, struct device *new_parent,
1943 enum dpm_order dpm_order)
1945 int error;
1946 struct device *old_parent;
1947 struct kobject *new_parent_kobj;
1949 dev = get_device(dev);
1950 if (!dev)
1951 return -EINVAL;
1953 device_pm_lock();
1954 new_parent = get_device(new_parent);
1955 new_parent_kobj = get_device_parent(dev, new_parent);
1957 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1958 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1959 error = kobject_move(&dev->kobj, new_parent_kobj);
1960 if (error) {
1961 cleanup_glue_dir(dev, new_parent_kobj);
1962 put_device(new_parent);
1963 goto out;
1965 old_parent = dev->parent;
1966 dev->parent = new_parent;
1967 if (old_parent)
1968 klist_remove(&dev->p->knode_parent);
1969 if (new_parent) {
1970 klist_add_tail(&dev->p->knode_parent,
1971 &new_parent->p->klist_children);
1972 set_dev_node(dev, dev_to_node(new_parent));
1975 if (dev->class) {
1976 error = device_move_class_links(dev, old_parent, new_parent);
1977 if (error) {
1978 /* We ignore errors on cleanup since we're hosed anyway... */
1979 device_move_class_links(dev, new_parent, old_parent);
1980 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1981 if (new_parent)
1982 klist_remove(&dev->p->knode_parent);
1983 dev->parent = old_parent;
1984 if (old_parent) {
1985 klist_add_tail(&dev->p->knode_parent,
1986 &old_parent->p->klist_children);
1987 set_dev_node(dev, dev_to_node(old_parent));
1990 cleanup_glue_dir(dev, new_parent_kobj);
1991 put_device(new_parent);
1992 goto out;
1995 switch (dpm_order) {
1996 case DPM_ORDER_NONE:
1997 break;
1998 case DPM_ORDER_DEV_AFTER_PARENT:
1999 device_pm_move_after(dev, new_parent);
2000 break;
2001 case DPM_ORDER_PARENT_BEFORE_DEV:
2002 device_pm_move_before(new_parent, dev);
2003 break;
2004 case DPM_ORDER_DEV_LAST:
2005 device_pm_move_last(dev);
2006 break;
2009 put_device(old_parent);
2010 out:
2011 device_pm_unlock();
2012 put_device(dev);
2013 return error;
2015 EXPORT_SYMBOL_GPL(device_move);
2018 * device_shutdown - call ->shutdown() on each device to shutdown.
2020 void device_shutdown(void)
2022 struct device *dev, *parent;
2024 spin_lock(&devices_kset->list_lock);
2026 * Walk the devices list backward, shutting down each in turn.
2027 * Beware that device unplug events may also start pulling
2028 * devices offline, even as the system is shutting down.
2030 while (!list_empty(&devices_kset->list)) {
2031 dev = list_entry(devices_kset->list.prev, struct device,
2032 kobj.entry);
2035 * hold reference count of device's parent to
2036 * prevent it from being freed because parent's
2037 * lock is to be held
2039 parent = get_device(dev->parent);
2040 get_device(dev);
2042 * Make sure the device is off the kset list, in the
2043 * event that dev->*->shutdown() doesn't remove it.
2045 list_del_init(&dev->kobj.entry);
2046 spin_unlock(&devices_kset->list_lock);
2048 /* hold lock to avoid race with probe/release */
2049 if (parent)
2050 device_lock(parent);
2051 device_lock(dev);
2053 /* Don't allow any more runtime suspends */
2054 pm_runtime_get_noresume(dev);
2055 pm_runtime_barrier(dev);
2057 if (dev->bus && dev->bus->shutdown) {
2058 if (initcall_debug)
2059 dev_info(dev, "shutdown\n");
2060 dev->bus->shutdown(dev);
2061 } else if (dev->driver && dev->driver->shutdown) {
2062 if (initcall_debug)
2063 dev_info(dev, "shutdown\n");
2064 dev->driver->shutdown(dev);
2067 device_unlock(dev);
2068 if (parent)
2069 device_unlock(parent);
2071 put_device(dev);
2072 put_device(parent);
2074 spin_lock(&devices_kset->list_lock);
2076 spin_unlock(&devices_kset->list_lock);
2077 async_synchronize_full();
2081 * Device logging functions
2084 #ifdef CONFIG_PRINTK
2085 static int
2086 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2088 const char *subsys;
2089 size_t pos = 0;
2091 if (dev->class)
2092 subsys = dev->class->name;
2093 else if (dev->bus)
2094 subsys = dev->bus->name;
2095 else
2096 return 0;
2098 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2101 * Add device identifier DEVICE=:
2102 * b12:8 block dev_t
2103 * c127:3 char dev_t
2104 * n8 netdev ifindex
2105 * +sound:card0 subsystem:devname
2107 if (MAJOR(dev->devt)) {
2108 char c;
2110 if (strcmp(subsys, "block") == 0)
2111 c = 'b';
2112 else
2113 c = 'c';
2114 pos++;
2115 pos += snprintf(hdr + pos, hdrlen - pos,
2116 "DEVICE=%c%u:%u",
2117 c, MAJOR(dev->devt), MINOR(dev->devt));
2118 } else if (strcmp(subsys, "net") == 0) {
2119 struct net_device *net = to_net_dev(dev);
2121 pos++;
2122 pos += snprintf(hdr + pos, hdrlen - pos,
2123 "DEVICE=n%u", net->ifindex);
2124 } else {
2125 pos++;
2126 pos += snprintf(hdr + pos, hdrlen - pos,
2127 "DEVICE=+%s:%s", subsys, dev_name(dev));
2130 return pos;
2132 EXPORT_SYMBOL(create_syslog_header);
2134 int dev_vprintk_emit(int level, const struct device *dev,
2135 const char *fmt, va_list args)
2137 char hdr[128];
2138 size_t hdrlen;
2140 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2142 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2144 EXPORT_SYMBOL(dev_vprintk_emit);
2146 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2148 va_list args;
2149 int r;
2151 va_start(args, fmt);
2153 r = dev_vprintk_emit(level, dev, fmt, args);
2155 va_end(args);
2157 return r;
2159 EXPORT_SYMBOL(dev_printk_emit);
2161 static int __dev_printk(const char *level, const struct device *dev,
2162 struct va_format *vaf)
2164 if (!dev)
2165 return printk("%s(NULL device *): %pV", level, vaf);
2167 return dev_printk_emit(level[1] - '0', dev,
2168 "%s %s: %pV",
2169 dev_driver_string(dev), dev_name(dev), vaf);
2172 int dev_printk(const char *level, const struct device *dev,
2173 const char *fmt, ...)
2175 struct va_format vaf;
2176 va_list args;
2177 int r;
2179 va_start(args, fmt);
2181 vaf.fmt = fmt;
2182 vaf.va = &args;
2184 r = __dev_printk(level, dev, &vaf);
2186 va_end(args);
2188 return r;
2190 EXPORT_SYMBOL(dev_printk);
2192 #define define_dev_printk_level(func, kern_level) \
2193 int func(const struct device *dev, const char *fmt, ...) \
2195 struct va_format vaf; \
2196 va_list args; \
2197 int r; \
2199 va_start(args, fmt); \
2201 vaf.fmt = fmt; \
2202 vaf.va = &args; \
2204 r = __dev_printk(kern_level, dev, &vaf); \
2206 va_end(args); \
2208 return r; \
2210 EXPORT_SYMBOL(func);
2212 define_dev_printk_level(dev_emerg, KERN_EMERG);
2213 define_dev_printk_level(dev_alert, KERN_ALERT);
2214 define_dev_printk_level(dev_crit, KERN_CRIT);
2215 define_dev_printk_level(dev_err, KERN_ERR);
2216 define_dev_printk_level(dev_warn, KERN_WARNING);
2217 define_dev_printk_level(dev_notice, KERN_NOTICE);
2218 define_dev_printk_level(_dev_info, KERN_INFO);
2220 #endif