summaryrefslogtreecommitdiff
path: root/kernel-devel/linux-device/my_example.patch
blob: 6e66c144e42085e62bdd091c86429c5c0d7fe493 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index c779509..43ae42d 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -51,6 +51,11 @@ config AD525X_DPOT_SPI
 	  To compile this driver as a module, choose M here: the
 	  module will be called ad525x_dpot-spi.
 
+config MY
+	tristate "my driver example"
+	help
+	  just an example for a simple driver dummy
+
 config ATMEL_PWM
 	tristate "Atmel AT32/AT91 PWM support"
 	depends on HAVE_CLK
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 3e1d801..614a3a2 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -8,6 +8,7 @@ obj-$(CONFIG_AD525X_DPOT_I2C)	+= ad525x_dpot-i2c.o
 obj-$(CONFIG_AD525X_DPOT_SPI)	+= ad525x_dpot-spi.o
 obj-$(CONFIG_INTEL_MID_PTI)	+= pti.o
 obj-$(CONFIG_ATMEL_PWM)		+= atmel_pwm.o
+obj-$(CONFIG_MY)		+= my.o
 obj-$(CONFIG_ATMEL_SSC)		+= atmel-ssc.o
 obj-$(CONFIG_ATMEL_TCLIB)	+= atmel_tclib.o
 obj-$(CONFIG_BMP085)		+= bmp085.o
diff --git a/drivers/misc/my.c b/drivers/misc/my.c
new file mode 100644
index 0000000..49759ad
--- /dev/null
+++ b/drivers/misc/my.c
@@ -0,0 +1,88 @@
+#include <linux/init.h>
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+
+#define DEVICE_NAME "mydev"
+static struct class *dummy;
+
+static ssize_t my_show_ro(struct device *my_dev,
+	struct device_attribute *my_attr, char *buf)
+{
+	return scnprintf(buf, PAGE_SIZE, "%s\n", "show some cool value");
+}
+
+static ssize_t my_show_rw(struct device *my_dev,
+	struct device_attribute *my_attr, char *buf)
+{
+	return scnprintf(buf, PAGE_SIZE, "%s\n", "write value and look at dmesg");
+}
+
+static ssize_t my_store_rw(struct device *my_dev,
+	struct device_attribute *my_attr, const char *buf, size_t count)
+{
+	printk(KERN_ERR "my_store_rw: %s", buf);
+	return count;
+}
+
+static DEVICE_ATTR(ro, S_IRUGO, my_show_ro, NULL);
+static DEVICE_ATTR(rw, S_IWUSR | S_IRUGO, my_show_rw, my_store_rw);
+
+static struct attribute *my_dev_attrs[] = {
+	&dev_attr_ro.attr,
+	&dev_attr_rw.attr,
+	NULL,
+};
+
+static struct attribute_group my_dev_attr_group = {
+	.attrs = my_dev_attrs,
+};
+
+static const struct attribute_group *my_dev_attr_groups[] = {
+	&my_dev_attr_group,
+	NULL,
+};
+
+static struct device my_dev = {
+	.init_name = "mydevice",
+};
+
+static int __init my_init(void)
+{
+	int ret = 0;
+
+	dummy = class_create(THIS_MODULE, "dummy");
+	device_initialize(&my_dev);
+	my_dev.groups = my_dev_attr_groups;
+	my_dev.parent = NULL;
+	my_dev.bus = NULL;
+	my_dev.class = dummy;
+	ret = device_add(&my_dev);
+	if (ret) {
+		printk(KERN_ERR "my: device register failed\n");
+		goto failed_dev;
+	}
+	return 0;
+
+failed_dev:
+	device_del(&my_dev);
+	class_destroy(dummy);
+
+	return ret;
+}
+
+static void __exit my_exit(void)
+{
+	device_del(&my_dev);
+	class_destroy(dummy);
+}
+
+MODULE_AUTHOR("Manuel Traut");
+MODULE_DESCRIPTION("just my example");
+MODULE_VERSION("20120412");
+MODULE_LICENSE("GPL");
+
+module_init(my_init);
+module_exit(my_exit);