当前位置: 首页 > 工具软件 > Device > 使用案例 >

Linux内核device结构体分析

微生景胜
2023-12-01

1、前言

Linux内核中的设备驱动模型,是建立在sysfs设备文件系统和kobject上的,由总线(bus)、设备(device)、驱动(driver)和类(class)所组成的关系结构,在底层,Linux系统中的每个设备都有一个device结构体的实例,本文将对Linux内核的device结构体以及相关结构进行简要分析。

 

2、device结构体

在Linux内核源码中,struct device结构体的定义在include/linux/device.h中,实现的主要方法在drivers/base/core.c文件中,device结构体的定义如下所示:

struct device {
    struct device        *parent;

    struct device_private    *p;

    struct kobject kobj;
    const char        *init_name; /* initial name of the device */
    const struct device_type *type;

    struct mutex        mutex;    /* mutex to synchronize calls to
                     * its driver.
                     */

    struct bus_type    *bus;        /* type of bus device is on */
    struct device_driver *driver;    /* which driver has allocated this
                       device */
    void        *platform_data;    /* Platform specific data, device
                       core doesn't touch it */
    void        *driver_data;    /* Driver data, set and get with
                       dev_set/get_drvdata */
    struct dev_links_info    links;
    struct dev_pm_info    power;
    struct dev_pm_domain    *pm_domain;

#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
    struct irq_domain    *msi_domain;
#endif
#ifdef CONFIG_PINCTRL
    struct dev_pin_info    *pins;
#endif
#ifdef CONFIG_GENERIC_MSI_IRQ
    struct list_head    msi_list;
#endif

#ifdef CONFIG_NUMA
    int        numa_node;    /* NUMA node this device is close to */
#endif
    const struct dma_map_ops *dma_ops;
    u64        *dma_mask;    /* dma mask (if dma'able device) */
    u64        coherent_dma_mask;/* Like dma_mask, but for
                         alloc_coherent mappings as
                         not all hardware supports
                         64 bit addresses for consistent
                         allocations such descriptors. */
    unsigned long    dma_pfn_offset;

    struct device_dma_parameters *dma_parms;

    struct list_head    dma_pools;    /* dma pools (if dma'ble) */

    struct dma_coherent_mem    *dma_mem; /* internal for coherent mem
                         override */
#ifdef CONFIG_DMA_CMA
    struct cma *cma_area;        /* contiguous memory area for dma
                       allocations */
#endif
    /* arch specific additions */
    struct dev_archdata    archdata;

    struct device_node    *of_node; /* associated device tree node */
    struct fwnode_handle    *fwnode; /* firmware device node */

    dev_t            devt;    /* dev_t, creates the sysfs "dev" */
    u32            id;    /* device instance */

    spinlock_t        devres_lock;
    struct list_head    devres_head;

    struct klist_node    knode_class;
    struct class        *class;
    const struct attribute_group **groups;    /* optional groups */

    void    (*release)(struct device *dev);
    struct iommu_group    *iommu_group;
    struct iommu_fwspec    *iommu_fwspec;

    bool            offline_disabled:1;
    bool            offline:1;
    bool            of_node_reused:1;
};

部分结构体成员解释:

parent:指向设备的“父”设备,它所连接的设备,在大多数情况下,父设备是某种总线或主机控制器,如果该成员为NULL,则该设备为顶级设备;

p:用于保存设备驱动核心部分的私有数据;

kobj:嵌入的struct kobject对象实例;

init_name:设备的初始名称

type:设备的类型,用于标识设备类型并携带特定类型信息;

mutex:用于同步的互斥锁;

bus:该设备所处于的总线;

driver:该设备所分配的驱动程序;

platform_data:设备中特定的平台数据;

driver_data:指向驱动程序特定的私有数据;

of_node:与设备数相联系的结构体指针;

devt:用于表示设备的设备号;

devres_lock:保护设备资源的自旋锁;

devres_head:设备资源的双向链表头;

knode_class:接入class链表时所需要的klist节点;

class:指向设备所属class的指针;

groups:该设备的属性集合;

release:函数指针,当设备需要释放时调用此函数。

 

device结构体中有一部分成员不愿意被外界看到,所以抽象出了struct device_private这个结构体,该结构体包括了设备驱动模型内部的链接,结构体定义如下:

struct device_private {
    struct klist klist_children;
    struct klist_node knode_parent;
    struct klist_node knode_driver;
    struct klist_node knode_bus;
    struct list_head deferred_probe;
    struct device *device;
};

部分结构体成员解释:

klist_children:子设备的klist链表;

knode_parent:接入父设备的klist_children时所需要的klist节点;

knode_driver:接入驱动的设备链表时所需要的klist节点;

knode_bus:接入总线的设备链表时所需要的klist节点;

device:回指struct device结构体的指针。

 

device结构体中包含了一个struct device_type结构体的指针,用于描述设备的类型,该结构体定义如下:

struct device_type {
    const char *name;
    const struct attribute_group **groups;
    int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
    char *(*devnode)(struct device *dev, umode_t *mode,
             kuid_t *uid, kgid_t *gid);
    void (*release)(struct device *dev);

    const struct dev_pm_ops *pm;
};

该结构体功能类似于kobj_type。

 

还有一个设备属性结构体,名称为struct device_attribute,是对struct attribute的进一步封装,并提供了设备属性的读写函数指针,结构体定义如下:

/* interface for exporting device attributes */
struct device_attribute {
    struct attribute    attr;
    ssize_t (*show)(struct device *dev, struct device_attribute *attr,
            char *buf);
    ssize_t (*store)(struct device *dev, struct device_attribute *attr,
             const char *buf, size_t count);
};

其它的一些struct device结构体成员,例如archdata、dma和devres等,是一些设备特有的东西,暂时不讨论,本文主要关心设备驱动模型的基本建立。

 

3、device的实现

接下来对device的实现进行简单分析,实现的方法主要在文件core.c中:

int __init devices_init(void)
{
    devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);///sys/devices目录
    if (!devices_kset)
        return -ENOMEM;
    dev_kobj = kobject_create_and_add("dev", NULL);///sys/dev目录
    if (!dev_kobj)
        goto dev_kobj_err;
    sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);///sys/dev/block目录
    if (!sysfs_dev_block_kobj)
        goto block_kobj_err;
    sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);///sys/dev/char目录
    if (!sysfs_dev_char_kobj)
        goto char_kobj_err;

    return 0;

 char_kobj_err:
    kobject_put(sysfs_dev_block_kobj);
 block_kobj_err:
    kobject_put(dev_kobj);
 dev_kobj_err:
    kset_unregister(devices_kset);
    return -ENOMEM;
}

