blob: 3c92121ba9afb3f75700e540e24c52f3aae8b974 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/atomic.h>
20
21struct kobject;
22struct module;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -070023enum kobj_ns_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25struct attribute {
Tejun Heo59f69012007-09-20 16:05:10 +090026 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 mode_t mode;
Eric W. Biederman6992f532010-02-11 15:21:53 -080028#ifdef CONFIG_DEBUG_LOCK_ALLOC
29 struct lock_class_key *key;
30 struct lock_class_key skey;
31#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
Eric W. Biederman35960252010-02-12 04:35:32 -080034/**
35 * sysfs_attr_init - initialize a dynamically allocated sysfs attribute
36 * @attr: struct attribute to initialize
37 *
38 * Initialize a dynamically allocated struct attribute so we can
39 * make lockdep happy. This is a new requirement for attributes
40 * and initially this is only needed when lockdep is enabled.
41 * Lockdep gives a nice error when your attribute is added to
42 * sysfs if you don't have this.
43 */
Eric W. Biederman6992f532010-02-11 15:21:53 -080044#ifdef CONFIG_DEBUG_LOCK_ALLOC
45#define sysfs_attr_init(attr) \
46do { \
47 static struct lock_class_key __key; \
48 \
49 (attr)->key = &__key; \
50} while(0)
51#else
52#define sysfs_attr_init(attr) do {} while(0)
53#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct attribute_group {
Tejun Heo59f69012007-09-20 16:05:10 +090056 const char *name;
James Bottomley0f423892008-03-20 20:47:52 -050057 mode_t (*is_visible)(struct kobject *,
James Bottomleyd4acd722007-10-31 09:38:04 -050058 struct attribute *, int);
Tejun Heo59f69012007-09-20 16:05:10 +090059 struct attribute **attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62
63
64/**
65 * Use these macros to make defining attributes easier. See include/linux/device.h
66 * for examples..
67 */
68
69#define __ATTR(_name,_mode,_show,_store) { \
Tejun Heo7b595752007-06-14 03:45:17 +090070 .attr = {.name = __stringify(_name), .mode = _mode }, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .show = _show, \
72 .store = _store, \
73}
74
75#define __ATTR_RO(_name) { \
Tejun Heo7b595752007-06-14 03:45:17 +090076 .attr = { .name = __stringify(_name), .mode = 0444 }, \
77 .show = _name##_show, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80#define __ATTR_NULL { .attr = { .name = NULL } }
81
82#define attr_name(_attr) (_attr).attr.name
83
Chris Wright2c3c8be2010-05-12 18:28:57 -070084struct file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085struct vm_area_struct;
86
87struct bin_attribute {
88 struct attribute attr;
89 size_t size;
90 void *private;
Chris Wright2c3c8be2010-05-12 18:28:57 -070091 ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
Zhang Rui91a69022007-06-09 13:57:22 +080092 char *, loff_t, size_t);
Chris Wright2c3c8be2010-05-12 18:28:57 -070093 ssize_t (*write)(struct file *,struct kobject *, struct bin_attribute *,
Zhang Rui91a69022007-06-09 13:57:22 +080094 char *, loff_t, size_t);
Chris Wright2c3c8be2010-05-12 18:28:57 -070095 int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct vm_area_struct *vma);
97};
98
Eric W. Biederman35960252010-02-12 04:35:32 -080099/**
100 * sysfs_bin_attr_init - initialize a dynamically allocated bin_attribute
101 * @attr: struct bin_attribute to initialize
102 *
103 * Initialize a dynamically allocated struct bin_attribute so we
104 * can make lockdep happy. This is a new requirement for
105 * attributes and initially this is only needed when lockdep is
106 * enabled. Lockdep gives a nice error when your attribute is
107 * added to sysfs if you don't have this.
108 */
Stephen Rothwell62e877b2010-03-01 20:38:36 +1100109#define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&(bin_attr)->attr)
Eric W. Biederman6992f532010-02-11 15:21:53 -0800110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct sysfs_ops {
112 ssize_t (*show)(struct kobject *, struct attribute *,char *);
113 ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
114};
115
Neil Brownf1282c82008-07-16 08:58:04 +1000116struct sysfs_dirent;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#ifdef CONFIG_SYSFS
119
Tejun Heo59f69012007-09-20 16:05:10 +0900120int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
121 void *data, struct module *owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400122
Tejun Heo59f69012007-09-20 16:05:10 +0900123int __must_check sysfs_create_dir(struct kobject *kobj);
124void sysfs_remove_dir(struct kobject *kobj);
125int __must_check sysfs_rename_dir(struct kobject *kobj, const char *new_name);
126int __must_check sysfs_move_dir(struct kobject *kobj,
127 struct kobject *new_parent_kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Tejun Heo59f69012007-09-20 16:05:10 +0900129int __must_check sysfs_create_file(struct kobject *kobj,
130 const struct attribute *attr);
Andi Kleen1c205ae2010-01-05 12:48:01 +0100131int __must_check sysfs_create_files(struct kobject *kobj,
132 const struct attribute **attr);
Jean Delvare49c19402010-07-02 16:54:05 +0200133int __must_check sysfs_chmod_file(struct kobject *kobj,
134 const struct attribute *attr, mode_t mode);
Tejun Heo59f69012007-09-20 16:05:10 +0900135void sysfs_remove_file(struct kobject *kobj, const struct attribute *attr);
Andi Kleen1c205ae2010-01-05 12:48:01 +0100136void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Andrew Morton4a7fb632006-08-14 22:43:17 -0700138int __must_check sysfs_create_bin_file(struct kobject *kobj,
Phil Carmody66ecb922009-12-18 15:34:20 +0200139 const struct bin_attribute *attr);
140void sysfs_remove_bin_file(struct kobject *kobj,
141 const struct bin_attribute *attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Tejun Heo59f69012007-09-20 16:05:10 +0900143int __must_check sysfs_create_link(struct kobject *kobj, struct kobject *target,
144 const char *name);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200145int __must_check sysfs_create_link_nowarn(struct kobject *kobj,
146 struct kobject *target,
147 const char *name);
Tejun Heo59f69012007-09-20 16:05:10 +0900148void sysfs_remove_link(struct kobject *kobj, const char *name);
Alan Sterndfa87c82007-02-20 15:02:44 -0500149
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800150int sysfs_rename_link(struct kobject *kobj, struct kobject *target,
151 const char *old_name, const char *new_name);
152
Eric W. Biederman746edb72010-03-30 11:31:28 -0700153void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
154 const char *name);
155
Tejun Heo59f69012007-09-20 16:05:10 +0900156int __must_check sysfs_create_group(struct kobject *kobj,
157 const struct attribute_group *grp);
James Bottomley0f423892008-03-20 20:47:52 -0500158int sysfs_update_group(struct kobject *kobj,
159 const struct attribute_group *grp);
Tejun Heo59f69012007-09-20 16:05:10 +0900160void sysfs_remove_group(struct kobject *kobj,
161 const struct attribute_group *grp);
162int sysfs_add_file_to_group(struct kobject *kobj,
163 const struct attribute *attr, const char *group);
164void sysfs_remove_file_from_group(struct kobject *kobj,
165 const struct attribute *attr, const char *group);
166
Trent Piepho8c0e3992008-09-25 16:45:13 -0700167void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr);
Neil Brownf1282c82008-07-16 08:58:04 +1000168void sysfs_notify_dirent(struct sysfs_dirent *sd);
169struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700170 const void *ns,
Neil Brownf1282c82008-07-16 08:58:04 +1000171 const unsigned char *name);
172struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd);
173void sysfs_put(struct sysfs_dirent *sd);
Andrew Mortonae872212007-08-24 16:11:54 -0700174void sysfs_printk_last_file(void);
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700175
Serge E. Hallynbe867b12010-05-03 16:23:15 -0500176/* Called to clear a ns tag when it is no longer valid */
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700177void sysfs_exit_ns(enum kobj_ns_type type, const void *tag);
178
Neil Brownf1282c82008-07-16 08:58:04 +1000179int __must_check sysfs_init(void);
Andrew Mortonf20a9ea2006-08-14 22:43:23 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#else /* CONFIG_SYSFS */
182
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400183static inline int sysfs_schedule_callback(struct kobject *kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700184 void (*func)(void *), void *data, struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400185{
186 return -ENOSYS;
187}
188
Tejun Heo59f69012007-09-20 16:05:10 +0900189static inline int sysfs_create_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 return 0;
192}
193
Tejun Heo59f69012007-09-20 16:05:10 +0900194static inline void sysfs_remove_dir(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Tejun Heo59f69012007-09-20 16:05:10 +0900198static inline int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Eric W. Biederman0b4a4fe2008-07-03 18:05:28 -0700200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Tejun Heo59f69012007-09-20 16:05:10 +0900203static inline int sysfs_move_dir(struct kobject *kobj,
204 struct kobject *new_parent_kobj)
Cornelia Huck8a824722006-11-20 17:07:51 +0100205{
206 return 0;
207}
208
Tejun Heo59f69012007-09-20 16:05:10 +0900209static inline int sysfs_create_file(struct kobject *kobj,
210 const struct attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 return 0;
213}
214
Andi Kleen1c205ae2010-01-05 12:48:01 +0100215static inline int sysfs_create_files(struct kobject *kobj,
216 const struct attribute **attr)
217{
218 return 0;
219}
220
Tejun Heo59f69012007-09-20 16:05:10 +0900221static inline int sysfs_chmod_file(struct kobject *kobj,
Jean Delvare49c19402010-07-02 16:54:05 +0200222 const struct attribute *attr, mode_t mode)
Kay Sievers31e5abe2005-04-18 21:57:32 -0700223{
224 return 0;
225}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Tejun Heo59f69012007-09-20 16:05:10 +0900227static inline void sysfs_remove_file(struct kobject *kobj,
228 const struct attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Andi Kleen1c205ae2010-01-05 12:48:01 +0100232static inline void sysfs_remove_files(struct kobject *kobj,
233 const struct attribute **attr)
234{
235}
236
Tejun Heo59f69012007-09-20 16:05:10 +0900237static inline int sysfs_create_bin_file(struct kobject *kobj,
Phil Carmody66ecb922009-12-18 15:34:20 +0200238 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 return 0;
241}
242
David Rientjes3612e062008-02-19 17:39:02 -0800243static inline void sysfs_remove_bin_file(struct kobject *kobj,
Phil Carmody66ecb922009-12-18 15:34:20 +0200244 const struct bin_attribute *attr)
Tejun Heo59f69012007-09-20 16:05:10 +0900245{
Tejun Heo59f69012007-09-20 16:05:10 +0900246}
247
248static inline int sysfs_create_link(struct kobject *kobj,
249 struct kobject *target, const char *name)
250{
251 return 0;
252}
253
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200254static inline int sysfs_create_link_nowarn(struct kobject *kobj,
255 struct kobject *target,
256 const char *name)
257{
258 return 0;
259}
260
Tejun Heo59f69012007-09-20 16:05:10 +0900261static inline void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800265static inline int sysfs_rename_link(struct kobject *k, struct kobject *t,
266 const char *old_name, const char *new_name)
267{
268 return 0;
269}
270
Eric W. Biederman746edb72010-03-30 11:31:28 -0700271static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
272 const char *name)
273{
274}
275
Tejun Heo59f69012007-09-20 16:05:10 +0900276static inline int sysfs_create_group(struct kobject *kobj,
277 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 return 0;
280}
281
Randy Dunlap1cbfb7a2008-04-30 09:01:17 -0700282static inline int sysfs_update_group(struct kobject *kobj,
283 const struct attribute_group *grp)
284{
285 return 0;
286}
287
Tejun Heo59f69012007-09-20 16:05:10 +0900288static inline void sysfs_remove_group(struct kobject *kobj,
289 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Alan Sterndfa87c82007-02-20 15:02:44 -0500293static inline int sysfs_add_file_to_group(struct kobject *kobj,
294 const struct attribute *attr, const char *group)
295{
296 return 0;
297}
298
299static inline void sysfs_remove_file_from_group(struct kobject *kobj,
Ralf Baechled701d8a2007-03-01 12:40:21 +0000300 const struct attribute *attr, const char *group)
Alan Sterndfa87c82007-02-20 15:02:44 -0500301{
Alan Sterndfa87c82007-02-20 15:02:44 -0500302}
303
Trent Piepho8c0e3992008-09-25 16:45:13 -0700304static inline void sysfs_notify(struct kobject *kobj, const char *dir,
305 const char *attr)
NeilBrown4508a7a2006-03-20 17:53:53 +1100306{
307}
Neil Brownf1282c82008-07-16 08:58:04 +1000308static inline void sysfs_notify_dirent(struct sysfs_dirent *sd)
309{
310}
311static inline
312struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700313 const void *ns,
Neil Brownf1282c82008-07-16 08:58:04 +1000314 const unsigned char *name)
315{
316 return NULL;
317}
318static inline struct sysfs_dirent *sysfs_get(struct sysfs_dirent *sd)
319{
320 return NULL;
321}
322static inline void sysfs_put(struct sysfs_dirent *sd)
323{
324}
NeilBrown4508a7a2006-03-20 17:53:53 +1100325
Eric W. Biederman27eabc72010-05-05 14:54:00 -0700326static inline void sysfs_exit_ns(int type, const void *tag)
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700327{
328}
329
Andrew Mortonf20a9ea2006-08-14 22:43:23 -0700330static inline int __must_check sysfs_init(void)
331{
332 return 0;
333}
334
Andrew Mortonae872212007-08-24 16:11:54 -0700335static inline void sysfs_printk_last_file(void)
336{
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#endif /* CONFIG_SYSFS */
340
341#endif /* _SYSFS_H_ */