linux 驱动模块,debian安装驱动

各位老铁们好,相信很多人对linux 驱动模块都不是特别的了解,因此呢,今天就来为大家分享下关于linux 驱动模块以及debian安装驱动的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

linux系统下安装usb网卡驱动图文

确认usb-wifi设备型号,根据确认结果选择驱动信息。

安装驱动 mt7601u,如果已经自带跳过此步骤

配置驱动启动后自动加载。

重启验证是否成功的自动加载驱动模块。

确认usb-wifi设备型号,根据确认结果选择驱动信息。

##初始USB接入时加载显示

lsusb| grep Ralink

Bus 001 Device 003: ID 148f:2878 Ralink Technology, Corp.

## lsusb执行后显示信息中有 usb-wifi设备时继续执行如下命令,进行模式切换(对于存在多模式设备可以正确显示出wifi设备的真实型号)

usb_modeswitch-KW-v 148f-p 2878

##模式切换后加载显示

lsusb| grep Ralink

Bus 001 Device 003: ID 148f:7601 Ralink Technology, Corp.

可以看到型号为 7601,据此我们选择 mt7601u驱动进行‘安装。

安装驱动 mt7601u,如果已经自带跳过此步骤

方法1:有可能系统内核已经集成好了你需要的驱动模块,只需要手工加载以下验证是否有效,如果无效再选择方法2.

方法2:github搜索型号的驱动源码,手动编译安装。

安装完毕后,需要配置下 wlan0的配置信息,创建一个ifcfg-wlan0文件(如果使用的是 NetworkManager服务可能不需要配置就可以使用了)

$ cat/etc/sysconfig/network/ifcfg-wlan0

BOOTPROTO='dhcp'

BROADCAST=''

DHCLIENT_SET_DEFAULT_ROUTE='yes'

ETHTOOL_OPTIONS=''

IPADDR=''

MTU=''

NAME=''

NETMASK=''

NETWORK=''

REMOTE_IPADDR=''

STARTMODE='auto'

WIRELESS_AP=''

WIRELESS_AP_SCANMODE='1'

WIRELESS_AUTH_MODE='psk'

WIRELESS_BITRATE='auto'

WIRELESS_CA_CERT=''

WIRELESS_CHANNEL=''

WIRELESS_CLIENT_CERT=''

WIRELESS_CLIENT_KEY=''

WIRELESS_CLIENT_KEY_PASSWORD=''

WIRELESS_DEFAULT_KEY='0'

WIRELESS_EAP_AUTH=''

WIRELESS_EAP_MODE=''

WIRELESS_ESSID='CPE_05010'

WIRELESS_FREQUENCY=''

WIRELESS_KEY=''

WIRELESS_KEY_0=''

WIRELESS_KEY_1=''

WIRELESS_KEY_2=''

WIRELESS_KEY_3=''

WIRELESS_KEY_LENGTH='128'

WIRELESS_MODE='Managed'

WIRELESS_NICK=''

WIRELESS_NWID=''

WIRELESS_PEAP_VERSION=''

WIRELESS_POWER='no'

WIRELESS_WPA_ANONID=''

WIRELESS_WPA_IDENTITY=''

WIRELESS_WPA_PASSWORD=''

WIRELESS_WPA_PSK='Abcd1234'

配置驱动启动后自动加载。

具体配置方法可以参考 man modules-load.d信息可以详细理解。

创建一个 mt7601u.conf文件,内容如下

# echo"mt7601u">/etc/modules-load.d/mt7601u.conf

# cat/etc/modules-load.d/mt7601u.conf

mt7601u

重启验证是否成功的自动加载驱动模块。

linux所有驱动都可以编译成模块吗

linux下编译运行驱动

嵌入式linux下设备驱动的运行和linux x86 pc下运行设备驱动是类似的,由于手头没有嵌入式linux设备,先在vmware上的linux上学习驱动开发。

按照如下方法就可以成功编译出hello world模块驱动。

1、首先确定本机linux版本

怎么查看Linux的内核kernel版本?

'uname'是Linux/unix系统中用来查看系统信息的命令,适用于所有Linux发行版。配合使用'uname'参数可以查看当前服务器内核运行的各个状态。

#uname-a

Linux whh 3.5.0-19-generic#30-Ubuntu SMPTue Nov 13 17:49:53 UTC 2012 i686 i686 i686 GNU/Linux

只打印内核版本,以及主要和次要版本:

#uname-r

3.5.0-19-generic

要打印系统的体系架构类型,即的机器是32位还是64位,使用:

#uname-p

i686

/proc/version文件也包含系统内核信息:

# cat/proc/version

Linux version 3.5.0-19-generic(buildd@aatxe)(gcc version 4.7.2(Ubuntu/Linaro 4.7.2-2ubuntu1))#30-UbuntuSMP Tue Nov 13 17:49:53 UTC 2012

发现自己的机器linux版本是:3.5.0-19-generic

2、下载机器内核对应linux源码

到下面网站可以下载各个版本linux源码

如我的机器3.5.0版本源码下载地址为:

下载完后,找一个路径解压,如我解压到/linux-3.5/

然后很重要的一步是:执行命令uname-r,可以看到Ubuntu的版本信息是3.5.0-19-generic

。进入linux源码目录,编辑Makefile,将EXTRAVERSION=修改为EXTRAVERSION=-19-generic。

这些都是要配置源码的版本号与系统版本号,如果源码版本号和系统版本号不一致,在加载模块的时候会出现如下错误:insmod: error inserting'hello.ko':-1 Invalid module format。

原因很明确:编译时用的hello.ko的kenerl不是我的pc的kenerl版本。

执行命令cp/boot/config-3.5.0-19-generic./config,覆盖原有配置文件。

进入linux源码目录,执行make menuconfig配置内核,执行make编译内核。

3、写一个最简单的linux驱动代码hello.c

/*======================================================================

Asimple kernel module:"hello world"

======================================================================*/

#include<linux/init.h>

#include<linux/module.h>

MODULE_LICENSE("zeroboundaryBSD/GPL");

static int hello_init(void)

{

printk(KERN_INFO"Hello World enter\n");

return0;

}

static void hello_exit(void)

{

printk(KERN_INFO"Hello World exit\n");

}

module_init(hello_init);

module_exit(hello_exit);

MODULE_AUTHOR("zeroboundary");

MODULE_DESCRIPTION("A simple HelloWorld Module");

MODULE_ALIAS("a simplestmodule");

4、写一个Makefile对源码进行编译

KERN_DIR=/linux-3.5

all:

make-C$(KERN_DIR) M=`pwd` modules

clean:

make-C$(KERN_DIR) M=`pwd` clean

obj-m+= hello.o

5、模块加载卸载测试

insmod hello.ko

rmmod hello.ko

然后dmesg|tail就可以看见结果了

最后,再次编译驱动程序hello.c得到hello.ko。执行insmod./hello.ko,即可正确insert模块。

使用insmod hello.ko将该Module加入内核中。在这里需要注意的是要用 su命令切换到root用户,否则会显示如下的错误:insmod: error inserting'hello.ko':-1 Operation not permitted

内核模块版本信息的命令为modinfo hello.ko

通过lsmod命令可以查看驱动是否成功加载到内核中

通过insmod命令加载刚编译成功的time.ko模块后,似乎系统没有反应,也没看到打印信息。而事实上,内核模块的打印信息一般不会打印在终端上。驱动的打印都在内核日志中,我们可以使用dmesg命令查看内核日志信息。dmesg|tail

可能还会遇到这种问题insmod: error inserting'hello.ko':-1 Invalid module format

用dmesg|tail查看内核日志详细错误

disagrees about version of symbolmodule_layout,详细看这里。

在X86上我的办法是:

make-C/usr/src/linux-headers-3.5.0-19-generic SUBDIRS=$PWD modules

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

本文记录我的第一个Linux设备驱动程序的编译过程。遇到问题的解决方法。

环境:2.4.18-14的内核,Linux内核源码:2.4.18。

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/module.h>

MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the module,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");

}

module_init(hello_init);/* load the module*/

module_exit(hello_exit);/* unload the module*/

进入目录:

[root@Alex_linux/]#cd/work/jiakun_test/moduletest

[root@Alex_linux moduletest]# vi hello.c

然后拷入上面书上的源码。

2.编译代码:

1>.首先我在2.4内核的虚拟机上进行编译,编译过程如下:

[root@Alex_linux moduletest]#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 `module_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 `module_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 moduletest]#gcc-D__KERNEL__-I/usr/src/linux-DMODULE-Wall-O2-c-o hello.o hello.c

[root@Alex_linux moduletest]#ls

hello.c hello.o Makefile

[root@Alex_linux moduletest]#

2>.接着我尝试在2.6内核的虚拟机上进行编译

编译过程如下:

[root@JiaKun moduletest]# ls

hello.c makefile

[root@JiaKun moduletest]# vi hello.c

[root@JiaKun moduletest]# make

make-C/mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moduletest modules

make:***/mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.

make:*** [modules] Error 2

[root@JiaKun moduletest]# vi makefile

[root@JiaKun moduletest]# make

make-C/usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moduletest modules

make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'

scripts/Makefile.build:17:/home/alex/test/moduletest/Makefile: No such file or directory

make[2]:*** No rule to make target `/home/alex/test/moduletest/Makefile'. Stop.

make[1]:*** [_module_/home/alex/test/moduletest] Error 2

make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'

make:*** [modules] Error 2

[root@JiaKun moduletest]# mv makefile Makefile

[root@JiaKun moduletest]# make

make-C/usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moduletest modules

make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'

CC [M]/home/alex/test/moduletest/hello.o

Building modules, stage 2.

MODPOST

CC/home/alex/test/moduletest/hello.mod.o

LD [M]/home/alex/test/moduletest/hello.ko

make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'

[root@JiaKun moduletest]# ls

hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Module.symvers

3.执行代码,加载驱动模块:

2.4内核加载模块:

insmod./hello.o

但是此时并没有输出printk打印的信息。但是可以在/var/log/messages中看到打印的信息,这是由于KERN_ALERT优先级不够高。这里

需要修改为:KERN_EMERG。再次编译,加载模块即可以看到结果

2.6内核加载模块:

[root@JiaKun moduletest]# insmod hello.ko

[root@JiaKun moduletest]#

Message from syslogd@ at Sat Jul 26 19:52:44 2008...

JiaKun kernel: Hello, world

有的朋友可能会出现insmod命令找不到的错误,这可能有下面几个原因:

<1>你的系统没有安装module-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 moduletest]# rmmod hello.ko

[root@JiaKun moduletest]#

Message from syslogd@ at Sat Jul 26 19:52:58 2008...

JiaKun kernel: Goodbye, cruel world

另外,如果有多个文件,则按下列方式编写Makefile文件(file1.c、file2.c):

obj-m:= modulename.o

module-objs:= file1.o file2.o

阅读剩余
THE END