devices_init()函数是在设备驱动模型初始化时调用的部分初始函数,它实现的功能是建立sysfs中的devices目录和dev目录,然后在dev目录下建立block和char两个子目录,block和char目录用来存放设备号文件。

#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)

static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
                 char *buf)
{
    struct device_attribute *dev_attr = to_dev_attr(attr);
    struct device *dev = kobj_to_dev(kobj);
    ssize_t ret = -EIO;

    if (dev_attr->show)
        ret = dev_attr->show(dev, dev_attr, buf);
    if (ret >= (ssize_t)PAGE_SIZE) {
        print_symbol("dev_attr_show: %s returned bad count\n",
                (unsigned long)dev_attr->show);
    }
    return ret;
}

static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
                  const char *buf, size_t count)
{
    struct device_attribute *dev_attr = to_dev_attr(attr);
    struct device *dev = kobj_to_dev(kobj);
    ssize_t ret = -EIO;

    if (dev_attr->store)
        ret = dev_attr->store(dev, dev_attr, buf, count);
    return ret;
}

static const struct sysfs_ops dev_sysfs_ops = {
    .show    = dev_attr_show,
    .store    = dev_attr_store,
};

to_dev_attr()宏定义用来获取struct device_attribute结构体的首地址,dev_sysfs_ops结构体的内容就是device注册到sysfs设备文件系统的操作函数,dev_attr_show()和dev_attr_store()函数会调用struct device_attribute结构体内的读写属性相关函数。

static void device_release(struct kobject *kobj)
{
    struct device *dev = kobj_to_dev(kobj);
    struct device_private *p = dev->p;

    /*
     * Some platform devices are driven without driver attached
     * and managed resources may have been acquired.  Make sure
     * all resources are released.
     *
     * Drivers still can add resources into device after device
     * is deleted but alive, so release devres here to avoid
     * possible memory leak.
     */
    devres_release_all(dev);

    if (dev->release)
        dev->release(dev);
    else if (dev->type && dev->type->release)
        dev->type->release(dev);
    else if (dev->class && dev->class->dev_release)
        dev->class->dev_release(dev);
    else
        WARN(1, KERN_ERR "Device '%s' does not have a release() "
            "function, it is broken and must be fixed.\n",
            dev_name(dev));
    kfree(p);
}

static const void *device_namespace(struct kobject *kobj)
{
    struct device *dev = kobj_to_dev(kobj);
    const void *ns = NULL;

    if (dev->class && dev->class->ns_type)
        ns = dev->class->namespace(dev);

    return ns;
}

static void device_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
{
    struct device *dev = kobj_to_dev(kobj);

    if (dev->class && dev->class->get_ownership)
        dev->class->get_ownership(dev, uid, gid);
}

static struct kobj_type device_ktype = {
    .release    = device_release,
    .sysfs_ops    = &dev_sysfs_ops,
    .namespace    = device_namespace,
    .get_ownership    = device_get_ownership,
};

当struct device结构体内的实例kobject引用计数到0时会调用device_release()函数来释放掉device,函数调用时,会先通过kobj指针获取device的首地址,然后判断device下的release()是否存在,如果存在则调用,否则,依次判断device下device_type下的release()和device下class下的dev_release()函数是否存在,存在则调用,最后将device_private结构体指针指向的内存释放掉。

static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
{
    struct kobj_type *ktype = get_ktype(kobj);

    if (ktype == &device_ktype) {
        struct device *dev = kobj_to_dev(kobj);
        if (dev->bus)
            return 1;
        if (dev->class)
            return 1;
    }
    return 0;
}

static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
{
    struct device *dev = kobj_to_dev(kobj);

    if (dev->bus)
        return dev->bus->name;
    if (dev->class)
        return dev->class->name;
    return NULL;
}

static int dev_uevent(struct kset *kset, struct kobject *kobj,
              struct kobj_uevent_env *env)
{
    struct device *dev = kobj_to_dev(kobj);
    int retval = 0;

    /* add device node properties if present */
    if (MAJOR(dev->devt)) {
        const char *tmp;
        const char *name;
        umode_t mode = 0;
        kuid_t uid = GLOBAL_ROOT_UID;
        kgid_t gid = GLOBAL_ROOT_GID;

        add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
        add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
        name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
        if (name) {
            add_uevent_var(env, "DEVNAME=%s", name);
            if (mode)
                add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
            if (!uid_eq(uid, GLOBAL_ROOT_UID))
                add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
            if (!gid_eq(gid, GLOBAL_ROOT_GID))
                add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
            kfree(tmp);
        }
    }

    if (dev->type && dev->type->name)
        add_uevent_var(env, "DEVTYPE=%s", dev->type->name);

    if (dev->driver)
        add_uevent_var(env, "DRIVER=%s", dev->driver->name);

    /* Add common DT information about the device */
    of_device_uevent(dev, env);

    /* have the bus specific function add its stuff */
    if (dev->bus && dev->bus->uevent) {
        retval = dev->bus->uevent(dev, env);
        if (retval)
            pr_debug("device: '%s': %s: bus uevent() returned %d\n",
                 dev_name(dev), __func__, retval);
    }

    /* have the class specific function add its stuff */
    if (dev->class && dev->class->dev_uevent) {
        retval = dev->class->dev_uevent(dev, env);
        if (retval)
            pr_debug("device: '%s': %s: class uevent() "
                 "returned %d\n", dev_name(dev),
                 __func__, retval);
    }

    /* have the device type specific function add its stuff */
    if (dev->type && dev->type->uevent) {
        retval = dev->type->uevent(dev, env);
        if (retval)
            pr_debug("device: '%s': %s: dev_type uevent() "
                 "returned %d\n", dev_name(dev),
                 __func__, retval);
    }

    return retval;
}

static const struct kset_uevent_ops device_uevent_ops = {
    .filter =    dev_uevent_filter,
    .name =        dev_uevent_name,
    .uevent =    dev_uevent,
};

kset_uevent_ops结构体内的函数是用于管理kset内部的kobject的uevent操作的,其中,filter()函数用于阻止一个kobject向用户空间发送uevent,当函数的返回值为0时表示阻止,在上面的dev_uevent_filter()函数中检查了device所属的bus或者class是否存在,如果都不存在,则返回0,也就是没有发送uevent的必要了,name()函数用于覆盖kset发送给用户空间的名称,在上面的dev_uevent_name()函数选择使用device所属的bus或者class的名称,uevent()函数是在uevent将被发送到用户空间之前进行调用的,用于向uevent中增加新的环境变量。

