Linux设备驱动之button按键驱动学习与小结

button按键驱动,相对于前面的LED驱动来说。增加了中断处理以及阻塞与非阻塞方式等新知识点。

先上学习的驱动代码。

内核:linux3.0

板子:fl2440

本文允许转载,但请注明出处:http://blog.csdn.net/u010944778/article/details/45113687

/*********************************************************************************
 *      Copyright:  (C) 2011 Guo Wenxue  
 *                  All rights reserved.
 *
 *       Filename:  plat_button.c
 *    Description:  This is the common button driver runs on S3C2440
 *                 
 *        Version:  1.0.0(10/27/2011~)
 *         Author:  Guo Wenxue 
 *      ChangeLog:  1, Release initial version on "10/27/2011 11:39:10 AM"
 *                 
 ********************************************************************************/
#include "s3c_driver.h"

#define DRV_AUTHOR                "Guo Wenxue "
#define DRV_DESC                  "S3C24XX button driver"

/* Driver version*/
#define DRV_MAJOR_VER             1
#define DRV_MINOR_VER             0
#define DRV_REVER_VER             0

#define DEV_NAME                  DEV_BUTTON_NAME

//#define DEV_MAJOR               DEV_BUTTON_MAJOR
#ifndef DEV_MAJOR
#define DEV_MAJOR                 0 /* dynamic major by default */
#endif

#define BUTTON_UP                 0 /* Button status is up */
#define BUTTON_DOWN               1 /* Button status is pushed down */
#define BUTTON_UNCERTAIN          2 /* Button status uncerntain */

#define TIMER_DELAY_DOWN          (HZ/50)   /*Remove button push down dithering timer delay 20ms  */
#define TIMER_DELAY_UP            (HZ/10)   /*Remove button up dithering timer delay 100ms  */

static int debug = DISABLE;
static int dev_major = DEV_MAJOR;
static int dev_minor = 0;


/*============================ Platform Device part ===============================*/
/* Button hardware informtation structure*/
struct s3c_button_info
{
    unsigned char           num;       /*Button nubmer  按键号*/  
    char *                  name;      /*Button nubmer  按键名*/
    int                     nIRQ;      /*Button IRQ number 中断号*/
    unsigned int            setting;   /*Button IRQ Pin Setting 中断引脚配置*/
    unsigned int            gpio;      /*Button GPIO port 对应的IO引脚*/
};

/* The button plaotform device private data structure */
struct s3c_button_platform_data //按键数据结构体
{
    struct s3c_button_info *buttons; //用来访问按键硬件信息的指针
    int                     nbuttons;//按键数量
};

/* Button hardware informtation data*/ //具体的相应按键信息
static struct s3c_button_info  s3c_buttons[] = {
    [0] = {
        .num = 1,
        .name = "KEY1",
        .nIRQ = IRQ_EINT0,//中断号
        .gpio = S3C2410_GPF(0),
        .setting = S3C2410_GPF0_EINT0,//datasheet手册上对应的IO中断口
    },
    [1] = {
        .num = 2,
        .name = "KEY2",
        .nIRQ = IRQ_EINT2,
        .gpio = S3C2410_GPF(2),
        .setting = S3C2410_GPF2_EINT2,
    },
    [2] = {
        .num = 3,
        .name = "KEY3",
        .nIRQ = IRQ_EINT3,
        .gpio = S3C2410_GPF(3),
        .setting = S3C2410_GPF3_EINT3,
    },
    [3] = {
        .num = 4,
        .name = "KEY4",
        .nIRQ = IRQ_EINT4,
        .gpio = S3C2410_GPF(4),
        .setting = S3C2410_GPF4_EINT4,
    },
};

/* The button platform device private data */
static struct s3c_button_platform_data s3c_button_data = {
    .buttons = s3c_buttons,
    .nbuttons = ARRAY_SIZE(s3c_buttons),
};

struct button_device
{
    unsigned char                      *status;      /* The buttons Push down or up status */
    struct s3c_button_platform_data    *data;        /* The buttons hardware information data */

    struct timer_list                  *timers;      /* The buttons remove dithering timers */
    wait_queue_head_t                  waitq;           /* Wait queue for poll()  */
    volatile int                       ev_press;     /* Button pressed event */

