]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/sysfs.h
Merge tag 'sound-4.2-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[mirror_ubuntu-artful-kernel.git] / include / linux / sysfs.h
CommitLineData
1da177e4
LT
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.
6d66f5cd
TH
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
1da177e4
LT
8 *
9 * Please see Documentation/filesystems/sysfs.txt for more information.
10 */
11
12#ifndef _SYSFS_H_
13#define _SYSFS_H_
14
b8441ed2 15#include <linux/kernfs.h>
4a7fb636 16#include <linux/compiler.h>
5851fadc 17#include <linux/errno.h>
bf0acc33 18#include <linux/list.h>
6992f533 19#include <linux/lockdep.h>
8488a38f 20#include <linux/kobject_ns.h>
3493f69f 21#include <linux/stat.h>
60063497 22#include <linux/atomic.h>
1da177e4
LT
23
24struct kobject;
25struct module;
6ab9cea1 26struct bin_attribute;
3ff195b0 27enum kobj_ns_type;
1da177e4
LT
28
29struct attribute {
59f69015 30 const char *name;
9104e427 31 umode_t mode;
6992f533 32#ifdef CONFIG_DEBUG_LOCK_ALLOC
356c05d5 33 bool ignore_lockdep:1;
6992f533
EB
34 struct lock_class_key *key;
35 struct lock_class_key skey;
36#endif
1da177e4
LT
37};
38
35960258
EB
39/**
40 * sysfs_attr_init - initialize a dynamically allocated sysfs attribute
41 * @attr: struct attribute to initialize
42 *
43 * Initialize a dynamically allocated struct attribute so we can
44 * make lockdep happy. This is a new requirement for attributes
45 * and initially this is only needed when lockdep is enabled.
46 * Lockdep gives a nice error when your attribute is added to
47 * sysfs if you don't have this.
48 */
6992f533
EB
49#ifdef CONFIG_DEBUG_LOCK_ALLOC
50#define sysfs_attr_init(attr) \
51do { \
52 static struct lock_class_key __key; \
53 \
54 (attr)->key = &__key; \
5da5c9c8 55} while (0)
6992f533 56#else
5da5c9c8 57#define sysfs_attr_init(attr) do {} while (0)
6992f533
EB
58#endif
59
ba61af6f
GR
60/**
61 * struct attribute_group - data structure used to declare an attribute group.
62 * @name: Optional: Attribute group name
63 * If specified, the attribute group will be created in
64 * a new subdirectory with this name.
65 * @is_visible: Optional: Function to return permissions associated with an
66 * attribute of the group. Will be called repeatedly for each
67 * attribute in the group. Only read/write permissions as well as
68 * SYSFS_PREALLOC are accepted. Must return 0 if an attribute is
69 * not visible. The returned value will replace static permissions
70 * defined in struct attribute or struct bin_attribute.
71 * @attrs: Pointer to NULL terminated list of attributes.
72 * @bin_attrs: Pointer to NULL terminated list of binary attributes.
73 * Either attrs or bin_attrs or both must be provided.
74 */
1da177e4 75struct attribute_group {
59f69015 76 const char *name;
587a1f16 77 umode_t (*is_visible)(struct kobject *,
d4acd722 78 struct attribute *, int);
59f69015 79 struct attribute **attrs;
6ab9cea1 80 struct bin_attribute **bin_attrs;
1da177e4
LT
81};
82
1da177e4
LT
83/**
84 * Use these macros to make defining attributes easier. See include/linux/device.h
85 * for examples..
86 */
87
2b75869b
N
88#define SYSFS_PREALLOC 010000
89
5da5c9c8 90#define __ATTR(_name, _mode, _show, _store) { \
58f86cc8
RR
91 .attr = {.name = __stringify(_name), \
92 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
aa01aa3c
OS
93 .show = _show, \
94 .store = _store, \
1da177e4
LT
95}
96
2b75869b
N
97#define __ATTR_PREALLOC(_name, _mode, _show, _store) { \
98 .attr = {.name = __stringify(_name), \
99 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
100 .show = _show, \
101 .store = _store, \
102}
103
aa01aa3c
OS
104#define __ATTR_RO(_name) { \
105 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
106 .show = _name##_show, \
1da177e4
LT
107}
108
a65fcce7
GKH
109#define __ATTR_WO(_name) { \
110 .attr = { .name = __stringify(_name), .mode = S_IWUSR }, \
111 .store = _name##_store, \
112}
113
aa01aa3c
OS
114#define __ATTR_RW(_name) __ATTR(_name, (S_IWUSR | S_IRUGO), \
115 _name##_show, _name##_store)
b9b32597 116
1da177e4 117#define __ATTR_NULL { .attr = { .name = NULL } }
356c05d5
AS
118
119#ifdef CONFIG_DEBUG_LOCK_ALLOC
120#define __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) { \
121 .attr = {.name = __stringify(_name), .mode = _mode, \
122 .ignore_lockdep = true }, \
123 .show = _show, \
124 .store = _store, \
125}
126#else
127#define __ATTR_IGNORE_LOCKDEP __ATTR
128#endif
1da177e4 129
3493f69f
OS
130#define __ATTRIBUTE_GROUPS(_name) \
131static const struct attribute_group *_name##_groups[] = { \
132 &_name##_group, \
f2f37f58
GKH
133 NULL, \
134}
135
3493f69f
OS
136#define ATTRIBUTE_GROUPS(_name) \
137static const struct attribute_group _name##_group = { \
138 .attrs = _name##_attrs, \
139}; \
140__ATTRIBUTE_GROUPS(_name)
141
2c3c8bea 142struct file;
1da177e4
LT
143struct vm_area_struct;
144
145struct bin_attribute {
146 struct attribute attr;
147 size_t size;
148 void *private;
2c3c8bea 149 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
91a69029 150 char *, loff_t, size_t);
5da5c9c8 151 ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
91a69029 152 char *, loff_t, size_t);
2c3c8bea 153 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
1da177e4
LT
154 struct vm_area_struct *vma);
155};
156
35960258
EB
157/**
158 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
159 * @attr: struct bin_attribute to initialize
160 *
161 * Initialize a dynamically allocated struct bin_attribute so we
162 * can make lockdep happy. This is a new requirement for
163 * attributes and initially this is only needed when lockdep is
164 * enabled. Lockdep gives a nice error when your attribute is
165 * added to sysfs if you don't have this.
166 */
62e877b8 167#define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
6992f533 168
3493f69f
OS
169/* macros to create static binary attributes easier */
170#define __BIN_ATTR(_name, _mode, _read, _write, _size) { \
171 .attr = { .name = __stringify(_name), .mode = _mode }, \
172 .read = _read, \
173 .write = _write, \
174 .size = _size, \
175}
176
177#define __BIN_ATTR_RO(_name, _size) { \
178 .attr = { .name = __stringify(_name), .mode = S_IRUGO }, \
179 .read = _name##_read, \
180 .size = _size, \
e4b63603
GKH
181}
182
3493f69f
OS
183#define __BIN_ATTR_RW(_name, _size) __BIN_ATTR(_name, \
184 (S_IWUSR | S_IRUGO), _name##_read, \
68531526 185 _name##_write, _size)
3493f69f
OS
186
187#define __BIN_ATTR_NULL __ATTR_NULL
188
189#define BIN_ATTR(_name, _mode, _read, _write, _size) \
190struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \
191 _write, _size)
192
193#define BIN_ATTR_RO(_name, _size) \
194struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size)
195
196#define BIN_ATTR_RW(_name, _size) \
197struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size)
198
1da177e4 199struct sysfs_ops {
5da5c9c8
GKH
200 ssize_t (*show)(struct kobject *, struct attribute *, char *);
201 ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
1da177e4
LT
202};
203
1da177e4
LT
204#ifdef CONFIG_SYSFS
205
e34ff490 206int __must_check sysfs_create_dir_ns(struct kobject *kobj, const void *ns);
59f69015 207void sysfs_remove_dir(struct kobject *kobj);
e34ff490
TH
208int __must_check sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
209 const void *new_ns);
210int __must_check sysfs_move_dir_ns(struct kobject *kobj,
211 struct kobject *new_parent_kobj,
212 const void *new_ns);
87d2846f
EB
213int __must_check sysfs_create_mount_point(struct kobject *parent_kobj,
214 const char *name);
215void sysfs_remove_mount_point(struct kobject *parent_kobj,
216 const char *name);
31e5abe9 217
58292cbe
TH
218int __must_check sysfs_create_file_ns(struct kobject *kobj,
219 const struct attribute *attr,
220 const void *ns);
1c205ae1
AK
221int __must_check sysfs_create_files(struct kobject *kobj,
222 const struct attribute **attr);
49c19400 223int __must_check sysfs_chmod_file(struct kobject *kobj,
48176a97 224 const struct attribute *attr, umode_t mode);
58292cbe
TH
225void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
226 const void *ns);
6b0afc2a 227bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr);
1c205ae1 228void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
1da177e4 229
4a7fb636 230int __must_check sysfs_create_bin_file(struct kobject *kobj,
66ecb92b
PC
231 const struct bin_attribute *attr);
232void sysfs_remove_bin_file(struct kobject *kobj,
233 const struct bin_attribute *attr);
1da177e4 234
59f69015
TH
235int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
236 const char *name);
36ce6dad
CH
237int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
238 struct kobject *target,
239 const char *name);
59f69015
TH
240void sysfs_remove_link(struct kobject *kobj, const char *name);
241
4b30ee58
TH
242int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
243 const char *old_name, const char *new_name,
244 const void *new_ns);
7cb32942 245
746edb7a
EB
246void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
247 const char *name);
248
59f69015
TH
249int __must_check sysfs_create_group(struct kobject *kobj,
250 const struct attribute_group *grp);
3e9b2bae
GKH
251int __must_check sysfs_create_groups(struct kobject *kobj,
252 const struct attribute_group **groups);
0f423895
JB
253int sysfs_update_group(struct kobject *kobj,
254 const struct attribute_group *grp);
59f69015
TH
255void sysfs_remove_group(struct kobject *kobj,
256 const struct attribute_group *grp);
3e9b2bae
GKH
257void sysfs_remove_groups(struct kobject *kobj,
258 const struct attribute_group **groups);
dfa87c82 259int sysfs_add_file_to_group(struct kobject *kobj,
59f69015 260 const struct attribute *attr, const char *group);
dfa87c82 261void sysfs_remove_file_from_group(struct kobject *kobj,
59f69015 262 const struct attribute *attr, const char *group);
69d44ffb
AS
263int sysfs_merge_group(struct kobject *kobj,
264 const struct attribute_group *grp);
265void sysfs_unmerge_group(struct kobject *kobj,
266 const struct attribute_group *grp);
0bb8f3d6
RW
267int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
268 struct kobject *target, const char *link_name);
269void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
270 const char *link_name);
dfa87c82 271
8c0e3998 272void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
3ff195b0 273
f1282c84 274int __must_check sysfs_init(void);
f20a9ead 275
fa4cd451
TH
276static inline void sysfs_enable_ns(struct kernfs_node *kn)
277{
278 return kernfs_enable_ns(kn);
279}
280
1da177e4
LT
281#else /* CONFIG_SYSFS */
282
e34ff490 283static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
1da177e4
LT
284{
285 return 0;
286}
287
59f69015 288static inline void sysfs_remove_dir(struct kobject *kobj)
1da177e4 289{
1da177e4
LT
290}
291
e34ff490
TH
292static inline int sysfs_rename_dir_ns(struct kobject *kobj,
293 const char *new_name, const void *new_ns)
1da177e4 294{
0b4a4fea 295 return 0;
1da177e4
LT
296}
297
e34ff490
TH
298static inline int sysfs_move_dir_ns(struct kobject *kobj,
299 struct kobject *new_parent_kobj,
300 const void *new_ns)
8a82472f
CH
301{
302 return 0;
303}
304
87d2846f
EB
305static inline int sysfs_create_mount_point(struct kobject *parent_kobj,
306 const char *name)
307{
308 return 0;
309}
310
311static inline void sysfs_remove_mount_point(struct kobject *parent_kobj,
312 const char *name)
313{
314}
315
58292cbe
TH
316static inline int sysfs_create_file_ns(struct kobject *kobj,
317 const struct attribute *attr,
318 const void *ns)
1da177e4
LT
319{
320 return 0;
321}
322
1c205ae1
AK
323static inline int sysfs_create_files(struct kobject *kobj,
324 const struct attribute **attr)
325{
326 return 0;
327}
328
59f69015 329static inline int sysfs_chmod_file(struct kobject *kobj,
48176a97 330 const struct attribute *attr, umode_t mode)
31e5abe9
KS
331{
332 return 0;
333}
1da177e4 334
58292cbe
TH
335static inline void sysfs_remove_file_ns(struct kobject *kobj,
336 const struct attribute *attr,
337 const void *ns)
1da177e4 338{
1da177e4
LT
339}
340
6b0afc2a
TH
341static inline bool sysfs_remove_file_self(struct kobject *kobj,
342 const struct attribute *attr)
343{
344 return false;
345}
346
1c205ae1
AK
347static inline void sysfs_remove_files(struct kobject *kobj,
348 const struct attribute **attr)
349{
350}
351
59f69015 352static inline int sysfs_create_bin_file(struct kobject *kobj,
66ecb92b 353 const struct bin_attribute *attr)
1da177e4
LT
354{
355 return 0;
356}
357
3612e06b 358static inline void sysfs_remove_bin_file(struct kobject *kobj,
66ecb92b 359 const struct bin_attribute *attr)
1da177e4 360{
1da177e4
LT
361}
362
59f69015
TH
363static inline int sysfs_create_link(struct kobject *kobj,
364 struct kobject *target, const char *name)
1da177e4
LT
365{
366 return 0;
367}
368
36ce6dad
CH
369static inline int sysfs_create_link_nowarn(struct kobject *kobj,
370 struct kobject *target,
371 const char *name)
372{
373 return 0;
374}
375
59f69015 376static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
1da177e4 377{
1da177e4
LT
378}
379
4b30ee58
TH
380static inline int sysfs_rename_link_ns(struct kobject *k, struct kobject *t,
381 const char *old_name,
382 const char *new_name, const void *ns)
7cb32942
EB
383{
384 return 0;
385}
386
746edb7a
EB
387static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
388 const char *name)
389{
390}
391
59f69015
TH
392static inline int sysfs_create_group(struct kobject *kobj,
393 const struct attribute_group *grp)
1da177e4
LT
394{
395 return 0;
396}
397
f7998780
GKH
398static inline int sysfs_create_groups(struct kobject *kobj,
399 const struct attribute_group **groups)
400{
574979c6 401 return 0;
f7998780
GKH
402}
403
1cbfb7a5
RD
404static inline int sysfs_update_group(struct kobject *kobj,
405 const struct attribute_group *grp)
406{
407 return 0;
408}
409
59f69015
TH
410static inline void sysfs_remove_group(struct kobject *kobj,
411 const struct attribute_group *grp)
1da177e4 412{
1da177e4
LT
413}
414
f7998780
GKH
415static inline void sysfs_remove_groups(struct kobject *kobj,
416 const struct attribute_group **groups)
417{
418}
419
dfa87c82
AS
420static inline int sysfs_add_file_to_group(struct kobject *kobj,
421 const struct attribute *attr, const char *group)
422{
423 return 0;
424}
425
426static inline void sysfs_remove_file_from_group(struct kobject *kobj,
d701d8a3 427 const struct attribute *attr, const char *group)
dfa87c82 428{
dfa87c82
AS
429}
430
69d44ffb
AS
431static inline int sysfs_merge_group(struct kobject *kobj,
432 const struct attribute_group *grp)
433{
434 return 0;
435}
436
437static inline void sysfs_unmerge_group(struct kobject *kobj,
438 const struct attribute_group *grp)
439{
440}
441
0bb8f3d6
RW
442static inline int sysfs_add_link_to_group(struct kobject *kobj,
443 const char *group_name, struct kobject *target,
444 const char *link_name)
445{
446 return 0;
447}
448
449static inline void sysfs_remove_link_from_group(struct kobject *kobj,
450 const char *group_name, const char *link_name)
451{
452}
453
8c0e3998
TP
454static inline void sysfs_notify(struct kobject *kobj, const char *dir,
455 const char *attr)
4508a7a7
N
456{
457}
458
f20a9ead
AM
459static inline int __must_check sysfs_init(void)
460{
461 return 0;
462}
463
fa4cd451
TH
464static inline void sysfs_enable_ns(struct kernfs_node *kn)
465{
466}
467
1da177e4
LT
468#endif /* CONFIG_SYSFS */
469
58292cbe
TH
470static inline int __must_check sysfs_create_file(struct kobject *kobj,
471 const struct attribute *attr)
472{
473 return sysfs_create_file_ns(kobj, attr, NULL);
474}
475
476static inline void sysfs_remove_file(struct kobject *kobj,
477 const struct attribute *attr)
478{
78e1da62 479 sysfs_remove_file_ns(kobj, attr, NULL);
58292cbe
TH
480}
481
4b30ee58
TH
482static inline int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
483 const char *old_name, const char *new_name)
484{
485 return sysfs_rename_link_ns(kobj, target, old_name, new_name, NULL);
486}
487
324a56e1 488static inline void sysfs_notify_dirent(struct kernfs_node *kn)
ccf73cf3 489{
324a56e1 490 kernfs_notify(kn);
ccf73cf3
TH
491}
492
324a56e1
TH
493static inline struct kernfs_node *sysfs_get_dirent(struct kernfs_node *parent,
494 const unsigned char *name)
388975cc 495{
324a56e1 496 return kernfs_find_and_get(parent, name);
388975cc
TH
497}
498
324a56e1 499static inline struct kernfs_node *sysfs_get(struct kernfs_node *kn)
024f6471 500{
324a56e1
TH
501 kernfs_get(kn);
502 return kn;
ccf73cf3
TH
503}
504
324a56e1 505static inline void sysfs_put(struct kernfs_node *kn)
ccf73cf3 506{
324a56e1 507 kernfs_put(kn);
024f6471
TH
508}
509
1da177e4 510#endif /* _SYSFS_H_ */