Chinaunix首页 | 论坛 | 博客
  • 博客访问: 432570
  • 博文数量: 117
  • 博客积分: 3003
  • 博客等级: 中校
  • 技术积分: 1221
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-16 14:11
文章分类

全部博文(117)

文章存档

2011年(7)

2010年(110)

我的朋友

分类: LINUX

2010-10-07 08:13:51

mdev的使用方法和原理

mdev是busybox自带的一个简化版的udev,适合于嵌入式的应用埸合。其具有使用简单的特点。它的作用,就是在系统启动和热插拔

或动态加载驱动程序时,自动产生驱动程序所需的节点文件。在以busybox为基础构建嵌入式linux的根文件系统时,使用它是最优

的选择。

mdev使用
mdev的使用在busybox中的mdev.txt文档已经将得很详细了。但作为例子,我简单讲讲我的使用过程:

(1)在编译时加上对mdev的支持(我是使用的是busybox1.10.1):
    Linux System Utilities  --->   
          
mdev      
          
   Support /etc/mdev.conf
          
     Support command execution at device addition/removal

(2)在启动时加上使用mdev的命令:
我在自己创建的根文件系统(nfs)中的/linuxrc文件中添加了如下指令:
#挂载/sys为sysfs文件系统
    echo "----------mount /sys as sysfs"
    /bin/mount -t tmpfs mdev /dev
    /bin/mount -t sysfs sysfs /sys
    echo "----------Starting mdev......"
    /bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
    mdev -s
注意:是/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug,并非/bin/echo /bin/mdev > /proc/sys/kernel/hotplug。

busybox的文档有错!!
 
(3)在你的驱动中加上对类设备接口的支持。
  在驱动程序的初始化函数中,使用下述的类似语句,就能在类设备目录下添加包含设备号的名为“dev”的属性文件。并通过mdev

在/dev目录下产生gpio_dev0的设备节点文件。
  my_class = class_create(THIS_MODULE, "gpio_class");
  if(IS_ERR(my_class)) {
    printk("Err: failed in creating class.\n");
    return -1;
  }
  /* register your own device in sysfs, and this will cause mdev to create corresponding device node */
  class_device_create(my_class, MKDEV(gpio_major_number, 0), NULL, "gpio_dev%d" ,0);
  在驱动程序的清除程序段,加入以下语句,以完成清除工作。
  class_device_destroy(my_class, MKDEV(gpio_major_number, 0));
  class_destroy(my_class);
  需要的头文件是linux/device.h,因此程序的开始应加入下句
  #include
  另外,my_class是class类型的结构体指针,要在程序开始时声明成全局变量。
  struct class *my_class;
  上述程序中的gpio_major_number是设备的主节点号。可以换成需要的节点号。gpio_dev是最终生成的设备节点文件的名子。%d是

用于以相同设备自动编号的。gpio_class是建立的class的名称,当驱动程序加载后,可以在/sys/class的目录下看到它。
  上述语句也不一定要在初始化和清除阶段使用,可以根据需要在其它地方使用。

(4)至于/etc/mdev.conf文件,可有可无,不影响使用,只是添加了些功能。
关于mdev的使用方法,我在网上找到一篇中文版的。大家可以到我上传的资源中下载。    

要想真正用好mdev,适当知道一下原理是必不可少的。现在简单介绍一下mdev的原理:

执行mdev -s
:以‘-s’为参数调用位于
/sbin目录写的mdev(其实是个链接,作用是传递参数给/bin目录下的busybox程序并调用它),mdev扫描 /sys/class 和
/sys/block
中所有的类设备目录,如果在目录中含有名为“dev”的文件,且文件中包含的是设备号,则mdev就利用这些信息为这个设备在/dev
下创建设备节点文件。一般只在启动时才执行一次 “mdev -s”。

热插拔事件:由于启动时运行了命
令:echo /sbin/mdev > /proc/sys/kernel/hotplug ,那么当有热插拔事件产生时,内核就会调用位于
/sbin目录的mdev。这时mdev通过环境变量中的 ACTION 和
DEVPATH,(这两个变量是系统自带的)来确定此次热插拔事件的动作以及影响了/sys中的那个目录。接着会看看这个目录中是否有