static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
               char *buf)
{
    struct kobject *top_kobj;
    struct kset *kset;
    struct kobj_uevent_env *env = NULL;
    int i;
    size_t count = 0;
    int retval;

    /* search the kset, the device belongs to */
    top_kobj = &dev->kobj;
    while (!top_kobj->kset && top_kobj->parent)
        top_kobj = top_kobj->parent;
    if (!top_kobj->kset)
        goto out;

    kset = top_kobj->kset;
    if (!kset->uevent_ops || !kset->uevent_ops->uevent)
        goto out;

    /* respect filter */
    if (kset->uevent_ops && kset->uevent_ops->filter)
        if (!kset->uevent_ops->filter(kset, &dev->kobj))
            goto out;

    env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
    if (!env)
        return -ENOMEM;

    /* let the kset specific function add its keys */
    retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
    if (retval)
        goto out;

    /* copy keys to file */
    for (i = 0; i < env->envp_idx; i++)
        count += sprintf(&buf[count], "%s\n", env->envp[i]);
out:
    kfree(env);
    return count;
}

static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
                const char *buf, size_t count)
{
    if (kobject_synth_uevent(&dev->kobj, buf, count))
        dev_err(dev, "uevent: failed to send synthetic uevent\n");

    return count;
}
static DEVICE_ATTR_RW(uevent);

device不仅在kset中添加了对uevent的管理,还把uevent信息封装成设备的一个属性文件uevent,其权限为拥有者可读写,其中uevent_show()函数用于在用户空间显示uevent中的环境变量,uevent_store()函数则用于将uevent属性写入到内核空间。

static int device_add_attrs(struct device *dev)
{
    struct class *class = dev->class;
    const struct device_type *type = dev->type;
    int error;

    if (class) {
        error = device_add_groups(dev, class->dev_groups);
        if (error)
            return error;
    }

    if (type) {
        error = device_add_groups(dev, type->groups);
        if (error)
            goto err_remove_class_groups;
    }

    error = device_add_groups(dev, dev->groups);
    if (error)
        goto err_remove_type_groups;

    if (device_supports_offline(dev) && !dev->offline_disabled) {
        error = device_create_file(dev, &dev_attr_online);
        if (error)
            goto err_remove_dev_groups;
    }

    return 0;

 err_remove_dev_groups:
    device_remove_groups(dev, dev->groups);
 err_remove_type_groups:
    if (type)
        device_remove_groups(dev, type->groups);
 err_remove_class_groups:
    if (class)
        device_remove_groups(dev, class->dev_groups);

    return error;
}

static void device_remove_attrs(struct device *dev)
{
    struct class *class = dev->class;
    const struct device_type *type = dev->type;

    device_remove_file(dev, &dev_attr_online);
    device_remove_groups(dev, dev->groups);

    if (type)
        device_remove_groups(dev, type->groups);

    if (class)
        device_remove_groups(dev, class->dev_groups);
}

device_add_attrs()负责device中的属性添加,包括几个部分的集合,分别是class中groups、device_type中的groups还有device本身的groups,device_remove_attrs()则是相反的操作,负责删除device的属性。

static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
            char *buf)
{
    return print_dev_t(buf, dev->devt);
}
static DEVICE_ATTR_RO(dev);

这里定义了一个名为dev的属性文件,其权限为拥有者只能读,函数实现的功能为显示设备的设备号。

/**
 * device_create_file - create sysfs attribute file for device.
 * @dev: device.
 * @attr: device attribute descriptor.
 */
int device_create_file(struct device *dev,
               const struct device_attribute *attr)
{
    int error = 0;

    if (dev) {
        WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
            "Attribute %s: write permission without 'store'\n",
            attr->attr.name);
        WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
            "Attribute %s: read permission without 'show'\n",
            attr->attr.name);
        error = sysfs_create_file(&dev->kobj, &attr->attr);
    }

    return error;
}

/**
 * device_remove_file - remove sysfs attribute file.
 * @dev: device.
 * @attr: device attribute descriptor.
 */
void device_remove_file(struct device *dev,
            const struct device_attribute *attr)
{
    if (dev)
        sysfs_remove_file(&dev->kobj, &attr->attr);
}


/**
 * device_create_bin_file - create sysfs binary attribute file for device.
 * @dev: device.
 * @attr: device binary attribute descriptor.
 */
int device_create_bin_file(struct device *dev,
               const struct bin_attribute *attr)
{
    int error = -EINVAL;
    if (dev)
        error = sysfs_create_bin_file(&dev->kobj, attr);
    return error;
}

/**
 * device_remove_bin_file - remove sysfs binary attribute file
 * @dev: device.
 * @attr: device binary attribute descriptor.
 */
void device_remove_bin_file(struct device *dev,
                const struct bin_attribute *attr)
{
    if (dev)
        sysfs_remove_bin_file(&dev->kobj, attr);
}

上面这些函数是对sysfs提供的API进行简单的封装,其中device_create_file()和device_remove_file()提供直接的设备属性文件管理方法,device_create_bin_file()和device_remove_bin_file()则是提供设备管理二进制文件的方法。

static void klist_children_get(struct klist_node *n)
{
    struct device_private *p = to_device_private_parent(n);
    struct device *dev = p->device;

    get_device(dev);
}

static void klist_children_put(struct klist_node *n)
{
    struct device_private *p = to_device_private_parent(n);
    struct device *dev = p->device;

    put_device(dev);
}

klist_children_get()和klist_children_put()函数是当设备挂入和删除父设备的klist_children链表时调用的函数,相当于对设备的kobject引用计数的操作。

/**
 * get_device - increment reference count for device.
 * @dev: device.
 *
 * This simply forwards the call to kobject_get(), though
 * we do take care to provide for the case that we get a NULL
 * pointer passed in.
 */
struct device *get_device(struct device *dev)
{
    return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
}

/**
 * put_device - decrement reference count.
 * @dev: device in question.
 */
void put_device(struct device *dev)
{
    /* might_sleep(); */
    if (dev)
        kobject_put(&dev->kobj);
}

get_device()函数和put_device()用于dev的引用计数,通过内嵌的kobject来实现,当引用计数为0时,将会调用前面分析到的device_release()函数。

