正在使用linux的ramoops驱动器模块,当编译完成加载。查找驱动程序加载失败。显然,直接用内核代码,为什么会出现这种情况?
第一眼ramoops初始化代码:
180 static int __init ramoops_init(void) 181 { 182 return platform_driver_probe(&ramoops_driver, ramoops_probe); 183 } 184 185 static void __exit ramoops_exit(void) 186 { 187 platform_driver_unregister(&ramoops_driver); 188 } 189 190 module_init(ramoops_init);180行開始的ramoops_init函数是不是有点奇怪?直接就调用了probe函数。标准的platform驱动程序的流程是这种:
怎么看起来好像缺少platform_device的定义和注冊。究竟是不是由于这个呢?我们来看一下Document/ramoops.txt的相关说明:
38 2. Setting the parameters 39 40 Setting the ramoops parameters can be done in 2 different manners: 41 1. Use the module parameters (which have the names of the variables described 42 as before). 43 For quick debugging, you can also reserve parts of memory during boot 44 and then use the reserved memory for ramoops. For example, assuming a machine 45 with > 128 MB of memory, the following kernel command line will tell the 46 kernel to use only the first 128 MB of memory, and place ECC-protected ramoops 47 region at 128 MB boundary: 48 "mem=128M ramoops.mem_address=0x8000000 ramoops.ecc=1" 49 2. Use a platform device and set the platform data. The parameters can then 50 be set through that platform data. An example of doing that is: 51 52 #include <linux/pstore_ram.h> 53 [...] 54 55 static struct ramoops_platform_data ramoops_data = { 56 .mem_size = <...>, 57 .mem_address = <...>, 58 .record_size = <...>, 59 .dump_oops = <...>, 60 .ecc = <...>, 61 }; 62 63 static struct platform_device ramoops_dev = { 64 .name = "ramoops", 65 .dev = { 66 .platform_data = &ramoops_data, 67 }, 68 }; 69 70 [... inside a function ...] 71 int ret; 72 73 ret = platform_device_register(&ramoops_dev); 74 if (ret) { 75 printk(KERN_ERR "unable to register platform device "); 76 return ret; 77 }原来真的是由于少了platform_device的缘故,赶紧加上。
追加platform_device的操作比較简单,依照Document上的来就能够了。
有一点须要主要。就是ramoops_dev的name这个成员。这个成员的值必须是"ramoops"。为什么呢?这是由于platform总线在调用自身的match函数,将driver与device进行匹配时,就是推断两个结构体中的name成员是否相等。
而platform_driver结构体中的name成员的值,从以下的代码中能够看出,已经写定为"ramoops"。假设platform_device中的值不同,则驱动相同无法载入。
172 static struct platform_driver ramoops_driver = { 173 .remove = __exit_p(ramoops_remove), 174 .driver = { 175 .name = "ramoops", 176 .owner = THIS_MODULE, 177 }, 178 };
有没有想过,为什么没注冊platform_device,ramoops的驱动代码就不能载入呢?再回过头来看一下ramoops的初始化代码:
180 static int __init ramoops_init(void) 181 { 182 return platform_driver_probe(&ramoops_driver, ramoops_probe); 183 }之前说过,一般init函数中会调用register函数,还说这是标准流程了呢。看一下platform_driver_probe函数的定义:
477 int __init_or_module platform_driver_probe(struct platform_driver *drv, 478 int (*probe)(struct platform_device *)) 479 { 480 int retval, code; 481 482 /* make sure driver won't have bind/unbind attributes */ 483 drv->driver.suppress_bind_attrs = true; 484 485 /* temporary section violation during probe() */ 486 drv->probe = probe; 487 retval = code = platform_driver_register(drv); 488 。看到platform_driver_register函数没?原来是将register函数封装了一层。。
。
}
看到这里应该明确了,为什么没有注冊platform_device。驱动会载入失败了吧。
什么,还是不知道?那你一定没看我之前写的关于linux设备驱动程序的注冊流程。假设看完了还没明确,那就是我的问题。链接在此:http://blog.csdn.net/tuzhutuzhu/article/details/34847619
版权声明:本文博主原创文章,博客,未经同意不得转载。