首页 > 代码库 > ramoops驱动注册失败原因详解

ramoops驱动注册失败原因详解

在使用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 = http://www.mamicode.com/{>原来真的是因为少了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