/* * 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 <asm/semaphore.h> #include "base.h"#include "power/power.h" int (*platform_notify)(struct device * dev) = NULL;int (*platform_notify_remove)(struct device * dev) = NULL; /* * sysfs bindings for devices. */ /** * 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(struct device *dev){ return dev->driver ? dev->driver->name : (dev->bus ? dev->bus->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_tdev_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); return ret;} static ssize_tdev_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 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); 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 { printk(KERN_ERR "Device '%s' does not have a release() function, " "it is broken and must be fixed.\n", dev->bus_id); WARN_ON(1); }} static struct kobj_type ktype_device = { .release = device_release, .sysfs_ops = &dev_sysfs_ops,}; static int dev_uevent_filter(struct kset *kset, struct kobject *kobj){ struct kobj_type *ktype = get_ktype(kobj); if (ktype == &ktype_device) { 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, char **envp, int num_envp, char *buffer, int buffer_size){ struct device *dev = to_dev(kobj); int i = 0; int length = 0; int retval = 0; /* add the major/minor if present */ if (MAJOR(dev->devt)) { add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "MAJOR=%u", MAJOR(dev->devt)); add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "MINOR=%u", MINOR(dev->devt)); } if (dev->driver) add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "DRIVER=%s", dev->driver->name); #ifdef CONFIG_SYSFS_DEPRECATED if (dev->class) { struct device *parent = dev->parent; /* find first bus device in parent chain */ while (parent && !parent->bus) parent = parent->parent; if (parent && parent->bus) { const char *path; path = kobject_get_path(&parent->kobj, GFP_KERNEL); add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVPATH=%s", path); kfree(path); add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVBUS=%s", parent->bus->name); if (parent->driver) add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVDRIVER=%s", parent->driver->name); } } else if (dev->bus) { add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVBUS=%s", dev->bus->name); if (dev->driver) add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVDRIVER=%s", dev->driver->name); }#endif /* terminate, set to next free slot, shrink available space */ envp[i] = NULL; envp = &envp[i]; num_envp -= i; buffer = &buffer[length]; buffer_size -= length; if (dev->bus && dev->bus->uevent) { /* have the bus specific function add its stuff */ retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); if (retval) pr_debug ("%s: bus uevent() returned %d\n", __FUNCTION__, retval); } if (dev->class && dev->class->dev_uevent) { /* have the class specific function add its stuff */ retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); if (retval) pr_debug("%s: class uevent() returned %d\n", __FUNCTION__, retval); } if (dev->type && dev->type->uevent) { /* have the device type specific fuction add its stuff */ retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size); if (retval) pr_debug("%s: dev_type uevent() returned %d\n", __FUNCTION__, retval); } return retval;} static struct kset_uevent_ops device_uevent_ops = { .filter = dev_uevent_filter, .name = dev_uevent_name, .uevent = dev_uevent,}; static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, const char *buf, size_t count){ kobject_uevent(&dev->kobj, KOBJ_ADD); return count;} static int device_add_groups(struct device *dev){ int i; int error = 0; if (dev->groups) { for (i = 0; dev->groups[i]; i++) { error = sysfs_create_group(&dev->kobj, dev->groups[i]); if (error) { while (--i >= 0) sysfs_remove_group(&dev->kobj, dev->groups[i]); goto out; } } }out: return error;} static void device_remove_groups(struct device *dev){ int i; if (dev->groups) { for (i = 0; dev->groups[i]; i++) { sysfs_remove_group(&dev->kobj, dev->groups[i]); } }} static int device_add_attrs(struct device *dev){ struct class *class = dev->class; struct device_type *type = dev->type; int error = 0; int i; if (class && class->dev_attrs) { for (i = 0; attr_name(class->dev_attrs[i]); i++) { error = device_create_file(dev, &class->dev_attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_file(dev, &class->dev_attrs[i]); } if (type && type->attrs) { for (i = 0; attr_name(type->attrs[i]); i++) { error = device_create_file(dev, &type->attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_file(dev, &type->attrs[i]); } return error;} static void device_remove_attrs(struct device *dev){ struct class *class = dev->class; struct device_type *type = dev->type; int i; if (class && class->dev_attrs) { for (i = 0; attr_name(class->dev_attrs[i]); i++) device_remove_file(dev, &class->dev_attrs[i]); } if (type && type->attrs) { for (i = 0; attr_name(type->attrs[i]); i++) device_remove_file(dev, &type->attrs[i]); }} static ssize_t show_dev(struct device *dev, struct device_attribute *attr, char *buf){ return print_dev_t(buf, dev->devt);} /* * devices_subsys - structure to be registered with kobject core. */ decl_subsys(devices, &ktype_device, &device_uevent_ops); /** * device_create_file - create sysfs attribute file for device. * @dev: device. * @attr: device attribute descriptor. */ int device_create_file(struct device * dev, struct device_attribute * attr){ int error = 0; if (get_device(dev)) { error = sysfs_create_file(&dev->kobj, &attr->attr); put_device(dev); } return error;} /** * device_remove_file - remove sysfs attribute file. * @dev: device. * @attr: device attribute descriptor. */ void device_remove_file(struct device * dev, struct device_attribute * attr){ if (get_device(dev)) { sysfs_remove_file(&dev->kobj, &attr->attr); put_device(dev); }} /** * device_create_bin_file - create sysfs binary attribute file for device. * @dev: device. * @attr: device binary attribute descriptor. */int device_create_bin_file(struct device *dev, struct bin_attribute *attr){ int error = -EINVAL; if (dev) error = sysfs_create_bin_file(&dev->kobj, attr); return error;}EXPORT_SYMBOL_GPL(device_create_bin_file); /** * device_remove_bin_file - remove sysfs binary attribute file * @dev: device. * @attr: device binary attribute descriptor. */void device_remove_bin_file(struct device *dev, struct bin_attribute *attr){ if (dev) sysfs_remove_bin_file(&dev->kobj, attr);}EXPORT_SYMBOL_GPL(device_remove_bin_file); /** * device_schedule_callback - helper to schedule a callback for a device * @dev: device. * @func: callback function to invoke later. * * Attribute methods must not unregister themselves or their parent device * (which would amount to the same thing). Attempts to do so will deadlock, * since unregistration is mutually exclusive with driver callbacks. * * Instead methods can call this routine, which will attempt to allocate * and schedule a workqueue request to call back @func with @dev as its * argument in the workqueue's process context. @dev will be pinned until * @func returns. * * Returns 0 if the request was submitted, -ENOMEM if storage could not * be allocated. * * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an * underlying sysfs routine (since it is intended for use by attribute * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. */int device_schedule_callback(struct device *dev, void (*func)(struct device *)){ return sysfs_schedule_callback(&dev->kobj, (void (*)(void *)) func, dev);}EXPORT_SYMBOL_GPL(device_schedule_callback); static void klist_children_get(struct klist_node *n){ struct device *dev = container_of(n, struct device, knode_parent); get_device(dev);} static void klist_children_put(struct klist_node *n){ struct device *dev = container_of(n, struct device, knode_parent); put_device(dev);} /** * device_initialize - init device structure. * @dev: device. * * This prepares the device for use by other layers, * including adding it to the device hierarchy. * It is the first half of device_register(), if called by * that, though it can also be called separately, so one * may use @dev's fields (e.g. the refcount). */ void device_initialize(struct device *dev){ kobj_set_kset_s(dev, devices_subsys); kobject_init(&dev->kobj); klist_init(&dev->klist_children, klist_children_get, klist_children_put); INIT_LIST_HEAD(&dev->dma_pools); INIT_LIST_HEAD(&dev->node); init_MUTEX(&dev->sem); spin_lock_init(&dev->devres_lock); INIT_LIST_HEAD(&dev->devres_head); device_init_wakeup(dev, 0); set_dev_node(dev, -1);} #ifdef CONFIG_SYSFS_DEPRECATEDstatic struct kobject * get_device_parent(struct device *dev, struct device *parent){ /* Set the parent to the class, not the parent device */ /* this keeps sysfs from having a symlink to make old udevs happy */ if (dev->class) return &dev->class->subsys.kset.kobj; else if (parent) return &parent->kobj; return NULL;}#elsestatic struct kobject * virtual_device_parent(struct device *dev){ if (!dev->class) return ERR_PTR(-ENODEV); if (!dev->class->virtual_dir) { static struct kobject *virtual_dir = NULL; if (!virtual_dir) virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual"); dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name); } return dev->class->virtual_dir;} static struct kobject * get_device_parent(struct device *dev, struct device *parent){ /* if this is a class device, and has no parent, create one */ if ((dev->class) && (parent == NULL)) {Lines 1–500 of 1185. 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 <asm/semaphore.h> #include "base.h" #include "power/power.h" int (*platform_notify)(struct device * dev) = NULL; int (*platform_notify_remove)(struct device * dev) = NULL; /* * sysfs bindings for devices. */ /** * 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(struct device *dev) { return dev->driver ? dev->driver->name : (dev->bus ? dev->bus->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); 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 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); 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 { printk(KERN_ERR "Device '%s' does not have a release() function, " "it is broken and must be fixed.\n", dev->bus_id); WARN_ON(1); } } static struct kobj_type ktype_device = { .release = device_release, .sysfs_ops = &dev_sysfs_ops, }; static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) { struct kobj_type *ktype = get_ktype(kobj); if (ktype == &ktype_device) { 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, char **envp, int num_envp, char *buffer, int buffer_size) { struct device *dev = to_dev(kobj); int i = 0; int length = 0; int retval = 0; /* add the major/minor if present */ if (MAJOR(dev->devt)) { add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "MAJOR=%u", MAJOR(dev->devt)); add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "MINOR=%u", MINOR(dev->devt)); } if (dev->driver) add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "DRIVER=%s", dev->driver->name); #ifdef CONFIG_SYSFS_DEPRECATED if (dev->class) { struct device *parent = dev->parent; /* find first bus device in parent chain */ while (parent && !parent->bus) parent = parent->parent; if (parent && parent->bus) { const char *path; path = kobject_get_path(&parent->kobj, GFP_KERNEL); add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVPATH=%s", path); kfree(path); add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVBUS=%s", parent->bus->name); if (parent->driver) add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVDRIVER=%s", parent->driver->name); } } else if (dev->bus) { add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVBUS=%s", dev->bus->name); if (dev->driver) add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length, "PHYSDEVDRIVER=%s", dev->driver->name); } #endif /* terminate, set to next free slot, shrink available space */ envp[i] = NULL; envp = &envp[i]; num_envp -= i; buffer = &buffer[length]; buffer_size -= length; if (dev->bus && dev->bus->uevent) { /* have the bus specific function add its stuff */ retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size); if (retval) pr_debug ("%s: bus uevent() returned %d\n", __FUNCTION__, retval); } if (dev->class && dev->class->dev_uevent) { /* have the class specific function add its stuff */ retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size); if (retval) pr_debug("%s: class uevent() returned %d\n", __FUNCTION__, retval); } if (dev->type && dev->type->uevent) { /* have the device type specific fuction add its stuff */ retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size); if (retval) pr_debug("%s: dev_type uevent() returned %d\n", __FUNCTION__, retval); } return retval; } static struct kset_uevent_ops device_uevent_ops = { .filter = dev_uevent_filter, .name = dev_uevent_name, .uevent = dev_uevent, }; static ssize_t store_uevent(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { kobject_uevent(&dev->kobj, KOBJ_ADD); return count; } static int device_add_groups(struct device *dev) { int i; int error = 0; if (dev->groups) { for (i = 0; dev->groups[i]; i++) { error = sysfs_create_group(&dev->kobj, dev->groups[i]); if (error) { while (--i >= 0) sysfs_remove_group(&dev->kobj, dev->groups[i]); goto out; } } } out: return error; } static void device_remove_groups(struct device *dev) { int i; if (dev->groups) { for (i = 0; dev->groups[i]; i++) { sysfs_remove_group(&dev->kobj, dev->groups[i]); } } } static int device_add_attrs(struct device *dev) { struct class *class = dev->class; struct device_type *type = dev->type; int error = 0; int i; if (class && class->dev_attrs) { for (i = 0; attr_name(class->dev_attrs[i]); i++) { error = device_create_file(dev, &class->dev_attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_file(dev, &class->dev_attrs[i]); } if (type && type->attrs) { for (i = 0; attr_name(type->attrs[i]); i++) { error = device_create_file(dev, &type->attrs[i]); if (error) break; } if (error) while (--i >= 0) device_remove_file(dev, &type->attrs[i]); } return error; } static void device_remove_attrs(struct device *dev) { struct class *class = dev->class; struct device_type *type = dev->type; int i; if (class && class->dev_attrs) { for (i = 0; attr_name(class->dev_attrs[i]); i++) device_remove_file(dev, &class->dev_attrs[i]); } if (type && type->attrs) { for (i = 0; attr_name(type->attrs[i]); i++) device_remove_file(dev, &type->attrs[i]); } } static ssize_t show_dev(struct device *dev, struct device_attribute *attr, char *buf) { return print_dev_t(buf, dev->devt); } /* * devices_subsys - structure to be registered with kobject core. */ decl_subsys(devices, &ktype_device, &device_uevent_ops); /** * device_create_file - create sysfs attribute file for device. * @dev: device. * @attr: device attribute descriptor. */ int device_create_file(struct device * dev, struct device_attribute * attr) { int error = 0; if (get_device(dev)) { error = sysfs_create_file(&dev->kobj, &attr->attr); put_device(dev); } return error; } /** * device_remove_file - remove sysfs attribute file. * @dev: device. * @attr: device attribute descriptor. */ void device_remove_file(struct device * dev, struct device_attribute * attr) { if (get_device(dev)) { sysfs_remove_file(&dev->kobj, &attr->attr); put_device(dev); } } /** * device_create_bin_file - create sysfs binary attribute file for device. * @dev: device. * @attr: device binary attribute descriptor. */ int device_create_bin_file(struct device *dev, struct bin_attribute *attr) { int error = -EINVAL; if (dev) error = sysfs_create_bin_file(&dev->kobj, attr); return error; } EXPORT_SYMBOL_GPL(device_create_bin_file); /** * device_remove_bin_file - remove sysfs binary attribute file * @dev: device. * @attr: device binary attribute descriptor. */ void device_remove_bin_file(struct device *dev, struct bin_attribute *attr) { if (dev) sysfs_remove_bin_file(&dev->kobj, attr); } EXPORT_SYMBOL_GPL(device_remove_bin_file); /** * device_schedule_callback - helper to schedule a callback for a device * @dev: device. * @func: callback function to invoke later. * * Attribute methods must not unregister themselves or their parent device * (which would amount to the same thing). Attempts to do so will deadlock, * since unregistration is mutually exclusive with driver callbacks. * * Instead methods can call this routine, which will attempt to allocate * and schedule a workqueue request to call back @func with @dev as its * argument in the workqueue's process context. @dev will be pinned until * @func returns. * * Returns 0 if the request was submitted, -ENOMEM if storage could not * be allocated. * * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an * underlying sysfs routine (since it is intended for use by attribute * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. */ int device_schedule_callback(struct device *dev, void (*func)(struct device *)) { return sysfs_schedule_callback(&dev->kobj, (void (*)(void *)) func, dev); } EXPORT_SYMBOL_GPL(device_schedule_callback); static void klist_children_get(struct klist_node *n) { struct device *dev = container_of(n, struct device, knode_parent); get_device(dev); } static void klist_children_put(struct klist_node *n) { struct device *dev = container_of(n, struct device, knode_parent); put_device(dev); } /** * device_initialize - init device structure. * @dev: device. * * This prepares the device for use by other layers, * including adding it to the device hierarchy. * It is the first half of device_register(), if called by * that, though it can also be called separately, so one * may use @dev's fields (e.g. the refcount). */ void device_initialize(struct device *dev) { kobj_set_kset_s(dev, devices_subsys); kobject_init(&dev->kobj); klist_init(&dev->klist_children, klist_children_get, klist_children_put); INIT_LIST_HEAD(&dev->dma_pools); INIT_LIST_HEAD(&dev->node); init_MUTEX(&dev->sem); spin_lock_init(&dev->devres_lock); INIT_LIST_HEAD(&dev->devres_head); device_init_wakeup(dev, 0); set_dev_node(dev, -1); } #ifdef CONFIG_SYSFS_DEPRECATED static struct kobject * get_device_parent(struct device *dev, struct device *parent) { /* Set the parent to the class, not the parent device */ /* this keeps sysfs from having a symlink to make old udevs happy */ if (dev->class) return &dev->class->subsys.kset.kobj; else if (parent) return &parent->kobj; return NULL; } #else static struct kobject * virtual_device_parent(struct device *dev) { if (!dev->class) return ERR_PTR(-ENODEV); if (!dev->class->virtual_dir) { static struct kobject *virtual_dir = NULL; if (!virtual_dir) virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual"); dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name); } return dev->class->virtual_dir; } static struct kobject * get_device_parent(struct device *dev, struct device *parent) { /* if this is a class device, and has no parent, create one */ if ((dev->class) && (parent == NULL)) {