字符设备实验之按键poll

该驱动在按键中断基础上修改,主要是添加了poll函数,程序调用驱动时可以实现超时退出的功能。

关于poll可以初略参考一下《Linux高级字符设备之Poll操作 - LoveFM - 博客园 》

http://www.cnblogs.com/geneil/archive/2011/12/04/2275559.html


#include <linux/module.h>

#include <linux/kernel.h>


#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/gpio_keys.h>
#include <linux/workqueue.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/poll.h>




#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>


#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-mem.h>
#include <mach/gpio.h>
#include <mach/gpio-smdkc110.h>
#include <mach/regs-gpio.h>


/********************************************************************************************
ioremap(),iounmap()
class_create(),class_destroy()
class_device_create(),class_device_unregister()
********************************************************************************************/
static dev_t devno;  
static struct cdev *pCdev;
static struct class *forthdrv_class;
static struct device *forthdrv_class_dev;




#define DEVICE_NAME "forth_drv"
int major,minor;


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


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


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] = {
{S5PV210_GPH0(0), 0x01},
{S5PV210_GPH0(1), 0x02},
{S5PV210_GPH0(2), 0x03},
{S5PV210_GPH0(3), 0x04},
};


/*
  * 确定按键值
  */
static irqreturn_t forth_drv_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;

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);   /* 唤醒休眠的进程, 在poll中用于唤醒等待的进程 */ 



return IRQ_RETVAL(IRQ_HANDLED);
}


static int forth_drv_open(struct inode *inode, struct file *file)
{
//printk("do forth_drv_open \n");

/*
* K1,K2,K3,K4对应GPH0,GPH1,GPH2,GPH3
*/


/* 配置K1,K2,K3,K4为输入引脚 */
request_irq(IRQ_EINT(0),  forth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S2", &pins_desc[0]);
request_irq(IRQ_EINT(1),  forth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S3", &pins_desc[1]);
request_irq(IRQ_EINT(2),  forth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S4", &pins_desc[2]);
request_irq(IRQ_EINT(3),  forth_drv_irq, IRQ_TYPE_EDGE_BOTH, "S5", &pins_desc[3]);

return 0;
}


static int forth_drv_release(struct inode *inode, struct file *file)
{
//printk("do forth_drv_release \n");
free_irq(IRQ_EINT(0), &pins_desc[0]);
free_irq(IRQ_EINT(1), &pins_desc[1]);
free_irq(IRQ_EINT(2), &pins_desc[2]);
free_irq(IRQ_EINT(3), &pins_desc[3]);
return 0;
}


static int forth_drv_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
if (count != 1)
return -EINVAL;


/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);


/* 如果有按键动作, 返回键值 */
copy_to_user(buff, &key_val, 1);
ev_press = 0;


return 1;
}


static ssize_t forth_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
//printk("do forth_drv_write \n");
return 0;
}


static unsigned forth_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 此处将当前进程加入到等待队列中,但并不阻塞


if (ev_press)
mask |= POLLIN | POLLRDNORM; //表示设备可读


return mask;
}


static struct file_operations forth_drv_fops =
{
.owner   = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module 变量*/
.open    = forth_drv_open,
.release = forth_drv_release,
.read    = forth_drv_read,
.write   = forth_drv_write,
.poll    = forth_drv_poll,
};


static int __init forth_drv_init(void)
{
//major = register_chrdev(0, DEVICE_NAME, &forth_drv_fops);
int ret;
  
//使用udev自动生成设备文件,并分配设备号
pCdev = cdev_alloc();
if (!pCdev) {
printk(KERN_WARNING "cdev_alloc failed\n");
//goto out;
}
    pCdev->owner = THIS_MODULE;  
cdev_init(pCdev, &forth_drv_fops);

ret = alloc_chrdev_region(&devno, 5, 1, "buttons");  
    if(ret){  
        printk(KERN_ERR "alloc char device region faild!\n");  
        //return ret;  
    }  

major = MAJOR(devno);
minor = MINOR(devno);

    ret = cdev_add(pCdev, devno, 1);  //cat /proc/devices可以查看到多了一个buttons设备
    if(ret){  
        printk(KERN_ERR "add char device faild!\n");  
        //goto add_error;  
    }

#if 1
forthdrv_class = class_create(THIS_MODULE, "buttonsClass");//ls /sys/class/buttonsClass/可以查看到多了一个buttonsClass类
if(IS_ERR(forthdrv_class)){  
        printk(KERN_ERR "create class error!\n");         
    } 

//ls /sys/class/buttonsClass/buttons5/可以查看到多了一个buttons5设备
forthdrv_class_dev = device_create(forthdrv_class, NULL, devno/*MKDEV(major, 0)*/, NULL, "buttons%d", MINOR(devno)); /* /dev/buttons */
if(IS_ERR(forthdrv_class_dev)){  
        printk(KERN_ERR "create buttons device error!\n");          
    } 
#endif

printk("forth_drv_init \n");
printk("major = %d,minor = %d \n", major,minor);


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

return 0;
}


static void __exit forth_drv_exit(void)
{
//unregister_chrdev(major, DEVICE_NAME);
#if 1
device_destroy(forthdrv_class,devno);
class_destroy(forthdrv_class);
#endif


cdev_del(pCdev);
unregister_chrdev_region(devno, 1);  


iounmap(gpgcon);

printk("forth_drv_exit \n");
}




module_init(forth_drv_init);
module_exit(forth_drv_exit);




MODULE_LICENSE("GPL");


测试程序为:



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


/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
int fd;
int cnt = 0;
unsigned char key_vals;
int ret;

struct pollfd fds[1];

fd = open("/dev/buttons5", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return 0;
}

fds[0].fd     = fd;
fds[0].events = POLLIN;
while (1)
{
ret = poll(fds, 1, 5000);
if (ret == 0)
{
printf("time out\n");    // 如果5秒内没有按键按下,终端会打印出time out
}
else
{
read(fd, &key_vals, 1);
printf("key_vals = 0x%x\n", key_vals);
}
}
return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值