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