Linux Bus/Device/Driver

Overview

Linux Driver Model里面有抽象出Bus,device和driver。Device和Driver都要归属于某一个bus,bus会根据device或driver的注册来遍历匹配的driver或device。

1. bus_register()主要是在sysfs中bus下创建对应的目录,并初始化bus structure中的一些成员。

2. driver_register()主要是在sysfs对应的bus目录下创建对应的目录,同时遍历bus下的device,通过bus的match function进行匹配,如果bus的 match为NULL,则默认match。然后再看device是否已经有bind driver,如果没有则优先调用bus的probe,如果bus的probe为NULL,则调用driver的probe function,同时把device加入到driver的device list,把driver也赋值给device->drv。

3. device_register()会创建sysfs目录,同时遍历bus下的driver,通过bus的match function进行匹配,如果bus的 match为NULL,则默认match。然后再看device是否已经有bind driver,如果没有则优先调用bus的probe,如果bus的probe为NULL,则调用driver的probe function。同时把device加入到driver的device list,把driver也赋值给device->drv。

Bus

A bus is a channel between the processor and one or more devices. For the purposes of the device model, all devices are connected via a bus, even if it is an internal, virtual, "platform" bus. Buses can plug into each other. A USB controller is usually a PCI device, for example. The device model represents the actual connections between buses and the devices they control. A bus is represented by the bus_type structure. It contains the name, the default attributes, the bus' methods, PM operations, and the driver core's private data.

struct bus_type {

    const char *name;  //The name of the bus

    const char *dev_name; //Used for subsystems to enumerate devices like ("foo%u", dev->id).

    struct device *dev_root; //Default device to use as the parent.

    int (*match)(struct device *dev, struct device_driver *drv);

    int (*probe)(struct device *dev);

    const struct dev_pm_ops *pm;

    const struct iommu_ops *iommu_ops;

    struct subsys_private *p;

    ...

};

The subsys_private structure is the one that is the actual kobject allowing struct bus_type/class to be statically allocated safely. Nothing outside of the * driver core should ever touch these fields.

struct subsys_private

    struct kset subsys; //the struct kset that defines this subsystem

    struct kset *devices_kset; //the subsystem's 'devices' directory

    struct list_head interfaces; 

    struct mutex mutex; 

    struct kset *drivers_kset;       

    struct klist klist_devices

    struct klist klist_drivers

    struct blocking_notifier_head bus_notifier; 

    unsigned int drivers_autoprobe:1; 

    struct bus_type *bus; 

    struct kset glue_dirs;

    struct class *class;       

};  

bus_register()

Register a driver-core subsystem. Once we have that, we register the bus with the kobject

infrastructure, then register the children subsystems it has:the devices and drivers that belong to the subsystem.

int bus_register(struct bus_type *bus){

    struct subsys_private *priv;

    priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);

    priv->bus = bus; 

    bus->p = priv;

    retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);// set the kset's kobject name

    priv->subsys.kobj.kset = bus_kset; //setting the kobject's kset as bus_kset, so the directory will under bus directory in sysfs.

    priv->subsys.kobj.ktype = &bus_ktype; //setting the ktype

    priv->drivers_autoprobe = 1; 

    retval = kset_register(&priv->subsys);//初始化kset,同时create dir in sysfs,这是会在sysfs 中bus目录下创建一个新的目录

    retval = bus_create_file(bus, &bus_attr_uevent); //在新create的目录下创建uevent文件

    priv->devices_kset = kset_create_and_add("devices", NULL, &priv->subsys.kobj);//在目录下创建devices目录

    priv->devices_kset = kset_create_and_add("devices", NULL, &priv->subsys.kobj);//创建drivers目录

    klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);  //list initialize

    klist_init(&priv->klist_drivers, NULL, NULL); //list init

    retval = add_probe_files(bus); //创建probe file

    return 0;

}

Device driver

The basic device driver structure.

The device driver-model tracks all of the drivers known to the system.The main reason for this tracking is to enable the driver core to match up drivers with new devices. Once drivers are known objects within the system, however, a number of other things become possible. Device drivers can export information and configuration variables that are independent of any specific device.

struct device_driver {

    const char *name; //Name of the device driver

    struct bus_type *bus; //The bus which the device of this driver belongs to

    struct module *owner; //Type of the probe (synchronous or asynchronous) to use.

    enum probe_type probe_type; //Type of the probe (synchronous or asynchronous) to use.

    int (*probe) (struct device *dev); //Called to query the existence of a specific device, * whether this driver can work with it, and bind the driver * to a specific device.

    struct driver_private *p;

};

struct driver_private {

    struct kobject kobj;

    struct klist klist_devices;

    struct klist_node knode_bus;

    struct module_kobject *mkobj;

    struct device_driver *driver;

};

driver_register()

Register driver with bus.

int driver_register(struct device_driver *drv)

{

    int ret;

    struct device_driver *other;

    other = driver_find(drv->name, drv->bus);//Find if a driver already registered by kobject name

    if (other) {

        return -EBUSY;

    }

    ret = bus_add_driver(drv);

    ret = driver_add_groups(drv, drv->groups);

    kobject_uevent(&drv->p->kobj, KOBJ_ADD);

    return ret;

}

