1
2The Basic Device Structure
3~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5See the kerneldoc for the struct device.
6
7
8Programming Interface
9~~~~~~~~~~~~~~~~~~~~~
10The bus driver that discovers the device uses this to register the
11device with the core:
12
13int device_register(struct device * dev);
14
15The bus should initialize the following fields:
16
17    - parent
18    - name
19    - bus_id
20    - bus
21
22A device is removed from the core when its reference count goes to
230. The reference count can be adjusted using:
24
25struct device * get_device(struct device * dev);
26void put_device(struct device * dev);
27
28get_device() will return a pointer to the struct device passed to it
29if the reference is not already 0 (if it's in the process of being
30removed already).
31
32A driver can access the lock in the device structure using: 
33
34void lock_device(struct device * dev);
35void unlock_device(struct device * dev);
36
37
38Attributes
39~~~~~~~~~~
40struct device_attribute {
41	struct attribute	attr;
42	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
43			char *buf);
44	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
45			 const char *buf, size_t count);
46};
47
48Attributes of devices can be exported by a device driver through sysfs.
49
50Please see Documentation/filesystems/sysfs.txt for more information
51on how sysfs works.
52
53As explained in Documentation/kobject.txt, device attributes must be be
54created before the KOBJ_ADD uevent is generated. The only way to realize
55that is by defining an attribute group.
56
57Attributes are declared using a macro called DEVICE_ATTR:
58
59#define DEVICE_ATTR(name,mode,show,store)
60
61Example:
62
63static DEVICE_ATTR(type, 0444, show_type, NULL);
64static DEVICE_ATTR(power, 0644, show_power, store_power);
65
66This declares two structures of type struct device_attribute with respective
67names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be
68organized as follows into a group:
69
70static struct attribute *dev_attrs[] = {
71	&dev_attr_type.attr,
72	&dev_attr_power.attr,
73	NULL,
74};
75
76static struct attribute_group dev_attr_group = {
77	.attrs = dev_attrs,
78};
79
80static const struct attribute_group *dev_attr_groups[] = {
81	&dev_attr_group,
82	NULL,
83};
84
85This array of groups can then be associated with a device by setting the
86group pointer in struct device before device_register() is invoked:
87
88      dev->groups = dev_attr_groups;
89      device_register(dev);
90
91The device_register() function will use the 'groups' pointer to create the
92device attributes and the device_unregister() function will use this pointer
93to remove the device attributes.
94
95Word of warning:  While the kernel allows device_create_file() and
96device_remove_file() to be called on a device at any time, userspace has
97strict expectations on when attributes get created.  When a new device is
98registered in the kernel, a uevent is generated to notify userspace (like
99udev) that a new device is available.  If attributes are added after the
100device is registered, then userspace won't get notified and userspace will
101not know about the new attributes.
102
103This is important for device driver that need to publish additional
104attributes for a device at driver probe time.  If the device driver simply
105calls device_create_file() on the device structure passed to it, then
106userspace will never be notified of the new attributes.
107