busybox mdev使用 自动创建设备节点
上一篇 /
下一篇 2013-03-12 15:42:40
/ 个人分类:Linux驱动
quote]------本文很多关于mdev解释的内容来源于网络,有说的不对的地方,望大家指正。-------
同时,各位也可以在我的blog里面找到,http://blog.chinaunix.net/space.php?uid=20752341&do=blog&id=3081976
写Linux 设备驱动程序的时候,很多时候都是利用mknod 命令手动创建设备节点,mdev可以用来在模块加载-- insmod-- 的时候自动在/dev 目录下创建相应设备节点,并在卸载模块-- rmmod --时删除该节点。
内核同时提供了class_create( …) 函数,可以用它来创建一个类,这个类存放于sysfs 下面,一旦创建好了这个类,再调用device_create(…) 函数来在/dev 目录下创建相应的设备节点。这样,加载模块的时候,用户空间中的udev 会自动响应device_create( …) 函数,去/sysfs 下寻找对应的类从而创建设备节点。
举例如下(只是把关键的函数说明下):
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/string.h>
- #include <linux/init.h>
- #include <linux/platform_device.h>
- #include <linux/interrupt.h>
- #include <linux/rtc.h>
- #include <linux/bcd.h>
- #include <linux/clk.h>
- #include <linux/device.h>
- MODULE_LICENSE("Dual BSD/GPL");
- static struct class *firstdrv_class;
- static struct class_device *firstdrv_class_dev;
- static int first_drv_open(struct inode *inode, struct file *file)
- {
- printk("first_drv_open\n");
-
- return 0;
- }
- static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
- {
- printk("first_drv_write\n");
-
- return 0;
- }
- static struct file_operations first_drv_fops =
- {
- .owner = THIS_MODULE,
- .open = first_drv_open,
- .write = first_drv_write,
- };
- int major;
- static int first_drv_init(void)
- {
- printk("Hello world!\n");
- major = register_chrdev(0, "first_drv", &first_drv_fops);
- firstdrv_class = class_create(THIS_MODULE, "firstdrv");
- firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz");
- return 0;
- }
- static void first_drv_exit(void)
- {
- printk("Bye, hello world!\n");
- unregister_chrdev(major, "first_drv");
- class_device_unregister(firstdrv_class_dev);
- class_destroy(firstdrv_class);
- }
- module_init(first_drv_init);
- module_exit(first_drv_exit);
复制代码 附上Makefile 文件:
- KERNELDIR = /opt/linux-2.6.22.6/
- # The current directory is passed to sub-makes as argument
- PWD := $(shell pwd)
- CC =arm-linux-gcc
- obj-m := hello.o
- modules:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
- clean:
- rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
- .PHONY: modules modules_install clean
复制代码进入目录,make
生成 hello.ko 文件,拷贝到nfs目录,发现insmod 成功,但是没有自动创建 /dev/xyz设备。
- # insmod hello.ko
- Hello world!
- # lsmod
- hello 2188 0 - Live 0xbf000000
- # cat /proc/devices
- Character devices:
- 1 mem
- 2 pty
- 3 ttyp
- 4 /dev/vc/0
- 4 tty
- 4 ttyS
- 5 /dev/tty
- 5 /dev/console
- 5 /dev/ptmx
- 6 lp
- 7 vcs
- 10 misc
- 13 input
- 29 fb
- 90 mtd
- 99 ppdev
- 128 ptm
- 136 pts
- 180 usb
- 189 usb_device
- 204 s3c2410_serial
- 252 first_drv
- 253 usb_endpoint
- 254 rtc
- Block devices:
- 1 ramdisk
- 7 loop
- 8 sd
- 31 mtdblock
- 65 sd
- 66 sd
- 67 sd
- 68 sd
- 69 sd
- 70 sd
- 71 sd
- 128 sd
- 129 sd
- 130 sd
- 131 sd
- 132 sd
- 133 sd
- 134 sd
- 135 sd
- # ls /dev/xyz
- ls: /dev/xyz: No such file or directory
复制代码
查看busybox里面的 mdev.txt 文件,于是在 /etc/init.d/rcS里面增加如下命令,蓝色部分(个人觉得mount -t proc proc /proc 这条可以不用):
#!/bin/sh
ifconfig eth0 192.168.1.133
ifconfig lo up
mount -a
mount -t proc proc /proc
mount -t sysfs sysfs /sys
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
然后
reboot,重新insmod 相应的.ko文件
- # cd /mnt/
- # insmod hello.ko
- Hello
- # lsmod
- hello 2188 0 - Live 0xbf000000
- # cat /proc/devices
- Character devices:
- 1 mem
- 2 pty
- 3 ttyp
- 4 /dev/vc/0
- 4 tty
- 4 ttyS
- 5 /dev/tty
- 5 /dev/console
- 5 /dev/ptmx
- 6 lp
- 7 vcs
- 10 misc
- 13 input
- 29 fb
- 90 mtd
- 99 ppdev
- 128 ptm
- 136 pts
- 180 usb
- 189 usb_device
- 204 s3c2410_serial
- 252 first_drv
- 253 usb_endpoint
- 254 rtc
- Block devices:
- 1 ramdisk
- 7 loop
- 8 sd
- 31 mtdblock
- 65 sd
- 66 sd
- 67 sd
- 68 sd
- 69 sd
- 70 sd
- 71 sd
- 128 sd
- 129 sd
- 130 sd
- 131 sd
- 132 sd
- 133 sd
- 134 sd
- 135 sd
- # ls /dev/xyz
- /dev/xyz
复制代码注意上面的蓝色字体部分,自动创建成功。
echo/sbin/mdev>/proc/sys/kernel/hotplug是当有热插拔事件产生时,内核就会调用位于/sbin目录的mdev。这时mdev通过环境变量中的ACTION和DEVPATH,(这两个变量是系统自带的)来确定此次热插拔事件的动作以及影响了/sys中的那个目录。接着会看看这个目录中是否有“dev”的属性文件,如果有就利用这些信息为这个设备在/dev下创建设备节点文件。
驱动里,那2个函数只是在sysfs里建信息;需要hotplug的mdev根据这些信息来创建节点
收藏
举报
TAG: