设备驱动中的device_driver(kernel-4.7)

device_driver结构体定义在driver/base/base.h中,如下:


/**
 * struct device_driver - The basic device driver structure
 * @name:   Name of the device driver.
 * @bus:    The bus which the device of this driver belongs to.
 * @owner:  The module owner.
 * @mod_name:   Used for built-in modules.
 * @suppress_bind_attrs: Disables bind/unbind via sysfs.
 * @probe_type: Type of the probe (synchronous or asynchronous) to use.
 * @of_match_table: The open firmware table.
 * @acpi_match_table: The ACPI match table.
 * @probe:  Called to query the existence of a specific device,
 *      whether this driver can work with it, and bind the driver
 *      to a specific device.
 * @remove: Called when the device is removed from the system to
 *      unbind a device from this driver.
 * @shutdown:   Called at shut-down time to quiesce the device.
 * @suspend:    Called to put the device to sleep mode. Usually to a
 *      low power state.
 * @resume: Called to bring a device from sleep mode.
 * @groups: Default attributes that get created by the driver core
 *      automatically.
 * @pm:     Power management operations of the device which matched
 *      this driver.
 * @p:      Driver core's private data, no one other than the driver
 *      core can touch this.
 *
 * 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; /*设备驱动程序的名称*/
    struct bus_type     *bus; //device_driver支持的device所依附的bus

    struct module       *owner; //所属的设备驱动程序的模块
    const char      *mod_name;  /* used for built-in modules */

    bool suppress_bind_attrs;   /* disables bind/unbind via sysfs */
    enum probe_type probe_type;

    const struct of_device_id   *of_match_table;
    const struct acpi_device_id *acpi_match_table;

    int (*probe) (struct device *dev); /*探测device_drvier是否支持参数指定的device*/
    int (*remove) (struct device *dev);/*device被移除时调用该函数,解除该device与device_driver的绑定*/
    void (*shutdown) (struct device *dev); /*当关机时调用该函数,以关闭参数指定的device*/
    int (*suspend) (struct device *dev, pm_message_t state); //当device进入休眠状态时,调用该函数
    int (*resume) (struct device *dev); //当device从休眠状态被唤醒时,调用该函数
    const struct attribute_group **groups;

    const struct dev_pm_ops *pm;

    struct driver_private *p; //device_driver私有数据
};

该结构体用于描述设备驱动程序模型中的驱动程序。

p是device_driver私有数据,它是struct driver_private类型,该类型定义在drivers/base/base.h文件中,其内容如下:


struct driver_private {
    struct kobject kobj; //是其所属的device_driver对应的kobject
    struct klist klist_devices; //其所属的device_driver支持的device链表
    struct klist_node knode_bus;
    struct module_kobject *mkobj;
    struct device_driver *driver; //所属的device_driver
};

#define to_driver(obj) container_of(obj, struct driver_private, kobj)

device_driver的注册是通过调用driver_register函数完成的,该函数定义在drivers/base/driver.c文件中,其内容如下:

/**
 * driver_register - register driver with bus
 * @drv: driver to register
 *
 * We pass off most of the work to the bus_add_driver() call,
 * since most of the things we have to do deal with the bus
 * structures.
 */
int driver_register(struct device_driver *drv)
{
    int ret;
    struct device_driver *other;

    BUG_ON(!drv->bus->p);

//如果bus和device_driver定义了相同的函数,会优先调用bus的相应函数,这里会发出警告信息
    if ((drv->bus->probe && drv->probe) ||
        (drv->bus->remove && drv->remove) ||
        (drv->bus->shutdown && drv->shutdown))
        printk(KERN_WARNING "Driver '%s' needs updating - please use "
            "bus_type methods\n", drv->name);

//调用driver_find在bus的drivers_kset中查找是否已经有同名device_driver已经注册过,如果已经注册过,则退出。
    other = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值