zoukankan      html  css  js  c++  java
  • 5、JZ2440按键驱动(异步通知)

    驱动代码

      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_dev4"  /* 加载模式后,执行”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 static struct fasync_struct *key_async;
     32 
     33 struct pin_desc{
     34     unsigned int pin;
     35     unsigned int key_val;
     36 };
     37 /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
     38 /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
     39 static unsigned char key_val;
     40 
     41 struct pin_desc pins_desc[4] = {
     42     {S3C2410_GPF0, 0x01},
     43     {S3C2410_GPF2, 0x02},
     44     {S3C2410_GPG3, 0x03},
     45     {S3C2410_GPG11, 0x04},
     46 };
     47 
     48 /*
     49   * 确定按键值
     50   */
     51 static irqreturn_t keys_irq(int irq, void *dev_id)
     52 {
     53     struct pin_desc * pindesc = (struct pin_desc *)dev_id;
     54     unsigned int pinval;
     55     
     56     pinval = s3c2410_gpio_getpin(pindesc->pin);
     57 
     58     if (pinval)
     59     {
     60         /* 松开 */
     61         key_val = 0x80 | pindesc->key_val;
     62     }
     63     else
     64     {
     65         /* 按下 */
     66         key_val = pindesc->key_val;
     67     }
     68 
     69     ev_press = 1;                  /* 表示中断发生了 */
     70     wake_up_interruptible(&key_waitq);   /* 唤醒休眠的进程 */
     71     
     72     kill_fasync (&key_async, SIGIO, POLL_IN);
     73 
     74     return IRQ_RETVAL(IRQ_HANDLED);
     75 }
     76 
     77 static int key_dev4_open(struct inode *inode, struct file *file)
     78 {
     79     /* 配置GPF0,2为输入引脚 */
     80     /* 配置GPG3,11为输入引脚 */
     81     request_irq(IRQ_EINT0,  keys_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
     82     request_irq(IRQ_EINT2,  keys_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
     83     request_irq(IRQ_EINT11, keys_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
     84     request_irq(IRQ_EINT19, keys_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);    
     85 
     86     return 0;
     87 }
     88 
     89 ssize_t key_dev4_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
     90 {
     91     if (size != 1)
     92         return -EINVAL;
     93 
     94     /* 如果没有按键动作, 休眠 */
     95     wait_event_interruptible(key_waitq, ev_press);
     96 
     97     /* 如果有按键动作, 返回键值 */
     98     copy_to_user(buf, &key_val, 1);
     99     ev_press = 0;
    100     return 1;
    101 }
    102 
    103 int key_dev4_close(struct inode *inode, struct file *file)
    104 {
    105     free_irq(IRQ_EINT0, &pins_desc[0]);
    106     free_irq(IRQ_EINT2, &pins_desc[1]);
    107     free_irq(IRQ_EINT11, &pins_desc[2]);
    108     free_irq(IRQ_EINT19, &pins_desc[3]);
    109     return 0;
    110 }
    111 
    112 static unsigned key_dev4_poll(struct file *file, poll_table *wait)
    113 {
    114     unsigned int mask = 0;
    115     poll_wait(file,&key_waitq,wait);
    116     if (ev_press)
    117         mask |= POLLIN | POLLRDNORM;
    118 
    119     return mask;
    120 }
    121 
    122 static int key_dev4_fasync (int fd, struct file *filp, int on)
    123 {
    124     printk("driver: fifth_drv_fasync
    ");
    125     return fasync_helper (fd, filp, on, &key_async);
    126 }
    127 
    128 static struct file_operations sencod_drv_fops = {
    129     .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    130     .open    =  key_dev4_open,     
    131     .read     =    key_dev4_read,       
    132     .release =  key_dev4_close,     
    133     .poll      =  key_dev4_poll,
    134     .fasync  =  key_dev4_fasync,
    135 };
    136 
    137 int major;
    138 static int key_drv_init(void)
    139 {
    140     major = register_chrdev(0, DEVICE_NAME, &sencod_drv_fops);
    141 
    142     key_class = class_create(THIS_MODULE, DEVICE_NAME);
    143 
    144     key_class_dev = class_device_create(key_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME); /* /dev/keys */
    145 
    146     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    147     gpfdat = gpfcon + 1;
    148 
    149     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
    150     gpgdat = gpgcon + 1;
    151 
    152     return 0;
    153 }
    154 
    155 static void key_drv_exit(void)
    156 {
    157     unregister_chrdev(major, DEVICE_NAME);
    158     class_device_unregister(key_class_dev);
    159     class_destroy(key_class);
    160     iounmap(gpfcon);
    161     iounmap(gpgcon);
    162     return 0;
    163 }
    164 
    165 module_init(key_drv_init);
    166 
    167 module_exit(key_drv_exit);
    168 
    169 MODULE_LICENSE("GPL");
    View Code

    测试代码

     1 #include <sys/types.h>
     2 #include <sys/stat.h>
     3 #include <fcntl.h>
     4 #include <stdio.h>
     5 #include <poll.h>
     6 #include <signal.h>
     7 #include <sys/types.h>
     8 #include <unistd.h>
     9 #include <fcntl.h>
    10 
    11 
    12 /* fifthdrvtest 
    13   */
    14 int fd;
    15 
    16 void my_signal_fun(int signum)
    17 {
    18     unsigned char key_val;
    19     read(fd, &key_val, 1);
    20     printf("key_val: 0x%x
    ", key_val);
    21 }
    22 
    23 int main(int argc, char **argv)
    24 {
    25     unsigned char key_val;
    26     int ret;
    27     int Oflags;
    28 
    29     signal(SIGIO, my_signal_fun);
    30     
    31     fd = open("/dev/key_dev4", O_RDWR);
    32     if (fd < 0)
    33     {
    34         printf("can't open!
    ");
    35     }
    36 
    37     fcntl(fd, F_SETOWN, getpid());
    38     
    39     Oflags = fcntl(fd, F_GETFL); 
    40     
    41     fcntl(fd, F_SETFL, Oflags | FASYNC);
    42 
    43 
    44     while (1)
    45     {
    46         sleep(1000);
    47     }
    48     
    49     return 0;
    50 }
    View Code

    Makefile

    1 KERN_DIR = /work/system/linux-2.6.22.6
    2 
    3 all:
    4     make -C $(KERN_DIR) M=`pwd` modules 
    5 
    6 clean:
    7     make -C $(KERN_DIR) M=`pwd` modules clean
    8     rm -rf modules.order
    9 obj-m    += key_dev4.o
    View Code
  • 相关阅读:
    [微软认证]MCP问题解答
    邮件服务
    QueryString 整站过滤
    今天开始安卓的底层开发吧。
    所谓的回调函数
    UTF8 to unicode
    TCP/IP中的拥塞窗口控制机制
    (转)前端页面,页内锚定位分析
    远程连接Sql Server 2008 R2 命名实例
    字符编码研究
  • 原文地址:https://www.cnblogs.com/yang-cheng/p/13458091.html
Copyright © 2011-2022 走看看