JZ2440笔记:字符设备驱动程序之异步通知(快递员直接给我打电话啊,要什么门卫通知)

(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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值