linux核心學習(21)裝置模型之第二層device_register

來源:互聯網
上載者:User

這篇文章將那個3個註冊函數說說,把整個裝置模型架構搭建起來,當然,是重點部分了。在這之前希望你已經懂得匯流排、裝置、驅動的資料結構及其裡面的有關資料結構。關於調用的函數,如果顯示為粗體,那麼在下面我有分析。

來自:drivers/base/core.c
int device_register(struct device *dev)
{
    device_initialize(dev);
//初始化裝置
    return device_add(dev);              //添加裝置
}

void device_initialize(struct device *dev)
{
    dev->kobj.kset = devices_kset;            //設定裝置的kobject所屬集合,devices_kset其實在第一層,sys/devices/
    kobject_init(&dev->kobj, &device_ktype);       //初始化裝置的kobject
    INIT_LIST_HEAD(&dev->dma_pools);           //初始化裝置的DMA池,用於傳遞大資料
    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);
}

int device_add(struct device *dev)
{
    struct device *parent = NULL;
    struct class_interface *class_intf;
    int error = -EINVAL;

    dev = get_device(dev);         //增加裝置的kobject的引用計數
    if (!dev)
        goto done;

    if (!dev->p) {                 
        error = device_private_init(dev);             //初始化裝置的私人成員
        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);         //設定裝置kobject的名稱
        dev->init_name = NULL;
    }

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

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

    parent = get_device(dev->parent);         //增加父裝置kobject的引用
    setup_parent(dev, parent);                //設定該裝置kobject父物件

    /* use parent numa_node */
    if (parent)
        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);          //將裝置kobject添加進父物件裝置模型
    if (error)
        goto Error;

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

    error = device_create_file(dev, &uevent_attr);
    if (error)
        goto attrError;

    if (MAJOR(dev->devt)) {
        error = device_create_file(dev, &devt_attr);
        if (error)
            goto ueventattrError;

        error = device_create_sys_dev_entry(dev);
        if (error)
            goto devtattrError;

        devtmpfs_create_node(dev);
    }

    error = device_add_class_symlinks(dev);
    if (error)
        goto SymlinkError;
    error = device_add_attrs(dev);
    if (error)
        goto AttrsError;
    error = bus_add_device(dev);          //將裝置添加進匯流排中
    if (error)
        goto BusError;
    error = dpm_sysfs_add(dev);
    if (error)
        goto DPMError;
    device_pm_add(dev);

    /* Notify clients of device addition.  This call must come
     * after dpm_sysf_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);
    bus_probe_device(dev);               //現在該為裝置在匯流排上尋找合適的驅動了
    if (parent)
        klist_add_tail(&dev->p->knode_parent,
                   &parent->p->klist_children);         //將裝置添加到父裝置的子裝置鏈表中

    if (dev->class) {
        mutex_lock(&dev->class->p->class_mutex);
        /* tie the class to the device */
        klist_add_tail(&dev->knode_class,
                   &dev->class->p->class_devices);

        /* notify any interfaces that the device is here */
        list_for_each_entry(class_intf,
                    &dev->class->p->class_interfaces, node)
            if (class_intf->add_dev)
                class_intf->add_dev(dev, class_intf);
        mutex_unlock(&dev->class->p->class_mutex);
    }
done:
    put_device(dev);
    return error;
 DPMError:
    bus_remove_device(dev);
 BusError:
    device_remove_attrs(dev);
 AttrsError:
    device_remove_class_symlinks(dev);
 SymlinkError:
    if (MAJOR(dev->devt))
        devtmpfs_delete_node(dev);
    if (MAJOR(dev->devt))
        device_remove_sys_dev_entry(dev);
 devtattrError:
    if (MAJOR(dev->devt))
        device_remove_file(dev, &devt_attr);
 ueventattrError:
    device_remove_file(dev, &uevent_attr);
 attrError:
    kobject_uevent(&dev->kobj, KOBJ_REMOVE);
    kobject_del(&dev->kobj);
 Error:
    cleanup_device_parent(dev);
    if (parent)
        put_device(parent);
name_error:
    kfree(dev->p);
    dev->p = NULL;
    goto done;
}

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);             //初始化裝置私人成員的子裝置鏈表,還有兩個函數,關於增加和減少子裝置引用計數的
    return 0;
}

static void setup_parent(struct device *dev, struct device *parent)
{
    struct kobject *kobj;
    kobj = get_device_parent(dev, parent);        //得到裝置kobject的父物件
    if (kobj)
        dev->kobj.parent = kobj;
}

int bus_add_device(struct device *dev)
{
    struct bus_type *bus = bus_get(dev->bus);
    int error = 0;

    if (bus) {
        pr_debug("bus: '%s': add device %s/n", bus->name, dev_name(dev));
        error = device_add_attrs(bus, dev);
        if (error)
            goto out_put;
        error = sysfs_create_link(&bus->p->devices_kset->kobj,
                        &dev->kobj, dev_name(dev));
        if (error)
            goto out_id;
        error = sysfs_create_link(&dev->kobj,
                &dev->bus->p->subsys.kobj, "subsystem");
        if (error)
            goto out_subsys;
        error = make_deprecated_bus_links(dev);
        if (error)
            goto out_deprecated;
        klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices);      //關鍵點了,將裝置添加進匯流排的裝置鏈表
    }
    return 0;

