(4)vi third_drv.c增加异步通知代码(快递员直接给我打电话)
static struct fasync_struct *button_fasync;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static unsigned char key_val;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pin_desc[4]={
{S3C2410_GPF(0), 0X01},
{S3C2410_GPF(2), 0X02},
{S3C2410_GPG(3), 0X03},
{S3C2410_GPG(11),0X04},
};
static irqreturn_t buttons_irq(int irq,void *dev_id)
{
unsigned int pinval;
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
pinval = gpio_get_value(pindesc->pin);
if(pinval)
{
key_val = 0x80|pindesc->key_val;
}
else
{
key_val = pindesc->key_val;
}
ev_press = 1;
wake_up_interruptible(&button_waitq);
kill_fasync(&button_fasync,SIGIO,POLL_IN);
return IRQ_HANDLED;
}
static int third_drv_fasync (int fd, struct file *file, int on)
{
return fasync_helper(fd,file,on,&button_fasync);
}
static struct file_operations third_drv_fops = {
.owner = THIS_MODULE,
.open = third_drv_open,
.read = third_drv_read,
.poll = third_drv_fasync,
.release = third_drv_close,
};
vi thirddrvtest.c
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
int fd;
void signal_handler(int sig_num)
{
unsigned char retval;
read(fd,&retval,1);
printf("key_value = 0x%x\n",retval);
}
int main(int argc,char *argv[])
{
unsigned char key_value;
int ret;
int oflags;
signal(SIGIO,signal_handler);
fd = open("/dev/buttons",O_RDWR);
if(fd<0)
{
printf("can't open /dev/buttons!\n");
return 0;
}
fcntl(fd,F_SETOWN,getpid());
oflags = fcntl(fd,F_GETFL);
fcntl(fd,F_SETFL,oflags|FASYNC);//调用.fasync
while(1)
{
sleep(1000);
}
return 0;
}
# insmod third_drv.ko
third_drv_init
# ./thirddrvtest &
# 按键
key_value = 0x81