LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-16-2004, 06:49 AM   #1
blavo
LQ Newbie
 
Registered: Jun 2004
Distribution: Suse 10
Posts: 22

Rep: Reputation: 0
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
 
Old 06-16-2004, 12:01 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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.
 
Old 06-17-2004, 09:31 AM   #3
blavo
LQ Newbie
 
Registered: Jun 2004
Distribution: Suse 10
Posts: 22

Original Poster
Rep: Reputation: 0
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?
 
Old 06-17-2004, 02:24 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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"); )
 
Old 06-18-2004, 12:20 PM   #5
AMDPwred
Member
 
Registered: Dec 2001
Location: Richmond, VA
Posts: 391

Rep: Reputation: 30
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/
 
Old 06-18-2004, 12:27 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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)
 
Old 06-18-2004, 12:55 PM   #7
AMDPwred
Member
 
Registered: Dec 2001
Location: Richmond, VA
Posts: 391

Rep: Reputation: 30
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.
 
Old 06-18-2004, 01:00 PM   #8
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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
 
Old 06-18-2004, 01:56 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 06-18-2004, 02:05 PM   #10
AMDPwred
Member
 
Registered: Dec 2001
Location: Richmond, VA
Posts: 391

Rep: Reputation: 30
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
 
Old 06-18-2004, 02:15 PM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Try
Code:
printk("Hello kernel!\n");
Just remove KERN_INFO and KERN_ALERT, it is for 2.6
 
Old 06-18-2004, 02:28 PM   #12
AMDPwred
Member
 
Registered: Dec 2001
Location: Richmond, VA
Posts: 391

Rep: Reputation: 30
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
 
Old 06-18-2004, 02:46 PM   #13
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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)
 
Old 06-18-2004, 03:05 PM   #14
AMDPwred
Member
 
Registered: Dec 2001
Location: Richmond, VA
Posts: 391

Rep: Reputation: 30
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.
 
Old 06-18-2004, 03:21 PM   #15
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
what is the output of :
uname -r
grep RELEASE /usr/src/linux/include/linux/version.h
 
  


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
C++ or pure C for Linux kernel module, Linux device driver development. What to use? Igor007 Programming 91 07-19-2014 11:55 AM
device driver carthyc Linux - Newbie 1 07-07-2005 07:23 AM
need help on writting a simple quickbutton driver captainstorm Programming 1 03-02-2004 01:05 PM
device driver rjc915 Programming 4 10-06-2003 11:43 AM
creating a simple module(device driver) pessanimahi Linux - Software 1 09-16-2003 11:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 AM.

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