Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Bart Van Assche and committed by
Greg Kroah-Hartman
d58cb9cc ebf4127c

+32 -33
+32 -33
Documentation/driver-model/device.txt
··· 45 45 const char *buf, size_t count); 46 46 }; 47 47 48 - Attributes of devices can be exported via drivers using a simple 49 - procfs-like interface. 48 + Attributes of devices can be exported by a device driver through sysfs. 50 49 51 50 Please see Documentation/filesystems/sysfs.txt for more information 52 51 on how sysfs works. 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. 53 56 54 57 Attributes are declared using a macro called DEVICE_ATTR: 55 58 ··· 60 57 61 58 Example: 62 59 63 - DEVICE_ATTR(power,0644,show_power,store_power); 60 + static DEVICE_ATTR(type, 0444, show_type, NULL); 61 + static DEVICE_ATTR(power, 0644, show_power, store_power); 64 62 65 - This declares a structure of type struct device_attribute named 66 - 'dev_attr_power'. This can then be added and removed to the device's 67 - directory using: 63 + This declares two structures of type struct device_attribute with respective 64 + names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be 65 + organized as follows into a group: 68 66 69 - int device_create_file(struct device *device, struct device_attribute * entry); 70 - void device_remove_file(struct device * dev, struct device_attribute * attr); 67 + static struct attribute *dev_attrs[] = { 68 + &dev_attr_type.attr, 69 + &dev_attr_power.attr, 70 + NULL, 71 + }; 71 72 72 - Example: 73 + static struct attribute_group dev_attr_group = { 74 + .attrs = dev_attrs, 75 + }; 73 76 74 - device_create_file(dev,&dev_attr_power); 75 - device_remove_file(dev,&dev_attr_power); 77 + static const struct attribute_group *dev_attr_groups[] = { 78 + &dev_attr_group, 79 + NULL, 80 + }; 76 81 77 - The file name will be 'power' with a mode of 0644 (-rw-r--r--). 82 + This array of groups can then be associated with a device by setting the 83 + group pointer in struct device before device_register() is invoked: 84 + 85 + dev->groups = dev_attr_groups; 86 + device_register(dev); 87 + 88 + The device_register() function will use the 'groups' pointer to create the 89 + device attributes and the device_unregister() function will use this pointer 90 + to remove the device attributes. 78 91 79 92 Word of warning: While the kernel allows device_create_file() and 80 93 device_remove_file() to be called on a device at any time, userspace has ··· 103 84 This is important for device driver that need to publish additional 104 85 attributes for a device at driver probe time. If the device driver simply 105 86 calls device_create_file() on the device structure passed to it, then 106 - userspace will never be notified of the new attributes. Instead, it should 107 - probably use class_create() and class->dev_attrs to set up a list of 108 - desired attributes in the modules_init function, and then in the .probe() 109 - hook, and then use device_create() to create a new device as a child 110 - of the probed device. The new device will generate a new uevent and 111 - properly advertise the new attributes to userspace. 112 - 113 - For example, if a driver wanted to add the following attributes: 114 - struct device_attribute mydriver_attribs[] = { 115 - __ATTR(port_count, 0444, port_count_show), 116 - __ATTR(serial_number, 0444, serial_number_show), 117 - NULL 118 - }; 119 - 120 - Then in the module init function is would do: 121 - mydriver_class = class_create(THIS_MODULE, "my_attrs"); 122 - mydriver_class.dev_attr = mydriver_attribs; 123 - 124 - And assuming 'dev' is the struct device passed into the probe hook, the driver 125 - probe function would do something like: 126 - device_create(&mydriver_class, dev, chrdev, &private_data, "my_name"); 87 + userspace will never be notified of the new attributes.