linux-3.5/Documentation/driver-model/bus.txt
先写一个简单的例子,是为了给学习platform做准备。
dev.h
1 #ifndef JASON_DEV_H_ 2 #define JASON_DEV_H_ 3 4 #include5 6 struct pridevice { 7 struct device device; 8 char *name; 9 };10 11 12 #endif
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 7 #include "dev.h" 8 9 static int mara_match(struct device *dev, struct device_driver *drv)10 {11 struct pridevice *pdev = container_of(dev, struct pridevice, device);12 13 printk("match: %s device try match %s driver...\n", pdev->name, drv->name);14 15 return !strcmp(pdev->name, drv->name );16 //返回值为1代表匹配成功,为0则失败17 }18 19 static struct bus_type demobus = {20 .name = "marathon",21 .match = mara_match,22 };23 EXPORT_SYMBOL_GPL(demobus);24 25 module_driver(demobus, bus_register, bus_unregister);26 27 MODULE_LICENSE("GPL");28 29 MODULE_AUTHOR("no name");30 MODULE_VERSION("J-15");31 MODULE_DESCRIPTION("a simple demo for driver module");
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 7 #include "dev.h" 8 9 extern struct bus_type demobus;10 11 static void demo_release(struct device *dev)12 {13 struct pridevice *pdev = container_of(dev, struct pridevice, device);14 15 printk("%s device is released....\n", pdev->name);16 }17 18 static struct pridevice demodev1 = {19 .device = {20 .init_name = "spring_1", 21 .bus = &demobus,22 .release = demo_release,23 },24 .name = "spring",25 };26 27 static int demo_init1(void)28 {29 return device_register(&demodev1.device);30 }31 32 /*此宏用于指定驱动的入口函数, 内核启动或插入模块到内核时被调用*/33 module_init(demo_init1);34 35 36 static void demo_exit1(void)37 {38 device_unregister(&demodev1.device);39 }40 41 /*此宏用于指定驱动模块输出函数*/42 module_exit(demo_exit1);43 44 MODULE_LICENSE("GPL");45 46 MODULE_AUTHOR("no name");47 MODULE_VERSION("J-15");48 MODULE_DESCRIPTION("a simple demo for driver module");
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include2 #include 3 #include 4 #include 5 #include 6 7 #include "dev.h" 8 9 extern struct bus_type demobus;10 11 static int demo_probe (struct device *dev)12 {13 /*14 当总线的match函数返回1时,则由内核调用驱动对象的probe指针指向的15 函数16 */17 18 struct pridevice *pdev = container_of(dev, struct pridevice, device);19 struct device_driver *pdrv = dev->driver;20 21 printk("probe: %s driver do %s device...\n",22 pdrv->name, pdev->name);23 24 return 0;25 }26 27 static int demo_remove (struct device *dev)28 {29 struct pridevice *pdev = container_of(dev, struct pridevice, device);30 struct device_driver *pdrv = dev->driver;31 32 printk("remove: %s driver remove %s device...\n",33 pdrv->name, pdev->name);34 35 return 0;36 }37 38 static struct device_driver demodrv1 = {39 .name = "spring",40 .bus = &demobus, 41 .probe = demo_probe,42 .remove = demo_remove,43 };44 45 module_driver(demodrv1, driver_register, driver_unregister);46 47 MODULE_LICENSE("GPL");48 49 MODULE_AUTHOR("no name");50 MODULE_VERSION("J-15");51 MODULE_DESCRIPTION("a simple demo for driver module");
查看总线:
insmod bus.ko 查看总线:
生成的:marathon
目前还没有加载dev drv
加载驱动后:
platform 总线是内核自身已经注册好的一种总线,我们无须再注册,直接根据总线的匹配规则
来管理设备对象和驱动对象。那么设备对象和驱动对象的类型如下:参考手册:linux-3.5/Documentation/driver-model/platform.txt
匹配规则:
看完理解之后,常用第二种和第三种匹配规则:
第二种匹配规则: