如何编写Linux驱动模块调用/使用另一个驱动模块?

时间:2022-07-21 16:55:07

I'm developing a Linux driver loadable module and I have to use another device in my driver.(kind of driver stacked on another driver)

我正在开发一个Linux驱动程序可加载模块,我必须在驱动程序中使用另一个设备。(类似于另一个司机)

How do I call/use another driver in my driver? I think they are both in the kernel so there might be a way that can use another driver directly.

我如何呼叫/使用另一个司机在我的司机?我认为它们都在内核中,所以可能有一种方法可以直接使用另一个驱动程序。

2 个解决方案

#1


17  

You will need the EXPORT_SYMBOL (or EXPORT_SYMBOL_GPL) macro. For example:

您将需要EXPORT_SYMBOL(或EXPORT_SYMBOL_GPL)宏。例如:

/* mod1.c */
#include <linux/module.h>
#include <linux/kernel.h>
#include "mod1.h"
....
void mod1_foo(void)
{
    printk(KERN_ALERT "mod1_foo\n");
}
EXPORT_SYMBOL(mod1_foo);

/* mod2.h */
....
extern void mod1_foo(void);
....

/* mod2.c */
#include <linux/module.h>
#include <linux/kernel.h>
#include "mod1.h"
#include "mod2.h"
int init_module(void)
{
    mod1_foo();
    ...

This should be plain sailing, but you must of course be careful with the namespace - stomping on somebody else's kernel module symbols would be unfortunate.

这应该是一帆风顺的,但是您必须小心使用名称空间——践踏别人的内核模块符号将是不幸的。

#2


4  

You forgot to mention that you should also study try_module_get/module_put/symbol_get/symbol_put/symbol_request, for ensuring loading of the other module, and the fact that it is not unloaded during usage. I don't recall the exact details though; I think that modprobe will ensure the other module is loaded, but I'm not sure if the runtime dependency for unloading will be added. I guess that those APIs might be needed for some other cases, but needs to know about them to check this.

您忘了提到,还应该研究try_module_get/module_put/symbol_get/symbol_put/ symbol_put/symbol_request,以确保加载其他模块,以及在使用过程中没有卸载这个事实。我不记得具体细节;我认为modprobe将确保加载其他模块,但我不确定是否会添加卸载的运行时依赖项。我想这些api在其他一些情况下可能是需要的,但是需要了解它们来检查这个。

Btw, the free book Linux Device Drivers is available here, and it will answer this question and much more: http://lwn.net/Kernel/LDD3/

顺便说一句,免费的图书Linux设备驱动程序可以在这里找到,它将回答这个问题和更多的问题:http://lwn.net/Kernel/LDD3/

#1


17  

You will need the EXPORT_SYMBOL (or EXPORT_SYMBOL_GPL) macro. For example:

您将需要EXPORT_SYMBOL(或EXPORT_SYMBOL_GPL)宏。例如:

/* mod1.c */
#include <linux/module.h>
#include <linux/kernel.h>
#include "mod1.h"
....
void mod1_foo(void)
{
    printk(KERN_ALERT "mod1_foo\n");
}
EXPORT_SYMBOL(mod1_foo);

/* mod2.h */
....
extern void mod1_foo(void);
....

/* mod2.c */
#include <linux/module.h>
#include <linux/kernel.h>
#include "mod1.h"
#include "mod2.h"
int init_module(void)
{
    mod1_foo();
    ...

This should be plain sailing, but you must of course be careful with the namespace - stomping on somebody else's kernel module symbols would be unfortunate.

这应该是一帆风顺的,但是您必须小心使用名称空间——践踏别人的内核模块符号将是不幸的。

#2


4  

You forgot to mention that you should also study try_module_get/module_put/symbol_get/symbol_put/symbol_request, for ensuring loading of the other module, and the fact that it is not unloaded during usage. I don't recall the exact details though; I think that modprobe will ensure the other module is loaded, but I'm not sure if the runtime dependency for unloading will be added. I guess that those APIs might be needed for some other cases, but needs to know about them to check this.

您忘了提到,还应该研究try_module_get/module_put/symbol_get/symbol_put/ symbol_put/symbol_request,以确保加载其他模块,以及在使用过程中没有卸载这个事实。我不记得具体细节;我认为modprobe将确保加载其他模块,但我不确定是否会添加卸载的运行时依赖项。我想这些api在其他一些情况下可能是需要的,但是需要了解它们来检查这个。

Btw, the free book Linux Device Drivers is available here, and it will answer this question and much more: http://lwn.net/Kernel/LDD3/

顺便说一句,免费的图书Linux设备驱动程序可以在这里找到,它将回答这个问题和更多的问题:http://lwn.net/Kernel/LDD3/