void device_initialize(struct device *dev)
{
    dev->kobj.kset = devices_kset;
    kobject_init(&dev->kobj, &device_ktype);
    INIT_LIST_HEAD(&dev->dma_pools);
    mutex_init(&dev->mutex);
    lockdep_set_novalidate_class(&dev->mutex);
    spin_lock_init(&dev->devres_lock);
    INIT_LIST_HEAD(&dev->devres_head);
    device_pm_init(dev);
    set_dev_node(dev, -1);
#ifdef CONFIG_GENERIC_MSI_IRQ
    INIT_LIST_HEAD(&dev->msi_list);
#endif
    INIT_LIST_HEAD(&dev->links.consumers);
    INIT_LIST_HEAD(&dev->links.suppliers);
    dev->links.status = DL_DEV_NO_DRIVER;
}

device_initialize()函数是设备在sysfs中注册的第一个阶段,用于将struct device结构体进行初始化,主要是对结构体内的一些成员进行初始化,结构体内嵌的kobject下的kset配置为devices_kset,调用kobject_init()函数设置device_ktype和sysfs_ops结构中的两个函数和device_release()函数,另外还有一些特定资源需要的成员的初始化。

static struct kobject *get_device_parent(struct device *dev,
                     struct device *parent)
{
    if (dev->class) {
        struct kobject *kobj = NULL;
        struct kobject *parent_kobj;
        struct kobject *k;

#ifdef CONFIG_BLOCK
        /* block disks show up in /sys/block */
        if (sysfs_deprecated && dev->class == &block_class) {
            if (parent && parent->class == &block_class)
                return &parent->kobj;
            return &block_class.p->subsys.kobj;
        }
#endif

        /*
         * If we have no parent, we live in "virtual".
         * Class-devices with a non class-device as parent, live
         * in a "glue" directory to prevent namespace collisions.
         */
        if (parent == NULL)
            parent_kobj = virtual_device_parent(dev);
        else if (parent->class && !dev->class->ns_type)
            return &parent->kobj;
        else
            parent_kobj = &parent->kobj;

        mutex_lock(&gdp_mutex);

        /* find our class-directory at the parent and reference it */
        spin_lock(&dev->class->p->glue_dirs.list_lock);
        list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
            if (k->parent == parent_kobj) {
                kobj = kobject_get(k);
                break;
            }
        spin_unlock(&dev->class->p->glue_dirs.list_lock);
        if (kobj) {
            mutex_unlock(&gdp_mutex);
            return kobj;
        }

        /* or create a new class-directory at the parent device */
        k = class_dir_create_and_add(dev->class, parent_kobj);
        /* do not emit an uevent for this simple "glue" directory */
        mutex_unlock(&gdp_mutex);
        return k;
    }

    /* subsystems can specify a default root directory for their devices */
    if (!parent && dev->bus && dev->bus->dev_root)
        return &dev->bus->dev_root->kobj;

    if (parent)
        return &parent->kobj;
    return NULL;
}

函数get_device_parent()用于获取父节点的kobject,get_device_parent()的返回值直接决定了device将被挂在哪个目录下,设备最终挂在的目录,是由多个因素综合决定的。

static int device_add_class_symlinks(struct device *dev)
{
    struct device_node *of_node = dev_of_node(dev);
    int error;

    if (of_node) {
        error = sysfs_create_link(&dev->kobj, of_node_kobj(of_node), "of_node");
        if (error)
            dev_warn(dev, "Error %d creating of_node link\n",error);
        /* An error here doesn't warrant bringing down the device */
    }

    if (!dev->class)
        return 0;

    error = sysfs_create_link(&dev->kobj,
                  &dev->class->p->subsys.kobj,
                  "subsystem");
    if (error)
        goto out_devnode;

    if (dev->parent && device_is_not_partition(dev)) {
        error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
                      "device");
        if (error)
            goto out_subsys;
    }

#ifdef CONFIG_BLOCK
    /* /sys/block has directories and does not need symlinks */
    if (sysfs_deprecated && dev->class == &block_class)
        return 0;
#endif

    /* link in the class directory pointing to the device */
    error = sysfs_create_link(&dev->class->p->subsys.kobj,
                  &dev->kobj, dev_name(dev));
    if (error)
        goto out_device;

    return 0;

out_device:
    sysfs_remove_link(&dev->kobj, "device");

out_subsys:
    sysfs_remove_link(&dev->kobj, "subsystem");
out_devnode:
    sysfs_remove_link(&dev->kobj, "of_node");
    return error;
}

device_add_class_symlinks()函数用于在device和class直接添加一些软链接,在device目录下创建指向class的subsystem文件,在class目录下创建指向device的同名文件,如果device有父设备,而且device不是块设备分区时,则在device目录下创建一个指向父设备的device链接文件。

static void device_remove_class_symlinks(struct device *dev)
{
    if (dev_of_node(dev))
        sysfs_remove_link(&dev->kobj, "of_node");

    if (!dev->class)
        return;

    if (dev->parent && device_is_not_partition(dev))
        sysfs_remove_link(&dev->kobj, "device");
    sysfs_remove_link(&dev->kobj, "subsystem");
#ifdef CONFIG_BLOCK
    if (sysfs_deprecated && dev->class == &block_class)
        return;
#endif
    sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
}

device_remove_class_symlinks()函数则是相反操作,用于删除device和class之间建立的软链接。

/**
 * dev_set_name - set a device name
 * @dev: device
 * @fmt: format string for the device's name
 */
int dev_set_name(struct device *dev, const char *fmt, ...)
{
    va_list vargs;
    int err;

    va_start(vargs, fmt);
    err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
    va_end(vargs);
    return err;
}

dev_set_name()函数用于设置device的名称,该函数只能在设备未注册之前使用,名称是通过dev->kobject进行管理的。

static struct kobject *device_to_dev_kobj(struct device *dev)
{
    struct kobject *kobj;

    if (dev->class)
        kobj = dev->class->dev_kobj;
    else
        kobj = sysfs_dev_char_kobj;

    return kobj;
}

该函数用于为device选择合适的/sys/dev下的kobject或者字符设备或者NULL。

#define format_dev_t(buffer, dev)                    \
    ({                                \
        sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev));    \
        buffer;                            \
    })

static int device_create_sys_dev_entry(struct device *dev)
{
    struct kobject *kobj = device_to_dev_kobj(dev);
    int error = 0;
    char devt_str[15];

    if (kobj) {
        format_dev_t(devt_str, dev->devt);
        error = sysfs_create_link(kobj, &dev->kobj, devt_str);
    }

    return error;
}

