Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Looking for a device driver template (solved)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
IRQsRFun
Apprentice
Apprentice


Joined: 02 May 2003
Posts: 195
Location: Somewhere between .3 and .7 Vdd

PostPosted: Mon Nov 05, 2007 12:34 am    Post subject: Looking for a device driver template (solved) Reply with quote

I am currently looking at an O'Rielly book Linux Device Drivers (for linux version 2.4). I now realize there may be some fundamental differences between kernel 2.4 and 2.6 (duh).

When I try to build the first example, I get a large number of warnings and errors.

Here is a what the example is trying to do (text changed):

Code:

int init_module(void)      { printk("<1>Init Module\n"); return 0; }
void cleanup_module(void)  { printk("<1>Cleanup Module\n"); }


I know this is insanely simple, but I have to start somewhere.

I have looked around in the linux sources, but I did not find anything I felt confortable using witout some guidance.

Any help would be appreciated.


Last edited by IRQsRFun on Sat Nov 10, 2007 2:18 pm; edited 1 time in total
Back to top
View user's profile Send private message
entox
n00b
n00b


Joined: 19 Jun 2005
Posts: 36

PostPosted: Wed Nov 07, 2007 6:37 pm    Post subject: Reply with quote

Hi
also had some trouble with this.
Here is a simple HelloWorld-Module:
Code:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static int hello_init(void) {
   printk(KERN_ALERT "Hello World \n");
   return 0;
}
static void hello_exit(void) {
   printk(KERN_ALERT "Finishing ... \n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Entox");


Afaik in 2.6 you have to register the init / exit -Function explicit using module_init, module_exit.

Entox
Back to top
View user's profile Send private message
IRQsRFun
Apprentice
Apprentice


Joined: 02 May 2003
Posts: 195
Location: Somewhere between .3 and .7 Vdd

PostPosted: Thu Nov 08, 2007 12:00 am    Post subject: Thank you but.... Reply with quote

Thank you for your reply.

So far, I have had to add:
Code:

#define __KERNEL__

before
Code:

#include <linux/module.h>

I am still working on other compile errors. Is there something you are using to setup gcc before compiling?
I am currently doing this to build:
Code:

gcc -c -I/usr/src/linux/include hello.c &> temp 



Any help would be appreciated.
Back to top
View user's profile Send private message
IRQsRFun
Apprentice
Apprentice


Joined: 02 May 2003
Posts: 195
Location: Somewhere between .3 and .7 Vdd

PostPosted: Thu Nov 08, 2007 3:02 am    Post subject: Reply with quote

Update:

Got your original to compile by adding
#include <linux/autoconf.h>
on the top line.

I also had to take from a makefile and get:
Code:

KERNELDIR = /usr/src/linux

include $(KERNELDIR)/.config

CFLAGS = -D__KERNEL__ -DMODULE -I$(KERNELDIR)/include \
  -O -Wall

ifdef CONFIG_SMP
 CFLAGS += -D__SMP__ -DSMP
endif

all: hello.o


When I execute
Code:

insmod hello.o   

dmesg reports:
Quote:

No module found in object

insmod reports:
Quote:

insmod: error inserting 'hello.o': -1 Invalid module format


Any help would be appreciated.
Back to top
View user's profile Send private message
dev_zero
n00b
n00b


Joined: 01 Nov 2007
Posts: 28
Location: Somewhere in the kernel source

PostPosted: Thu Nov 08, 2007 4:38 am    Post subject: Reply with quote

This is how the makefile should look like:
Code:
obj-m += hello-1.o
all:
   make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
   make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean



Btw there is a guide about kernel module programming over at tldp. http://tldp.org/LDP/lkmpg/2.6/html/index.html
_________________
Dell Latitude D630 DSDT
Back to top
View user's profile Send private message
IRQsRFun
Apprentice
Apprentice


Joined: 02 May 2003
Posts: 195
Location: Somewhere between .3 and .7 Vdd

PostPosted: Fri Nov 09, 2007 2:11 am    Post subject: Reply with quote

Thank you dev_zero for the response and the link.

However, I am still having trouble.

I am using the makefile and the hello world program in the above link. (same makefile as above) If you are new to makefiles and reading this, please undestand that make uses tabs, not spaces for indentation.

Here is my error:
Quote:

prompt # make
make -C /lib/modules/2.6.24-rc2/build M=/root/Desktop/hello_world modules
make: *** /lib/modules/2.6.24-rc2/build: No such file or directory. Stop.
make: *** [all] Error 2


Quote:

prompt # ls /lib/modules/2.6.24-rc2
modules.alias modules.ieee1394map modules.ofmap modules.symbols
modules.ccwmap modules.inputmap modules.pcimap modules.usbmap
modules.dep modules.isapnpmap modules.seriomap video


As shown, there is no file named build in lib/modules/2.6.24-rc2.


Any help would be appreciated
Back to top
View user's profile Send private message
IRQsRFun
Apprentice
Apprentice


Joined: 02 May 2003
Posts: 195
Location: Somewhere between .3 and .7 Vdd

PostPosted: Sat Nov 10, 2007 2:17 pm    Post subject: Reply with quote

Success!!


Somewhere between 2.6.18-gentoo-r2 and 2.6.18-gentoo-r3 the symbolic link build was not included in /lib/modules/XXXX. Therefore, I had to change my makefile to:
Code:

obj-m = hello.o

# all:
#    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
all:
   make -C /usr/src/linux M=$(PWD) modules

# clean:
#    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
clean:
   make -C /usr/src/linux M=$(PWD) clean


thank you to all who have tried to help me with this hello world build.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum