plat_button驱动实例
前面对plat_button驱动代码重要部分做了简要分析,现在基于plat_button驱动代码写一个测试程序,用以测试驱动代码的功能是否正确。
驱动代码源码
/*********************************************************************************
* Copyright: (C) 2017 Li Wanneng<liwjng@gmail.com>
* All rights reserved.
*
* Filename: plat_button.c
* Description: This is the common button driver runs on S3C2440
*
* Version: 1.0.0(4/10/2017~)
* Author: Li Wanneng<liwjng@gmail.com>
* ChangeLog: 1, Release initial version on "4/10/2017 11:39:10 AM"
*
********************************************************************************/
#include "s3c_driver.h"
#define DRV_AUTHOR "Li Wanneng<liwjng@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 name */
int nIRQ; /*Button IRQ number*/
unsigned int setting; /*Button IRQ Pin Setting*/
unsigned int gpio; /*Button GPIO port */
};
/* 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;
}
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;
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);//从inode结构体获取idev信息
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);//配置gpio引脚为中断模式
irq_set_irq_type(pdata->buttons[i].nIRQ, IRQ_TYPE_EDGE_FALLING);//中断模式为下降沿触发
/* Request for button GPIO pin interrupt */
/*一旦发生中断请求,调用s3c_button_intterupt函数*/
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;
}
/*__user表示该变量是从用户空间传过来的地址,不可直接使用*/
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=0
{
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");
/*该函数修改task的状态为TASK_INTERRUPTIBLE,意味着该进程将不会继续运行直到被唤醒,然后被添加到等待队列wq中。*/
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]);
/*假如key3按下
status[0] = 0
status[1] = 0
status[2] = 1
status[3] = 0
status[2]<<2 | status = 0001<<2 | status = 0100 | 0000 = 0100
status = 0100 = 4
所以status = 4的时候表示按键3按下*/
status |= (pdev->status[i]<<i); //通过位操作判断哪一个按键按下,并将其状态保存到status
}
/*copy_to_user(void __user *to, const void *from, unsigned long n)*/
ret = copy_to_user(buf, (void *)&status, min(sizeof(status), count));//多了一个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;
/*void poll_wait(struct file *filp, wait_queue_head_t *queue, poll_table *wait);*/
/*typedef struct poll_table_struct {
poll_queue_proc qproc;
unsigned long key;
} poll_table*/
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; 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);//初始化cdev
button_device.cdev.owner = THIS_MODULE;
result = cdev_add (&(button_device.cdev), devno , 1); //添加cdev到内核
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)
{
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");
s3c_driver.h
在s3c_driver中定义了驱动程序中用到的相关宏
/********************************************************************************
* Copyright: (C) 2017 Li Wanneng<liwjng@gmail.com>
* All rights reserved.
*
* Filename: app_turnled.c
* Description: This file
*
* Version: 1.0.0(04/10/2017)
* Author: Li Wanneng <liwjng@gmail.com>
* ChangeLog: 1, Release initial version on "04/10/2017 01:26:36 AM"
*
********************************************************************************/
#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 */
makefile
1 LINUX_SRC?=../../kernel/linux-lwn-3.0.1
CROSS_COMPILE=/opt/dl/buildroot-2012.08/ARM920t/usr/bin/arm-linux-
obj-m := plat_button.o
modules:
@make -C $(LINUX_SRC) M=`pwd` modules
@make clean
clean:
rm -f *.ko.* *.o *mod.c *.order *.symvers
测试程序源码
/*********************************************************************************
* Copyright: (C) 2017 Li Wanneng<liwjng@gmail.com>
* All rights reserved.
*
* Filename: app_turnled.c
* Description: This file
*
* Version: 1.0.0(04/10/2017)
* Author: Li Wanneng <liwjng@gmail.com>
* ChangeLog: 1, Release initial version on "04/10/2017 01:26:36 AM"
*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#define KEY1 0x01
#define KEY2 0x02
#define KEY3 0x04
#define KEY4 0x08
#define PLATDRV_MAGIC 0x60
#define OFF _IO (PLATDRV_MAGIC,0x18)
#define ON _IO (PLATDRV_MAGIC,0X19)
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
int button_fd;
int led_fd;
int ret_button;
int current_button;
button_fd = open("/dev/button",0);
if(button_fd<0)
{
printf("open button failed.\n");
exit(1);
}
while(1)
{
ret_button = read(button_fd,¤t_button,sizeof(4));
if(ret_button!=sizeof(current_button))
{
printf("Read() button failed.\n");
}
else
{
switch(current_button)
{
case KEY1:
printf("Turn led0 ON.\n");
if(led_fd>=0)
{
ioctl(led_fd,OFF);
close(led_fd);
}
led_fd = open("/dev/led0",O_RDWR,755);
break;
case KEY2:
printf("Turn led1 ON\n");
if(led_fd>=0)
{
ioctl(led_fd,OFF);
close(led_fd);
}
led_fd = open("/dev/led1",O_RDWR,755);
break;
case KEY3:
printf("Turn led2 ON.\n");
if(led_fd>=0)
{
ioctl(led_fd,OFF);
close(led_fd);
}
led_fd = open("/dev/led2",O_RDWR,755);
break;
case KEY4:
printf("Turn led3 ON.\n");
if(led_fd>=0)
{
ioctl(led_fd,OFF);
close(led_fd);
}
led_fd = open("/dev/led3",O_RDWR,755);
break;
default:printf("errno.\n");
}
if(led_fd<0)
{
printf("open led failed.\n");
exit(1);
}
}
ioctl(led_fd,ON);
}
ioctl(led_fd,OFF);
close(led_fd);
close(button_fd);
return 0;
} /* ----- End of main() ----- */
从源码可以看出,测试程序实现的功能为4个按键分别对应4个LED灯,每按下一个按键,对应的LED灯点亮,其他LED灯熄灭。
编译烧录到开发板
使用make命令编译生成.ko驱动文件
使用交叉编译将测试程序编译生成可在开发板执行的二进制文件turn
在开发板上测试