远程视频监控之驱动篇(按键)

       转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/38515211


       这里我仅仅贴出了代码和应用,没有进行其它的解说。由于之前我写过的按键驱动的恩恩怨怨,解说的很清楚,这个驱动就是依据之前写的改写而成。连接:http://blog.csdn.net/ruoyunliufeng/article/details/23946487

一.代码

#include <linux/module.h>
#include <linux/sched.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/device.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h>

static struct class *wvm_buttons_class;
static  int major;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;

volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

static struct timer_list buttons_timer;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */
static volatile int ev_press = 0;

static struct fasync_struct *button_async;


struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};


/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;

struct pin_desc pins_desc[4] = {
	{S3C2410_GPF(0), 0x01},
	{S3C2410_GPF(3), 0x02},
	{S3C2410_GPG(5), 0x03},
	{S3C2410_GPG(6), 0x04},
};

static struct pin_desc *irq_pd;

struct semaphore button_lock;


/*
  * 确定按键值
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
	/* 20ms后启动定时器 */
	irq_pd = (struct pin_desc *)dev_id;
	mod_timer(&buttons_timer, jiffies+HZ/50);
	return IRQ_RETVAL(IRQ_HANDLED);
}

static int wvm_buttons_drv_open(struct inode *inode, struct file *file)
{	
	
        static int IRQ_EINT8_r,IRQ_EINT11_r,IRQ_EINT13_r,IRQ_EINT14_r;
	if (file->f_flags & O_NONBLOCK)
	{
	       if (down_trylock(&button_lock))
			return -EBUSY;
	}
	else
	{
		/* 获取信号量 */
		down(&button_lock);
	}

	/* 配置GPF0,2为输入引脚 */
	/* 配置GPG3,11为输入引脚 */
	IRQ_EINT8_r=request_irq(IRQ_EINT8,  buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &pins_desc[0]);
	if (IRQ_EINT8_r)
	return -EBUSY;
	
	IRQ_EINT11_r=request_irq(IRQ_EINT11,  buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &pins_desc[1]);
	if (IRQ_EINT11_r)
	return -EBUSY;
	IRQ_EINT13_r=request_irq(IRQ_EINT13, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S4", &pins_desc[2]);
	if (IRQ_EINT13_r)
	return -EBUSY;
	IRQ_EINT14_r=request_irq(IRQ_EINT14, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S5", &pins_desc[3]);
	if (IRQ_EINT14_r)
	return -EBUSY;	

	return 0;
}

ssize_t wvm_buttons_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) //应用中没用到这样的方式
{
	int val;
	if (size != 1)
		return -EINVAL;

	if (file->f_flags & O_NONBLOCK)
	{
		if (!ev_press)
			return -EAGAIN;
	}
	else
	{
		/* 假设没有按键动作, 休眠 */
		wait_event_interruptible(button_waitq, ev_press);
	}

	/* 假设有按键动作, 返回键值 */
	val=copy_to_user(buf, &key_val, 1);
	
	if(val)
	return -EAGAIN;
	
	ev_press = 0;
	
	return 1;
}

static int wvm_buttons_drv_fasync (int fd, struct file *filp, int on)
{
	printk("driver: wvm_buttons_fasync
");
	return fasync_helper (fd, filp, on, &button_async);
}

int wvm_buttons_drv_close(struct inode *inode, struct file *file)
{
	//atomic_inc(&canopen);
	free_irq(IRQ_EINT8,  &pins_desc[0]);
	free_irq(IRQ_EINT11, &pins_desc[1]);
	free_irq(IRQ_EINT13, &pins_desc[2]);
	free_irq(IRQ_EINT14, &pins_desc[3]);
	up(&button_lock);
	return 0;
}

static struct file_operations wvm_buttons_drv_fops = {
        .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自己主动创建的__this_module变量 */
        .open    =  wvm_buttons_drv_open,     
	.read	 =  wvm_buttons_drv_read,	   
	.release =  wvm_buttons_drv_close,
	.fasync	 =  wvm_buttons_drv_fasync,
};

static void buttons_timer_function(unsigned long data)
{
	struct pin_desc * pindesc = irq_pd;
	unsigned int pinval;

	if (!pindesc)
		return;
	
	pinval = s3c2410_gpio_getpin(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_async, SIGIO, POLL_IN);
}


static int wvm_buttons_drv_init(void) //入口
{
	init_timer(&buttons_timer);
	buttons_timer.function = buttons_timer_function;
	add_timer(&buttons_timer); 

	major = register_chrdev(0, "wvm_buttons", &wvm_buttons_drv_fops);
	if(major < 0)
        {
          printk(  " wvm_buttons register falid!/n");
          return major;
        }

	wvm_buttons_class = class_create(THIS_MODULE, "wvm_buttons");
	if(IS_ERR(wvm_buttons_class))
        {
          printk( " wvm_buttons register class falid!/n");
          return -1;
        }

	/* 为了让mdev依据这些信息来创建设备节点 */
	device_create(wvm_buttons_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;

	gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
	gpgdat = gpgcon + 1;

	sema_init(&button_lock, 1);

	return 0;
}

static void wvm_buttons_drv_exit(void)  //出口
{
	unregister_chrdev(major, "wvm_buttons");
	device_destroy(wvm_buttons_class, MKDEV(major, 0));
	class_destroy(wvm_buttons_class);
	iounmap(gpfcon);
	iounmap(gpgcon);
}

module_init(wvm_buttons_drv_init);

module_exit(wvm_buttons_drv_exit);

MODULE_LICENSE("GPL");



二.应用測试

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

/* 
wvm_buttonstest 
  */
int fd;

void my_signal_fun(int signum)
{
	unsigned char key_val;
	read(fd, &key_val, 1);
	printf("key_val: 0x%x
", key_val);
}

int main(int argc, char **argv)
{
	unsigned char key_val;
	int ret;
	int Oflags;

	signal(SIGIO, my_signal_fun);
	
	fd = open("/dev/buttons", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!
");
	}

	fcntl(fd, F_SETOWN, getpid());
	
	Oflags = fcntl(fd, F_GETFL); 
	
	fcntl(fd, F_SETFL, Oflags | FASYNC);


	while (1)
	{
		sleep(1000);
	}
	
	return 0;
}



原文地址:https://www.cnblogs.com/hrhguanli/p/4005898.html