本文主要是介绍busybox mdev使用 自动创建设备节点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
------本文很多关于mdev解释的内容来源于网络,有说的不对的地方,望大家指正。------- 写 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里面增加如下命令,蓝色部分:
- #!/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根据这些信息来创建节点
相关热门文章
- 承接自动化测试培训、外包、实...
- Solaris PowerTOP 1.0 发布
- For STKMonitor
- busybox的httpd使用CGI脚本(Bu...
- 项目小体会
- 修改默认端口为222,centos自...
- 用PHP做一个ftp登录页面...
- Toad for Oracle工具,为什么在...
- 本地win7安装vmw9系统winserv...
- powermt config是所什么用的...
给主人留下些什么吧!~~
评论热议
这篇关于busybox mdev使用 自动创建设备节点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!