“dev”的属性文件,如果有就利用这些信息为
这个设备在/dev 下创建设备节点文件。

最后,附上我在工作中编写的一段简单的gpio控制驱动程序。此程序没有什么功能,主要是做一些测试用的。有兴趣的朋友可以用

它测试一下上述的mdev的使用方法。我用的是友善公司的mini2440开发板。

 

#include
#include
#include
#include

#include        /* printk() */
#include            /* everything... */
#include
#include     /* request_irq() */
#include
#include
#include
#include         /* copy_to_user() */
#include         /* mdelay() */
#include        /*class_create()*/
#include
#include

 

#define VERSION_STRING  "gpio driver for JM_Xcontrol"

#define DEVICE_NAME "JM_Xcontrol_gpio"

/* Use 0xE0 as magic number */
#define XRAY_IOC_MAGIC  0xE0

#define XRAY_IOCLCDBACKLIGHT    _IO(XRAY_IOC_MAGIC, 0)
#define XRAY_IOC485REC    _IO(XRAY_IOC_MAGIC, 1)
#define XRAY_IOC485TRC    _IO(XRAY_IOC_MAGIC, 2)
#define XRAY_IOCBUZZER    _IO(XRAY_IOC_MAGIC, 3)
#define XRAY_IOC_MAXNR 12

MODULE_AUTHOR("hugerat");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION(VERSION_STRING);

unsigned int gpio_major_number=0;
struct cdev gpio_dev;


struct class *my_class;

 

static int gpio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
  int err=0;
  unsigned long tmp;
  //-------以下检查命令---------//

  if (_IOC_TYPE(cmd) != XRAY_IOC_MAGIC) return -ENOTTY;
  if (_IOC_NR(cmd) > XRAY_IOC_MAXNR) return -ENOTTY;

  if (_IOC_DIR(cmd) & _IOC_READ)
    err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
  else if (_IOC_DIR(cmd) & _IOC_WRITE)
    err =  !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
  if (err)
    return -EFAULT;
  //--------------------------//
  switch ( cmd )
  {
    case XRAY_IOCLCDBACKLIGHT: //控制LCD背光开关
      if(arg==0)
      {
        s3c2410_gpio_setpin(S3C2410_GPB1, 1);
      }
      else
      {
        s3c2410_gpio_setpin(S3C2410_GPB1, 0);
      }
      break;
    case XRAY_IOC485REC:
      if(arg==0)
      {
        s3c2410_gpio_setpin(S3C2410_GPG10, 1);
      }
      else
      {
        s3c2410_gpio_setpin(S3C2410_GPG10, 0);
      }
      break;
    case XRAY_IOC485TRC:
      if(arg==0)
      {
        s3c2410_gpio_setpin(S3C2410_GPG12, 0);
      }
      else
      {
        s3c2410_gpio_setpin(S3C2410_GPG12, 1);
      }
      break;
    case XRAY_IOCBUZZER:
      if(arg==0)
      {
        s3c2410_gpio_setpin(S3C2410_GPB0, 0);
      }
      else
      {
        s3c2410_gpio_setpin(S3C2410_GPB0, 1);
      }
      break;
    default:
      break;
  }
  return 0;
}

static struct file_operations gpio_fops = {
  .owner   = THIS_MODULE,
  //.open    = xray_open,
  //.release = xray_release,
  //.read    = xray_read,
  //.write   = xray_write,
  .ioctl   = gpio_ioctl,
  //.fasync  = xray_fasync,
};

