blob: d50a96b9bb6d47a4a1485d1ee2b94fdf64a72f0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sysfs.h - definitions for the device driver filesystem
3 *
4 * Copyright (c) 2001,2002 Patrick Mochel
5 * Copyright (c) 2004 Silicon Graphics, Inc.
Tejun Heo6d66f5c2007-09-20 17:31:38 +09006 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Please see Documentation/filesystems/sysfs.txt for more information.
10 */
11
12#ifndef _SYSFS_H_
13#define _SYSFS_H_
14
Andrew Morton4a7fb632006-08-14 22:43:17 -070015#include <linux/compiler.h>
Ralf Baechle5851fad2007-03-18 12:58:08 +000016#include <linux/errno.h>
Frank Haverkampbf0acc32007-01-17 17:51:18 +010017#include <linux/list.h>
Eric W. Biederman6992f532010-02-11 15:21:53 -080018#include <linux/lockdep.h>
David Howells8488a382010-08-11 15:01:02 +010019#include <linux/kobject_ns.h>
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22struct kobject;
23struct module;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -070024enum kobj_ns_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26struct attribute {
Tejun Heo59f69012007-09-20 16:05:10 +090027 const char *name;
Al Viro9104e422011-07-23 23:10:46 -040028 umode_t mode;
Eric W. Biederman6992f532010-02-11 15:21:53 -080029#ifdef CONFIG_DEBUG_LOCK_ALLOC
Alan Stern356c05d2012-05-14 13:30:03 -040030 bool ignore_lockdep:1;
Eric W. Biederman6992f532010-02-11 15:21:53 -080031 struct lock_class_key *key;
32 struct lock_class_key skey;
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Eric W. Biederman35960252010-02-12 04:35:32 -080036/**
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 */
Eric W. Biederman6992f532010-02-11 15:21:53 -080046#ifdef CONFIG_DEBUG_LOCK_ALLOC
47#define sysfs_attr_init(attr) \
48do { \
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct attribute_group {
Tejun Heo59f69012007-09-20 16:05:10 +090058 const char *name;
Al Viro587a1f12011-07-23 23:11:19 -040059 umode_t (*is_visible)(struct kobject *,
James Bottomleyd4acd722007-10-31 09:38:04 -050060 struct attribute *, int);
Tejun Heo59f69012007-09-20 16:05:10 +090061 struct attribute **attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
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) { \
Tejun Heo7b595752007-06-14 03:45:17 +090072 .attr = {.name = __stringify(_name), .mode = _mode }, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .show = _show, \
74 .store = _store, \
75}
76
77#define __ATTR_RO(_name) { \
Tejun Heo7b595752007-06-14 03:45:17 +090078 .attr = { .name = __stringify(_name), .mode = 0444 }, \
79 .show = _name##_show, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Greg Kroah-Hartmanb9b32592013-07-14 16:05:51 -070082#define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define __ATTR_NULL { .attr = { .name = NULL } }
85
Alan Stern356c05d2012-05-14 13:30:03 -040086#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
Greg Kroah-Hartmanf2f37f52013-07-14 16:05:52 -070097#define ATTRIBUTE_GROUPS(name) \
98static const struct attribute_group name##_group = { \
99 .attrs = name##_attrs, \
100}; \
101static const struct attribute_group *name##_groups[] = { \
102 &name##_group, \
103 NULL, \
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define attr_name(_attr) (_attr).attr.name
107
Chris Wright2c3c8be2010-05-12 18:28:57 -0700108struct file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109struct vm_area_struct;
110
111struct bin_attribute {
112 struct attribute attr;
113 size_t size;
114 void *private;
Chris Wright2c3c8be2010-05-12 18:28:57 -0700115 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
Zhang Rui91a69022007-06-09 13:57:22 +0800116 char *, loff_t, size_t);
Chris Wright2c3c8be2010-05-12 18:28:57 -0700117 ssize_t (*write)(struct file *,struct kobject *, struct bin_attribute *,
Zhang Rui91a69022007-06-09 13:57:22 +0800118 char *, loff_t, size_t);
Chris Wright2c3c8be2010-05-12 18:28:57 -0700119 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct vm_area_struct *vma);
121};
122
Eric W. Biederman35960252010-02-12 04:35:32 -0800123/**
124 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
125 * @attr: struct bin_attribute to initialize
126 *
127 * Initialize a dynamically allocated struct bin_attribute so we
128 * can make lockdep happy. This is a new requirement for
129 * attributes and initially this is only needed when lockdep is
130 * enabled. Lockdep gives a nice error when your attribute is
131 * added to sysfs if you don't have this.
132 */
Stephen Rothwell62e877b82010-03-01 20:38:36 +1100133#define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
Eric W. Biederman6992f532010-02-11 15:21:53 -0800134
Greg Kroah-Hartmane4b63602013-07-14 16:05:53 -0700135/* macro to create static binary attributes easier */
136#define BIN_ATTR(_name, _mode, _read, _write, _size) \
137struct bin_attribute bin_attr_##_name = { \
138 .attr = {.name = __stringify(_name), .mode = _mode }, \
139 .read = _read, \
140 .write = _write, \
141 .size = _size, \
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144struct sysfs_ops {
145 ssize_t (*show)(struct kobject *, struct attribute *,char *);
146 ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
Eric W. Biederman487505c2011-10-12 21:53:38 +0000147 const void *(*namespace)(struct kobject *, const struct attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Neil Brownf1282c82008-07-16 08:58:04 +1000150struct sysfs_dirent;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#ifdef CONFIG_SYSFS
153
Tejun Heo59f69012007-09-20 16:05:10 +0900154int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
155 void *data, struct module *owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400156
Tejun Heo59f69012007-09-20 16:05:10 +0900157int __must_check sysfs_create_dir(struct kobject *kobj);
158void sysfs_remove_dir(struct kobject *kobj);
159int __must_check sysfs_rename_dir(struct kobject *kobj, const char *new_name);
160int __must_check sysfs_move_dir(struct kobject *kobj,
161 struct kobject *new_parent_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Tejun Heo59f69012007-09-20 16:05:10 +0900163int __must_check sysfs_create_file(struct kobject *kobj,
164 const struct attribute *attr);
Andi Kleen1c205ae2010-01-05 12:48:01 +0100165int __must_check sysfs_create_files(struct kobject *kobj,
166 const struct attribute **attr);
Jean Delvare49c19402010-07-02 16:54:05 +0200167int __must_check sysfs_chmod_file(struct kobject *kobj,
Al Viro48176a92011-07-24 03:40:40 -0400168 const struct attribute *attr, umode_t mode);
Tejun Heo59f69012007-09-20 16:05:10 +0900169void sysfs_remove_file(struct kobject *kobj, const struct attribute *attr);
Andi Kleen1c205ae2010-01-05 12:48:01 +0100170void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Andrew Morton4a7fb632006-08-14 22:43:17 -0700172int __must_check sysfs_create_bin_file(struct kobject *kobj,
Phil Carmody66ecb922009-12-18 15:34:20 +0200173 const struct bin_attribute *attr);
174void sysfs_remove_bin_file(struct kobject *kobj,
175 const struct bin_attribute *attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Tejun Heo59f69012007-09-20 16:05:10 +0900177int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
178 const char *name);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200179int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
180 struct kobject *target,
181 const char *name);
Tejun Heo59f69012007-09-20 16:05:10 +0900182void sysfs_remove_link(struct kobject *kobj, const char *name);
Alan Sterndfa87c82007-02-20 15:02:44 -0500183
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800184int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
185 const char *old_name, const char *new_name);
186
Eric W. Biederman746edb72010-03-30 11:31:28 -0700187void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
188 const char *name);
189
Tejun Heo59f69012007-09-20 16:05:10 +0900190int __must_check sysfs_create_group(struct kobject *kobj,
191 const struct attribute_group *grp);
James Bottomley0f423892008-03-20 20:47:52 -0500192int sysfs_update_group(struct kobject *kobj,
193 const struct attribute_group *grp);
Tejun Heo59f69012007-09-20 16:05:10 +0900194void sysfs_remove_group(struct kobject *kobj,
195 const struct attribute_group *grp);
196int sysfs_add_file_to_group(struct kobject *kobj,
197 const struct attribute *attr, const char *group);
198void sysfs_remove_file_from_group(struct kobject *kobj,
199 const struct attribute *attr, const char *group);
Alan Stern69d44ff2010-09-25 23:34:22 +0200200int sysfs_merge_group(struct kobject *kobj,
201 const struct attribute_group *grp);
202void sysfs_unmerge_group(struct kobject *kobj,
203 const struct attribute_group *grp);
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +0100204int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
205 struct kobject *target, const char *link_name);
206void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
207 const char *link_name);
Tejun Heo59f69012007-09-20 16:05:10 +0900208
Trent Piepho8c0e3992008-09-25 16:45:13 -0700209void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
Neil Brownf1282c82008-07-16 08:58:04 +1000210void sysfs_notify_dirent(struct sysfs_dirent *sd);
211struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700212 const void *ns,
Neil Brownf1282c82008-07-16 08:58:04 +1000213 const unsigned char *name);
214struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd);
215void sysfs_put(struct sysfs_dirent *sd);
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700216
Neil Brownf1282c82008-07-16 08:58:04 +1000217int __must_check sysfs_init(void);
Andrew Mortonf20a9ea2006-08-14 22:43:23 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#else /* CONFIG_SYSFS */
220
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400221static inline int sysfs_schedule_callback(struct kobject *kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700222 void (*func)(void *), void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400223{
224 return -ENOSYS;
225}
226
Tejun Heo59f69012007-09-20 16:05:10 +0900227static inline int sysfs_create_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 return 0;
230}
231
Tejun Heo59f69012007-09-20 16:05:10 +0900232static inline void sysfs_remove_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Tejun Heo59f69012007-09-20 16:05:10 +0900236static inline int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Tejun Heo59f69012007-09-20 16:05:10 +0900241static inline int sysfs_move_dir(struct kobject *kobj,
242 struct kobject *new_parent_kobj)
Cornelia Huck8a824722006-11-20 17:07:51 +0100243{
244 return 0;
245}
246
Tejun Heo59f69012007-09-20 16:05:10 +0900247static inline int sysfs_create_file(struct kobject *kobj,
248 const struct attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 return 0;
251}
252
Andi Kleen1c205ae2010-01-05 12:48:01 +0100253static inline int sysfs_create_files(struct kobject *kobj,
254 const struct attribute **attr)
255{
256 return 0;
257}
258
Tejun Heo59f69012007-09-20 16:05:10 +0900259static inline int sysfs_chmod_file(struct kobject *kobj,
Al Viro48176a92011-07-24 03:40:40 -0400260 const struct attribute *attr, umode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700261{
262 return 0;
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Tejun Heo59f69012007-09-20 16:05:10 +0900265static inline void sysfs_remove_file(struct kobject *kobj,
266 const struct attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Andi Kleen1c205ae2010-01-05 12:48:01 +0100270static inline void sysfs_remove_files(struct kobject *kobj,
271 const struct attribute **attr)
272{
273}
274
Tejun Heo59f69012007-09-20 16:05:10 +0900275static inline int sysfs_create_bin_file(struct kobject *kobj,
Phil Carmody66ecb922009-12-18 15:34:20 +0200276 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 return 0;
279}
280
David Rientjes3612e062008-02-19 17:39:02 -0800281static inline void sysfs_remove_bin_file(struct kobject *kobj,
Phil Carmody66ecb922009-12-18 15:34:20 +0200282 const struct bin_attribute *attr)
Tejun Heo59f69012007-09-20 16:05:10 +0900283{
Tejun Heo59f69012007-09-20 16:05:10 +0900284}
285
286static inline int sysfs_create_link(struct kobject *kobj,
287 struct kobject *target, const char *name)
288{
289 return 0;
290}
291
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200292static inline int sysfs_create_link_nowarn(struct kobject *kobj,
293 struct kobject *target,
294 const char *name)
295{
296 return 0;
297}
298
Tejun Heo59f69012007-09-20 16:05:10 +0900299static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800303static inline int sysfs_rename_link(struct kobject *k, struct kobject *t,
304 const char *old_name, const char *new_name)
305{
306 return 0;
307}
308
Eric W. Biederman746edb72010-03-30 11:31:28 -0700309static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
310 const char *name)
311{
312}
313
Tejun Heo59f69012007-09-20 16:05:10 +0900314static inline int sysfs_create_group(struct kobject *kobj,
315 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 return 0;
318}
319
Randy Dunlap1cbfb7a2008-04-30 09:01:17 -0700320static inline int sysfs_update_group(struct kobject *kobj,
321 const struct attribute_group *grp)
322{
323 return 0;
324}
325
Tejun Heo59f69012007-09-20 16:05:10 +0900326static inline void sysfs_remove_group(struct kobject *kobj,
327 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Alan Sterndfa87c82007-02-20 15:02:44 -0500331static inline int sysfs_add_file_to_group(struct kobject *kobj,
332 const struct attribute *attr, const char *group)
333{
334 return 0;
335}
336
337static inline void sysfs_remove_file_from_group(struct kobject *kobj,
Ralf Baechled701d8a2007-03-01 12:40:21 +0000338 const struct attribute *attr, const char *group)
Alan Sterndfa87c82007-02-20 15:02:44 -0500339{
Alan Sterndfa87c82007-02-20 15:02:44 -0500340}
341
Alan Stern69d44ff2010-09-25 23:34:22 +0200342static inline int sysfs_merge_group(struct kobject *kobj,
343 const struct attribute_group *grp)
344{
345 return 0;
346}
347
348static inline void sysfs_unmerge_group(struct kobject *kobj,
349 const struct attribute_group *grp)
350{
351}
352
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +0100353static inline int sysfs_add_link_to_group(struct kobject *kobj,
354 const char *group_name, struct kobject *target,
355 const char *link_name)
356{
357 return 0;
358}
359
360static inline void sysfs_remove_link_from_group(struct kobject *kobj,
361 const char *group_name, const char *link_name)
362{
363}
364
Trent Piepho8c0e3992008-09-25 16:45:13 -0700365static inline void sysfs_notify(struct kobject *kobj, const char *dir,
366 const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100367{
368}
Neil Brownf1282c82008-07-16 08:58:04 +1000369static inline void sysfs_notify_dirent(struct sysfs_dirent *sd)
370{
371}
372static inline
373struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700374 const void *ns,
Neil Brownf1282c82008-07-16 08:58:04 +1000375 const unsigned char *name)
376{
377 return NULL;
378}
379static inline struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
380{
381 return NULL;
382}
383static inline void sysfs_put(struct sysfs_dirent *sd)
384{
385}
NeilBrown4508a7a2006-03-20 17:53:53 +1100386
Andrew Mortonf20a9ea2006-08-14 22:43:23 -0700387static inline int __must_check sysfs_init(void)
388{
389 return 0;
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif /* CONFIG_SYSFS */
393
394#endif /* _SYSFS_H_ */