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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-06-2014, 03:33 AM
|
#1
|
Member
Registered: Dec 2013
Location: Japan
Distribution: Debian...very few times Ubuntu
Posts: 51
Rep:
|
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.
|
|
|
10-08-2014, 08:10 AM
|
#2
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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.
|
10-08-2014, 08:11 AM
|
#3
|
Moderator
Registered: Aug 2002
Posts: 26,807
|
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
|
|
|
10-08-2014, 08:35 AM
|
#4
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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.
|
10-08-2014, 07:56 PM
|
#5
|
Member
Registered: Dec 2013
Location: Japan
Distribution: Debian...very few times Ubuntu
Posts: 51
Original Poster
Rep:
|
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
|
|
|
10-08-2014, 08:48 PM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
Quote:
Originally Posted by KansaiRobot
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.
|
10-09-2014, 09:55 PM
|
#7
|
Member
Registered: Dec 2013
Location: Japan
Distribution: Debian...very few times Ubuntu
Posts: 51
Original Poster
Rep:
|
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 
|
|
|
10-10-2014, 07:25 AM
|
#8
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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.
|
10-10-2014, 10:24 AM
|
#9
|
Member
Registered: Mar 2007
Location: Whitsett,NC
Distribution: Slackware 14.1 and MINT 17.1
Posts: 105
Rep:
|
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.
|
All times are GMT -5. The time now is 06:32 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|