Files
resy/project/modules/_common/infrared_sensor.h
2017-06-14 08:08:33 +02:00

82 lines
1.9 KiB
C

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