static void device_remove_sys_dev_entry(struct device *dev)
{
    struct kobject *kobj = device_to_dev_kobj(dev);
    char devt_str[15];

    if (kobj) {
        format_dev_t(devt_str, dev->devt);
        sysfs_remove_link(kobj, devt_str);
    }
}

device_create_sys_dev_entry()函数实现的功能是在/sys/dev相应的目录下创建相应设备的软链接,首先通过调用device_to_dev_kobj()函数获得父节点的kobj,然后调用sysfs_create_link()函数建立软链接,device_remove_sys_dev_entry()函数则是执行相反的操作,用于删除已经在/sys/dev下建立的软链接。

int device_private_init(struct device *dev)
{
    dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
    if (!dev->p)
        return -ENOMEM;
    dev->p->device = dev;
    klist_init(&dev->p->klist_children, klist_children_get,
           klist_children_put);
    INIT_LIST_HEAD(&dev->p->deferred_probe);
    return 0;
}

函数device_private_init()为dev->p分配内存空间并进行初始化,该内存的空间释放是在调用device_release()函数释放设备时才会释放。

上面提到到的函数都是比较零散的函数,看起来并没有什么联系,接下来继续分析一下提供给外界的接口函数实现:

/**
 * device_register - register a device with the system.
 * @dev: pointer to the device structure
 *
 * This happens in two clean steps - initialize the device
 * and add it to the system. The two steps can be called
 * separately, but this is the easiest and most common.
 * I.e. you should only call the two helpers separately if
 * have a clearly defined need to use and refcount the device
 * before it is added to the hierarchy.
 *
 * For more information, see the kerneldoc for device_initialize()
 * and device_add().
 *
 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up the
 * reference initialized in this function instead.
 */
int device_register(struct device *dev)
{
    device_initialize(dev);
    return device_add(dev);
}

首先是device_register()函数,该函数是提供给外界注册设备的接口,该函数首先调用device_initialize()函数进行结构体的变量初始化,然后调用device_add()函数将device添加到系统中,但是需要注意的是,在调用device_register()注册device之前,有一些device结构体变量需要自己设置,其中有指明设备位置的struct device *parent、struct bus_type *bus、struct class *class等,有指明设备属性的const char *init_name、struct device_type *type、const struct attribute_group **groups、dev_t devt和release()函数等,不同的设备使用的方法不同。

接下来分析device_add()函数的实现:

/**
 * device_add - add device to device hierarchy.
 * @dev: device.
 *
 * This is part 2 of device_register(), though may be called
 * separately _iff_ device_initialize() has been called separately.
 *
 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
 * to the global and sibling lists for the device, then
 * adds it to the other relevant subsystems of the driver model.
 *
 * Do not call this routine or device_register() more than once for
 * any device structure.  The driver model core is not designed to work
 * with devices that get unregistered and then spring back to life.
 * (Among other things, it's very hard to guarantee that all references
 * to the previous incarnation of @dev have been dropped.)  Allocate
 * and register a fresh new struct device instead.
 *
 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up your
 * reference instead.
 */
int device_add(struct device *dev)
{
    struct device *parent;
    struct kobject *kobj;
    struct class_interface *class_intf;
    int error = -EINVAL;
    struct kobject *glue_dir = NULL;

    dev = get_device(dev);    //增加device的引用计数
    if (!dev)
        goto done;

    if (!dev->p) {
        error = device_private_init(dev);    //分配和初始化dev->p
        if (error)
            goto done;
    }

    /*
     * for statically allocated devices, which should all be converted
     * some day, we need to initialize the name. We prevent reading back
     * the name, and force the use of dev_name()
     */
    if (dev->init_name) {
        dev_set_name(dev, "%s", dev->init_name);    //设置设备的名称
        dev->init_name = NULL;
    }

    /* subsystems can specify simple device enumeration */
    if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
        dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);

    if (!dev_name(dev)) {
        error = -EINVAL;
        goto name_error;
    }

    pr_debug("device: '%s': %s\n", dev_name(dev), __func__);

    parent = get_device(dev->parent);    //增加对parent的引用计数,无parent时返回NULL
    kobj = get_device_parent(dev, parent);    //获取父kobject
    if (IS_ERR(kobj)) {
        error = PTR_ERR(kobj);
        goto parent_error;
    }
    if (kobj)
        dev->kobj.parent = kobj;    //设置dev->kobj的父kobject

    /* use parent numa_node */
    if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
        set_dev_node(dev, dev_to_node(parent));

    /* first, register with generic layer. */
    /* we require the name to be set before, and pass NULL */
    error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);    //将device内嵌的kobj加入到kobject层次结构中
    if (error) {
        glue_dir = get_glue_dir(dev);
        goto Error;
    }

    /* notify platform of device entry */
    if (platform_notify)
        platform_notify(dev);

    error = device_create_file(dev, &dev_attr_uevent);    //添加uevent属性文件
    if (error)
        goto attrError;

    error = device_add_class_symlinks(dev);    //dev与class软链接创建
    if (error)
        goto SymlinkError;
    error = device_add_attrs(dev);    //添加属性
    if (error)
        goto AttrsError;
    error = bus_add_device(dev);    //将设备添加到总线上,创建dev与bus间的软链接
    if (error)
        goto BusError;
    error = dpm_sysfs_add(dev);        //增加dev下的power属性集合
    if (error)
        goto DPMError;
    device_pm_add(dev);

    if (MAJOR(dev->devt)) {        //主设备号存在
        error = device_create_file(dev, &dev_attr_dev);    //添加dev属性
        if (error)
            goto DevAttrError;

        error = device_create_sys_dev_entry(dev);    //在/sys/dev下添加相应的软链接
        if (error)
            goto SysEntryError;

        devtmpfs_create_node(dev);    //在/dev下添加相应的设备节点
    }

    /* Notify clients of device addition.  This call must come
     * after dpm_sysfs_add() and before kobject_uevent().
     */
    if (dev->bus)
        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
                         BUS_NOTIFY_ADD_DEVICE, dev);

    kobject_uevent(&dev->kobj, KOBJ_ADD);    //kobject发布KOBJ_ADD消息到用户空间
    bus_probe_device(dev);        //为device寻找合适的驱动
    if (parent)
        klist_add_tail(&dev->p->knode_parent,
                   &parent->p->klist_children);    //如果父节点存在,将挂入到klist_children链表

    if (dev->class) {    //如果device所属的class存在
        mutex_lock(&dev->class->p->mutex);
        /* tie the class to the device */
        klist_add_tail(&dev->knode_class,
                   &dev->class->p->klist_devices);    //节点插入链表

        /* notify any interfaces that the device is here */
        list_for_each_entry(class_intf,
                    &dev->class->p->interfaces, node)
            if (class_intf->add_dev)
                class_intf->add_dev(dev, class_intf);
        mutex_unlock(&dev->class->p->mutex);
    }
