Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1801075
  • 博文数量: 286
  • 博客积分: 3713
  • 博客等级: 少校
  • 技术积分: 2275
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 09:47
个人简介

http://blog.chinaunix.net/uid/16979052.html

文章分类

全部博文(286)

文章存档

2018年(1)

2017年(16)

2016年(9)

2015年(17)

2014年(15)

2013年(112)

2012年(116)

分类: 嵌入式

2017-02-16 14:56:16

     ------本文很多关于mdev解释的内容来源于网络,有说的不对的地方,望大家指正。-------
 
   写Linux 设备驱动程序的时候,很多时候都是利用mknod 命令手动创建设备节点,mdev可以用来在模块加载-- insmod-- 的时候自动在/dev 目录下创建相应设备节点,并在卸载模块-- rmmod --时删除该节点。
   内核同时提供了class_create( …) 函数,可以用它来创建一个类,这个类存放于sysfs 下面,一旦创建好了这个类,再调用device_create() 函数来在/dev 目录下创建相应的设备节点。这样,加载模块的时候,用户空间中的udev 会自动响应device_create( …) 函数,去/sysfs 下寻找对应的类从而创建设备节点。 
举例如下(只是把关键的函数说明下):
  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/string.h>
  4. #include <linux/init.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/rtc.h>
  8. #include <linux/bcd.h>
  9. #include <linux/clk.h>
  10. #include <linux/device.h>

  11. MODULE_LICENSE("Dual BSD/GPL");

  12. static struct class *firstdrv_class;
  13. static struct class_device *firstdrv_class_dev;


  14. static int first_drv_open(struct inode *inode, struct file *file)
  15. {
  16.     printk("first_drv_open\n");
  17.     
  18.     return 0;
  19. }

  20. static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  21. {
  22.     printk("first_drv_write\n");
  23.     
  24.     return 0;
  25. }

  26. static struct file_operations first_drv_fops =
  27. {
  28.     .owner = THIS_MODULE,
  29.     .open = first_drv_open,
  30.     .write = first_drv_write,
  31. };

  32. int major;
  33. static int first_drv_init(void)
  34. {
  35.     printk("Hello world!\n");
  36.     major = register_chrdev(0, "first_drv", &first_drv_fops);
  37.     firstdrv_class = class_create(THIS_MODULE, "firstdrv");
  38.     firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz");
  39.     return 0;
  40. }

  41. static void first_drv_exit(void)
  42. {
  43.     printk("Bye, hello world!\n");
  44.     unregister_chrdev(major, "first_drv");
  45.     class_device_unregister(firstdrv_class_dev);
  46.     class_destroy(firstdrv_class);
  47. }

  48. module_init(first_drv_init);
  49. module_exit(first_drv_exit);
附上Makefile文件:
  1. KERNELDIR = /opt/linux-2.6.22.6/
  2.     # The current directory is passed to sub-makes as argument
  3. PWD := $(shell pwd)
  4. CC =arm-linux-gcc
  5. obj-m := hello.o
  6. modules:
  7.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
  8. clean:
  9.     rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
  10. .PHONY: modules modules_install clean
进入目录,make
生成 hello.ko 文件,拷贝到nfs目录,发现insmod 成功,但是没有自动创建 /dev/xyz设备。
  1. # insmod hello.ko 
  2. Hello world!
  3. # lsmod
  4. hello 2188 0 - Live 0xbf000000
  5. # cat /proc/devices 
  6. Character devices:
  7.   1 mem
  8.   2 pty
  9.   3 ttyp
  10.   4 /dev/vc/0
  11.   4 tty
  12.   4 ttyS
  13.   5 /dev/tty
  14.   5 /dev/console
  15.   5 /dev/ptmx
  16.   6 lp
  17.   7 vcs
  18.  10 misc
  19.  13 input
  20.  29 fb
  21.  90 mtd
  22.  99 ppdev
  23. 128 ptm
  24. 136 pts
  25. 180 usb
  26. 189 usb_device
  27. 204 s3c2410_serial
  28. 252 first_drv
  29. 253 usb_endpoint
  30. 254 rtc

  31. Block devices:
  32.   1 ramdisk
  33.   7 loop
  34.   8 sd
  35.  31 mtdblock
  36.  65 sd
  37.  66 sd
  38.  67 sd
  39.  68 sd
  40.  69 sd
  41.  70 sd
  42.  71 sd
  43. 128 sd
  44. 129 sd
  45. 130 sd
  46. 131 sd
  47. 132 sd
  48. 133 sd
  49. 134 sd
  50. 135 sd
  51. # ls /dev/xyz
  52. ls: /dev/xyz: No such file or directory
查看 busybox里面的 mdev.txt 文件,于是在 /etc/init.d/rcS里面增加如下命令,蓝色部分:
  1. #!/bin/sh
  2. ifconfig eth0 192.168.1.133
  3. ifconfig lo up
  4. mount -a
  5. mount -t proc proc /proc
  6. mount -t sysfs sysfs /sys
  7. echo /sbin/mdev > /proc/sys/kernel/hotplug
  8. mdev -s
然后reboot,重新insmod 相应的.ko文件
  1. # cd /mnt/
  2. # insmod hello.ko
  3. Hello
  4. # lsmod
  5. hello 2188 0 - Live 0xbf000000
  6. # cat /proc/devices
  7. Character devices:
  8.   1 mem
  9.   2 pty
  10.   3 ttyp
  11.   4 /dev/vc/0
  12.   4 tty
  13.   4 ttyS
  14.   5 /dev/tty
  15.   5 /dev/console
  16.   5 /dev/ptmx
  17.   6 lp
  18.   7 vcs
  19.  10 misc
  20.  13 input
  21.  29 fb
  22.  90 mtd
  23.  99 ppdev
  24. 128 ptm
  25. 136 pts
  26. 180 usb
  27. 189 usb_device
  28. 204 s3c2410_serial
  29. 252 first_drv
  30. 253 usb_endpoint
  31. 254 rtc

  32. Block devices:
  33.   1 ramdisk
  34.   7 loop
  35.   8 sd
  36.  31 mtdblock
  37.  65 sd
  38.  66 sd
  39.  67 sd
  40.  68 sd
  41.  69 sd
  42.  70 sd
  43.  71 sd
  44. 128 sd
  45. 129 sd
  46. 130 sd
  47. 131 sd
  48. 132 sd
  49. 133 sd
  50. 134 sd
  51. 135 sd
  52. # ls /dev/xyz
  53. /dev/xyz
注意上面的蓝色字体部分,自动创建成功。
echo /sbin/mdev > /proc/sys/kernel/hotplug 当有热插拔事件产生时,内核就会调用位于/sbin目录的mdev。这时mdev通过环境变量中的 ACTION DEVPATH,(这两个变量是系统自带的)来确定此次热插拔事件的动作以及影响了/sys中的那个目录。接着会看看这个目录中是否有“dev”的属性文件,如果有就利用这些信息为这个设备在/dev 下创建设备节点文件。

驱动里,那2个函数只是在sysfs里建信息;需要hotplug的mdev根据这些信息来创建节点

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