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