linux Platform设备驱动.doc
《linux Platform设备驱动.doc》由会员分享,可在线阅读,更多相关《linux Platform设备驱动.doc(14页珍藏版)》请在三一办公上搜索。
1、linux Platform设备驱动PlatForm设备驱动:一、platform总线、设备与驱动1.一个现实的Linux设备和驱动通常都需要挂接在一种总线上,对于本身依附于PCI、USB、I2C、SPI等的设备而言,这自然不是问题,但是在嵌入式系统里面,SoC系统中集成的独立的外设控制器、挂接在SoC内存空间的外设等确不依附于此类总线。基于这一背景,Linux发明了一种虚拟的总线,称为platform总线,相应的设备称为platform_device,而驱动成为 platform_driver。2.注意,所谓的platform_device并不是与字符设备、块设备和网络设备并列的概念,而是L
2、inux系统提供的一种附加手段,例如,在 S3C6410处理器中,把内部集成的I2C、RTC、SPI、LCD、看门狗等控制器都归纳为platform_device,而它们本身就是字符设备。3.基于Platform总线的驱动开发流程如下:(1)定义初始化platform bus(2)定义各种platform devices(3)注册各种platform devices(4)定义相关platform driver(5)注册相关platform driver(6)操作相关设备4.平台相关结构/platform_device结构体struct platform_deviceconstchar*name
3、;/*设备名*/u32 id;/设备id,用于给插入给该总线并且具有相同name的设备编号,如果只有一个设备的话填-1。struct device dev;/结构体中内嵌的device结构体。u32 num_resources;/*设备所使用各类资源数量*/struct resource*resource;/*/定义平台设备的资源*/;/平台资源结构struct resourceresource_size_t start;/定义资源的起始地址resource_size_tend;/定义资源的结束地址constchar*name;/定义资源的名称unsigned long flags;/定义资源
4、的类型,比如MEM,IO,IRQ,DMA类型struct resource*parent,*sibling,*child;/设备的驱动:platform_driver这个结构体中包含probe()、remove()、shutdown()、suspend()、resume()函数,通常也需要由驱动实现。struct platform_driverint(*probe)(struct platform_device*);int(*remove)(struct platform_device*);void(*shutdown)(struct platform_device*);int(*suspen
5、d)(struct platform_device*,pm_message_t state);int(*suspend_late)(struct platform_device*,pm_message_t state);int(*resume_early)(struct platform_device*);int(*resume)(struct platform_device*);struct pm_ext_ops*pm;struct device_driver driver;/系统中为platform总线定义了一个bus_type的实例platform_bus_type,struct bus
6、_type platform_bus_type=.name=“platform”,.dev_attrs=platform_dev_attrs,.match=platform_match,.uevent=platform_uevent,.pm=PLATFORM_PM_OPS_PTR,;EXPORT_SYMBOL_GPL(platform_bus_type);/这里要重点关注其match()成员函数,正是此成员表明了platform_device和platform_driver之间如何匹配。staticintplatform_match(struct device*dev,struct devic
7、e_driver*drv)struct platform_device*pdev;pdev=container_of(dev,struct platform_device,dev);return(strncmp(pdev-name,drv-name,BUS_ID_SIZE)=0);/匹配platform_device和platform_driver主要看二者的name字段是否相同。/对platform_device的定义通常在BSP的板文件中实现,在板文件中,将platform_device归纳为一个数组,最终通过platform_add_devices()函数统一注册。/platform_a
8、dd_devices()函数可以将平台设备添加到系统中,这个函数的 原型为:intplatform_add_devices(struct platform_device*devs,intnum);/该函数的第一个参数为平台设备数组的指针,第二个参数为平台设备的数量,它内部调用了platform_device_register()函 数用于注册单个的平台设备。1.platform bus总线先被kenrel注册。2.系统初始化过程中调用platform_add_devices或者platform_device_register,将平台设备(platform devices)注册到平台总线中(pl
9、atform bus)3.平台驱动(platform driver)与平台设备(platform device)的关联是在platform_driver_register或者driver_register中实现,一般这个函数在驱动的初始化过程调用。通过这三步,就将平台总线,设备,驱动关联起来。二Platform初始化系统启动时初始化时创建了platform_bus总线设备和platform_bus_type总线,platform总线是在内核初始化的时候就注册进了内核。内核初始化函数kernel_init()中调用了do_basic_setup(),该函数中调用driver_init(),该函数
10、中调用platform_bus_init(),我们看看platform_bus_init()函数:int_init platform_bus_init(void)interror;early_platform_cleanup();/清除platform设备链表/该函数把设备名为platform 的设备platform_bus注册到系统中,其他的platform的设备都会以它为parent。它在sysfs中目录下.即/sys/devices/platform。/platform_bus总线也是设备,所以也要进行设备的注册/struct device platform_bus=/.init_nam
11、e=platform,/;error=device_register(/将平台bus作为一个设备注册,出现在sys文件系统的device目录if(error)returnerror;/接着bus_register(/*struct bus_type platform_bus_type=.name=“platform”,.dev_attrs=platform_dev_attrs,.match=platform_match,.uevent=platform_uevent,.pm=PLATFORM_PM_OPS_PTR,;*/默认platform_bus_type中没有定义probe函数。error
12、=bus_register(/注册平台类型的bus,将出现sys文件系统在bus目录下,创建一个platform的目录,以及相关属性文件if(error)device_unregister(returnerror;/总线类型match函数是在设备匹配驱动时调用,uevent函数在产生事件时调用。/platform_match函数在当属于platform的设备或者驱动注册到内核时就会调用,完成设备与驱动的匹配工作。staTIcintplatform_match(struct device*dev,struct device_driver*drv)struct platform_device*pd
13、ev=to_platform_device(dev);struct platform_driver*pdrv=to_platform_driver(drv);/*match against the id table first*/if(pdrv-id_table)return platform_match_id(pdrv-id_table,pdev)!=NULL;/*fall-backtodriver name match*/return(strcmp(pdev-name,drv-name)=0);/比较设备和驱动的名称是否一样staTIcconststruct platform_device
14、_id*platform_match_id(struct platform_device_id*id,struct platform_device*pdev)while(id-name0)if(strcmp(pdev-name,id-name)=0)pdev-id_entry=id;return id;id+;returnNULL;/不难看出,如果pdrv的id_table数组中包含了pdev-name,或者drv-name和pdev-name名字相同,都会认为是匹配成功。/id_table数组是为了应对那些对应设备和驱动的drv-name和pdev-name名字不同的情况。/再看看platf
15、orm_uevent()函数:platform_uevent 热插拔操作函数staTIcintplatform_uevent(struct device*dev,struct kobj_uevent_env*env)struct platform_device*pdev=to_platform_device(dev);add_uevent_var(env,MODALIAS=%s%s,PLATFORM_MODULE_PREFIX,(pdev-id_entry)?pdev-id_entry-name:pdev-name);return 0;/添加了MODALIAS环境变量,我们回顾一下:platf
16、orm_bus.parent-kobj-kset-uevent_ops为device_uevent_ops,bus_uevent_ops的定义如下:staTIc struct kset_uevent_ops device_uevent_ops=.filter=dev_uevent_filter,.name=dev_uevent_name,.uevent=dev_uevent,;/当调用device_add()时会调用kobject_uevent(三Platform设备的注册我们在设备模型的分析中知道了把设备添加到系统要调用device_initialize()和platform_device_
17、add(pdev)函数。Platform设备的注册分两种方式:1.对于platform设备的初注册,内核源码提供了platform_device_add()函数,输入参数platform_device可以是静态的全局设备,它是进行一系列的操作后调用device_add()将设备注册到相应的总线(platform总线)上,内核代码中platform设备的其他注册函数都是基于这个函数,如platform_device_register()、platform_device_register_simple()、platform_device_register_data()等。2.另外一种机制就是动态申
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linux Platform设备驱动 Platform 设备 驱动

链接地址:https://www.31ppt.com/p-4842635.html