LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Device drivers (https://www.linuxquestions.org/questions/programming-9/device-drivers-599760/)

jeyram 11-15-2007 01:32 AM

Device drivers
 
hi,
i have started to learn Device driver programing.

this is my first coding.

#define MODULE
#include <linux/module.h>

int init_module(void){printk("<1>Hello world\n"); return 0;}

void cleanup_module(void){ printk("<1>bye\n"); }


i can compile this coding. but when i try to run this code i got error.

this is a error message:
insmod: error inserting './hello.o': -1 Invalid module format

please help me.

-thanks-

adisoft83 11-15-2007 03:11 AM

You have to set up a kernel make enviroment. So a simple gcc would not suffice. You need to have both a makefile, and a kbuild file. I can't tell you for the latest kernel, I worked on an older kernel, and the build process may have changed a bit.

Let's say you have the following module:
Code:

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

MODULE_DESCRIPTION("My kernel module");
MODULE_AUTHOR("Me");
MODULE_LICENSE("GPL");

static int dummy_init(void)
{
        printk( KERN_DEBUG "Hi\n" );
        return 0;
}

static void dummy_exit(void)
{
        printk( KERN_DEBUG "Bye\n" );
}

module_init(dummy_init);
module_exit(dummy_exit);

Let' say you call this module.c
You will also need a Makefile, and a kbuild file:
The makefile:
Code:

KDIR=/lib/modules/`uname -r`/build

kbuild:
        make -C $(KDIR) M=`pwd`

clean:
        make -C $(KDIR) M=`pwd` clean

The Kbuild file: (note that you may only need the kbuild file.. dunno the newer kernels - that should have worked on 2.6.12)
Code:

EXTRA_CFLAGS=-g

obj-m        = module.o

and then make!. The output should be a .ko module. You may need to tune this a bit to work on the current kernel versions.

Links:
http://tldp.org/LDP/lkmpg/2.6/html/index.html - the linux kernel programming guide
http://lwn.net/images/pdf/LDD3/ch02.pdf // linux Device drivers third edition, 2nd chapter

There is also a cd Linux ddk, google it and you will get everything you need (including a kernel) on one big .iso file.

A small tip: never sleep while holding a lock!

jeyram 11-16-2007 07:27 AM

thanks adisoft83,

now i got it.

i am using 2.6.20-16-generic.

first i wrote your program (hello2.c) and after that i wrote a makefile like this:

obj-m := hello2.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

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

that's all

and thankyou for your resource.
first i used ldd 2nd edition. that is suitable for 2.4 kernel.

-jeyram


All times are GMT -5. The time now is 03:17 PM.