Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Functions | Variables
core.c File Reference
#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/of.h>
#include <linux/of_device.h>
#include <linux/genhd.h>
#include <linux/kallsyms.h>
#include <linux/mutex.h>
#include <linux/async.h>
#include <linux/pm_runtime.h>
#include <linux/netdevice.h>
#include "base.h"
#include "power/power.h"

Go to the source code of this file.

Data Structures

struct  class_dir
 
struct  root_device
 

Macros

#define to_dev_attr(_attr)   container_of(_attr, struct device_attribute, attr)
 
#define to_ext_attr(x)   container_of(x, struct dev_ext_attribute, attr)
 
#define to_class_dir(obj)   container_of(obj, struct class_dir, kobj)
 

Functions

const chardev_driver_string (const struct device *dev)
 
 EXPORT_SYMBOL (dev_driver_string)
 
ssize_t device_store_ulong (struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
 
 EXPORT_SYMBOL_GPL (device_store_ulong)
 
ssize_t device_show_ulong (struct device *dev, struct device_attribute *attr, char *buf)
 
 EXPORT_SYMBOL_GPL (device_show_ulong)
 
ssize_t device_store_int (struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
 
 EXPORT_SYMBOL_GPL (device_store_int)
 
ssize_t device_show_int (struct device *dev, struct device_attribute *attr, char *buf)
 
 EXPORT_SYMBOL_GPL (device_show_int)
 
int device_create_file (struct device *dev, const struct device_attribute *attr)
 
void device_remove_file (struct device *dev, const struct device_attribute *attr)
 
int device_create_bin_file (struct device *dev, const struct bin_attribute *attr)
 
 EXPORT_SYMBOL_GPL (device_create_bin_file)
 
void device_remove_bin_file (struct device *dev, const struct bin_attribute *attr)
 
 EXPORT_SYMBOL_GPL (device_remove_bin_file)
 
int device_schedule_callback_owner (struct device *dev, void(*func)(struct device *), struct module *owner)
 
 EXPORT_SYMBOL_GPL (device_schedule_callback_owner)
 
void device_initialize (struct device *dev)
 
int dev_set_name (struct device *dev, const char *fmt,...)
 
 EXPORT_SYMBOL_GPL (dev_set_name)
 
int device_private_init (struct device *dev)
 
int device_add (struct device *dev)
 
int device_register (struct device *dev)
 
struct deviceget_device (struct device *dev)
 
void put_device (struct device *dev)
 
void device_del (struct device *dev)
 
void device_unregister (struct device *dev)
 
const chardevice_get_devnode (struct device *dev, umode_t *mode, const char **tmp)
 
int device_for_each_child (struct device *parent, void *data, int(*fn)(struct device *dev, void *data))
 
struct devicedevice_find_child (struct device *parent, void *data, int(*match)(struct device *dev, void *data))
 
int __init devices_init (void)
 
 EXPORT_SYMBOL_GPL (device_for_each_child)
 
 EXPORT_SYMBOL_GPL (device_find_child)
 
 EXPORT_SYMBOL_GPL (device_initialize)
 
 EXPORT_SYMBOL_GPL (device_add)
 
 EXPORT_SYMBOL_GPL (device_register)
 
 EXPORT_SYMBOL_GPL (device_del)
 
 EXPORT_SYMBOL_GPL (device_unregister)
 
 EXPORT_SYMBOL_GPL (get_device)
 
 EXPORT_SYMBOL_GPL (put_device)
 
 EXPORT_SYMBOL_GPL (device_create_file)
 
 EXPORT_SYMBOL_GPL (device_remove_file)
 
struct root_deviceto_root_device (struct device *d)
 
: root device name

__root_device_register - allocate and register a root device

: owner module of the root device, usually THIS_MODULE

This function allocates a root device and registers it using device_register(). In order to free the returned device, use root_device_unregister().

Root devices are dummy devices which allow other devices to be grouped under /sys/devices. Use this function to allocate a root device and then use it as the parent of any device which should appear under /sys/devices/{name}

The /sys/devices/{name} directory will also contain a 'module' symlink which points to the directory in sysfs.

Returns &struct device pointer on success, or ERR_PTR() on error.

Note: You probably want to use root_device_register().

struct device__root_device_register (const char *name, struct module *owner)
 
 EXPORT_SYMBOL_GPL (__root_device_register)
 
void root_device_unregister (struct device *dev)
 
 EXPORT_SYMBOL_GPL (root_device_unregister)
 
struct devicedevice_create_vargs (struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, va_list args)
 
 EXPORT_SYMBOL_GPL (device_create_vargs)
 
struct devicedevice_create (struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt,...)
 
 EXPORT_SYMBOL_GPL (device_create)
 
void device_destroy (struct class *class, dev_t devt)
 
 EXPORT_SYMBOL_GPL (device_destroy)
 
int device_rename (struct device *dev, const char *new_name)
 
 EXPORT_SYMBOL_GPL (device_rename)
 
int device_move (struct device *dev, struct device *new_parent, enum dpm_order dpm_order)
 
 EXPORT_SYMBOL_GPL (device_move)
 
void device_shutdown (void)
 

Variables

int(* platform_notify )(struct device *dev) = NULL
 
int(* platform_notify_remove )(struct device *dev) = NULL
 
struct kobjectsysfs_dev_char_kobj
 
struct kobjectsysfs_dev_block_kobj
 
struct ksetdevices_kset
 

Macro Definition Documentation

#define to_class_dir (   obj)    container_of(obj, struct class_dir, kobj)

Definition at line 688 of file core.c.

#define to_dev_attr (   _attr)    container_of(_attr, struct device_attribute, attr)

Definition at line 88 of file core.c.

#define to_ext_attr (   x)    container_of(x, struct dev_ext_attribute, attr)

Definition at line 123 of file core.c.

Function Documentation

struct device* __root_device_register ( const char name,
struct module owner 
)
read

Definition at line 1434 of file core.c.

const char* dev_driver_string ( const struct device dev)

dev_driver_string - Return a device's driver name, if at all possible : 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 driver, 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.

Definition at line 73 of file core.c.

int dev_set_name ( struct device dev,
const char fmt,
  ... 
)

dev_set_name - set a device name : device : format string for the device's name

Definition at line 872 of file core.c.

int device_add ( struct device dev)

device_add - add device to device hierarchy. : device.

This is part 2 of device_register(), though may be called separately iff device_initialize() has been called separately.

This adds to the kobject hierarchy via kobject_add(), adds it to the global and sibling lists for the device, then adds it to the other relevant subsystems of the driver model.

Do not call this routine or device_register() more than once for any device structure. The driver model core is not designed to work with devices that get unregistered and then spring back to life. (Among other things, it's very hard to guarantee that all references to the previous incarnation of have been dropped.) Allocate and register a fresh new struct device instead.

NOTE: Never directly free after calling this function, even if it returned an error! Always use put_device() to give up your reference instead.

Definition at line 966 of file core.c.

struct device* device_create ( struct class class,
struct device parent,
dev_t  devt,
void drvdata,
const char fmt,
  ... 
)
read

Definition at line 1586 of file core.c.

int device_create_bin_file ( struct device dev,
const struct bin_attribute attr 
)

device_create_bin_file - create sysfs binary attribute file for device. : device. : device binary attribute descriptor.

Definition at line 567 of file core.c.

int device_create_file ( struct device dev,
const struct device_attribute attr 
)

device_create_file - create sysfs attribute file for device. : device. : device attribute descriptor.

Definition at line 541 of file core.c.

struct device* device_create_vargs ( struct class class,
struct device parent,
dev_t  devt,
void drvdata,
const char fmt,
va_list  args 
)
read

Definition at line 1524 of file core.c.

void device_del ( struct device dev)

device_del - delete device from system. : device.

This is the first part of the device unregistration sequence. This removes the device from the lists we control from here, has it removed from the other driver model subsystems it was added to in device_add(), and removes it from the kobject hierarchy.

NOTE: this should be called manually iff device_add() was also called manually.

Definition at line 1172 of file core.c.

void device_destroy ( struct class class,
dev_t  devt 
)

Definition at line 1614 of file core.c.

struct device* device_find_child ( struct device parent,
void data,
int(*)(struct device *dev, void *data match 
)
read

device_find_child - device iterator for locating a particular device. : parent struct device : Data to pass to match function : Callback function to check device

This is similar to the device_for_each_child() function above, but it returns a reference to a device that is 'found' for later use, as determined by the callback.

The callback should return 0 if the device doesn't match and non-zero if it does. If the callback returns non-zero and a reference to the current device can be obtained, this function will return to the caller and not iterate over any more devices.

Definition at line 1339 of file core.c.

int device_for_each_child ( struct device parent,
void data,
int(*)(struct device *dev, void *data fn 
)

Definition at line 1307 of file core.c.

const char* device_get_devnode ( struct device dev,
umode_t mode,
const char **  tmp 
)

device_get_devnode - path of device node file : device : returned file access mode : possibly allocated string

Return the relative path of a possible device node. Non-default names may need to allocate a memory to compose a name. This memory is returned in tmp and needs to be freed by the caller.

Definition at line 1263 of file core.c.

void device_initialize ( struct device dev)

device_initialize - init device structure. : device.

This prepares the device for use by other layers by initializing its fields. It is the first half of device_register(), if called by that function, though it can also be called separately, so one may use 's fields. In particular, get_device()/put_device() may be used for reference counting of after calling this function.

All fields in must be initialized by the caller to 0, except for those explicitly set to some other value. The simplest approach is to use kzalloc() to allocate the structure containing .

NOTE: Use put_device() to give up your reference instead of freeing directly once you have called this function.

Definition at line 659 of file core.c.

int device_move ( struct device dev,
struct device new_parent,
enum dpm_order dpm_order   
)

device_move - moves a device to a new parent : the pointer to the struct device to be moved : the new parent of the device (can by NULL) : how to reorder the dpm_list

Definition at line 1727 of file core.c.

int device_private_init ( struct device dev)

Definition at line 932 of file core.c.

int device_register ( struct device dev)

device_register - register a device with the system. : pointer to the device structure

This happens in two clean steps - initialize the device and add it to the system. The two steps can be called separately, but this is the easiest and most common. I.e. you should only call the two helpers separately if have a clearly defined need to use and refcount the device before it is added to the hierarchy.

For more information, see the kerneldoc for device_initialize() and device_add().

NOTE: Never directly free after calling this function, even if it returned an error! Always use put_device() to give up the reference initialized in this function instead.

Definition at line 1129 of file core.c.

void device_remove_bin_file ( struct device dev,
const struct bin_attribute attr 
)

device_remove_bin_file - remove sysfs binary attribute file : device. : device binary attribute descriptor.

Definition at line 582 of file core.c.

void device_remove_file ( struct device dev,
const struct device_attribute attr 
)

device_remove_file - remove sysfs attribute file. : device. : device attribute descriptor.

Definition at line 555 of file core.c.

int device_rename ( struct device dev,
const char new_name 
)

device_rename - renames a device : the pointer to the struct device to be renamed : the new name of the device

It is the responsibility of the caller to provide mutual exclusion between two different calls of device_rename on the same device to ensure that new_name is valid and won't conflict with other devices.

Note: Don't call this function. Currently, the networking layer calls this function, but that will change. The following text from Kay Sievers offers some insight:

Renaming devices is racy at many levels, symlinks and other stuff are not replaced atomically, and you get a "move" uevent, but it's not easy to connect the event to the old and new device. Device nodes are not renamed at all, there isn't even support for that in the kernel now.

In the meantime, during renaming, your target name might be taken by another driver, creating conflicts. Or the old name is taken directly after you renamed it – then you get events for the same DEVPATH, before you even see the "move" event. It's just a mess, and nothing new should ever rely on kernel device renaming. Besides that, it's not even implemented now for other things than (driver-core wise very simple) network devices.

We are currently about to change network renaming in udev to completely disallow renaming of devices in the same namespace as the kernel uses, because we can't solve the problems properly, that arise with swapping names of multiple interfaces without races. Means, renaming of eth[0-9]* will only be allowed to some other name than eth[0-9]*, for the aforementioned reasons.

Make up a "real" name in the driver before you register anything, or add some other attributes for userspace to find the device, or use udev to add symlinks – but never rename kernel devices later, it's a complete mess. We don't even want to get into that and try to implement the missing pieces in the core. We really have other pieces to fix in the driver core mess. :)

Definition at line 1665 of file core.c.

int device_schedule_callback_owner ( struct device dev,
void(*)(struct device *)  func,
struct module owner 
)

device_schedule_callback_owner - helper to schedule a callback for a device : device. : callback function to invoke later. : 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 with as its argument in the workqueue's process context. will be pinned until returns.

This routine is usually called via the inline device_schedule_callback(), which automatically sets to THIS_MODULE.

Returns 0 if the request was submitted, -ENOMEM if storage could not be allocated, -ENODEV if a reference to 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.

Definition at line 615 of file core.c.

ssize_t device_show_int ( struct device dev,
struct device_attribute attr,
char buf 
)

Definition at line 164 of file core.c.

ssize_t device_show_ulong ( struct device dev,
struct device_attribute attr,
char buf 
)

Definition at line 140 of file core.c.

void device_shutdown ( void  )

device_shutdown - call ->shutdown() on each device to shutdown.

Definition at line 1805 of file core.c.

ssize_t device_store_int ( struct device dev,
struct device_attribute attr,
const char buf,
size_t  size 
)

Definition at line 149 of file core.c.

ssize_t device_store_ulong ( struct device dev,
struct device_attribute attr,
const char buf,
size_t  size 
)

Definition at line 125 of file core.c.

void device_unregister ( struct device dev)

device_unregister - unregister device from system. : device going away.

We do this in two parts, like we do device_register(). First, we remove it from all the subsystems with device_del(), then we decrement the reference count via put_device(). If that is the final reference count, the device will be cleaned up via device_release() above. Otherwise, the structure will stick around until the final reference to the device is dropped.

Definition at line 1232 of file core.c.

int __init devices_init ( void  )

Definition at line 1356 of file core.c.

EXPORT_SYMBOL ( dev_driver_string  )
EXPORT_SYMBOL_GPL ( device_store_ulong  )
EXPORT_SYMBOL_GPL ( device_show_ulong  )
EXPORT_SYMBOL_GPL ( device_store_int  )
EXPORT_SYMBOL_GPL ( device_show_int  )
EXPORT_SYMBOL_GPL ( device_create_bin_file  )
EXPORT_SYMBOL_GPL ( device_remove_bin_file  )
EXPORT_SYMBOL_GPL ( device_schedule_callback_owner  )
EXPORT_SYMBOL_GPL ( dev_set_name  )
EXPORT_SYMBOL_GPL ( device_for_each_child  )
EXPORT_SYMBOL_GPL ( device_find_child  )
EXPORT_SYMBOL_GPL ( device_initialize  )
EXPORT_SYMBOL_GPL ( device_add  )
EXPORT_SYMBOL_GPL ( device_register  )
EXPORT_SYMBOL_GPL ( device_del  )
EXPORT_SYMBOL_GPL ( device_unregister  )
EXPORT_SYMBOL_GPL ( get_device  )
EXPORT_SYMBOL_GPL ( put_device  )
EXPORT_SYMBOL_GPL ( device_create_file  )
EXPORT_SYMBOL_GPL ( device_remove_file  )
EXPORT_SYMBOL_GPL ( __root_device_register  )
EXPORT_SYMBOL_GPL ( root_device_unregister  )
EXPORT_SYMBOL_GPL ( device_create_vargs  )
EXPORT_SYMBOL_GPL ( device_create  )
EXPORT_SYMBOL_GPL ( device_destroy  )
EXPORT_SYMBOL_GPL ( device_rename  )
EXPORT_SYMBOL_GPL ( device_move  )
struct device* get_device ( struct device dev)
read

get_device - increment reference count for device. : device.

This simply forwards the call to kobject_get(), though we do take care to provide for the case that we get a NULL pointer passed in.

Definition at line 1143 of file core.c.

void put_device ( struct device dev)

put_device - decrement reference count. : device in question.

Definition at line 1152 of file core.c.

void root_device_unregister ( struct device dev)

root_device_unregister - unregister and free a root device : device going away

This function unregisters and cleans up a device that was created by root_device_register().

Definition at line 1481 of file core.c.

struct root_device* to_root_device ( struct device d)
read

Definition at line 1402 of file core.c.

Variable Documentation

struct kset* devices_kset

Definition at line 534 of file core.c.

int(* platform_notify)(struct device *dev) = NULL

Definition at line 46 of file core.c.

int(* platform_notify_remove)(struct device *dev) = NULL

Definition at line 47 of file core.c.

struct kobject* sysfs_dev_block_kobj

Definition at line 50 of file core.c.

struct kobject* sysfs_dev_char_kobj

Definition at line 49 of file core.c.