@@ -96,7 +96,12 @@ struct device_type {
9696 const struct dev_pm_ops * pm ;
9797};
9898
99- /* interface for exporting device attributes */
99+ /**
100+ * struct device_attribute - Interface for exporting device attributes.
101+ * @attr: sysfs attribute definition.
102+ * @show: Show handler.
103+ * @store: Store handler.
104+ */
100105struct device_attribute {
101106 struct attribute attr ;
102107 ssize_t (* show )(struct device * dev , struct device_attribute * attr ,
@@ -105,6 +110,11 @@ struct device_attribute {
105110 const char * buf , size_t count );
106111};
107112
113+ /**
114+ * struct dev_ext_attribute - Exported device attribute with extra context.
115+ * @attr: Exported device attribute.
116+ * @var: Pointer to context.
117+ */
108118struct dev_ext_attribute {
109119 struct device_attribute attr ;
110120 void * var ;
@@ -123,30 +133,124 @@ ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
123133ssize_t device_store_bool (struct device * dev , struct device_attribute * attr ,
124134 const char * buf , size_t count );
125135
136+ /**
137+ * DEVICE_ATTR - Define a device attribute.
138+ * @_name: Attribute name.
139+ * @_mode: File mode.
140+ * @_show: Show handler. Optional, but mandatory if attribute is readable.
141+ * @_store: Store handler. Optional, but mandatory if attribute is writable.
142+ *
143+ * Convenience macro for defining a struct device_attribute.
144+ *
145+ * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to:
146+ *
147+ * .. code-block:: c
148+ *
149+ * struct device_attribute dev_attr_foo = {
150+ * .attr = { .name = "foo", .mode = 0644 },
151+ * .show = foo_show,
152+ * .store = foo_store,
153+ * };
154+ */
126155#define DEVICE_ATTR (_name , _mode , _show , _store ) \
127156 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
157+
158+ /**
159+ * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
160+ * @_name: Attribute name.
161+ * @_mode: File mode.
162+ * @_show: Show handler. Optional, but mandatory if attribute is readable.
163+ * @_store: Store handler. Optional, but mandatory if attribute is writable.
164+ *
165+ * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
166+ */
128167#define DEVICE_ATTR_PREALLOC (_name , _mode , _show , _store ) \
129168 struct device_attribute dev_attr_##_name = \
130169 __ATTR_PREALLOC(_name, _mode, _show, _store)
170+
171+ /**
172+ * DEVICE_ATTR_RW - Define a read-write device attribute.
173+ * @_name: Attribute name.
174+ *
175+ * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show,
176+ * and @_store is <_name>_store.
177+ */
131178#define DEVICE_ATTR_RW (_name ) \
132179 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
180+
181+ /**
182+ * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
183+ * @_name: Attribute name.
184+ *
185+ * Like DEVICE_ATTR_RW(), but @_mode is 0600.
186+ */
133187#define DEVICE_ATTR_ADMIN_RW (_name ) \
134188 struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
189+
190+ /**
191+ * DEVICE_ATTR_RO - Define a readable device attribute.
192+ * @_name: Attribute name.
193+ *
194+ * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
195+ */
135196#define DEVICE_ATTR_RO (_name ) \
136197 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
198+
199+ /**
200+ * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
201+ * @_name: Attribute name.
202+ *
203+ * Like DEVICE_ATTR_RO(), but @_mode is 0400.
204+ */
137205#define DEVICE_ATTR_ADMIN_RO (_name ) \
138206 struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
207+
208+ /**
209+ * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
210+ * @_name: Attribute name.
211+ *
212+ * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
213+ */
139214#define DEVICE_ATTR_WO (_name ) \
140215 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
216+
217+ /**
218+ * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
219+ * @_name: Attribute name.
220+ * @_mode: File mode.
221+ * @_var: Identifier of unsigned long.
222+ *
223+ * Like DEVICE_ATTR(), but @_show and @_store are automatically provided
224+ * such that reads and writes to the attribute from userspace affect @_var.
225+ */
141226#define DEVICE_ULONG_ATTR (_name , _mode , _var ) \
142227 struct dev_ext_attribute dev_attr_##_name = \
143228 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
229+
230+ /**
231+ * DEVICE_INT_ATTR - Define a device attribute backed by an int.
232+ * @_name: Attribute name.
233+ * @_mode: File mode.
234+ * @_var: Identifier of int.
235+ *
236+ * Like DEVICE_ULONG_ATTR(), but @_var is an int.
237+ */
144238#define DEVICE_INT_ATTR (_name , _mode , _var ) \
145239 struct dev_ext_attribute dev_attr_##_name = \
146240 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
241+
242+ /**
243+ * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
244+ * @_name: Attribute name.
245+ * @_mode: File mode.
246+ * @_var: Identifier of bool.
247+ *
248+ * Like DEVICE_ULONG_ATTR(), but @_var is a bool.
249+ */
147250#define DEVICE_BOOL_ATTR (_name , _mode , _var ) \
148251 struct dev_ext_attribute dev_attr_##_name = \
149252 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
253+
150254#define DEVICE_ATTR_IGNORE_LOCKDEP (_name , _mode , _show , _store ) \
151255 struct device_attribute dev_attr_##_name = \
152256 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
@@ -711,6 +815,11 @@ static inline bool device_iommu_mapped(struct device *dev)
711815/* Get the wakeup routines, which depend on struct device */
712816#include <linux/pm_wakeup.h>
713817
818+ /**
819+ * dev_name - Return a device's name.
820+ * @dev: Device with name to get.
821+ * Return: The kobject name of the device, or its initial name if unavailable.
822+ */
714823static inline const char * dev_name (const struct device * dev )
715824{
716825 /* Use the init name until the kobject becomes available */
0 commit comments