/* * drivers/base/core.c - core driver model code (device registration, etc) * * Copyright (c) 2002-3 Patrick Mochel * Copyright (c) 2002-3 Open Source Development Labs * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> * Copyright (c) 2006 Novell, Inc. * * This file is released under the GPLv2 * */ #include <linux/device.h>#include <linux/err.h>#include <linux/init.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/kdev_t.h>#include <linux/notifier.h>#include <linux/genhd.h>#include <linux/kallsyms.h>#include <linux/mutex.h>#include <linux/async.h>#include <linux/pm_runtime.h> #include "base.h"#include "power/power.h" #ifdef CONFIG_SYSFS_DEPRECATED#ifdef CONFIG_SYSFS_DEPRECATED_V2long sysfs_deprecated = 1;#elselong sysfs_deprecated = 0;#endifstatic __init int sysfs_deprecated_setup(char *arg){ return strict_strtol(arg, 10, &sysfs_deprecated);}early_param("sysfs.deprecated", sysfs_deprecated_setup);#endif int (*platform_notify)(struct device *dev) = NULL;int (*platform_notify_remove)(struct device *dev) = NULL;static struct kobject *dev_kobj;struct kobject *sysfs_dev_char_kobj;struct kobject *sysfs_dev_block_kobj; #ifdef CONFIG_BLOCKstatic inline int device_is_not_partition(struct device *dev){ return !(dev->type == &part_type);}#elsestatic inline int device_is_not_partition(struct device *dev){ return 1;}#endif /** * dev_driver_string - Return a device's driver name, if at all possible * @dev: struct device to get the name of * * Will return the device's driver's name if it is bound to a device. If * the device is not bound to a device, it will return the name of the bus * it is attached to. If it is not attached to a bus either, an empty * string will be returned. */const char *dev_driver_string(const struct device *dev){ struct device_driver *drv; /* dev->driver can change to NULL underneath us because of unbinding, * so be careful about accessing it. dev->bus and dev->class should * never change once they are set, so they don't need special care. */ drv = ACCESS_ONCE(dev->driver); return drv ? drv->name : (dev->bus ? dev->bus->name : (dev->class ? dev->class->name : ""));}EXPORT_SYMBOL(dev_driver_string); #define to_dev(obj) container_of(obj, struct device, kobj)#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, char *buf){ struct device_attribute *dev_attr = to_dev_attr(attr); struct device *dev = to_dev(kobj); ssize_t ret = -EIO; if (dev_attr->show) ret = dev_attr->show(dev, dev_attr, buf); if (ret >= (ssize_t)PAGE_SIZE) { print_symbol("dev_attr_show: %s returned bad count\n", (unsigned long)dev_attr->show); } return ret;} static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count){ struct device_attribute *dev_attr = to_dev_attr(attr); struct device *dev = to_dev(kobj); ssize_t ret = -EIO; if (dev_attr->store) ret = dev_attr->store(dev, dev_attr, buf, count); return ret;} static const struct sysfs_ops dev_sysfs_ops = { .show = dev_attr_show, .store = dev_attr_store,}; /** * device_release - free device structure. * @kobj: device's kobject. * * This is called once the reference count for the object * reaches 0. We forward the call to the device's release * method, which should handle actually freeing the structure. */static void device_release(struct kobject *kobj){ struct device *dev = to_dev(kobj); struct device_private *p = dev->p; if (dev->release) dev->release(dev); else if (dev->type && dev->type->release) dev->type->release(dev); else if (dev->class && dev->class->dev_release) dev->class->dev_release(dev); else WARN(1, KERN_ERR "Device '%s' does not have a release() " "function, it is broken and must be fixed.\n", dev_name(dev)); kfree(p);} static const void *device_namespace(struct kobject *kobj){ struct device *dev = to_dev(kobj); const void *ns = NULL; if (dev->class && dev->class->ns_type) ns = dev->class->namespace(dev); return ns;} static struct kobj_type device_ktype = { .release = device_release, .sysfs_ops = &dev_sysfs_ops, .namespace = device_namespace,}; static int dev_uevent_filter(struct kset *kset, struct kobject *kobj){ struct kobj_type *ktype = get_ktype(kobj); if (ktype == &device_ktype) { struct device *dev = to_dev(kobj); if (dev->bus) return 1; if (dev->class) return 1; } return 0;} static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj){ struct device *dev = to_dev(kobj); if (dev->bus) return dev->bus->name; if (dev->class) return dev->class->name; return NULL;} static int dev_uevent(struct kset *kset, struct kobject *kobj, struct kobj_uevent_env *env){ struct device *dev = to_dev(kobj); int retval = 0; /* add device node properties if present */ if (MAJOR(dev->devt)) { const char *tmp; const char *name; mode_t mode = 0; add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); name = device_get_devnode(dev, &mode, &tmp); if (name) { add_uevent_var(env, "DEVNAME=%s", name); kfree(tmp); if (mode) add_uevent_var(env, "DEVMODE=%#o", mode & 0777); } } if (dev->type && dev->type->name) add_uevent_var(env, "DEVTYPE=%s", dev->type->name); if (dev->driver) add_uevent_var(env, "DRIVER=%s", dev->driver->name); /* have the bus specific function add its stuff */ if (dev->bus && dev->bus->uevent) { retval = dev->bus->uevent(dev, env); if (retval) pr_debug("device: '%s': %s: bus uevent() returned %d\n", dev_name(dev), __func__, retval); } /* have the class specific function add its stuff */ if (dev->class && dev->class->dev_uevent) { retval = dev->class->dev_uevent(dev, env); if (retval) pr_debug("device: '%s': %s: class uevent() " "returned %d\n", dev_name(dev), __func__, retval); } /* have the device type specific function add its stuff */ if (dev->type && dev->type->uevent) { retval = dev->type->uevent(dev, env); if (retval) pr_debug("device: '%s': %s: dev_type uevent() " "returned %d\n", dev_name(dev), __func__, retval); } return retval;} static const struct kset_uevent_ops device_uevent_ops = { .filter = dev_uevent_filter, .name = dev_uevent_name, .uevent = dev_uevent,}; static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, char *buf){ struct kobject *top_kobj; struct kset *kset; struct kobj_uevent_env *env = NULL; int i; size_t count = 0; int retval; /* search the kset, the device belongs to */ top_kobj = &dev->kobj; while (!top_kobj->kset && top_kobj->parent) top_kobj = top_kobj->parent; if (!top_kobj->kset) goto out; kset = top_kobj->kset; if (!kset->uevent_ops || !kset->uevent_ops->uevent) goto out; /* respect filter */ if (kset->uevent_ops && kset->uevent_ops->filter) if (!kset->uevent_ops->filter(kset, &dev->kobj)) goto out; env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); if (!env) return -ENOMEM; /* let the kset specific function add its keys */ retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); if (retval) goto out; /* copy keys to file */ for (i = 0; i < env->envp_idx; i++) count += sprintf(&buf[count], "%s\n", env->envp[i]);out: kfree(env); return count;} static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ enum kobject_action action; if (kobject_action_type(buf, count, &action) == 0) kobject_uevent(&dev->kobj, action); else dev_err(dev, "uevent: unknown action-string\n"); return count;} static struct device_attribute uevent_attr = __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); static int device_add_attributes(struct device *dev, struct device_attribute *attrs){ int error = 0; int i; if (attrs) { for (i = 0; attr_name(attrs[i]); i++) { error = device_create_file(dev, &attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_file(dev, &attrs[i]); } return error;} static void device_remove_attributes(struct device *dev, struct device_attribute *attrs){ int i; if (attrs) for (i = 0; attr_name(attrs[i]); i++) device_remove_file(dev, &attrs[i]);} static int device_add_bin_attributes(struct device *dev, struct bin_attribute *attrs){ int error = 0; int i; if (attrs) { for (i = 0; attr_name(attrs[i]); i++) { error = device_create_bin_file(dev, &attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_bin_file(dev, &attrs[i]); } return error;} static void device_remove_bin_attributes(struct device *dev, struct bin_attribute *attrs){ int i; if (attrs) for (i = 0; attr_name(attrs[i]); i++) device_remove_bin_file(dev, &attrs[i]);} static int device_add_groups(struct device *dev, const struct attribute_group **groups){ int error = 0; int i; if (groups) { for (i = 0; groups[i]; i++) { error = sysfs_create_group(&dev->kobj, groups[i]); if (error) { while (--i >= 0) sysfs_remove_group(&dev->kobj, groups[i]); break; } } } return error;} static void device_remove_groups(struct device *dev, const struct attribute_group **groups){ int i; if (groups) for (i = 0; groups[i]; i++) sysfs_remove_group(&dev->kobj, groups[i]);} static int device_add_attrs(struct device *dev){ struct class *class = dev->class; const struct device_type *type = dev->type; int error; if (class) { error = device_add_attributes(dev, class->dev_attrs); if (error) return error; error = device_add_bin_attributes(dev, class->dev_bin_attrs); if (error) goto err_remove_class_attrs; } if (type) { error = device_add_groups(dev, type->groups); if (error) goto err_remove_class_bin_attrs; } error = device_add_groups(dev, dev->groups); if (error) goto err_remove_type_groups; return 0; err_remove_type_groups: if (type) device_remove_groups(dev, type->groups); err_remove_class_bin_attrs: if (class) device_remove_bin_attributes(dev, class->dev_bin_attrs); err_remove_class_attrs: if (class) device_remove_attributes(dev, class->dev_attrs); return error;} static void device_remove_attrs(struct device *dev){ struct class *class = dev->class; const struct device_type *type = dev->type; device_remove_groups(dev, dev->groups); if (type) device_remove_groups(dev, type->groups); if (class) { device_remove_attributes(dev, class->dev_attrs); device_remove_bin_attributes(dev, class->dev_bin_attrs); }} static ssize_t show_dev(struct device *dev, struct device_attribute *attr, char *buf){ return print_dev_t(buf, dev->devt);} static struct device_attribute devt_attr = __ATTR(dev, S_IRUGO, show_dev, NULL); /* kset to create /sys/devices/ */struct kset *devices_kset; /** * device_create_file - create sysfs attribute file for device. * @dev: device. * @attr: device attribute descriptor. */int device_create_file(struct device *dev, const struct device_attribute *attr){ int error = 0; if (dev) error = sysfs_create_file(&dev->kobj, &attr->attr); return error;} /** * device_remove_file - remove sysfs attribute file. * @dev: device. * @attr: device attribute descriptor. */void device_remove_file(struct device *dev, const struct device_attribute *attr){ if (dev) sysfs_remove_file(&dev->kobj, &attr->attr);} /** * device_create_bin_file - create sysfs binary attribute file for device. * @dev: device. * @attr: device binary attribute descriptor. */Lines 1–500 of 1829. Load more
/* * drivers/base/core.c - core driver model code (device registration, etc) * * Copyright (c) 2002-3 Patrick Mochel * Copyright (c) 2002-3 Open Source Development Labs * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> * Copyright (c) 2006 Novell, Inc. * * This file is released under the GPLv2 * */ #include <linux/device.h> #include <linux/err.h> #include <linux/init.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/kdev_t.h> #include <linux/notifier.h> #include <linux/genhd.h> #include <linux/kallsyms.h> #include <linux/mutex.h> #include <linux/async.h> #include <linux/pm_runtime.h> #include "base.h" #include "power/power.h" #ifdef CONFIG_SYSFS_DEPRECATED #ifdef CONFIG_SYSFS_DEPRECATED_V2 long sysfs_deprecated = 1; #else long sysfs_deprecated = 0; #endif static __init int sysfs_deprecated_setup(char *arg) { return strict_strtol(arg, 10, &sysfs_deprecated); } early_param("sysfs.deprecated", sysfs_deprecated_setup); #endif int (*platform_notify)(struct device *dev) = NULL; int (*platform_notify_remove)(struct device *dev) = NULL; static struct kobject *dev_kobj; struct kobject *sysfs_dev_char_kobj; struct kobject *sysfs_dev_block_kobj; #ifdef CONFIG_BLOCK static inline int device_is_not_partition(struct device *dev) { return !(dev->type == &part_type); } #else static inline int device_is_not_partition(struct device *dev) { return 1; } #endif /** * dev_driver_string - Return a device's driver name, if at all possible * @dev: struct device to get the name of * * Will return the device's driver's name if it is bound to a device. If * the device is not bound to a device, it will return the name of the bus * it is attached to. If it is not attached to a bus either, an empty * string will be returned. */ const char *dev_driver_string(const struct device *dev) { struct device_driver *drv; /* dev->driver can change to NULL underneath us because of unbinding, * so be careful about accessing it. dev->bus and dev->class should * never change once they are set, so they don't need special care. */ drv = ACCESS_ONCE(dev->driver); return drv ? drv->name : (dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "")); } EXPORT_SYMBOL(dev_driver_string); #define to_dev(obj) container_of(obj, struct device, kobj) #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) { struct device_attribute *dev_attr = to_dev_attr(attr); struct device *dev = to_dev(kobj); ssize_t ret = -EIO; if (dev_attr->show) ret = dev_attr->show(dev, dev_attr, buf); if (ret >= (ssize_t)PAGE_SIZE) { print_symbol("dev_attr_show: %s returned bad count\n", (unsigned long)dev_attr->show); } return ret; } static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) { struct device_attribute *dev_attr = to_dev_attr(attr); struct device *dev = to_dev(kobj); ssize_t ret = -EIO; if (dev_attr->store) ret = dev_attr->store(dev, dev_attr, buf, count); return ret; } static const struct sysfs_ops dev_sysfs_ops = { .show = dev_attr_show, .store = dev_attr_store, }; /** * device_release - free device structure. * @kobj: device's kobject. * * This is called once the reference count for the object * reaches 0. We forward the call to the device's release * method, which should handle actually freeing the structure. */ static void device_release(struct kobject *kobj) { struct device *dev = to_dev(kobj); struct device_private *p = dev->p; if (dev->release) dev->release(dev); else if (dev->type && dev->type->release) dev->type->release(dev); else if (dev->class && dev->class->dev_release) dev->class->dev_release(dev); else WARN(1, KERN_ERR "Device '%s' does not have a release() " "function, it is broken and must be fixed.\n", dev_name(dev)); kfree(p); } static const void *device_namespace(struct kobject *kobj) { struct device *dev = to_dev(kobj); const void *ns = NULL; if (dev->class && dev->class->ns_type) ns = dev->class->namespace(dev); return ns; } static struct kobj_type device_ktype = { .release = device_release, .sysfs_ops = &dev_sysfs_ops, .namespace = device_namespace, }; static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) { struct kobj_type *ktype = get_ktype(kobj); if (ktype == &device_ktype) { struct device *dev = to_dev(kobj); if (dev->bus) return 1; if (dev->class) return 1; } return 0; } static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj) { struct device *dev = to_dev(kobj); if (dev->bus) return dev->bus->name; if (dev->class) return dev->class->name; return NULL; } static int dev_uevent(struct kset *kset, struct kobject *kobj, struct kobj_uevent_env *env) { struct device *dev = to_dev(kobj); int retval = 0; /* add device node properties if present */ if (MAJOR(dev->devt)) { const char *tmp; const char *name; mode_t mode = 0; add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt)); add_uevent_var(env, "MINOR=%u", MINOR(dev->devt)); name = device_get_devnode(dev, &mode, &tmp); if (name) { add_uevent_var(env, "DEVNAME=%s", name); kfree(tmp); if (mode) add_uevent_var(env, "DEVMODE=%#o", mode & 0777); } } if (dev->type && dev->type->name) add_uevent_var(env, "DEVTYPE=%s", dev->type->name); if (dev->driver) add_uevent_var(env, "DRIVER=%s", dev->driver->name); /* have the bus specific function add its stuff */ if (dev->bus && dev->bus->uevent) { retval = dev->bus->uevent(dev, env); if (retval) pr_debug("device: '%s': %s: bus uevent() returned %d\n", dev_name(dev), __func__, retval); } /* have the class specific function add its stuff */ if (dev->class && dev->class->dev_uevent) { retval = dev->class->dev_uevent(dev, env); if (retval) pr_debug("device: '%s': %s: class uevent() " "returned %d\n", dev_name(dev), __func__, retval); } /* have the device type specific function add its stuff */ if (dev->type && dev->type->uevent) { retval = dev->type->uevent(dev, env); if (retval) pr_debug("device: '%s': %s: dev_type uevent() " "returned %d\n", dev_name(dev), __func__, retval); } return retval; } static const struct kset_uevent_ops device_uevent_ops = { .filter = dev_uevent_filter, .name = dev_uevent_name, .uevent = dev_uevent, }; static ssize_t show_uevent(struct device *dev, struct device_attribute *attr, char *buf) { struct kobject *top_kobj; struct kset *kset; struct kobj_uevent_env *env = NULL; int i; size_t count = 0; int retval; /* search the kset, the device belongs to */ top_kobj = &dev->kobj; while (!top_kobj->kset && top_kobj->parent) top_kobj = top_kobj->parent; if (!top_kobj->kset) goto out; kset = top_kobj->kset; if (!kset->uevent_ops || !kset->uevent_ops->uevent) goto out; /* respect filter */ if (kset->uevent_ops && kset->uevent_ops->filter) if (!kset->uevent_ops->filter(kset, &dev->kobj)) goto out; env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL); if (!env) return -ENOMEM; /* let the kset specific function add its keys */ retval = kset->uevent_ops->uevent(kset, &dev->kobj, env); if (retval) goto out; /* copy keys to file */ for (i = 0; i < env->envp_idx; i++) count += sprintf(&buf[count], "%s\n", env->envp[i]); out: kfree(env); return count; } static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { enum kobject_action action; if (kobject_action_type(buf, count, &action) == 0) kobject_uevent(&dev->kobj, action); else dev_err(dev, "uevent: unknown action-string\n"); return count; } static struct device_attribute uevent_attr = __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent); static int device_add_attributes(struct device *dev, struct device_attribute *attrs) { int error = 0; int i; if (attrs) { for (i = 0; attr_name(attrs[i]); i++) { error = device_create_file(dev, &attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_file(dev, &attrs[i]); } return error; } static void device_remove_attributes(struct device *dev, struct device_attribute *attrs) { int i; if (attrs) for (i = 0; attr_name(attrs[i]); i++) device_remove_file(dev, &attrs[i]); } static int device_add_bin_attributes(struct device *dev, struct bin_attribute *attrs) { int error = 0; int i; if (attrs) { for (i = 0; attr_name(attrs[i]); i++) { error = device_create_bin_file(dev, &attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_bin_file(dev, &attrs[i]); } return error; } static void device_remove_bin_attributes(struct device *dev, struct bin_attribute *attrs) { int i; if (attrs) for (i = 0; attr_name(attrs[i]); i++) device_remove_bin_file(dev, &attrs[i]); } static int device_add_groups(struct device *dev, const struct attribute_group **groups) { int error = 0; int i; if (groups) { for (i = 0; groups[i]; i++) { error = sysfs_create_group(&dev->kobj, groups[i]); if (error) { while (--i >= 0) sysfs_remove_group(&dev->kobj, groups[i]); break; } } } return error; } static void device_remove_groups(struct device *dev, const struct attribute_group **groups) { int i; if (groups) for (i = 0; groups[i]; i++) sysfs_remove_group(&dev->kobj, groups[i]); } static int device_add_attrs(struct device *dev) { struct class *class = dev->class; const struct device_type *type = dev->type; int error; if (class) { error = device_add_attributes(dev, class->dev_attrs); if (error) return error; error = device_add_bin_attributes(dev, class->dev_bin_attrs); if (error) goto err_remove_class_attrs; } if (type) { error = device_add_groups(dev, type->groups); if (error) goto err_remove_class_bin_attrs; } error = device_add_groups(dev, dev->groups); if (error) goto err_remove_type_groups; return 0; err_remove_type_groups: if (type) device_remove_groups(dev, type->groups); err_remove_class_bin_attrs: if (class) device_remove_bin_attributes(dev, class->dev_bin_attrs); err_remove_class_attrs: if (class) device_remove_attributes(dev, class->dev_attrs); return error; } static void device_remove_attrs(struct device *dev) { struct class *class = dev->class; const struct device_type *type = dev->type; device_remove_groups(dev, dev->groups); if (type) device_remove_groups(dev, type->groups); if (class) { device_remove_attributes(dev, class->dev_attrs); device_remove_bin_attributes(dev, class->dev_bin_attrs); } } static ssize_t show_dev(struct device *dev, struct device_attribute *attr, char *buf) { return print_dev_t(buf, dev->devt); } static struct device_attribute devt_attr = __ATTR(dev, S_IRUGO, show_dev, NULL); /* kset to create /sys/devices/ */ struct kset *devices_kset; /** * device_create_file - create sysfs attribute file for device. * @dev: device. * @attr: device attribute descriptor. */ int device_create_file(struct device *dev, const struct device_attribute *attr) { int error = 0; if (dev) error = sysfs_create_file(&dev->kobj, &attr->attr); return error; } /** * device_remove_file - remove sysfs attribute file. * @dev: device. * @attr: device attribute descriptor. */ void device_remove_file(struct device *dev, const struct device_attribute *attr) { if (dev) sysfs_remove_file(&dev->kobj, &attr->attr); } /** * device_create_bin_file - create sysfs binary attribute file for device. * @dev: device. * @attr: device binary attribute descriptor. */