这篇文章接着上篇文章介绍按键驱动的第二种实现方法,就是应用驱动和设备分离的思想。
话不多说,直接上程序:
整个程序的结构如下:
.
├── dev
│ ├── key_dev.c
│ └── Makefile
├── dri
│ ├── key_dri.c
│ └── Makefile
└── test
└── test.c
key_dev.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/irq.h>
struct jz2440_key_t
{
const char *name;
unsigned int pin;
unsigned char key_val;
unsigned int irq; /*按键的中断号 */
unsigned int setting; /* 按键对应的功能的掩码 */
unsigned int id;
};
struct jz2440_key_platform
{
struct jz2440_key_t *buttonp;
int button_num;
};
/*
* key s2 is GPF0 -- EINT0
* key s3 is GPF2 -- EINT2
* key s4 is GPG3 -- EINT11
* key s5 is GPG11-- EINT19
*/
struct jz2440_key_t key_table[] =
{
{"Button S2", S3C2410_GPF(0), 0, IRQ_EINT(0), S3C2410_GPF0_EINT0, 0},
{"Button S3", S3C2410_GPF(2), 0, IRQ_EINT(2), S3C2410_GPF2_EINT2, 1},
{"Button S4", S3C2410_GPG(3), 0, IRQ_EINT(11), S3C2410_GPG3_EINT11, 2},
{"Button S5", S3C2410_GPG(11), 0, IRQ_EINT(19), S3C2410_GPG11_EINT19, 3},
};
struct jz2440_key_platform jz2440_key = {
.buttonp = key_table,
.button_num = ARRAY_SIZE(key_table),
};
static void device_release(struct device *dev)
{
printk("platform: device release\n");
};
struct platform_device jz2440_key_dev = {
.id = -1,
.name = "jz2440_button", /* 用于匹配的名字 */
.dev = {
.release = device_release,
.platform_data = &jz2440_key, /* 是 void * 类型 */
}
};
static int __init jz2440_button_dev_init(void)
{
printk("dev init\n");
platform_device_register(&jz2440_key_dev);
return 0;
}
static void __exit jz2440_button_dev_exit(void)
{
printk("dev exit\n");
platform_device_unregister(&jz2440_key_dev);
}
module_init(jz2440_button_dev_init);
module_exit(jz2440_button_dev_exit);
MODULE_LICENSE("Dual BSD/GPL");
说明:
【1】要想将自己定义的结构体数组传递给别的模块,需要首地址和数组的元素个数,所以又定义了一个 jz2440_key 的结构体
Makefile
ifeq ($(KERNELRELEASE),)
#KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELDIR ?= ~/wor_lip/linux-3.4.112
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules* Module*
.PHONY: modules modules_install clean
else
obj-m := key_dev.o
endif
key_dri.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/wait.h>
#include <linux/device.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/platform_device.h>
#define DEVICE_NAME "jz2440_button_drv"
static unsigned char key_vals[4]; /* 保存要传递给用户空间的4个按键值 */
static struct timer_list key_timers[4];
#define KEYDOWN_DELAY (HZ/100) /* 按键按下时的去抖延时是10ms */
#define KEYUP_DELAY (HZ/200) /* 按键抬起时的去抖延时是5ms */
struct jz2440_key_t
{
const char *name;
unsigned int pin;
unsigned char key_val;
unsigned int irq; /*按键的中断号 */
unsigned int setting; /* 按键对应的功能的掩码 */
unsigned int id;
};
struct jz2440_key_platform
{
struct jz2440_key_t *buttonp;
int button_num;
};
struct jz2440_key_t *key_tablep;
struct jz2440_key_platform *key_platformp;
static struct class *button_drv_class;
int major;
unsigned int key_pressed;
unsigned int key_pressed_middle_flag;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
int jz2440_key_dri_probe(struct platform_device *dev)
{
key_platformp = (struct jz2440_key_platform *)dev->dev.platform_data;
key_tablep = key_platformp->buttonp;
printk("platform: match ok!\n");
return 0;
}
int jz2440_key_dri_remove(struct platform_device *dev)
{
printk("platform: driver remove\n");
return 0;
}
struct platform_driver jz2440_key_dri = {
.probe = jz2440_key_dri_probe,
.remove = jz2440_key_dri_remove,
.driver = {
.name = "jz2440_button"
},
};
irqreturn_t irq_handler(int irq, void *dev_id)
{
int key_num = *((int *)dev_id);
unsigned int pinval = s3c2410_gpio_getpin(key_tablep[key_num].pin);
if (!pinval)
{
key_pressed_middle_flag = 1;
mod_timer(&key_timers[key_num], jiffies + KEYDOWN_DELAY);
}else
{
key_pressed_middle_flag = 0;
mod_timer(&key_timers[key_num], jiffies + KEYUP_DELAY);
}
return IRQ_RETVAL(IRQ_HANDLED);
}
void fun_timer(unsigned long arg)
{
int pinval = s3c2410_gpio_getpin(key_tablep[arg].pin);
if (!pinval)
{
if (key_pressed_middle_flag == 1)
{
/*printk("pree down\n");*/
key_pressed = 1;
key_tablep[arg].key_val = 1;
wake_up_interruptible(&button_waitq); /* 唤醒如果读而引起的阻塞 */
}
}else
{
if (key_pressed_middle_flag == 0)
{
/*printk("pree up\n");*/
key_pressed = 0;
key_tablep[arg].key_val = 0;
}
}
}
static int jz2440_button_drv_open(struct inode *inode, struct file *filp)
{
int i;
for (i = 0; i < key_platformp->button_num; i++)
{
s3c2410_gpio_cfgpin(key_tablep[i].pin, key_tablep[i].setting); /* 将芯片引脚设置成中断 */
irq_set_irq_type(key_tablep[i].irq, IRQ_TYPE_EDGE_BOTH); /* 下降沿触发 */
request_irq(key_tablep[i].irq, irq_handler, IRQF_DISABLED, key_tablep[i].name, &key_tablep[i].id); /* 向系统内核申请快速中断 */
setup_timer(&key_timers[i], fun_timer, i);
}
printk("button_drv_open\n");
return 0;
}
static int jz2440_button_drv_close(struct inode *inode, struct file *filp)
{
int i;
for (i = 0; i < key_platformp->button_num; i++)
{
del_timer(&key_timers[i]);
disable_irq(key_tablep[i].irq);
free_irq(key_tablep[i].irq, &key_tablep[i].id);
}
return 0;
}
/* 读的时候按键的数量必须是 4 ,返回成功读取的个数 */
ssize_t jz2440_button_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
int j;
unsigned long ret;
if (size != sizeof(key_vals))
return -EINVAL;
/* 如果是非阻塞的读 */
if (file->f_flags & O_NONBLOCK)
{
if (!key_pressed) /* 非阻塞的读,按键没有按下过 */
return -EAGAIN;
}
else if (!key_pressed) /* 阻塞的读,按键没有按下过 */
{
wait_event_interruptible(button_waitq, key_pressed);
}
key_pressed = 0;
for (j = 0; j < key_platformp->button_num; j++)
{
key_vals[j] = key_tablep[j].key_val;
}
ret = copy_to_user(buf, key_vals, size); /* 返回key_vals数组数据到用户空间 */
return ret ? -EFAULT : min(sizeof(key_vals), size);
}
unsigned int jz2440_button_drv_poll (struct file *filp, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(filp, &button_waitq, wait);/* 加读等待队列头 */
if (key_pressed)
mask |= POLLIN | POLLRDNORM; /* 标示数据可获得 */
return mask;
}
static struct file_operations button_drv_fops =
{
.owner = THIS_MODULE,
.open = jz2440_button_drv_open,
.release = jz2440_button_drv_close,
.read = jz2440_button_drv_read,
.poll = jz2440_button_drv_poll,
};
int button_drv_init(void)
{
platform_driver_register(&jz2440_key_dri);
/* auto choice major */
major = register_chrdev(0, DEVICE_NAME, &button_drv_fops);
if (major < 0)
{
printk("jz2440 button driver can't register major number!\n");
return major;
}
button_drv_class = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(button_drv_class))
{
printk("error, fail to init button_drv_class");
return -1;
}
device_create(button_drv_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
printk("BUTTON_DRV initialized! \n");
return 0;
}
void button_drv_exit(void)
{
platform_driver_unregister(&jz2440_key_dri);
device_destroy(button_drv_class, MKDEV(major, 0)); //删掉设备节点
class_destroy(button_drv_class); //注销类
unregister_chrdev(major, DEVICE_NAME); //卸载驱动
printk("exit is finished.\n");
}
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
说明:
【1】在 probe 函数中想要用到匹配好的设备文件中的元素,直接使用 platform_device 的指针
【2】在调试过程中在申请了中断后,可以用 cat /proc/interrupt 查看申请的中断,显示如下
CPU0
16: 2 s3c-ext0 Button S2
18: 2 s3c-ext0 Button S3
24: 0 s3c s3c2410-rtc tick
30: 24994 s3c S3C2410 Timer Tick
32: 0 s3c s3c2410-lcd
37: 0 s3c s3c-mci
42: 0 s3c ohci_hcd:usb1
43: 8 s3c s3c2440-i2c
46: 0 s3c s3c2410-rtc alarm
51: 2805 s3c-ext eth0
55: 2 s3c-ext Button S4
60: 1 s3c-ext s3c-mci
63: 2 s3c-ext Button S5
第一列是中断号,第二列是中断发生的次数,第三列是,第四列是中断的名字
【3】这里有个现象,也不知道是不是问题,当 free_irq 之后,发现中断并没有在列表中消失,只是名字消失了。
【4】这个函数的 init 和 exit 中对字符设备的注册和释放以及设备节点的创建还是有点麻烦,可以查看 混杂设备驱动 进行 混杂设备驱动改写,可以减少不少代码。
Makefile 中改一下目标文件即可
test.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
int fd;
fd_set rds;
void stop(int signo)
{
close(fd);
FD_CLR(fd, &rds);
_exit(0);
}
int main(int argc, const char *argv[])
{
unsigned char data_buf[4];
signal(SIGINT, stop); /* 按下ctrl+c,调用stop函数 */
/* 阻塞的读 */
if ((fd = open("/dev/jz2440_button_drv", O_RDONLY)) < 0)
{
perror("fail to open");
return -1;
}
while (1)
{
int ret, i;
FD_ZERO(&rds);
FD_SET(fd, &rds); /* 将fd文件描述符添加到rds这个文件描述符集中去 */
ret = select(fd + 1, &rds, NULL, NULL, NULL);
printf("doing\n");
if (ret < 0)
{
printf("read jz2440 button fail!\n");
continue;
}else if (ret == 0)
{
printf("read jz2440 button time out!\n");
continue;
}
if (FD_ISSET(fd, &rds))
{
ret = read(fd, data_buf, sizeof(data_buf));
if (ret < 4)
{
printf("read not complete.\n");
break;
}
for (i = 0 ; i < 4; i++)
{
printf("S%d:%d ", 2 + i, data_buf[i]);
}
printf("\n");
}
}
return 0;
}
这个文件的编译命令:
arm-none-linux-gnueabi-gcc test.c -o button -march=armv4t
实验结果:
[root@lip ~]# insmod key_dev.ko
dev init
[root@lip ~]# insmod key_dri.ko
platform: match ok!
BUTTON_DRV initialized!
[root@lip ~]# ./button
button_drv_open
doing
S2:1 S3:0 S4:0 S5:0
doing
S2:0 S3:1 S4:0 S5:0
doing
S2:0 S3:0 S4:1 S5:0
doing
S2:0 S3:0 S4:0 S5:1