aboutsummaryrefslogtreecommitdiff
path: root/driver/main.c
blob: 92ff9aabee5a305f7f56599c0a8db19d36458d79 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// #include <linux/cdev.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>

#include "main.h"
#include "platform.h"

// struct cdev *cdev;
struct device *dev;
struct class *class;
dev_t node = 0; // do not register /dev/lork

static int mod_init(void) {
	int err;

	// create /sys/class/lork
	class = class_create("lork");
	if (IS_ERR_OR_NULL(class)) {
		err = PTR_ERR(class);
		goto free_class;
	}

	// create device (required for device attributes under /sys/class)
	dev = device_create(class, NULL, node, NULL, "lork");
	if (IS_ERR_OR_NULL(dev)) {
		err = PTR_ERR(dev);
		goto free_device;
	}

	// create platform driver
	err = platform_driver_register(&lork_driver);
	if (err) goto free_platform_driver;

	// create driver attribute under /sys/bus/platform/drivers/gpio-extern
	err = driver_create_file(&lork_driver.driver, &drv_attr_int);
	if (err) goto free_drv_attr_int;

	// create device attributes under /sys/class
	err = device_create_file(dev, &dev_attr_int);
	if (err) goto free_dev_attr_int;
	err = device_create_file(dev, &dev_attr_str);
	if (err) goto free_dev_attr_str;

	printk("%s() OK\n", __PRETTY_FUNCTION__);
	return 0;

free_dev_attr_str:
	device_remove_file(dev, &dev_attr_str);
free_dev_attr_int:
	device_remove_file(dev, &dev_attr_int);
free_drv_attr_int:
	driver_remove_file(&lork_driver.driver, &drv_attr_int);
free_platform_driver:
	platform_driver_unregister(&lork_driver);
free_device:
	device_destroy(class, node);
free_class:
	class_destroy(class);
	printk("%s() -> %d\n", __PRETTY_FUNCTION__, err);
	return err;
}

static void mod_exit(void) {
	device_remove_file(dev, &dev_attr_str);
	device_remove_file(dev, &dev_attr_int);
	driver_remove_file(&lork_driver.driver, &drv_attr_int);
	platform_driver_unregister(&lork_driver);
	device_destroy(class, node);
	class_destroy(class);
	printk("%s()\n", __PRETTY_FUNCTION__);
}

module_init(mod_init);
module_exit(mod_exit);
MODULE_LICENSE("Dual MIT/GPL");