前几天学习了一个linux下的led灯的驱动,这两天认真看了一下platform平台下的按键驱动。说实话,看得自己头皮发麻,稀里糊涂。看了5,6遍勉强看懂了些。今天我就把自己分析的内容记录一下,希望也能对一些跟我一样刚刚驱动入门的同学一些帮助。
不多说,贴一下代码。
#include "s3c_driver.h"
#define DRV_AUTHOR "Guo Wenxue <guowenxue@gmail.com>"
#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,
},
[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;
}
//这是真正要加入Pplatform总线的一个struct platform_device类型的结构体
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; 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;传入的参数是setip_timer函数的最后一个参数 即第几个按键
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));//唤醒等待队列
}
/* 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; i<pdata->nbuttons; 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_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);
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)
{
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++)//不同的按键按下得到的status的值不一样
{
dbg_print("button[%d] status=%d\n", i, pdev->status[i]);
status |= (pdev->status[i]<<i);
}
ret = copy_to_user(buf, (void *)&status, min(sizeof(status), count));//从内核空间拷贝数据到用户空间
return ret ? -EFAULT : min(sizeof(status), count);
}
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);//此函数在系统调用select内部被使用,作用是把当前的文件指针挂到设备内部定义的等待.
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; i<pdata->nbuttons; 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)//probe函数,建立设备与驱动的联系
{
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_kbd",
.owner = THIS_MODULE,
},
};
static int __init s3c_button_init(void)//设备和驱动都注册到platform 总线上
{
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");
其实platform总线机制就是将驱动与设备分成了两个独立的部分,再通过platform的操作将二者联系起来。
就我自己的理解分析一下上面的驱动,有些地方我在代码处已经注释过了,这是我自己思考和网上资料查阅得到的一些理解,也不知道是不是全全部正确。
首先是设备部分,难点在于对设备的抽象,这里有几个结构体,不认真看的话,很容易迷糊。这里最开始是将单个按键的硬件抽象了,也就是struct s3c_button_info这个结构体,然后将所有按键进行了抽象,即加入了按键个数这个成员,即结构体struct s3c_button_platform_data,后面又将按键这个设备进行了抽象,即
struct button_device这个结构体,,其中包括了设备的信息,如这个设备使用的定时器,设备所对应的c_dev等等。
然后就是这个static struct platform_device s3c_button_device结构体了,这个结构体就是最后要注册到platform平台上的设备结构体。其实这一层就是结构体的嵌套使用,认真细心地看一看就行
接着就是按键对应的中断服务程序了这里要注意的是按键的消抖。
对于定时器的服务程序要注意的是定时器的重启,对于内核的定时器,用完一次后需要重新定时才能使用,这个定时器是一次性使用的。然后里面的wake_up_interruptible(&(button_device.waitq)),这个函数是唤醒睡眠的进程,这个驱动使用了轮询机制,在这里需要唤醒挂起的进程。
接下来是button_open()函数,这里面做了一些内存分配工作和一些初始化工作。这里面要注意的是,对于内核分配内存的函数是kmalloc(),初始化工作做了等待队列的初始化,定时器的初始化,中断的配置,中断的申请。
最重要的在于button_read()函数和button_poll()函数,对于“读”操作来说,是阻塞的,一般来说,当程序执行读操作的时候,若是有数据传入,则成功读出,而若是没有数据传入,则程序会阻塞在这里,而不会去做其他的事,直到有数据传入。而我们这里使用了轮询机制,即在读操作这里会将进程加入等待队列,直到按键的中断服务程序中按键按下后,才会唤醒。poll()函数对应的应用程序中select()函数,调用select()函数时,在驱动一层实际是调用poll()函数,这个函数实现了轮询机制。
后面的probe函数其实就是完成驱动在内核注册,很大程度的跟前一个驱动的init函数内容相同,个人感觉没什么特别的新内容。
最后就是驱动模块的抽象,跟设备的抽象类似的套路。
总结一下
platform驱动就是将驱动和设备分离,分别定义,处理时更加方便。这个驱动值得注意的是轮询机制在驱动下是如何实现的。
驱动这个东西感觉博客写不动,稍微想写多一点就牵扯出很多很多东西,稍微追一下某一个函数,就会牵扯出许许多多的东西,想要写的深一点就感觉水平不够。对于一些有关的知识,我会继续学习,后面陆续写在博客上的。