LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-06-2014, 03:33 AM   #1
KansaiRobot
Member
 
Registered: Dec 2013
Location: Japan
Distribution: Debian...very few times Ubuntu
Posts: 51

Rep: Reputation: 0
Device Drivers- programming


Hello everybody.

I have recently started reading some books about device driver porgramming, (Linux Device Drivers, Third Edition By Jonathan Corbet, Writing Linux Device Drivers etc) and found an excellent resource in

http://www.freesoftwaremagazine.com/.../drivers_linux

Anyway, this is all quite new for me. While the info is quite nice and I can understand it, as usual they leave the most basic and probably obvious thing without saying: How do I even compile the most basic example?? or even more precisely:
how do I "set a suitable system with a configured and built kernel source tree " which I think is a requisite to start?

I mean, ok I download the source file of the kernel, I put it somewhere on my file tree and then what? Do I just compile the driver program? or do I have to recompile the whole kernel? or what?

In a related point, does the linux I am developing the driver for has to be the same linux I am using as developing environment? I ask this because I am using a Devian linux running on a PC to develop but the target is a uCLinux on an arm microcontroller.

But back to the basics, how do I even compile the most basic -do nothing- driver?

Thanks a lot for the help

Kansai

P.S. this happens a lot to me, great tutorials explain very complicated things but just leave the most basic thing unexplained.

Last edited by KansaiRobot; 10-06-2014 at 03:57 AM.
 
Old 10-08-2014, 08:10 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
I did this over a decade ago so my knowledge is rusty at best. But since no one seems to answer you...

Strictly speaking you need only the kernel headers of the running kernel to compile your module. But that is the running kernel, you might want to use your own kernel.

I can recommend that you do a step at a time.

First install the kernel tree for your distribution. There are many articles about how to do that. Compile it by running make. Then add your own module somewhere in the module tree. Look in other module subdirectories how the makefiles are defined if there are any. It can be that the makefile for the entire module tree is at the top of the module tree. I don't recall.

I can be that you have to put your makefile in the same directory as your module. The makefile on the next higher level will execute this one. Insctructions on how to set up a makefile are in chapter 2 of the O'Reilly book. It is not all that clear, you have to download and look at the examples, combine and understand.

If you have succesfully compiled and inserted the Hello World driver in your running kernel you might want to set up a custom kernel tree. There are plenty of instructions for that on the internet as well.

Compile again but treat this kernel as not you running kernel and see if you can install it, boot, and insert the module.

Once this works you can continue developing your module. Compile only your module from now on. Because you just compiled the kernel with these same headers, your module will run when it is inserted.

Quote:
P.S. this happens a lot to me, great tutorials explain very complicated things but just leave the most basic thing unexplained.
I have been there as well. Either the author assumes some basic knowledge or he doesn't have an idea that the reader might not understand it. The other side of the spectrum are those books where for every action all window dialogs are printed and that over and over again, even for a file which has to be saved. In this situation it is not too difficult to find information about how to install kernel trees and compile a module. But it is all bit & pieces. The book focuses on developing modules.

jlinkels
 
1 members found this post helpful.
Old 10-08-2014, 08:11 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,807

Rep: Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333Reputation: 6333
No, it does not have to be the same architecture for development vs target.

I would say the authors of the tutorials etc assume that you are developing the driver on the "target" device itself. All it takes for most distributions is to use its package management system to install the compiler, source files etc. Setting up a cross compiler environment would be a bit out of scope unless the book/tutorial was specifically for embedded applications IMHO.

Did your embedded board include any sort of development tools?

http://www.uclinux.org/index.html
 
Old 10-08-2014, 08:35 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
Once you master compiling modules on your own system, it is only a small step (well relatively...) to do it for a foreign architecture. That is why I propose a step-by-step approach.

It can be done both on the target itself, or using a compiler directive. In both cases you need the complete kernel tree of the target installed on the system where you compile.

jlinkels
 
2 members found this post helpful.
Old 10-08-2014, 07:56 PM   #5
KansaiRobot
Member
 
Registered: Dec 2013
Location: Japan
Distribution: Debian...very few times Ubuntu
Posts: 51

