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

2012年(15)

分类: LINUX

2012-05-17 18:50:43

--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 <asm/io.h>
  12. #include <asm/module.h>
  13. #include <asm/uaccess.h>
  14. #include <mach/regs-gpio.h>
  15. #include <mach/hardware.h>

  16. #include "keydev.h"
  17. #define KEY_MAJOR 0
  18. #define DEVICE_NAME "keys"
  19. #define KEY_NR_DEVS 1
  20. static int key_major = KEY_MAJOR;

  21. struct cdev *devp;
  22. static struct class *key_class;

  23. static unsigned long key_table[] =
  24. {
  25.     S3C2410_GPG(0),
  26.     S3C2410_GPG(3),
  27.     S3C2410_GPG(5),
  28.     S3C2410_GPG(6),
  29.     S3C2410_GPG(7),
  30.     S3C2410_GPG(11),
  31. };

  32. static unsigned int key_cfg_table[] =
  33. {
  34.     S3C2410_GPIO_INPUT,
  35.     S3C2410_GPIO_INPUT,
  36.     S3C2410_GPIO_INPUT,
  37.     S3C2410_GPIO_INPUT,
  38.     S3C2410_GPIO_INPUT,
  39.     S3C2410_GPIO_INPUT,
  40. };

  41. static int s3c2440_key_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  42. {
  43.     switch (cmd)
  44.     {
  45.         case KEYDEV_POLL:
  46.             break;
  47.         case KEYDEV_IRQ:
  48.             break;
  49.         default:
  50.             return -EINVAL;
  51.     }
  52.     return 0;
  53. }

  54. static int s3c2440_key_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
  55. {
  56.     int i, j, flag = 0;
  57.     static int old = -1, new = -1;
  58.     for (i = 0; i < sizeof(key_table)/sizeof(key_table[0]); ++i)
  59.     {
  60.         if (!s3c2410_gpio_getpin(key_table[i]))
  61.         {
  62.             flag = 1;
  63.             break;
  64.         }
  65.     }
  66.     if (flag)
  67.     {
  68.         new = i;

  69.         for (j = 0; j < 100000; ++j);
  70.         if (!s3c2410_gpio_getpin(key_table[i]) && new != old)
  71.         {
  72.             copy_to_user(buf, &i, 4);
  73.             old = new;
  74.             return 1;
  75.         }
  76.     }
  77.     return 0;
  78. }


  79. static int s3c2440_key_open(struct inode *inode, struct file *filp)
  80. {
  81.     int i;
  82.     for (i = 0; i < sizeof(key_table)/sizeof(key_table[1]); ++i)
  83.         s3c2410_gpio_cfgpin(key_table[i], key_cfg_table[i]);
  84.     return 0;
  85. }

  86. static struct file_operations dev_fops =
  87. {
  88.     .owner = THIS_MODULE,
  89.     .ioctl = s3c2440_key_ioctl,
  90.     .open = s3c2440_key_open,
  91.     .read = s3c2440_key_read,
  92. };

  93. static int __init s3c2440_key_init(void)
  94. {
  95.     int result;
  96.     struct device *devxx;
  97.     dev_t devno = MKDEV(key_major, 0);

  98.     if (key_major)
  99.         result = register_chrdev_region(devno, KEY_NR_DEVS, DEVICE_NAME);
  100.     else
  101.     {
  102.         result = alloc_chrdev_region(&devno, 0, KEY_NR_DEVS, DEVICE_NAME);
  103.         key_major = MAJOR(devno);
  104.         printk("Major-->%d\n", key_major);
  105.     }

  106.     if (result < 0)
  107.         return result;

  108.     devp = kmalloc(sizeof(struct cdev), GFP_KERNEL);
  109.     if (!devp)
  110.     {
  111.         result = -ENOMEM;
  112.         goto fail_malloc;
  113.     }
  114.     memset(devp, 0, sizeof(struct cdev));

  115.     cdev_init(devp, &dev_fops);
  116.     devp->owner = THIS_MODULE;

  117.     cdev_add(devp, devno, KEY_NR_DEVS);
  118.     
  119.     key_class = class_create(THIS_MODULE, "key_class");
  120.     if (IS_ERR(key_class))
  121.     {
  122.         printk("create class fail!\n");
  123.         goto fail_class;
  124.     }
  125.     devxx = device_create(key_class, NULL, devno, NULL, DEVICE_NAME);
  126.     if (IS_ERR(devxx))
  127.     {
  128.         printk("create device fail!\n");
  129.         goto fail_device;
  130.     }
  131.     return 0;

  132. fail_device:
  133.     class_destroy(key_class);
  134. fail_class:
  135.     cdev_del(devp);
  136. fail_malloc:
  137.     unregister_chrdev_region(devno, KEY_NR_DEVS);
  138.     return result;
  139. }


  140. static void __exit s3c2440_key_exit(void)
  141. {
  142.     cdev_del(devp);
  143.     device_destroy(key_class, MKDEV(key_major, 0));
  144.     class_destroy(key_class);
  145.     unregister_chrdev_region(MKDEV(key_major, 0), KEY_NR_DEVS);
  146. }


  147. MODULE_LICENSE("GPL");
  148. MODULE_AUTHOR("Domod");
  149. module_init(s3c2440_key_init);
  150. module_exit(s3c2440_key_exit);
--keydev.h

点击(此处)折叠或打开

  1. #ifndef __KEYDEV_H__
  2. #define __KEYDEV_H__

  3. #include <linux/ioctl.h>
  4. #define KEYDEV_IOC_MAGIC 'k'

  5. #define KEYDEV_POLL _IO(KEYDEV_IOC_MAGIC, 1)
  6. #define KEYDEV_IRQ _IO(KEYDEV_IOC_MAGIC, 2)

  7. #define KEYDEV_IOC_MAXNR 2

  8. #endif
--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. int main(int argc, char **argv)
  7. {
  8.     int no;
  9.     int fd;
  10.     fd = open("/dev/keys", 0);
  11.     if (fd < 0)
  12.     {
  13.         perror("open device key!");
  14.         exit(1);
  15.     }
  16.     ioctl(fd, KEYDEV_POLL, 0 );
  17.     while (1)
  18.     {
  19.         if (read(fd, &no, 4))
  20.             printf("key%d is pressing!\n", no);
  21.     }

  22. }
--Makefile

点击(此处)折叠或打开

  1. ifneq ($(KERNELRELEASE),)

  2. obj-m := key.o

  3. else

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


  11. endif

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