了解Linux平台总线的驱动设备模型一定不会错.doc
《了解Linux平台总线的驱动设备模型一定不会错.doc》由会员分享,可在线阅读,更多相关《了解Linux平台总线的驱动设备模型一定不会错.doc(8页珍藏版)》请在三一文库上搜索。
1、了解Linux平台总线的驱动设备模型一定不会错 platform总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver。Linux 2.6的设备驱动模型中,把I2C、RTC、LCD等都归纳为platform_device。总线将设备和驱动绑定,在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。Linux2.6系统中定义了一个bus_type的实例platform_bus_typecppview plaincopyprint?structbus_typeplatfor
2、m_bus_type=.name=platform,.dev_attrs=platform_dev_attrs,.match=platform_match,/设备和驱动使用match函数来判断是否匹配.uevent=platform_uevent,.pm=PLATFORM_PM_OPS_PTR,;cppview plaincopyprint?/*platform_match函数用于匹配总线中的驱动和设备*/staticintplatform_match(structdevice*dev,structdevice_driver*drv)structplatform_device*pdev=to_
3、platform_device(dev);structplatform_driver*pdrv=to_platform_driver(drv);/*matchagainsttheidtablefirst*/if(pdrv-id_table)returnplatform_match_id(pdrv-id_table,pdev)!=NULL;/*fall-backtodrivernamematch*/return(strcmp(pdev-name,drv-name)=0);platform_match函数首先判断是否由id_table,如果有则使用id_table来进行匹配,否则,判断platfo
4、rm_device和platform_driver成员里的name,如果二者的name字段相同则匹配,如果匹配则调用platform_driver的probe函数。platform_device结构体的定义cppview plaincopyprint?structplatform_deviceconstchar*name;/*名字*/intid;structdevicedev;u32num_resources;/*资源总数*/structresource*resource;/*资源*/structplatform_device_id*id_entry;其中有个重要的成员是resource,是设
5、备的资源信息,如IO地址,中断号等。cppview plaincopyprint?structresourceresource_size_tstart;/资源的起始值resource_size_tend;/资源的结束值constchar*name;unsignedlongflags;/资源的类型,如IORESOURCE_IO,IORESOURCE_MEM,IORESOURCE_IRQ,IORESOURCE_DMAstructresource*parent,*sibling,*child;有的设备可能有多个资源,通常使用platform_get_resource函数来获取资源cppview pl
6、aincopyprint?/*platform_get_resource-getaresourceforadevice*dev:platformdevice*type:resourcetype*num:resourceindex*/structresource*platform_get_resource(structplatform_device*dev,unsignedinttype,unsignedintnum)inti;for(i=0;inum_resources;i+)structresource*r=if(type=resource_type(r)returnr;returnNULL
7、;平台设备的注册,使用platform_device_register函数cppview plaincopyprint?intplatform_device_register(structplatform_device*pdev)device_initialize(returnplatform_device_add(pdev);platform_device_register函数先通过device_iniTIalize函数初始化platform_device的device成员,然后调用platform_device_add向内核添加一个平台设备。cppview plaincopyprint?i
8、ntplatform_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=pdev-dev.bus=/*设置总线类型*/if(pdev-id!=-1)/*如果id=-1则表示自动分配name*/dev_set_name(elsedev_set_name(for(i=0;inum_re
9、sources;i+)structresource*p,*r=/*获取资源*/if(r-name=NULL)r-name=dev_name(p=r-parent;if(!p)if(resource_type(r)=IORESOURCE_MEM)/*设置资源类型*/p=elseif(resource_type(r)=IORESOURCE_IO)p=if(pprintk(KERN_ERR%s:failedtoclaimresource%dn,dev_name(ret=-EBUSY;gotofailed;pr_debug(Registeringplatformdevice%s.Parentat%sn
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 了解 Linux 平台 总线 驱动 设备 模型 一定 不会
链接地址:https://www.31doc.com/p-3371925.html