1

The below module capture the key press from keyboard and will print some messages to dmesg. I have inserted the module and I was getting the messages in dmesg. But I have crashed after unloading the blow Linux driver module.

Could some one shed some light on this? I am using Ubuntu with Linux Kernel version 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);
4

0 回答 0