LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-18-2008, 10:55 AM   #1
urup
LQ Newbie
 
Registered: Jun 2008
Posts: 7

Rep: Reputation: 0
Question howto use one module's dev/functions in another module.


Hi all, and thanks for helping.

The situation:
I have one module (ad7877), that creates entries under /sys, which I need to access using another module.

It's a simple gpio I need to toogle, but how do I do that?
I know that I can't access the filesystem (and why it's a bad idea), so instead I want to use a function in the first module to set the gpio value.

I know that I can export the function via export_symbol(...), but how do I pass arguments between the two (and how do I get the right ones)?

The function in the first module is:
Code:
static ssize_t ad7877_gpio1_store(struct device *dev,
                                     struct device_attribute *attr,
                                     const char *buf, size_t count)
{
        struct ad7877 *ts = dev_get_drvdata(dev);
        char *endp;
        int i;

        i = simple_strtoul(buf, &endp, 10);

        if (i) {
                ts->gpio1=1;
        } else {
                ts->gpio1=0;
        }

        ad7877_write(dev, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA | (ts->gpio3 << 5) | (ts->gpio2 << 6)| (ts->gpio1 << 7));

        return count;
}
where the "dev" and "attr" parameters are specific for the ad7877 device driver instance.

Since my system only has one ad7877 (touchscreen controller), I guess I could just export the dev and attr as well, but that doesn't seem like a good solution.

How is this best done?
 
Old 06-19-2008, 04:57 AM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Export the functions so that other modules can call them. You can see that sort of thing throughout the driver code: EXPORT_SYMBOL
 
Old 06-20-2008, 02:19 AM   #3
urup
LQ Newbie
 
Registered: Jun 2008
Posts: 7

Original Poster
Rep: Reputation: 0
pinniped, you're absolutely right!
That was also what I wanted to do, but I was missing some of "glue" to bind it all together. "Grepping" though some of the kernel code helped though.

For others that where just as confused as I was:
If you from module1 want to call a function in module2 (lets say "module2_test_function(u16 param)").

export the method you want to call in module1:
EXPORT_SYMBOL(module2_test_function);

In module2 just call the function:
u16 param;
module2_test_function(param);

Even though you don't specify the parameter list in the EXPORT_SYMBOL, it still works.
 
Old 06-20-2008, 08:48 AM   #4
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Another trick you might want to look at is putting some module parameters into the sysfs tree so that you can change their values dynamically by simply "writing" to the parameter name (using 'echo' or any other mean); you can also query the current setting with a "read" (cat).
 
Old 06-23-2008, 01:36 AM   #5
urup
LQ Newbie
 
Registered: Jun 2008
Posts: 7

Original Poster
Rep: Reputation: 0
As i've read, writing/reading directly to sysfs is a very bad idea.
But how would I go about reading and writing to the sysfs if I wanted to (to make it safe)?
using system utilities like 'echo' and 'cat' seams like a bad idea, and youcan't use open/popen/fstream since you're in kernelspace nad only allowed to include stuff from the kernel itself.
 
Old 06-27-2008, 06:39 AM   #6
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
I mean that module parameters (such as irq, io, and others) can be made read/write and often in such a way that you can change the parameters simply by 'writing' to its virtual directory entry. An example of such a parameter exposed for read/write is the forwarding parameter in the network iptables code. You can look at that code as an example, but also read the relevant chapters in Linux Device Drivers 3rd ed. by Rubini and others.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use functions exported by module in user application kandhala Linux - Kernel 1 12-15-2007 03:40 AM
How to link with functions exported by module in user application kandhala Linux - Kernel 1 11-28-2007 06:16 AM
use module functions in own C program? captaint Programming 4 06-30-2007 04:44 AM
warning undefined functions.....doing kernel module programing swift_a2002 Programming 10 05-24-2007 11:50 AM
linking freeradius functions to an external auth module kky Programming 0 09-26-2006 09:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration