diff options
Diffstat (limited to 'Documentation/driver-model/device.txt')
-rw-r--r-- | Documentation/driver-model/device.txt | 65 |
1 files changed, 32 insertions, 33 deletions
diff --git a/Documentation/driver-model/device.txt b/Documentation/driver-model/device.txt index bdefe728a73..1e70220d20f 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 | ||
48 | Attributes of devices can be exported via drivers using a simple | 48 | Attributes of devices can be exported by a device driver through sysfs. |
49 | procfs-like interface. | ||
50 | 49 | ||
51 | Please see Documentation/filesystems/sysfs.txt for more information | 50 | Please see Documentation/filesystems/sysfs.txt for more information |
52 | on how sysfs works. | 51 | on how sysfs works. |
53 | 52 | ||
53 | As explained in Documentation/kobject.txt, device attributes must be be | ||
54 | created before the KOBJ_ADD uevent is generated. The only way to realize | ||
55 | that is by defining an attribute group. | ||
56 | |||
54 | Attributes are declared using a macro called DEVICE_ATTR: | 57 | Attributes 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 | ||
58 | Example: | 61 | Example: |
59 | 62 | ||
60 | DEVICE_ATTR(power,0644,show_power,store_power); | 63 | static DEVICE_ATTR(type, 0444, show_type, NULL); |
64 | static DEVICE_ATTR(power, 0644, show_power, store_power); | ||
61 | 65 | ||
62 | This declares a structure of type struct device_attribute named | 66 | This 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 | 67 | names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be |
64 | directory using: | 68 | organized as follows into a group: |
65 | 69 | ||
66 | int device_create_file(struct device *device, struct device_attribute * entry); | 70 | static struct attribute *dev_attrs[] = { |
67 | void 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 | ||
69 | Example: | 76 | static struct attribute_group dev_attr_group = { |
77 | .attrs = dev_attrs, | ||
78 | }; | ||
79 | |||
80 | static const struct attribute_group *dev_attr_groups[] = { | ||
81 | &dev_attr_group, | ||
82 | NULL, | ||
83 | }; | ||
84 | |||
85 | This array of groups can then be associated with a device by setting the | ||
86 | group pointer in struct device before device_register() is invoked: | ||
70 | 87 | ||
71 | device_create_file(dev,&dev_attr_power); | 88 | dev->groups = dev_attr_groups; |
72 | device_remove_file(dev,&dev_attr_power); | 89 | device_register(dev); |
73 | 90 | ||
74 | The file name will be 'power' with a mode of 0644 (-rw-r--r--). | 91 | The device_register() function will use the 'groups' pointer to create the |
92 | device attributes and the device_unregister() function will use this pointer | ||
93 | to remove the device attributes. | ||
75 | 94 | ||
76 | Word of warning: While the kernel allows device_create_file() and | 95 | Word of warning: While the kernel allows device_create_file() and |
77 | device_remove_file() to be called on a device at any time, userspace has | 96 | device_remove_file() to be called on a device at any time, userspace has |
@@ -84,24 +103,4 @@ not know about the new attributes. | |||
84 | This is important for device driver that need to publish additional | 103 | This is important for device driver that need to publish additional |
85 | attributes for a device at driver probe time. If the device driver simply | 104 | attributes for a device at driver probe time. If the device driver simply |
86 | calls device_create_file() on the device structure passed to it, then | 105 | calls device_create_file() on the device structure passed to it, then |
87 | userspace will never be notified of the new attributes. Instead, it should | 106 | userspace will never be notified of the new attributes. |
88 | probably use class_create() and class->dev_attrs to set up a list of | ||
89 | desired attributes in the modules_init function, and then in the .probe() | ||
90 | hook, and then use device_create() to create a new device as a child | ||
91 | of the probed device. The new device will generate a new uevent and | ||
92 | properly advertise the new attributes to userspace. | ||
93 | |||
94 | For example, if a driver wanted to add the following attributes: | ||
95 | struct device_attribute mydriver_attribs[] = { | ||
96 | __ATTR(port_count, 0444, port_count_show), | ||
97 | __ATTR(serial_number, 0444, serial_number_show), | ||
98 | NULL | ||
99 | }; | ||
100 | |||
101 | Then 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 | |||
105 | And assuming 'dev' is the struct device passed into the probe hook, the driver | ||
106 | probe function would do something like: | ||
107 | device_create(&mydriver_class, dev, chrdev, &private_data, "my_name"); | ||