    struct cdev                        cdev;           
    struct class                       *dev_class; 
} button_device;

static void platform_button_release(struct device * dev)
{
    return; 
}

static struct platform_device s3c_button_device = {
    .name    = "s3c_button",
    .id      = 1,
    .dev     = 
    {
        .platform_data = &s3c_button_data, 
        .release = platform_button_release,
    },
};

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

            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)); ev_press唤醒等待队列
        }

        /* 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 ;
}


/*===================== Button device driver part ===========================*/

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

    /* 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; inbuttons; i++) 
    {
        /* Initialize all the buttons status to UP  */
        pdev->status[i] = BUTTON_UP; 

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

        /* Set all the buttons GPIO to EDGE_FALLING interrupt mode */
        s3c2410_gpio_cfgpin(pdata->buttons[i].gpio, pdata->buttons[i].setting);//按键相应的管脚配置成IRQ中断模式
        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;
}

static int button_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{ 
    struct button_device *pdev = file->private_data;
    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) //ev_press为按键标识符,即按下时才为1.结合下面等待队列程序
    {
         if(file->f_flags & O_NONBLOCK)
         {
             dbg_print("read() without block mode.\n");// O_NONBLOCK是设置为非阻塞模式
             return -EAGAIN;//若没有按键按下,还采用非阻塞模式则会一直浪费CPU的时间等待。
         }
         else
         {
             /* Read() will be blocked here */
             dbg_print("read() blocked here now.\n");
             wait_event_interruptible(pdev->waitq, pdev->ev_press); //在阻塞模式读取且没按键按下则让等待队列进入睡眠,直到ev_press为1
         }
    }

    pdev->ev_press = 0;清除标识,准备下一次

    for(i=0; inbuttons; i++)
    {
        dbg_print("button[%d] status=%d\n", i, pdev->status[i]);
        status |= (pdev->status[i]<private_data;
    unsigned int mask = 0;

    poll_wait(file, &(pdev->waitq), wait);//添加等待队列到等待队列中
    if(pdev->ev_press)
    {
        mask |= POLLIN | POLLRDNORM; /* The data aviable */ 
    }

    return mask;
}

static int button_release(struct inode *inode, struct file *file)
{ 
    int i;
    struct button_device *pdev = file->private_data;
    struct s3c_button_platform_data *pdata;
    pdata = pdev->data;

    for(i=0; inbuttons; i++) 
    {
        disable_irq(pdata->buttons[i].nIRQ);
        free_irq(pdata->buttons[i].nIRQ, (void *)i);
        del_timer(&(pdev->timers[i]));
    }

    kfree(pdev->timers);
    kfree((unsigned char *)pdev->status);

    return 0;
}


static struct file_operations button_fops = { 
    .owner = THIS_MODULE,
    .open = button_open, 
    .read = button_read,
    .poll = button_poll, 
    .release = button_release, 
};


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*/
     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;
}


static int s3c_button_remove(struct platform_device *dev)
{
    dev_t devno = MKDEV(dev_major, dev_minor);

    cdev_del(&(button_device.cdev));
    device_destroy(button_device.dev_class, devno);
    class_destroy(button_device.dev_class);

    unregister_chrdev_region(devno, 1); 
    printk("S3C %s driver removed\n", DEV_NAME);

    return 0;
}


/*===================== Platform Device and driver regist part ===========================*/

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


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 void s3c_button_exit(void)
{
    platform_driver_unregister(&s3c_button_driver);
    dbg_print("S3C %s platform device removed.\n", DEV_NAME);

    platform_device_unregister(&s3c_button_device);
    dbg_print("S3C %s platform driver removed.\n", DEV_NAME);
}

module_init(s3c_button_init);
module_exit(s3c_button_exit);

module_param(debug, int, S_IRUGO);
module_param(dev_major, int, S_IRUGO);
module_param(dev_minor, int, S_IRUGO);

MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:S3C24XX_button");

学习小结

(一)

request_irq  (中断号,中断处理程序,标志位,设备名,中断处理的参数)

在linux内核中用于申请中断的函数是request_irq(),函数原型在Kernel/irq/manage.c中定义:

