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. |
Robert P. J. Day | 883ce42 | 2008-04-25 08:52:51 -0400 | [diff] [blame] | 12 | * See Documentation/DocBook/filesystems for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame^] | 18 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 20 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 37 | if (inode->i_private) |
| 38 | file->private_data = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 43 | const struct file_operations debugfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | .read = default_read_file, |
| 45 | .write = default_write_file, |
| 46 | .open = default_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 47 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 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 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 61 | static int debugfs_u8_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 62 | { |
| 63 | *(u8 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 64 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 65 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 66 | static int debugfs_u8_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 67 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 68 | *val = *(u8 *)data; |
| 69 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 70 | } |
| 71 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 76 | * 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] | 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 |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 80 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 92 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 94 | * 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] | 95 | * 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] | 96 | * %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] | 97 | * code. |
| 98 | */ |
| 99 | struct dentry *debugfs_create_u8(const char *name, mode_t mode, |
| 100 | struct dentry *parent, u8 *value) |
| 101 | { |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return debugfs_create_file(name, mode, parent, value, &fops_u8); |
| 110 | } |
| 111 | EXPORT_SYMBOL_GPL(debugfs_create_u8); |
| 112 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 113 | static int debugfs_u16_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 114 | { |
| 115 | *(u16 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 116 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 117 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 118 | static int debugfs_u16_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 119 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 120 | *val = *(u16 *)data; |
| 121 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 122 | } |
| 123 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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"); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 128 | * 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] | 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 |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 132 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 144 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 146 | * 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] | 147 | * 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] | 148 | * %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] | 149 | * code. |
| 150 | */ |
| 151 | struct dentry *debugfs_create_u16(const char *name, mode_t mode, |
| 152 | struct dentry *parent, u16 *value) |
| 153 | { |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return debugfs_create_file(name, mode, parent, value, &fops_u16); |
| 162 | } |
| 163 | EXPORT_SYMBOL_GPL(debugfs_create_u16); |
| 164 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 165 | static int debugfs_u32_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 166 | { |
| 167 | *(u32 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 168 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 169 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 170 | static int debugfs_u32_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 171 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 172 | *val = *(u32 *)data; |
| 173 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 174 | } |
| 175 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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"); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 180 | * 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] | 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 |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 184 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 196 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 198 | * 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] | 199 | * 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] | 200 | * %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] | 201 | * code. |
| 202 | */ |
| 203 | struct dentry *debugfs_create_u32(const char *name, mode_t mode, |
| 204 | struct dentry *parent, u32 *value) |
| 205 | { |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return debugfs_create_file(name, mode, parent, value, &fops_u32); |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(debugfs_create_u32); |
| 216 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 217 | static int debugfs_u64_set(void *data, u64 val) |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 218 | { |
| 219 | *(u64 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 220 | return 0; |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 221 | } |
| 222 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 223 | static int debugfs_u64_get(void *data, u64 *val) |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 224 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 225 | *val = *(u64 *)data; |
| 226 | return 0; |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 227 | } |
| 228 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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"); |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 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 | { |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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 | |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 266 | return debugfs_create_file(name, mode, parent, value, &fops_u64); |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(debugfs_create_u64); |
| 269 | |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 270 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 273 | |
| 274 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n"); |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 277 | |
| 278 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n"); |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 281 | |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 282 | DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); |
| 283 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 284 | /* |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 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 |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 286 | * |
| 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 |
| 289 | * decimal functions. |
| 290 | */ |
| 291 | |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 292 | /** |
| 293 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 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. |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 301 | */ |
| 302 | struct dentry *debugfs_create_x8(const char *name, mode_t mode, |
| 303 | struct dentry *parent, u8 *value) |
| 304 | { |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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 | |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 312 | return debugfs_create_file(name, mode, parent, value, &fops_x8); |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(debugfs_create_x8); |
| 315 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 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 | */ |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 326 | struct dentry *debugfs_create_x16(const char *name, mode_t mode, |
| 327 | struct dentry *parent, u16 *value) |
| 328 | { |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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 | |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 336 | return debugfs_create_file(name, mode, parent, value, &fops_x16); |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(debugfs_create_x16); |
| 339 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 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 | */ |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 350 | struct dentry *debugfs_create_x32(const char *name, mode_t mode, |
| 351 | struct dentry *parent, u32 *value) |
| 352 | { |
Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 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 | |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 360 | return debugfs_create_file(name, mode, parent, value, &fops_x32); |
| 361 | } |
| 362 | EXPORT_SYMBOL_GPL(debugfs_create_x32); |
| 363 | |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 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 | |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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]; |
Stephen Boyd | c42d223 | 2011-05-12 16:50:07 -0700 | [diff] [blame] | 432 | size_t buf_size; |
Jonathan Cameron | 8705b48 | 2011-04-19 12:43:46 +0100 | [diff] [blame] | 433 | bool bv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jonathan Cameron | 8705b48 | 2011-04-19 12:43:46 +0100 | [diff] [blame] | 440 | if (strtobool(buf, &bv) == 0) |
| 441 | *val = bv; |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return count; |
| 444 | } |
| 445 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 446 | static const struct file_operations fops_bool = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | .read = read_file_bool, |
| 448 | .write = write_file_bool, |
| 449 | .open = default_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 450 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | }; |
| 452 | |
| 453 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 454 | * 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] | 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 |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 458 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 470 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 472 | * 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] | 473 | * 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] | 474 | * %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] | 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 | |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 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 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 492 | static const struct file_operations fops_blob = { |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 493 | .read = read_file_blob, |
| 494 | .open = default_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 495 | .llseek = default_llseek, |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 496 | }; |
| 497 | |
| 498 | /** |
Jonathan Corbet | 400ced6 | 2009-05-25 10:15:27 -0600 | [diff] [blame] | 499 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 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 |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 503 | * directory dentry if set. If this parameter is %NULL, then the |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 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, |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 515 | * you are responsible here.) If an error occurs, %NULL will be returned. |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 516 | * |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 517 | * 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] | 518 | * 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] | 519 | * %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] | 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); |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame^] | 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 | */ |
| 551 | int debugfs_print_regs32(struct seq_file *s, struct debugfs_reg32 *regs, |
| 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); |