学习到这里到这里我们觉得这下这个驱动应该就很完善了吧,但是不仅然,我们是不是想当有按键按下的时候,这个时候再去通知用户空间的read函数来读,这样是不是更方便的都,免得函数也老是在哪里休眠。在这里说下:我是不会讲代码的,这些代码比较简单,这只是一些基础的字符设备驱动驱动,到以后我也会讲一些高级点的驱动。这些代码已经讲得很清楚了,全是本人自己写的,是没有错误的,只是自己写的时候注意下自己的内核版本就可以了,以免有一些函数是不能被发现。
上一篇文章的链接:http://blog.csdn.net/qq_21792169/article/details/48416743
驱动程序:
#include <linux/module.h> /*模块有关的*/
#include <linux/kernel.h>/*内核有关的*/
#include <linux/fs.h>/*文件系统有关的*/
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>/*linux中断*/
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h> //copy_to_user
#include <mach/regs-gpio.h>/*寄存器设置S3C2410_GPF0等的定义*/
#include <mach/hardware.h>//s3c2410_gpio_getpin等的定义
#include <mach/irqs.h> //IRQ_EINT0等的定义
#include <asm/system.h>
#include <asm/signal.h>
/*
* 休眠用的队列的一个宏
*/
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
#define DEVICE_NAME "signal"
static struct class *signal_class;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
static unsigned char keyval;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 0x04},
};
static struct fasync_struct *button_async;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
keyval = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
keyval = pindesc->key_val;
}
kill_fasync (&button_async, SIGIO, POLL_IN);
printk("1233\n");
return IRQ_HANDLED;
}
static int fifth_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT0, buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S3", &pins_desc[1]);
request_irq(IRQ_EINT3, buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S4", &pins_desc[2]);
request_irq(IRQ_EINT4, buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S5", &pins_desc[3]);
return 0;
}
ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if(size!=1)
return EINVAL;
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &keyval, 1);
return 0;
}
int fifth_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT3, &pins_desc[2]);
free_irq(IRQ_EINT4, &pins_desc[3]);
return 0;
}
static int fifth_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
return fasync_helper (fd, filp, on, &button_async);
}
static struct file_operations fifth_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = fifth_drv_open,
.read =fifth_drv_read,
.release = fifth_drv_close,
.fasync = fifth_drv_fasync,
};
int major;
static int fifth_drv_init(void)
{
major = register_chrdev(0, DEVICE_NAME, &fifth_drv_fops);
signal_class = class_create(THIS_MODULE, DEVICE_NAME);
device_create(signal_class, NULL, MKDEV(major, 0), NULL, "signal");
return 0;
}
static void fifth_drv_exit(void)
{
unregister_chrdev(major, "signal");
device_destroy(signal_class,MKDEV(major, 0));
class_destroy(signal_class);
}
module_init(fifth_drv_init);
module_exit(fifth_drv_exit);
MODULE_LICENSE("GPL");
测试程序:
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fd;
void my_signal_fun(int signum)
{
unsigned char key_val;
printf("this is test!\n");
read(fd, &key_val, 1);
printf("key_val: 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
unsigned char key_val;
int ret;
int Oflags;
signal(SIGIO, my_signal_fun);//只能通过kill_fasync (&button_async, SIGIO, POLL_IN);调用
fd = open("/dev/signal", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
/*
* 下面三句是一些初始化工作
*/
fcntl(fd, F_SETOWN, getpid());
Oflags = fcntl(fd, F_GETFL); //这样的两句就可以将文件描述符fd与
fcntl(fd, F_SETFL, Oflags | FASYNC);//异步通知关联,到时候将会接到相关通知。
while (1)
{
sleep(1000);
}
return 0;
}
Makefile:
Makefile其他的部分是不变的,只需要修改文件名字就可以了。
obj-m :=signal.o
KERNELDIR ?= /home/work/Linux/linux-2.6.28.7
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -f *o *.mod.o *mod.c *.symvers *.order