summaryrefslogtreecommitdiffstats
path: root/debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'debug.c')
-rw-r--r--debug.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/debug.c b/debug.c
new file mode 100644
index 0000000..40ee10c
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,100 @@
+/*
+ * debug.c
+ *
+ * utilize debufs
+ * FIXME: R/W from /sys/kernel/debug/hello/magic doesn't work
+ *
+ */
+
+#include <linux/debugfs.h>
+#include <linux/types.h>
+#include <linux/errno.h>
+
+#include <debug.h>
+#include <main.h>
+
+#define BUFF_LEN 256
+char kernel_buff[BUFF_LEN] = {'f','o','o','b','a','r','\0'};
+
+/* debugfs_create_file's 4th param
+ * @data: a pointer to something that the caller will want to get to later
+ * on. The inode.i_private pointer will point to this value on
+ * the open() call.
+ */
+int fileprv = 1; /* hmm */
+
+/* struct file, an abstract open file, not on disk, which would be a an inode */
+static ssize_t debugfs_read(struct file *fp, char __user /* userspace */ *ubuff, size_t c, loff_t *pos)
+{
+ /**
+ * simple_read_from_buffer - copy data from the buffer to user space
+ * @to: the user space buffer to read to
+ * @count: the maximum number of bytes to read
+ * @ppos: the current position in the buffer
+ * @from: the buffer to read from
+ * @available: the size of the buffer
+ *
+ * The simple_read_from_buffer() function reads up to @count bytes from the
+ * buffer @from at offset @ppos into the user space address starting at @to.
+ *
+ * On success, the number of bytes read is returned and the offset @ppos is
+ * advanced by this number, or negative value is returned on error.
+ **/
+ return simple_read_from_buffer(ubuff, c, pos, kernel_buff, BUFF_LEN);
+}
+
+static ssize_t debugfs_write(struct file *fp, const char __user *ubuff, size_t c, loff_t *pos)
+{
+ /**
+ * simple_write_to_buffer - copy data from user space to the buffer
+ * @to: the buffer to write to
+ * @available: the size of the buffer
+ * @ppos: the current position in the buffer
+ * @from: the user space buffer to read from
+ * @count: the maximum number of bytes to read
+ *
+ * The simple_write_to_buffer() function reads up to @count bytes from the user
+ * space address starting at @from into the buffer @to at offset @ppos.
+ *
+ * On success, the number of bytes written is returned and the offset @ppos is
+ * advanced by this number, or negative value is returned on error.
+ **/
+
+ if (c > BUFF_LEN)
+ return (-EINVAL);
+
+ return simple_write_to_buffer(kernel_buff, BUFF_LEN, pos, ubuff, c);
+}
+
+int debug_init(struct dentry **dir, struct dentry **file)
+{
+ /* func ptrs that perform various operations on the device, in this case char device, well not even */
+ struct file_operations fops = {
+ .read = debugfs_read,
+ .write = debugfs_write
+ };
+ /* omitted fields are initialized to NULL */
+
+ *dir = debugfs_create_dir("hello", NULL);
+ if (!*dir)
+ return (-ENODEV);
+
+ /* regular file, read by user/group/other, write by user */
+ *file = debugfs_create_file("magic", S_IFREG | S_IRUGO | S_IWUSR, *dir, &fileprv, &fops);
+ if (!*file)
+ printk("debugfs: error creating file\n");
+
+ return 0;
+}
+
+int debug_destroy(struct dentry *dir, struct dentry *file)
+{
+ if (file)
+ debugfs_remove(file);
+
+ if (dir)
+ debugfs_remove(dir);
+
+ return 0;
+}
+