Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1259735
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2011-11-17 22:46:58

  1. === udev
  2. 在设备被发现时自动加载对应驱动模块。(热插拔,设备被发现时,自动创建dev文件,并加载驱动模块)
  3. 1.    vim /udev/Makefile    修改 安装路径和编译器
  4.     prefix = /nfsroot/
  5.     CROSS_COMPILE = /usr/local/arm/3.4.1/bin/arm-linux-
  6.     
  7.     make
  8.     make install

  9. 2.    vim /nfsroot/etc/init.d/rcS 加上
  10.     mount -t tmpfs tmpfs /dev
  11.     /sbin/udevd -d
  12.     /sbin/udevstart
  13.     开机自动挂载

  14.     ------------------ src -------------------------
  15.     init:
  16.         struct cdev my_cdev;
  17.         struct class *my_class;
  18.         #define DUMMY_MAJOR 255
  19.         #define DUMMY_MINOR 0

  20.         dev_t devno = MKDEV(DUMMY_MAJOR, DUMMY_MINOR);

  21.         cdev_init(&my_cdev, &f_ops);
  22.         cdev_add(&my_cdev, MKDEV(DUMMY_MAJOR, DUMMY_MINOR), 1);

  23.         my_class =class_create(THIS_MODULE, "my_class");
  24.         class_device_create(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR), NULL,"my_dev");

  25.     exit:
  26.         cdev_del(&my_cdev);
  27.         class_device_destroy(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR));
  28.         class_destroy(my_class);
  1. #include <stdio.h>

  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>

  5. int main(void)
  6. {
  7.     int fd;
  8.     fd = open("/dev/test_udev", O_RDWR);
  9.     if(fd < 0)
  10.     {
  11.         printf("open failed\n");
  12.     }

  13.     return 0;
  14. }
  1. #include <linux/module.h>
  2. #include <linux/init.h>

  3. #include <linux/fs.h>
  4. #include <linux/cdev.h>

  5. #include <linux/device.h>

  6. dev_t devno;
  7. struct cdev my_cdev;
  8. struct class *my_class;

  9. int my_open(struct inode *nod, struct file *filp)
  10. {
  11.     printk(" my_open\n");
  12.     return 0;
  13. }
  14. struct file_operations my_ops = {
  15.     .open = my_open,
  16. };

  17. int init_test(void)
  18. {

  19.     devno = MKDEV(253, 0);
  20.     register_chrdev_region(devno, 1, "test udev");
  21.     cdev_init(&my_cdev, &my_ops);

  22.     //init
  23.     my_class = class_create(THIS_MODULE, "test_udev_class");
  24.     device_create(my_class, NULL, devno, NULL, "%s", "test_udev");

  25.     cdev_add(&my_cdev, devno, 1);
  26.     printk("hello udev\n");        
  27.     return 0;
  28. }

  29. void exit_test(void)
  30. {
  31.     cdev_del(&my_cdev);
  32.     device_destroy(my_class, devno);
  33.     class_destroy(my_class);
  34.     unregister_chrdev_region(devno, 1);
  35.     printk("bye\n");        
  36. }

  37. module_init(init_test);
  38. module_exit(exit_test);

  39. MODULE_LICENSE("GPL");

  40. MODULE_AUTHOR("Richard");
  41. MODULE_DESCRIPTION("this is first module");
  42. MODULE_VERSION("v0.1");
阅读(851) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~