int request_irq( unsigned int irq,

irq_handler_t handler,

unsigned long irqflags, 

const char *devname, 

void *dev_id)

irq是要申请的硬件中断号。

handler 是向系统注册的中断处理函数,是一个回调函数,中断发生时,系统调用这个函数,dev_id参数将被传递给它。

irqflags 是中断处理的属性

若设置了IRQF_DISABLED (老版本中的SA_INTERRUPT,本版zhon已经不支持了),则表示中断处理程序是快速处理程序,快速处理程序被调用时屏蔽所有中断,慢速处理程序不屏蔽;

若设置了IRQF_SHARED (老版本中的SA_SHIRQ),则表示多个设备共享中断。

若设置了IRQF_SAMPLE_RANDOM(老版本中的SA_SAMPLE_RANDOM),表示对系统熵有贡献,对系统获取随机数有好处。(这几个flag是可以通过或的方式同时使用的)

devname设置中断名称,通常是设备驱动程序的名称  在cat /proc/interrupts中可以看到此名称。

dev_id在中断共享时会用到,一般设置为这个设备的设备结构体或者NULL

request_irq()返回0表示成功返回-INVAL表示中断号无效或处理函数指针为NULL,返回-EBUSY表示中断已经被占用且不能共享。

(二)

首先在open函数里面会获得设备信息,并且会动态的申请内存为每一个button都创建一个定时器。再有就是动态的创建存储状态status的buf。然后初始化让所有按键的状态为BUTTON_UP以及调用Setup_timer初始化定时器以及设置定时器的回调函数button_timer_handler.,s3c2410_gpio_cfgpin把相应的管脚设置为IRQ中断模式(否则可能是GPIO模式),irq_set_irq_type把相应的中断号设置为下降沿触发的方式。然后request_irq()把中断注册给内核。一旦发生中断则调用s3c_button_intterupt这个中断处理程序。

紧接着来到s3c_button_intterupt处理程序:通过判断获取具体的中断号之后便进行消抖的处理。(此时通过按键的状态不管是否按下依然为BUTTON_up状态,这是我们最初认为设定的)紧接着通过mod_timer激活定时器,在超时之后,然后进入定时器原本设定好的定时器中断处理程序button_timer_handler中正式的处理这个中断请求。也就是说中断处理程序s3c_button_intterupt()只是确认按键是否有效,真正处理中断的程序是定时器里面的button_timer_handler( )

再看Button_timer_handler函数:因为当按下的时候进入了s3c_button_intterupt函数然后定时器超时调用到了button_timer_handler里,此时为低电平。然后我们这时候将他设置为BUTTON_DOWN状态。且唤醒等待队列让设备进行读取ev_press为按键按下的发生标识,按下ev_press即为1也通过ev_press参数值来设置工作模式,选定阻塞或者是非阻塞模式。

对于read函数来说:获取了按键的设备信息之后通过ev_press来剔除非阻塞模式选择阻塞模式,在阻塞模式下读取且没按键按下则让等待队列进入睡眠,直到ev_press为1。通过位运算来判断哪一个按键按下并保存状态值到status中。

另外还有poll函数,类似于应用层select,是通过轮询方式不断的监听。说一下驱动中的轮询。这个与应用程序中select的使用相对应

