标签:printk closed ecc 函数 驱动模块 cal auth remove struct
linux-3.5/Documentation/driver-model/bus.txt
先写一个简单的例子,是为了给学习platform做准备。
dev.h
1 #ifndef JASON_DEV_H_ 2 #define JASON_DEV_H_ 3 4 #include <linux/device.h> 5 6 struct pridevice { 7 struct device device; 8 char *name; 9 }; 10 11 12 #endif
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");
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 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");
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 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
标签:printk closed ecc 函数 驱动模块 cal auth remove struct
原文地址:https://www.cnblogs.com/jason-linux/p/10478422.html