Skip to contentSkip to conversation

how an application program utilizes the driver.

DISCUSSION Ming Liu 4 participants 5 messages Sep 20, 2006 – Sep 21, 2006

lore
Share & tools
lore

Ming Liu

author
#0 lore raw

Dear all,
Maybe this question sounds stupid for most software engineers. But I really have much confusion on this topic. So I have to ask for explanation.

My situation is: I want to write a driver for my custom device.(In the driver there are some functions defined to read or write my device.) Then in my application program, I use these defined functions to operate my hardware device. I think this is a normal process to operate the hardware with driver+app, right?

My driver is defined in drv_hello.c. I have compiled it into a module drv_hello.ko and it could be insmoded or rmmoded correctly. It looks like:

//drv_hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL");

void show_hello(){
printk("hello.\n");
}

static int hello_init(void){
printk(KERN_ALERT "module starts.\n");
return 0;
}

static void hello_exit(void){
printk(KERN_ALERT "module exits.\n");
}

module_init(hello_init);
module_exit(hello_exit);

EXPORT_SYMBOL(show_hello);

In my application program app_hello.c, I use the function show_hello to achieve the simple task: print "hello" on the console. Here is my app program:

//app_hello.c
extern void show_hello();

int main(int argc, char** argv)
{
show_hello();

return 0;
}

Then, here comes my problem: I don't know how to compile my application program and then it could dynamically refer to the function show_hello() defined in the memory area of the driver module. How can I tell the compiler and linker that the app program is expected to be linked with the driver module dynamically? If possible, please give me a detailed explanation.

Waiting for your suggestion. Thanks for your help.

Regards
Ming

_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn

#1 lore raw

Hi Ming,

Quoted 5 lines

My situation is: I want to write a driver for my custom device.(In the driver there are some functions defined to read or write my device.) Then in my application program, I use these defined functions to operate my hardware device. I think this is a normal process to operate the hardware with driver+app, right?

Right, but you've missed a few critical points.

For your custom device, you'll create a driver that implements *kernel space* functions my_driver_open(), read(), write(), close() etc. You'll install that module and create an appropriate device node, eg. /dev/my_driver.

In your *user-space* application code, you'll open that device

int main(argc, argv) ...

int fd = open("/dev/my_driver" ...)

and then write to your device ...

  int write(fd, buffer, len);

or read from your device ...

  int read(fd, buffer, len);

The user-space calls open, read, write eventually call down into your driver space code. The driver and application are not linked, they communicate via the /dev interface.

You really should read through Linux Device Drivers by Rubini, or check out a tutorial such as

http://www.ovro.caltech.edu/~dwh/correlator/pdf/LNX-723-Hawkins.pdf
http://www.ovro.caltech.edu/~dwh/correlator/software/driver_design.tar.gz
http://www.ovro.caltech.edu/~dwh/correlator/index.html

Regards,
Dave

author
#3 lore raw
In reply to David Hawkins

Dear Dave and Josh,
Thanks so much for your suggestion first.

Yes, it looks that I am really a beginner and there are so many points I cannot understand well. So I will follow your suggestions and go through those material first...

In fact, I am reading LDD3 now. However, it seems that the book is mainly focused on the driver part and there is not some chapter to tell how apps work with drivers. So after a rough glance, I still have much confusion. :(

I will read more to understand this topic. Thanks for your help again.

Regards
Ming

Quoted 10 lines

From: David Hawkins <[email protected]>
To: Ming Liu <[email protected]>
CC: [email protected]
Subject: Re: how an application program utilizes the driver.
Date: Wed, 20 Sep 2006 09:14:46 -0700

Hi Ming,

Quoted 2 lines

My situation is: I want to write a driver for my custom device.(In the driver there are some functions defined to read or write my device.)

Then

Quoted 2 lines
Quoted 2 lines

in my application program, I use these defined functions to operate my hardware device. I think this is a normal process to operate the

hardware

Quoted 31 lines
Quoted 1 line

with driver+app, right?

Right, but you've missed a few critical points.

For your custom device, you'll create a driver that implements *kernel space* functions my_driver_open(), read(), write(), close() etc. You'll install that module and create an appropriate device node, eg. /dev/my_driver.

In your *user-space* application code, you'll open that device

int main(argc, argv) ...

int fd = open("/dev/my_driver" ...)

and then write to your device ...

  int write(fd, buffer, len);

or read from your device ...

  int read(fd, buffer, len);

The user-space calls open, read, write eventually call down into your driver space code. The driver and application are not linked, they communicate via the /dev interface.

You really should read through Linux Device Drivers by Rubini, or check out a tutorial such as

Quoted 12 lines

http://www.ovro.caltech.edu/~dwh/correlator/pdf/LNX-723-Hawkins.pdf
http://www.ovro.caltech.edu/~dwh/correlator/software/driver_design.tar.gz
http://www.ovro.caltech.edu/~dwh/correlator/index.html

Regards,
Dave

_________________________________________________________________
享用世界上最大的电子邮件系统— MSN Hotmail。 http://www.hotmail.com

#4 lore raw
In reply to Ming Liu

this probably isn't the level /topic to ask on the list, so I suggest reading the Linux Device Drivers book...

you'll want to access the device via ioctl() calls , but there has to be a little more involved to get to that point:

experiment with the scull device driver in this book

http://lwn.net/images/pdf/LDD3/ch06.pdf

----Original Message Follows----
From: "Ming Liu" <[email protected]>
To: [email protected]
Subject: how an application program utilizes the driver.
Date: Wed, 20 Sep 2006 13:48:25 +0000

Dear all,
Maybe this question sounds stupid for most software engineers. But I really have much confusion on this topic. So I have to ask for explanation.

My situation is: I want to write a driver for my custom device.(In the driver there are some functions defined to read or write my device.) Then in my application program, I use these defined functions to operate my hardware device. I think this is a normal process to operate the hardware with driver+app, right?

My driver is defined in drv_hello.c. I have compiled it into a module drv_hello.ko and it could be insmoded or rmmoded correctly. It looks like:

//drv_hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL");

void show_hello(){
    printk("hello.\n");
}
static int hello_init(void){
    printk(KERN_ALERT "module starts.\n");
    return 0;
}
static void hello_exit(void){
    printk(KERN_ALERT "module exits.\n");
}

module_init(hello_init);
module_exit(hello_exit);

EXPORT_SYMBOL(show_hello);

In my application program app_hello.c, I use the function show_hello to achieve the simple task: print "hello" on the console. Here is my app program:

//app_hello.c
extern void show_hello();

int main(int argc, char** argv)

{
    show_hello();

    return 0;
}

Then, here comes my problem: I don't know how to compile my application program and then it could dynamically refer to the function show_hello() defined in the memory area of the driver module. How can I tell the compiler and linker that the app program is expected to be linked with the driver module dynamically? If possible, please give me a detailed explanation.

Waiting for your suggestion. Thanks for your help.

Regards
Ming

_________________________________________________________________
�����������ѽ��н�������ʹ�� MSN Messenger: http://messenger.msn.com/cn

_______________________________________________
Linuxppc-embedded mailing list
[email protected]
https://ozlabs.org/mailman/listinfo/linuxppc-embedded