4、JZ2440按键驱动(poll)

驱动程序

4、JZ2440按键驱动(poll)
  1 #include <linux/module.h>
  2 #include <linux/kernel.h>
  3 #include <linux/fs.h>
  4 #include <linux/init.h>
  5 #include <linux/delay.h>
  6 #include <linux/irq.h>
  7 #include <asm/uaccess.h>
  8 #include <asm/irq.h>
  9 #include <asm/io.h>
 10 #include <asm/arch/regs-gpio.h>
 11 #include <asm/hardware.h>
 12 #include <linux/poll.h>
 13 
 14 #define DEVICE_NAME     "key_dev3"  /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
 15 
 16 static struct class *key_class;
 17 static struct class_device    *key_class_dev;
 18 
 19 volatile unsigned long *gpfcon;
 20 volatile unsigned long *gpfdat;
 21 
 22 volatile unsigned long *gpgcon;
 23 volatile unsigned long *gpgdat;
 24 
 25 /* 创建以个等待队列头 */
 26 static DECLARE_WAIT_QUEUE_HEAD(key_waitq);
 27 
 28 /* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
 29 static volatile int ev_press = 0;
 30 
 31 struct pin_desc{
 32     unsigned int pin;
 33     unsigned int key_val;
 34 };
 35 /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
 36 /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
 37 static unsigned char key_val;
 38 
 39 struct pin_desc pins_desc[4] = {
 40     {S3C2410_GPF0, 0x01},
 41     {S3C2410_GPF2, 0x02},
 42     {S3C2410_GPG3, 0x03},
 43     {S3C2410_GPG11, 0x04},
 44 };
 45 
 46 /*
 47   * 确定按键值
 48   */
 49 static irqreturn_t keys_irq(int irq, void *dev_id)
 50 {
 51     struct pin_desc * pindesc = (struct pin_desc *)dev_id;
 52     unsigned int pinval;
 53     
 54     pinval = s3c2410_gpio_getpin(pindesc->pin);
 55 
 56     if (pinval)
 57     {
 58         /* 松开 */
 59         key_val = 0x80 | pindesc->key_val;
 60     }
 61     else
 62     {
 63         /* 按下 */
 64         key_val = pindesc->key_val;
 65     }
 66 
 67     ev_press = 1;                  /* 表示中断发生了 */
 68     wake_up_interruptible(&key_waitq);   /* 唤醒休眠的进程 */
 69     return IRQ_RETVAL(IRQ_HANDLED);
 70 }
 71 
 72 static int key_dev3_open(struct inode *inode, struct file *file)
 73 {
 74     /* 配置GPF0,2为输入引脚 */
 75     /* 配置GPG3,11为输入引脚 */
 76     request_irq(IRQ_EINT0,  keys_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
 77     request_irq(IRQ_EINT2,  keys_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
 78     request_irq(IRQ_EINT11, keys_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
 79     request_irq(IRQ_EINT19, keys_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);    
 80 
 81     return 0;
 82 }
 83 
 84 ssize_t key_dev3_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
 85 {
 86     if (size != 1)
 87         return -EINVAL;
 88 
 89     /* 如果没有按键动作, 休眠 */
 90     wait_event_interruptible(key_waitq, ev_press);
 91 
 92     /* 如果有按键动作, 返回键值 */
 93     copy_to_user(buf, &key_val, 1);
 94     ev_press = 0;
 95     return 1;
 96 }
 97 
 98 int key_dev3_close(struct inode *inode, struct file *file)
 99 {
100     free_irq(IRQ_EINT0, &pins_desc[0]);
101     free_irq(IRQ_EINT2, &pins_desc[1]);
102     free_irq(IRQ_EINT11, &pins_desc[2]);
103     free_irq(IRQ_EINT19, &pins_desc[3]);
104     return 0;
105 }
106 
107 static unsigned key_dev3_poll(struct file *file, poll_table *wait)
108 {
109     unsigned int mask = 0;
110     poll_wait(file,&key_waitq,wait);
111     if (ev_press)
112         mask |= POLLIN | POLLRDNORM;
113 
114     return mask;
115 }
116 
117 static struct file_operations sencod_drv_fops = {
118     .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
119     .open    =  key_dev3_open,     
120     .read     =    key_dev3_read,       
121     .release =  key_dev3_close,     
122     .poll      =  key_dev3_poll,
123     
124 };
125 
126 int major;
127 static int key_drv_init(void)
128 {
129     major = register_chrdev(0, DEVICE_NAME, &sencod_drv_fops);
130 
131     key_class = class_create(THIS_MODULE, DEVICE_NAME);
132 
133     key_class_dev = class_device_create(key_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME); /* /dev/keys */
134 
135     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
136     gpfdat = gpfcon + 1;
137 
138     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
139     gpgdat = gpgcon + 1;
140 
141     return 0;
142 }
143 
144 static void key_drv_exit(void)
145 {
146     unregister_chrdev(major, DEVICE_NAME);
147     class_device_unregister(key_class_dev);
148     class_destroy(key_class);
149     iounmap(gpfcon);
150     iounmap(gpgcon);
151     return 0;
152 }
153 
154 module_init(key_drv_init);
155 
156 module_exit(key_drv_exit);
157 
158 MODULE_LICENSE("GPL");
View Code

测试程序

4、JZ2440按键驱动(poll)
 1 #include <sys/types.h>
 2 #include <sys/stat.h>
 3 #include <fcntl.h>
 4 #include <stdio.h>
 5 #include <poll.h>
 6 
 7 /* thirddrvtest 
 8   */
 9 int main(int argc, char **argv)
10 {
11     int fd;
12     unsigned char key_val;
13     int ret;
14 
15     struct pollfd fds[1];
16     
17     fd = open("/dev/key_dev3", O_RDWR);
18     if (fd < 0)
19     {
20         printf("can't open!\n");
21     }
22 
23     fds[0].fd     = fd;
24     fds[0].events = POLLIN;
25     while (1)
26     {
27         ret = poll(fds, 1, 5000);
28         if (ret == 0)
29         {
30             printf("time out\n");
31         }
32         else
33         {
34             read(fd, &key_val, 1);
35             printf("key_val = 0x%x\n", key_val);
36         }
37     }
38     
39     return 0;
40 }
View Code

 

上一篇:Select Poll Epoll


下一篇:linux poll机制使用(一)