Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1702172
  • 博文数量: 98
  • 博客积分: 667
  • 博客等级: 上士
  • 技术积分: 1631
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-27 15:59
个人简介

一沙一世界 一树一菩提

文章分类

全部博文(98)

文章存档

2021年(8)

2020年(16)

2019年(8)

2017年(1)

2016年(11)

2015年(17)

2014年(9)

2013年(4)

2012年(19)

2011年(1)

2009年(4)

分类: 嵌入式

2012-06-26 14:06:06

mdev是简化版的udev,适合于嵌入式的应用埸合。其具有使用简单的特点。
它的作用热插拔或动态加载驱动程序时,自动产生驱动程序所需的节点文件。
根据我的使用情况记录下我的使用过程:
(1)我的busybox是1.19.4版本。在编译时加上对mdev的支持
    Linux System Utilities  --->               
mdev                    
Support /etc/mdev.conf                
Support command execution at device addition/removal
(2)在启动时加上使用mdev的命令,下面2句
    /bin/echo /sbin/mdev > /proc/sys/kernel/hotplug   
mdev -s 
我的上面2句开始放在inittab中,但是这样的话u盘在上电的时候不能正常挂载,但是设备启动以后可以正常挂载和卸载。后来放在rcS中,可以正常工作了。但是在系统启动流程上是一样的,这点我很疑惑。
在执行上面2句以前保证已经挂载了proc,sys文件系统。
(3)编写mdev.conf,我的这个文件内容如下:
Sd[a-z][0-9] 0:0 660 *(/etc/automount.sh $MDEV)
而具体执行的脚本是automount.sh文件,内容附上,希望对做同样工作的人或多或少有些帮助,可以减少斟酌和验证时间,当然我的不一定就正确啊,有错误之处,希望大家共同交流,不要拍砖。
#!/bin/sh
# mount the /dev/sd* to /mnt/doc
if [ "$1" == "" ]; then
exit 1
fi
mounted=`mount | grep $1 | wc -l`
if [ ! -d /mnt/work ]; then
exit 1
fi
# If mounted, we will umount it
if [ $mounted -ge 1 ]; then
if ! umount "/mnt/work"; then
umount -l /mnt/work
exit 1
fi
else
fsck.ext3 /dev/$1 -p
if ! mount "/dev/$1" "/mnt/work" -o sync ; then
exit 1
fi
fi
exit 0
(4)当然mdev.conf也不是必须的,我的之所以需要这个文件,是为了挂载和卸载u盘,如果只是需要动态建立设备节点,这个文件就不需要了。
要在加载驱动的时候再dev目录下动态建立设备节点其实只需要下面两个函数:
class_create和device_create
下面也把我的代码关键部分提供给大家:
static int RtControl_Init (void)
{
module_class = class_create(THIS_MODULE, "module");
if(IS_ERR(module_class))
{
Result = PTR_ERR(module_class);
printk("create module_class failed!\n");
goto ERR_CREATE_CLASS;
}
for (i= 0; i < DEV_NUM; i++)
{
Devs[i].nDevId = MKDEV(Devs[i].Id, 0);
Result = register_chrdev_region(Devs[i].Id, 1, Devs[i].DevName);
if (Result < 0)
{
ECError("Could not register the specific major number of device : %s!\n",Devs[i].DevName);
goto ERR_REG_ID;
}
cdev_init(&Devs[i].cDevice, &Devs[i].DevOps);
Devs[i].Device.owner = THIS_MODULE;
Devs[i].Device.ops = &Devs[i].DevOps;
Result = cdev_add(&Devs[i].Device, Devs[i].Id, 1);
if (Result < 0)
{
ECError("Could not add the device <%s> to system!\n", Devs[i].DevName);
goto ERR_ADD_DEV;
}
ECPrint("Register device : <%s : %x> successfully!\n", Devs[i].DevName, Devs[i].Id);
device_create(module_class, NULL, Devs[i].Id, NULL, Devs[i].DevName);
printk("device_create %s success!\n", Devs[i].DevName);
}
return iResult;
ERR_ADD_DEV:
。。。
ERR_REG_ID:
。。。
ERR_CREATE_CLASS:
。。。
}

static void Control_Cleanup (void)
{
。。。
for (i= 0; i< DEV_NUM; i++)
{
if (Devs[i].Id)
device_destroy(module_class, Devs[i].Id);
cdev_del(&Devs[i].Device);
if (Devs[i].Id)
unregister_chrdev_region(Devs[i].Id, 1);
}
class_destroy(module_class);
return;
}
module_init(RtControl_Init);
module_exit(RtControl_Cleanup);

然后编译ko文件,insmod的时候就可以在dev目录下建立设备节点,确实挺方便












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