device_create ()详解

在驱动程序中初始化入口函数中,向内核注册一个设备后,往往要注册一个类
例如
static int __init mydriver_init(void) //驱动程序的初始化
{  
   ……
    MYDRIVER_Major = register_chrdev(0, DEVICE_NAME, &mydriver_fops); //向内核注册一个设备,返回值为注册的主设备号
    if (MYDRIVER_Major < 0)
    {
        printk(DEVICE_NAME " can't register major number\n");
        return MYDRIVER_Major;
    }
    ……
    mydriver_class = class_create(THIS_MODULE, DEVICE_NAME); 
//注册一个类,使mdev可以在"/dev/"目录下 面建立设备节点
    ……
    //创建一个设备节点,节点名为DEVICE_NAME
    device_create(mydriver_class, NULL, MKDEV(MYDRIVER_Major, 0), NULL, DEVICE_NAME);
    ……
}

从linux内核2.6的某个版本之后,devfs不复存在,udev成为devfs的 替代。相比devfs,udev有很多优势,在此就不罗嗦了,提醒一点,udev是应用层的东东,不要试图在内核的配置选项里找到它;加入对udev的支 持很简单,以作者所写的一个字符设备驱动为例,在驱动初始化的代码里调用class_create为该设备创建一个class,再为每个设备调用 class_device_create创建对应的设备。大致用法如下:
struct class *myclass = class_create(THIS_MODULE, “my_device_driver”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);
这样的module被加载时,udev daemon就会自动在/dev下创建my_device设备文件


class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
    class_create - create a struct class structure
    @owner: pointer to the module that is to "own" this struct class
    @name: pointer to a string for the name of this class.
在/sys/class/下创建类目录

class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_create(struct class        *cls,
                                         struct class_device *parent,
                                         dev_t               devt,
                                         struct device       *device,
                                         const char          *fmt, ...)

    class_device_create - creates a class device and registers it with sysfs
    @cls: pointer to the struct class that this device should be registered to.
    @parent: pointer to the parent struct class_device of this new device, if any.
    @devt: the dev_t for the char device to be added.
    @device: a pointer to a struct device that is assiociated with this class device.
    @fmt: string for the class device's name

 

对于没有指定dev->parent的dev都将被添加到/sys/devices/virtual/tty/ 目录下,如果指定了dev->parent,那么同时该dev->class存在,同时parent->class存在,那么该dev->name目录将被添加到parent->class所在目录下

****@*****:~$ ls -a /sys/class/tty/console
lrwxrwxrwx 1 root root 0 2009-06-30 09:40 /sys/class/tty/console->../../devices/virtual/tty/console
****@*****:~$ ls -a/sys/devices/virtual/tty/console/
total 0
-rw-r--r-- 1 root root 4.0K 2009-06-30 10:51 uevent
drwxr-xr-x 2 root root 0 2009-06-30 10:51 power
lrwxrwxrwx 1 root root 0 2009-06-30 10:57 subsystem-> ../../../../class/tty
-r--r--r-- 1 root root 4.0K 2009-06-30 10:57 dev

来看看linux2.6.25内核源码,是怎么做得.

device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1),"console");
==>device_register
==>device_add
==>setup_parent
==>get_device_parent
==>dev->kobj.parent= kobj("/sys/devices/virtual/tty");
//所以所有的文件添加都将指向该目录

#ifdef CONFIG_SYSFS_DEPRECATED
static struct kobject *get_device_parent(struct device*dev,
                    struct device *parent)
{
    
    if (dev->class&& (!parent || parent->class!= dev->class))
        return &dev->class->subsys.kobj;
    
    else if (parent)
        return &parent->kobj;

    return NULL;
}
#else
static struct kobject*get_device_parent(struct device *dev,
                    struct device *parent)
{
    int retval;

    if (dev->class){
        struct kobject *kobj = NULL;
        struct kobject *parent_kobj;
        struct kobject *k;

        

        if (parent== NULL)
            parent_kobj= virtual_device_parent(dev);
// 获取/sys/devices/virtual目录对应的kobj
        else if(parent->class)
            return&parent->kobj;
        else
            parent_kobj= &parent->kobj;

        
        spin_lock(&dev->class->class_dirs.list_lock); // class->class_dirs本身就是一个kset
       

        
// 如果该kset的list链表上没有挂接到/sys/devices/virtual目录上的son,那么说明该class_dirs还没有在
        
// /sys/devices/virtual目录下创建,所以就需要创建该class名对应的目录
        list_for_each_entry(k,&dev->class->class_dirs.list, entry)
            if(k->parent == parent_kobj){
                kobj= kobject_get(k);
                break;
            }
        spin_unlock(&dev->class->class_dirs.list_lock);
        if (kobj)
            return kobj;
        
// 创建/sys/devices/virtual/tty这个tty_class对应的目录
        
        k = kobject_create();
        if (!k)
            returnNULL;
        k->kset= &dev->class->class_dirs;

// 名在/sys/devices/virtual/目录下创建以tty_class的name为目录名的目录[luther.gliethttp]

        retval = kobject_add(k, parent_kobj,"%s", dev->class->name);
// 将kobj添加到parent_kobj对应目录下
        if (retval< 0) {
            kobject_put(k);
            returnNULL;
        }
        
        return k;
    }
    if (parent)
        return &parent->kobj;
    return NULL;
}
#endif

你可能感兴趣的:(device_create ()详解)