aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/driver-model
diff options
context:
space:
mode:
authorBart Van Assche <bvanassche@acm.org>2011-08-23 13:27:27 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2011-08-24 19:01:24 -0400
commitd58cb9cc8a46c9e539cbf8007cd5ede3c09c3524 (patch)
tree39f10b718e1d084752dc97608c481509e06d52fc /Documentation/driver-model
parentebf4127cd677e9781b450e44dfaaa1cc595efcaa (diff)
docs/driver-model: Document device.groups
Several drivers use device_create_file() where device.groups should be used instead. This patch documents that and also removes the comments about device classes since these should not be used in new code in the way documented until now in Documentation/driver-model/device.txt. Signed-off-by: Bart Van Assche <bvanassche@acm.org> Cc: Randy Dunlap <rdunlap@xenotime.net> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'Documentation/driver-model')
-rw-r--r--Documentation/driver-model/device.txt65
1 files changed, 32 insertions, 33 deletions
diff --git a/Documentation/driver-model/device.txt b/Documentation/driver-model/device.txt
index bdefe728a737..1e70220d20f4 100644
--- a/Documentation/driver-model/device.txt
+++ b/Documentation/driver-model/device.txt
@@ -45,33 +45,52 @@ struct device_attribute {
45 const char *buf, size_t count); 45 const char *buf, size_t count);
46}; 46};
47 47
48Attributes of devices can be exported via drivers using a simple 48Attributes of devices can be exported by a device driver through sysfs.
49procfs-like interface.
50 49
51Please see Documentation/filesystems/sysfs.txt for more information 50Please see Documentation/filesystems/sysfs.txt for more information
52on how sysfs works. 51on how sysfs works.
53 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
54Attributes are declared using a macro called DEVICE_ATTR: 57Attributes are declared using a macro called DEVICE_ATTR:
55 58
56#define DEVICE_ATTR(name,mode,show,store) 59#define DEVICE_ATTR(name,mode,show,store)
57 60
58Example: 61Example:
59 62
60DEVICE_ATTR(power,0644,show_power,store_power); 63static DEVICE_ATTR(type, 0444, show_type, NULL);
64static DEVICE_ATTR(power, 0644, show_power, store_power);
61 65
62This declares a structure of type struct device_attribute named 66This declares two structures of type struct device_attribute with respective
63'dev_attr_power'. This can then be added and removed to the device's 67names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be
64directory using: 68organized as follows into a group:
65 69
66int device_create_file(struct device *device, struct device_attribute * entry); 70static struct attribute *dev_attrs[] = {
67void device_remove_file(struct device * dev, struct device_attribute * attr); 71 &dev_attr_type.attr,
72 &dev_attr_power.attr,
73 NULL,
74};
68 75
69Example: 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:
70 87
71device_create_file(dev,&dev_attr_power); 88 dev->groups = dev_attr_groups;
72device_remove_file(dev,&dev_attr_power); 89 device_register(dev);
73 90
74The file name will be 'power' with a mode of 0644 (-rw-r--r--). 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.
75 94
76Word of warning: While the kernel allows device_create_file() and 95Word of warning: While the kernel allows device_create_file() and
77device_remove_file() to be called on a device at any time, userspace has 96device_remove_file() to be called on a device at any time, userspace has
@@ -84,24 +103,4 @@ not know about the new attributes.
84This is important for device driver that need to publish additional 103This is important for device driver that need to publish additional
85attributes for a device at driver probe time. If the device driver simply 104attributes for a device at driver probe time. If the device driver simply
86calls device_create_file() on the device structure passed to it, then 105calls device_create_file() on the device structure passed to it, then
87userspace will never be notified of the new attributes. Instead, it should 106userspace will never be notified of the new attributes.
88probably use class_create() and class->dev_attrs to set up a list of
89desired attributes in the modules_init function, and then in the .probe()
90hook, and then use device_create() to create a new device as a child
91of the probed device. The new device will generate a new uevent and
92properly advertise the new attributes to userspace.
93
94For example, if a driver wanted to add the following attributes:
95struct device_attribute mydriver_attribs[] = {
96 __ATTR(port_count, 0444, port_count_show),
97 __ATTR(serial_number, 0444, serial_number_show),
98 NULL
99};
100
101Then in the module init function is would do:
102 mydriver_class = class_create(THIS_MODULE, "my_attrs");
103 mydriver_class.dev_attr = mydriver_attribs;
104
105And assuming 'dev' is the struct device passed into the probe hook, the driver
106probe function would do something like:
107 device_create(&mydriver_class, dev, chrdev, &private_data, "my_name");