done:
    put_device(dev);    //减少device的引用计数
    return error;
 SysEntryError:
    if (MAJOR(dev->devt))
        device_remove_file(dev, &dev_attr_dev);
 DevAttrError:
    device_pm_remove(dev);
    dpm_sysfs_remove(dev);
 DPMError:
    bus_remove_device(dev);
 BusError:
    device_remove_attrs(dev);
 AttrsError:
    device_remove_class_symlinks(dev);
 SymlinkError:
    device_remove_file(dev, &dev_attr_uevent);
 attrError:
    kobject_uevent(&dev->kobj, KOBJ_REMOVE);
    glue_dir = get_glue_dir(dev);
    kobject_del(&dev->kobj);
 Error:
    cleanup_glue_dir(dev, glue_dir);
parent_error:
    put_device(parent);
name_error:
    kfree(dev->p);
    dev->p = NULL;
    goto done;
}

函数device_add()用于将dev添加到设备驱动模型中去,它先调用get_device()来增加dev的引用计数,然后调用device_private_init()进行dev->p的分配和初始化,调用dev_set_name()对dev的名字进行设置,接下来,要做的是准备将dev添加到sysfs设备文件系统中去,首先是调用get_device()增加对paren的引用计数(无论是直接挂在parent下还是通过一个类层挂在parent下都要增加parent的引用计数),然后调用get_device_parent()找到实际要加入的父kobject,并调用kobject_add()将dev->kobj加入到dev->kobj.parent的层次结构中去,接下来是完成属性和属性集合的添加,调用device_create_file()添加uevent属性文件,然后调用device_add_class_symlinks()在dev下创建一个软链接subsystem,指向相对应的class,然后继续调用device_add_attrs()添加属性和属性集合,调用bus_add_device()添加设备的总线属性,在dev与bus之间创建软链接,并将dev挂入到总线的设备链表中去,dpm_sysfs_add()用于增加dev下的power属性集合,调用device_pm_add()将设备添加到dpm_list链表中去。如果设备被分配了主设备号,调用device_create_file()添加dev属性文件,然后调用device_create_sys_dev_entry()在/sys/dev下创建相应的软链接,调用devtmpfs_create_node()在/dev下添加对应的设备节点文件。函数开始调用kobject_uevent()向用户空间发布KOBJ_ADD消息通知,并调用bus_probe_device()为设备探测寻找合适的驱动程序,如果设备有父节点的话,则把dev->p->knode_parent挂入到parent->p->klist_children链表中,如果设备有所属的class,则将dev->knode_class挂入class->p>class_devices上,并调用可能的类设备接口add_dev()函数,对于直接在bus上的设备来讲,可以调用bus_probe_device()来查找驱动程序,但是不与bus直接接触的设备,则靠class来去寻找驱动,便使用了class_interface内的add_dev()方式,函数最后调用put_device()减少在开头增加的引用计数并返回。

/**
 * device_unregister - unregister device from system.
 * @dev: device going away.
 *
 * We do this in two parts, like we do device_register(). First,
 * we remove it from all the subsystems with device_del(), then
 * we decrement the reference count via put_device(). If that
 * is the final reference count, the device will be cleaned up
 * via device_release() above. Otherwise, the structure will
 * stick around until the final reference to the device is dropped.
 */
void device_unregister(struct device *dev)
{
    pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
    device_del(dev);
    put_device(dev);
}

有设备注册函数,肯定也有设备注销函数,device_unregister()函数实现的功能为将dev从系统中注销,并减少创建时产生的引用计数,当引用计数为0时,将销毁dev。

/**
 * device_del - delete device from system.
 * @dev: device.
 *
 * This is the first part of the device unregistration
 * sequence. This removes the device from the lists we control
 * from here, has it removed from the other driver model
 * subsystems it was added to in device_add(), and removes it
 * from the kobject hierarchy.
 *
 * NOTE: this should be called manually _iff_ device_add() was
 * also called manually.
 */
void device_del(struct device *dev)
{
    struct device *parent = dev->parent;
    struct kobject *glue_dir = NULL;
    struct class_interface *class_intf;

    /* Notify clients of device removal.  This call must come
     * before dpm_sysfs_remove().
     */
    if (dev->bus)
        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
                         BUS_NOTIFY_DEL_DEVICE, dev);

    dpm_sysfs_remove(dev);    //将sysfs下的power属性集合移除
    if (parent)            //如果存在父节点
        klist_del(&dev->p->knode_parent);    //将设备节点从父节点链表中移除
    if (MAJOR(dev->devt)) {            //如果分配了主设备号
        devtmpfs_delete_node(dev);        //将/dev下的设备节点文件移除
        device_remove_sys_dev_entry(dev);    ///sys/dev下的软链接取消
        device_remove_file(dev, &dev_attr_dev);    //将属性文件移除
    }
    if (dev->class) {
        device_remove_class_symlinks(dev);

        mutex_lock(&dev->class->p->mutex);
        /* notify any interfaces that the device is now gone */
        list_for_each_entry(class_intf,
                    &dev->class->p->interfaces, node)
            if (class_intf->remove_dev)
                class_intf->remove_dev(dev, class_intf);
        /* remove the device from the class list */
        klist_del(&dev->knode_class);
        mutex_unlock(&dev->class->p->mutex);
    }
    device_remove_file(dev, &dev_attr_uevent);    //移除uevent属性文件
    device_remove_attrs(dev);    //属性及属性集合移除
    bus_remove_device(dev);        //总线上移除设备
    device_pm_remove(dev);        //将dev从dpm_list中移除
    driver_deferred_probe_del(dev);        //驱动移除
    device_remove_properties(dev);
    device_links_purge(dev);

    /* Notify the platform of the removal, in case they
     * need to do anything...
     */
    if (platform_notify_remove)
        platform_notify_remove(dev);
    if (dev->bus)
        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
                         BUS_NOTIFY_REMOVED_DEVICE, dev);
    kobject_uevent(&dev->kobj, KOBJ_REMOVE);    //kobject发布KOBJ_REMOVE消息到用户空间
    glue_dir = get_glue_dir(dev);
    kobject_del(&dev->kobj);    //将内嵌的kobj从层次结构中移除
    cleanup_glue_dir(dev, glue_dir);
    put_device(parent);        //父节点引用计数减1操作
}

