13 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
28 #include <linux/netdevice.h>
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
39 static __init int sysfs_deprecated_setup(
char *
arg)
43 early_param(
"sysfs.deprecated", sysfs_deprecated_setup);
48 static struct kobject *dev_kobj;
53 static inline int device_is_not_partition(
struct device *
dev)
58 static inline int device_is_not_partition(
struct device *
dev)
82 return drv ? drv->
name :
83 (dev->
bus ? dev->
bus->name :
88 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
98 ret = dev_attr->
show(dev, dev_attr, buf);
100 print_symbol(
"dev_attr_show: %s returned bad count\n",
101 (
unsigned long)dev_attr->
show);
114 ret = dev_attr->
store(dev, dev_attr, buf, count);
118 static const struct sysfs_ops dev_sysfs_ops = {
119 .show = dev_attr_show,
120 .store = dev_attr_store,
123 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
127 const char *buf,
size_t size)
134 *(
unsigned long *)(ea->
var) =
new;
151 const char *buf,
size_t size)
158 *(
int *)(ea->
var) =
new;
182 static void device_release(
struct kobject *kobj)
184 struct device *dev = kobj_to_dev(kobj);
200 else if (dev->
type && dev->
type->release)
201 dev->
type->release(dev);
202 else if (dev->
class && dev->
class->dev_release)
203 dev->
class->dev_release(dev);
205 WARN(1,
KERN_ERR "Device '%s' does not have a release() "
206 "function, it is broken and must be fixed.\n",
211 static const void *device_namespace(
struct kobject *kobj)
213 struct device *dev = kobj_to_dev(kobj);
217 ns = dev->
class->namespace(dev);
223 .release = device_release,
224 .sysfs_ops = &dev_sysfs_ops,
233 if (ktype == &device_ktype) {
234 struct device *dev = kobj_to_dev(kobj);
243 static const char *dev_uevent_name(
struct kset *kset,
struct kobject *kobj)
245 struct device *dev = kobj_to_dev(kobj);
248 return dev->
bus->name;
250 return dev->
class->name;
254 static int dev_uevent(
struct kset *kset,
struct kobject *kobj,
257 struct device *dev = kobj_to_dev(kobj);
287 if (dev->
bus && dev->
bus->uevent) {
288 retval = dev->
bus->uevent(dev, env);
290 pr_debug(
"device: '%s': %s: bus uevent() returned %d\n",
291 dev_name(dev), __func__, retval);
296 retval = dev->
class->dev_uevent(dev, env);
298 pr_debug(
"device: '%s': %s: class uevent() "
299 "returned %d\n", dev_name(dev),
304 if (dev->
type && dev->
type->uevent) {
305 retval = dev->
type->uevent(dev, env);
307 pr_debug(
"device: '%s': %s: dev_type uevent() "
308 "returned %d\n", dev_name(dev),
316 .filter = dev_uevent_filter,
317 .name = dev_uevent_name,
318 .uevent = dev_uevent,
332 top_kobj = &dev->
kobj;
334 top_kobj = top_kobj->
parent;
338 kset = top_kobj->
kset;
358 count +=
sprintf(&buf[count],
"%s\n", env->
envp[i]);
365 const char *buf,
size_t count)
372 dev_err(dev,
"uevent: unknown action-string\n");
379 static int device_add_attributes(
struct device *dev,
398 static void device_remove_attributes(
struct device *dev,
408 static int device_add_bin_attributes(
struct device *dev,
427 static void device_remove_bin_attributes(
struct device *dev,
437 static int device_add_groups(
struct device *dev,
444 for (i = 0; groups[
i]; i++) {
457 static void device_remove_groups(
struct device *dev,
463 for (i = 0; groups[
i]; i++)
467 static int device_add_attrs(
struct device *dev)
479 goto err_remove_class_attrs;
483 error = device_add_groups(dev, type->
groups);
485 goto err_remove_class_bin_attrs;
488 error = device_add_groups(dev, dev->
groups);
490 goto err_remove_type_groups;
494 err_remove_type_groups:
496 device_remove_groups(dev, type->
groups);
497 err_remove_class_bin_attrs:
500 err_remove_class_attrs:
507 static void device_remove_attrs(
struct device *dev)
512 device_remove_groups(dev, dev->
groups);
515 device_remove_groups(dev, type->
groups);
619 (
void (*)(
void *))
func, dev, owner);
623 static void klist_children_get(
struct klist_node *
n)
631 static void klist_children_put(
struct klist_node *
n)
669 set_dev_node(dev, -1);
672 static struct kobject *virtual_device_parent(
struct device *dev)
678 &devices_kset->
kobj);
688 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
690 static void class_dir_release(
struct kobject *kobj)
700 return dir->
class->ns_type;
703 static struct kobj_type class_dir_ktype = {
704 .release = class_dir_release,
706 .child_ns_type = class_dir_child_ns_type
710 class_dir_create_and_add(
struct class *
class,
struct kobject *parent_kobj)
722 dir->
kobj.kset = &
class->p->glue_dirs;
746 return &parent->
kobj;
757 parent_kobj = virtual_device_parent(dev);
758 else if (parent->
class && !dev->
class->ns_type)
759 return &parent->
kobj;
761 parent_kobj = &parent->
kobj;
766 spin_lock(&dev->
class->p->glue_dirs.list_lock);
768 if (k->parent == parent_kobj) {
772 spin_unlock(&dev->
class->p->glue_dirs.list_lock);
779 k = class_dir_create_and_add(dev->
class, parent_kobj);
786 if (!parent && dev->
bus && dev->
bus->dev_root)
787 return &dev->
bus->dev_root->kobj;
790 return &parent->
kobj;
794 static void cleanup_glue_dir(
struct device *dev,
struct kobject *glue_dir)
797 if (!glue_dir || !dev->
class ||
798 glue_dir->
kset != &dev->
class->p->glue_dirs)
804 static void cleanup_device_parent(
struct device *dev)
806 cleanup_glue_dir(dev, dev->
kobj.parent);
809 static int device_add_class_symlinks(
struct device *dev)
817 &dev->
class->p->subsys.kobj,
822 if (dev->
parent && device_is_not_partition(dev)) {
837 &dev->
kobj, dev_name(dev));
852 static void device_remove_class_symlinks(
struct device *dev)
857 if (dev->
parent && device_is_not_partition(dev))
895 static struct kobject *device_to_dev_kobj(
struct device *dev)
900 kobj = dev->
class->dev_kobj;
907 static int device_create_sys_dev_entry(
struct device *dev)
909 struct kobject *kobj = device_to_dev_kobj(dev);
921 static void device_remove_sys_dev_entry(
struct device *dev)
923 struct kobject *kobj = device_to_dev_kobj(dev);
937 dev->
p->device =
dev;
938 klist_init(&dev->
p->klist_children, klist_children_get,
940 INIT_LIST_HEAD(&dev->
p->deferred_probe);
994 if (!dev_name(dev) && dev->
bus && dev->
bus->dev_name)
997 if (!dev_name(dev)) {
1002 pr_debug(
"device: '%s': %s\n", dev_name(dev), __func__);
1005 kobj = get_device_parent(dev, parent);
1007 dev->
kobj.parent = kobj;
1011 set_dev_node(dev, dev_to_node(parent));
1030 goto ueventattrError;
1032 error = device_create_sys_dev_entry(dev);
1039 error = device_add_class_symlinks(dev);
1042 error = device_add_attrs(dev);
1064 &parent->
p->klist_children);
1070 &dev->
class->p->klist_devices);
1076 class_intf->
add_dev(dev, class_intf);
1085 device_remove_attrs(dev);
1087 device_remove_class_symlinks(dev);
1092 device_remove_sys_dev_entry(dev);
1102 cleanup_device_parent(dev);
1189 device_remove_sys_dev_entry(dev);
1193 device_remove_class_symlinks(dev);
1206 device_remove_attrs(dev);
1216 cleanup_device_parent(dev);
1234 pr_debug(
"device: '%s': %s\n", dev_name(dev), __func__);
1264 umode_t *mode,
const char **tmp)
1271 if (dev->
type && dev->
type->devnode)
1272 *tmp = dev->
type->devnode(dev, mode);
1278 *tmp = dev->
class->devnode(dev, mode);
1284 return dev_name(dev);
1290 while ((s =
strchr(*tmp,
'!')))
1308 int (*
fn)(
struct device *dev,
void *data))
1318 while ((child = next_device(&i)) && !error)
1319 error =
fn(child, data);
1349 while ((child = next_device(&i)))
1365 if (!sysfs_dev_block_kobj)
1366 goto block_kobj_err;
1368 if (!sysfs_dev_char_kobj)
1407 static void root_device_release(
struct device *dev)
1441 return ERR_PTR(err);
1446 return ERR_PTR(err);
1449 root->
dev.release = root_device_release;
1454 return ERR_PTR(err);
1457 #ifdef CONFIG_MODULES
1464 return ERR_PTR(err);
1493 static void device_create_release(
struct device *dev)
1495 pr_debug(
"device: '%s': %s\n", dev_name(dev), __func__);
1531 if (
class ==
NULL || IS_ERR(
class))
1543 dev->
release = device_create_release;
1558 return ERR_PTR(retval);
1599 static int __match_devt(
struct device *dev,
void *data)
1667 char *old_class_name =
NULL;
1668 char *new_class_name =
NULL;
1669 char *old_device_name =
NULL;
1676 pr_debug(
"device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1677 __func__, new_name);
1680 if (!old_device_name) {
1687 &dev->
kobj, old_device_name, new_name);
1699 kfree(new_class_name);
1700 kfree(old_class_name);
1701 kfree(old_device_name);
1707 static int device_move_class_links(
struct device *dev,
1708 struct device *old_parent,
1709 struct device *new_parent)
1731 struct device *old_parent;
1732 struct kobject *new_parent_kobj;
1740 new_parent_kobj = get_device_parent(dev, new_parent);
1742 pr_debug(
"device: '%s': %s: moving to '%s'\n", dev_name(dev),
1743 __func__, new_parent ? dev_name(new_parent) :
"<NULL>");
1746 cleanup_glue_dir(dev, new_parent_kobj);
1750 old_parent = dev->
parent;
1751 dev->
parent = new_parent;
1756 &new_parent->
p->klist_children);
1757 set_dev_node(dev, dev_to_node(new_parent));
1761 error = device_move_class_links(dev, old_parent, new_parent);
1764 device_move_class_links(dev, new_parent, old_parent);
1768 dev->
parent = old_parent;
1771 &old_parent->
p->klist_children);
1772 set_dev_node(dev, dev_to_node(old_parent));
1775 cleanup_glue_dir(dev, new_parent_kobj);
1780 switch (dpm_order) {
1815 while (!list_empty(&devices_kset->
list)) {
1830 list_del_init(&dev->
kobj.entry);
1835 device_lock(dev->
parent);
1839 pm_runtime_get_noresume(dev);
1842 if (dev->
bus && dev->
bus->shutdown) {
1844 dev->
bus->shutdown(dev);
1847 dev->
driver->shutdown(dev);
1852 device_unlock(dev->
parent);
1867 #ifdef CONFIG_PRINTK
1869 create_syslog_header(
const struct device *dev,
char *
hdr,
size_t hdrlen)
1875 subsys = dev->
class->name;
1877 subsys = dev->
bus->name;
1881 pos +=
snprintf(hdr + pos, hdrlen - pos,
"SUBSYSTEM=%s", subsys);
1893 if (
strcmp(subsys,
"block") == 0)
1898 pos +=
snprintf(hdr + pos, hdrlen - pos,
1901 }
else if (
strcmp(subsys,
"net") == 0) {
1905 pos +=
snprintf(hdr + pos, hdrlen - pos,
1909 pos +=
snprintf(hdr + pos, hdrlen - pos,
1910 "DEVICE=+%s:%s", subsys, dev_name(dev));
1917 int dev_vprintk_emit(
int level,
const struct device *dev,
1923 hdrlen = create_syslog_header(dev, hdr,
sizeof(hdr));
1925 return vprintk_emit(0, level, hdrlen ? hdr :
NULL, hdrlen, fmt, args);
1929 int dev_printk_emit(
int level,
const struct device *dev,
const char *fmt, ...)
1936 r = dev_vprintk_emit(level, dev, fmt, args);
1944 static int __dev_printk(
const char *level,
const struct device *dev,
1948 return printk(
"%s(NULL device *): %pV", level, vaf);
1950 return dev_printk_emit(level[1] -
'0', dev,
1955 int dev_printk(
const char *level,
const struct device *dev,
1956 const char *fmt, ...)
1967 r = __dev_printk(level, dev, &vaf);
1975 #define define_dev_printk_level(func, kern_level) \
1976 int func(const struct device *dev, const char *fmt, ...) \
1978 struct va_format vaf; \
1982 va_start(args, fmt); \
1987 r = __dev_printk(kern_level, dev, &vaf); \
1993 EXPORT_SYMBOL(func);
1995 define_dev_printk_level(dev_emerg,
KERN_EMERG);
1996 define_dev_printk_level(dev_alert,
KERN_ALERT);
1997 define_dev_printk_level(dev_crit,
KERN_CRIT);
2001 define_dev_printk_level(_dev_info,
KERN_INFO);