aboutsummaryrefslogtreecommitdiff
path: root/1/main.c
blob: 66ceac4b46a656f445e0443df2286f3d214c1cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/module.h>

#include "fopdrv.h"
#include "config.h"

struct cdev *cdev;
struct file_operations fops = {
	.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");