Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Linux driver module call/use another driver module?

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)

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.

like image 662
teerapap Avatar asked Jan 05 '09 18:01

teerapap


People also ask

How are drivers loaded in Linux?

Device drivers make use of standard kernel services such as memory allocation, interrupt delivery and wait queues to operate, Loadable. Most of the Linux device drivers can be loaded on demand as kernel modules when they are needed and unloaded when they are no longer being used.

How do I load a .KO file in Linux?

To load a kernel module, we can use the insmod (insert module) command. Here, we have to specify the full path of the module. The command below will insert the speedstep-lib. ko module.


2 Answers

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

/* 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.

like image 75
Martin Carpenter Avatar answered Oct 11 '22 19:10

Martin Carpenter


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.

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

like image 42
Blaisorblade Avatar answered Oct 11 '22 18:10

Blaisorblade