try to implement the infrared_sensor-module

This commit is contained in:
Siegfried Kienzle
2017-06-07 08:10:20 +02:00
parent 811e431b91
commit 3c1a29ca11
2 changed files with 125 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
#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";
#define NUMBER_OF_MINOR_DEVICE (1);
#include "infrared_sensor.def.h"
static int black_white_open(struct inode devfile, struct file *instance)
{
}
static ssize_t black_white_read( struct file *instance, char __user *buffer,
size_t max_bytes_to_read, loff_t *offset)
{
}
static int black_white_close( struct inode devfile, struct file *instance)
{
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = black_white_open,
.release = black_white_close,
.read = black_white_read,
};
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,4 @@
static dev_t devno;
static struct cdev chardev;
static struct class *chardev_class;
static struct device *chardev_device[NUMBER_OF_MINOR_DEVICE];