Linux平台驱动设备总线模型
发布时间:2020-12-14 01:58:14 所属栏目:Linux 来源:网络整理
导读:Linux中的Platform总线是 虚拟总线 ,它把驱动和设备的注册都分开来,对于他们的牵红线(匹配)过程,提供了“总线-设备-驱动”模型。 它的结构如下图所示: ? 为什么要这么做? 试想,如果设备和驱动不分开,那么不同设备将会产生大量驱动(代码冗余),而
Linux中的Platform总线是虚拟总线,它把驱动和设备的注册都分开来,对于他们的牵红线(匹配)过程,提供了“总线-设备-驱动”模型。 它的结构如下图所示: ?
试想,如果设备和驱动不分开,那么不同设备将会产生大量驱动(代码冗余),而且维护起来工作量很大。 Platform模型则把稳定不变的部分放在driver,把尽量可变需要设置的部分放到device端,并且两边分别注册管理。 这样做有如下好处:
?适用于可选的平台资源,例如灯、按键、LCD等
注册设备 int platform_device_register(struct platform_device *pdev); ?注销设备 void platform_device_unregister(struct platform_device *pdev); ?注册驱动 int platform_driver_register(struct platform_driver *drv); ?注销驱动 void platform_driver_unregister(struct platform_driver *drv); ?获取平台资源 struct resource *platform_get_resource(struct platform_device *dev,unsigned int type,unsigned int num); int platform_get_irq(struct platform_device *dev,unsigned int num); struct resource *platform_get_resource_byname(struct platform_device *dev,const char *name); int platform_get_irq_byname(struct platform_device *dev,const char *name);
设备端注册 1 /************************ arch/xxx/xxx_platform.c ************************/ 2 static void device_release(struct device *dev) 3 { 4 } 5 6 static struct resource xxx_res[]={ 7 [0]={ 8 .start = 11, 9 .end = 22,10 .flags = IORESOURCE_MEM,11 },12 [1]={ 13 .start = 33,14 .end = 44,15 .flags = IORESOURCE_IRQ,16 },17 }; 18 19 struct platform_device xxx_device = { 20 .id = -1,21 .name = "xxx_device" 22 .resource = xxx_res,23 .num_resources = ARRAY_SIZE(xxx_res); 24 .dev = { 25 .release = device_release,/* 一般要有改函数,否则驱动卸载会异常 */ 26 },27 }; 28 29 static int __init xxx_platform_init(void) 30 { 31 return platform_device_register(&xxx_device); 32 } 33 34 static void __exit xxx_platform_exit(void) 35 { 36 platform_device_unregister(&xxx_device); 37 } 38 39 module_init(xxx_platform_init); 40 module_exit(xxx_platform_exit); ? ? ? 驱动注册 ? 1 /************************ driver/xxx/xxx_driver.c ************************/ 2 static int driver_probe(struct platform_device *dev) 3 { 4 struct resource * res = platform_get_resource(dev,IORESOURCE_MEM,0); 5 ... 6 res=platform_get_resource(dev,IORESOURCE_IRQ,0); 7 ... 8 return 0; 9 } 10 11 static int driver_remove(struct platform_device *dev) 12 { 13 return 0; 14 } 15 16 static sturct platform_driver xxx_driver = { 17 .probe = driver_probe,18 .remove = driver_remove,19 .driver = { 20 .name = "xxx_device" 21 },22 }; 23 24 static int __init xxx_driver_init(void) 25 { 26 return platform_driver_register(&xxx_driver); 27 } 28 29 static void __exit xxx_driver_exit(void) 30 { 31 platform_driver_unregister(&xxx_driver); 32 } 33 34 module_init(xxx_driver_init); 35 module_exit(xxx_driver_exit); ? ? ?
?TBD (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |