LinuxQuestions.org
Visit Jeremy's Blog.
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 11-15-2007, 01:32 AM   #1
jeyram
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 26

Rep: Reputation: 15
Cool 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-
 
Old 11-15-2007, 03:11 AM   #2
adisoft83
LQ Newbie
 
Registered: Sep 2004
Posts: 8

Rep: Reputation: 0
Wink

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!

Last edited by adisoft83; 11-15-2007 at 03:13 AM. Reason: typo (module.o)
 
Old 11-16-2007, 07:27 AM   #3
jeyram
LQ Newbie
 
Registered: Jun 2007
Distribution: Ubuntu
Posts: 26

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Device drivers?? ramesh6056 Linux - Software 2 07-03-2007 10:33 PM
New to device drivers sksingh Linux - Software 1 08-02-2004 07:03 AM
Device Drivers reza_asghari Programming 5 08-30-2002 01:05 PM
Looking for device drivers... Chijtska Linux - Software 3 02-19-2002 06:55 PM
device drivers Susan Linux - Newbie 3 10-11-2001 07:36 AM

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

All times are GMT -5. The time now is 10:28 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