aboutsummaryrefslogtreecommitdiff
path: root/1
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-04-18 16:11:30 +0200
committerlonkaars <loek@pipeframe.xyz>2024-04-18 16:11:30 +0200
commit2d8b061d0f442d8a8853025460a836e3482cc09b (patch)
tree17ad110cb6052d2906b98d67281e21d69458a195 /1
parentc83f52365564e70450841b632547f4563920b1fb (diff)
add character device to the system
Diffstat (limited to '1')
-rw-r--r--1/config.h5
-rw-r--r--1/main.c38
2 files changed, 37 insertions, 6 deletions
diff --git a/1/config.h b/1/config.h
new file mode 100644
index 0000000..1d15f75
--- /dev/null
+++ b/1/config.h
@@ -0,0 +1,5 @@
+#pragma once
+
+#define NODE_MAJOR 500
+#define NODE_MINOR 0
+
diff --git a/1/main.c b/1/main.c
index 88ae0d0..66ceac4 100644
--- a/1/main.c
+++ b/1/main.c
@@ -1,26 +1,52 @@
+#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/module.h>
#include "fopdrv.h"
+#include "config.h"
-MODULE_LICENSE("MIT");
-
+struct cdev *cdev;
struct file_operations fops = {
- .read = fop_read,
- .write = fop_write,
- .open = fop_open,
- .release = fop_release,
+ .read = fop_read,
+ .write = fop_write,
+ .open = fop_open,
+ .release = fop_release,
};
static int mod_init(void) {
+ int err;
+ cdev = cdev_alloc();
+ dev_t node = MKDEV(NODE_MAJOR, NODE_MINOR);
+
+ if (!cdev) {
+ err = ENOMEM;
+ goto free_cdev;
+ }
+
+ cdev->ops = &fops;
+
+ err = cdev_add(cdev, node, 1);
+ if (err < 0) {
+ printk(KERN_ERR "cdev_add failed w/ error code %d\n", err);
+ goto free_cdev;
+ }
+
printk("%s\n", __PRETTY_FUNCTION__);
return 0;
+
+free_cdev:
+ cdev_del(cdev);
+
+ printk("%s: %d\n", __PRETTY_FUNCTION__, err);
+ return err;
}
static void mod_exit(void) {
+ cdev_del(cdev);
printk("%s\n", __PRETTY_FUNCTION__);
}
module_init(mod_init);
module_exit(mod_exit);
+MODULE_LICENSE("MIT");