implemented infrared-modules

This commit is contained in:
Siegfried Kienzle
2017-06-14 08:08:33 +02:00
parent f853d6b3e8
commit 1b80f5b89e
13 changed files with 481 additions and 0 deletions

3
project/modules/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.tmp_versions
*.ko.mod
*.o.cmd

43
project/modules/Makefile Normal file
View File

@@ -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

View File

@@ -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];

View File

@@ -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 );

View File

@@ -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

View File

@@ -0,0 +1 @@
../Makefile

View File

@@ -0,0 +1,77 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#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";

View File

@@ -0,0 +1 @@
../Makefile

View File

@@ -0,0 +1,89 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#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";

View File

@@ -0,0 +1 @@
../Makefile

View File

@@ -0,0 +1,89 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#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";

View File

@@ -0,0 +1 @@
../Makefile

View File

@@ -0,0 +1,89 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#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";