Original Poster
Rep: Reputation: 0
thank you very much for your responses. I have been torturing myself over the sight of "O replies"

Anyway, I have ubuntu and devian, and found out that the kernel source is not installed by default so I downloaded the kernel source.
Before of that I tried to compile it through the headers only but it didnt work.

Anyway, I thought ubuntu would be better but it is a pain (it doesnt even have the necessary "ncurses"?? to run make menuconfig) so I am back at devian. I ll try to build the kernel first today.

----------------

Bythe way, something ocured to me yesterday. Obviusly the tools for building executables for a PC and for an arm architecture are different but... the kernel source files ** are the same** right? , after all they are all just text files, arent they??

fortunately I have the cross compiler ready (I have built user space programs for that architecture without problem) but as jlinkels said, I want to first try my luck on the PC (my own) system first.

Anyway, thanks and I ll post my results

Any advice of course is greatly appreciated
 
Old 10-08-2014, 08:48 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
Quote:
Originally Posted by KansaiRobot View Post
Anyway, I thought ubuntu would be better but it is a pain (it doesnt even have the necessary "ncurses"?? to run make menuconfig) so I am back at devian. I ll try to build the kernel first today.
If I were a software engineer I would stay far from Ubuntu. Debian is much better and designed to give you access to the system internals. For kernel compiling you should follow the "Debian way". Once you master that you can see where the Debian framework is in your way and deviate from it if you need to. There is information available on how to compile a kernel in Debian and how to add/compile modules. Just google it.

jlinkels
 
1 members found this post helpful.
Old 10-09-2014, 09:55 PM   #7
KansaiRobot
Member
 
Registered: Dec 2013
Location: Japan
Distribution: Debian...very few times Ubuntu
Posts: 51

Original Poster
Rep: Reputation: 0
Thank you for all the help

Well, I built the kernel and then later compiled succesfuly my first driver (just a "hello world" one :P)

but when I try to do insmod to it I got the invalid module format error.

I searched and it seems this is because the kernel version I used to build it and the kernel version I am running on are different

built with 3.16.4 and running 3.2.0-4-686-pae

anyway, I read some solutions that says "rebuild with the old kernel"

is that the only way??? (I can not update my running kernel to the newest one afaik, wouldnt that have big consequences for other things that are already running??)

your advice will be greatly appreciated
 
Old 10-10-2014, 07:25 AM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
It is correct that you cannot use your new module in your old kernel. Kernel and module must be the same version.

That is, the new module must have been compiled against (as they say it) the running kernel. You can legally cheat and compile the module against the kernel headers of the kernel you run. It will also work.

So step #1: compile the module against your running kernel. It should load.

step #2: install a different kernel source, compile it with your module. If you followed the Debian kernel compile procedure (which you really should have done) the result of your kernel compilation is a .deb file. You can install it and boot into the new kernel. Your module should load.

You can have different kernels installed on your machine. Grub will present you with a menu which one to boot. Some of the very new kernels might not work if you run Debian Stable. Then chose a kernel which is closer to the version you are running now. Either older or newer. IIRC kernels 2.6.32 and 3.2 can both run on Wheezy. So everything in between as well.

jlinkels
 
1 members found this post helpful.
Old 10-10-2014, 10:24 AM   #9
Gary Baker
Member
 
Registered: Mar 2007
Location: Whitsett,NC
Distribution: Slackware 14.1 and MINT 17.1
Posts: 105

Rep: Reputation: 3
This is just my two cents worth but I have had some very good tutorials on linux kernal programming on Youtube. Why don't you check out device drivers on Youtube. I think you will be surprised they are step by step. Good luck.
 
1 members found this post helpful.
  


Reply

Tags
drivers, kernel


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
How to start with programming serial device drivers in Linux swapnilborg Linux - Hardware 2 08-21-2006 01:45 AM
I want some guide to know the basic about device drivers programming in linux. kadhiravan.r Linux - Software 2 07-18-2005 07:07 AM
Kernel Programming/Device Drivers Matir Programming 2 04-06-2005 03:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:32 PM.

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