Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 1 | ========================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | The Basic Device Structure |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 3 | ========================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
Wanlong Gao | 63dc355 | 2011-05-05 07:55:37 +0800 | [diff] [blame] | 5 | See the kerneldoc for the struct device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | |
| 8 | Programming Interface |
| 9 | ~~~~~~~~~~~~~~~~~~~~~ |
| 10 | The bus driver that discovers the device uses this to register the |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 11 | device with the core:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 13 | int device_register(struct device * dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | The bus should initialize the following fields: |
| 16 | |
| 17 | - parent |
| 18 | - name |
| 19 | - bus_id |
| 20 | - bus |
| 21 | |
| 22 | A device is removed from the core when its reference count goes to |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 23 | 0. The reference count can be adjusted using:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 25 | struct device * get_device(struct device * dev); |
| 26 | void put_device(struct device * dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | get_device() will return a pointer to the struct device passed to it |
| 29 | if the reference is not already 0 (if it's in the process of being |
| 30 | removed already). |
| 31 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 32 | A driver can access the lock in the device structure using:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 34 | void lock_device(struct device * dev); |
| 35 | void unlock_device(struct device * dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | Attributes |
| 39 | ~~~~~~~~~~ |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 40 | |
| 41 | :: |
| 42 | |
| 43 | struct device_attribute { |
Mike Murphy | 245127d | 2009-02-22 01:17:14 -0500 | [diff] [blame] | 44 | struct attribute attr; |
| 45 | ssize_t (*show)(struct device *dev, struct device_attribute *attr, |
| 46 | char *buf); |
| 47 | ssize_t (*store)(struct device *dev, struct device_attribute *attr, |
| 48 | const char *buf, size_t count); |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 49 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 51 | Attributes of devices can be exported by a device driver through sysfs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Mauro Carvalho Chehab | 0c1bc6b | 2020-04-14 18:48:37 +0200 | [diff] [blame] | 53 | Please see Documentation/filesystems/sysfs.rst for more information |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | on how sysfs works. |
| 55 | |
Mauro Carvalho Chehab | 0c1bc6b | 2020-04-14 18:48:37 +0200 | [diff] [blame] | 56 | As explained in Documentation/core-api/kobject.rst, device attributes must be |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 57 | created before the KOBJ_ADD uevent is generated. The only way to realize |
| 58 | that is by defining an attribute group. |
| 59 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 60 | Attributes are declared using a macro called DEVICE_ATTR:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 62 | #define DEVICE_ATTR(name,mode,show,store) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 64 | Example::: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Geert Uytterhoeven | d944f0b | 2021-03-03 14:38:44 +0100 | [diff] [blame] | 66 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
| 67 | static DEVICE_ATTR(power, 0644, power_show, power_store); |
| 68 | |
| 69 | Helper macros are available for common values of mode, so the above examples |
| 70 | can be simplified to::: |
| 71 | |
| 72 | static DEVICE_ATTR_RO(type); |
| 73 | static DEVICE_ATTR_RW(power); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 75 | This declares two structures of type struct device_attribute with respective |
| 76 | names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 77 | organized as follows into a group:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 79 | static struct attribute *dev_attrs[] = { |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 80 | &dev_attr_type.attr, |
| 81 | &dev_attr_power.attr, |
| 82 | NULL, |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 83 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Geert Uytterhoeven | 459d7ed | 2021-03-03 14:38:45 +0100 | [diff] [blame] | 85 | static struct attribute_group dev_group = { |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 86 | .attrs = dev_attrs, |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 87 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Geert Uytterhoeven | 459d7ed | 2021-03-03 14:38:45 +0100 | [diff] [blame] | 89 | static const struct attribute_group *dev_groups[] = { |
| 90 | &dev_group, |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 91 | NULL, |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 92 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Geert Uytterhoeven | 459d7ed | 2021-03-03 14:38:45 +0100 | [diff] [blame] | 94 | A helper macro is available for the common case of a single group, so the |
| 95 | above two structures can be declared using::: |
| 96 | |
| 97 | ATTRIBUTE_GROUPS(dev); |
| 98 | |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 99 | This array of groups can then be associated with a device by setting the |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 100 | group pointer in struct device before device_register() is invoked:: |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 101 | |
Geert Uytterhoeven | 459d7ed | 2021-03-03 14:38:45 +0100 | [diff] [blame] | 102 | dev->groups = dev_groups; |
Mauro Carvalho Chehab | 4489f161 | 2019-06-18 17:53:27 -0300 | [diff] [blame] | 103 | device_register(dev); |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 104 | |
| 105 | The device_register() function will use the 'groups' pointer to create the |
| 106 | device attributes and the device_unregister() function will use this pointer |
| 107 | to remove the device attributes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Grant Likely | b22813b | 2009-03-06 14:05:39 -0700 | [diff] [blame] | 109 | Word of warning: While the kernel allows device_create_file() and |
| 110 | device_remove_file() to be called on a device at any time, userspace has |
| 111 | strict expectations on when attributes get created. When a new device is |
| 112 | registered in the kernel, a uevent is generated to notify userspace (like |
| 113 | udev) that a new device is available. If attributes are added after the |
| 114 | device is registered, then userspace won't get notified and userspace will |
| 115 | not know about the new attributes. |
| 116 | |
| 117 | This is important for device driver that need to publish additional |
| 118 | attributes for a device at driver probe time. If the device driver simply |
| 119 | calls device_create_file() on the device structure passed to it, then |
Bart Van Assche | d58cb9c | 2011-08-23 19:27:27 +0200 | [diff] [blame] | 120 | userspace will never be notified of the new attributes. |