button_drv.c驱动文件:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h> 
#include <asm/uaccess.h> 
#include <linux/device.h> 
#include <asm/arch/regs-gpio.h> 
#include <linux/irq.h> 
#include <asm-arm/irq.h> 
#include <linux/interrupt.h> 
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>

#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"

int major;

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

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

struct class *buttondrv_class; 
struct class_device *buttondrv_class_device; 


unsigned int ev_press;
DECLARE_WAIT_QUEUE_HEAD(button_waitq); 

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

unsigned int key_val;
struct pin_desc pins_desc[4] = {
  {S3C2410_GPF0, 0x01},
  {S3C2410_GPF2, 0x02},
  {S3C2410_GPG3, 0x03},
  {S3C2410_GPG11, 0x04},
};

irqreturn_t buttons_irq(int irq, void *dev_id)
{
  unsigned int pin_val;
  struct pin_desc *pin_desc = (struct pin_desc *)dev_id;

  pin_val = s3c2410_gpio_getpin(pin_desc->pin);

  if(pin_val) 
  {
    key_val = 0x80 | pin_desc->key_val;
  }
  else
  {
    key_val = pin_desc->key_val;
  }
  wake_up_interruptible(&button_waitq); 
  ev_press = 1;
  return IRQ_HANDLED;
}

int button_drv_open(struct inode *inode, struct file *file)
{
  int ret;
  ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S1", (void*)&pins_desc[0]); 
  if(ret)
  {
  printk("open failed 1 \n");
  return -1;
  }
  ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S2", (void*)&pins_desc[1]);
  if(ret)
  {
    printk("open failed 2 \n");
    return -1;
  }
  ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S3", (void*)&pins_desc[2]);
  if(ret)
  {
    printk("open fail 3 \n");
    return -1;
  }
  ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S4", (void*)&pins_desc[3]);
  if(ret)
  {
    printk("open fail 4 \n");
    return -1;
  }
  return 0;
}

ssize_t button_drv_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
  int ret;

  ret = copy_to_user(userbuf, &key_val, 1); 
  if(ret)
  {
    printk("copy eror \n");
    return -1;
  }
  ev_press = 0; 
  return 1;
}

int button_drv_close(struct inode *inode, struct file *file)
{
  free_irq(IRQ_EINT0, (void*)&pins_desc[0]);
  free_irq(IRQ_EINT2, (void*)&pins_desc[1]);
  free_irq(IRQ_EINT11, (void*)&pins_desc[2]);
  free_irq(IRQ_EINT19, (void*)&pins_desc[3]);
  return 0;
}

unsigned int button_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;
}

struct file_operations button_drv_fops = {
  .owner = THIS_MODULE,
  .open = button_drv_open,
  .read = button_drv_read,
  .release = button_drv_close, 
  .poll = button_drv_poll,
};

int __init button_drv_init(void)
{
  major = register_chrdev(0, DRIVER_NAME, &button_drv_fops); 
  if(major<0)
  {
    printk("fail 1 button_drv \n");
  }

  buttondrv_class = class_create(THIS_MODULE, DEVICE_NAME); 
  if(buttondrv_class<0)
  {
    printk("fail 2 button_dev \n");
  }
  buttondrv_class_device = class_device_create(buttondrv_class, NULL, MKDEV(major,0), NULL, DEVICE_NAME); 
  if(buttondrv_class_device<0)
  {
    printk("fail 3 button_dev \n");
  }

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

  printk("register button_drv \n");

  return 0;
}

void __exit button_drv_exit(void)
{
  unregister_chrdev(major, DEVICE_NAME); 
  class_device_unregister(buttondrv_class_device); 
  class_destroy(buttondrv_class); 

  iounmap(gpfcon); 
  iounmap(gpgcon);

  printk("unregister button_drv \n");
}

module_init(button_drv_init);
module_exit(button_drv_exit);

MODULE_LICENSE("GPL");

Makefile文件:

obj-m += button_drv.o

KERN_DIR = /work/system/linux-2.6.22.6

all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c

button_app.c文件:

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

int main(int argc, char **argv)
{
  int fd;
  int ret;
  char *filename;
  struct pollfd fds;
  unsigned char keyVal;

  filename = argv[1];

  fd = open(filename, O_RDWR);
  if(fd<0)
  {
    printf("can not open \n");
  }

  fds.fd = fd;
  fds.events = POLLIN;

  while(1)
  {
    ret = poll(&fds, 1, 5000);     //linux系统再调用poll函数时候,如果没有发生需要的事件,那么进程进入休眠。

                        //如果在限定的时间内得到需要的事件,那么成功返回,如果没有则返回超时错误信息。
    if(ret<0)
    {
      printf("time out \n");
    }
    else
    {
      read(fd, &keyVal, 1);
      printf("keyVal: %x \n",keyVal);
    }

  }
  close(fd);

  return 0;
}

编译生成button_drv.kobutton_app文件,运行./button_app /dev/button_dev