1450 lines · 36.8 KB · C
RawDownload
/* * 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/semaphore.h> #include "base.h"#include "power/power.h" int (*platform_notify)(struct device *dev) = NULL;int (*platform_notify_remove)(struct device *dev) = NULL; #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(struct device *dev){	return dev->driver ? dev->driver->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 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 device_ktype = {	.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 == &device_ktype) {		struct device *dev = to_dev(kobj);		if (dev->uevent_suppress)			return 0;		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 the major/minor if present */	if (MAJOR(dev->devt)) {		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));	} 	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); #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);			if (path) {				add_uevent_var(env, "PHYSDEVPATH=%s", path);				kfree(path);			} 			add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name); 			if (parent->driver)				add_uevent_var(env, "PHYSDEVDRIVER=%s",					       parent->driver->name);		}	} else if (dev->bus) {		add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name); 		if (dev->driver)			add_uevent_var(env, "PHYSDEVDRIVER=%s",				       dev->driver->name);	}#endif 	/* 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->bus_id, __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->bus_id,				 __func__, retval);	} 	/* have the device type specific fuction 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->bus_id,				 __func__, 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 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);		goto out;	} 	dev_err(dev, "uevent: unsupported action-string; this will "		     "be ignored in a future kernel version\n");	kobject_uevent(&dev->kobj, KOBJ_ADD);out:	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_groups(struct device *dev,			     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,				 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;	struct device_type *type = dev->type;	int error; 	if (class) {		error = device_add_attributes(dev, class->dev_attrs);		if (error)			return error;	} 	if (type) {		error = device_add_groups(dev, type->groups);		if (error)			goto err_remove_class_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_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;	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);}  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, 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, 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. */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_owner - helper to schedule a callback for a device * @dev: device. * @func: callback function to invoke later. * @owner: module owning the callback routine * * 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. * * This routine is usually called via the inline device_schedule_callback(), * which automatically sets @owner to THIS_MODULE. * * Returns 0 if the request was submitted, -ENOMEM if storage could not * be allocated, -ENODEV if a reference to @owner isn't available. * * 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_owner(struct device *dev,		void (*func)(struct device *), struct module *owner){

Lines 1–500 of 1450.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t