sysfs.h: add __ATTR_RW() macro
[firefly-linux-kernel-4.4.55.git] / include / linux / sysfs.h
1 /*
2  * sysfs.h - definitions for the device driver filesystem
3  *
4  * Copyright (c) 2001,2002 Patrick Mochel
5  * Copyright (c) 2004 Silicon Graphics, Inc.
6  * Copyright (c) 2007 SUSE Linux Products GmbH
7  * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8  *
9  * Please see Documentation/filesystems/sysfs.txt for more information.
10  */
11
12 #ifndef _SYSFS_H_
13 #define _SYSFS_H_
14
15 #include <linux/compiler.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/lockdep.h>
19 #include <linux/kobject_ns.h>
20 #include <linux/atomic.h>
21
22 struct kobject;
23 struct module;
24 enum kobj_ns_type;
25
26 struct attribute {
27         const char              *name;
28         umode_t                 mode;
29 #ifdef CONFIG_DEBUG_LOCK_ALLOC
30         bool                    ignore_lockdep:1;
31         struct lock_class_key   *key;
32         struct lock_class_key   skey;
33 #endif
34 };
35
36 /**
37  *      sysfs_attr_init - initialize a dynamically allocated sysfs attribute
38  *      @attr: struct attribute to initialize
39  *
40  *      Initialize a dynamically allocated struct attribute so we can
41  *      make lockdep happy.  This is a new requirement for attributes
42  *      and initially this is only needed when lockdep is enabled.
43  *      Lockdep gives a nice error when your attribute is added to
44  *      sysfs if you don't have this.
45  */
46 #ifdef CONFIG_DEBUG_LOCK_ALLOC
47 #define sysfs_attr_init(attr)                           \
48 do {                                                    \
49         static struct lock_class_key __key;             \
50                                                         \
51         (attr)->key = &__key;                           \
52 } while(0)
53 #else
54 #define sysfs_attr_init(attr) do {} while(0)
55 #endif
56
57 struct attribute_group {
58         const char              *name;
59         umode_t                 (*is_visible)(struct kobject *,
60                                               struct attribute *, int);
61         struct attribute        **attrs;
62 };
63
64
65
66 /**
67  * Use these macros to make defining attributes easier. See include/linux/device.h
68  * for examples..
69  */
70
71 #define __ATTR(_name,_mode,_show,_store) { \
72         .attr = {.name = __stringify(_name), .mode = _mode },   \
73         .show   = _show,                                        \
74         .store  = _store,                                       \
75 }
76
77 #define __ATTR_RO(_name) { \
78         .attr   = { .name = __stringify(_name), .mode = 0444 }, \
79         .show   = _name##_show,                                 \
80 }
81
82 #define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
83
84 #define __ATTR_NULL { .attr = { .name = NULL } }
85
86 #ifdef CONFIG_DEBUG_LOCK_ALLOC
87 #define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) {    \
88         .attr = {.name = __stringify(_name), .mode = _mode,     \
89                         .ignore_lockdep = true },               \
90         .show           = _show,                                \
91         .store          = _store,                               \
92 }
93 #else
94 #define __ATTR_IGNORE_LOCKDEP   __ATTR
95 #endif
96
97 #define attr_name(_attr) (_attr).attr.name
98
99 struct file;
100 struct vm_area_struct;
101
102 struct bin_attribute {
103         struct attribute        attr;
104         size_t                  size;
105         void                    *private;
106         ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
107                         char *, loff_t, size_t);
108         ssize_t (*write)(struct file *,struct kobject *, struct bin_attribute *,
109                          char *, loff_t, size_t);
110         int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
111                     struct vm_area_struct *vma);
112 };
113
114 /**
115  *      sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
116  *      @attr: struct bin_attribute to initialize
117  *
118  *      Initialize a dynamically allocated struct bin_attribute so we
119  *      can make lockdep happy.  This is a new requirement for
120  *      attributes and initially this is only needed when lockdep is
121  *      enabled.  Lockdep gives a nice error when your attribute is
122  *      added to sysfs if you don't have this.
123  */
124 #define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
125
126 struct sysfs_ops {
127         ssize_t (*show)(struct kobject *, struct attribute *,char *);
128         ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
129         const void *(*namespace)(struct kobject *, const struct attribute *);
130 };
131
132 struct sysfs_dirent;
133
134 #ifdef CONFIG_SYSFS
135
136 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
137                             void *data, struct module *owner);
138
139 int __must_check sysfs_create_dir(struct kobject *kobj);
140 void sysfs_remove_dir(struct kobject *kobj);
141 int __must_check sysfs_rename_dir(struct kobject *kobj, const char *new_name);
142 int __must_check sysfs_move_dir(struct kobject *kobj,
143                                 struct kobject *new_parent_kobj);
144
145 int __must_check sysfs_create_file(struct kobject *kobj,
146                                    const struct attribute *attr);
147 int __must_check sysfs_create_files(struct kobject *kobj,
148                                    const struct attribute **attr);
149 int __must_check sysfs_chmod_file(struct kobject *kobj,
150                                   const struct attribute *attr, umode_t mode);
151 void sysfs_remove_file(struct kobject *kobj, const struct attribute *attr);
152 void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
153
154 int __must_check sysfs_create_bin_file(struct kobject *kobj,
155                                        const struct bin_attribute *attr);
156 void sysfs_remove_bin_file(struct kobject *kobj,
157                            const struct bin_attribute *attr);
158
159 int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
160                                    const char *name);
161 int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
162                                           struct kobject *target,
163                                           const char *name);
164 void sysfs_remove_link(struct kobject *kobj, const char *name);
165
166 int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
167                         const char *old_name, const char *new_name);
168
169 void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
170                         const char *name);
171
172 int __must_check sysfs_create_group(struct kobject *kobj,
173                                     const struct attribute_group *grp);
174 int sysfs_update_group(struct kobject *kobj,
175                        const struct attribute_group *grp);
176 void sysfs_remove_group(struct kobject *kobj,
177                         const struct attribute_group *grp);
178 int sysfs_add_file_to_group(struct kobject *kobj,
179                         const struct attribute *attr, const char *group);
180 void sysfs_remove_file_from_group(struct kobject *kobj,
181                         const struct attribute *attr, const char *group);
182 int sysfs_merge_group(struct kobject *kobj,
183                        const struct attribute_group *grp);
184 void sysfs_unmerge_group(struct kobject *kobj,
185                        const struct attribute_group *grp);
186 int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
187                             struct kobject *target, const char *link_name);
188 void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
189                                   const char *link_name);
190
191 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
192 void sysfs_notify_dirent(struct sysfs_dirent *sd);
193 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
194                                       const void *ns,
195                                       const unsigned char *name);
196 struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd);
197 void sysfs_put(struct sysfs_dirent *sd);
198
199 int __must_check sysfs_init(void);
200
201 #else /* CONFIG_SYSFS */
202
203 static inline int sysfs_schedule_callback(struct kobject *kobj,
204                 void (*func)(void *), void *data, struct module *owner)
205 {
206         return -ENOSYS;
207 }
208
209 static inline int sysfs_create_dir(struct kobject *kobj)
210 {
211         return 0;
212 }
213
214 static inline void sysfs_remove_dir(struct kobject *kobj)
215 {
216 }
217
218 static inline int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
219 {
220         return 0;
221 }
222
223 static inline int sysfs_move_dir(struct kobject *kobj,
224                                  struct kobject *new_parent_kobj)
225 {
226         return 0;
227 }
228
229 static inline int sysfs_create_file(struct kobject *kobj,
230                                     const struct attribute *attr)
231 {
232         return 0;
233 }
234
235 static inline int sysfs_create_files(struct kobject *kobj,
236                                     const struct attribute **attr)
237 {
238         return 0;
239 }
240
241 static inline int sysfs_chmod_file(struct kobject *kobj,
242                                    const struct attribute *attr, umode_t mode)
243 {
244         return 0;
245 }
246
247 static inline void sysfs_remove_file(struct kobject *kobj,
248                                      const struct attribute *attr)
249 {
250 }
251
252 static inline void sysfs_remove_files(struct kobject *kobj,
253                                      const struct attribute **attr)
254 {
255 }
256
257 static inline int sysfs_create_bin_file(struct kobject *kobj,
258                                         const struct bin_attribute *attr)
259 {
260         return 0;
261 }
262
263 static inline void sysfs_remove_bin_file(struct kobject *kobj,
264                                          const struct bin_attribute *attr)
265 {
266 }
267
268 static inline int sysfs_create_link(struct kobject *kobj,
269                                     struct kobject *target, const char *name)
270 {
271         return 0;
272 }
273
274 static inline int sysfs_create_link_nowarn(struct kobject *kobj,
275                                            struct kobject *target,
276                                            const char *name)
277 {
278         return 0;
279 }
280
281 static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
282 {
283 }
284
285 static inline int sysfs_rename_link(struct kobject *k, struct kobject *t,
286                                     const char *old_name, const char *new_name)
287 {
288         return 0;
289 }
290
291 static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
292                                      const char *name)
293 {
294 }
295
296 static inline int sysfs_create_group(struct kobject *kobj,
297                                      const struct attribute_group *grp)
298 {
299         return 0;
300 }
301
302 static inline int sysfs_update_group(struct kobject *kobj,
303                                 const struct attribute_group *grp)
304 {
305         return 0;
306 }
307
308 static inline void sysfs_remove_group(struct kobject *kobj,
309                                       const struct attribute_group *grp)
310 {
311 }
312
313 static inline int sysfs_add_file_to_group(struct kobject *kobj,
314                 const struct attribute *attr, const char *group)
315 {
316         return 0;
317 }
318
319 static inline void sysfs_remove_file_from_group(struct kobject *kobj,
320                 const struct attribute *attr, const char *group)
321 {
322 }
323
324 static inline int sysfs_merge_group(struct kobject *kobj,
325                        const struct attribute_group *grp)
326 {
327         return 0;
328 }
329
330 static inline void sysfs_unmerge_group(struct kobject *kobj,
331                        const struct attribute_group *grp)
332 {
333 }
334
335 static inline int sysfs_add_link_to_group(struct kobject *kobj,
336                 const char *group_name, struct kobject *target,
337                 const char *link_name)
338 {
339         return 0;
340 }
341
342 static inline void sysfs_remove_link_from_group(struct kobject *kobj,
343                 const char *group_name, const char *link_name)
344 {
345 }
346
347 static inline void sysfs_notify(struct kobject *kobj, const char *dir,
348                                 const char *attr)
349 {
350 }
351 static inline void sysfs_notify_dirent(struct sysfs_dirent *sd)
352 {
353 }
354 static inline
355 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
356                                       const void *ns,
357                                       const unsigned char *name)
358 {
359         return NULL;
360 }
361 static inline struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
362 {
363         return NULL;
364 }
365 static inline void sysfs_put(struct sysfs_dirent *sd)
366 {
367 }
368
369 static inline int __must_check sysfs_init(void)
370 {
371         return 0;
372 }
373
374 #endif /* CONFIG_SYSFS */
375
376 #endif /* _SYSFS_H_ */