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

2012年(15)

分类: LINUX

2012-05-16 00:04:13

--led.c

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/types.h>
  3. #include <linux/fs.h>
  4. #include <linux/errno.h>
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/init.h>
  8. #include <linux/cdev.h>
  9. #include <linux/device.h>
  10. #include <linux/gpio.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 "led.h"
  17. #include "leddev.h"
  18. #define DEVICE_NAME "ledss"
  19. #define LEDDEV_MAJOR 0
  20. #define LED_NR_DEVS 1
  21. #define LEDON 0
  22. #define LEDOFF 1

  23. struct cdev dev;
  24. struct class *my_class;

  25. static int led_major = LEDDEV_MAJOR;

  26. static unsigned long led_table[] =
  27. {
  28.     S3C2410_GPB(5),
  29.     S3C2410_GPB(6),
  30.     S3C2410_GPB(7),
  31.     S3C2410_GPB(8),
  32. };

  33. static unsigned int led_cfg_table[] =
  34. {
  35.     S3C2410_GPIO_OUTPUT,
  36.     S3C2410_GPIO_OUTPUT,
  37.     S3C2410_GPIO_OUTPUT,
  38.     S3C2410_GPIO_OUTPUT,
  39. };
  40. static int s3c2440_leds_open(struct inode *inode, struct file *filp)
  41. {
  42.     int i;
  43.     for(i = 0; i < 4; ++i)
  44.     {
  45.         s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
  46.         s3c2410_gpio_setpin(led_table[i], 0);
  47.     }
  48.     return 0;
  49. }
  50. static int s3c2440_leds_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  51. {
  52.     int i;
  53.     struct status st;
  54.     memset(&st, 0, sizeof(struct status));
  55.     switch(cmd)
  56.     {
  57.         case LEDDEV_IOCON:
  58.             for (i = 0; i < sizeof(led_table)/sizeof(led_table[0]); ++i)
  59.                 s3c2410_gpio_setpin(led_table[i], LEDON);
  60.             break;
  61.         case LEDDEV_IOCOFF:
  62.             for (i = 0; i < sizeof(led_table)/sizeof(led_table[0]); ++i)
  63.                 s3c2410_gpio_setpin(led_table[i], LEDOFF);
  64.             break;
  65.         case LEDDEV_IOCROF:
  66.             copy_from_user((void *)&st, (const void *)arg, sizeof(struct status));
  67.             for (i = 0; i < sizeof(led_table)/sizeof(led_table[0]); ++i)
  68.                 s3c2410_gpio_setpin(led_table[i], LEDOFF);
  69.             s3c2410_gpio_setpin(led_table[st.led_no - 1], !st.led_status);
  70.             break;
  71.         default:
  72.             return -EINVAL;
  73.     }
  74.     return 0;
  75. }

  76. static struct file_operations dev_fops =
  77. {
  78.     .owner = THIS_MODULE,
  79.     .ioctl = s3c2440_leds_ioctl,
  80.     .open = s3c2440_leds_open,
  81. };


  82. static int __init s3c2440_led_init(void)
  83. {
  84.     int result;
  85.     struct device *devxx;
  86.     dev_t devno = MKDEV(led_major, 0);

  87.     if (led_major)
  88.         result = register_chrdev_region(devno, 1, DEVICE_NAME);
  89.     else
  90.     {
  91.         result = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);
  92.         led_major = MAJOR(devno);
  93.         printk("Major-->%d\n", led_major);
  94.     }

  95.     if (result < 0)
  96.         return result;

  97.     cdev_init(&dev, &dev_fops);
  98.     dev.owner = THIS_MODULE;
  99.     dev.ops = &dev_fops;

  100.     cdev_add(&dev, devno, LED_NR_DEVS);

  101.     my_class = class_create(THIS_MODULE, "my_class");
  102.     if (IS_ERR(my_class))
  103.         return -1;
  104.     devxx = device_create(my_class, NULL, devno, NULL, DEVICE_NAME);
  105.     if (IS_ERR(devxx))
  106.     {
  107.         printk("create device node fail!\n");
  108.         return -1;
  109.     }
  110.     printk("create device node sucess!\n");
  111.     return result;
  112. }

  113. static void __exit s3c2440_led_exit(void)
  114. {
  115.     cdev_del(&dev);
  116.     device_destroy(my_class, MKDEV(led_major, 0));
  117.     class_destroy(my_class);
  118.     unregister_chrdev_region(MKDEV(led_major, 0), LED_NR_DEVS);
  119. }

  120. MODULE_LICENSE("GPL");
  121. MODULE_AUTHOR("Domod");
  122. module_init(s3c2440_led_init);
  123. module_exit(s3c2440_led_exit);
