2017 linux 驱动模型,Linux平台总线驱动设备模型

platform总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver。Linux 2.6的设备驱动模型中,把I2C、RTC、LCD等都归纳为platform_device。

总线将设备和驱动绑定,在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。

Linux2.6系统中定义了一个bus_type的实例platform_bus_type

[cpp] view plaincopy

print?

structbus_type platform_bus_type = {

.name       = "platform",

.dev_attrs  = platform_dev_attrs,

.match      = platform_match,       //设备和驱动使用match函数来判断是否匹配

.uevent     = platform_uevent,

.pm     = PLATFORM_PM_OPS_PTR,

};

[cpp] view plaincopy

print?

/* platform_match函数用于匹配总线中的驱动和设备 */

staticintplatform_match(structdevice *dev,structdevice_driver *drv)

{

structplatform_device *pdev = to_platform_device(dev);

structplatform_driver *pdrv = to_platform_driver(drv);

/* match against the id table first */

if(pdrv->id_table)

returnplatform_match_id(pdrv->id_table, pdev) != NULL;

/* fall-back to driver name match */

return(strcmp(pdev->name, drv->name) == 0);

}

platform_match函数首先判断是否由id_table,如果有则使用id_table来进行匹配,否则,判断platform_device和platform_driver成员里的name,如果二者的name字段相同则匹配,如果匹配则调用platform_driver的probe函数。

platform_device结构体的定义

[cpp] view plaincopy

print?

structplatform_device {

constchar* name;/* 名字 */

intid;

structdevice   dev;

u32     num_resources;      /* 资源总数 */

structresource * resource;/* 资源 */

structplatform_device_id   *id_entry;

};

其中有个重要的成员是resource,是设备的资源信息,如IO地址,中断号等。

[cpp] view plaincopy

print?

structresource {

resource_size_t start;      //资源的起始值

resource_size_t end;        //资源的结束值

constchar*name;

unsigned longflags;//资源的类型,如IORESOURCE_IO,IORESOURCE_MEM,IORESOURCE_IRQ,IORESOURCE_DMA

structresource *parent, *sibling, *child;

};

有的设备可能有多个资源,通常使用platform_get_resource函数来获取资源

[cpp] view plaincopy

print?

/**

* platform_get_resource - get a resource for a device

* @dev: platform device

* @type: resource type

* @num: resource index

*/

structresource *platform_get_resource(structplatform_device *dev,

unsigned inttype, unsignedintnum)

{

inti;

for(i = 0; i num_resources; i++) {

structresource *r = &dev->resource[i];

if(type == resource_type(r) && num-- == 0)

returnr;

}

returnNULL;

}

平台设备的注册,使用platform_device_register函数

[cpp] view plaincopy

print?

intplatform_device_register(structplatform_device *pdev)

{

device_initialize(&pdev->dev);

returnplatform_device_add(pdev);

}

platform_device_register函数先通过device_initialize函数初始化platform_device的device成员,然后调用platform_device_add向内核添加一个平台设备。

[cpp] view plaincopy

print?

intplatform_device_add(structplatform_device *pdev)

{

inti, ret = 0;

if(!pdev)/* 如果pdev为空则返回EINVAL */

return-EINVAL;

/* 如果pdev->dev.parent为空则将pdev->dev.parent设置为platform_bus */

if(!pdev->dev.parent)

pdev->dev.parent = &platform_bus;

pdev->dev.bus = &platform_bus_type;  /* 设置总线类型 */

if(pdev->id != -1)/* 如果id = -1则表示自动分配name */

dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);

else

dev_set_name(&pdev->dev, pdev->name);

for(i = 0; i num_resources; i++) {

structresource *p, *r = &pdev->resource[i];/* 获取资源 */

if(r->name == NULL)

r->name = dev_name(&pdev->dev);

p = r->parent;

if(!p) {

if(resource_type(r) == IORESOURCE_MEM)/* 设置资源类型 */

p = &iomem_resource;

elseif(resource_type(r) == IORESOURCE_IO)

p = &ioport_resource;

}

if(p && insert_resource(p, r)) {

printk(KERN_ERR

"%s: failed to claim resource %d\n",

dev_name(&pdev->dev), i);

ret = -EBUSY;

gotofailed;

}

}

