按键驱动分析

愿得韶华刹那,开得满树芳华 ——也顾偕 《梦落芳华》

相对于前面的LED驱动,按键驱动增加了一部分新的代码,增加了一些概念:
中断指当出现需要时,CPU暂时停止当前程序的执行转而执行处理新情况的程序和执行过程。这里具体指按键被按下后产生的中断,CPU要处理按键的事件。
去抖:通常的按键所用开关为机械弹性开关,当机械触点断开、闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一下子断开。因而在闭合及断开的瞬间均伴随有一连串的抖动,为了不产生这种现象而作的措施就是按键消抖。软件消抖的方法是不断检测按键值,直到按键值稳定。实现方法:假设未按键时输入1,按键后输入为0,抖动时不定。可以做以下检测:检测到按键输入为0之后,延时5ms~10ms,再次检测,如果按键还为0,那么就认为有按键输入。延时的5ms~10ms恰好避开了抖动期。


接下来让我们逐个分析:

当然,分析驱动代码,还得按照套路来,先看我们的init模块:
static int __init s3c_button_init(void)
{
   int       ret = 0;

   ret = platform_device_register(&s3c_button_device);
   if(ret)
   {
        printk(KERN_ERR "%s: Can't register platform device %d\n", __FUNCTION__, ret); 
        goto fail_reg_plat_dev;
   }
   dbg_print("Regist S3C %s Device successfully.\n", DEV_NAME);

   ret = platform_driver_register(&s3c_button_driver);
   if(ret)
   {
        printk(KERN_ERR "%s: Can't register platform driver %d\n", __FUNCTION__, ret); 
        goto fail_reg_plat_drv;
   }
   dbg_print("Regist S3C %s Driver successfully.\n", DEV_NAME);

   return 0;

fail_reg_plat_drv:
   platform_driver_unregister(&s3c_button_driver);
fail_reg_plat_dev:
   return ret;
}
这里就是完成平台设备与平台驱动的注册。

接下来是我们的平台设备和平台驱动:
static struct platform_device s3c_button_device = {
    .name    = "s3c_button",
    .id      = 1,
    .dev     = 
    {
        .platform_data = &s3c_button_data, 
        .release = platform_button_release,
    },
};


static struct platform_driver s3c_button_driver = { 
    .probe      = s3c_button_probe, 
    .remove     = s3c_button_remove, 
    .driver     = { 
        .name       = "s3c_button", 
        .owner      = THIS_MODULE, 
    },
};

这里注意name的匹配以及这些结构体的赋值咯。


当总线上的设备与驱动匹配成功后,我们进入probe函数:
static int s3c_button_probe(struct platform_device *dev)
{
    int result = 0;
    dev_t devno;


    /* Alloc the device for driver  */ 
    if (0 != dev_major) 
    { 
        devno = MKDEV(dev_major, dev_minor);        //设备号的获取
        result = register_chrdev_region(devno, 1, DEV_NAME); 
    } 
    else 
    { 
        result = alloc_chrdev_region(&devno, dev_minor, 1, DEV_NAME); 
        dev_major = MAJOR(devno); 
    }

    /* Alloc for device major failure */
    if (result < 0) 
    { 
        printk("%s driver can't get major %d\n", DEV_NAME, dev_major); 
        return result; 
    }

    /*  Initialize button_device structure and register cdev*/         //初始化按键设备以及注册我们的cdev结构体
     memset(&button_device, 0, sizeof(button_device));
     button_device.data = dev->dev.platform_data;
     cdev_init (&(button_device.cdev), &button_fops);
     button_device.cdev.owner  = THIS_MODULE;

     result = cdev_add (&(button_device.cdev), devno , 1); 
     if (result) 
     { 
         printk (KERN_NOTICE "error %d add %s device", result, DEV_NAME); 
         goto ERROR; 
     }

     button_device.dev_class = class_create(THIS_MODULE, DEV_NAME);     //自动创建结点
     if(IS_ERR(button_device.dev_class)) 
     { 
         printk("%s driver create class failture\n",DEV_NAME); 
         result =  -ENOMEM; 
         goto ERROR; 
     }

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)     
     device_create(button_device.dev_class, NULL, devno, NULL, DEV_NAME);
#else
     device_create (button_device.dev_class, NULL, devno, DEV_NAME);
