linux驱动源码
Linux内核源码路径:/usr/src/linux(这个源码是从kernel.org网站download的2.4.18版本)
按照《linux设备驱动开发详解》一书中的步骤实现经典例子"hello,world!"的例子。
具体步骤如下:
=============================================
1.源码如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
进入目录:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然后拷入上面书上的源码。
2.编译代码:
1>.首先我在2.4内核的虚拟机上进行编译,编译过程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I选项指定内河源码,也就是内核源码树路径。编译结果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在网上查询有网友提示没有引入kernel.h
解决:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次编译仍然报KERN_ALERT没有声明
修改编译条件-I,再次编译:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接着我尝试在2.6内核的虚拟机上进行编译
编译过程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
3.执行代码,加载驱动模块:
2.4内核加载模块:
insmod ./hello.o
但是此时并没有输出printk打印的信息。但是可以在/var/log/messages 中看到打印的信息,这是由于KERN_ALERT优先级不够高。这里
需要修改为:KERN_EMERG。再次编译,加载模块即可以看到结果
2.6内核加载模块:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能会出现insmod命令找不到的错误,这可能有下面几个原因:
<1> 你的系统没有安装mole-init-tools工具,关于此问题,只需安装即可,但是一般装完系统是有这个命令的。
<2> 环境变量没有添加导致不能使用该命令。使用echo $PATH即可查看PATH环境变量,发现没有/sbin这个路径,所以你当然不能使用insmod这个命令了。解决的方法很简单,只需在命令行输入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin这个目录下,你可以使用whereis insmod查看)。
<3> insmod这个命令需要在root权限下才能使用。
加载完成后你可以输入lsmod查看hello这个模块哦。
4.卸载驱动模块:rmmod hello.
加载模块后就可在屏幕上看到如下信息:Hello world enter.
卸载时就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
另外,如果有多个文件,则按下列方式编写Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o
转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦
2. 哪位知道HDMI的驱动源码在linux源码包的哪个目录下,具体是 xxx.c
应该都在这了,hdmi.c
3. Linux内核源码在哪里
一般在Linux系统中的/usr/src/linux*.*.*(*.*.*代表的是内核版本,如2.4.23)目录下就是内核源代码(如果没有类似目录,是因为还没安装内核代码)。另外还可从互连网上免费下载。注意,不要总到http://www.kernel.org/去下载,最好使用它的镜像站点下载。请在http://www.kernel.org/mirrors/里找一个合适的下载点,再到pub/linux/kernel/v2.6/目录下去下载2.4.23内核。
代码目录结构
在阅读源码之前,还应知道Linux内核源码的整体分布情况。现代的操作系统一般由进程管理、内存管理、文件系统、驱动程序和网络等组成。Linux内核源码的各个目录大致与此相对应,其组成如下(假设相对于Linux-2.4.23目录):
1.arch目录包括了所有和体系结构相关的核心代码。它下面的每一个子目录都代表一种Linux支持的体系结构,例如i386就是Intel CPU及与之相兼容体系结构的子目录。PC机一般都基于此目录。
2.include目录包括编译核心所需要的大部分头文件,例如与平台无关的头文件在include/linux子目录下。
3.init目录包含核心的初始化代码(不是系统的引导代码),有main.c和Version.c两个文件。这是研究核心如何工作的好起点。
4.mm目录包含了所有的内存管理代码。与具体硬件体系结构相关的内存管理代码位于arch/*/mm目录下。
5.drivers目录中是系统中所有的设备驱动程序。它又进一步划分成几类设备驱动,每一种有对应的子目录,如声卡的驱动对应于drivers/sound。
6.ipc目录包含了核心进程间的通信代码。
7.moles目录存放了已建好的、可动态加载的模块。
8.fs目录存放Linux支持的文件系统代码。不同的文件系统有不同的子目录对应,如ext3文件系统对应的就是ext3子目录。
Kernel内核管理的核心代码放在这里。同时与处理器结构相关代码都放在arch/*/kernel目录下。
9.net目录里是核心的网络部分代码,其每个子目录对应于网络的一个方面。
10.lib目录包含了核心的库代码,不过与处理器结构相关的库代码被放在arch/*/lib/目录下。
11.scripts目录包含用于配置核心的脚本文件。
12.documentation目录下是一些文档,是对每个目录作用的具体说明。
一般在每个目录下都有一个.depend文件和一个Makefile文件。这两个文件都是编译时使用的辅助文件。仔细阅读这两个文件对弄清各个文件之间的联系和依托关系很有帮助。另外有的目录下还有Readme文件,它是对该目录下文件的一些说明,同样有利于对内核源码的理解。
在阅读方法或顺序上,有纵向与横向之分。所谓纵向就是顺着程序的执行顺序逐步进行;所谓横向,就是按模块进行。它们经常结合在一起进行。对于Linux启动的代码可顺着Linux的启动顺序一步步来阅读;对于像内存管理部分,可以单独拿出来进行阅读分析。实际上这是一个反复的过程,不可能读一遍就理解。
4. linux驱动中内核源码树和加载模块的问题
恩? 理解不全,要重新编译一个内核来使用,要注意两点:
a,安装 LKMs 到 /lib/moles 的子目录下;
b,将新的内核映像拷贝到 /boot 分区中,并配置 grub/lilo;
centos/redhat 发行版中的内核版本有自己很多的补丁修改,不同于主流的
从 kernel.org 中下载下来的内核。你只是简单的修改版本名称那是不行的,
mod version 内部还是会被认为是两个不同的内核(实际上也是不同的)。
在 centos/redhat 上为本机运行着的内核开发驱动,实际上可以不用去下载
任何版本的内核代码树,你只需要安装 相对应的 kernel-devel rpm 包即可。
当然,为了更方便,你可以直接从 centos 官网下载合适的源码包来重新编
译后再开发你的驱动程序。
更多问题,可以上巨立安邮件列表中进行交流。
巨立安技术是你在arm/x86下学习 linux 开发的上佳指导。
5. 请问各位大神,做linux驱动移植时,内核没有的一些外设的驱动源码一般是去哪里找呢
linux内核源码,能支持的驱动已经是很丰富的了。
如果没有具体型号的外设驱动,那么,就参考linux内核已有的驱动源码,来修改。
比如:网卡驱动
A板卡,网络芯片是DM9000,在linux-x.x.x/drivers/net目录下有dm9000.c驱动;
现在要做网络芯片是CS8900,没有驱动,就可以参考DM9000驱动,
对照芯片手册,修改驱动,编译通过,调试等。
6. linux-3.18.1内核源代码的block设备驱动源码的这些.c文件分别负责什么功能,来个大神给我解释一下。
每个c文件头里拍世有说明啊弊谨
比如cciss是 HP Smart Array block driver,是一个比较旧袭卜肢的HP RAID控制器块驱动
7. AT070TN92 linux驱动源码
是去外设芯片的官网还是一些着名论坛?还是要自己写。。。。求解答。。
8. linux 2.6.32提供驱动程序源码吗为什么都是 .ko文件这些文件能打开吗
.ko文件是编译好的驱动模块,打不开,但可以动态加枣拆载进内核使用。linux系统是开源的,内核源带隐代码蠢岩厅都是可以下载到的
9. 驱动开发必须使用开发板厂家提供的Linux源码吗
驱动开发环境
要进行linux驱动开发我们首先要有linux内核的源码树,并且这个linux内核的源码树要和开发板中的内核源码树要一直;
比如说我们开发板中用的是linux kernel内核版本为2.6.35.7,在我们ubuntu虚拟机上必须要有同样版本的源码树,
我们再编译好驱动的的时候,使用modinfo XXX命令会打印出一个版本号,这个版本号是与使用的源码树版本有关,如果开发板中源码树中版本与
modinfo的版本信息不一致使无法安装驱动的;
我们开发板必须设置好nfs挂载;这些在根文件系统一章有详细的介绍;
10. linux的U盘驱动源码
根据你的描述,估计FUSE不适合你。
参考以下几个文件吧(我的系统是DEBIAN LENNY)
localhost:/home/aaa/program# apt-cache search libusb
libusb-0.1-4 - userspace USB programming library
libusb-1.0-0 - userspace USB programming library
libusb-1.0-0-dev - userspace USB programming library development files
libusb-dev - userspace USB programming library development files
用apt-get source libusb下载过来的内如如下所示:
total 1688
-rw-r--r-- 1 500 500 2661 2005-02-14 acinclude.m4
-rw-r--r-- 1 500 500 244051 2006-03-04 aclocal.m4
drwxrwxrwx 2 500 500 4096 2006-03-04 apidocs
-rw-r--r-- 1 500 500 130 2004-04-22 AUTHORS
-rw-r--r-- 1 500 500 16833 2006-03-04 bsd.c
-rw-r--r-- 1 500 500 189 2004-01-28 ChangeLog
-rwxr-xr-x 1 500 500 3642 2004-03-12 compile
-rwxr-xr-x 1 500 500 42037 2004-04-12 config.guess
-rw-r--r-- 1 500 500 2467 2006-03-04 config.h.in
-rwxr-xr-x 1 500 500 30221 2004-04-12 config.sub
-rwxr-xr-x 1 500 500 746195 2006-03-04 configure
-rw-r--r-- 1 500 500 6777 2006-03-04 configure.in
-rw-r--r-- 1 500 500 26428 2004-01-28 COPYING
-rw-r--r-- 1 500 500 35685 2006-03-04 darwin.c
drwxr-xr-x 3 root root 4096 02-19 18:51 debian
-rwxr-xr-x 1 500 500 14841 2004-03-12 depcomp
-rw-r--r-- 1 500 500 15021 2006-03-04 descriptors.c
drwxrwxrwx 2 500 500 4096 02-19 18:49 doc
-rw-r--r-- 1 500 500 44099 2006-03-04 Doxyfile
-rw-r--r-- 1 500 500 44154 2006-02-07 Doxyfile.in
-rw-r--r-- 1 500 500 759 2004-01-28 error.c
-rw-r--r-- 1 500 500 716 2004-01-28 error.h
-rw-r--r-- 1 500 500 2043 2006-03-04 INSTALL.libusb
-rw-r--r-- 1 500 500 2063 2004-01-28 INSTALL.libusb.in
-rwxr-xr-x 1 500 500 9208 2004-03-12 install-sh
-rw-r--r-- 1 500 500 1267 2004-01-28 libusb-config.in
-rw-r--r-- 1 500 500 196 2005-02-15 libusb.pc.in
-rw-r--r-- 1 500 500 1290 2006-03-04 libusb.spec
-rw-r--r-- 1 500 500 1293 2006-03-04 libusb.spec.in
-rw-r--r-- 1 500 500 2248 2004-01-28 LICENSE
-rw-r--r-- 1 500 500 19148 2006-03-04 linux.c
-rw-r--r-- 1 500 500 3146 2005-02-03 linux.h
-rw-r--r-- 1 500 500 183730 2004-04-12 ltmain.sh
-rw-r--r-- 1 500 500 2220 2006-03-04 Makefile.am
-rw-r--r-- 1 500 500 34139 2006-03-04 Makefile.in
-rwxr-xr-x 1 500 500 10678 2004-03-12 missing
-rw-r--r-- 1 500 500 8 2004-01-28 NEWS
-rw-r--r-- 1 500 500 2546 2006-03-04 README
-rw-r--r-- 1 500 500 2556 2006-03-04 README.in
drwxrwxrwx 2 500 500 4096 2006-03-04 tests
-rw-r--r-- 1 500 500 6443 2006-03-04 usb.c
-rw-r--r-- 1 500 500 8367 2006-03-04 usb.h.in
-rw-r--r-- 1 500 500 1771 2006-03-04 usbi.h
-rw-r--r-- 1 500 500 13511 2006-03-04 usbpp.cpp
-rw-r--r-- 1 500 500 24428 2005-02-10 usbpp.h
可以从usb.c, usb.h.in入手来研究。