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