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