| 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> | 
 | 18 | #include <linux/pagemap.h> | 
| Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 19 | #include <linux/namei.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/debugfs.h> | 
 | 21 |  | 
 | 22 | static ssize_t default_read_file(struct file *file, char __user *buf, | 
 | 23 | 				 size_t count, loff_t *ppos) | 
 | 24 | { | 
 | 25 | 	return 0; | 
 | 26 | } | 
 | 27 |  | 
 | 28 | static ssize_t default_write_file(struct file *file, const char __user *buf, | 
 | 29 | 				   size_t count, loff_t *ppos) | 
 | 30 | { | 
 | 31 | 	return count; | 
 | 32 | } | 
 | 33 |  | 
 | 34 | static int default_open(struct inode *inode, struct file *file) | 
 | 35 | { | 
| Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 36 | 	if (inode->i_private) | 
 | 37 | 		file->private_data = inode->i_private; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 |  | 
 | 39 | 	return 0; | 
 | 40 | } | 
 | 41 |  | 
| Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 42 | const struct file_operations debugfs_file_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | 	.read =		default_read_file, | 
 | 44 | 	.write =	default_write_file, | 
 | 45 | 	.open =		default_open, | 
 | 46 | }; | 
 | 47 |  | 
| Peter Oberparleiter | 66f5496 | 2007-02-13 12:13:54 +0100 | [diff] [blame] | 48 | static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd) | 
 | 49 | { | 
 | 50 | 	nd_set_link(nd, dentry->d_inode->i_private); | 
 | 51 | 	return NULL; | 
 | 52 | } | 
 | 53 |  | 
 | 54 | const struct inode_operations debugfs_link_operations = { | 
 | 55 | 	.readlink       = generic_readlink, | 
 | 56 | 	.follow_link    = debugfs_follow_link, | 
 | 57 | }; | 
 | 58 |  | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 59 | static int debugfs_u8_set(void *data, u64 val) | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 60 | { | 
 | 61 | 	*(u8 *)data = val; | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 62 | 	return 0; | 
| 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 | static int debugfs_u8_get(void *data, u64 *val) | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 65 | { | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 66 | 	*val = *(u8 *)data; | 
 | 67 | 	return 0; | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 68 | } | 
 | 69 | 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] | 70 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); | 
 | 71 | DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 |  | 
 | 73 | /** | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 74 |  * 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] | 75 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 76 |  * @mode: the permission that the file should have | 
 | 77 |  * @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] | 78 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 |  *          file will be created in the root of the debugfs filesystem. | 
 | 80 |  * @value: a pointer to the variable that the file should read to and write | 
 | 81 |  *         from. | 
 | 82 |  * | 
 | 83 |  * This function creates a file in debugfs with the given name that | 
 | 84 |  * contains the value of the variable @value.  If the @mode variable is so | 
 | 85 |  * set, it can be read from, and written to. | 
 | 86 |  * | 
 | 87 |  * This function will return a pointer to a dentry if it succeeds.  This | 
 | 88 |  * pointer must be passed to the debugfs_remove() function when the file is | 
 | 89 |  * to be removed (no automatic cleanup happens if your module is unloaded, | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 90 |  * you are responsible here.)  If an error occurs, %NULL will be returned. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 |  * | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 92 |  * 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] | 93 |  * 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] | 94 |  * %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] | 95 |  * code. | 
 | 96 |  */ | 
 | 97 | struct dentry *debugfs_create_u8(const char *name, mode_t mode, | 
 | 98 | 				 struct dentry *parent, u8 *value) | 
 | 99 | { | 
| Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 100 | 	/* if there are no write bits set, make read only */ | 
 | 101 | 	if (!(mode & S_IWUGO)) | 
 | 102 | 		return debugfs_create_file(name, mode, parent, value, &fops_u8_ro); | 
 | 103 | 	/* if there are no read bits set, make write only */ | 
 | 104 | 	if (!(mode & S_IRUGO)) | 
 | 105 | 		return debugfs_create_file(name, mode, parent, value, &fops_u8_wo); | 
 | 106 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | 	return debugfs_create_file(name, mode, parent, value, &fops_u8); | 
 | 108 | } | 
 | 109 | EXPORT_SYMBOL_GPL(debugfs_create_u8); | 
 | 110 |  | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 111 | static int debugfs_u16_set(void *data, u64 val) | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 112 | { | 
 | 113 | 	*(u16 *)data = val; | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 114 | 	return 0; | 
| 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 | static int debugfs_u16_get(void *data, u64 *val) | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 117 | { | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 118 | 	*val = *(u16 *)data; | 
 | 119 | 	return 0; | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 120 | } | 
 | 121 | 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] | 122 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); | 
 | 123 | DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 124 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /** | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 126 |  * 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] | 127 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 128 |  * @mode: the permission that the file should have | 
 | 129 |  * @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] | 130 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 |  *          file will be created in the root of the debugfs filesystem. | 
 | 132 |  * @value: a pointer to the variable that the file should read to and write | 
 | 133 |  *         from. | 
 | 134 |  * | 
 | 135 |  * This function creates a file in debugfs with the given name that | 
 | 136 |  * contains the value of the variable @value.  If the @mode variable is so | 
 | 137 |  * set, it can be read from, and written to. | 
 | 138 |  * | 
 | 139 |  * This function will return a pointer to a dentry if it succeeds.  This | 
 | 140 |  * pointer must be passed to the debugfs_remove() function when the file is | 
 | 141 |  * to be removed (no automatic cleanup happens if your module is unloaded, | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 142 |  * you are responsible here.)  If an error occurs, %NULL will be returned. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 |  * | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 144 |  * 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] | 145 |  * 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] | 146 |  * %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] | 147 |  * code. | 
 | 148 |  */ | 
 | 149 | struct dentry *debugfs_create_u16(const char *name, mode_t mode, | 
 | 150 | 				  struct dentry *parent, u16 *value) | 
 | 151 | { | 
| Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 152 | 	/* if there are no write bits set, make read only */ | 
 | 153 | 	if (!(mode & S_IWUGO)) | 
 | 154 | 		return debugfs_create_file(name, mode, parent, value, &fops_u16_ro); | 
 | 155 | 	/* if there are no read bits set, make write only */ | 
 | 156 | 	if (!(mode & S_IRUGO)) | 
 | 157 | 		return debugfs_create_file(name, mode, parent, value, &fops_u16_wo); | 
 | 158 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | 	return debugfs_create_file(name, mode, parent, value, &fops_u16); | 
 | 160 | } | 
 | 161 | EXPORT_SYMBOL_GPL(debugfs_create_u16); | 
 | 162 |  | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 163 | static int debugfs_u32_set(void *data, u64 val) | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 164 | { | 
 | 165 | 	*(u32 *)data = val; | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 166 | 	return 0; | 
| 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 | static int debugfs_u32_get(void *data, u64 *val) | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 169 | { | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 170 | 	*val = *(u32 *)data; | 
 | 171 | 	return 0; | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 172 | } | 
 | 173 | 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] | 174 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); | 
 | 175 | DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); | 
| Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 176 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | /** | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 178 |  * 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] | 179 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 180 |  * @mode: the permission that the file should have | 
 | 181 |  * @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] | 182 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 |  *          file will be created in the root of the debugfs filesystem. | 
 | 184 |  * @value: a pointer to the variable that the file should read to and write | 
 | 185 |  *         from. | 
 | 186 |  * | 
 | 187 |  * This function creates a file in debugfs with the given name that | 
 | 188 |  * contains the value of the variable @value.  If the @mode variable is so | 
 | 189 |  * set, it can be read from, and written to. | 
 | 190 |  * | 
 | 191 |  * This function will return a pointer to a dentry if it succeeds.  This | 
 | 192 |  * pointer must be passed to the debugfs_remove() function when the file is | 
 | 193 |  * to be removed (no automatic cleanup happens if your module is unloaded, | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 194 |  * you are responsible here.)  If an error occurs, %NULL will be returned. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 |  * | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 196 |  * 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] | 197 |  * 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] | 198 |  * %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] | 199 |  * code. | 
 | 200 |  */ | 
 | 201 | struct dentry *debugfs_create_u32(const char *name, mode_t mode, | 
 | 202 | 				 struct dentry *parent, u32 *value) | 
 | 203 | { | 
| Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 204 | 	/* if there are no write bits set, make read only */ | 
 | 205 | 	if (!(mode & S_IWUGO)) | 
 | 206 | 		return debugfs_create_file(name, mode, parent, value, &fops_u32_ro); | 
 | 207 | 	/* if there are no read bits set, make write only */ | 
 | 208 | 	if (!(mode & S_IRUGO)) | 
 | 209 | 		return debugfs_create_file(name, mode, parent, value, &fops_u32_wo); | 
 | 210 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | 	return debugfs_create_file(name, mode, parent, value, &fops_u32); | 
 | 212 | } | 
 | 213 | EXPORT_SYMBOL_GPL(debugfs_create_u32); | 
 | 214 |  | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 215 | static int debugfs_u64_set(void *data, u64 val) | 
| Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 216 | { | 
 | 217 | 	*(u64 *)data = val; | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 218 | 	return 0; | 
| Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 219 | } | 
 | 220 |  | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 221 | static int debugfs_u64_get(void *data, u64 *val) | 
| Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 222 | { | 
| Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 223 | 	*val = *(u64 *)data; | 
 | 224 | 	return 0; | 
| Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 225 | } | 
 | 226 | 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] | 227 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); | 
 | 228 | DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); | 
| Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 229 |  | 
 | 230 | /** | 
 | 231 |  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value | 
 | 232 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 233 |  * @mode: the permission that the file should have | 
 | 234 |  * @parent: a pointer to the parent dentry for this file.  This should be a | 
 | 235 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
 | 236 |  *          file will be created in the root of the debugfs filesystem. | 
 | 237 |  * @value: a pointer to the variable that the file should read to and write | 
 | 238 |  *         from. | 
 | 239 |  * | 
 | 240 |  * This function creates a file in debugfs with the given name that | 
 | 241 |  * contains the value of the variable @value.  If the @mode variable is so | 
 | 242 |  * set, it can be read from, and written to. | 
 | 243 |  * | 
 | 244 |  * This function will return a pointer to a dentry if it succeeds.  This | 
 | 245 |  * pointer must be passed to the debugfs_remove() function when the file is | 
 | 246 |  * to be removed (no automatic cleanup happens if your module is unloaded, | 
 | 247 |  * you are responsible here.)  If an error occurs, %NULL will be returned. | 
 | 248 |  * | 
 | 249 |  * If debugfs is not enabled in the kernel, the value -%ENODEV will be | 
 | 250 |  * returned.  It is not wise to check for this value, but rather, check for | 
 | 251 |  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling | 
 | 252 |  * code. | 
 | 253 |  */ | 
 | 254 | struct dentry *debugfs_create_u64(const char *name, mode_t mode, | 
 | 255 | 				 struct dentry *parent, u64 *value) | 
 | 256 | { | 
| Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 257 | 	/* if there are no write bits set, make read only */ | 
 | 258 | 	if (!(mode & S_IWUGO)) | 
 | 259 | 		return debugfs_create_file(name, mode, parent, value, &fops_u64_ro); | 
 | 260 | 	/* if there are no read bits set, make write only */ | 
 | 261 | 	if (!(mode & S_IRUGO)) | 
 | 262 | 		return debugfs_create_file(name, mode, parent, value, &fops_u64_wo); | 
 | 263 |  | 
| Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 264 | 	return debugfs_create_file(name, mode, parent, value, &fops_u64); | 
 | 265 | } | 
 | 266 | EXPORT_SYMBOL_GPL(debugfs_create_u64); | 
 | 267 |  | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 268 | 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] | 269 | DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); | 
 | 270 | 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] | 271 |  | 
 | 272 | 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] | 273 | DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); | 
 | 274 | 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] | 275 |  | 
 | 276 | 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] | 277 | DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); | 
 | 278 | 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] | 279 |  | 
| Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 280 | DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n"); | 
 | 281 |  | 
| Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 282 | /* | 
| Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 283 |  * 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] | 284 |  * | 
 | 285 |  * These functions are exactly the same as the above functions (but use a hex | 
 | 286 |  * output for the decimal challenged). For details look at the above unsigned | 
 | 287 |  * decimal functions. | 
 | 288 |  */ | 
 | 289 |  | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 290 | /** | 
 | 291 |  * 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] | 292 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 293 |  * @mode: the permission that the file should have | 
 | 294 |  * @parent: a pointer to the parent dentry for this file.  This should be a | 
 | 295 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
 | 296 |  *          file will be created in the root of the debugfs filesystem. | 
 | 297 |  * @value: a pointer to the variable that the file should read to and write | 
 | 298 |  *         from. | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 299 |  */ | 
 | 300 | struct dentry *debugfs_create_x8(const char *name, mode_t mode, | 
 | 301 | 				 struct dentry *parent, u8 *value) | 
 | 302 | { | 
| Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 303 | 	/* if there are no write bits set, make read only */ | 
 | 304 | 	if (!(mode & S_IWUGO)) | 
 | 305 | 		return debugfs_create_file(name, mode, parent, value, &fops_x8_ro); | 
 | 306 | 	/* if there are no read bits set, make write only */ | 
 | 307 | 	if (!(mode & S_IRUGO)) | 
 | 308 | 		return debugfs_create_file(name, mode, parent, value, &fops_x8_wo); | 
 | 309 |  | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 310 | 	return debugfs_create_file(name, mode, parent, value, &fops_x8); | 
 | 311 | } | 
 | 312 | EXPORT_SYMBOL_GPL(debugfs_create_x8); | 
 | 313 |  | 
| Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 314 | /** | 
 | 315 |  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value | 
 | 316 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 317 |  * @mode: the permission that the file should have | 
 | 318 |  * @parent: a pointer to the parent dentry for this file.  This should be a | 
 | 319 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
 | 320 |  *          file will be created in the root of the debugfs filesystem. | 
 | 321 |  * @value: a pointer to the variable that the file should read to and write | 
 | 322 |  *         from. | 
 | 323 |  */ | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 324 | struct dentry *debugfs_create_x16(const char *name, mode_t mode, | 
 | 325 | 				 struct dentry *parent, u16 *value) | 
 | 326 | { | 
| Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 327 | 	/* if there are no write bits set, make read only */ | 
 | 328 | 	if (!(mode & S_IWUGO)) | 
 | 329 | 		return debugfs_create_file(name, mode, parent, value, &fops_x16_ro); | 
 | 330 | 	/* if there are no read bits set, make write only */ | 
 | 331 | 	if (!(mode & S_IRUGO)) | 
 | 332 | 		return debugfs_create_file(name, mode, parent, value, &fops_x16_wo); | 
 | 333 |  | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 334 | 	return debugfs_create_file(name, mode, parent, value, &fops_x16); | 
 | 335 | } | 
 | 336 | EXPORT_SYMBOL_GPL(debugfs_create_x16); | 
 | 337 |  | 
| Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 338 | /** | 
 | 339 |  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value | 
 | 340 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 341 |  * @mode: the permission that the file should have | 
 | 342 |  * @parent: a pointer to the parent dentry for this file.  This should be a | 
 | 343 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
 | 344 |  *          file will be created in the root of the debugfs filesystem. | 
 | 345 |  * @value: a pointer to the variable that the file should read to and write | 
 | 346 |  *         from. | 
 | 347 |  */ | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 348 | struct dentry *debugfs_create_x32(const char *name, mode_t mode, | 
 | 349 | 				 struct dentry *parent, u32 *value) | 
 | 350 | { | 
| Robin Getz | e4792aa | 2009-06-02 03:00:47 -0400 | [diff] [blame] | 351 | 	/* if there are no write bits set, make read only */ | 
 | 352 | 	if (!(mode & S_IWUGO)) | 
 | 353 | 		return debugfs_create_file(name, mode, parent, value, &fops_x32_ro); | 
 | 354 | 	/* if there are no read bits set, make write only */ | 
 | 355 | 	if (!(mode & S_IRUGO)) | 
 | 356 | 		return debugfs_create_file(name, mode, parent, value, &fops_x32_wo); | 
 | 357 |  | 
| Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 358 | 	return debugfs_create_file(name, mode, parent, value, &fops_x32); | 
 | 359 | } | 
 | 360 | EXPORT_SYMBOL_GPL(debugfs_create_x32); | 
 | 361 |  | 
| Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 362 | /** | 
 | 363 |  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value | 
 | 364 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 365 |  * @mode: the permission that the file should have | 
 | 366 |  * @parent: a pointer to the parent dentry for this file.  This should be a | 
 | 367 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
 | 368 |  *          file will be created in the root of the debugfs filesystem. | 
 | 369 |  * @value: a pointer to the variable that the file should read to and write | 
 | 370 |  *         from. | 
 | 371 |  */ | 
 | 372 | struct dentry *debugfs_create_x64(const char *name, mode_t mode, | 
 | 373 | 				 struct dentry *parent, u64 *value) | 
 | 374 | { | 
 | 375 | 	return debugfs_create_file(name, mode, parent, value, &fops_x64); | 
 | 376 | } | 
 | 377 | EXPORT_SYMBOL_GPL(debugfs_create_x64); | 
 | 378 |  | 
| Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 379 |  | 
 | 380 | static int debugfs_size_t_set(void *data, u64 val) | 
 | 381 | { | 
 | 382 | 	*(size_t *)data = val; | 
 | 383 | 	return 0; | 
 | 384 | } | 
 | 385 | static int debugfs_size_t_get(void *data, u64 *val) | 
 | 386 | { | 
 | 387 | 	*val = *(size_t *)data; | 
 | 388 | 	return 0; | 
 | 389 | } | 
 | 390 | DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, | 
 | 391 | 			"%llu\n");	/* %llu and %zu are more or less the same */ | 
 | 392 |  | 
 | 393 | /** | 
 | 394 |  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value | 
 | 395 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 396 |  * @mode: the permission that the file should have | 
 | 397 |  * @parent: a pointer to the parent dentry for this file.  This should be a | 
 | 398 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
 | 399 |  *          file will be created in the root of the debugfs filesystem. | 
 | 400 |  * @value: a pointer to the variable that the file should read to and write | 
 | 401 |  *         from. | 
 | 402 |  */ | 
 | 403 | struct dentry *debugfs_create_size_t(const char *name, mode_t mode, | 
 | 404 | 				     struct dentry *parent, size_t *value) | 
 | 405 | { | 
 | 406 | 	return debugfs_create_file(name, mode, parent, value, &fops_size_t); | 
 | 407 | } | 
 | 408 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); | 
 | 409 |  | 
 | 410 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | static ssize_t read_file_bool(struct file *file, char __user *user_buf, | 
 | 412 | 			      size_t count, loff_t *ppos) | 
 | 413 | { | 
 | 414 | 	char buf[3]; | 
 | 415 | 	u32 *val = file->private_data; | 
 | 416 | 	 | 
 | 417 | 	if (*val) | 
 | 418 | 		buf[0] = 'Y'; | 
 | 419 | 	else | 
 | 420 | 		buf[0] = 'N'; | 
 | 421 | 	buf[1] = '\n'; | 
 | 422 | 	buf[2] = 0x00; | 
 | 423 | 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2); | 
 | 424 | } | 
 | 425 |  | 
 | 426 | static ssize_t write_file_bool(struct file *file, const char __user *user_buf, | 
 | 427 | 			       size_t count, loff_t *ppos) | 
 | 428 | { | 
 | 429 | 	char buf[32]; | 
 | 430 | 	int buf_size; | 
 | 431 | 	u32 *val = file->private_data; | 
 | 432 |  | 
 | 433 | 	buf_size = min(count, (sizeof(buf)-1)); | 
 | 434 | 	if (copy_from_user(buf, user_buf, buf_size)) | 
 | 435 | 		return -EFAULT; | 
 | 436 |  | 
 | 437 | 	switch (buf[0]) { | 
 | 438 | 	case 'y': | 
 | 439 | 	case 'Y': | 
 | 440 | 	case '1': | 
 | 441 | 		*val = 1; | 
 | 442 | 		break; | 
 | 443 | 	case 'n': | 
 | 444 | 	case 'N': | 
 | 445 | 	case '0': | 
 | 446 | 		*val = 0; | 
 | 447 | 		break; | 
 | 448 | 	} | 
 | 449 | 	 | 
 | 450 | 	return count; | 
 | 451 | } | 
 | 452 |  | 
| Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 453 | static const struct file_operations fops_bool = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | 	.read =		read_file_bool, | 
 | 455 | 	.write =	write_file_bool, | 
 | 456 | 	.open =		default_open, | 
 | 457 | }; | 
 | 458 |  | 
 | 459 | /** | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 460 |  * 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] | 461 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 462 |  * @mode: the permission that the file should have | 
 | 463 |  * @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] | 464 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 |  *          file will be created in the root of the debugfs filesystem. | 
 | 466 |  * @value: a pointer to the variable that the file should read to and write | 
 | 467 |  *         from. | 
 | 468 |  * | 
 | 469 |  * This function creates a file in debugfs with the given name that | 
 | 470 |  * contains the value of the variable @value.  If the @mode variable is so | 
 | 471 |  * set, it can be read from, and written to. | 
 | 472 |  * | 
 | 473 |  * This function will return a pointer to a dentry if it succeeds.  This | 
 | 474 |  * pointer must be passed to the debugfs_remove() function when the file is | 
 | 475 |  * to be removed (no automatic cleanup happens if your module is unloaded, | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 476 |  * you are responsible here.)  If an error occurs, %NULL will be returned. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 |  * | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 478 |  * 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] | 479 |  * 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] | 480 |  * %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] | 481 |  * code. | 
 | 482 |  */ | 
 | 483 | struct dentry *debugfs_create_bool(const char *name, mode_t mode, | 
 | 484 | 				   struct dentry *parent, u32 *value) | 
 | 485 | { | 
 | 486 | 	return debugfs_create_file(name, mode, parent, value, &fops_bool); | 
 | 487 | } | 
 | 488 | EXPORT_SYMBOL_GPL(debugfs_create_bool); | 
 | 489 |  | 
| Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 490 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, | 
 | 491 | 			      size_t count, loff_t *ppos) | 
 | 492 | { | 
 | 493 | 	struct debugfs_blob_wrapper *blob = file->private_data; | 
 | 494 | 	return simple_read_from_buffer(user_buf, count, ppos, blob->data, | 
 | 495 | 			blob->size); | 
 | 496 | } | 
 | 497 |  | 
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 498 | static const struct file_operations fops_blob = { | 
| Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 499 | 	.read =		read_file_blob, | 
 | 500 | 	.open =		default_open, | 
 | 501 | }; | 
 | 502 |  | 
 | 503 | /** | 
| Jonathan Corbet | 400ced6 | 2009-05-25 10:15:27 -0600 | [diff] [blame] | 504 |  * 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] | 505 |  * @name: a pointer to a string containing the name of the file to create. | 
 | 506 |  * @mode: the permission that the file should have | 
 | 507 |  * @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] | 508 |  *          directory dentry if set.  If this parameter is %NULL, then the | 
| Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 509 |  *          file will be created in the root of the debugfs filesystem. | 
 | 510 |  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer | 
 | 511 |  *        to the blob data and the size of the data. | 
 | 512 |  * | 
 | 513 |  * This function creates a file in debugfs with the given name that exports | 
 | 514 |  * @blob->data as a binary blob. If the @mode variable is so set it can be | 
 | 515 |  * read from. Writing is not supported. | 
 | 516 |  * | 
 | 517 |  * This function will return a pointer to a dentry if it succeeds.  This | 
 | 518 |  * pointer must be passed to the debugfs_remove() function when the file is | 
 | 519 |  * to be removed (no automatic cleanup happens if your module is unloaded, | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 520 |  * you are responsible here.)  If an error occurs, %NULL will be returned. | 
| Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 521 |  * | 
| Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 522 |  * 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] | 523 |  * 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] | 524 |  * %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] | 525 |  * code. | 
 | 526 |  */ | 
 | 527 | struct dentry *debugfs_create_blob(const char *name, mode_t mode, | 
 | 528 | 				   struct dentry *parent, | 
 | 529 | 				   struct debugfs_blob_wrapper *blob) | 
 | 530 | { | 
 | 531 | 	return debugfs_create_file(name, mode, parent, blob, &fops_blob); | 
 | 532 | } | 
 | 533 | EXPORT_SYMBOL_GPL(debugfs_create_blob); |