pr_debug("Registering platform device '%s'. Parent at %s\n",

dev_name(&pdev->dev), dev_name(pdev->dev.parent));

/* 向内核添加一个device */

ret = device_add(&pdev->dev);

if(ret == 0)

returnret;

failed:

while(--i >= 0) {

structresource *r = &pdev->resource[i];

unsigned longtype = resource_type(r);

if(type == IORESOURCE_MEM || type == IORESOURCE_IO)

release_resource(r);

}

returnret;

}

platform_device_add最终调用device_add来完成平台设备的注册。

相反地,如果要注销平台设备则使用platform_device_unregister函数

[cpp] view plaincopy

print?

voidplatform_device_unregister(structplatform_device *pdev)

{

platform_device_del(pdev);

platform_device_put(pdev);

}

platform_device_unregister函数调用platform_device_del函数来注销平台设备

[cpp] view plaincopy

print?

voidplatform_device_del(structplatform_device *pdev)

{

inti;

if(pdev) {

device_del(&pdev->dev);

for(i = 0; i num_resources; i++) {

structresource *r = &pdev->resource[i];

unsigned longtype = resource_type(r);

if(type == IORESOURCE_MEM || type == IORESOURCE_IO)

release_resource(r);

}

}

}

platform_device_del函数调用device_del函数来删除平台设备,相应地,要释放资源应调用release_resource函数,前提是资源的类型必须为IORESOURCE_MEM或者IORESOURCE_IO

platform_driver的定义:

[cpp] view plaincopy

print?

structplatform_driver {

int(*probe)(structplatform_device *);

int(*remove)(structplatform_device *);

void(*shutdown)(structplatform_device *);

int(*suspend)(structplatform_device *, pm_message_t state);

int(*resume)(structplatform_device *);

structdevice_driver driver;

conststructplatform_device_id *id_table;

};

device_driver的定义:

[cpp] view plaincopy

print?

structdevice_driver {

constchar*name;

structbus_type     *bus;

structmodule       *owner;

constchar*mod_name;/* used for built-in modules */

boolsuppress_bind_attrs;/* disables bind/unbind via sysfs */

conststructof_device_id   *of_match_table;

conststructacpi_device_id *acpi_match_table;

int(*probe) (structdevice *dev);

int(*remove) (structdevice *dev);

void(*shutdown) (structdevice *dev);

int(*suspend) (structdevice *dev, pm_message_t state);

int(*resume) (structdevice *dev);

conststructattribute_group **groups;

conststructdev_pm_ops *pm;

structdriver_private *p;

};

platform_driver结构体有device_driver成员,该成员的各自字段如上所示,device_driver也有probe、remove、shutdown等函数,在平台驱动注册的时候被初始化。

前面说过,当系统中存在有平台设备和平台驱动通过总线的match函数匹配后则会调用platform_driver的probe函数,参数为platform_device,有时候也通过id_table来判断是否匹配。

[cpp] view plaincopy

print?

structplatform_device_id {

charname[PLATFORM_NAME_SIZE];

kernel_ulong_t driver_data

__attribute__((aligned(sizeof(kernel_ulong_t))));

};

平台驱动的注册使用platform_driver_register函数

[cpp] view plaincopy

print?

intplatform_driver_register(structplatform_driver *drv)

{

drv->driver.bus = &platform_bus_type;

if(drv->probe)

drv->driver.probe = platform_drv_probe;

if(drv->remove)

drv->driver.remove = platform_drv_remove;

if(drv->shutdown)

drv->driver.shutdown = platform_drv_shutdown;

if(drv->suspend)

drv->driver.suspend = platform_drv_suspend;

if(drv->resume)

drv->driver.resume = platform_drv_resume;

returndriver_register(&drv->driver);

}

先初始化platform_driver里的driver,该driver的类型为device_driver,设置driver的bus为platform_bus_type;设置driver的probe为platform_drv_probe;设置driver的remove为platform_drv_remove;设置driver的shutdown为platform_drv_shutdown;设置driver的suspend为platform_drv_suspend;设置driver的resume为platform_drv_resume,最后调用driver_register函数来注册平台驱动。

相反地,要注销平台驱动的话,使用platform_driver_unregister函数

[cpp] view plaincopy

print?

voidplatform_driver_unregister(structplatform_driver *drv)

{

driver_unregister(&drv->driver);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值