**********************************************************************************************************************************************
#ifndef __S3C_DRIVER_H
#define __S3C_DRIVER_H
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <linux/spinlock.h>
#include <asm/uaccess.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/string.h>
#include <linux/bcd.h>
#include <linux/miscdevice.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/sysfs.h>
#include <linux/proc_fs.h>
#include <linux/rtc.h>
#include <linux/spinlock.h>
#include <linux/usb.h>
#include <asm/uaccess.h>
#include <asm/delay.h>
#include <linux/syscalls.h> /* For sys_access*/
#include <linux/platform_device.h>
#include <linux/unistd.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/irq.h>
#include <mach/regs-gpio.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32)
#include <mach/hardware.h>
#include <mach/gpio.h>
#include <asm/irq.h>
#else
#include <asm-arm/irq.h>
#include <asm/arch/gpio.h>
#include <asm/arch/hardware.h>
#endif
//#include "plat_ioctl.h"
/* ===========================================================================
* S3C24XX device driver common macro definition
*===========================================================================*/
#define ENPULLUP 1
#define DISPULLUP 0
#define HIGHLEVEL 1
#define LOWLEVEL 0
#define INPUT 1
#define OUTPUT 0
#define OFF 0
#define ON 1
#define ENABLE 1
#define DISABLE 0
#define TRUE 1
#define FALSE 0
/* ===========================================================================
* S3C24XX device driver name and Major number define
*===========================================================================*/
#define DEV_LED_NAME "led"
#define DEV_LED_MAJOR 203
#define DEV_BUTTON_NAME "button"
#define DEV_BUTTON_MAJOR "211"
#define DEV_ADC_NAME "adc"
#define DEV_ADC_MAJOR "212"
/* ***** Bit Operate Define *****/
#define SET_BIT(data, i) ((data) |= (1 << (i))) /* Set the bit "i" in "data" to 1 */
#define CLR_BIT(data, i) ((data) &= ~(1 << (i))) /* Clear the bit "i" in "data" to 0 */
#define NOT_BIT(data, i) ((data) ^= (1 << (i))) /* Inverse the bit "i" in "data" */
#define GET_BIT(data, i) ((data) >> (i) & 1) /* Get the value of bit "i" in "data" */
#define L_SHIFT(data, i) ((data) << (i)) /* Shift "data" left for "i" bit */
#define R_SHIFT(data, i) ((data) >> (i)) /* Shift "data" Right for "i" bit */
/* ===========================================================================
* S3C24XX device driver common function definition
*===========================================================================*/
#define SLEEP(x) {DECLARE_WAIT_QUEUE_HEAD (stSleep); if (10 > x) mdelay ((x * 1000)); \
else wait_event_interruptible_timeout (stSleep, 0, (x / 10));}
#define VERSION_CODE(a,b,c) ( ((a)<<16) + ((b)<<8) + (c))
#define DRV_VERSION VERSION_CODE(DRV_MAJOR_VER, DRV_MINOR_VER, DRV_REVER_VER)
#define MAJOR_VER(a) ((a)>>16&0xFF)
#define MINOR_VER(a) ((a)>>8&0xFF)
#define REVER_VER(a) ((a)&0xFF)
#define dbg_print(format,args...) if(DISABLE!=debug) \
{printk("[kernel] ");printk(format, ##args);}
static inline void print_version(int version)
{
#ifdef __KERNEL__
printk("%d.%d.%d\n", MAJOR_VER(version), MINOR_VER(version), REVER_VER(version));
#else
printf("%d.%d.%d\n", MAJOR_VER(version), MINOR_VER(version), REVER_VER(version));
#endif
}
#endif /* __S3C_DRIVER_H */
因为这个头文件是3个驱动的头文件所有比较大
**********************************************************************************************************************************************
plat_button.c
<span style="font-family:SimSun;font-size:10px;">#include "s3c_driver.h"</span>
#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 platform device private data structure */
struct s3c_button_platform_data //按键私有数据结构体
{
struct s3c_button_info *buttons;//s3c_button_info类型的指针,用来访问按键硬件信息
int nbuttons;//按键数量
};
/* Button hardware informtation data*/
static struct s3c_button_info s3c_buttons[] = { //s3c_button_info类型的数组,用来保存每个按键的具体数据
[0] = {
.num = 1,
.name = "KEY1",
.nIRQ = IRQ_EINT0,
.gpio = S3C2410_GPF(0),
.setting = S3C2410_GPF0_EINT0,
}, 硬件原理图分析。由原理图可知每个按键所用到的外部中断分别是EINT0、EINT2、EINT3、EINT4,所对应的IO口分别是GPG0、GPG2、GPG3、GPG4
[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)//按键中断服务函数
/*为每一个button都申请一个中断号,dev_id就是button_device的一维的地址,中断发生后,dev_id就会传到中断服务函数中,
中断服务函数根据dev_id判断是那个button按下了。*/
{
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;//#define IRQ_NONE (0) 失败则返回0
/* 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;//#define IRQ_HANDLED (1) 成功则返回1
}
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;//首先设置ev_press为1
wake_up_interruptible(&(button_device.waitq));//如果ev_press为0,则进程进入休眠状态,为1则响应中断,唤醒等待队列让设备进行读取。
}
/* 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);//container_of能根据结构体成员的地址知道结构体的首地址;
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);
//void *kmalloc(size_t size, int flags);第一个参数是要分配的块的大小第二个参数是分配标志(flags)他提供了多种kmalloc的行为。最常用的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));//每个按键都有一个定时器,所示是nbuttons*
/* 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)//读到的数据保存在缓冲区buf中,count是请求读到的字节
{
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是等待条件,这里判断是否需要唤起等待队列(即按键有没有按下),不需要则往下执行。
{
if(file->f_flags & O_NONBLOCK)//O_NONBLOCK是设置为非阻塞模式,这里是判断这个文件是否是非阻塞模式
{
//应用程序若采用非阻塞方式读取则返回错误(因为按键没有按下,如果你用非阻塞的模式去读的话则CPU一直在访问,这样浪费cpu资源)
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);//当ev_press为真时即返回
}
}
pdev->ev_press = 0;//1为按键按下产生,并清除标识为0,准备给下一次判断用
for(i=0; i<pdata->nbuttons; i++)
{
dbg_print("button[%d] status=%d\n", i, pdev->status[i]);
status |= (pdev->status[i]<<i); //如果是第一盏灯则左移1位,第二盏左移2位,以此类推用来判断是第几盏灯。
}
ret = copy_to_user(buf, (void *)&status, min(sizeof(status), count));//将内核中的按键状态数据拷贝到用户空间给应用程序使用
/*由于内核空间与用户空间的内存不能直接互访,因此借助函数copy_to_user()完成将内核空间的数据到用户空间的复制,
函数copy_from_user()完成将用户空间数据到内核空间的复制。
函数原型:unsigned long copy_to_user(void __user *to, const void *from, unsigned long n);
如果数据拷贝成功,则返回零;否则,返回没有拷贝成功的数据字节数。
*to是用户空间的指针,
*from是内核空间指针,
n表示从内核空间向用户空间拷贝数据的字节数*/
return ret ? -EFAULT : min(sizeof(status), count);
}
static unsigned int button_poll(struct file *file, poll_table * wait)//驱动中的轮询。这个与应用程序中的select的使用相对应
/*轮询的概念和作用:
使用非阻塞I/O的应用程序通常会使用select()和poll(),poll和select用于查询设备的状态,以便用户程序获知是否能对设备进行非阻塞的访问,
它们都需要设备驱动程序中的poll函数支持。
select()和poll()系统调用最终会引发设备驱动设备中的poll()函数被执行。poll()函数为最终执行体
Linux下select调用的过程:
1.用户层应用程序调用select(),底层调用poll() <就是这里的button_poll函数>
2.核心层调用sys_select() -->do_select() 最终调用文件描述符fd对应的struct file类型变量的structfile_operations *f_ops的poll函数。*/
{
struct button_device *pdev = file->private_data;
unsigned int mask = 0;
poll_wait(file, &(pdev->waitq), wait); //添加等待队列到等待队列表中(poll_table)
if(pdev->ev_press)
{
mask |= POLLIN | POLLRDNORM; /* The data aviable */ //标识数据可以获得(将测试条件设置成普通和优先级数据可读)
}
return mask;//返回表示能对按键以及按下,则可以对其中数据进行普通和优先级数据的读
}
/*其实上面这个函数个人理解是给用户空间提供一个轮询的功能,非阻塞访问则用到了poll机制,不过poll函数将有2个功能函数,首先是poll_wait将这个进程加入到等待队列中,
因为如果你要唤醒它就必须要找到一个地方去等待队列去唤醒它,另一个是do_poll函数,它会帮你一遍遍去查询这个按键是否按下,有则跳出查询,没有则重新加入等待队列,
里面设置了timeout,timeout:是poll函数调用阻塞的时间,单位:毫秒;poll()函数会阻塞timeout所指定的毫秒时间长度之后返回,如果timeout==0,那么poll() 函数立即返回而不阻塞,
如果timeout==INFTIM,那么poll() 函数会一直阻塞下去,直到所检测的有按键按下的事件发生是才返回*/
/*Ps:这里补充一下用户空间select的用法:
使用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)阻塞等待
(5)若fd=1,fd=2上都发生可读事件,则select返回,此时set变为0000,0011。注意:没有事件发生的fd=5被清空。
*/
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)
{
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");
/*小结:这个驱动里面最难懂的我个人觉得是引入了中断服务程序与去抖定时器之间的操作和引入了轮询的机制。
首先我们看到open函数里面,我们先定义了所有按键的初始状态为up,(所有状态都是自己定义的,但是按键的真实状态要靠高低电平去判断,这里是我导致出现问题的致命原因)
然后setup_timer去初始化每一个定时器,给定时器加入了一个function timer->handler_timer,这个函数是一个机制,当mod_timer后会去调用这个函数.当按键按下时,传来一个
中断,这个时候我们就需要调用irqreturn_t类型的 s3c_button_intterupt函数,用来判断中断是否接受成功。(里面加入了一个去抖的程序,判断是否按键是否真实被我们按下)
经过一个时间再去判断电平是否还是低的,如果是的则判断为有效按键并设置按键状态为down,否则为无效。