以下模块从键盘捕获按键,并将一些消息打印到dmesg
。我已经插入了模块,我收到了dmesg
中的消息。但卸载了Linux驱动程序模块后,我崩溃了。
有人可以对此有所了解吗?我在Linux内核版本Ubuntu
3.5
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/init.h>
#include<linux/kdev_t.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/interrupt.h>
#include<linux/slab.h>
MODULE_LICENSE("GPL");
struct file_operations fops;
dev_t first;
unsigned long val1=0;
struct class *cl;
struct work_struct my_work;
static void my_wq_fun( )
{
printk("*****Mediatech********");
return;
}
void tasklet_f(unsigned long data)
{
printk("\nTasklet\n");
return;
}
DECLARE_TASKLET(t1,tasklet_f,&val1);
irqreturn_t isr(int i,void *p)
{
int ret;
printk("<1>\nfirst: You pressed a key\n");
schedule_work(&my_work);
tasklet_schedule(&t1);
printk("<1> \n second:");
printk("<1> \n third:");
}
int my_init(void)
{
printk("<1>\n I am loaded in to kernel\n");
first=MKDEV(62,0);
val1=register_chrdev(62,"myDev",&fops);
cl=class_create(THIS_MODULE,"myclass");
device_create(cl,NULL,first,NULL,"mydev2");
request_irq(1,isr,IRQF_SHARED,"myDev",&fops);
INIT_WORK( &my_work, my_wq_fun );
return 0;
}
void my_exit(void)
{
printk("<1>\n I am out of this kernel\n");
tasklet_kill(&t1);
free_irq(1,NULL);
device_destroy(cl,first);
class_unregister(cl);
unregister_chrdev(125,"myDev");
}
module_init(my_init);
module_exit(my_exit);