static int __init gpio_init(void)
{
  int ret,devno;
  dev_t dev;
  unsigned long tmp;
  ret = alloc_chrdev_region(&dev,0,1,DEVICE_NAME);
  gpio_major_number = MAJOR(dev);
  printk(KERN_INFO "Initial jm_xcontrol_gpio driver!\n");
  if (ret<0) {
    printk(KERN_WARNING "gpio:can't get major number %d\n",gpio_major_number);
    return ret;
  }

  devno = MKDEV(gpio_major_number,0);
  cdev_init(&gpio_dev,&gpio_fops);
  gpio_dev.owner = THIS_MODULE;
  gpio_dev.ops   = &gpio_fops;

  ret = cdev_add(&gpio_dev,devno,1);
  if (ret) {
    unregister_chrdev_region(dev,1);
    printk(KERN_NOTICE "Error %d adding gpio device\n",ret);
    return ret;
  }
  my_class = class_create(THIS_MODULE, "gpio_class");
  if(IS_ERR(my_class)) {
    printk("Err: failed in creating class.\n");
    return -1;
  }
  /* register your own device in sysfs, and this will cause mdev to create corresponding device node */
  class_device_create(my_class, MKDEV(gpio_major_number, 0), NULL, "gpio_dev%d" ,0);
 
  //LCD背光
  s3c2410_gpio_cfgpin(S3C2410_GPB1, S3C2410_GPB1_OUTP);
  s3c2410_gpio_setpin(S3C2410_GPB1, 0);
  //蜂鸣器
  s3c2410_gpio_cfgpin(S3C2410_GPB0, S3C2410_GPB0_OUTP);
  s3c2410_gpio_setpin(S3C2410_GPB0, 0);
 
  //485收发控制
  s3c2410_gpio_cfgpin(S3C2410_GPG10, S3C2410_GPG10_OUTP); //收
  s3c2410_gpio_setpin(S3C2410_GPG10, 0);
  s3c2410_gpio_cfgpin(S3C2410_GPG12, S3C2410_GPG12_OUTP);   //发
  s3c2410_gpio_setpin(S3C2410_GPG12, 0);
  return 0;
}

static void __exit gpio_cleanup(void)
{
  unsigned long tmp;
  dev_t dev=MKDEV(gpio_major_number,0);
  cdev_del(&gpio_dev);
  class_device_destroy(my_class, MKDEV(gpio_major_number, 0));
  class_destroy(my_class);
  unregister_chrdev_region(dev,1);
  s3c2410_gpio_setpin(S3C2410_GPB1, 1); //关背光
  s3c2410_gpio_setpin(S3C2410_GPB0, 0); //关蜂鸣器
 
  s3c2410_gpio_setpin(S3C2410_GPG10, 1); //关485收
  s3c2410_gpio_setpin(S3C2410_GPG12, 0); //关485发
  printk(KERN_INFO "unregistered the %s\n",DEVICE_NAME);
}

module_init(gpio_init);
module_exit(gpio_cleanup);  
---------------------


-------------
 MDEV Primer
-------------

For those of us who know how to use mdev, a primer might seem lame.  For
everyone else, mdev is a weird black box that they hear is awesome, but can't
seem to get their head around how it works.  Thus, a primer.

-----------
 Basic Use
-----------

Mdev has two primary uses: initial population and dynamic updates.  Both
require sysfs support in the kernel and have it mounted at /sys.  For dynamic
updates, you also need to have hotplugging enabled in your kernel.

Here's a typical code snippet from the init script:
[0] mount -t proc proc /proc
[1] mount -t sysfs sysfs /sys
[2] echo /sbin/mdev > /proc/sys/kernel/hotplug
[3] mdev -s

Alternatively, without procfs the above becomes:
[1] mount -t sysfs sysfs /sys
[2] sysctl -w kernel.hotplug=/sbin/mdev
[3] mdev -s


Of course, a more "full" setup would entail executing this before the previous
code snippet:
[4] mount -t tmpfs -o size=64k,mode=0755 tmpfs /dev
[5] mkdir /dev/pts
[6] mount -t devpts devpts /dev/pts

The simple explanation here is that [1] you need to have /sys mounted before
executing mdev.  Then you [2] instruct the kernel to execute /sbin/mdev whenever
a device is added or removed so that the device node can be created or
destroyed.  Then you [3] seed /dev with all the device nodes that were created
while the system was booting.

