Linux 2.6.14.1
[linux/fpc-iii.git] / fs / debugfs / file.c
blobefc97d9b78604d040ba12ae9b1e418a6f9818534
1 /*
2 * file.c - part of debugfs, a tiny little debug file system
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Inc.
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.
11 * debugfs is for people to use instead of /proc or /sys.
12 * See Documentation/DocBook/kernel-api for more details.
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/fs.h>
19 #include <linux/pagemap.h>
20 #include <linux/debugfs.h>
22 static ssize_t default_read_file(struct file *file, char __user *buf,
23 size_t count, loff_t *ppos)
25 return 0;
28 static ssize_t default_write_file(struct file *file, const char __user *buf,
29 size_t count, loff_t *ppos)
31 return count;
34 static int default_open(struct inode *inode, struct file *file)
36 if (inode->u.generic_ip)
37 file->private_data = inode->u.generic_ip;
39 return 0;
42 struct file_operations debugfs_file_operations = {
43 .read = default_read_file,
44 .write = default_write_file,
45 .open = default_open,
48 static void debugfs_u8_set(void *data, u64 val)
50 *(u8 *)data = val;
52 static u64 debugfs_u8_get(void *data)
54 return *(u8 *)data;
56 DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
58 /**
59 * debugfs_create_u8 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value.
61 * @name: a pointer to a string containing the name of the file to create.
62 * @mode: the permission that the file should have
63 * @parent: a pointer to the parent dentry for this file. This should be a
64 * directory dentry if set. If this paramater is NULL, then the
65 * file will be created in the root of the debugfs filesystem.
66 * @value: a pointer to the variable that the file should read to and write
67 * from.
69 * This function creates a file in debugfs with the given name that
70 * contains the value of the variable @value. If the @mode variable is so
71 * set, it can be read from, and written to.
73 * This function will return a pointer to a dentry if it succeeds. This
74 * pointer must be passed to the debugfs_remove() function when the file is
75 * to be removed (no automatic cleanup happens if your module is unloaded,
76 * you are responsible here.) If an error occurs, NULL will be returned.
78 * If debugfs is not enabled in the kernel, the value -ENODEV will be
79 * returned. It is not wise to check for this value, but rather, check for
80 * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
81 * code.
83 struct dentry *debugfs_create_u8(const char *name, mode_t mode,
84 struct dentry *parent, u8 *value)
86 return debugfs_create_file(name, mode, parent, value, &fops_u8);
88 EXPORT_SYMBOL_GPL(debugfs_create_u8);
90 static void debugfs_u16_set(void *data, u64 val)
92 *(u16 *)data = val;
94 static u64 debugfs_u16_get(void *data)
96 return *(u16 *)data;
98 DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
101 * debugfs_create_u16 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value.
103 * @name: a pointer to a string containing the name of the file to create.
104 * @mode: the permission that the file should have
105 * @parent: a pointer to the parent dentry for this file. This should be a
106 * directory dentry if set. If this paramater is NULL, then the
107 * file will be created in the root of the debugfs filesystem.
108 * @value: a pointer to the variable that the file should read to and write
109 * from.
111 * This function creates a file in debugfs with the given name that
112 * contains the value of the variable @value. If the @mode variable is so
113 * set, it can be read from, and written to.
115 * This function will return a pointer to a dentry if it succeeds. This
116 * pointer must be passed to the debugfs_remove() function when the file is
117 * to be removed (no automatic cleanup happens if your module is unloaded,
118 * you are responsible here.) If an error occurs, NULL will be returned.
120 * If debugfs is not enabled in the kernel, the value -ENODEV will be
121 * returned. It is not wise to check for this value, but rather, check for
122 * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
123 * code.
125 struct dentry *debugfs_create_u16(const char *name, mode_t mode,
126 struct dentry *parent, u16 *value)
128 return debugfs_create_file(name, mode, parent, value, &fops_u16);
130 EXPORT_SYMBOL_GPL(debugfs_create_u16);
132 static void debugfs_u32_set(void *data, u64 val)
134 *(u32 *)data = val;
136 static u64 debugfs_u32_get(void *data)
138 return *(u32 *)data;
140 DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
143 * debugfs_create_u32 - create a file in the debugfs filesystem that is used to read and write a unsigned 8 bit value.
145 * @name: a pointer to a string containing the name of the file to create.
146 * @mode: the permission that the file should have
147 * @parent: a pointer to the parent dentry for this file. This should be a
148 * directory dentry if set. If this paramater is NULL, then the
149 * file will be created in the root of the debugfs filesystem.
150 * @value: a pointer to the variable that the file should read to and write
151 * from.
153 * This function creates a file in debugfs with the given name that
154 * contains the value of the variable @value. If the @mode variable is so
155 * set, it can be read from, and written to.
157 * This function will return a pointer to a dentry if it succeeds. This
158 * pointer must be passed to the debugfs_remove() function when the file is
159 * to be removed (no automatic cleanup happens if your module is unloaded,
160 * you are responsible here.) If an error occurs, NULL will be returned.
162 * If debugfs is not enabled in the kernel, the value -ENODEV will be
163 * returned. It is not wise to check for this value, but rather, check for
164 * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
165 * code.
167 struct dentry *debugfs_create_u32(const char *name, mode_t mode,
168 struct dentry *parent, u32 *value)
170 return debugfs_create_file(name, mode, parent, value, &fops_u32);
172 EXPORT_SYMBOL_GPL(debugfs_create_u32);
174 static ssize_t read_file_bool(struct file *file, char __user *user_buf,
175 size_t count, loff_t *ppos)
177 char buf[3];
178 u32 *val = file->private_data;
180 if (*val)
181 buf[0] = 'Y';
182 else
183 buf[0] = 'N';
184 buf[1] = '\n';
185 buf[2] = 0x00;
186 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
189 static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
190 size_t count, loff_t *ppos)
192 char buf[32];
193 int buf_size;
194 u32 *val = file->private_data;
196 buf_size = min(count, (sizeof(buf)-1));
197 if (copy_from_user(buf, user_buf, buf_size))
198 return -EFAULT;
200 switch (buf[0]) {
201 case 'y':
202 case 'Y':
203 case '1':
204 *val = 1;
205 break;
206 case 'n':
207 case 'N':
208 case '0':
209 *val = 0;
210 break;
213 return count;
216 static struct file_operations fops_bool = {
217 .read = read_file_bool,
218 .write = write_file_bool,
219 .open = default_open,
223 * debugfs_create_bool - create a file in the debugfs filesystem that is used to read and write a boolean value.
225 * @name: a pointer to a string containing the name of the file to create.
226 * @mode: the permission that the file should have
227 * @parent: a pointer to the parent dentry for this file. This should be a
228 * directory dentry if set. If this paramater is NULL, then the
229 * file will be created in the root of the debugfs filesystem.
230 * @value: a pointer to the variable that the file should read to and write
231 * from.
233 * This function creates a file in debugfs with the given name that
234 * contains the value of the variable @value. If the @mode variable is so
235 * set, it can be read from, and written to.
237 * This function will return a pointer to a dentry if it succeeds. This
238 * pointer must be passed to the debugfs_remove() function when the file is
239 * to be removed (no automatic cleanup happens if your module is unloaded,
240 * you are responsible here.) If an error occurs, NULL will be returned.
242 * If debugfs is not enabled in the kernel, the value -ENODEV will be
243 * returned. It is not wise to check for this value, but rather, check for
244 * NULL or !NULL instead as to eliminate the need for #ifdef in the calling
245 * code.
247 struct dentry *debugfs_create_bool(const char *name, mode_t mode,
248 struct dentry *parent, u32 *value)
250 return debugfs_create_file(name, mode, parent, value, &fops_bool);
252 EXPORT_SYMBOL_GPL(debugfs_create_bool);