Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * file.c - part of debugfs, a tiny little debug file system | |
3 | * | |
4 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> | |
5 | * Copyright (C) 2004 IBM Inc. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * debugfs is for people to use instead of /proc or /sys. | |
883ce42e | 12 | * See Documentation/DocBook/filesystems for more details. |
1da177e4 LT |
13 | * |
14 | */ | |
15 | ||
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/fs.h> | |
1a087c6a | 18 | #include <linux/seq_file.h> |
1da177e4 LT |
19 | #include <linux/pagemap.h> |
20 | #include <linux/debugfs.h> | |
03e099fb | 21 | #include <linux/io.h> |
9fe2a701 | 22 | #include <linux/slab.h> |
3a76e5e0 | 23 | #include <linux/atomic.h> |
98210b7f | 24 | #include <linux/device.h> |
9fd4dcec | 25 | #include <linux/srcu.h> |
49d200de | 26 | #include <asm/poll.h> |
9fd4dcec NS |
27 | |
28 | #include "internal.h" | |
1da177e4 | 29 | |
49d200de NS |
30 | struct poll_table_struct; |
31 | ||
1da177e4 LT |
32 | static ssize_t default_read_file(struct file *file, char __user *buf, |
33 | size_t count, loff_t *ppos) | |
34 | { | |
35 | return 0; | |
36 | } | |
37 | ||
38 | static ssize_t default_write_file(struct file *file, const char __user *buf, | |
39 | size_t count, loff_t *ppos) | |
40 | { | |
41 | return count; | |
42 | } | |
43 | ||
9fd4dcec | 44 | const struct file_operations debugfs_noop_file_operations = { |
1da177e4 LT |
45 | .read = default_read_file, |
46 | .write = default_write_file, | |
234e3405 | 47 | .open = simple_open, |
6038f373 | 48 | .llseek = noop_llseek, |
1da177e4 LT |
49 | }; |
50 | ||
9fd4dcec NS |
51 | /** |
52 | * debugfs_use_file_start - mark the beginning of file data access | |
53 | * @dentry: the dentry object whose data is being accessed. | |
54 | * @srcu_idx: a pointer to some memory to store a SRCU index in. | |
55 | * | |
56 | * Up to a matching call to debugfs_use_file_finish(), any | |
57 | * successive call into the file removing functions debugfs_remove() | |
58 | * and debugfs_remove_recursive() will block. Since associated private | |
59 | * file data may only get freed after a successful return of any of | |
60 | * the removal functions, you may safely access it after a successful | |
61 | * call to debugfs_use_file_start() without worrying about | |
62 | * lifetime issues. | |
63 | * | |
64 | * If -%EIO is returned, the file has already been removed and thus, | |
65 | * it is not safe to access any of its data. If, on the other hand, | |
66 | * it is allowed to access the file data, zero is returned. | |
67 | * | |
68 | * Regardless of the return code, any call to | |
69 | * debugfs_use_file_start() must be followed by a matching call | |
70 | * to debugfs_use_file_finish(). | |
71 | */ | |
49d200de | 72 | int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx) |
9fd4dcec NS |
73 | __acquires(&debugfs_srcu) |
74 | { | |
75 | *srcu_idx = srcu_read_lock(&debugfs_srcu); | |
76 | barrier(); | |
77 | if (d_unlinked(dentry)) | |
78 | return -EIO; | |
79 | return 0; | |
80 | } | |
49d200de | 81 | EXPORT_SYMBOL_GPL(debugfs_use_file_start); |
9fd4dcec NS |
82 | |
83 | /** | |
84 | * debugfs_use_file_finish - mark the end of file data access | |
85 | * @srcu_idx: the SRCU index "created" by a former call to | |
86 | * debugfs_use_file_start(). | |
87 | * | |
88 | * Allow any ongoing concurrent call into debugfs_remove() or | |
89 | * debugfs_remove_recursive() blocked by a former call to | |
90 | * debugfs_use_file_start() to proceed and return to its caller. | |
91 | */ | |
49d200de | 92 | void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu) |
9fd4dcec NS |
93 | { |
94 | srcu_read_unlock(&debugfs_srcu, srcu_idx); | |
95 | } | |
49d200de | 96 | EXPORT_SYMBOL_GPL(debugfs_use_file_finish); |
9fd4dcec NS |
97 | |
98 | #define F_DENTRY(filp) ((filp)->f_path.dentry) | |
99 | ||
100 | #define REAL_FOPS_DEREF(dentry) \ | |
101 | ((const struct file_operations *)(dentry)->d_fsdata) | |
102 | ||
103 | static int open_proxy_open(struct inode *inode, struct file *filp) | |
104 | { | |
105 | const struct dentry *dentry = F_DENTRY(filp); | |
106 | const struct file_operations *real_fops = NULL; | |
107 | int srcu_idx, r; | |
108 | ||
109 | r = debugfs_use_file_start(dentry, &srcu_idx); | |
110 | if (r) { | |
111 | r = -ENOENT; | |
112 | goto out; | |
113 | } | |
114 | ||
115 | real_fops = REAL_FOPS_DEREF(dentry); | |
116 | real_fops = fops_get(real_fops); | |
117 | if (!real_fops) { | |
118 | /* Huh? Module did not clean up after itself at exit? */ | |
119 | WARN(1, "debugfs file owner did not clean up at exit: %pd", | |
120 | dentry); | |
121 | r = -ENXIO; | |
122 | goto out; | |
123 | } | |
124 | replace_fops(filp, real_fops); | |
125 | ||
126 | if (real_fops->open) | |
127 | r = real_fops->open(inode, filp); | |
128 | ||
129 | out: | |
9fd4dcec NS |
130 | debugfs_use_file_finish(srcu_idx); |
131 | return r; | |
132 | } | |
133 | ||
134 | const struct file_operations debugfs_open_proxy_file_operations = { | |
135 | .open = open_proxy_open, | |
136 | }; | |
137 | ||
49d200de NS |
138 | #define PROTO(args...) args |
139 | #define ARGS(args...) args | |
140 | ||
141 | #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ | |
142 | static ret_type full_proxy_ ## name(proto) \ | |
143 | { \ | |
144 | const struct dentry *dentry = F_DENTRY(filp); \ | |
145 | const struct file_operations *real_fops = \ | |
146 | REAL_FOPS_DEREF(dentry); \ | |
147 | int srcu_idx; \ | |
148 | ret_type r; \ | |
149 | \ | |
150 | r = debugfs_use_file_start(dentry, &srcu_idx); \ | |
151 | if (likely(!r)) \ | |
152 | r = real_fops->name(args); \ | |
153 | debugfs_use_file_finish(srcu_idx); \ | |
154 | return r; \ | |
155 | } | |
156 | ||
157 | FULL_PROXY_FUNC(llseek, loff_t, filp, | |
158 | PROTO(struct file *filp, loff_t offset, int whence), | |
159 | ARGS(filp, offset, whence)); | |
160 | ||
161 | FULL_PROXY_FUNC(read, ssize_t, filp, | |
162 | PROTO(struct file *filp, char __user *buf, size_t size, | |
163 | loff_t *ppos), | |
164 | ARGS(filp, buf, size, ppos)); | |
165 | ||
166 | FULL_PROXY_FUNC(write, ssize_t, filp, | |
167 | PROTO(struct file *filp, const char __user *buf, size_t size, | |
168 | loff_t *ppos), | |
169 | ARGS(filp, buf, size, ppos)); | |
170 | ||
171 | FULL_PROXY_FUNC(unlocked_ioctl, long, filp, | |
172 | PROTO(struct file *filp, unsigned int cmd, unsigned long arg), | |
173 | ARGS(filp, cmd, arg)); | |
174 | ||
175 | static unsigned int full_proxy_poll(struct file *filp, | |
176 | struct poll_table_struct *wait) | |
177 | { | |
178 | const struct dentry *dentry = F_DENTRY(filp); | |
179 | const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry); | |
180 | int srcu_idx; | |
181 | unsigned int r = 0; | |
182 | ||
183 | if (debugfs_use_file_start(dentry, &srcu_idx)) { | |
184 | debugfs_use_file_finish(srcu_idx); | |
185 | return POLLHUP; | |
186 | } | |
187 | ||
188 | r = real_fops->poll(filp, wait); | |
189 | debugfs_use_file_finish(srcu_idx); | |
190 | return r; | |
191 | } | |
192 | ||
193 | static int full_proxy_release(struct inode *inode, struct file *filp) | |
194 | { | |
195 | const struct dentry *dentry = F_DENTRY(filp); | |
196 | const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry); | |
197 | const struct file_operations *proxy_fops = filp->f_op; | |
198 | int r = 0; | |
199 | ||
200 | /* | |
201 | * We must not protect this against removal races here: the | |
202 | * original releaser should be called unconditionally in order | |
203 | * not to leak any resources. Releasers must not assume that | |
204 | * ->i_private is still being meaningful here. | |
205 | */ | |
206 | if (real_fops->release) | |
207 | r = real_fops->release(inode, filp); | |
208 | ||
209 | replace_fops(filp, d_inode(dentry)->i_fop); | |
210 | kfree((void *)proxy_fops); | |
211 | fops_put(real_fops); | |
212 | return 0; | |
213 | } | |
214 | ||
215 | static void __full_proxy_fops_init(struct file_operations *proxy_fops, | |
216 | const struct file_operations *real_fops) | |
217 | { | |
218 | proxy_fops->release = full_proxy_release; | |
219 | if (real_fops->llseek) | |
220 | proxy_fops->llseek = full_proxy_llseek; | |
221 | if (real_fops->read) | |
222 | proxy_fops->read = full_proxy_read; | |
223 | if (real_fops->write) | |
224 | proxy_fops->write = full_proxy_write; | |
225 | if (real_fops->poll) | |
226 | proxy_fops->poll = full_proxy_poll; | |
227 | if (real_fops->unlocked_ioctl) | |
228 | proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; | |
229 | } | |
230 | ||
231 | static int full_proxy_open(struct inode *inode, struct file *filp) | |
232 | { | |
233 | const struct dentry *dentry = F_DENTRY(filp); | |
234 | const struct file_operations *real_fops = NULL; | |
235 | struct file_operations *proxy_fops = NULL; | |
236 | int srcu_idx, r; | |
237 | ||
238 | r = debugfs_use_file_start(dentry, &srcu_idx); | |
239 | if (r) { | |
240 | r = -ENOENT; | |
241 | goto out; | |
242 | } | |
243 | ||
244 | real_fops = REAL_FOPS_DEREF(dentry); | |
245 | real_fops = fops_get(real_fops); | |
246 | if (!real_fops) { | |
247 | /* Huh? Module did not cleanup after itself at exit? */ | |
248 | WARN(1, "debugfs file owner did not clean up at exit: %pd", | |
249 | dentry); | |
250 | r = -ENXIO; | |
251 | goto out; | |
252 | } | |
253 | ||
254 | proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); | |
255 | if (!proxy_fops) { | |
256 | r = -ENOMEM; | |
257 | goto free_proxy; | |
258 | } | |
259 | __full_proxy_fops_init(proxy_fops, real_fops); | |
260 | replace_fops(filp, proxy_fops); | |
261 | ||
262 | if (real_fops->open) { | |
263 | r = real_fops->open(inode, filp); | |
b10e3e90 NS |
264 | if (r) { |
265 | replace_fops(filp, d_inode(dentry)->i_fop); | |
266 | goto free_proxy; | |
267 | } else if (filp->f_op != proxy_fops) { | |
49d200de NS |
268 | /* No protection against file removal anymore. */ |
269 | WARN(1, "debugfs file owner replaced proxy fops: %pd", | |
270 | dentry); | |
271 | goto free_proxy; | |
272 | } | |
273 | } | |
274 | ||
275 | goto out; | |
276 | free_proxy: | |
277 | kfree(proxy_fops); | |
278 | fops_put(real_fops); | |
279 | out: | |
280 | debugfs_use_file_finish(srcu_idx); | |
281 | return r; | |
282 | } | |
283 | ||
284 | const struct file_operations debugfs_full_proxy_file_operations = { | |
285 | .open = full_proxy_open, | |
286 | }; | |
287 | ||
c6468808 NS |
288 | ssize_t debugfs_attr_read(struct file *file, char __user *buf, |
289 | size_t len, loff_t *ppos) | |
290 | { | |
291 | ssize_t ret; | |
292 | int srcu_idx; | |
293 | ||
294 | ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); | |
295 | if (likely(!ret)) | |
296 | ret = simple_attr_read(file, buf, len, ppos); | |
297 | debugfs_use_file_finish(srcu_idx); | |
298 | return ret; | |
299 | } | |
300 | EXPORT_SYMBOL_GPL(debugfs_attr_read); | |
301 | ||
302 | ssize_t debugfs_attr_write(struct file *file, const char __user *buf, | |
303 | size_t len, loff_t *ppos) | |
304 | { | |
305 | ssize_t ret; | |
306 | int srcu_idx; | |
307 | ||
308 | ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); | |
309 | if (likely(!ret)) | |
310 | ret = simple_attr_write(file, buf, len, ppos); | |
311 | debugfs_use_file_finish(srcu_idx); | |
312 | return ret; | |
313 | } | |
314 | EXPORT_SYMBOL_GPL(debugfs_attr_write); | |
315 | ||
4909f168 NS |
316 | static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, |
317 | struct dentry *parent, void *value, | |
318 | const struct file_operations *fops, | |
319 | const struct file_operations *fops_ro, | |
320 | const struct file_operations *fops_wo) | |
321 | { | |
322 | /* if there are no write bits set, make read only */ | |
323 | if (!(mode & S_IWUGO)) | |
324 | return debugfs_create_file_unsafe(name, mode, parent, value, | |
325 | fops_ro); | |
326 | /* if there are no read bits set, make write only */ | |
327 | if (!(mode & S_IRUGO)) | |
328 | return debugfs_create_file_unsafe(name, mode, parent, value, | |
329 | fops_wo); | |
330 | ||
331 | return debugfs_create_file_unsafe(name, mode, parent, value, fops); | |
332 | } | |
333 | ||
8b88b099 | 334 | static int debugfs_u8_set(void *data, u64 val) |
acaefc25 AB |
335 | { |
336 | *(u8 *)data = val; | |
8b88b099 | 337 | return 0; |
acaefc25 | 338 | } |
8b88b099 | 339 | static int debugfs_u8_get(void *data, u64 *val) |
acaefc25 | 340 | { |
8b88b099 CH |
341 | *val = *(u8 *)data; |
342 | return 0; | |
acaefc25 | 343 | } |
4909f168 NS |
344 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); |
345 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); | |
346 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); | |
1da177e4 LT |
347 | |
348 | /** | |
6468b3af | 349 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
1da177e4 LT |
350 | * @name: a pointer to a string containing the name of the file to create. |
351 | * @mode: the permission that the file should have | |
352 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 353 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
354 | * file will be created in the root of the debugfs filesystem. |
355 | * @value: a pointer to the variable that the file should read to and write | |
356 | * from. | |
357 | * | |
358 | * This function creates a file in debugfs with the given name that | |
359 | * contains the value of the variable @value. If the @mode variable is so | |
360 | * set, it can be read from, and written to. | |
361 | * | |
362 | * This function will return a pointer to a dentry if it succeeds. This | |
363 | * pointer must be passed to the debugfs_remove() function when the file is | |
364 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 365 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 366 | * |
6468b3af | 367 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 368 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 369 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
370 | * code. |
371 | */ | |
f4ae40a6 | 372 | struct dentry *debugfs_create_u8(const char *name, umode_t mode, |
1da177e4 LT |
373 | struct dentry *parent, u8 *value) |
374 | { | |
4909f168 | 375 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, |
b97f6799 | 376 | &fops_u8_ro, &fops_u8_wo); |
1da177e4 LT |
377 | } |
378 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | |
379 | ||
8b88b099 | 380 | static int debugfs_u16_set(void *data, u64 val) |
acaefc25 AB |
381 | { |
382 | *(u16 *)data = val; | |
8b88b099 | 383 | return 0; |
acaefc25 | 384 | } |
8b88b099 | 385 | static int debugfs_u16_get(void *data, u64 *val) |
acaefc25 | 386 | { |
8b88b099 CH |
387 | *val = *(u16 *)data; |
388 | return 0; | |
acaefc25 | 389 | } |
4909f168 NS |
390 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); |
391 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); | |
392 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); | |
acaefc25 | 393 | |
1da177e4 | 394 | /** |
6468b3af | 395 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
1da177e4 LT |
396 | * @name: a pointer to a string containing the name of the file to create. |
397 | * @mode: the permission that the file should have | |
398 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 399 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
400 | * file will be created in the root of the debugfs filesystem. |
401 | * @value: a pointer to the variable that the file should read to and write | |
402 | * from. | |
403 | * | |
404 | * This function creates a file in debugfs with the given name that | |
405 | * contains the value of the variable @value. If the @mode variable is so | |
406 | * set, it can be read from, and written to. | |
407 | * | |
408 | * This function will return a pointer to a dentry if it succeeds. This | |
409 | * pointer must be passed to the debugfs_remove() function when the file is | |
410 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 411 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 412 | * |
6468b3af | 413 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 414 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 415 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
416 | * code. |
417 | */ | |
f4ae40a6 | 418 | struct dentry *debugfs_create_u16(const char *name, umode_t mode, |
1da177e4 LT |
419 | struct dentry *parent, u16 *value) |
420 | { | |
4909f168 | 421 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, |
b97f6799 | 422 | &fops_u16_ro, &fops_u16_wo); |
1da177e4 LT |
423 | } |
424 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | |
425 | ||
8b88b099 | 426 | static int debugfs_u32_set(void *data, u64 val) |
acaefc25 AB |
427 | { |
428 | *(u32 *)data = val; | |
8b88b099 | 429 | return 0; |
acaefc25 | 430 | } |
8b88b099 | 431 | static int debugfs_u32_get(void *data, u64 *val) |
acaefc25 | 432 | { |
8b88b099 CH |
433 | *val = *(u32 *)data; |
434 | return 0; | |
acaefc25 | 435 | } |
4909f168 NS |
436 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); |
437 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); | |
438 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); | |
acaefc25 | 439 | |
1da177e4 | 440 | /** |
6468b3af | 441 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
1da177e4 LT |
442 | * @name: a pointer to a string containing the name of the file to create. |
443 | * @mode: the permission that the file should have | |
444 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 445 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
446 | * file will be created in the root of the debugfs filesystem. |
447 | * @value: a pointer to the variable that the file should read to and write | |
448 | * from. | |
449 | * | |
450 | * This function creates a file in debugfs with the given name that | |
451 | * contains the value of the variable @value. If the @mode variable is so | |
452 | * set, it can be read from, and written to. | |
453 | * | |
454 | * This function will return a pointer to a dentry if it succeeds. This | |
455 | * pointer must be passed to the debugfs_remove() function when the file is | |
456 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 457 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 458 | * |
6468b3af | 459 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 460 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 461 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
462 | * code. |
463 | */ | |
f4ae40a6 | 464 | struct dentry *debugfs_create_u32(const char *name, umode_t mode, |
1da177e4 LT |
465 | struct dentry *parent, u32 *value) |
466 | { | |
4909f168 | 467 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, |
b97f6799 | 468 | &fops_u32_ro, &fops_u32_wo); |
1da177e4 LT |
469 | } |
470 | EXPORT_SYMBOL_GPL(debugfs_create_u32); | |
471 | ||
8b88b099 | 472 | static int debugfs_u64_set(void *data, u64 val) |
8447891f ME |
473 | { |
474 | *(u64 *)data = val; | |
8b88b099 | 475 | return 0; |
8447891f ME |
476 | } |
477 | ||
8b88b099 | 478 | static int debugfs_u64_get(void *data, u64 *val) |
8447891f | 479 | { |
8b88b099 CH |
480 | *val = *(u64 *)data; |
481 | return 0; | |
8447891f | 482 | } |
4909f168 NS |
483 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); |
484 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); | |
485 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); | |
8447891f ME |
486 | |
487 | /** | |
488 | * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
489 | * @name: a pointer to a string containing the name of the file to create. | |
490 | * @mode: the permission that the file should have | |
491 | * @parent: a pointer to the parent dentry for this file. This should be a | |
492 | * directory dentry if set. If this parameter is %NULL, then the | |
493 | * file will be created in the root of the debugfs filesystem. | |
494 | * @value: a pointer to the variable that the file should read to and write | |
495 | * from. | |
496 | * | |
497 | * This function creates a file in debugfs with the given name that | |
498 | * contains the value of the variable @value. If the @mode variable is so | |
499 | * set, it can be read from, and written to. | |
500 | * | |
501 | * This function will return a pointer to a dentry if it succeeds. This | |
502 | * pointer must be passed to the debugfs_remove() function when the file is | |
503 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
504 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
505 | * | |
506 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
507 | * returned. It is not wise to check for this value, but rather, check for | |
508 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
509 | * code. | |
510 | */ | |
f4ae40a6 | 511 | struct dentry *debugfs_create_u64(const char *name, umode_t mode, |
8447891f ME |
512 | struct dentry *parent, u64 *value) |
513 | { | |
4909f168 | 514 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, |
b97f6799 | 515 | &fops_u64_ro, &fops_u64_wo); |
8447891f ME |
516 | } |
517 | EXPORT_SYMBOL_GPL(debugfs_create_u64); | |
518 | ||
c23fe831 VK |
519 | static int debugfs_ulong_set(void *data, u64 val) |
520 | { | |
521 | *(unsigned long *)data = val; | |
522 | return 0; | |
523 | } | |
524 | ||
525 | static int debugfs_ulong_get(void *data, u64 *val) | |
526 | { | |
527 | *val = *(unsigned long *)data; | |
528 | return 0; | |
529 | } | |
4909f168 NS |
530 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, |
531 | "%llu\n"); | |
532 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); | |
533 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); | |
c23fe831 VK |
534 | |
535 | /** | |
536 | * debugfs_create_ulong - create a debugfs file that is used to read and write | |
537 | * an unsigned long value. | |
538 | * @name: a pointer to a string containing the name of the file to create. | |
539 | * @mode: the permission that the file should have | |
540 | * @parent: a pointer to the parent dentry for this file. This should be a | |
541 | * directory dentry if set. If this parameter is %NULL, then the | |
542 | * file will be created in the root of the debugfs filesystem. | |
543 | * @value: a pointer to the variable that the file should read to and write | |
544 | * from. | |
545 | * | |
546 | * This function creates a file in debugfs with the given name that | |
547 | * contains the value of the variable @value. If the @mode variable is so | |
548 | * set, it can be read from, and written to. | |
549 | * | |
550 | * This function will return a pointer to a dentry if it succeeds. This | |
551 | * pointer must be passed to the debugfs_remove() function when the file is | |
552 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
553 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
554 | * | |
555 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
556 | * returned. It is not wise to check for this value, but rather, check for | |
557 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
558 | * code. | |
559 | */ | |
560 | struct dentry *debugfs_create_ulong(const char *name, umode_t mode, | |
561 | struct dentry *parent, unsigned long *value) | |
562 | { | |
4909f168 NS |
563 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
564 | &fops_ulong, &fops_ulong_ro, | |
565 | &fops_ulong_wo); | |
c23fe831 VK |
566 | } |
567 | EXPORT_SYMBOL_GPL(debugfs_create_ulong); | |
568 | ||
4909f168 NS |
569 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
570 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); | |
571 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); | |
2ebefc50 | 572 | |
4909f168 NS |
573 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, |
574 | "0x%04llx\n"); | |
575 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); | |
576 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); | |
2ebefc50 | 577 | |
4909f168 NS |
578 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, |
579 | "0x%08llx\n"); | |
580 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); | |
581 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); | |
2ebefc50 | 582 | |
4909f168 NS |
583 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, |
584 | "0x%016llx\n"); | |
585 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); | |
586 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); | |
15b0beaa | 587 | |
e6716b87 | 588 | /* |
15b0beaa | 589 | * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value |
2ebefc50 | 590 | * |
e6716b87 RD |
591 | * These functions are exactly the same as the above functions (but use a hex |
592 | * output for the decimal challenged). For details look at the above unsigned | |
2ebefc50 RG |
593 | * decimal functions. |
594 | */ | |
e6716b87 RD |
595 | |
596 | /** | |
597 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value | |
598 | * @name: a pointer to a string containing the name of the file to create. | |
599 | * @mode: the permission that the file should have | |
600 | * @parent: a pointer to the parent dentry for this file. This should be a | |
601 | * directory dentry if set. If this parameter is %NULL, then the | |
602 | * file will be created in the root of the debugfs filesystem. | |
603 | * @value: a pointer to the variable that the file should read to and write | |
604 | * from. | |
605 | */ | |
f4ae40a6 | 606 | struct dentry *debugfs_create_x8(const char *name, umode_t mode, |
2ebefc50 RG |
607 | struct dentry *parent, u8 *value) |
608 | { | |
4909f168 | 609 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, |
b97f6799 | 610 | &fops_x8_ro, &fops_x8_wo); |
2ebefc50 RG |
611 | } |
612 | EXPORT_SYMBOL_GPL(debugfs_create_x8); | |
613 | ||
e6716b87 RD |
614 | /** |
615 | * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value | |
616 | * @name: a pointer to a string containing the name of the file to create. | |
617 | * @mode: the permission that the file should have | |
618 | * @parent: a pointer to the parent dentry for this file. This should be a | |
619 | * directory dentry if set. If this parameter is %NULL, then the | |
620 | * file will be created in the root of the debugfs filesystem. | |
621 | * @value: a pointer to the variable that the file should read to and write | |
622 | * from. | |
623 | */ | |
f4ae40a6 | 624 | struct dentry *debugfs_create_x16(const char *name, umode_t mode, |
2ebefc50 RG |
625 | struct dentry *parent, u16 *value) |
626 | { | |
4909f168 | 627 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, |
b97f6799 | 628 | &fops_x16_ro, &fops_x16_wo); |
2ebefc50 RG |
629 | } |
630 | EXPORT_SYMBOL_GPL(debugfs_create_x16); | |
631 | ||
e6716b87 RD |
632 | /** |
633 | * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value | |
634 | * @name: a pointer to a string containing the name of the file to create. | |
635 | * @mode: the permission that the file should have | |
636 | * @parent: a pointer to the parent dentry for this file. This should be a | |
637 | * directory dentry if set. If this parameter is %NULL, then the | |
638 | * file will be created in the root of the debugfs filesystem. | |
639 | * @value: a pointer to the variable that the file should read to and write | |
640 | * from. | |
641 | */ | |
f4ae40a6 | 642 | struct dentry *debugfs_create_x32(const char *name, umode_t mode, |
2ebefc50 RG |
643 | struct dentry *parent, u32 *value) |
644 | { | |
4909f168 | 645 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, |
b97f6799 | 646 | &fops_x32_ro, &fops_x32_wo); |
2ebefc50 RG |
647 | } |
648 | EXPORT_SYMBOL_GPL(debugfs_create_x32); | |
649 | ||
15b0beaa HY |
650 | /** |
651 | * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
652 | * @name: a pointer to a string containing the name of the file to create. | |
653 | * @mode: the permission that the file should have | |
654 | * @parent: a pointer to the parent dentry for this file. This should be a | |
655 | * directory dentry if set. If this parameter is %NULL, then the | |
656 | * file will be created in the root of the debugfs filesystem. | |
657 | * @value: a pointer to the variable that the file should read to and write | |
658 | * from. | |
659 | */ | |
f4ae40a6 | 660 | struct dentry *debugfs_create_x64(const char *name, umode_t mode, |
15b0beaa HY |
661 | struct dentry *parent, u64 *value) |
662 | { | |
4909f168 | 663 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, |
82b7d4fb | 664 | &fops_x64_ro, &fops_x64_wo); |
15b0beaa HY |
665 | } |
666 | EXPORT_SYMBOL_GPL(debugfs_create_x64); | |
667 | ||
5e078787 IPG |
668 | |
669 | static int debugfs_size_t_set(void *data, u64 val) | |
670 | { | |
671 | *(size_t *)data = val; | |
672 | return 0; | |
673 | } | |
674 | static int debugfs_size_t_get(void *data, u64 *val) | |
675 | { | |
676 | *val = *(size_t *)data; | |
677 | return 0; | |
678 | } | |
4909f168 NS |
679 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, |
680 | "%llu\n"); /* %llu and %zu are more or less the same */ | |
681 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); | |
682 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); | |
5e078787 IPG |
683 | |
684 | /** | |
685 | * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value | |
686 | * @name: a pointer to a string containing the name of the file to create. | |
687 | * @mode: the permission that the file should have | |
688 | * @parent: a pointer to the parent dentry for this file. This should be a | |
689 | * directory dentry if set. If this parameter is %NULL, then the | |
690 | * file will be created in the root of the debugfs filesystem. | |
691 | * @value: a pointer to the variable that the file should read to and write | |
692 | * from. | |
693 | */ | |
f4ae40a6 | 694 | struct dentry *debugfs_create_size_t(const char *name, umode_t mode, |
5e078787 IPG |
695 | struct dentry *parent, size_t *value) |
696 | { | |
4909f168 NS |
697 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
698 | &fops_size_t, &fops_size_t_ro, | |
699 | &fops_size_t_wo); | |
5e078787 IPG |
700 | } |
701 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); | |
702 | ||
3a76e5e0 SJ |
703 | static int debugfs_atomic_t_set(void *data, u64 val) |
704 | { | |
705 | atomic_set((atomic_t *)data, val); | |
706 | return 0; | |
707 | } | |
708 | static int debugfs_atomic_t_get(void *data, u64 *val) | |
709 | { | |
710 | *val = atomic_read((atomic_t *)data); | |
711 | return 0; | |
712 | } | |
4909f168 | 713 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, |
3a76e5e0 | 714 | debugfs_atomic_t_set, "%lld\n"); |
4909f168 NS |
715 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, |
716 | "%lld\n"); | |
717 | DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, | |
718 | "%lld\n"); | |
3a76e5e0 SJ |
719 | |
720 | /** | |
721 | * debugfs_create_atomic_t - create a debugfs file that is used to read and | |
722 | * write an atomic_t value | |
723 | * @name: a pointer to a string containing the name of the file to create. | |
724 | * @mode: the permission that the file should have | |
725 | * @parent: a pointer to the parent dentry for this file. This should be a | |
726 | * directory dentry if set. If this parameter is %NULL, then the | |
727 | * file will be created in the root of the debugfs filesystem. | |
728 | * @value: a pointer to the variable that the file should read to and write | |
729 | * from. | |
730 | */ | |
731 | struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, | |
732 | struct dentry *parent, atomic_t *value) | |
733 | { | |
4909f168 NS |
734 | return debugfs_create_mode_unsafe(name, mode, parent, value, |
735 | &fops_atomic_t, &fops_atomic_t_ro, | |
736 | &fops_atomic_t_wo); | |
3a76e5e0 SJ |
737 | } |
738 | EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); | |
5e078787 | 739 | |
0642ef6f RF |
740 | ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, |
741 | size_t count, loff_t *ppos) | |
1da177e4 LT |
742 | { |
743 | char buf[3]; | |
4d45f797 NS |
744 | bool val; |
745 | int r, srcu_idx; | |
746 | ||
747 | r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); | |
748 | if (likely(!r)) | |
749 | val = *(bool *)file->private_data; | |
750 | debugfs_use_file_finish(srcu_idx); | |
751 | if (r) | |
752 | return r; | |
88e412ea | 753 | |
4d45f797 | 754 | if (val) |
1da177e4 LT |
755 | buf[0] = 'Y'; |
756 | else | |
757 | buf[0] = 'N'; | |
758 | buf[1] = '\n'; | |
759 | buf[2] = 0x00; | |
760 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
761 | } | |
0642ef6f | 762 | EXPORT_SYMBOL_GPL(debugfs_read_file_bool); |
1da177e4 | 763 | |
0642ef6f RF |
764 | ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, |
765 | size_t count, loff_t *ppos) | |
1da177e4 LT |
766 | { |
767 | char buf[32]; | |
c42d2237 | 768 | size_t buf_size; |
8705b48e | 769 | bool bv; |
4d45f797 | 770 | int r, srcu_idx; |
621a5f7a | 771 | bool *val = file->private_data; |
1da177e4 LT |
772 | |
773 | buf_size = min(count, (sizeof(buf)-1)); | |
774 | if (copy_from_user(buf, user_buf, buf_size)) | |
775 | return -EFAULT; | |
776 | ||
a3b2c8c7 | 777 | buf[buf_size] = '\0'; |
4d45f797 NS |
778 | if (strtobool(buf, &bv) == 0) { |
779 | r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); | |
780 | if (likely(!r)) | |
781 | *val = bv; | |
782 | debugfs_use_file_finish(srcu_idx); | |
783 | if (r) | |
784 | return r; | |
785 | } | |
8705b48e | 786 | |
1da177e4 LT |
787 | return count; |
788 | } | |
0642ef6f | 789 | EXPORT_SYMBOL_GPL(debugfs_write_file_bool); |
1da177e4 | 790 | |
4b6f5d20 | 791 | static const struct file_operations fops_bool = { |
0642ef6f RF |
792 | .read = debugfs_read_file_bool, |
793 | .write = debugfs_write_file_bool, | |
234e3405 | 794 | .open = simple_open, |
6038f373 | 795 | .llseek = default_llseek, |
1da177e4 LT |
796 | }; |
797 | ||
6713e8fb SB |
798 | static const struct file_operations fops_bool_ro = { |
799 | .read = debugfs_read_file_bool, | |
800 | .open = simple_open, | |
801 | .llseek = default_llseek, | |
802 | }; | |
803 | ||
804 | static const struct file_operations fops_bool_wo = { | |
805 | .write = debugfs_write_file_bool, | |
806 | .open = simple_open, | |
807 | .llseek = default_llseek, | |
808 | }; | |
809 | ||
1da177e4 | 810 | /** |
6468b3af | 811 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
1da177e4 LT |
812 | * @name: a pointer to a string containing the name of the file to create. |
813 | * @mode: the permission that the file should have | |
814 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 815 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
816 | * file will be created in the root of the debugfs filesystem. |
817 | * @value: a pointer to the variable that the file should read to and write | |
818 | * from. | |
819 | * | |
820 | * This function creates a file in debugfs with the given name that | |
821 | * contains the value of the variable @value. If the @mode variable is so | |
822 | * set, it can be read from, and written to. | |
823 | * | |
824 | * This function will return a pointer to a dentry if it succeeds. This | |
825 | * pointer must be passed to the debugfs_remove() function when the file is | |
826 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 827 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 828 | * |
6468b3af | 829 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 830 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 831 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
832 | * code. |
833 | */ | |
f4ae40a6 | 834 | struct dentry *debugfs_create_bool(const char *name, umode_t mode, |
621a5f7a | 835 | struct dentry *parent, bool *value) |
1da177e4 | 836 | { |
4d45f797 | 837 | return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, |
6713e8fb | 838 | &fops_bool_ro, &fops_bool_wo); |
1da177e4 LT |
839 | } |
840 | EXPORT_SYMBOL_GPL(debugfs_create_bool); | |
841 | ||
dd308bc3 ME |
842 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
843 | size_t count, loff_t *ppos) | |
844 | { | |
845 | struct debugfs_blob_wrapper *blob = file->private_data; | |
83b711cb NS |
846 | ssize_t r; |
847 | int srcu_idx; | |
848 | ||
849 | r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx); | |
850 | if (likely(!r)) | |
851 | r = simple_read_from_buffer(user_buf, count, ppos, blob->data, | |
852 | blob->size); | |
853 | debugfs_use_file_finish(srcu_idx); | |
854 | return r; | |
dd308bc3 ME |
855 | } |
856 | ||
00977a59 | 857 | static const struct file_operations fops_blob = { |
dd308bc3 | 858 | .read = read_file_blob, |
234e3405 | 859 | .open = simple_open, |
6038f373 | 860 | .llseek = default_llseek, |
dd308bc3 ME |
861 | }; |
862 | ||
863 | /** | |
400ced61 | 864 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
dd308bc3 ME |
865 | * @name: a pointer to a string containing the name of the file to create. |
866 | * @mode: the permission that the file should have | |
867 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 868 | * directory dentry if set. If this parameter is %NULL, then the |
dd308bc3 ME |
869 | * file will be created in the root of the debugfs filesystem. |
870 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer | |
871 | * to the blob data and the size of the data. | |
872 | * | |
873 | * This function creates a file in debugfs with the given name that exports | |
874 | * @blob->data as a binary blob. If the @mode variable is so set it can be | |
875 | * read from. Writing is not supported. | |
876 | * | |
877 | * This function will return a pointer to a dentry if it succeeds. This | |
878 | * pointer must be passed to the debugfs_remove() function when the file is | |
879 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 880 | * you are responsible here.) If an error occurs, %NULL will be returned. |
dd308bc3 | 881 | * |
6468b3af | 882 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
dd308bc3 | 883 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 884 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
dd308bc3 ME |
885 | * code. |
886 | */ | |
f4ae40a6 | 887 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, |
dd308bc3 ME |
888 | struct dentry *parent, |
889 | struct debugfs_blob_wrapper *blob) | |
890 | { | |
83b711cb | 891 | return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob); |
dd308bc3 ME |
892 | } |
893 | EXPORT_SYMBOL_GPL(debugfs_create_blob); | |
1a087c6a | 894 | |
9fe2a701 SV |
895 | struct array_data { |
896 | void *array; | |
897 | u32 elements; | |
898 | }; | |
899 | ||
e05e279e LT |
900 | static size_t u32_format_array(char *buf, size_t bufsize, |
901 | u32 *array, int array_size) | |
9fe2a701 SV |
902 | { |
903 | size_t ret = 0; | |
9fe2a701 | 904 | |
e05e279e | 905 | while (--array_size >= 0) { |
9fe2a701 | 906 | size_t len; |
e05e279e | 907 | char term = array_size ? ' ' : '\n'; |
9fe2a701 | 908 | |
e05e279e | 909 | len = snprintf(buf, bufsize, "%u%c", *array++, term); |
9fe2a701 SV |
910 | ret += len; |
911 | ||
e05e279e LT |
912 | buf += len; |
913 | bufsize -= len; | |
9fe2a701 | 914 | } |
9fe2a701 SV |
915 | return ret; |
916 | } | |
917 | ||
36048853 | 918 | static int u32_array_open(struct inode *inode, struct file *file) |
9fe2a701 | 919 | { |
9fe2a701 | 920 | struct array_data *data = inode->i_private; |
e05e279e LT |
921 | int size, elements = data->elements; |
922 | char *buf; | |
923 | ||
924 | /* | |
925 | * Max size: | |
926 | * - 10 digits + ' '/'\n' = 11 bytes per number | |
927 | * - terminating NUL character | |
928 | */ | |
929 | size = elements*11; | |
930 | buf = kmalloc(size+1, GFP_KERNEL); | |
931 | if (!buf) | |
36048853 | 932 | return -ENOMEM; |
e05e279e LT |
933 | buf[size] = 0; |
934 | ||
935 | file->private_data = buf; | |
936 | u32_format_array(buf, size, data->array, data->elements); | |
937 | ||
36048853 DR |
938 | return nonseekable_open(inode, file); |
939 | } | |
9fe2a701 | 940 | |
36048853 DR |
941 | static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, |
942 | loff_t *ppos) | |
943 | { | |
944 | size_t size = strlen(file->private_data); | |
9fe2a701 SV |
945 | |
946 | return simple_read_from_buffer(buf, len, ppos, | |
947 | file->private_data, size); | |
948 | } | |
949 | ||
950 | static int u32_array_release(struct inode *inode, struct file *file) | |
951 | { | |
952 | kfree(file->private_data); | |
953 | ||
954 | return 0; | |
955 | } | |
956 | ||
957 | static const struct file_operations u32_array_fops = { | |
958 | .owner = THIS_MODULE, | |
959 | .open = u32_array_open, | |
960 | .release = u32_array_release, | |
961 | .read = u32_array_read, | |
962 | .llseek = no_llseek, | |
963 | }; | |
964 | ||
965 | /** | |
966 | * debugfs_create_u32_array - create a debugfs file that is used to read u32 | |
967 | * array. | |
968 | * @name: a pointer to a string containing the name of the file to create. | |
969 | * @mode: the permission that the file should have. | |
970 | * @parent: a pointer to the parent dentry for this file. This should be a | |
971 | * directory dentry if set. If this parameter is %NULL, then the | |
972 | * file will be created in the root of the debugfs filesystem. | |
973 | * @array: u32 array that provides data. | |
974 | * @elements: total number of elements in the array. | |
975 | * | |
976 | * This function creates a file in debugfs with the given name that exports | |
977 | * @array as data. If the @mode variable is so set it can be read from. | |
978 | * Writing is not supported. Seek within the file is also not supported. | |
979 | * Once array is created its size can not be changed. | |
980 | * | |
981 | * The function returns a pointer to dentry on success. If debugfs is not | |
982 | * enabled in the kernel, the value -%ENODEV will be returned. | |
983 | */ | |
984 | struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, | |
985 | struct dentry *parent, | |
986 | u32 *array, u32 elements) | |
987 | { | |
988 | struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); | |
989 | ||
990 | if (data == NULL) | |
991 | return NULL; | |
992 | ||
993 | data->array = array; | |
994 | data->elements = elements; | |
995 | ||
c4a74f63 NS |
996 | return debugfs_create_file_unsafe(name, mode, parent, data, |
997 | &u32_array_fops); | |
9fe2a701 SV |
998 | } |
999 | EXPORT_SYMBOL_GPL(debugfs_create_u32_array); | |
1000 | ||
3b85e4ab HC |
1001 | #ifdef CONFIG_HAS_IOMEM |
1002 | ||
1a087c6a AR |
1003 | /* |
1004 | * The regset32 stuff is used to print 32-bit registers using the | |
1005 | * seq_file utilities. We offer printing a register set in an already-opened | |
1006 | * sequential file or create a debugfs file that only prints a regset32. | |
1007 | */ | |
1008 | ||
1009 | /** | |
1010 | * debugfs_print_regs32 - use seq_print to describe a set of registers | |
1011 | * @s: the seq_file structure being used to generate output | |
1012 | * @regs: an array if struct debugfs_reg32 structures | |
b5763acc | 1013 | * @nregs: the length of the above array |
1a087c6a AR |
1014 | * @base: the base address to be used in reading the registers |
1015 | * @prefix: a string to be prefixed to every output line | |
1016 | * | |
1017 | * This function outputs a text block describing the current values of | |
1018 | * some 32-bit hardware registers. It is meant to be used within debugfs | |
1019 | * files based on seq_file that need to show registers, intermixed with other | |
1020 | * information. The prefix argument may be used to specify a leading string, | |
1021 | * because some peripherals have several blocks of identical registers, | |
1022 | * for example configuration of dma channels | |
1023 | */ | |
9761536e JP |
1024 | void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
1025 | int nregs, void __iomem *base, char *prefix) | |
1a087c6a | 1026 | { |
9761536e | 1027 | int i; |
1a087c6a AR |
1028 | |
1029 | for (i = 0; i < nregs; i++, regs++) { | |
1030 | if (prefix) | |
9761536e JP |
1031 | seq_printf(s, "%s", prefix); |
1032 | seq_printf(s, "%s = 0x%08x\n", regs->name, | |
1033 | readl(base + regs->offset)); | |
1034 | if (seq_has_overflowed(s)) | |
1035 | break; | |
1a087c6a | 1036 | } |
1a087c6a AR |
1037 | } |
1038 | EXPORT_SYMBOL_GPL(debugfs_print_regs32); | |
1039 | ||
1040 | static int debugfs_show_regset32(struct seq_file *s, void *data) | |
1041 | { | |
1042 | struct debugfs_regset32 *regset = s->private; | |
1043 | ||
1044 | debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); | |
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | static int debugfs_open_regset32(struct inode *inode, struct file *file) | |
1049 | { | |
1050 | return single_open(file, debugfs_show_regset32, inode->i_private); | |
1051 | } | |
1052 | ||
1053 | static const struct file_operations fops_regset32 = { | |
1054 | .open = debugfs_open_regset32, | |
1055 | .read = seq_read, | |
1056 | .llseek = seq_lseek, | |
1057 | .release = single_release, | |
1058 | }; | |
1059 | ||
1060 | /** | |
1061 | * debugfs_create_regset32 - create a debugfs file that returns register values | |
1062 | * @name: a pointer to a string containing the name of the file to create. | |
1063 | * @mode: the permission that the file should have | |
1064 | * @parent: a pointer to the parent dentry for this file. This should be a | |
1065 | * directory dentry if set. If this parameter is %NULL, then the | |
1066 | * file will be created in the root of the debugfs filesystem. | |
1067 | * @regset: a pointer to a struct debugfs_regset32, which contains a pointer | |
1068 | * to an array of register definitions, the array size and the base | |
1069 | * address where the register bank is to be found. | |
1070 | * | |
1071 | * This function creates a file in debugfs with the given name that reports | |
1072 | * the names and values of a set of 32-bit registers. If the @mode variable | |
1073 | * is so set it can be read from. Writing is not supported. | |
1074 | * | |
1075 | * This function will return a pointer to a dentry if it succeeds. This | |
1076 | * pointer must be passed to the debugfs_remove() function when the file is | |
1077 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
1078 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
1079 | * | |
1080 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
1081 | * returned. It is not wise to check for this value, but rather, check for | |
1082 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
1083 | * code. | |
1084 | */ | |
88187398 | 1085 | struct dentry *debugfs_create_regset32(const char *name, umode_t mode, |
1a087c6a AR |
1086 | struct dentry *parent, |
1087 | struct debugfs_regset32 *regset) | |
1088 | { | |
1089 | return debugfs_create_file(name, mode, parent, regset, &fops_regset32); | |
1090 | } | |
1091 | EXPORT_SYMBOL_GPL(debugfs_create_regset32); | |
3b85e4ab HC |
1092 | |
1093 | #endif /* CONFIG_HAS_IOMEM */ | |
98210b7f AS |
1094 | |
1095 | struct debugfs_devm_entry { | |
1096 | int (*read)(struct seq_file *seq, void *data); | |
1097 | struct device *dev; | |
1098 | }; | |
1099 | ||
1100 | static int debugfs_devm_entry_open(struct inode *inode, struct file *f) | |
1101 | { | |
1102 | struct debugfs_devm_entry *entry = inode->i_private; | |
1103 | ||
1104 | return single_open(f, entry->read, entry->dev); | |
1105 | } | |
1106 | ||
1107 | static const struct file_operations debugfs_devm_entry_ops = { | |
1108 | .owner = THIS_MODULE, | |
1109 | .open = debugfs_devm_entry_open, | |
1110 | .release = single_release, | |
1111 | .read = seq_read, | |
1112 | .llseek = seq_lseek | |
1113 | }; | |
1114 | ||
1115 | /** | |
1116 | * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. | |
1117 | * | |
1118 | * @dev: device related to this debugfs file. | |
1119 | * @name: name of the debugfs file. | |
1120 | * @parent: a pointer to the parent dentry for this file. This should be a | |
1121 | * directory dentry if set. If this parameter is %NULL, then the | |
1122 | * file will be created in the root of the debugfs filesystem. | |
1123 | * @read_fn: function pointer called to print the seq_file content. | |
1124 | */ | |
1125 | struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, | |
1126 | struct dentry *parent, | |
1127 | int (*read_fn)(struct seq_file *s, | |
1128 | void *data)) | |
1129 | { | |
1130 | struct debugfs_devm_entry *entry; | |
1131 | ||
1132 | if (IS_ERR(parent)) | |
1133 | return ERR_PTR(-ENOENT); | |
1134 | ||
1135 | entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); | |
1136 | if (!entry) | |
1137 | return ERR_PTR(-ENOMEM); | |
1138 | ||
1139 | entry->read = read_fn; | |
1140 | entry->dev = dev; | |
1141 | ||
1142 | return debugfs_create_file(name, S_IRUGO, parent, entry, | |
1143 | &debugfs_devm_entry_ops); | |
1144 | } | |
1145 | EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); | |
1146 |