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> |
1da177e4 LT |
25 | |
26 | static ssize_t default_read_file(struct file *file, char __user *buf, | |
27 | size_t count, loff_t *ppos) | |
28 | { | |
29 | return 0; | |
30 | } | |
31 | ||
32 | static ssize_t default_write_file(struct file *file, const char __user *buf, | |
33 | size_t count, loff_t *ppos) | |
34 | { | |
35 | return count; | |
36 | } | |
37 | ||
4b6f5d20 | 38 | const struct file_operations debugfs_file_operations = { |
1da177e4 LT |
39 | .read = default_read_file, |
40 | .write = default_write_file, | |
234e3405 | 41 | .open = simple_open, |
6038f373 | 42 | .llseek = noop_llseek, |
1da177e4 LT |
43 | }; |
44 | ||
8b88b099 | 45 | static int debugfs_u8_set(void *data, u64 val) |
acaefc25 AB |
46 | { |
47 | *(u8 *)data = val; | |
8b88b099 | 48 | return 0; |
acaefc25 | 49 | } |
8b88b099 | 50 | static int debugfs_u8_get(void *data, u64 *val) |
acaefc25 | 51 | { |
8b88b099 CH |
52 | *val = *(u8 *)data; |
53 | return 0; | |
acaefc25 AB |
54 | } |
55 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); | |
e4792aa3 RG |
56 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); |
57 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); | |
1da177e4 LT |
58 | |
59 | /** | |
6468b3af | 60 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
1da177e4 LT |
61 | * @name: a pointer to a string containing the name of the file to create. |
62 | * @mode: the permission that the file should have | |
63 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 64 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
65 | * file will be created in the root of the debugfs filesystem. |
66 | * @value: a pointer to the variable that the file should read to and write | |
67 | * from. | |
68 | * | |
69 | * This function creates a file in debugfs with the given name that | |
70 | * contains the value of the variable @value. If the @mode variable is so | |
71 | * set, it can be read from, and written to. | |
72 | * | |
73 | * This function will return a pointer to a dentry if it succeeds. This | |
74 | * pointer must be passed to the debugfs_remove() function when the file is | |
75 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 76 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 77 | * |
6468b3af | 78 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 79 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 80 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
81 | * code. |
82 | */ | |
f4ae40a6 | 83 | struct dentry *debugfs_create_u8(const char *name, umode_t mode, |
1da177e4 LT |
84 | struct dentry *parent, u8 *value) |
85 | { | |
e4792aa3 RG |
86 | /* if there are no write bits set, make read only */ |
87 | if (!(mode & S_IWUGO)) | |
88 | return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); | |
89 | /* if there are no read bits set, make write only */ | |
90 | if (!(mode & S_IRUGO)) | |
91 | return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); | |
92 | ||
1da177e4 LT |
93 | return debugfs_create_file(name, mode, parent, value, &fops_u8); |
94 | } | |
95 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | |
96 | ||
8b88b099 | 97 | static int debugfs_u16_set(void *data, u64 val) |
acaefc25 AB |
98 | { |
99 | *(u16 *)data = val; | |
8b88b099 | 100 | return 0; |
acaefc25 | 101 | } |
8b88b099 | 102 | static int debugfs_u16_get(void *data, u64 *val) |
acaefc25 | 103 | { |
8b88b099 CH |
104 | *val = *(u16 *)data; |
105 | return 0; | |
acaefc25 AB |
106 | } |
107 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); | |
e4792aa3 RG |
108 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); |
109 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); | |
acaefc25 | 110 | |
1da177e4 | 111 | /** |
6468b3af | 112 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
1da177e4 LT |
113 | * @name: a pointer to a string containing the name of the file to create. |
114 | * @mode: the permission that the file should have | |
115 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 116 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
117 | * file will be created in the root of the debugfs filesystem. |
118 | * @value: a pointer to the variable that the file should read to and write | |
119 | * from. | |
120 | * | |
121 | * This function creates a file in debugfs with the given name that | |
122 | * contains the value of the variable @value. If the @mode variable is so | |
123 | * set, it can be read from, and written to. | |
124 | * | |
125 | * This function will return a pointer to a dentry if it succeeds. This | |
126 | * pointer must be passed to the debugfs_remove() function when the file is | |
127 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 128 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 129 | * |
6468b3af | 130 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 131 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 132 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
133 | * code. |
134 | */ | |
f4ae40a6 | 135 | struct dentry *debugfs_create_u16(const char *name, umode_t mode, |
1da177e4 LT |
136 | struct dentry *parent, u16 *value) |
137 | { | |
e4792aa3 RG |
138 | /* if there are no write bits set, make read only */ |
139 | if (!(mode & S_IWUGO)) | |
140 | return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); | |
141 | /* if there are no read bits set, make write only */ | |
142 | if (!(mode & S_IRUGO)) | |
143 | return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); | |
144 | ||
1da177e4 LT |
145 | return debugfs_create_file(name, mode, parent, value, &fops_u16); |
146 | } | |
147 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | |
148 | ||
8b88b099 | 149 | static int debugfs_u32_set(void *data, u64 val) |
acaefc25 AB |
150 | { |
151 | *(u32 *)data = val; | |
8b88b099 | 152 | return 0; |
acaefc25 | 153 | } |
8b88b099 | 154 | static int debugfs_u32_get(void *data, u64 *val) |
acaefc25 | 155 | { |
8b88b099 CH |
156 | *val = *(u32 *)data; |
157 | return 0; | |
acaefc25 AB |
158 | } |
159 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); | |
e4792aa3 RG |
160 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); |
161 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); | |
acaefc25 | 162 | |
1da177e4 | 163 | /** |
6468b3af | 164 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
1da177e4 LT |
165 | * @name: a pointer to a string containing the name of the file to create. |
166 | * @mode: the permission that the file should have | |
167 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 168 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
169 | * file will be created in the root of the debugfs filesystem. |
170 | * @value: a pointer to the variable that the file should read to and write | |
171 | * from. | |
172 | * | |
173 | * This function creates a file in debugfs with the given name that | |
174 | * contains the value of the variable @value. If the @mode variable is so | |
175 | * set, it can be read from, and written to. | |
176 | * | |
177 | * This function will return a pointer to a dentry if it succeeds. This | |
178 | * pointer must be passed to the debugfs_remove() function when the file is | |
179 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 180 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 181 | * |
6468b3af | 182 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 183 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 184 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
185 | * code. |
186 | */ | |
f4ae40a6 | 187 | struct dentry *debugfs_create_u32(const char *name, umode_t mode, |
1da177e4 LT |
188 | struct dentry *parent, u32 *value) |
189 | { | |
e4792aa3 RG |
190 | /* if there are no write bits set, make read only */ |
191 | if (!(mode & S_IWUGO)) | |
192 | return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); | |
193 | /* if there are no read bits set, make write only */ | |
194 | if (!(mode & S_IRUGO)) | |
195 | return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); | |
196 | ||
1da177e4 LT |
197 | return debugfs_create_file(name, mode, parent, value, &fops_u32); |
198 | } | |
199 | EXPORT_SYMBOL_GPL(debugfs_create_u32); | |
200 | ||
8b88b099 | 201 | static int debugfs_u64_set(void *data, u64 val) |
8447891f ME |
202 | { |
203 | *(u64 *)data = val; | |
8b88b099 | 204 | return 0; |
8447891f ME |
205 | } |
206 | ||
8b88b099 | 207 | static int debugfs_u64_get(void *data, u64 *val) |
8447891f | 208 | { |
8b88b099 CH |
209 | *val = *(u64 *)data; |
210 | return 0; | |
8447891f ME |
211 | } |
212 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); | |
e4792aa3 RG |
213 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); |
214 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); | |
8447891f ME |
215 | |
216 | /** | |
217 | * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
218 | * @name: a pointer to a string containing the name of the file to create. | |
219 | * @mode: the permission that the file should have | |
220 | * @parent: a pointer to the parent dentry for this file. This should be a | |
221 | * directory dentry if set. If this parameter is %NULL, then the | |
222 | * file will be created in the root of the debugfs filesystem. | |
223 | * @value: a pointer to the variable that the file should read to and write | |
224 | * from. | |
225 | * | |
226 | * This function creates a file in debugfs with the given name that | |
227 | * contains the value of the variable @value. If the @mode variable is so | |
228 | * set, it can be read from, and written to. | |
229 | * | |
230 | * This function will return a pointer to a dentry if it succeeds. This | |
231 | * pointer must be passed to the debugfs_remove() function when the file is | |
232 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
233 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
234 | * | |
235 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
236 | * returned. It is not wise to check for this value, but rather, check for | |
237 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
238 | * code. | |
239 | */ | |
f4ae40a6 | 240 | struct dentry *debugfs_create_u64(const char *name, umode_t mode, |
8447891f ME |
241 | struct dentry *parent, u64 *value) |
242 | { | |
e4792aa3 RG |
243 | /* if there are no write bits set, make read only */ |
244 | if (!(mode & S_IWUGO)) | |
245 | return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); | |
246 | /* if there are no read bits set, make write only */ | |
247 | if (!(mode & S_IRUGO)) | |
248 | return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); | |
249 | ||
8447891f ME |
250 | return debugfs_create_file(name, mode, parent, value, &fops_u64); |
251 | } | |
252 | EXPORT_SYMBOL_GPL(debugfs_create_u64); | |
253 | ||
2ebefc50 | 254 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
e4792aa3 RG |
255 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); |
256 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); | |
2ebefc50 RG |
257 | |
258 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); | |
e4792aa3 RG |
259 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); |
260 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); | |
2ebefc50 RG |
261 | |
262 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); | |
e4792aa3 RG |
263 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); |
264 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); | |
2ebefc50 | 265 | |
15b0beaa HY |
266 | DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); |
267 | ||
e6716b87 | 268 | /* |
15b0beaa | 269 | * 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 | 270 | * |
e6716b87 RD |
271 | * These functions are exactly the same as the above functions (but use a hex |
272 | * output for the decimal challenged). For details look at the above unsigned | |
2ebefc50 RG |
273 | * decimal functions. |
274 | */ | |
e6716b87 RD |
275 | |
276 | /** | |
277 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value | |
278 | * @name: a pointer to a string containing the name of the file to create. | |
279 | * @mode: the permission that the file should have | |
280 | * @parent: a pointer to the parent dentry for this file. This should be a | |
281 | * directory dentry if set. If this parameter is %NULL, then the | |
282 | * file will be created in the root of the debugfs filesystem. | |
283 | * @value: a pointer to the variable that the file should read to and write | |
284 | * from. | |
285 | */ | |
f4ae40a6 | 286 | struct dentry *debugfs_create_x8(const char *name, umode_t mode, |
2ebefc50 RG |
287 | struct dentry *parent, u8 *value) |
288 | { | |
e4792aa3 RG |
289 | /* if there are no write bits set, make read only */ |
290 | if (!(mode & S_IWUGO)) | |
291 | return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); | |
292 | /* if there are no read bits set, make write only */ | |
293 | if (!(mode & S_IRUGO)) | |
294 | return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); | |
295 | ||
2ebefc50 RG |
296 | return debugfs_create_file(name, mode, parent, value, &fops_x8); |
297 | } | |
298 | EXPORT_SYMBOL_GPL(debugfs_create_x8); | |
299 | ||
e6716b87 RD |
300 | /** |
301 | * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value | |
302 | * @name: a pointer to a string containing the name of the file to create. | |
303 | * @mode: the permission that the file should have | |
304 | * @parent: a pointer to the parent dentry for this file. This should be a | |
305 | * directory dentry if set. If this parameter is %NULL, then the | |
306 | * file will be created in the root of the debugfs filesystem. | |
307 | * @value: a pointer to the variable that the file should read to and write | |
308 | * from. | |
309 | */ | |
f4ae40a6 | 310 | struct dentry *debugfs_create_x16(const char *name, umode_t mode, |
2ebefc50 RG |
311 | struct dentry *parent, u16 *value) |
312 | { | |
e4792aa3 RG |
313 | /* if there are no write bits set, make read only */ |
314 | if (!(mode & S_IWUGO)) | |
315 | return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); | |
316 | /* if there are no read bits set, make write only */ | |
317 | if (!(mode & S_IRUGO)) | |
318 | return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); | |
319 | ||
2ebefc50 RG |
320 | return debugfs_create_file(name, mode, parent, value, &fops_x16); |
321 | } | |
322 | EXPORT_SYMBOL_GPL(debugfs_create_x16); | |
323 | ||
e6716b87 RD |
324 | /** |
325 | * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value | |
326 | * @name: a pointer to a string containing the name of the file to create. | |
327 | * @mode: the permission that the file should have | |
328 | * @parent: a pointer to the parent dentry for this file. This should be a | |
329 | * directory dentry if set. If this parameter is %NULL, then the | |
330 | * file will be created in the root of the debugfs filesystem. | |
331 | * @value: a pointer to the variable that the file should read to and write | |
332 | * from. | |
333 | */ | |
f4ae40a6 | 334 | struct dentry *debugfs_create_x32(const char *name, umode_t mode, |
2ebefc50 RG |
335 | struct dentry *parent, u32 *value) |
336 | { | |
e4792aa3 RG |
337 | /* if there are no write bits set, make read only */ |
338 | if (!(mode & S_IWUGO)) | |
339 | return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); | |
340 | /* if there are no read bits set, make write only */ | |
341 | if (!(mode & S_IRUGO)) | |
342 | return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); | |
343 | ||
2ebefc50 RG |
344 | return debugfs_create_file(name, mode, parent, value, &fops_x32); |
345 | } | |
346 | EXPORT_SYMBOL_GPL(debugfs_create_x32); | |
347 | ||
15b0beaa HY |
348 | /** |
349 | * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
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 | |
353 | * directory dentry if set. If this parameter is %NULL, then the | |
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 | */ | |
f4ae40a6 | 358 | struct dentry *debugfs_create_x64(const char *name, umode_t mode, |
15b0beaa HY |
359 | struct dentry *parent, u64 *value) |
360 | { | |
361 | return debugfs_create_file(name, mode, parent, value, &fops_x64); | |
362 | } | |
363 | EXPORT_SYMBOL_GPL(debugfs_create_x64); | |
364 | ||
5e078787 IPG |
365 | |
366 | static int debugfs_size_t_set(void *data, u64 val) | |
367 | { | |
368 | *(size_t *)data = val; | |
369 | return 0; | |
370 | } | |
371 | static int debugfs_size_t_get(void *data, u64 *val) | |
372 | { | |
373 | *val = *(size_t *)data; | |
374 | return 0; | |
375 | } | |
376 | DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, | |
377 | "%llu\n"); /* %llu and %zu are more or less the same */ | |
378 | ||
379 | /** | |
380 | * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value | |
381 | * @name: a pointer to a string containing the name of the file to create. | |
382 | * @mode: the permission that the file should have | |
383 | * @parent: a pointer to the parent dentry for this file. This should be a | |
384 | * directory dentry if set. If this parameter is %NULL, then the | |
385 | * file will be created in the root of the debugfs filesystem. | |
386 | * @value: a pointer to the variable that the file should read to and write | |
387 | * from. | |
388 | */ | |
f4ae40a6 | 389 | struct dentry *debugfs_create_size_t(const char *name, umode_t mode, |
5e078787 IPG |
390 | struct dentry *parent, size_t *value) |
391 | { | |
392 | return debugfs_create_file(name, mode, parent, value, &fops_size_t); | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); | |
395 | ||
3a76e5e0 SJ |
396 | static int debugfs_atomic_t_set(void *data, u64 val) |
397 | { | |
398 | atomic_set((atomic_t *)data, val); | |
399 | return 0; | |
400 | } | |
401 | static int debugfs_atomic_t_get(void *data, u64 *val) | |
402 | { | |
403 | *val = atomic_read((atomic_t *)data); | |
404 | return 0; | |
405 | } | |
406 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, | |
407 | debugfs_atomic_t_set, "%lld\n"); | |
408 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n"); | |
409 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n"); | |
410 | ||
411 | /** | |
412 | * debugfs_create_atomic_t - create a debugfs file that is used to read and | |
413 | * write an atomic_t value | |
414 | * @name: a pointer to a string containing the name of the file to create. | |
415 | * @mode: the permission that the file should have | |
416 | * @parent: a pointer to the parent dentry for this file. This should be a | |
417 | * directory dentry if set. If this parameter is %NULL, then the | |
418 | * file will be created in the root of the debugfs filesystem. | |
419 | * @value: a pointer to the variable that the file should read to and write | |
420 | * from. | |
421 | */ | |
422 | struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode, | |
423 | struct dentry *parent, atomic_t *value) | |
424 | { | |
425 | /* if there are no write bits set, make read only */ | |
426 | if (!(mode & S_IWUGO)) | |
427 | return debugfs_create_file(name, mode, parent, value, | |
428 | &fops_atomic_t_ro); | |
429 | /* if there are no read bits set, make write only */ | |
430 | if (!(mode & S_IRUGO)) | |
431 | return debugfs_create_file(name, mode, parent, value, | |
432 | &fops_atomic_t_wo); | |
433 | ||
434 | return debugfs_create_file(name, mode, parent, value, &fops_atomic_t); | |
435 | } | |
436 | EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); | |
5e078787 | 437 | |
1da177e4 LT |
438 | static ssize_t read_file_bool(struct file *file, char __user *user_buf, |
439 | size_t count, loff_t *ppos) | |
440 | { | |
441 | char buf[3]; | |
442 | u32 *val = file->private_data; | |
88e412ea | 443 | |
1da177e4 LT |
444 | if (*val) |
445 | buf[0] = 'Y'; | |
446 | else | |
447 | buf[0] = 'N'; | |
448 | buf[1] = '\n'; | |
449 | buf[2] = 0x00; | |
450 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
451 | } | |
452 | ||
453 | static ssize_t write_file_bool(struct file *file, const char __user *user_buf, | |
454 | size_t count, loff_t *ppos) | |
455 | { | |
456 | char buf[32]; | |
c42d2237 | 457 | size_t buf_size; |
8705b48e | 458 | bool bv; |
1da177e4 LT |
459 | u32 *val = file->private_data; |
460 | ||
461 | buf_size = min(count, (sizeof(buf)-1)); | |
462 | if (copy_from_user(buf, user_buf, buf_size)) | |
463 | return -EFAULT; | |
464 | ||
a3b2c8c7 | 465 | buf[buf_size] = '\0'; |
8705b48e JC |
466 | if (strtobool(buf, &bv) == 0) |
467 | *val = bv; | |
468 | ||
1da177e4 LT |
469 | return count; |
470 | } | |
471 | ||
4b6f5d20 | 472 | static const struct file_operations fops_bool = { |
1da177e4 LT |
473 | .read = read_file_bool, |
474 | .write = write_file_bool, | |
234e3405 | 475 | .open = simple_open, |
6038f373 | 476 | .llseek = default_llseek, |
1da177e4 LT |
477 | }; |
478 | ||
479 | /** | |
6468b3af | 480 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
1da177e4 LT |
481 | * @name: a pointer to a string containing the name of the file to create. |
482 | * @mode: the permission that the file should have | |
483 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 484 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
485 | * file will be created in the root of the debugfs filesystem. |
486 | * @value: a pointer to the variable that the file should read to and write | |
487 | * from. | |
488 | * | |
489 | * This function creates a file in debugfs with the given name that | |
490 | * contains the value of the variable @value. If the @mode variable is so | |
491 | * set, it can be read from, and written to. | |
492 | * | |
493 | * This function will return a pointer to a dentry if it succeeds. This | |
494 | * pointer must be passed to the debugfs_remove() function when the file is | |
495 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 496 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 497 | * |
6468b3af | 498 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 499 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 500 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
501 | * code. |
502 | */ | |
f4ae40a6 | 503 | struct dentry *debugfs_create_bool(const char *name, umode_t mode, |
1da177e4 LT |
504 | struct dentry *parent, u32 *value) |
505 | { | |
506 | return debugfs_create_file(name, mode, parent, value, &fops_bool); | |
507 | } | |
508 | EXPORT_SYMBOL_GPL(debugfs_create_bool); | |
509 | ||
dd308bc3 ME |
510 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
511 | size_t count, loff_t *ppos) | |
512 | { | |
513 | struct debugfs_blob_wrapper *blob = file->private_data; | |
514 | return simple_read_from_buffer(user_buf, count, ppos, blob->data, | |
515 | blob->size); | |
516 | } | |
517 | ||
00977a59 | 518 | static const struct file_operations fops_blob = { |
dd308bc3 | 519 | .read = read_file_blob, |
234e3405 | 520 | .open = simple_open, |
6038f373 | 521 | .llseek = default_llseek, |
dd308bc3 ME |
522 | }; |
523 | ||
524 | /** | |
400ced61 | 525 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
dd308bc3 ME |
526 | * @name: a pointer to a string containing the name of the file to create. |
527 | * @mode: the permission that the file should have | |
528 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 529 | * directory dentry if set. If this parameter is %NULL, then the |
dd308bc3 ME |
530 | * file will be created in the root of the debugfs filesystem. |
531 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer | |
532 | * to the blob data and the size of the data. | |
533 | * | |
534 | * This function creates a file in debugfs with the given name that exports | |
535 | * @blob->data as a binary blob. If the @mode variable is so set it can be | |
536 | * read from. Writing is not supported. | |
537 | * | |
538 | * This function will return a pointer to a dentry if it succeeds. This | |
539 | * pointer must be passed to the debugfs_remove() function when the file is | |
540 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 541 | * you are responsible here.) If an error occurs, %NULL will be returned. |
dd308bc3 | 542 | * |
6468b3af | 543 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
dd308bc3 | 544 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 545 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
dd308bc3 ME |
546 | * code. |
547 | */ | |
f4ae40a6 | 548 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, |
dd308bc3 ME |
549 | struct dentry *parent, |
550 | struct debugfs_blob_wrapper *blob) | |
551 | { | |
552 | return debugfs_create_file(name, mode, parent, blob, &fops_blob); | |
553 | } | |
554 | EXPORT_SYMBOL_GPL(debugfs_create_blob); | |
1a087c6a | 555 | |
9fe2a701 SV |
556 | struct array_data { |
557 | void *array; | |
558 | u32 elements; | |
559 | }; | |
560 | ||
e05e279e LT |
561 | static size_t u32_format_array(char *buf, size_t bufsize, |
562 | u32 *array, int array_size) | |
9fe2a701 SV |
563 | { |
564 | size_t ret = 0; | |
9fe2a701 | 565 | |
e05e279e | 566 | while (--array_size >= 0) { |
9fe2a701 | 567 | size_t len; |
e05e279e | 568 | char term = array_size ? ' ' : '\n'; |
9fe2a701 | 569 | |
e05e279e | 570 | len = snprintf(buf, bufsize, "%u%c", *array++, term); |
9fe2a701 SV |
571 | ret += len; |
572 | ||
e05e279e LT |
573 | buf += len; |
574 | bufsize -= len; | |
9fe2a701 | 575 | } |
9fe2a701 SV |
576 | return ret; |
577 | } | |
578 | ||
36048853 | 579 | static int u32_array_open(struct inode *inode, struct file *file) |
9fe2a701 | 580 | { |
9fe2a701 | 581 | struct array_data *data = inode->i_private; |
e05e279e LT |
582 | int size, elements = data->elements; |
583 | char *buf; | |
584 | ||
585 | /* | |
586 | * Max size: | |
587 | * - 10 digits + ' '/'\n' = 11 bytes per number | |
588 | * - terminating NUL character | |
589 | */ | |
590 | size = elements*11; | |
591 | buf = kmalloc(size+1, GFP_KERNEL); | |
592 | if (!buf) | |
36048853 | 593 | return -ENOMEM; |
e05e279e LT |
594 | buf[size] = 0; |
595 | ||
596 | file->private_data = buf; | |
597 | u32_format_array(buf, size, data->array, data->elements); | |
598 | ||
36048853 DR |
599 | return nonseekable_open(inode, file); |
600 | } | |
9fe2a701 | 601 | |
36048853 DR |
602 | static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, |
603 | loff_t *ppos) | |
604 | { | |
605 | size_t size = strlen(file->private_data); | |
9fe2a701 SV |
606 | |
607 | return simple_read_from_buffer(buf, len, ppos, | |
608 | file->private_data, size); | |
609 | } | |
610 | ||
611 | static int u32_array_release(struct inode *inode, struct file *file) | |
612 | { | |
613 | kfree(file->private_data); | |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
618 | static const struct file_operations u32_array_fops = { | |
619 | .owner = THIS_MODULE, | |
620 | .open = u32_array_open, | |
621 | .release = u32_array_release, | |
622 | .read = u32_array_read, | |
623 | .llseek = no_llseek, | |
624 | }; | |
625 | ||
626 | /** | |
627 | * debugfs_create_u32_array - create a debugfs file that is used to read u32 | |
628 | * array. | |
629 | * @name: a pointer to a string containing the name of the file to create. | |
630 | * @mode: the permission that the file should have. | |
631 | * @parent: a pointer to the parent dentry for this file. This should be a | |
632 | * directory dentry if set. If this parameter is %NULL, then the | |
633 | * file will be created in the root of the debugfs filesystem. | |
634 | * @array: u32 array that provides data. | |
635 | * @elements: total number of elements in the array. | |
636 | * | |
637 | * This function creates a file in debugfs with the given name that exports | |
638 | * @array as data. If the @mode variable is so set it can be read from. | |
639 | * Writing is not supported. Seek within the file is also not supported. | |
640 | * Once array is created its size can not be changed. | |
641 | * | |
642 | * The function returns a pointer to dentry on success. If debugfs is not | |
643 | * enabled in the kernel, the value -%ENODEV will be returned. | |
644 | */ | |
645 | struct dentry *debugfs_create_u32_array(const char *name, umode_t mode, | |
646 | struct dentry *parent, | |
647 | u32 *array, u32 elements) | |
648 | { | |
649 | struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); | |
650 | ||
651 | if (data == NULL) | |
652 | return NULL; | |
653 | ||
654 | data->array = array; | |
655 | data->elements = elements; | |
656 | ||
657 | return debugfs_create_file(name, mode, parent, data, &u32_array_fops); | |
658 | } | |
659 | EXPORT_SYMBOL_GPL(debugfs_create_u32_array); | |
660 | ||
3b85e4ab HC |
661 | #ifdef CONFIG_HAS_IOMEM |
662 | ||
1a087c6a AR |
663 | /* |
664 | * The regset32 stuff is used to print 32-bit registers using the | |
665 | * seq_file utilities. We offer printing a register set in an already-opened | |
666 | * sequential file or create a debugfs file that only prints a regset32. | |
667 | */ | |
668 | ||
669 | /** | |
670 | * debugfs_print_regs32 - use seq_print to describe a set of registers | |
671 | * @s: the seq_file structure being used to generate output | |
672 | * @regs: an array if struct debugfs_reg32 structures | |
b5763acc | 673 | * @nregs: the length of the above array |
1a087c6a AR |
674 | * @base: the base address to be used in reading the registers |
675 | * @prefix: a string to be prefixed to every output line | |
676 | * | |
677 | * This function outputs a text block describing the current values of | |
678 | * some 32-bit hardware registers. It is meant to be used within debugfs | |
679 | * files based on seq_file that need to show registers, intermixed with other | |
680 | * information. The prefix argument may be used to specify a leading string, | |
681 | * because some peripherals have several blocks of identical registers, | |
682 | * for example configuration of dma channels | |
683 | */ | |
9761536e JP |
684 | void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
685 | int nregs, void __iomem *base, char *prefix) | |
1a087c6a | 686 | { |
9761536e | 687 | int i; |
1a087c6a AR |
688 | |
689 | for (i = 0; i < nregs; i++, regs++) { | |
690 | if (prefix) | |
9761536e JP |
691 | seq_printf(s, "%s", prefix); |
692 | seq_printf(s, "%s = 0x%08x\n", regs->name, | |
693 | readl(base + regs->offset)); | |
694 | if (seq_has_overflowed(s)) | |
695 | break; | |
1a087c6a | 696 | } |
1a087c6a AR |
697 | } |
698 | EXPORT_SYMBOL_GPL(debugfs_print_regs32); | |
699 | ||
700 | static int debugfs_show_regset32(struct seq_file *s, void *data) | |
701 | { | |
702 | struct debugfs_regset32 *regset = s->private; | |
703 | ||
704 | debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); | |
705 | return 0; | |
706 | } | |
707 | ||
708 | static int debugfs_open_regset32(struct inode *inode, struct file *file) | |
709 | { | |
710 | return single_open(file, debugfs_show_regset32, inode->i_private); | |
711 | } | |
712 | ||
713 | static const struct file_operations fops_regset32 = { | |
714 | .open = debugfs_open_regset32, | |
715 | .read = seq_read, | |
716 | .llseek = seq_lseek, | |
717 | .release = single_release, | |
718 | }; | |
719 | ||
720 | /** | |
721 | * debugfs_create_regset32 - create a debugfs file that returns register values | |
722 | * @name: a pointer to a string containing the name of the file to create. | |
723 | * @mode: the permission that the file should have | |
724 | * @parent: a pointer to the parent dentry for this file. This should be a | |
725 | * directory dentry if set. If this parameter is %NULL, then the | |
726 | * file will be created in the root of the debugfs filesystem. | |
727 | * @regset: a pointer to a struct debugfs_regset32, which contains a pointer | |
728 | * to an array of register definitions, the array size and the base | |
729 | * address where the register bank is to be found. | |
730 | * | |
731 | * This function creates a file in debugfs with the given name that reports | |
732 | * the names and values of a set of 32-bit registers. If the @mode variable | |
733 | * is so set it can be read from. Writing is not supported. | |
734 | * | |
735 | * This function will return a pointer to a dentry if it succeeds. This | |
736 | * pointer must be passed to the debugfs_remove() function when the file is | |
737 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
738 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
739 | * | |
740 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
741 | * returned. It is not wise to check for this value, but rather, check for | |
742 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
743 | * code. | |
744 | */ | |
88187398 | 745 | struct dentry *debugfs_create_regset32(const char *name, umode_t mode, |
1a087c6a AR |
746 | struct dentry *parent, |
747 | struct debugfs_regset32 *regset) | |
748 | { | |
749 | return debugfs_create_file(name, mode, parent, regset, &fops_regset32); | |
750 | } | |
751 | EXPORT_SYMBOL_GPL(debugfs_create_regset32); | |
3b85e4ab HC |
752 | |
753 | #endif /* CONFIG_HAS_IOMEM */ | |
98210b7f AS |
754 | |
755 | struct debugfs_devm_entry { | |
756 | int (*read)(struct seq_file *seq, void *data); | |
757 | struct device *dev; | |
758 | }; | |
759 | ||
760 | static int debugfs_devm_entry_open(struct inode *inode, struct file *f) | |
761 | { | |
762 | struct debugfs_devm_entry *entry = inode->i_private; | |
763 | ||
764 | return single_open(f, entry->read, entry->dev); | |
765 | } | |
766 | ||
767 | static const struct file_operations debugfs_devm_entry_ops = { | |
768 | .owner = THIS_MODULE, | |
769 | .open = debugfs_devm_entry_open, | |
770 | .release = single_release, | |
771 | .read = seq_read, | |
772 | .llseek = seq_lseek | |
773 | }; | |
774 | ||
775 | /** | |
776 | * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. | |
777 | * | |
778 | * @dev: device related to this debugfs file. | |
779 | * @name: name of the debugfs file. | |
780 | * @parent: a pointer to the parent dentry for this file. This should be a | |
781 | * directory dentry if set. If this parameter is %NULL, then the | |
782 | * file will be created in the root of the debugfs filesystem. | |
783 | * @read_fn: function pointer called to print the seq_file content. | |
784 | */ | |
785 | struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, | |
786 | struct dentry *parent, | |
787 | int (*read_fn)(struct seq_file *s, | |
788 | void *data)) | |
789 | { | |
790 | struct debugfs_devm_entry *entry; | |
791 | ||
792 | if (IS_ERR(parent)) | |
793 | return ERR_PTR(-ENOENT); | |
794 | ||
795 | entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); | |
796 | if (!entry) | |
797 | return ERR_PTR(-ENOMEM); | |
798 | ||
799 | entry->read = read_fn; | |
800 | entry->dev = dev; | |
801 | ||
802 | return debugfs_create_file(name, S_IRUGO, parent, entry, | |
803 | &debugfs_devm_entry_ops); | |
804 | } | |
805 | EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); | |
806 |