#endif

     printk("S3C %s driver version %d.%d.%d initiliazed.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);

     return 0;

ERROR: 
     printk("S3C %s driver version %d.%d.%d install failure.\n", DEV_NAME, DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER);
     cdev_del(&(button_device.cdev)); 
     unregister_chrdev_region(devno, 1);
     return result;
}


和LED驱动一样,probe函数主要完成设备号的注册,cdev结构体的注册,以及自动创建结点等工作。

那么,我们会问,既然都和前面一样,那我们按键驱动和LED驱动有什么区别呢?

答案就在函数调用上,先看看我们的文件操作结构体:
static struct file_operations button_fops = { 
    .owner = THIS_MODULE,
    .open = button_open, 
    .read = button_read,
    .poll = button_poll, 
    .release = button_release, 
};
大家注意到这里有open,read,以及poll函数,我们的LED驱动是没有read和poll的,那么这两个函数肯定是有其特殊功能的,我们来揭开其神秘面纱:
当然先从open开始咯:
static int button_open(struct inode *inode, struct file *file)
{ 
    struct button_device *pdev ;                      //获得按键设备信息
    struct s3c_button_platform_data *pdata;
    int i, result;

    pdev = container_of(inode->i_cdev,struct button_device, cdev);  //通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址。
    pdata = pdev->data;
    file->private_data = pdev;

    /* Malloc for all the buttons remove dithering timer */
    pdev->timers = (struct timer_list *) kmalloc(pdata->nbuttons*sizeof(struct timer_list), GFP_KERNEL);   //分配去抖时间
    if(NULL == pdev->timers)
    {
        printk("Alloc %s driver for timers failure.\n", DEV_NAME);
        return -ENOMEM;
    }
    memset(pdev->timers, 0, pdata->nbuttons*sizeof(struct timer_list));   //memset常用于内存空间的初始化

    /* Malloc for all the buttons status buffer */
    pdev->status = (unsigned char *)kmalloc(pdata->nbuttons*sizeof(unsigned char), GFP_KERNEL);  //分配存储状态的空间
    if(NULL == pdev->status)
    {
        printk("Alloc %s driver for status failure.\n", DEV_NAME);
        result = -ENOMEM; 
        goto  ERROR;
    }
    memset(pdev->status, 0, pdata->nbuttons*sizeof(unsigned char));      

    init_waitqueue_head(&(pdev->waitq));   //初始化等待队列

    for(i=0; i<pdata->nbuttons; i++) 
    {
        /* Initialize all the buttons status to UP  */
        pdev->status[i] = BUTTON_UP;     //初始化按键状态为UP

        /* Initialize all the buttons' remove dithering timer */
        setup_timer(&(pdev->timers[i]), button_timer_handler, i);            //初始化定时器,回调函数button_timer_handler

        /* Set all the buttons GPIO to EDGE_FALLING interrupt mode */
        s3c2410_gpio_cfgpin(pdata->buttons[i].gpio, pdata->buttons[i].setting);   //设置相应管脚为中断模式
        irq_set_irq_type(pdata->buttons[i].nIRQ, IRQ_TYPE_EDGE_FALLING);     设置下降沿触发中断

        /* Request for button GPIO pin interrupt  */
        result = request_irq(pdata->buttons[i].nIRQ, s3c_button_intterupt, IRQF_DISABLED, DEV_NAME, (void *)i);  //中断的注册,一旦发生中断调用s3c_button_intterupt
        if( result )
        {
            result = -EBUSY;
            goto ERROR1;
        }
    }

    return 0;

ERROR1:
     kfree((unsigned char *)pdev->status);
     while(--i) 
     { 
         disable_irq(pdata->buttons[i].nIRQ); 
         free_irq(pdata->buttons[i].nIRQ, (void *)i); 
     }

ERROR:
     kfree(pdev->timers);

     return result;
}
open函数主要是做一些初始化以及分配空间的工作,这里注册了中断,设置了中断模式为下降沿触发。下面发生中断后就看看中断函数吧:
static irqreturn_t s3c_button_intterupt(int irq,void *de_id)
{
    int i;
    int found = 0;
    struct s3c_button_platform_data *pdata = button_device.data;

    for(i=0; i<pdata->nbuttons; i++)
    {
        if(irq == pdata->buttons[i].nIRQ)    //中断号匹配
        {
            found = 1;                       //判断是否产生中断
            break;
        }
    }

    if(!found) /* An ERROR interrupt  */
        return IRQ_NONE;

    /* Only when button is up then we will handle this event */   //如果按键状态一直是为按下,则我们设置其状态为未定,用定时器来去抖
    if(BUTTON_UP  == button_device.status[i])
    {
       button_device.status[i] = BUTTON_UNCERTAIN;
       mod_timer(&(button_device.timers[i]), jiffies+TIMER_DELAY_DOWN);
    }

    return IRQ_HANDLED;
}
当然,去抖需要时间,那么要获得稳定的按键状态,就必须得延时,延时函数就用来处理中断事件,接下来介绍延时函数:
static void button_timer_handler(unsigned long data)
{
    struct s3c_button_platform_data *pdata = button_device.data;
    int num =(int)data;
    int status = s3c2410_gpio_getpin( pdata->buttons[num].gpio );

    if(LOWLEVEL == status)
    {
        if(BUTTON_UNCERTAIN == button_device.status[num]) /* Come from interrupt */
        {
            //dbg_print("Key pressed!\n");
            button_device.status[num] = BUTTON_DOWN;    //按键按下,传过来的值是down

            printk("%s pressed.\n", pdata->buttons[num].name);

            /* Wake up the wait queue for read()/poll() */   //按键按下,唤醒等待队列中的函数
            button_device.ev_press = 1;                    //选择阻塞模式
            wake_up_interruptible(&(button_device.waitq));
        }

        /* Cancel the dithering  */
        mod_timer(&(button_device.timers[num]), jiffies+TIMER_DELAY_UP);    //启动定时器,延时等待按键被按下,
    }
    else
    {
        //dbg_print("Key Released!\n");
        button_device.status[num] = BUTTON_UP;
     //   enable_irq(pdata->buttons[num].nIRQ);
    }

    return ;
}
这段函数检测按键状态是否有效,以及唤醒等待队列中的函数。