int bus_add_driver(struct device_driver *drv) 

    struct bus_type *bus; 

    struct driver_private *priv; 

    bus = bus_get(drv->bus);    //increase the reference count of drv->bus->p->subsys->kobject

    priv = kzalloc(sizeof(*priv), GFP_KERNEL);

    klist_init(&priv->klist_devices, NULL, NULL); //Initialize devices list

    priv->driver = drv; 

    drv->p = priv; 

    priv->kobj.kset = bus->p->drivers_kset; //set the driver kobject's kset as the bus drivers_set

   error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, "%s", drv->name); //Due to the parent is NULL, so the drivers kobject parent is the kset's object which is bus->drivers_set. So create a dir under sysfs bus/*/drivers/

    klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);//add driver in bus drivers list

    if (drv->bus->p->drivers_autoprobe) {

        error = driver_attach(drv);

    }

    module_add_driver(drv->owner, drv);

    error = driver_create_file(drv, &driver_attr_uevent); //create uevent file under driver directory in sysfs

    return 0;

}

int driver_attach(struct device_driver *drv)

{

    return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);

}

* bus_for_each_dev - device iterator.

* @bus: bus type.

* @start: device to start iterating from.

* @data: data for the callback.

* @fn: function to be called for each device.

int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int (*fn)(struct device *, void *))

{

    struct klist_iter i;

    struct device *dev;

    int error = 0;

    klist_iter_init_node(&bus->p->klist_devices, &i, (start ? &start->p->knode_bus : NULL));

    while ((dev = next_device(&i)) && !error) //iterate the bus device list

        error = fn(dev, data); //invoke __driver_attach() function

    klist_iter_exit(&i);

    return error;

}

static int __driver_attach(struct device *dev, void *data)

{

    struct device_driver *drv = data;

    if (!driver_match_device(drv, dev))

        return 0;

    device_lock(dev); //mutex_lock(&dev->mutex)

    //这里可以看到只有当device的driver为NULL时,也即是device还没有bind对应的driver时,才会继续执行driver的probe

    if (!dev->driver)

        driver_probe_device(drv, dev);

    device_unlock(dev);

    return 0;

}

static inline int driver_match_device(struct device_driver *drv, struct device *dev)

{

    //如果driver的bus有match function,那么调用bus的match function,否则默认认为match, //如果不match,则直接跳到下一个device

    return drv->bus->match ? drv->bus->match(dev, drv) : 1;

}

//driver_probe_device - attempt to bind device & driver together

int driver_probe_device(struct device_driver *drv, struct device *dev)

{

    int ret = 0;

    ret = really_probe(dev, drv);

    return ret;

}

static int really_probe(struct device *dev, struct device_driver *drv) {

    dev->driver = drv;//赋值device driver field

    driver_sysfs_add(dev); //在sysfs中创建link file

//如果device的bus有support probe function,那么就调用bus的probe function,如果bus的probe function为NULL,那么就调用driver的probe function

    if (dev->bus->probe) { 

        ret = dev->bus->probe(dev); 

    } else if (drv->probe) {

        ret = drv->probe(dev);    

    } 

    driver_bound(dev);

    return ret;

}

//将device加入到driver的klist_devices中

static void driver_bound(struct device *dev) {

    klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);

}

device_register()

struct device {

//The device's "parent" device, the device to which it is attached. * In most cases, a parent //device is some sort of bus or host * controller. If parent is NULL, the device, is a top-level //device, * which is not usually what you want.

    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

    struct bus_type *bus; /* type of bus device is on */

    struct device_driver *driver; /* which driver has allocated thisdevice */

    void *driver_data; /* Driver data, set and get with dev_set/get_drvdata */

    u64 *dma_mask; /* dma mask (if dma'able device) */

    u64 coherent_dma_mask;

    dev_t devt; /* dev_t, creates the sysfs "dev" */

    u32 id; /* device instance */

    struct class *class;

};

int device_register(struct device *dev)

{

    device_initialize(dev);

    return device_add(dev);

}

void device_initialize(struct device *dev)

{

    dev->kobj.kset = devices_kset; //sys/devices

    kobject_init(&dev->kobj, &device_ktype);

    mutex_init(&dev->mutex);

    ...

}

int device_add(struct device *dev)

{

    //set device 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);

    error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);

    bus_probe_device(dev);

}

void bus_probe_device(struct device *dev)

{

    struct bus_type *bus = dev->bus;

    if (bus->p->drivers_autoprobe)

        device_initial_probe(dev);

}

void device_initial_probe(struct device *dev)

{

    __device_attach(dev, true);

}

static int __device_attach(struct device *dev, bool allow_async)

{

    ret = bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);

}

static int __device_attach_driver(struct device_driver *drv, void *_data)

{

    struct device_attach_data *data = _data;

    struct device *dev = data->dev;

    if (!driver_match_device(drv, dev))

        return 0;

    return driver_probe_device(drv, dev);//又回到driver_register调用的函数去调用probe

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342