View Single Post
 
Old September 27th, 2005, 12:30 PM
ninuzzo ninuzzo is offline
Registered User
 
Join Date: Sep 2005
Location: Castrovillari, , Italy.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default porting ch18 sample code to new kernels

Hi,
I'm posting these notes in the hope that they could be useful
to other readers.

While trying out sample code from Chapter 18 (Device
Drivers) I realized it doesn't work with 2.6+ kernels and
probably neither with 2.5.

Even if the code compiles, the resulting module cannot be
loaded into the kernel. This is because these more recent
kernel versions introduced many changes while the examples
were written at the time the 2.4 kernel series was in use.

Searching google I found this site which is an excellent
source of information about porting device drivers to the
2.6 kernel:

http://lwn.net/Articles/driver-porting/

E.g., here is the "hello kernel" program on page 727
rewritten for kernel 2.5 and above:

/* hello.c */
/*
 * Hello world module.
 */
#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
        printk(KERN_DEBUG "Hello, kernel!\n");
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_DEBUG "Good-bye, kernel!\n");
}

/* This is to avoid warnings when loading the module. */
MODULE_LICENSE("GPL");

Please note that the kernel source dir
/usr/src/linux-version or whatever you choose must be
configured using "make menuconfig" before compiling this
module. Also the kernel build mechanism changed and you
need a new Makefile:

# Makefile
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /usr/src/linux-$(shell uname -r)
PWD := $(shell pwd)

all:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
        rm -f *.mod.c *.ko *.o *~ core
endif

Note: if you cut & paste this ensure there is a tab before
every rule - see page 368.

Alternately you can use a simpler Makefile:

# Makefile
obj-m := hello.o

clean:
        rm -f *.mod.c *.ko *.o *~ core

but you will need a more elaborate command to build the
module instead of a simple "make":

$ make -C /usr/src/linux-version SUBDIRS=$PWD modules

Modules are now special kernel object ending with .ko in
place of .o:

# insmod hello.ko

PS:
I'd like to port subsequent examples if the book authors
haven't already done it.
Of course If I succeed I will post them here under GPL :-)

Should I run into problems will you - Neil, Rick or somebody
else - please help me, answering my specific questions or
pointing me out to other documentation?

antonio