summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Traut <manut@linutronix.de>2012-04-13 01:01:20 +0200
committerManuel Traut <manut@linutronix.de>2018-03-16 21:37:38 +0100
commit82a80bd9f60dda4ae6ea5cf230742e7d6bb1433b (patch)
tree5e031923911f2be82785e5a6c3c6d5cc72df97f6
parent5b9a6ec39f0608de73642e4ca9bfa122b5099f74 (diff)
add example for linux device
Signed-off-by: Manuel Traut <manut@linutronix.de>
-rw-r--r--kernel-devel/linux-device/my_example.patch122
-rw-r--r--kernel-devel/linux-device/pres_linux-device_en.tex5
2 files changed, 125 insertions, 2 deletions
diff --git a/kernel-devel/linux-device/my_example.patch b/kernel-devel/linux-device/my_example.patch
new file mode 100644
index 0000000..6e66c14
--- /dev/null
+++ b/kernel-devel/linux-device/my_example.patch
@@ -0,0 +1,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);
diff --git a/kernel-devel/linux-device/pres_linux-device_en.tex b/kernel-devel/linux-device/pres_linux-device_en.tex
index 1b87bc7..c234b36 100644
--- a/kernel-devel/linux-device/pres_linux-device_en.tex
+++ b/kernel-devel/linux-device/pres_linux-device_en.tex
@@ -174,6 +174,7 @@ these fields should be initialized before calling the register function:
\item parent
\item name
\item bus
+\item class
\end{itemize}
\end{frame}
@@ -203,8 +204,8 @@ Attributes can be exported to sysfs by the device driver.
\begin{lstlisting}[frame=trBL]
my.c:
...
-static DEVICE_ATTR(ro, 0444, my_show_ro, NULL);
-static DEVICE_ATTR(rw, 0644, my_show_rw, my_store_rw);
+static DEVICE_ATTR(ro_example, 0444, my_show_ro, NULL);
+static DEVICE_ATTR(rw_example, 0644, my_show_rw, my_store_rw);
static struct attribute *my_dev_attrs[] = {
&dev_attr_ro.attr,