Linux-3.5_总线驱动设备

参考手册:linux-3.5/Documentation/driver-model/

先做一个简单的测试:

 1 #include <linux/module.h>
 2 #include <linux/init.h>
 3 #include <linux/syscalls.h>
 4 #include <linux/io.h>
 5 #include <linux/uaccess.h>
 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");
bus.c
 1 #include <linux/module.h>
 2 #include <linux/init.h>
 3 #include <linux/syscalls.h>
 4 #include <linux/io.h>
 5 #include <linux/uaccess.h>
 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 demodev = {
19     .device = {
20         .init_name     = "spring",    
21         .bus           = &demobus,
22         .release    = demo_release,
23     },
24     .name    =  "spring",
25 };
26 
27 static int demo_init(void)
28 {
29     return device_register(&demodev.device);
30 }
31 
32 /*此宏用于指定驱动的入口函数, 内核启动或插入模块到内核时被调用*/
33 module_init(demo_init);
34 
35 
36 static void demo_exit(void)
37 {
38     device_unregister(&demodev.device);
39 }
40 
41 /*此宏用于指定驱动模块输出函数*/
42 module_exit(demo_exit);
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");
dev.c
 1 #include <linux/module.h>
 2 #include <linux/init.h>
 3 #include <linux/syscalls.h>
 4 #include <linux/io.h>
 5 #include <linux/uaccess.h>
 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 demodrv = {
39     .name    =       "spring",
40     .bus    =     &demobus,    
41     .probe    =    demo_probe,
42     .remove    =    demo_remove,
43 };
44 
45 module_driver(demodrv, 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");
drv.c

查看总线:

insmod bus.ko     查看总线:

生成的:marathon

目前还没有加载dev       drv  

加载后:

猜你喜欢

转载自www.cnblogs.com/jason-linux/p/10454751.html