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 | 19 | #include <linux/pagemap.h> |
66f54963 | 20 | #include <linux/namei.h> |
1da177e4 LT |
21 | #include <linux/debugfs.h> |
22 | ||
23 | static ssize_t default_read_file(struct file *file, char __user *buf, | |
24 | size_t count, loff_t *ppos) | |
25 | { | |
26 | return 0; | |
27 | } | |
28 | ||
29 | static ssize_t default_write_file(struct file *file, const char __user *buf, | |
30 | size_t count, loff_t *ppos) | |
31 | { | |
32 | return count; | |
33 | } | |
34 | ||
35 | static int default_open(struct inode *inode, struct file *file) | |
36 | { | |
8e18e294 TT |
37 | if (inode->i_private) |
38 | file->private_data = inode->i_private; | |
1da177e4 LT |
39 | |
40 | return 0; | |
41 | } | |
42 | ||
4b6f5d20 | 43 | const struct file_operations debugfs_file_operations = { |
1da177e4 LT |
44 | .read = default_read_file, |
45 | .write = default_write_file, | |
46 | .open = default_open, | |
6038f373 | 47 | .llseek = noop_llseek, |
1da177e4 LT |
48 | }; |
49 | ||
66f54963 PO |
50 | static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
51 | { | |
52 | nd_set_link(nd, dentry->d_inode->i_private); | |
53 | return NULL; | |
54 | } | |
55 | ||
56 | const struct inode_operations debugfs_link_operations = { | |
57 | .readlink = generic_readlink, | |
58 | .follow_link = debugfs_follow_link, | |
59 | }; | |
60 | ||
8b88b099 | 61 | static int debugfs_u8_set(void *data, u64 val) |
acaefc25 AB |
62 | { |
63 | *(u8 *)data = val; | |
8b88b099 | 64 | return 0; |
acaefc25 | 65 | } |
8b88b099 | 66 | static int debugfs_u8_get(void *data, u64 *val) |
acaefc25 | 67 | { |
8b88b099 CH |
68 | *val = *(u8 *)data; |
69 | return 0; | |
acaefc25 AB |
70 | } |
71 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); | |
e4792aa3 RG |
72 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); |
73 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); | |
1da177e4 LT |
74 | |
75 | /** | |
6468b3af | 76 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
1da177e4 LT |
77 | * @name: a pointer to a string containing the name of the file to create. |
78 | * @mode: the permission that the file should have | |
79 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 80 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
81 | * file will be created in the root of the debugfs filesystem. |
82 | * @value: a pointer to the variable that the file should read to and write | |
83 | * from. | |
84 | * | |
85 | * This function creates a file in debugfs with the given name that | |
86 | * contains the value of the variable @value. If the @mode variable is so | |
87 | * set, it can be read from, and written to. | |
88 | * | |
89 | * This function will return a pointer to a dentry if it succeeds. This | |
90 | * pointer must be passed to the debugfs_remove() function when the file is | |
91 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 92 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 93 | * |
6468b3af | 94 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 95 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 96 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
97 | * code. |
98 | */ | |
99 | struct dentry *debugfs_create_u8(const char *name, mode_t mode, | |
100 | struct dentry *parent, u8 *value) | |
101 | { | |
e4792aa3 RG |
102 | /* if there are no write bits set, make read only */ |
103 | if (!(mode & S_IWUGO)) | |
104 | return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); | |
105 | /* if there are no read bits set, make write only */ | |
106 | if (!(mode & S_IRUGO)) | |
107 | return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); | |
108 | ||
1da177e4 LT |
109 | return debugfs_create_file(name, mode, parent, value, &fops_u8); |
110 | } | |
111 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | |
112 | ||
8b88b099 | 113 | static int debugfs_u16_set(void *data, u64 val) |
acaefc25 AB |
114 | { |
115 | *(u16 *)data = val; | |
8b88b099 | 116 | return 0; |
acaefc25 | 117 | } |
8b88b099 | 118 | static int debugfs_u16_get(void *data, u64 *val) |
acaefc25 | 119 | { |
8b88b099 CH |
120 | *val = *(u16 *)data; |
121 | return 0; | |
acaefc25 AB |
122 | } |
123 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); | |
e4792aa3 RG |
124 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); |
125 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); | |
acaefc25 | 126 | |
1da177e4 | 127 | /** |
6468b3af | 128 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
1da177e4 LT |
129 | * @name: a pointer to a string containing the name of the file to create. |
130 | * @mode: the permission that the file should have | |
131 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 132 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
133 | * file will be created in the root of the debugfs filesystem. |
134 | * @value: a pointer to the variable that the file should read to and write | |
135 | * from. | |
136 | * | |
137 | * This function creates a file in debugfs with the given name that | |
138 | * contains the value of the variable @value. If the @mode variable is so | |
139 | * set, it can be read from, and written to. | |
140 | * | |
141 | * This function will return a pointer to a dentry if it succeeds. This | |
142 | * pointer must be passed to the debugfs_remove() function when the file is | |
143 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 144 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 145 | * |
6468b3af | 146 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 147 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 148 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
149 | * code. |
150 | */ | |
151 | struct dentry *debugfs_create_u16(const char *name, mode_t mode, | |
152 | struct dentry *parent, u16 *value) | |
153 | { | |
e4792aa3 RG |
154 | /* if there are no write bits set, make read only */ |
155 | if (!(mode & S_IWUGO)) | |
156 | return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); | |
157 | /* if there are no read bits set, make write only */ | |
158 | if (!(mode & S_IRUGO)) | |
159 | return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); | |
160 | ||
1da177e4 LT |
161 | return debugfs_create_file(name, mode, parent, value, &fops_u16); |
162 | } | |
163 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | |
164 | ||
8b88b099 | 165 | static int debugfs_u32_set(void *data, u64 val) |
acaefc25 AB |
166 | { |
167 | *(u32 *)data = val; | |
8b88b099 | 168 | return 0; |
acaefc25 | 169 | } |
8b88b099 | 170 | static int debugfs_u32_get(void *data, u64 *val) |
acaefc25 | 171 | { |
8b88b099 CH |
172 | *val = *(u32 *)data; |
173 | return 0; | |
acaefc25 AB |
174 | } |
175 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); | |
e4792aa3 RG |
176 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); |
177 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); | |
acaefc25 | 178 | |
1da177e4 | 179 | /** |
6468b3af | 180 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
1da177e4 LT |
181 | * @name: a pointer to a string containing the name of the file to create. |
182 | * @mode: the permission that the file should have | |
183 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 184 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
185 | * file will be created in the root of the debugfs filesystem. |
186 | * @value: a pointer to the variable that the file should read to and write | |
187 | * from. | |
188 | * | |
189 | * This function creates a file in debugfs with the given name that | |
190 | * contains the value of the variable @value. If the @mode variable is so | |
191 | * set, it can be read from, and written to. | |
192 | * | |
193 | * This function will return a pointer to a dentry if it succeeds. This | |
194 | * pointer must be passed to the debugfs_remove() function when the file is | |
195 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 196 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 197 | * |
6468b3af | 198 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 199 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 200 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
201 | * code. |
202 | */ | |
203 | struct dentry *debugfs_create_u32(const char *name, mode_t mode, | |
204 | struct dentry *parent, u32 *value) | |
205 | { | |
e4792aa3 RG |
206 | /* if there are no write bits set, make read only */ |
207 | if (!(mode & S_IWUGO)) | |
208 | return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); | |
209 | /* if there are no read bits set, make write only */ | |
210 | if (!(mode & S_IRUGO)) | |
211 | return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); | |
212 | ||
1da177e4 LT |
213 | return debugfs_create_file(name, mode, parent, value, &fops_u32); |
214 | } | |
215 | EXPORT_SYMBOL_GPL(debugfs_create_u32); | |
216 | ||
8b88b099 | 217 | static int debugfs_u64_set(void *data, u64 val) |
8447891f ME |
218 | { |
219 | *(u64 *)data = val; | |
8b88b099 | 220 | return 0; |
8447891f ME |
221 | } |
222 | ||
8b88b099 | 223 | static int debugfs_u64_get(void *data, u64 *val) |
8447891f | 224 | { |
8b88b099 CH |
225 | *val = *(u64 *)data; |
226 | return 0; | |
8447891f ME |
227 | } |
228 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); | |
e4792aa3 RG |
229 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); |
230 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); | |
8447891f ME |
231 | |
232 | /** | |
233 | * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
234 | * @name: a pointer to a string containing the name of the file to create. | |
235 | * @mode: the permission that the file should have | |
236 | * @parent: a pointer to the parent dentry for this file. This should be a | |
237 | * directory dentry if set. If this parameter is %NULL, then the | |
238 | * file will be created in the root of the debugfs filesystem. | |
239 | * @value: a pointer to the variable that the file should read to and write | |
240 | * from. | |
241 | * | |
242 | * This function creates a file in debugfs with the given name that | |
243 | * contains the value of the variable @value. If the @mode variable is so | |
244 | * set, it can be read from, and written to. | |
245 | * | |
246 | * This function will return a pointer to a dentry if it succeeds. This | |
247 | * pointer must be passed to the debugfs_remove() function when the file is | |
248 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
249 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
250 | * | |
251 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
252 | * returned. It is not wise to check for this value, but rather, check for | |
253 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
254 | * code. | |
255 | */ | |
256 | struct dentry *debugfs_create_u64(const char *name, mode_t mode, | |
257 | struct dentry *parent, u64 *value) | |
258 | { | |
e4792aa3 RG |
259 | /* if there are no write bits set, make read only */ |
260 | if (!(mode & S_IWUGO)) | |
261 | return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); | |
262 | /* if there are no read bits set, make write only */ | |
263 | if (!(mode & S_IRUGO)) | |
264 | return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); | |
265 | ||
8447891f ME |
266 | return debugfs_create_file(name, mode, parent, value, &fops_u64); |
267 | } | |
268 | EXPORT_SYMBOL_GPL(debugfs_create_u64); | |
269 | ||
2ebefc50 | 270 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
e4792aa3 RG |
271 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); |
272 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); | |
2ebefc50 RG |
273 | |
274 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); | |
e4792aa3 RG |
275 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); |
276 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); | |
2ebefc50 RG |
277 | |
278 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); | |
e4792aa3 RG |
279 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); |
280 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); | |
2ebefc50 | 281 | |
15b0beaa HY |
282 | DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); |
283 | ||
e6716b87 | 284 | /* |
15b0beaa | 285 | * 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 | 286 | * |
e6716b87 RD |
287 | * These functions are exactly the same as the above functions (but use a hex |
288 | * output for the decimal challenged). For details look at the above unsigned | |
2ebefc50 RG |
289 | * decimal functions. |
290 | */ | |
e6716b87 RD |
291 | |
292 | /** | |
293 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value | |
294 | * @name: a pointer to a string containing the name of the file to create. | |
295 | * @mode: the permission that the file should have | |
296 | * @parent: a pointer to the parent dentry for this file. This should be a | |
297 | * directory dentry if set. If this parameter is %NULL, then the | |
298 | * file will be created in the root of the debugfs filesystem. | |
299 | * @value: a pointer to the variable that the file should read to and write | |
300 | * from. | |
301 | */ | |
2ebefc50 RG |
302 | struct dentry *debugfs_create_x8(const char *name, mode_t mode, |
303 | struct dentry *parent, u8 *value) | |
304 | { | |
e4792aa3 RG |
305 | /* if there are no write bits set, make read only */ |
306 | if (!(mode & S_IWUGO)) | |
307 | return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); | |
308 | /* if there are no read bits set, make write only */ | |
309 | if (!(mode & S_IRUGO)) | |
310 | return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); | |
311 | ||
2ebefc50 RG |
312 | return debugfs_create_file(name, mode, parent, value, &fops_x8); |
313 | } | |
314 | EXPORT_SYMBOL_GPL(debugfs_create_x8); | |
315 | ||
e6716b87 RD |
316 | /** |
317 | * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value | |
318 | * @name: a pointer to a string containing the name of the file to create. | |
319 | * @mode: the permission that the file should have | |
320 | * @parent: a pointer to the parent dentry for this file. This should be a | |
321 | * directory dentry if set. If this parameter is %NULL, then the | |
322 | * file will be created in the root of the debugfs filesystem. | |
323 | * @value: a pointer to the variable that the file should read to and write | |
324 | * from. | |
325 | */ | |
2ebefc50 RG |
326 | struct dentry *debugfs_create_x16(const char *name, mode_t mode, |
327 | struct dentry *parent, u16 *value) | |
328 | { | |
e4792aa3 RG |
329 | /* if there are no write bits set, make read only */ |
330 | if (!(mode & S_IWUGO)) | |
331 | return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); | |
332 | /* if there are no read bits set, make write only */ | |
333 | if (!(mode & S_IRUGO)) | |
334 | return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); | |
335 | ||
2ebefc50 RG |
336 | return debugfs_create_file(name, mode, parent, value, &fops_x16); |
337 | } | |
338 | EXPORT_SYMBOL_GPL(debugfs_create_x16); | |
339 | ||
e6716b87 RD |
340 | /** |
341 | * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value | |
342 | * @name: a pointer to a string containing the name of the file to create. | |
343 | * @mode: the permission that the file should have | |
344 | * @parent: a pointer to the parent dentry for this file. This should be a | |
345 | * directory dentry if set. If this parameter is %NULL, then the | |
346 | * file will be created in the root of the debugfs filesystem. | |
347 | * @value: a pointer to the variable that the file should read to and write | |
348 | * from. | |
349 | */ | |
2ebefc50 RG |
350 | struct dentry *debugfs_create_x32(const char *name, mode_t mode, |
351 | struct dentry *parent, u32 *value) | |
352 | { | |
e4792aa3 RG |
353 | /* if there are no write bits set, make read only */ |
354 | if (!(mode & S_IWUGO)) | |
355 | return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); | |
356 | /* if there are no read bits set, make write only */ | |
357 | if (!(mode & S_IRUGO)) | |
358 | return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); | |
359 | ||
2ebefc50 RG |
360 | return debugfs_create_file(name, mode, parent, value, &fops_x32); |
361 | } | |
362 | EXPORT_SYMBOL_GPL(debugfs_create_x32); | |
363 | ||
15b0beaa HY |
364 | /** |
365 | * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value | |
366 | * @name: a pointer to a string containing the name of the file to create. | |
367 | * @mode: the permission that the file should have | |
368 | * @parent: a pointer to the parent dentry for this file. This should be a | |
369 | * directory dentry if set. If this parameter is %NULL, then the | |
370 | * file will be created in the root of the debugfs filesystem. | |
371 | * @value: a pointer to the variable that the file should read to and write | |
372 | * from. | |
373 | */ | |
374 | struct dentry *debugfs_create_x64(const char *name, mode_t mode, | |
375 | struct dentry *parent, u64 *value) | |
376 | { | |
377 | return debugfs_create_file(name, mode, parent, value, &fops_x64); | |
378 | } | |
379 | EXPORT_SYMBOL_GPL(debugfs_create_x64); | |
380 | ||
5e078787 IPG |
381 | |
382 | static int debugfs_size_t_set(void *data, u64 val) | |
383 | { | |
384 | *(size_t *)data = val; | |
385 | return 0; | |
386 | } | |
387 | static int debugfs_size_t_get(void *data, u64 *val) | |
388 | { | |
389 | *val = *(size_t *)data; | |
390 | return 0; | |
391 | } | |
392 | DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, | |
393 | "%llu\n"); /* %llu and %zu are more or less the same */ | |
394 | ||
395 | /** | |
396 | * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value | |
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 | |
400 | * directory dentry if set. If this parameter is %NULL, then the | |
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 | struct dentry *debugfs_create_size_t(const char *name, mode_t mode, | |
406 | struct dentry *parent, size_t *value) | |
407 | { | |
408 | return debugfs_create_file(name, mode, parent, value, &fops_size_t); | |
409 | } | |
410 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); | |
411 | ||
412 | ||
1da177e4 LT |
413 | static ssize_t read_file_bool(struct file *file, char __user *user_buf, |
414 | size_t count, loff_t *ppos) | |
415 | { | |
416 | char buf[3]; | |
417 | u32 *val = file->private_data; | |
418 | ||
419 | if (*val) | |
420 | buf[0] = 'Y'; | |
421 | else | |
422 | buf[0] = 'N'; | |
423 | buf[1] = '\n'; | |
424 | buf[2] = 0x00; | |
425 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | |
426 | } | |
427 | ||
428 | static ssize_t write_file_bool(struct file *file, const char __user *user_buf, | |
429 | size_t count, loff_t *ppos) | |
430 | { | |
431 | char buf[32]; | |
c42d2237 | 432 | size_t buf_size; |
8705b48e | 433 | bool bv; |
1da177e4 LT |
434 | u32 *val = file->private_data; |
435 | ||
436 | buf_size = min(count, (sizeof(buf)-1)); | |
437 | if (copy_from_user(buf, user_buf, buf_size)) | |
438 | return -EFAULT; | |
439 | ||
8705b48e JC |
440 | if (strtobool(buf, &bv) == 0) |
441 | *val = bv; | |
442 | ||
1da177e4 LT |
443 | return count; |
444 | } | |
445 | ||
4b6f5d20 | 446 | static const struct file_operations fops_bool = { |
1da177e4 LT |
447 | .read = read_file_bool, |
448 | .write = write_file_bool, | |
449 | .open = default_open, | |
6038f373 | 450 | .llseek = default_llseek, |
1da177e4 LT |
451 | }; |
452 | ||
453 | /** | |
6468b3af | 454 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
1da177e4 LT |
455 | * @name: a pointer to a string containing the name of the file to create. |
456 | * @mode: the permission that the file should have | |
457 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 458 | * directory dentry if set. If this parameter is %NULL, then the |
1da177e4 LT |
459 | * file will be created in the root of the debugfs filesystem. |
460 | * @value: a pointer to the variable that the file should read to and write | |
461 | * from. | |
462 | * | |
463 | * This function creates a file in debugfs with the given name that | |
464 | * contains the value of the variable @value. If the @mode variable is so | |
465 | * set, it can be read from, and written to. | |
466 | * | |
467 | * This function will return a pointer to a dentry if it succeeds. This | |
468 | * pointer must be passed to the debugfs_remove() function when the file is | |
469 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 470 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 471 | * |
6468b3af | 472 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
1da177e4 | 473 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 474 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
1da177e4 LT |
475 | * code. |
476 | */ | |
477 | struct dentry *debugfs_create_bool(const char *name, mode_t mode, | |
478 | struct dentry *parent, u32 *value) | |
479 | { | |
480 | return debugfs_create_file(name, mode, parent, value, &fops_bool); | |
481 | } | |
482 | EXPORT_SYMBOL_GPL(debugfs_create_bool); | |
483 | ||
dd308bc3 ME |
484 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
485 | size_t count, loff_t *ppos) | |
486 | { | |
487 | struct debugfs_blob_wrapper *blob = file->private_data; | |
488 | return simple_read_from_buffer(user_buf, count, ppos, blob->data, | |
489 | blob->size); | |
490 | } | |
491 | ||
00977a59 | 492 | static const struct file_operations fops_blob = { |
dd308bc3 ME |
493 | .read = read_file_blob, |
494 | .open = default_open, | |
6038f373 | 495 | .llseek = default_llseek, |
dd308bc3 ME |
496 | }; |
497 | ||
498 | /** | |
400ced61 | 499 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
dd308bc3 ME |
500 | * @name: a pointer to a string containing the name of the file to create. |
501 | * @mode: the permission that the file should have | |
502 | * @parent: a pointer to the parent dentry for this file. This should be a | |
6468b3af | 503 | * directory dentry if set. If this parameter is %NULL, then the |
dd308bc3 ME |
504 | * file will be created in the root of the debugfs filesystem. |
505 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer | |
506 | * to the blob data and the size of the data. | |
507 | * | |
508 | * This function creates a file in debugfs with the given name that exports | |
509 | * @blob->data as a binary blob. If the @mode variable is so set it can be | |
510 | * read from. Writing is not supported. | |
511 | * | |
512 | * This function will return a pointer to a dentry if it succeeds. This | |
513 | * pointer must be passed to the debugfs_remove() function when the file is | |
514 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 515 | * you are responsible here.) If an error occurs, %NULL will be returned. |
dd308bc3 | 516 | * |
6468b3af | 517 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
dd308bc3 | 518 | * returned. It is not wise to check for this value, but rather, check for |
6468b3af | 519 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
dd308bc3 ME |
520 | * code. |
521 | */ | |
522 | struct dentry *debugfs_create_blob(const char *name, mode_t mode, | |
523 | struct dentry *parent, | |
524 | struct debugfs_blob_wrapper *blob) | |
525 | { | |
526 | return debugfs_create_file(name, mode, parent, blob, &fops_blob); | |
527 | } | |
528 | EXPORT_SYMBOL_GPL(debugfs_create_blob); | |
1a087c6a AR |
529 | |
530 | /* | |
531 | * The regset32 stuff is used to print 32-bit registers using the | |
532 | * seq_file utilities. We offer printing a register set in an already-opened | |
533 | * sequential file or create a debugfs file that only prints a regset32. | |
534 | */ | |
535 | ||
536 | /** | |
537 | * debugfs_print_regs32 - use seq_print to describe a set of registers | |
538 | * @s: the seq_file structure being used to generate output | |
539 | * @regs: an array if struct debugfs_reg32 structures | |
540 | * @mregs: the length of the above array | |
541 | * @base: the base address to be used in reading the registers | |
542 | * @prefix: a string to be prefixed to every output line | |
543 | * | |
544 | * This function outputs a text block describing the current values of | |
545 | * some 32-bit hardware registers. It is meant to be used within debugfs | |
546 | * files based on seq_file that need to show registers, intermixed with other | |
547 | * information. The prefix argument may be used to specify a leading string, | |
548 | * because some peripherals have several blocks of identical registers, | |
549 | * for example configuration of dma channels | |
550 | */ | |
8ee4dd9f | 551 | int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
1a087c6a AR |
552 | int nregs, void __iomem *base, char *prefix) |
553 | { | |
554 | int i, ret = 0; | |
555 | ||
556 | for (i = 0; i < nregs; i++, regs++) { | |
557 | if (prefix) | |
558 | ret += seq_printf(s, "%s", prefix); | |
559 | ret += seq_printf(s, "%s = 0x%08x\n", regs->name, | |
560 | readl((void *)(base + regs->offset))); | |
561 | } | |
562 | return ret; | |
563 | } | |
564 | EXPORT_SYMBOL_GPL(debugfs_print_regs32); | |
565 | ||
566 | static int debugfs_show_regset32(struct seq_file *s, void *data) | |
567 | { | |
568 | struct debugfs_regset32 *regset = s->private; | |
569 | ||
570 | debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); | |
571 | return 0; | |
572 | } | |
573 | ||
574 | static int debugfs_open_regset32(struct inode *inode, struct file *file) | |
575 | { | |
576 | return single_open(file, debugfs_show_regset32, inode->i_private); | |
577 | } | |
578 | ||
579 | static const struct file_operations fops_regset32 = { | |
580 | .open = debugfs_open_regset32, | |
581 | .read = seq_read, | |
582 | .llseek = seq_lseek, | |
583 | .release = single_release, | |
584 | }; | |
585 | ||
586 | /** | |
587 | * debugfs_create_regset32 - create a debugfs file that returns register values | |
588 | * @name: a pointer to a string containing the name of the file to create. | |
589 | * @mode: the permission that the file should have | |
590 | * @parent: a pointer to the parent dentry for this file. This should be a | |
591 | * directory dentry if set. If this parameter is %NULL, then the | |
592 | * file will be created in the root of the debugfs filesystem. | |
593 | * @regset: a pointer to a struct debugfs_regset32, which contains a pointer | |
594 | * to an array of register definitions, the array size and the base | |
595 | * address where the register bank is to be found. | |
596 | * | |
597 | * This function creates a file in debugfs with the given name that reports | |
598 | * the names and values of a set of 32-bit registers. If the @mode variable | |
599 | * is so set it can be read from. Writing is not supported. | |
600 | * | |
601 | * This function will return a pointer to a dentry if it succeeds. This | |
602 | * pointer must be passed to the debugfs_remove() function when the file is | |
603 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
604 | * you are responsible here.) If an error occurs, %NULL will be returned. | |
605 | * | |
606 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
607 | * returned. It is not wise to check for this value, but rather, check for | |
608 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | |
609 | * code. | |
610 | */ | |
611 | struct dentry *debugfs_create_regset32(const char *name, mode_t mode, | |
612 | struct dentry *parent, | |
613 | struct debugfs_regset32 *regset) | |
614 | { | |
615 | return debugfs_create_file(name, mode, parent, regset, &fops_regset32); | |
616 | } | |
617 | EXPORT_SYMBOL_GPL(debugfs_create_regset32); |