/* 轮询的概念和作用:

使用非阻塞I/O的应用程序通常会使用select()和poll()用于查询设备的状态,以便获知用户程序是否能对设备进行非阻塞的访问.他们都需要设备驱动程序中的poll函数支持。select()和poll()系统调用最终会引发设备驱动中的poll()函数被执行,poll()函数为最终的执行体。

Linux下select调用的过程:

1.用户层应用程序调用select(),底层调用poll()

2.核心层调用sys_select() -> do_select() 最终调用文件描述符fd对应的struct file类型变量的structfile_operations fops中的polll函数。

用户空间的select函数的用法:先调用宏FD_ZERO将指定的fd_set清零,然后调用宏FD_SET将需要测试的fd加入fd_set,接着调用函数select测试fd_set中的所有fd,最后用宏FD_ISSET检查某个fd在函数select调用后,相应位是否仍然为1。

理解select模型的关键在于理解fd_set,为说明方便,取fd_set长度为1字节,fd_set中的每一bit可以对应一个文件描述符fd。则1字节长的fd_set最大可以对应8个fd

   (1)执行fd_set set; FD_ZERO(&set);则set用位表示是0000,0000。
   (2)若fd=5,执行FD_SET(fd,&set);后set变为0001,0000(第5位置为1)
   (3)若再加入fd=2,fd=1,则set变为0001,0011
   (4)执行select(6,&set,0,0,0)阻塞等待 //最大fd值+1
   (5)若fd=1,fd=2上都发生可读事件,则select返回,此时set变为0000,0011。注意:没有事件发生的fd=5被清空

推荐博文:http://blog.csdn.net/lingfengtengfei/article/details/12392449 select函数详解

下面简介阻塞与非阻塞方式:

阻塞操作: 是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后再进行操作,被挂起的进程会进入休眠状态,被从调度器的运行队列中移除,直到条件被满足。
非阻塞操作: 是指在执行设备操作时,若不能获得资源,并不挂起,或者放弃他或者不停地的查询,直到可运行时

而我们驱动中等待队列使用的则是阻塞的方式,首先申请一个等待队列,一旦阻塞该进程就进入休眠将资源让给其他进程的,然后直到满足条件再唤醒等待队列,重新执行程序。

(三)

个人觉得高深莫测,牛逼轰轰的是对内核中的那些宏以及功能函数的创造,比如container_of与timer。简直神奇。

小插曲:起初看程序有两个中断程序,以为注册中断后发生中断返回的中断服务程序是正牌。其实不然,对于高级的CPU都是会找个定时器来帮助处理中断。就好比领导不会何事都亲力亲为一样。那样浪费时间,不小心就把领导累死了。。。有了中断,从中断服务程序获得了具体的设备号且为有效按键中断之后就由定时器代理领导来全盘接手。如果要处理中断就会调用定时器的handler中断处理程序来处理。

还有在消抖处理那段程序时,习惯性的想的太智能,忘记了status的状态值是代码中起初自己设定好的,即使那个时候你按键是按下的,状态也是status_UP。当时困扰了很久,想着若是按下后状态自然就应该是down和低电平%……&**¥#@

另外起初看到setup _timer是初始化定时器,一直在纳闷,搞不明白到底在哪里给定时器赋了初始值,还找到回调函数handler那去了,后面看消抖调用mod_timer搞了半天才明白原来这个初始化的真正意思其实是说:把每个最初的定时器赋予超时便返回到指定的处理函数的功能。这也是初始化,只是说没有具体给这个定时器马上赋初始值而已。


下面是按键测试程序:

/*********************************************************************************
 *      Copyright:  (C) 2015 songyong
 *                  All rights reserved.
 *
 *       Filename:  copy.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2015年04月18日)
 *         Author:  sky 
 *      ChangeLog:  1, Release initial version on "2015年04月18日 13时17分14秒"
 *                 
 ********************************************************************************/
#include
#include
#include
#include
#include

#define BUTTON_STATUS  4 
#define KEY1  0x1 
#define KEY2  0x2 
#define KEY3  0x4 
#define KEY4  0x8 
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    int i = 0;
    int button_fd;
    int ret;
    int current_button;

    button_fd = open("/dev/button",0);

    if(button_fd < 0)
    {
        printf("Open buttons device faild!\n");
        exit(1);
    }
    while(1)
    {
         ret = read(button_fd,¤t_button,sizeof(BUTTON_STATUS));

    if (ret != sizeof(current_button) )
         {
           printf("Read button device faild.\n"); 
         }
    else 
        {
    switch(current_button)
            {
                case KEY1: printf("KEY1 Down.!\n");break;
                case KEY2: printf("KEY2 Down.!\n");break;
                case KEY3: printf("KEY3 Down.!\n");break;
                case KEY4: printf("KEY4 Down.!\n");break;//其实在驱动代码的定时器中断处理程序handler中已有输出打印代码
                default: printf("kkey error\n");
            }
        }
    }
    return 0;
} /* ----- End of main() ----- */


Linux设备驱动之button按键驱动学习与小结_第1张图片

接下来弄按键点亮LED灯^。^

你可能感兴趣的:(Linux设备驱动之button按键驱动学习与小结)