out_deprecated:
    sysfs_remove_link(&dev->kobj, "subsystem");
out_subsys:
    sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev));
out_id:
    device_remove_attrs(bus, dev);
out_put:
    bus_put(dev->bus);
    return error;
}

void bus_probe_device(struct device *dev)
{
    struct bus_type *bus = dev->bus;
    int ret;

    if (bus && bus->p->drivers_autoprobe) {        //如果需要自動匹配驅動
        ret = device_attach(dev);              //為裝置尋找驅動
        WARN_ON(ret < 0);
    }
}

int device_attach(struct device *dev)
{
    int ret = 0;

    device_lock(dev);          //鎖住裝置
    if (dev->driver) {            //如果裝置有驅動
        ret = device_bind_driver(dev);          //那麼將裝置和驅動綁定
        if (ret == 0)
            ret = 1;
        else {
            dev->driver = NULL;
            ret = 0;
        }
    } else {
        pm_runtime_get_noresume(dev);
        ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);     
//否則,在匯流排上尋找驅動與該裝置進行匹配
        pm_runtime_put_sync(dev);
    }
    device_unlock(dev);
    return ret;
}

int device_bind_driver(struct device *dev)
{
    int ret;

    ret = driver_sysfs_add(dev);   
    if (!ret)
        driver_bound(dev);   //驅動綁定裝置
    return ret;
}

int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
             void *data, int (*fn)(struct device_driver *, void *))
{
    struct klist_iter i;
    struct device_driver *drv;
    int error = 0;

    if (!bus)
        return -EINVAL;

    klist_iter_init_node(&bus->p->klist_drivers, &i,
                 start ? &start->p->knode_bus : NULL);   //初始化i結構體
    while ((drv = next_driver(&i)) && !error)   //遍曆匯流排上的驅動
        error = fn(drv, data);           //將驅動和裝置進行匹配,這裡的fn=__device_attach
    klist_iter_exit(&i);
    return error;
}

static int __device_attach(struct device_driver *drv, void *data)
{
    struct device *dev = data;

    if (!driver_match_device(drv, dev))     //現用匯流排上的match匹配函數進行低級匹配
        return 0;

    return driver_probe_device(drv, dev);   //在來進階匹配
}

static inline int driver_match_device(struct device_driver *drv,
                      struct device *dev)
{
    return drv->bus->match ? drv->bus->match(dev, drv) : 1;
//看到沒,這裡要調用匯流排上定義的match函數
}

int driver_probe_device(struct device_driver *drv, struct device *dev)
{
    int ret = 0;

    if (!device_is_registered(dev))  //裝置是否註冊
        return -ENODEV;

    pr_debug("bus: '%s': %s: matched device %s with driver %s/n",
         drv->bus->name, __func__, dev_name(dev), drv->name);

    pm_runtime_get_noresume(dev);
    pm_runtime_barrier(dev);
    ret = really_probe(dev, drv);  //調用真正的匹配
    pm_runtime_put_sync(dev);

    return ret;
}

static int really_probe(struct device *dev, struct device_driver *drv)
{
    int ret = 0;

    atomic_inc(&probe_count);
    pr_debug("bus: '%s': %s: probing driver %s with device %s/n",
         drv->bus->name, __func__, drv->name, dev_name(dev));
    WARN_ON(!list_empty(&dev->devres_head));

    dev->driver = drv;
    if (driver_sysfs_add(dev)) {
        printk(KERN_ERR "%s: driver_sysfs_add(%s) failed/n",
            __func__, dev_name(dev));
        goto probe_failed;
    }

    if (dev->bus->probe) {         //現用匯流排上定義的probe函數嘗試一下
        ret = dev->bus->probe(dev);
        if (ret)
            goto probe_failed;
    } else if (drv->probe) {       //如果不行,在用驅動上的probe嘗試
        ret = drv->probe(dev);
        if (ret)
            goto probe_failed;
    }

    driver_bound(dev);      //驅動綁定裝置
    ret = 1;
    pr_debug("bus: '%s': %s: bound device %s to driver %s/n",
         drv->bus->name, __func__, dev_name(dev), drv->name);
    goto done;

probe_failed:
    devres_release_all(dev);
    driver_sysfs_remove(dev);
    dev->driver = NULL;

    if (ret != -ENODEV && ret != -ENXIO) {
        /* driver matched but the probe failed */
        printk(KERN_WARNING
               "%s: probe of %s failed with error %d/n",
               drv->name, dev_name(dev), ret);
    }
    /*
     * Ignore errors returned by ->probe so that the next driver can try
     * its luck.
     */
    ret = 0;
done:
    atomic_dec(&probe_count);
    wake_up(&probe_waitqueue);
    return ret;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.