导航:首页 > 源码编译 > linux编译驱动

linux编译驱动

发布时间:2022-02-06 09:45:42

linux安装设备驱动需要哪些编译环境

和编译一些开源代码的环境一样,就是make工具和GCC编译器。

⑵ 怎么编译目标机linux设备驱动

在宿主机上安装开发工具和下载linux源码(要求版本号和目标机上的linux内核版本一致)。开发工具主要有gcc、gdb、make等,这些工具在redhat或fc中默认就安装了,在debian或Ubuntu中可以通过下面这个命令安装:
apt-get install build-essential
linux源码可以通过以下几种途径获得:
将源码解压到/usr/src/目录后,进入linux-source-(版本号)目录中执行下面几个命令:
make oldconfig
make prepare
make scripts
直接去www.kernel.org下载
通过包管理工具下载源码,在debian和Ubuntu中可以通过下面这个命令下载,
apt-get install linux-source-(版本号) ,下载后的文件在/usr/src目录中,解压到该目录即可
编写Linux驱动程序,以一个最简单的hello.c为例,hello.c的内容如下:

#include "linux/init.h"
#include "linux/mole.h"

static int hello_init(void)
{
printk(KERN_ALERT "Hello World linux_driver_mole\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbey linux_driver_mole\n");
}

mole_init(hello_init);
mole_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lpj");

写Makefile文件,一个示例如下,里面各项参数根据实际情况更改:

#sample driver mole
obj-m := hello.o
KDIR = /usr/src/linux-source-2.6.24/

all:
$(MAKE) -C $(KDIR) M=$(PWD)

.PHONY:clean
clean:
rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions

编译,在hello.c和Makefile所在目录下执行 make 即可,编译后在当前目录生成hello.ko文件
加载并测试:加载使用insmod或modprobe命令来实现,如在当前路径执行如下代码:
insmod hello.ko 或 modprobe hello
注意,如果在虚拟终端加载内核的话,将看不到内核打印信息,因为内核打印信息不会输出到虚拟终端,而是输出到/proc/kmsg文件中,所以可以通过以下方式查看内核信息:
cat /proc/kmsg 会一直打印,需要Ctrl-C手动终止
dmesg 或 dmesg | tail -N ,N为一数字,表示显示最后N行
卸载:使用rmmod命令卸载驱动模块,如 rmmod hello

⑶ linux下怎么编译安装驱动

linux 编译安装驱动有两种,动态加载与静态加载
动态加载
一,编译,在指点内核树下编译,生成.o文件或.ko文件
二,将生成的.o或.ko文件拷到相应目录,一般是/lib/mole/kernel下面
三,用insmod命令加载,用rmmod命令卸载
静态加载
静态加载主要就是编译内核。就是将编写好的驱动放进内核相应的目录下面。然后编译内核。然后运行编译好的内核。

⑷ 如何编写Linux的驱动程序

};  //IO功能选项,硬件上拉输出  static unsigned int gpio_cfg_table[] = {      S3C2410_GPB5_OUTP,    S3C2410_GPB6_OUTP,    S3C2410_GPB7_OUTP,    S3C2410_GPB8_OUTP, };  //编写一个ioctl函数,这个函数提供给用户端使用(也就是用户态使用)  static int my_ioctl(struct inode *inode,struct file* file,unsigned int cmd,           unsigned long arg) {                 if (arg > 4)        {            return -EINVAL;        }         if (cmd == 1) //led ON        {             s3c2410_gpio_setpin(gpio_table[arg],0);            return 0;        }         if (cmd == 0) //led OFF        {            s3c2410_gpio_setpin(gpio_table[arg],1);           return 0;        }        else        {             return -EINVAL;        }  }  //一个和文件设备相关的结构体。  static struct file_operations dev_fops =  {         .owner = THIS_MODULE,        .ioctl = my_ioctl,         //.read  = my_read,   //这个暂时屏蔽,一会我们再加入一个读操作的函数 };  //linux中设备的注册结构体 static struct miscdevice misc = 
{         .minor = MISC_DYNAMIC_MINOR,        .name  = DEVICE_NAME,        .fops  = &dev_fops, };  //设备初始化(包括注册)函数 static int __init dev_init(void) {         int ret;        int i;         for (i=0;i<4;i++)        {             s3c2410_gpio_cfgpin(gpio_table[i],gpio_cfg_table[i]);            s3c2410_gpio_setpin(gpio_table[i],0);            mdelay(500);             s3c2410_gpio_setpin(gpio_table[i],1);        }         ret = misc_register(&misc);         printk(DEVICE_NAME"MY_LED_DRIVER init ok\n");        return ret; }  //设备注销函数   static void __exit dev_exit(void) {         misc_deregister(&misc); }  //与模块相关的函数 mole_init(dev_init); mole_exit(dev_exit); MODULE_LICENSE("GPL");  MODULE_AUTHOR("blog.ednchina.com/itspy"); 
MODULE_DESCRIPTION("MY LED DRIVER");  到此,上面就完成了一个简单的驱动(别急,下面我们再会稍微增加点复杂的东西),以上代码的可以简单概括为:像自己写51单片机或者ARM的裸奔程序一样操作IO函数,然后再linux系统中进行相关必须的函数关联和注册。 为什么要关联呢,为什么注册呢? 因为这是必须的,从以下这些结构体就知道了。 stuct file_operations{  struct mole *owner;  loff_t (*llseek) (struct file *, loff_t, int);  ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);  ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);  ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);  ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);  int (*readdir) (struct file *, void *, filldir_t); 
 unsigned int (*poll) (struct file *, struct poll_table_struct *);  int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);  long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); … }  file_operations 结构体中包括了很多与设备相关的函数指针,指向了驱动所提供的函数。 struct inode{  struct hlist_node i_hash;  struct list_head i_list;  struct list_head i_sb_list;  struct list_head i_dentry;  unsigned long  i_ino;  atomic_t  i_count;  unsigned int  i_nlink;  uid_t   i_uid;  gid_t   i_gid;  dev_t   i_rdev;  u64   i_version;  loff_t   i_size; … }     inode 是 UNIX 操作系统中的一种数据结构,它包含了与文件系统中各个文件相关的一些重要信息。在 UNIX 中创建文件系统时,同时将会创建大量的 inode 。通常,文件系统磁盘空间中大约百分之一空间分配给了 inode 表。  大略了解以上信息之后,我们只需把我们所要实现的功能和结构体关联起来。上例中已经完成IO写操作的函数,现在我们再添加一个读的函数。基于这种原理,我们想实现各种功能的驱动也就很简单了。  //添加读函数示意, 用户层可以通过 read函数来操作。  static int my_read(struct file* fp, char __user *dat,size_t cnt) {        size_t i;         printk("now read the hardware...\n");       for(i=0;i<cnt;i++)           dat[i] = 'A';       dat[i] = '\0';       return cnt;  }  这样,完成驱动编写。编译之后,本驱动可以通过直接嵌入内核中,也可以以模块的嵌入的形式加载到linux内核中去。  完成了驱动,写个应用程序了验证一下吧:  int main(int argc,char ** argv) {  
    int on;     int led_no;     int fd;     char str[10];     int cnt =0;      fd = open("/dev/MY_LED_DRIVER",0);     if (fd < 0)     {         printf("can't open dev\n");        exit(1);         }      printf("read process\n");     cnt = read(fd,str,10);      printf("get data from driver:\n%s\ncount = %d\n",str,cnt);     printf("read process end \n");     cnt = 0;      printf("running...\n");     while(cnt++<1000)     {        ioctl(fd,0,0);  //led off        ioctl(fd,0,1);       ioctl(fd,0,2);       ioctl(fd,0,3);       sleep(1);   //printf("sdfdsfdsfdsfds...\n");       ioctl(fd,1,0);  //led on       ioctl(fd,1,1);       ioctl(fd,1,2);       ioctl(fd,1,3);       sleep(1);        printf("%d\b",cnt);     }      close(fd);     return 0; }

⑸ linux 查看有哪些驱动编译进了内核

一、 驱动程序编译进内核的步骤
在 linux 内核中增加程序需要完成以下三项工作:
1. 将编写的源代码复制到 Linux 内核源代码的相应目录;
2. 在目录的 Kconfig 文件中增加新源代码对应项目的编译配置选项;
3. 在目录的 Makefile 文件中增加对新源代码的编译条目。

bq27501驱动编译到内核中具体步骤如下:
1. 先将驱动代码bq27501文件夹复制到 ti-davinci/drivers/ 目录下。
确定bq27501驱动模块应在内核源代码树中处于何处。
设备驱动程序存放在内核源码树根目录 drivers/ 的子目录下,在其内部,设备驱动文件进一步按照类别,类型等有序地组织起来。
a. 字符设备存在于 drivers/char/ 目录下
b. 块设备存放在 drivers/block/ 目录下
c. USB 设备则存放在 drivers/usb/ 目录下。
注意:
(1) 此处的文件组织规则并非绝对不变,例如: USB 设备也属于字符设备,也可以存放在 drivers/usb/ 目录下。
(2) 在 drivers/char/ 目录下,在该目录下同时存在大量的 C 源代码文件和许多其他目录。所有对于仅仅只有一两个源文件的设备驱动程序,可以直接存放在该目录下,但如果驱动程序包含许多源文件和其他辅助文件,那么可以创建一个新子目录。
(3) bq27501的驱动是属于字符设备驱动类别,虽然驱动相关的文件只有两个,但是为了方面查看,将相关文件放在了bq27501的文件夹中。在drivers/char/目录下增加新的设备过程比较简单,但是在drivers/下直接添加新的设备稍微复杂点。所以下面首先给出在drivers/下添加bq27501驱动的过程,然后再简单说明在drivers/char/目录下添加的过程。

2. 在/bq27501下面新建一个Makefile文件。向里面添加代码:
obj-$(CONFIG_BQ27501)+=bq27501.o
此时,构建系统运行就将会进入 bq27501/ 目录下,并且将bq27501.c 编译为 bq27501.o
3. 在/bq27501下面新建Kconfig文件。添加代码:
menu "bq27501 driver"

config BQ27501
tristate"BQ27501"
default y
---help---
Say 'Y' here, it will be compiled into thekernel; If you choose 'M', it will be compiled into a mole named asbq27501.ko.
endmenu
注意:help中的文字不能加回车符,否则make menuconfig编译的时候会报错。
4. 修改/drivers目录下的Kconfig文件,在endmenu之前添加一条语句‘source drivers/bq27501/Kconfig’ 对于驱动程序,Kconfig 通常和源代码处于同一目录。 若建立了一个新的目录,而且也希望 Kconfig 文件存在于该目录中的话,那么就必须在一个已存在的 Kconfig 文件中将它引入,需要用上面的语句将其挂接在 drivers 目录中的Kconfig 中。

5. 修改/drivers目下Makefile文件,添加‘obj-$(CONFIG_BQ27501) +=bq27501/’。这行编译指令告诉模块构建系统在编译模块时需要进入 bq27501/ 子目录中。此时的驱动程序的编译取决于一个特殊配置 CONFIG_BQ27501 配置选项。

6. 修改arch/arm目录下的Kconfig文件,在menu "Device Drivers……endmenu"直接添加语句
source "drivers/bq27501/Kconfig" 楼主 学习linux可以看看http://www.linuxprobe.com/chapter-00.html。

⑹ 如何编译一个linux下的驱动模块

按照《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

⑺ linux 驱动程序开发,为什么要编译内核源码树

您好,这样的情况建议您下载最新版本的驱动精灵,或是直接在线升级一下驱动精灵。希望可以帮到您。

⑻ Linux驱动编译

这文件目录下面根本就没有需要编译的文件,所以make的时候提示,命令没有找到(没有那个文件)
“README_STA_usb”,你看看这个文档,是安装说明,里面应该有讲解。

⑼ linux怎么编译进驱动进内核

一、 驱动程序编译进内核的步骤
在 linux 内核中增加程序需要完成以下三项工作:
1. 将编写的源代码复制到 Linux 内核源代码的相应目录;
2. 在目录的 Kconfig 文件中增加新源代码对应项目的编译配置选项;
3. 在目录的 Makefile 文件中增加对新源代码的编译条目。

bq27501驱动编译到内核中具体步骤如下:
1. 先将驱动代码bq27501文件夹复制到 ti-davinci/drivers/ 目录下。
确定bq27501驱动模块应在内核源代码树中处于何处。
设备驱动程序存放在内核源码树根目录 drivers/ 的子目录下,在其内部,设备驱动文件进一步按照类别,类型等有序地组织起来。
a. 字符设备存在于 drivers/char/ 目录下
b. 块设备存放在 drivers/block/ 目录下
c. USB 设备则存放在 drivers/usb/ 目录下。
注意:
(1) 此处的文件组织规则并非绝对不变,例如: USB 设备也属于字符设备,也可以存放在 drivers/usb/ 目录下。
(2) 在 drivers/char/ 目录下,在该目录下同时存在大量的 C 源代码文件和许多其他目录。所有对于仅仅只有一两个源文件的设备驱动程序,可以直接存放在该目录下,但如果驱动程序包含许多源文件和其他辅助文件,那么可以创建一个新子目录。
(3) bq27501的驱动是属于字符设备驱动类别,虽然驱动相关的文件只有两个,但是为了方面查看,将相关文件放在了bq27501的文件夹中。在drivers/char/目录下增加新的设备过程比较简单,但是在drivers/下直接添加新的设备稍微复杂点。所以下面首先给出在drivers/下添加bq27501驱动的过程,然后再简单说明在drivers/char/目录下添加的过程。

2. 在/bq27501下面新建一个Makefile文件。向里面添加代码:
obj-$(CONFIG_BQ27501)+=bq27501.o
此时,构建系统运行就将会进入 bq27501/ 目录下,并且将bq27501.c 编译为 bq27501.o
3. 在/bq27501下面新建Kconfig文件。添加代码:
menu "bq27501 driver"

config BQ27501
tristate"BQ27501"
default y
---help---
Say 'Y' here, it will be compiled into thekernel; If you choose 'M', it will be compiled into a mole named asbq27501.ko.
endmenu
注意:help中的文字不能加回车符,否则make menuconfig编译的时候会报错。
4. 修改/drivers目录下的Kconfig文件,在endmenu之前添加一条语句‘source drivers/bq27501/Kconfig’ 对于驱动程序,Kconfig 通常和源代码处于同一目录。 若建立了一个新的目录,而且也希望 Kconfig 文件存在于该目录中的话,那么就必须在一个已存在的 Kconfig 文件中将它引入,需要用上面的语句将其挂接在 drivers 目录中的Kconfig 中。

5. 修改/drivers目下Makefile文件,添加‘obj-$(CONFIG_BQ27501) +=bq27501/’。这行编译指令告诉模块构建系统在编译模块时需要进入 bq27501/ 子目录中。此时的驱动程序的编译取决于一个特殊配置 CONFIG_BQ27501 配置选项。

6. 修改arch/arm目录下的Kconfig文件,在menu "Device Drivers……endmenu"直接添加语句
source "drivers/bq27501/Kconfig"

⑽ 如何编译linux usb驱动

得自己写makefile文件

ifneq($(KERNELRELEASE),)
obj-m:=xxx.o
else
KERNELDIR?=/lib/moles/$(shelluname-r)/build
PWD:=$(shellpwd)
default:
$(MAKE)-C$(KERNELDIR)M=$(PWD)moles
endif
clean:
rm-rf*.mod.**.o*.ko.*.ko.*.tmp*.*.mod.o.*.*.o.*


其 中xxx是源文件的文件名,在linux下直接执行make就可以生成驱动模块(xxx.ko)了。生成驱动模块后使用insmod xxx.ko就可以插入到内核中运行了,用lsmod可以看到你插入到内核中的模块,也可以从系统中用命令rmmod xxx把模块卸载掉;如果把编译出来的驱动模块拷贝到/lib/moles/~/kernel/drivers/usb/下,然后depmod一下, 那么你在插入USB设备的时候,系统就会自动为你加载驱动模块的;当然这个得有hotplug的支持;加载驱动模块成功后就会在/dev/下生成设备文件 了,如果用命令cat /proc/bus/usb/devices,我们可以看到驱动程序已经绑定到接口上了:
T: Bus=03 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#= 2 Spd=12 MxCh= 0
D: Ver= 1.10 Cls=02(comm.) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=1234 ProdID=2345 Rev= 1.10
C:* #Ifs= 1 Cfg#= 1 Atr=c0 MxPwr= 0mA
I: If#= 1 Alt= 0 #EPs= 2 Cls=0a(data ) Sub=00 Prot=00 Driver=test_usb_driver /*我们的驱动*/
E: Ad=01(O) Atr=02(Bulk) MxPS= 64 Ivl=0ms
E: Ad=82(I) Atr=02(Bulk) MxPS= 64 Ivl=0ms
此框架程序生成的是skel0(可以自由修改)的设备文件,现在就可以对这个设备文件进行打开、读写、关闭等的操作了。

阅读全文

与linux编译驱动相关的资料

热点内容
android如何新建activity 浏览:737
ntp支持的认证算法 浏览:710
想做快手主播需要什么app 浏览:921
阿里云服务器如何转账户 浏览:901
编译器和解释器实现技术完全不同 浏览:429
虐杀原形汉化补丁怎么解压 浏览:643
文件夹验证失败 浏览:635
python是用什么软件编程 浏览:247
java并发编程教程 浏览:319
江铃宝典空调压缩机工作时间过短 浏览:634
自制单片机玩具车 浏览:901
stm32单片机模块电源电压 浏览:187
pdf层次 浏览:735
电脑里找不到编译器 浏览:843
明茨伯格pdf 浏览:442
把网页存成pdf 浏览:268
如何对电脑的d盘加密 浏览:101
刀片式服务器怎么连接电脑 浏览:82
矩阵计算java 浏览:235
如何把各银行app整合 浏览:881