diff --git a/project/module/infrared_sensor/infrared_sensor.c b/project/module/infrared_sensor/infrared_sensor.c index 04852c1..296d118 100644 --- a/project/module/infrared_sensor/infrared_sensor.c +++ b/project/module/infrared_sensor/infrared_sensor.c @@ -13,19 +13,40 @@ #include "infrared_sensor.def.h" +static unsigned int infrared_sensor_in_1 = 2; +static unsigned int infrared_sensor_in_2 = 3; +static unsigned int infrared_sensor_in_3 = 4; +static unsigned int infrared_sensor_in_4 = 17; -static int black_white_open(struct inode devfile, struct file *instance) + +static int infrared_sensor_open(struct inode devfile, struct file *instance) { + int err; + + err = gpio_request(infrared_sensor_in_1, "rpi-gpio-1"); + if (err) { + printk("gpio_request failed %d\n", err); + return -1; + } + err = gpio_direction_output(infrared_sensor_in_1); + if (err) { + printk("gpio_direction_input failed %d\n", err); + gpio_free(infrared_sensor_in_1); + return -1; + } + printk("gpio 2 successfull configured\n"); + + } -static ssize_t black_white_read( struct file *instance, char __user *buffer, +static ssize_t infrared_sensor_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 int infrared_sensor_close( struct inode devfile, struct file *instance) { } @@ -33,89 +54,9 @@ 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, + .open = infrared_sensor_open, + .release = infrared_sensor_close, + .read = infrared_sensor_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 ); +#include "infrared_sensor.h"; diff --git a/project/module/infrared_sensor/infrared_sensor.h b/project/module/infrared_sensor/infrared_sensor.h new file mode 100644 index 0000000..181b976 --- /dev/null +++ b/project/module/infrared_sensor/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