read函数阻塞的实现:

static int button_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{ 
    struct button_device *pdev = file->private_data;     //将结构体button_device的信息传给这个指针
    struct s3c_button_platform_data *pdata;
    int   i, ret;
    unsigned int status = 0;

    pdata = pdev->data;

    dbg_print("ev_press: %d\n", pdev->ev_press);
    if(!pdev->ev_press)
    {
         if(file->f_flags & O_NONBLOCK)    //非阻塞模式
         {
             dbg_print("read() without block mode.\n");
             return -EAGAIN;
         }
         else
         {
             /* Read() will be blocked here */
             dbg_print("read() blocked here now.\n");     //阻塞模式,等待中断事件产生,并把此时的进程挂起
             wait_event_interruptible(pdev->waitq, pdev->ev_press);
         }
    }

    pdev->ev_press = 0;

    for(i=0; i<pdata->nbuttons; i++)                  //阻塞进行时
    {
        dbg_print("button[%d] status=%d\n", i, pdev->status[i]);
        status |= (pdev->status[i]<<i);    //通过位运算保存按键状态到status中
    }

    ret = copy_to_user(buf, (void *)&status, min(sizeof(status), count));   //把数据复制给用户

    return ret ? -EFAULT : min(sizeof(status), count);
}

最后还有个poll函数
static unsigned int button_poll(struct file *file, poll_table * wait)
{ 
    struct button_device *pdev = file->private_data;
    unsigned int mask = 0;

    poll_wait(file, &(pdev->waitq), wait);
    if(pdev->ev_press)
    {
        mask |= POLLIN | POLLRDNORM; /* The data aviable */ 
    }

    return mask;
}
poll函数用于监听查询设备状态。

分析完以上代码,这里说一下我对整个驱动程序逻辑的理解:

执行open函数,初始化等待队列以及定时器,设置按键状态,定时列表指向定时函数,设置按键模式为中断模式并且申请中断,执行poll,监视button节点是否可读,如果可读,执行read,read中被等待队列阻塞,按键标志位为1且队列被唤醒,read继续执行,清除按键标志位,将按键状态按位保存,传到用户空间。


当按键按下的时候,产生中断,然后read又要读取按键的状态,所以来不及同时处理。则read的时候就用等待队列来阻塞进程,等到确定了按键状态之后再唤醒被阻塞的进程。


简单的分析就到这了,下篇文章给出完整驱动程序以及按键点亮LED的测试程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值