Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 12 | * See Documentation/DocBook/kernel-api for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/debugfs.h> |
| 20 | |
| 21 | static ssize_t default_read_file(struct file *file, char __user *buf, |
| 22 | size_t count, loff_t *ppos) |
| 23 | { |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | static ssize_t default_write_file(struct file *file, const char __user *buf, |
| 28 | size_t count, loff_t *ppos) |
| 29 | { |
| 30 | return count; |
| 31 | } |
| 32 | |
| 33 | static int default_open(struct inode *inode, struct file *file) |
| 34 | { |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 35 | if (inode->i_private) |
| 36 | file->private_data = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 41 | const struct file_operations debugfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | .read = default_read_file, |
| 43 | .write = default_write_file, |
| 44 | .open = default_open, |
| 45 | }; |
| 46 | |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 47 | static void debugfs_u8_set(void *data, u64 val) |
| 48 | { |
| 49 | *(u8 *)data = val; |
| 50 | } |
| 51 | static u64 debugfs_u8_get(void *data) |
| 52 | { |
| 53 | return *(u8 *)data; |
| 54 | } |
| 55 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 58 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * @name: a pointer to a string containing the name of the file to create. |
| 60 | * @mode: the permission that the file should have |
| 61 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 62 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * file will be created in the root of the debugfs filesystem. |
| 64 | * @value: a pointer to the variable that the file should read to and write |
| 65 | * from. |
| 66 | * |
| 67 | * This function creates a file in debugfs with the given name that |
| 68 | * contains the value of the variable @value. If the @mode variable is so |
| 69 | * set, it can be read from, and written to. |
| 70 | * |
| 71 | * This function will return a pointer to a dentry if it succeeds. This |
| 72 | * pointer must be passed to the debugfs_remove() function when the file is |
| 73 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 74 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 76 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 78 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * code. |
| 80 | */ |
| 81 | struct dentry *debugfs_create_u8(const char *name, mode_t mode, |
| 82 | struct dentry *parent, u8 *value) |
| 83 | { |
| 84 | return debugfs_create_file(name, mode, parent, value, &fops_u8); |
| 85 | } |
| 86 | EXPORT_SYMBOL_GPL(debugfs_create_u8); |
| 87 | |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 88 | static void debugfs_u16_set(void *data, u64 val) |
| 89 | { |
| 90 | *(u16 *)data = val; |
| 91 | } |
| 92 | static u64 debugfs_u16_get(void *data) |
| 93 | { |
| 94 | return *(u16 *)data; |
| 95 | } |
| 96 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 99 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | * @name: a pointer to a string containing the name of the file to create. |
| 101 | * @mode: the permission that the file should have |
| 102 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 103 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * file will be created in the root of the debugfs filesystem. |
| 105 | * @value: a pointer to the variable that the file should read to and write |
| 106 | * from. |
| 107 | * |
| 108 | * This function creates a file in debugfs with the given name that |
| 109 | * contains the value of the variable @value. If the @mode variable is so |
| 110 | * set, it can be read from, and written to. |
| 111 | * |
| 112 | * This function will return a pointer to a dentry if it succeeds. This |
| 113 | * pointer must be passed to the debugfs_remove() function when the file is |
| 114 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 115 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 117 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 119 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * code. |
| 121 | */ |
| 122 | struct dentry *debugfs_create_u16(const char *name, mode_t mode, |
| 123 | struct dentry *parent, u16 *value) |
| 124 | { |
| 125 | return debugfs_create_file(name, mode, parent, value, &fops_u16); |
| 126 | } |
| 127 | EXPORT_SYMBOL_GPL(debugfs_create_u16); |
| 128 | |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 129 | static void debugfs_u32_set(void *data, u64 val) |
| 130 | { |
| 131 | *(u32 *)data = val; |
| 132 | } |
| 133 | static u64 debugfs_u32_get(void *data) |
| 134 | { |
| 135 | return *(u32 *)data; |
| 136 | } |
| 137 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 140 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * @name: a pointer to a string containing the name of the file to create. |
| 142 | * @mode: the permission that the file should have |
| 143 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 144 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | * file will be created in the root of the debugfs filesystem. |
| 146 | * @value: a pointer to the variable that the file should read to and write |
| 147 | * from. |
| 148 | * |
| 149 | * This function creates a file in debugfs with the given name that |
| 150 | * contains the value of the variable @value. If the @mode variable is so |
| 151 | * set, it can be read from, and written to. |
| 152 | * |
| 153 | * This function will return a pointer to a dentry if it succeeds. This |
| 154 | * pointer must be passed to the debugfs_remove() function when the file is |
| 155 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 156 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 158 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 160 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | * code. |
| 162 | */ |
| 163 | struct dentry *debugfs_create_u32(const char *name, mode_t mode, |
| 164 | struct dentry *parent, u32 *value) |
| 165 | { |
| 166 | return debugfs_create_file(name, mode, parent, value, &fops_u32); |
| 167 | } |
| 168 | EXPORT_SYMBOL_GPL(debugfs_create_u32); |
| 169 | |
| 170 | static ssize_t read_file_bool(struct file *file, char __user *user_buf, |
| 171 | size_t count, loff_t *ppos) |
| 172 | { |
| 173 | char buf[3]; |
| 174 | u32 *val = file->private_data; |
| 175 | |
| 176 | if (*val) |
| 177 | buf[0] = 'Y'; |
| 178 | else |
| 179 | buf[0] = 'N'; |
| 180 | buf[1] = '\n'; |
| 181 | buf[2] = 0x00; |
| 182 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 183 | } |
| 184 | |
| 185 | static ssize_t write_file_bool(struct file *file, const char __user *user_buf, |
| 186 | size_t count, loff_t *ppos) |
| 187 | { |
| 188 | char buf[32]; |
| 189 | int buf_size; |
| 190 | u32 *val = file->private_data; |
| 191 | |
| 192 | buf_size = min(count, (sizeof(buf)-1)); |
| 193 | if (copy_from_user(buf, user_buf, buf_size)) |
| 194 | return -EFAULT; |
| 195 | |
| 196 | switch (buf[0]) { |
| 197 | case 'y': |
| 198 | case 'Y': |
| 199 | case '1': |
| 200 | *val = 1; |
| 201 | break; |
| 202 | case 'n': |
| 203 | case 'N': |
| 204 | case '0': |
| 205 | *val = 0; |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | return count; |
| 210 | } |
| 211 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 212 | static const struct file_operations fops_bool = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | .read = read_file_bool, |
| 214 | .write = write_file_bool, |
| 215 | .open = default_open, |
| 216 | }; |
| 217 | |
| 218 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 219 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | * @name: a pointer to a string containing the name of the file to create. |
| 221 | * @mode: the permission that the file should have |
| 222 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 223 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | * file will be created in the root of the debugfs filesystem. |
| 225 | * @value: a pointer to the variable that the file should read to and write |
| 226 | * from. |
| 227 | * |
| 228 | * This function creates a file in debugfs with the given name that |
| 229 | * contains the value of the variable @value. If the @mode variable is so |
| 230 | * set, it can be read from, and written to. |
| 231 | * |
| 232 | * This function will return a pointer to a dentry if it succeeds. This |
| 233 | * pointer must be passed to the debugfs_remove() function when the file is |
| 234 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 235 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 237 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 239 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | * code. |
| 241 | */ |
| 242 | struct dentry *debugfs_create_bool(const char *name, mode_t mode, |
| 243 | struct dentry *parent, u32 *value) |
| 244 | { |
| 245 | return debugfs_create_file(name, mode, parent, value, &fops_bool); |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(debugfs_create_bool); |
| 248 | |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 249 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
| 250 | size_t count, loff_t *ppos) |
| 251 | { |
| 252 | struct debugfs_blob_wrapper *blob = file->private_data; |
| 253 | return simple_read_from_buffer(user_buf, count, ppos, blob->data, |
| 254 | blob->size); |
| 255 | } |
| 256 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame^] | 257 | static const struct file_operations fops_blob = { |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 258 | .read = read_file_blob, |
| 259 | .open = default_open, |
| 260 | }; |
| 261 | |
| 262 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 263 | * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 264 | * @name: a pointer to a string containing the name of the file to create. |
| 265 | * @mode: the permission that the file should have |
| 266 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 267 | * directory dentry if set. If this parameter is %NULL, then the |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 268 | * file will be created in the root of the debugfs filesystem. |
| 269 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer |
| 270 | * to the blob data and the size of the data. |
| 271 | * |
| 272 | * This function creates a file in debugfs with the given name that exports |
| 273 | * @blob->data as a binary blob. If the @mode variable is so set it can be |
| 274 | * read from. Writing is not supported. |
| 275 | * |
| 276 | * This function will return a pointer to a dentry if it succeeds. This |
| 277 | * pointer must be passed to the debugfs_remove() function when the file is |
| 278 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 279 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 280 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 281 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 282 | * returned. It is not wise to check for this value, but rather, check for |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 283 | * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 284 | * code. |
| 285 | */ |
| 286 | struct dentry *debugfs_create_blob(const char *name, mode_t mode, |
| 287 | struct dentry *parent, |
| 288 | struct debugfs_blob_wrapper *blob) |
| 289 | { |
| 290 | return debugfs_create_file(name, mode, parent, blob, &fops_blob); |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(debugfs_create_blob); |