LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 08-27-2007, 05:38 AM   #1
i_love_linux
LQ Newbie
 
Registered: Aug 2007
Location: India
Posts: 8

Rep: Reputation: 0
Question Compiling Device Driver in Linux


I have written a simple program to access kernel as follows.

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

MODULE_LICENSE("Dual BSD/GPL");

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

static void hello_exit(void) {
printk("<1> Bye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

When I compile it using gcc -c kernel_module.c (I am doubtful about this compilation method. I think it needs some more words for compiling the program properly)
Then I use
insmod kernel_module.o

It shows me error the program is compiler for kernel version 2.4.20 and your kernel version is 2.4.29-8.

Can you tell me solution for this problem and may be some proper way of compiling it
 
Old 08-27-2007, 09:57 AM   #2
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
Quote:
Originally Posted by i_love_linux View Post
gcc -c kernel_module.c[/B] (I am doubtful about this compilation method. I think it needs some more words for compiling the program properly)
Its better to use make file as following
Code:
objm:= kernel_module.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
Quote:
Then I use
insmod kernel_module.o
You must be root for this.

Quote:
It shows me error the program is compiler for kernel version 2.4.20 and your kernel version is 2.4.29-8.
The error you mentioned is not clear to me but still just giving a guess, maybe the kernel source and the kernel you are running are not the same.

Last edited by hiren_bhatt; 08-28-2007 at 08:16 AM.
 
Old 08-27-2007, 08:14 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,401

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
When you compile any module, you must point to the correct kernel sources for the kernel that you are now running. Mismatches will be detected and the compile will fail.
 
Old 08-28-2007, 01:22 AM   #4
i_love_linux
LQ Newbie
 
Registered: Aug 2007
Location: India
Posts: 8

Original Poster
Rep: Reputation: 0
Exclamation

Quote:
Originally Posted by hiren_bhatt View Post
Its better to use make file as following
Code:
objm:= kernel_module.o
KDIR := /lib/modules/$(shell uname r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

You must be root for this.



The error you mentioned is not clear to me but still just giving a guess, maybe the kernel source and the kernel you are running are not the same.
It is basically showing me kernel version mismatch error as the code is getting compiled for kernel 2.4.20 and the kernel is 2.4.20-8. Is there any method with which I can make my program run on kernel of different version. Or do I need to recompile kernel source and then try to insert my module.
 
Old 08-28-2007, 01:23 AM   #5
i_love_linux
LQ Newbie
 
Registered: Aug 2007
Location: India
Posts: 8

Original Poster
Rep: Reputation: 0
Exclamation

Quote:
Originally Posted by sundialsvcs View Post
When you compile any module, you must point to the correct kernel sources for the kernel that you are now running. Mismatches will be detected and the compile will fail.
It is basically showing me kernel version mismatch error as the code is getting compiled for kernel 2.4.20 and the kernel is 2.4.20-8. Is there any method with which I can make my program run on kernel of different version. Or do I need to recompile kernel source and then try to insert my module.
 
Old 08-28-2007, 04:50 AM   #6
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
Quote:
Originally Posted by i_love_linux View Post
It is basically showing me kernel version mismatch error as the code is getting compiled for kernel 2.4.20 and the kernel is 2.4.20-8. Is there any method with which I can make my program run on kernel of different version. Or do I need to recompile kernel source and then try to insert my module.
There is a way to do this using modversioning. But then you kernel should be compiled with then modversioning flag on(which is default off)Also you need modversioning.h file(which i am not sure will be there or not)

The better way is to get the source of the same kernel that you use.
 
Old 08-28-2007, 07:59 AM   #7
clnbabu
LQ Newbie
 
Registered: Aug 2007
Posts: 8

Rep: Reputation: 0
i too trying to do samething.I have installed FC-7 on my computer.In the i am found there is no source files present..please give me the links where i can get the source of FC-7.so that i will put it in /usr/src/redhat/ folder
 
Old 08-28-2007, 08:31 AM   #8
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
Quote:
Originally Posted by clnbabu View Post
i too trying to do samething.I have installed FC-7 on my computer.In the i am found there is no source files present..please give me the links where i can get the source of FC-7.so that i will put it in /usr/src/redhat/ folder
I have not upgraded to FC7 yet, nor I have much hands-on managing packages using yum. Since fedora now uses yum you can get kernel source using that only.
A quick google search has resulted into the following link. This guides you not only how to install Kernel source but many other things.
http://www.mjmwired.net/resources/mj...html#kernelsrc
 
Old 08-29-2007, 06:34 AM   #9
clnbabu
LQ Newbie
 
Registered: Aug 2007
Posts: 8

Rep: Reputation: 0
today i am downloading kernel-devel-2.6.21-1.3194.fc7.i686.rpm from the net..i will try to run this rpm.Thank you
 
Old 08-29-2007, 11:54 PM   #10
i_love_linux
LQ Newbie
 
Registered: Aug 2007
Location: India
Posts: 8

Original Poster
Rep: Reputation: 0
Smile Writing character device driver in linux

Quote:
Originally Posted by hiren_bhatt View Post
There is a way to do this using modversioning. But then you kernel should be compiled with then modversioning flag on(which is default off)Also you need modversioning.h file(which i am not sure will be there or not)

The better way is to get the source of the same kernel that you use.
Thanks for your help. But the mail problem with version mismatch was the way I was compiling my module. Problem is usually what they tell you the way to compile a module is gcc -c filename.c which is totally wrong for kernel versions 2.4 or 2.6.

One thing I wanted to ask is suppose I have one device which operates on serial port. I want to read/ write into this device using linux. How should I start with it? And where can I see the data coming from this device? Like in windows you have hyperterminal to view data transaction from and in serial port of PC. As I am new to write device driver I have asked such a basic question. I think this would be a smiplest device driver to start with. Can you help me out with this please?
 
Old 08-30-2007, 12:03 AM   #11
i_love_linux
LQ Newbie
 
Registered: Aug 2007
Location: India
Posts: 8

Original Poster
Rep: Reputation: 0
Cool

Quote:
Originally Posted by hiren_bhatt View Post
I have not upgraded to FC7 yet, nor I have much hands-on managing packages using yum. Since fedora now uses yum you can get kernel source using that only.
A quick google search has resulted into the following link. This guides you not only how to install Kernel source but many other things.
http://www.mjmwired.net/resources/mj...html#kernelsrc
Hey that is one cool link you have given ..

One more thing I want to ask is if I load Adobe Acrobat PDF reader on Red Hat Linux 7 it doesn't work and it shows me some qt 2.4 ..package (I don't remember it exactly) missing. Is it not possible to view pdf files in linux as we do conventionly in windows? Or would there be any rpm missing? There is one pdf viewer which comes with linux but it does not have graphics and display as Adobe Acrobat pdf reader does.
 
Old 08-30-2007, 07:41 AM   #12
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
Quote:
Originally Posted by i_love_linux View Post
One more thing I want to ask is if I load Adobe Acrobat PDF reader on Red Hat Linux 7 it doesn't work and it shows me some qt 2.4 ..package (I don't remember it exactly) missing.
It may be showing you for GTK 2.4 because I think Adobe Reader 7 needs
that. Just check if you have GTK of that version, if not just get it updated.
Quote:
Is it not possible to view pdf files in linux as we do conventionly in windows? Or would there be any rpm missing? There is one pdf viewer which comes with linux but it does not have graphics and display as Adobe Acrobat pdf reader does.
XPDF, KPDF can be used to view pdfs. Yes it may not be like Adobe and may not offer few things like search, but still they are good at least they don't require as much memory as Adobe Reader requires.
 
Old 09-03-2007, 02:59 PM   #13
clnbabu
LQ Newbie
 
Registered: Aug 2007
Posts: 8

Rep: Reputation: 0
pci driver

i want to develope a relay based pci card...please help me how to proceed for this....
 
Old 09-04-2007, 09:24 AM   #14
johnhamiltion
Member
 
Registered: Aug 2007
Posts: 92

Rep: Reputation: 15
Quote:
Originally Posted by clnbabu View Post
i want to develope a relay based pci card...please help me how to proceed for this....
Start by reading Greg Kroah-Hartman's book

"The Linux Device Driver Kit."

You can download it (as a single web-page) from

http://m.domaindlx.com/LinuxHelp/ldd3/about-ldd3.htm

Last edited by jtshaw; 04-06-2008 at 11:37 AM.
 
  


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
Linux Device Driver malacma Linux - Software 10 10-09-2007 06:14 AM
linux device driver faris10 Linux - Kernel 4 07-25-2007 09:51 AM
compiling raw source code of device driver bishalpoudyal Linux - Hardware 1 04-08-2006 03:09 AM
Linux Device Driver Bible? yspm Programming 2 01-03-2006 09:30 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration