Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80239
  • 博文数量: 15
  • 博客积分: 346
  • 博客等级: 一等列兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-14 17:20
文章分类
文章存档

2012年(15)

分类: LINUX

2012-05-28 22:01:35

---key.c

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/fs.h>
  4. #include <linux/errno.h>
  5. #include <linux/sched.h>
  6. #include <linux/init.h>
  7. #include <linux/cdev.h>
  8. #include <linux/device.h>
  9. #include <linux/gpio.h>
  10. #include <linux/mm.h>
  11. #include <linux/irq.h>
  12. #include <linux/interrupt.h>
  13. #include <asm/io.h>
  14. #include <asm/module.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/irq.h>
  17. #include <mach/regs-gpio.h>
  18. #include <mach/hardware.h>
  19. #include <linux/input.h>

  20. struct input_dev *devp;

  21. struct key_irq_desc
  22. {
  23.     int irq;
  24.     int pin;
  25.     int pin_value;
  26.     int no;
  27.     char *name;
  28. };

  29. static struct key_irq_desc key_irq_table[] =
  30. {
  31.     {IRQ_EINT8 , S3C2410_GPG(0) , S3C2410_GPG0_EINT8 , 0, "KEY0"},
  32.     {IRQ_EINT11, S3C2410_GPG(3) , S3C2410_GPG3_EINT11 , 1, "KEY1"},
  33.     {IRQ_EINT13, S3C2410_GPG(5) , S3C2410_GPG5_EINT13 , 2, "KEY2"},
  34.     {IRQ_EINT14, S3C2410_GPG(6) , S3C2410_GPG6_EINT14 , 3, "KEY3"},
  35.     {IRQ_EINT15, S3C2410_GPG(7) , S3C2410_GPG7_EINT15 , 4, "KEY4"},
  36.     {IRQ_EINT19, S3C2410_GPG(11), S3C2410_GPG11_EINT19, 5, "KEY5"},
  37. };

  38. static irqreturn_t s3c2440_key_interrupt(int irq, void *dev_id)
  39. {
  40.     int i = *(int *)dev_id;
  41.     if (!s3c2410_gpio_getpin(key_irq_table[i].pin))
  42.     {
  43.         switch(i)
  44.         {
  45.             case 0:
  46.                 input_report_key(devp, KEY_0, 1);
  47.                 break;
  48.             case 1:
  49.                 input_report_key(devp, KEY_1, 1);
  50.                 break;
  51.             case 2:
  52.                 input_report_key(devp, KEY_2, 1);
  53.                 break;
  54.             case 3:
  55.                 input_report_key(devp, KEY_3, 1);
  56.                 break;
  57.             case 4:
  58.                 input_report_key(devp, KEY_4, 1);
  59.                 break;
  60.             case 5:
  61.                 input_report_key(devp, KEY_5, 1);
  62.                 break;
  63.         }
  64.         input_sync(devp);
  65.     }
  66.     return IRQ_RETVAL(IRQ_HANDLED);
  67. }
  68. static int s3c2440_key_setup_irq(void)
  69. {
  70.     int i;
  71.     for (i = 0; i < sizeof(key_irq_table)/sizeof(key_irq_table[0]); ++i)
  72.     {
  73.         if (key_irq_table[i].irq < 0)
  74.             continue;
  75.         if (request_irq(key_irq_table[i].irq, s3c2440_key_interrupt, IRQ_TYPE_EDGE_BOTH, key_irq_table[i].name, (void *)&key_irq_table[i].no))
  76.         {
  77.             printk(KERN_ERR "allocate irq fail%d\n", i);
  78.             return -EBUSY;
  79.         }
  80.     }
  81.     return 0;
  82. }

  83. static void s3c2440_key_free_irq(void)
  84. {
  85.     int i;
  86.     for (i = 0; i < sizeof(key_irq_table)/sizeof(key_irq_table[0]); ++i)
  87.         free_irq(key_irq_table[i].irq, &key_irq_table[i].no);    
  88. }

  89. static int __init s3c2440_key_init(void)
  90. {
  91.     int i, err;

  92.     for (i = 0; i < sizeof(key_irq_table)/sizeof(key_irq_table[0]); ++i)
  93.         s3c2410_gpio_setpin(key_irq_table[i].pin, key_irq_table[i].pin_value);
  94.     if (s3c2440_key_setup_irq())
  95.         return -EBUSY;
  96.     devp = input_allocate_device();
  97.     if (!devp)
  98.     {
  99.         printk(KERN_ERR "allocate input_dev fail!\n");
  100.         err = -ENOMEM;
  101.         goto fail_free_irq;
  102.     }
  103.     devp->name = "s3c2440_key_input";
  104.     
  105.     //set_bit(EV_KEY, devp->evbit);
  106.     devp->evbit[0] = BIT_MASK(EV_SYN) | BIT_MASK(EV_KEY);
  107.     set_bit(KEY_0, devp->keybit);
  108.     set_bit(KEY_1, devp->keybit);
  109.     set_bit(KEY_2, devp->keybit);
  110.     set_bit(KEY_3, devp->keybit);
  111.     set_bit(KEY_4, devp->keybit);
  112.     set_bit(KEY_5, devp->keybit);
  113.     
  114.     if ((err = input_register_device(devp)))
  115.     {
  116.         printk(KERN_ERR "register input_dev fail\n");
  117.         goto fail_free_dev;
  118.     }
  119.     return 0;

  120. fail_free_dev:
  121.     input_free_device(devp);
  122. fail_free_irq:
  123.     s3c2440_key_free_irq();
  124.     return err;
  125. }


  126. static void __exit s3c2440_key_exit(void)
  127. {
  128.     input_unregister_device(devp);
  129.     input_free_device(devp);
  130.     s3c2440_key_free_irq();
  131. }


  132. MODULE_LICENSE("GPL");
  133. MODULE_AUTHOR("Domod");
  134. module_init(s3c2440_key_init);
  135. module_exit(s3c2440_key_exit);
--app-key.c

点击(此处)折叠或打开

  1. #include "keydev.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/ioctl.h>
  6. #include <linux/input.h>


  7. int main(int argc, char **argv)
  8. {
  9.     int count, i;
  10.     int fd;
  11.     struct input_event ev_key;
  12.     fd = open("/dev/event1", 0);
  13.     if (fd < 0)
  14.     {
  15.         perror("open device key!");
  16.         exit(1);
  17.     }
  18.     while (1)
  19.     {
  20.         count = read(fd, &ev_key, sizeof(struct input_event));
  21.         if (EV_KEY == ev_key.type)
  22.             printf("type:%d,code:%d,value:%d\n", ev_key.type, ev_key.code, ev_key.value);
  23.         if (EV_SYN == ev_key.type)
  24.             printf("syn event\n");
  25.     }
  26.     close(fd);
  27.     return 0;
  28. }
--Makefile

点击(此处)折叠或打开

  1. ifneq ($(KERNELRELEASE),)

  2. obj-m := key.o

  3. else
  4.     
  5. KDIR := /home/domod/arm/kernel/src/linux-2.6.32.2
  6. all:
  7.     make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
  8. app:
  9.     arm-linux-gcc app-key.c -o key
  10. clean:
  11.     rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*


  12. endif



阅读(2741) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~