From 1b80f5b89e2d5d067d27b9c65e853bfedc852a15 Mon Sep 17 00:00:00 2001 From: Siegfried Kienzle Date: Wed, 14 Jun 2017 08:08:33 +0200 Subject: [PATCH] implemented infrared-modules --- project/modules/.gitignore | 3 + project/modules/Makefile | 43 +++++++++ project/modules/_common/infrared_sensor.def.h | 4 + project/modules/_common/infrared_sensor.h | 82 +++++++++++++++++ .../.infrared_sensor_in_1.ko.cmd | 1 + project/modules/infrared_sensor_in_1/Makefile | 1 + .../infrared_sensor_in_1.c | 77 ++++++++++++++++ project/modules/infrared_sensor_in_2/Makefile | 1 + .../infrared_sensor_in_2.c | 89 +++++++++++++++++++ project/modules/infrared_sensor_in_3/Makefile | 1 + .../infrared_sensor_in_3.c | 89 +++++++++++++++++++ project/modules/infrared_sensor_in_4/Makefile | 1 + .../infrared_sensor_in_4.c | 89 +++++++++++++++++++ 13 files changed, 481 insertions(+) create mode 100644 project/modules/.gitignore create mode 100644 project/modules/Makefile create mode 100644 project/modules/_common/infrared_sensor.def.h create mode 100644 project/modules/_common/infrared_sensor.h create mode 100644 project/modules/infrared_sensor_in_1/.infrared_sensor_in_1.ko.cmd create mode 120000 project/modules/infrared_sensor_in_1/Makefile create mode 100644 project/modules/infrared_sensor_in_1/infrared_sensor_in_1.c create mode 120000 project/modules/infrared_sensor_in_2/Makefile create mode 100644 project/modules/infrared_sensor_in_2/infrared_sensor_in_2.c create mode 120000 project/modules/infrared_sensor_in_3/Makefile create mode 100644 project/modules/infrared_sensor_in_3/infrared_sensor_in_3.c create mode 120000 project/modules/infrared_sensor_in_4/Makefile create mode 100644 project/modules/infrared_sensor_in_4/infrared_sensor_in_4.c diff --git a/project/modules/.gitignore b/project/modules/.gitignore new file mode 100644 index 0000000..33ca879 --- /dev/null +++ b/project/modules/.gitignore @@ -0,0 +1,3 @@ +.tmp_versions +*.ko.mod +*.o.cmd diff --git a/project/modules/Makefile b/project/modules/Makefile new file mode 100644 index 0000000..6a7a92c --- /dev/null +++ b/project/modules/Makefile @@ -0,0 +1,43 @@ +PWD = $(realpath $(shell pwd)) +MODULE_NAME ?= $(shell basename "$(PWD)") + +ifneq ($(KERNELRELEASE),) + # call from kernel build system + obj-m := $(MODULE_NAME).o +else + ARCH ?= aarch64 + KERNEL_ARCH ?= arm64 + CROSS_COMPILE ?= $(ARCH)-linux-gnu- + KVER ?= $(shell ls "$(PWD)/../../kernel" | grep 'linux-' | sed 's/linux-//') + KERNELDIR ?= $(PWD)/../../kernel/linux-$(KVER) + MODDESTDIR ?= /lib/modules/$(KVER)/kernel/drivers/syso + +default: + ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) MODULE_NAME=$(MODULE_NAME) $(MAKE) -C $(KERNELDIR) M=$(PWD) modules +endif + +test: $(MODULE_NAME).ko.test + +$(MODULE_NAME).ko.test: $(MODULE_NAME).test.o + $(CROSS_COMPILE)$(CC) $(LDFLAGS) -pthread $^ -o $@ + +%.o: %.c + $(CROSS_COMPILE)$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +clean: + rm -rf *.ko *.o *.mod.c *~ core .depend .*.cmd .tmp_versions *.symvers *.order + +depend .depend dep: + $(CROSS_COMPILE)$(CC) $(CFLAGS) -M *.c > .depend + +install: + install -p -m 644 $(MODULE_NAME).ko $(MODDESTDIR) + /sbin/depmod -a ${KVER} + +uninstall: + rm -f $(MODDESTDIR)/$(MODULE_NAME).ko + /sbin/depmod -a ${KVER} + +ifeq (.depend,$(wildcard .depend)) + include .depend +endif diff --git a/project/modules/_common/infrared_sensor.def.h b/project/modules/_common/infrared_sensor.def.h new file mode 100644 index 0000000..dfe45f0 --- /dev/null +++ b/project/modules/_common/infrared_sensor.def.h @@ -0,0 +1,4 @@ +static dev_t devno; +static struct cdev chardev; +static struct class *chardev_class; +static struct device *chardev_device[NUMBER_OF_MINOR_DEVICE]; diff --git a/project/modules/_common/infrared_sensor.h b/project/modules/_common/infrared_sensor.h new file mode 100644 index 0000000..181b976 --- /dev/null +++ b/project/modules/_common/infrared_sensor.h @@ -0,0 +1,82 @@ +MODULE_AUTHOR("Manuel Vögele, Siegfried Kienzle, Simon Wörner"); +MODULE_DESCRIPTION(DEVICE_NAME "driver"); +MODULE_LICENSE("GPL"); + +static int __init ModInit(void) +{ + + unsigned int i = 0; + + printk(KERN_DEBUG DEVICE_NAME ": init"); + + if (alloc_chrdev_region(&devno, 0, NUMBER_OF_MINOR_DEVICE, DEVICE_NAME) < 0) + return -EIO; + + cdev_init(&chardev, &fops); + chardev.owner = THIS_MODULE; + chardev.ops = &fops; + + if (cdev_add(&chardev, devno, NUMBER_OF_MINOR_DEVICE)) { + pr_err(DEVICE_NAME ": cdev_add failed."); + goto free_device_number; + } + + chardev_class = class_create(THIS_MODULE, DEVICE_NAME); + + if (IS_ERR(chardev_class)) { + pr_err(DEVICE_NAME ": no udev support\n"); + goto free_cdev; + } + + for (i = 0; i < NUMBER_OF_MINOR_DEVICE; ++i) + { +#if NUMBER_OF_MINOR_DEVICE == 1 + chardev_device[i] = device_create(chardev_class, NULL, devno + i, NULL, DEVICE_NAME); +#else + chardev_device[i] = device_create(chardev_class, NULL, devno + i, NULL, DEVICE_NAME "%d", i); +#endif + + if (IS_ERR(chardev_device[i])) { + pr_err(DEVICE_NAME ": device create failed\n"); + goto free_device; + } + } + +#ifdef CUSTOM_INIT + CUSTOM_INIT +#endif + + return 0; + +free_device: + for (; i > 0; --i) { + device_destroy(chardev_class, devno + i - 1); + } + class_destroy(chardev_class); +free_cdev: + cdev_del(&chardev); +free_device_number: + unregister_chrdev_region(devno, NUMBER_OF_MINOR_DEVICE); + return -EIO; +} + +static void __exit ModExit(void) +{ + unsigned int i = 0; + + printk(KERN_DEBUG DEVICE_NAME ": exit"); + +#ifdef CUSTOM_EXIT + CUSTOM_EXIT +#endif + + for (i = NUMBER_OF_MINOR_DEVICE; i > 0; --i) { + device_destroy(chardev_class, devno + i - 1); + } + class_destroy(chardev_class); + cdev_del(&chardev); + unregister_chrdev_region(devno, NUMBER_OF_MINOR_DEVICE); +} + +module_init( ModInit ); +module_exit( ModExit ); \ No newline at end of file diff --git a/project/modules/infrared_sensor_in_1/.infrared_sensor_in_1.ko.cmd b/project/modules/infrared_sensor_in_1/.infrared_sensor_in_1.ko.cmd new file mode 100644 index 0000000..a495293 --- /dev/null +++ b/project/modules/infrared_sensor_in_1/.infrared_sensor_in_1.ko.cmd @@ -0,0 +1 @@ +cmd_/home/sikienzl/git/resy-ss17-grp1/project/modules/infrared_sensor_in_1/infrared_sensor_in_1.ko := aarch64-linux-gnu-ld -EL -r -T ./scripts/module-common.lds --build-id -o /home/sikienzl/git/resy-ss17-grp1/project/modules/infrared_sensor_in_1/infrared_sensor_in_1.ko /home/sikienzl/git/resy-ss17-grp1/project/modules/infrared_sensor_in_1/infrared_sensor_in_1.o /home/sikienzl/git/resy-ss17-grp1/project/modules/infrared_sensor_in_1/infrared_sensor_in_1.mod.o ; true diff --git a/project/modules/infrared_sensor_in_1/Makefile b/project/modules/infrared_sensor_in_1/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/project/modules/infrared_sensor_in_1/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/project/modules/infrared_sensor_in_1/infrared_sensor_in_1.c b/project/modules/infrared_sensor_in_1/infrared_sensor_in_1.c new file mode 100644 index 0000000..761eca3 --- /dev/null +++ b/project/modules/infrared_sensor_in_1/infrared_sensor_in_1.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor_in_1" +#define NUMBER_OF_MINOR_DEVICE (1) + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_1 = 2; +static unsigned int count = 0; + +static int infrared_sensor_open(struct inode *devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_1, "rpi-gpio-2"); + if (err) { + printk("gpio_request for in_1 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_1); + if (err) { + printk("gpio_direction_input for in_1 failed %d\n", err); + gpio_free(infrared_sensor_in_1); + return -1; + } + printk("gpio 2 successfull configured\n"); + return 0; + + +} + + +static ssize_t infrared_sensor_read( struct file *file, char __user *buffer, size_t length, loff_t *offset) +{ + unsigned long not_copied; + u32 value=0; + + + + printk(KERN_DEBUG DEVICE_NAME ": read"); + + if (*offset > 0) { + return 0; + } + + value = gpio_get_value(infrared_sensor_in_1); + + not_copied = copy_to_user(buffer, &value, count); + *offset += count - not_copied; + + return count - not_copied; +} + +static int infrared_sensor_close( struct inode *devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_1 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h"; diff --git a/project/modules/infrared_sensor_in_2/Makefile b/project/modules/infrared_sensor_in_2/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/project/modules/infrared_sensor_in_2/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/project/modules/infrared_sensor_in_2/infrared_sensor_in_2.c b/project/modules/infrared_sensor_in_2/infrared_sensor_in_2.c new file mode 100644 index 0000000..83fcb16 --- /dev/null +++ b/project/modules/infrared_sensor_in_2/infrared_sensor_in_2.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor_in_2" +#define NUMBER_OF_MINOR_DEVICE (1) + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_2 = 3; +static unsigned int count = 0; + +static int infrared_sensor_open(struct inode *devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_2, "rpi-gpio-2"); + if (err) { + printk("gpio_request for in_1 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_2); + if (err) { + printk("gpio_direction_input for in_1 failed %d\n", err); + gpio_free(infrared_sensor_in_2); + return -1; + } + printk("gpio 2 successfull configured\n"); + return 0; + + +} + +<<<<<<< HEAD:project/module/infrared_sensor/infrared_sensor_in_2.c +static ssize_t infrared_sensor_read( struct file *file, char __user *buffer, size_t length, loff_t *offset) +{ + unsigned long not_copied; + u32 value=0; + + + + printk(KERN_DEBUG DEVICE_NAME ": read"); + + if (*offset > 0) { + return 0; + } + + value = gpio_get_value(infrared_sensor_in_2); + + not_copied = copy_to_user(buffer, &value, count); + *offset += count - not_copied; + + return count - not_copied; +======= +static ssize_t infrared_sensor_read( struct file *instance, char __user *user, + size_t max_bytes_to_read, loff_t *offset) +{ + unsigned long not_copied, to_copy; + u32 value = gpio_get_value; + + to_copy = min( max_bytes_to_read, sizeof(value) ); + not_copied=copy_to_user( user, &value, to_copy); + + return to_copy - not_copied; +>>>>>>> 8ad08b638d0490724f49b13692420465d9c65658:project/modules/infrared_sensor_in_2/infrared_sensor_in_2.c +} + +static int infrared_sensor_close( struct inode *devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_2 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h"; diff --git a/project/modules/infrared_sensor_in_3/Makefile b/project/modules/infrared_sensor_in_3/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/project/modules/infrared_sensor_in_3/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/project/modules/infrared_sensor_in_3/infrared_sensor_in_3.c b/project/modules/infrared_sensor_in_3/infrared_sensor_in_3.c new file mode 100644 index 0000000..0973048 --- /dev/null +++ b/project/modules/infrared_sensor_in_3/infrared_sensor_in_3.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor_in_3" +#define NUMBER_OF_MINOR_DEVICE (1) + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_3 = 4; +static unsigned int count = 0; + +static int infrared_sensor_open(struct inode *devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_3, "rpi-gpio-2"); + if (err) { + printk("gpio_request for in_1 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_3); + if (err) { + printk("gpio_direction_input for in_1 failed %d\n", err); + gpio_free(infrared_sensor_in_3); + return -1; + } + printk("gpio 2 successfull configured\n"); + return 0; + + +} + +<<<<<<< HEAD:project/module/infrared_sensor/infrared_sensor_in_3.c +static ssize_t infrared_sensor_read( struct file *file, char __user *buffer, size_t length, loff_t *offset) +{ + unsigned long not_copied; + u32 value=0; + + + + printk(KERN_DEBUG DEVICE_NAME ": read"); + + if (*offset > 0) { + return 0; + } + + value = gpio_get_value(infrared_sensor_in_3); + + not_copied = copy_to_user(buffer, &value, count); + *offset += count - not_copied; + + return count - not_copied; +======= +static ssize_t infrared_sensor_read( struct file *instance, char __user *user, + size_t max_bytes_to_read, loff_t *offset) +{ + unsigned long not_copied, to_copy; + u32 value = gpio_get_value; + + to_copy = min( max_bytes_to_read, sizeof(value) ); + not_copied=copy_to_user( user, &value, to_copy); + + return to_copy - not_copied; +>>>>>>> 8ad08b638d0490724f49b13692420465d9c65658:project/modules/infrared_sensor_in_3/infrared_sensor_in_3.c +} + +static int infrared_sensor_close( struct inode *devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_3 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h"; diff --git a/project/modules/infrared_sensor_in_4/Makefile b/project/modules/infrared_sensor_in_4/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/project/modules/infrared_sensor_in_4/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/project/modules/infrared_sensor_in_4/infrared_sensor_in_4.c b/project/modules/infrared_sensor_in_4/infrared_sensor_in_4.c new file mode 100644 index 0000000..39a79f0 --- /dev/null +++ b/project/modules/infrared_sensor_in_4/infrared_sensor_in_4.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "infrared_sensor_in_4" +#define NUMBER_OF_MINOR_DEVICE (1) + +#include "../_common/infrared_sensor.def.h" + +static unsigned int infrared_sensor_in_4 = 18; +static unsigned int count = 0; + +static int infrared_sensor_open(struct inode *devfile, struct file *instance) +{ + int err; + + err = gpio_request(infrared_sensor_in_4, "rpi-gpio-2"); + if (err) { + printk("gpio_request for in_1 failed %d\n", err); + return -1; + } + err = gpio_direction_input(infrared_sensor_in_4); + if (err) { + printk("gpio_direction_input for in_1 failed %d\n", err); + gpio_free(infrared_sensor_in_4); + return -1; + } + printk("gpio 2 successfull configured\n"); + return 0; + + +} + +<<<<<<< HEAD:project/module/infrared_sensor/infrared_sensor_in_4.c +static ssize_t infrared_sensor_read( struct file *file, char __user *buffer, size_t length, loff_t *offset) +{ + unsigned long not_copied; + u32 value=0; + + + + printk(KERN_DEBUG DEVICE_NAME ": read"); + + if (*offset > 0) { + return 0; + } + + value = gpio_get_value(infrared_sensor_in_4); + + not_copied = copy_to_user(buffer, &value, count); + *offset += count - not_copied; + + return count - not_copied; +======= +static ssize_t infrared_sensor_read( struct file *instance, char __user *user, + size_t max_bytes_to_read, loff_t *offset) +{ + unsigned long not_copied, to_copy; + u32 value = gpio_get_value; + + to_copy = min( max_bytes_to_read, sizeof(value) ); + not_copied=copy_to_user( user, &value, to_copy); + + return to_copy - not_copied; +>>>>>>> 8ad08b638d0490724f49b13692420465d9c65658:project/modules/infrared_sensor_in_4/infrared_sensor_in_4.c +} + +static int infrared_sensor_close( struct inode *devfile, struct file *instance) +{ + printk( "driver_close called\n" ); + gpio_free( infrared_sensor_in_4 ); + return 0; +} + + +static struct file_operations fops = { + .owner = THIS_MODULE, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_read, +}; + +#include "../_common/infrared_sensor.h";