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