--app-led.c

点击(此处)折叠或打开

  1. #include "leddev.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 on, led_no;
  9.     int fd;
  10.     struct status st;
  11.     memset(&st, 0, sizeof(struct status));
  12.     if (argc < 2 || argc > 3 )
  13.     {
  14.         fprintf(stderr, "Usage:%s 0|1\n\tOr %s led_no 0|1\n", argv[0], argv[0]);
  15.         exit(1);
  16.     }

  17.     fd = open("/dev/ledss", 0);
  18.     if (fd < 0)
  19.     {
  20.         perror("open device leds");
  21.         exit(1);
  22.     }

  23.     if (argc == 2)
  24.     {
  25.         printf("argc=%d\nargv[1]=%s\n", argc, argv[1]);
  26.         if (sscanf(argv[1], "%d", &on) != 1 || on < 0 || on > 1)
  27.         {
  28.             fprintf(stderr, "Usage:%s 0|1\n\tOr %s led_no 0|1\n", argv[0], argv[0]);
  29.             exit(1);
  30.         }
  31.         if (on)
  32.         {
  33.             printf("turn on all leds!\n");
  34.             ioctl(fd, LEDDEV_IOCON, 0);
  35.         }
  36.         else
  37.         {
  38.             printf("turn off all leds!\n");
  39.             ioctl(fd, LEDDEV_IOCOFF, 0);
  40.         }
  41.     }
  42.     else if (argc == 3)
  43.     {
  44.         if (sscanf(argv[1], "%d", &led_no) != 1 || led_no > 4 || led_no < 1 || sscanf(argv[2], "%d", &on) != 1 || on < 0 || on > 1)
  45.         {
  46.             fprintf(stderr, "Usage:%s 0|1\n\tOr %s led_no 0|1\n", argv[0], argv[0]);
  47.             exit(1);
  48.         }
  49.         st.led_no = led_no;
  50.         st.led_status = on;
  51.         printf("select what you want!\n");
  52.         ioctl(fd, LEDDEV_IOCROF, &st);
  53.     }
  54. }
--leddev.h

点击(此处)折叠或打开

  1. #ifndef __LEDDEV_H__
  2. #define __LEDDEV_H__

  3. #include <linux/ioctl.h>
  4. #define LEDDEV_IOC_MAGIC 'l'

  5. #define LEDDEV_IOCON _IO(LEDDEV_IOC_MAGIC, 1)
  6. #define LEDDEV_IOCOFF _IO(LEDDEV_IOC_MAGIC, 2)
  7. #define LEDDEV_IOCROF _IO(LEDDEV_IOC_MAGIC, 3)

  8. #define LEDDEV_IOC_MAXNR 3

  9. struct status
  10. {
  11.     int led_no;
  12.     int led_status;
  13. };

  14. #endif
--Makefile

点击(此处)折叠或打开

  1. CFILE=app-led.c
  2. TFILE=led.ko
  3. ifneq ($(KERNELRELEASE),)

  4. obj-m := led.o

  5. else

  6. KDIR := /home/domod/arm/kernel/src/linux-2.6.32.2
  7. all:
  8. [tab]make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
  9. app:
  10. [tab]arm-linux-gcc $(CFILE) -o app
  11. sub:
  12. [tab]cp $(TFILE) app ../rootfs
  13. clean:
  14. [tab]rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*

  15. endif



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