For the "full" setup, you want to [4] make sure /dev is a tmpfs filesystem
(assuming you're running out of flash).  Then you want to [5] create the
/dev/pts mount point and finally [6] mount the devpts filesystem on it.

-------------
 MDEV Config   (/etc/mdev.conf)
-------------

Mdev has an optional config file for controlling ownership/permissions of
device nodes if your system needs something more than the default root/root
660 permissions.

The file has the format:
           :
 or @ :

For example:
    hd[a-z][0-9]* 0:3 660

The config file parsing stops at the first matching line.  If no line is
matched, then the default of 0:0 660 is used.  To set your own default, simply
create your own total match like so:
    .* 1:1 777

You can rename/move device nodes by using the next optional field.
    : [=path]
So if you want to place the device node into a subdirectory, make sure the path
has a trailing /.  If you want to rename the device node, just place the name.
    hda 0:3 660 =drives/
This will move "hda" into the drives/ subdirectory.
    hdb 0:3 660 =cdrom
This will rename "hdb" to "cdrom".

Similarly, ">path" renames/moves the device but it also creates
a direct symlink /dev/DEVNAME to the renamed/moved device.

You can also prevent creation of device nodes with the 4th field as "!":
    tty[a-z]. 0:0 660 !
    pty[a-z]. 0:0 660 !

If you also enable support for executing your own commands, then the file has
the format:
    : [=path] [@|$|*]
    or
    : [>path] [@|$|*]
    or
    : [!] [@|$|*]

For example:
---8<---
# block devices
([hs]d[a-z])        root:disk    660    >disk/%1/0
([hs]d[a-z])([0-9]+)    root:disk    660    >disk/%1/%2
mmcblk([0-9]+)        root:disk    660    >disk/mmc/%1/0
mmcblk([0-9]+)p([0-9]+)    root:disk    660    >disk/mmc/%1/%2
# network devices
(tun|tap)        root:network    660    >net/%1
---8<---

The special characters have the meaning:
    @ Run after creating the device.
    $ Run before removing the device.
    * Run both after creating and before removing the device.

The command is executed via the system() function (which means you're giving a
command to the shell), so make sure you have a shell installed at /bin/sh.  You
should also keep in mind that the kernel executes hotplug helpers with stdin,
stdout, and stderr connected to /dev/null.

For your convenience, the shell env var $MDEV is set to the device name.  So if
the device "hdc" was matched, MDEV would be set to "hdc".

----------
 FIRMWARE
----------

Some kernel device drivers need to request firmware at runtime in order to
properly initialize a device.  Place all such firmware files into the
/lib/firmware/ directory.  At runtime, the kernel will invoke mdev with the
filename of the firmware which mdev will load out of /lib/firmware/ and into
the kernel via the sysfs interface.  The exact filename is hardcoded in the
kernel, so look there if you need to know how to name the file in userspace.

------------
 SEQUENCING
------------

Kernel does not serialize hotplug events. It increments SEQNUM environmental
variable for each successive hotplug invocation. Normally, mdev doesn't care.
This may reorder hotplug and hot-unplug events, with typical symptoms of
device nodes sometimes not created as expected.

However, if /dev/mdev.seq file is found, mdev will compare its
contents with SEQNUM. It will retry up to two seconds, waiting for them
to match. If they match exactly (not even trailing '\n' is allowed),
or if two seconds pass, mdev runs as usual, then it rewrites /dev/mdev.seq
with SEQNUM+1.

IOW: this will serialize concurrent mdev invocations.

If you want to activate this feature, execute "echo >/dev/mdev.seq" prior to
setting mdev to be the hotplug handler. This writes single '\n' to the file.
NB: mdev recognizes /dev/mdev.seq consisting of single '\n' character
as a special case. IOW: this will not make your first hotplug event
to stall for two seconds.
阅读(1691) | 评论(0) | 转发(0) |
0

上一篇:ARMEB,ARMEL,AEBI

下一篇:C++标准库简介

给主人留下些什么吧!~~