Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
06-16-2004, 06:49 AM
|
#1
|
|
LQ Newbie
Registered: Jun 2004
Distribution: Suse 10
Posts: 22
Rep:
|
simple hello.c device driver for 2.6 kernel
I'm trying to build a very simple device driver using Mandrake 10 with the 2.6 kernel. Every attempt seems to fail and all the links I'm finding explain the procedure for the previous kernel versions. I found an example on a website that said this should work. Bellow is a cut and past directly from the website.
My source file "helllo2.c" looks like this.
/* hello2.c - Demonstrating the module_init() and module_exit() macros. This is the
* preferred over using init_module() and cleanup_module().
*/
#include <linux/module.h> // Needed by all modules
#include <linux/kernel.h> // Needed for KERN_ALERT
#include <linux/init.h> // Needed for the macros
static int hello_2_init(void)
{
printk(KERN_ALERT "Hello, world 2\n");
return 0;
}
static void hello_2_exit(void)
{
printk(KERN_ALERT "Goodbye, world 2\n");
}
module_init(hello_2_init);
module_exit(hello_2_exit);
The example make file looks like this
TARGET := hello2
WARN := -W -Wall
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc-3.0
${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean:
rm -rf {TARGET}.o
I've also tried building hello2.c from the command line gcc -c hello2.c and that doesn't work either.
I'm looking for help in getting this very simple driver to build, any help is greatly appreciated.
Thanks
|
|
|
|
06-16-2004, 12:01 PM
|
#2
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
Try this Makefile :
Code:
ifneq ($(KERNELRELEASE),)
obj-m := hello2.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
endif
And look at http://lwn.net/Articles/driver-porting/, there are good stuff here for 2.6 kernel
Last edited by keefaz; 06-16-2004 at 03:48 PM.
|
|
|
|
06-17-2004, 09:31 AM
|
#3
|
|
LQ Newbie
Registered: Jun 2004
Distribution: Suse 10
Posts: 22
Original Poster
Rep:
|
I tried your suggestion and I get a message that says "nothing to do for default".
I have a question on the first line ifneq ($(KERNELRELEASE),) I left this line as is, does something need to go in after the comma?
|
|
|
|
06-17-2004, 02:24 PM
|
#4
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
Don't you get your kernel module helllo2.ko issuing this Makefile ? It need nothing after the comma, I grabbed this Makefile as this from the link I mentioned above (but it seems break today).
For me it's worked, I result with a .ko module which I loads from insmod modulename.ko, when I typed dmesg, it give me expected output messages ( printk(KERN_ALERT "Hello, world 2\n"); )
|
|
|
|
06-18-2004, 12:20 PM
|
#5
|
|
Member
Registered: Dec 2001
Location: Richmond, VA
Posts: 391
Rep:
|
Can anybody help with the make file for a 2.4 kernel? I found the website you are using and I'd really like to try this on a 2.4 kernel. Once you write the make file and get the code compiled, how does it actually get added to the kernel?
http://lwn.net/Articles/21817/
|
|
|
|
06-18-2004, 12:27 PM
|
#6
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
The first Makefile described by blavo at the top of this thread is good for 2.4
It is loaded in the kernel by : insmod yourModuleName.o (for 2.4)
|
|
|
|
06-18-2004, 12:55 PM
|
#7
|
|
Member
Registered: Dec 2001
Location: Richmond, VA
Posts: 391
Rep:
|
Do I need to place my source file anywhere specific to get this to work? I wrote the make file mentioned above and ran "make hellokernel.c" and got the "Nothing to be done for hellokernel.c" message. Then I tried to compile it manually with gcc and got compile errors. It looks like the header files cannot be found.
|
|
|
|
06-18-2004, 01:00 PM
|
#8
|
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
i use this makefile daily on 2.4:
Code:
[n00b@localho.outernet] cat devdr/makefile
KERNELDIR = /lib/modules/`uname -r`/build/include/
OBJECTS = usb-skeleton.o cmos.o
WARNINGS = -Wall
CC = gcc
OPT = -O2
CFLAGS = -D__KERNEL__ -DMODULE -isystem$(KERNELDIR) $(WARNINGS) $(OPT)
all: $(OBJECTS)
clean:
rm -f $(OBJECTS) *~ core
|
|
|
|
06-18-2004, 01:56 PM
|
#9
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
AMDPwred> Adapt Makefile to suit your project by replacing target value by your module name.
ie for first Makefile:
TARGET := hellokernel
for second Makefile (provided by infamous41md) :
OBJECTS = hellokernel.o
And when invoking make do not use extension after module name. ie:
make hellokernel
|
|
|
|
06-18-2004, 02:05 PM
|
#10
|
|
Member
Registered: Dec 2001
Location: Richmond, VA
Posts: 391
Rep:
|
Ok cool, that got the make piece running. During the compile I still get those errors. Do I need to include a path to these header files in my system PATH?
hellokernel.c
Code:
/*
* hellokernel.c
* Demonstrating the init_module() and cleanup_module().
*/
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello kernel!\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Peace out kernel!\n");
}
error during make
Code:
chris@zion kernelmodule $ make hellokernel
gcc hellokernel.c -o hellokernel
hellokernel.c: In function `init_module':
hellokernel.c:12: error: `KERN_INFO' undeclared (first use in this function)
hellokernel.c:12: error: (Each undeclared identifier is reported only once
hellokernel.c:12: error: for each function it appears in.)
hellokernel.c:12: error: syntax error before string constant
hellokernel.c: In function `cleanup_module':
hellokernel.c:18: error: `KERN_INFO' undeclared (first use in this function)
hellokernel.c:18: error: syntax error before string constant
make: *** [hellokernel] Error 1
|
|
|
|
06-18-2004, 02:15 PM
|
#11
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
Try
Code:
printk("Hello kernel!\n");
Just remove KERN_INFO and KERN_ALERT, it is for 2.6
|
|
|
|
06-18-2004, 02:28 PM
|
#12
|
|
Member
Registered: Dec 2001
Location: Richmond, VA
Posts: 391
Rep:
|
Here is the error output after removing those two phrases:
Code:
chris@zion kernelmodule $ make hellokernel
gcc hellokernel.c -o hellokernel
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/../../../crt1.o(.text+0x18): In function `_start':
: undefined reference to `main'
/tmp/ccmDnTfs.o(.text+0xe): In function `init_module':
: undefined reference to `printk'
/tmp/ccmDnTfs.o(.text+0x27): In function `cleanup_module':
: undefined reference to `printk'
collect2: ld returned 1 exit status
make: *** [hellokernel] Error 1
|
|
|
|
06-18-2004, 02:46 PM
|
#13
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
verify in your /lib/modules/`uname -r`/build/include/linux directory that module.h and kernel.h exist, if yes verify in your Makefile that you have inclued the directory by parameter -isystem (which make sure that /lib/modules/`uname -r`/build/include/ will be used instead of /usr/include)
|
|
|
|
06-18-2004, 03:05 PM
|
#14
|
|
Member
Registered: Dec 2001
Location: Richmond, VA
Posts: 391
Rep:
|
Those header files don't exist in that directory. Which is weird because I remember seeing them browsing the kernel source tree one day. I can't seem to find them at the moment though.
Update:
I found a directory where they do exist:
Code:
/usr/src/linux/include/linux
I used that in the make file and I'm getting the same error. I also double checked to make sure I'm using -isystem for the include path.
Last edited by AMDPwred; 06-18-2004 at 03:13 PM.
|
|
|
|
06-18-2004, 03:21 PM
|
#15
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
what is the output of :
uname -r
grep RELEASE /usr/src/linux/include/linux/version.h
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:56 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|