函数device_del()是与device_add()相对的函数,device_add()将设备添加到系统中,device_del()则是将设备从系统中移除,包括了将dev从设备驱动模型的各种klist链表中进行脱离,又将dev从sysfs的各个地方创建的文件进行删除的工作。

static struct device *prev_device(struct klist_iter *i)    //返回前一个设备
{
    struct klist_node *n = klist_prev(i);
    struct device *dev = NULL;
    struct device_private *p;

    if (n) {
        p = to_device_private_parent(n);//返回device_private结构体的首地址
        dev = p->device;//将struct device结构体地址返回
    }
    return dev;
}

static struct device *next_device(struct klist_iter *i)
{
    struct klist_node *n = klist_next(i);
    struct device *dev = NULL;
    struct device_private *p;

    if (n) {
        p = to_device_private_parent(n);
        dev = p->device;
    }
    return dev;
}

内部函数prev_device()和next_device()用于device的klist的链表遍历,prev_device()将返回前一个设备,next_device()将返回下一个设备。

/**
 * device_get_devnode - path of device node file
 * @dev: device
 * @mode: returned file access mode
 * @uid: returned file owner
 * @gid: returned file group
 * @tmp: possibly allocated string
 *
 * Return the relative path of a possible device node.
 * Non-default names may need to allocate a memory to compose
 * a name. This memory is returned in tmp and needs to be
 * freed by the caller.
 */
const char *device_get_devnode(struct device *dev,
                   umode_t *mode, kuid_t *uid, kgid_t *gid,
                   const char **tmp)
{
    char *s;

    *tmp = NULL;

    /* the device type may provide a specific name */
    if (dev->type && dev->type->devnode)
        *tmp = dev->type->devnode(dev, mode, uid, gid);
    if (*tmp)
        return *tmp;

    /* the class may provide a specific name */
    if (dev->class && dev->class->devnode)
        *tmp = dev->class->devnode(dev, mode);
    if (*tmp)
        return *tmp;

    /* return name without allocation, tmp == NULL */
    if (strchr(dev_name(dev), '!') == NULL)
        return dev_name(dev);

    /* replace '!' in the name with '/' */
    s = kstrdup(dev_name(dev), GFP_KERNEL);
    if (!s)
        return NULL;
    strreplace(s, '!', '/');
    return *tmp = s;
}

函数device_get_devnode()用于返回设备的路径名。

/**
 * device_for_each_child - device child iterator.
 * @parent: parent struct device.
 * @fn: function to be called for each device.
 * @data: data for the callback.
 *
 * Iterate over @parent's child devices, and call @fn for each,
 * passing it @data.
 *
 * We check the return of @fn each time. If it returns anything
 * other than 0, we break out and return that value.
 */
int device_for_each_child(struct device *parent, void *data,
              int (*fn)(struct device *dev, void *data))
{
    struct klist_iter i;
    struct device *child;
    int error = 0;

    if (!parent->p)
        return 0;

    klist_iter_init(&parent->p->klist_children, &i);//迭代器初始化,从链表头开始
    while ((child = next_device(&i)) && !error)    //正序遍历klist_children链表
        error = fn(child, data);
    klist_iter_exit(&i);
    return error;
}

int device_for_each_child_reverse(struct device *parent, void *data,
                  int (*fn)(struct device *dev, void *data))
{
    struct klist_iter i;
    struct device *child;
    int error = 0;

    if (!parent->p)
        return 0;

    klist_iter_init(&parent->p->klist_children, &i);
    while ((child = prev_device(&i)) && !error)    //逆序遍历klist_children链表
        error = fn(child, data);
    klist_iter_exit(&i);
    return error;
}

struct device *device_find_child(struct device *parent, void *data,
                 int (*match)(struct device *dev, void *data))
{
    struct klist_iter i;
    struct device *child;

    if (!parent)
        return NULL;

    klist_iter_init(&parent->p->klist_children, &i);
    while ((child = next_device(&i)))
        if (match(child, data) && get_device(child))
            break;
    klist_iter_exit(&i);
    return child;
}

在上面的函数都是对设备链表的遍历,device_for_each_child()函数和device_for_each_child_reverse()函数对父设备下的子设备进行遍历,并都调用一个特定的函数fn()进行处理,device_find_child()函数则是查找特定的子设备,查找使用特定match()函数进行匹配。

接下来,继续分析动态创建struct device的方法,其原理和kobject和kset的动态创建类似:

static void device_create_release(struct device *dev)
{
    pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
    kfree(dev);
}

static struct device *
device_create_groups_vargs(struct class *class, struct device *parent,
               dev_t devt, void *drvdata,
               const struct attribute_group **groups,
               const char *fmt, va_list args)
{
    struct device *dev = NULL;
    int retval = -ENODEV;

    if (class == NULL || IS_ERR(class))//判断class指针是否有效
        goto error;

    dev = kzalloc(sizeof(*dev), GFP_KERNEL);//为dev动态分配内存
    if (!dev) {
        retval = -ENOMEM;
        goto error;
    }

    device_initialize(dev);//设备初始化
    dev->devt = devt;
    dev->class = class;
    dev->parent = parent;
    dev->groups = groups;
    dev->release = device_create_release;
    dev_set_drvdata(dev, drvdata);

    retval = kobject_set_name_vargs(&dev->kobj, fmt, args);//设置kobject的name
    if (retval)
        goto error;

    retval = device_add(dev);//将设备添加到sysfs层次系统
    if (retval)
        goto error;

    return dev;

error:
    put_device(dev);
    return ERR_PTR(retval);//将异常指针返回
}

/**
 * device_create_vargs - creates a device and registers it with sysfs
 * @class: pointer to the struct class that this device should be registered to
 * @parent: pointer to the parent struct device of this new device, if any
 * @devt: the dev_t for the char device to be added
 * @drvdata: the data to be added to the device for callbacks
 * @fmt: string for the device's name
 * @args: va_list for the device's name
 *
 * This function can be used by char device classes.  A struct device
 * will be created in sysfs, registered to the specified class.
 *
 * A "dev" file will be created, showing the dev_t for the device, if
 * the dev_t is not 0,0.
 * If a pointer to a parent struct device is passed in, the newly created
 * struct device will be a child of that device in sysfs.
 * The pointer to the struct device will be returned from the call.
 * Any further sysfs files that might be required can be created using this
 * pointer.
 *
 * Returns &struct device pointer on success, or ERR_PTR() on error.
 *
 * Note: the struct class passed to this function must have previously
 * been created with a call to class_create().
 */
struct device *device_create_vargs(struct class *class, struct device *parent,
                   dev_t devt, void *drvdata, const char *fmt,
                   va_list args)
{
    return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
                      fmt, args);
}

struct device *device_create(struct class *class, struct device *parent,
                 dev_t devt, void *drvdata, const char *fmt, ...)
{
    va_list vargs;
    struct device *dev;

    va_start(vargs, fmt);
    dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
    va_end(vargs);
    return dev;
}

struct device *device_create_with_groups(struct class *class,
                     struct device *parent, dev_t devt,
                     void *drvdata,
                     const struct attribute_group **groups,
                     const char *fmt, ...)
{
    va_list vargs;
    struct device *dev;

    va_start(vargs, fmt);
    dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
                     fmt, vargs);
    va_end(vargs);
    return dev;
}

在上面代码中,device_create_release()和device_create_groups_vargs()属于两个内部的静态函数,第一个函数用于释放device分配的内核空间,由于dev是动态分配的,而第二个函数则是用来动态创建一个device,函数首先对传入的class进行判断,然后对device进行内存分配,分配成功后就是开始调用device_initialize()对设备初始化,并手动对device的一些成员进行赋值,然后调用kobject_set_name_vargs()对device中嵌入的kobject进行名称设置,最后,则是调用device_add()函数将动态创建的device添加到sysfs层次系统,而device_create_vargs()、device_create()和device_create_with_groups()都是对device_create_groups_vargs()的进一步封装,device_create()和device_create_with_groups()的区别在于创建的时候是否要创建device的组属性文件。

static int __match_devt(struct device *dev, const void *data)
{
    const dev_t *devt = data;

    return dev->devt == *devt;//设备号匹配
}

/**
 * device_destroy - removes a device that was created with device_create()
 * @class: pointer to the struct class that this device was registered with
 * @devt: the dev_t of the device that was previously registered
 *
 * This call unregisters and cleans up a device that was created with a
 * call to device_create().
 */
void device_destroy(struct class *class, dev_t devt)
{
    struct device *dev;

    dev = class_find_device(class, NULL, &devt, __match_devt);//在class下寻找设备
    if (dev) {
        put_device(dev);//减少引用计数
        device_unregister(dev);//注销设备
    }
}

device_create()用于动态创建一个设备,而device_destroy()函数则用来销毁一个device_create()创建出来的设备,__match_devt()属于内部的静态函数,用于class_find_device()函数寻找需要销毁的device,主要是通过设备号进行匹配而寻找,之所以需要使用put_device()减少引用计数,是因为使用class_find_device()中调用了get_device()增加了引用计数。

int device_rename(struct device *dev, const char *new_name)
{
    struct kobject *kobj = &dev->kobj;
    char *old_device_name = NULL;
    int error;

    dev = get_device(dev);
    if (!dev)
        return -EINVAL;

    dev_dbg(dev, "renaming to %s\n", new_name);

    old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
    if (!old_device_name) {
        error = -ENOMEM;
        goto out;
    }

    if (dev->class) {
        error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
                         kobj, old_device_name,
                         new_name, kobject_namespace(kobj));
        if (error)
            goto out;
    }

    error = kobject_rename(kobj, new_name);
    if (error)
        goto out;

out:
    put_device(dev);

    kfree(old_device_name);

    return error;
}

函数device_rename()是当设备在sysfs中注册后,用来改变设备的名称用的,首先改变/sys/class目录下的软链接的名称,然后使用kobject_rename()将device下嵌入的kobject进行重新命名。

/**
 * device_shutdown - call ->shutdown() on each device to shutdown.
 */
void device_shutdown(void)
{
    struct device *dev, *parent;

    spin_lock(&devices_kset->list_lock);
    /*
     * Walk the devices list backward, shutting down each in turn.
     * Beware that device unplug events may also start pulling
     * devices offline, even as the system is shutting down.
     */
    while (!list_empty(&devices_kset->list)) {
        dev = list_entry(devices_kset->list.prev, struct device,
                kobj.entry);

        /*
         * hold reference count of device's parent to
         * prevent it from being freed because parent's
         * lock is to be held
         */
        parent = get_device(dev->parent);
        get_device(dev);
        /*
         * Make sure the device is off the kset list, in the
         * event that dev->*->shutdown() doesn't remove it.
         */
        list_del_init(&dev->kobj.entry);
        spin_unlock(&devices_kset->list_lock);

        /* hold lock to avoid race with probe/release */
        if (parent)
            device_lock(parent);
        device_lock(dev);

        /* Don't allow any more runtime suspends */
        pm_runtime_get_noresume(dev);
        pm_runtime_barrier(dev);

        if (dev->class && dev->class->shutdown_pre) {
            if (initcall_debug)
                dev_info(dev, "shutdown_pre\n");
            dev->class->shutdown_pre(dev);
        }
        if (dev->bus && dev->bus->shutdown) {
            if (initcall_debug)
                dev_info(dev, "shutdown\n");
            dev->bus->shutdown(dev);
        } else if (dev->driver && dev->driver->shutdown) {
            if (initcall_debug)
                dev_info(dev, "shutdown\n");
            dev->driver->shutdown(dev);
        }

        device_unlock(dev);
        if (parent)
            device_unlock(parent);

        put_device(dev);
        put_device(parent);

        spin_lock(&devices_kset->list_lock);
    }
    spin_unlock(&devices_kset->list_lock);
}

函数device_shutdown()用来关闭sysfs上的每个设备,它在系统关闭时才会进行调用,在函数内,使用了devices_kset这个顶层kset,所在的目录为/sys/devices,因此,函数调用会遍历到注册的到sysfs上的每个设备,调用设备相应的总线或驱动定义的shutdown()函数,每个设备虽然可以有不同的parent,但是kset还是一样的,当在调用kobject_add()函数时,将devices_kset这个kset->kobj设置成parent,那么新添加的kobject就会挂在/sys/devices顶层目录下,例如virtual目录等。

 

4、小结

在内核中,struct device结构体的实现非常地复杂,它是Linux内核设备驱动模型的基础,为了适应越来越复杂的情景,以及提高设备的驱动性能,其实现将会越来越复杂,对其分析点到为止。

 

参考:

《LINUX设备驱动程序(第三版)》

https://blog.csdn.net/qb_2008/article/details/6847133

https://blog.csdn.net/abo8888882006/article/details/5424363

转载于:https://www.cnblogs.com/Cqlismy/p/11507216.html

 类似资料: