1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/interrupt.h>
struct file_operations fops = {
.owner = THIS_MODULE,
.write = write,
.read = read,
.open = open,
.release = release,
};
int driver_major = 23;
unsigned int compt_interupt;
bool lecture;
const unsigned int irq_souris = 19;
bool interruption;
module_init(init);
module_exit(exit);
// Initialisation du pilote
int init(void)
{
int result = register_chrdev(driver_major, "mouse_driver", &fops);
if(result < 0)
{
printk("<1>mouse_driver: cannot obtain major number %d\n", driver_major);
return result;
}
compt_interupt = 0;
lecture = false;
interruption = false;
// Installation du handler
request_irq(irq_souris, intr_handler, IRQF_SHARED, "souris usb", NULL);
return 0;
}
void exit(void)
{
free_irq(irq_souris, NULL);
unregister_chrdev(driver_major, "mouse_driver");
}
// on ouvre le pilote
int open(struct inode *inode, struct file *filp)
{
return 0;
}
// on ferme le pilote
int release(struct inode *inode, struct file *filp)
{
return 0;
}
// lire les caractères
ssize_t read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
int car;
char *output_buffer = NULL;
output_buffer = kmalloc(25, GFP_KERNEL);
memset(output_buffer, 0, 25);
if(lecture)
{
lecture = false;
return 0;
}
car = copy_to_user(buf, output_buffer, strlen(output_buffer));
if(car != 0)
{
return -EFAULT;
}
lecture = true;
kfree(output_buffer);
return strlen(output_buffer);
}
// Écriture des caractères
ssize_t write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
int car;
char *input_buffer = NULL;
input_buffer = kmalloc((count)*sizeof(buf), GFP_KERNEL);
memset(input_buffer, 0, (count)*sizeof(buf));
car = copy_from_user(input_buffer, buf, count);
if(car != 0)
{
return -EFAULT;
}
// si le caractère saisis et "d", on démarre le compteur
if(strcmp(input_buffer, "d\n") == 0)
{
interruption = true;
}
kfree(input_buffer);
return count;
}
irqreturn_t intr_handler(int irq, void *dev_id)
{
if(interruption)
compt_interupt++;
return IRQ_HANDLED;
} |
Partager