在kernel中添加自定义驱动、无线网卡/声卡的配置

一、引言

最近做产品是,需要用到USB无线网卡和声卡,所以来整理一下,如果内核中没有我们所使用的型号,则需要我们自己添加相关驱动。

二、内核配置方式

linux-3.10.y

1.USB网卡
make menuconfig
[y] Networking support -->
-y- Wireless -->
[y] Generic IEEE 802.11 Networking Stack (mac80211)

Device Drivers -->
[ y] Network device support -->
[y] Wireless LAN -->
[y] Ralink driver support -->
[y] Ralink rt27xx/rt28xx/rt30xx (USB) support
[y] rt2800usb - Include support for rt33xx devices
[y] rt2800usb - Include support for rt35xx devices
(这里选上使用网卡的型号即可)

2.USB声卡

Device Drivers -->
Sound card support -->
— Sound card support Preclaim OSS device numbers
Advanced Linux Sound Architecture -->
— Advanced Linux Sound Architecture
OSS Mixer API
OSS PCM (digital audio) API
[y] OSS PCM (digital audio) API - Include plugin system
[y ] USB sound devices —>
ALSA for SoC audio support —>

三、内核中增加新驱动

我们以GPIO为例,创建一个新目录

1、drivers目录下面创建GPIO文件夹,,文件夹下面创建三个文件,分别是:gpio.c,Kconfig Makefile,三个文件

gpio.c:驱动程序
Kconfig:配置菜单,也就是它会在make menuconfig之后显示出来的东西。它和makefile文件一样需要和上级目录下的Kconfig文件还有Makefile文件关联。
makefile:编译规则

编写相关文件

Kconfig:
menu "Fxq_GPIO_Driver "
comment “GPIO_Driver”
config fxq_gpio_driver
tristate “GPIO_Driver”
help
this is 4412_gpio_driver
endmenu

Makefile:
obj-$(CONFIG_GPIO) += gpio.o

在上级Kconfig目录下添加
source “drivers/GPIO/Kconfig”###myconfig file

在上级Makefile文件中添加
obj-$(CONFIG_GPIO) += GPIO/

添加之后全部保存,在命令行中linux目录中输入make menuconfig 命令之后,可以见到添加进去的目录

Linux\Android上配置无线网络

iwconfig是Linux Wireless Extensions(LWE)的用户层配置工具之一。LWE是Linux下对无线网络配置的工具,包括内核的支持、用户层配置工具和驱动接口的支持三部分。目前很多无线网卡都支持LWE,而且主流的Linux发布版本,比如Redhat Linux、Ubuntu Linux都已经带了这个配置工具。

1、在USB接口001/003上检测到网卡

查看网卡状态

lsusb

Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 002: ID 093a:2510 Pixart Imaging, Inc. Optical Mouse
Bus 001 Device 003: ID 0cf3:7015 Atheros Communications, Inc. 

检测USB网卡

iwconfig

lo        no wireless extensions.

eth0      no wireless extensions.

wlan0     IEEE 802.11bgn  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=0 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off

可得知:无线网卡为 wlan0

2、激活网卡

ifconfig wlan0 up
busybox ifconfig wlan0 up(在部分Android中需要执行如下命令)

3、扫描网络

iwlist wlan0 scan

wlan0     Scan completed :
          Cell 01 - Address: 54:E6:FC:22:E1:D2
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=53/70  Signal level=-57 dBm  
                    Encryption key:on
                    ESSID:"TP-LINK_22E1D2"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=000000292a41bd80
                    Extra: Last beacon: 960ms ago
                    IE: Unknown: 000E54502D4C494E4B5F323245314432
                    IE: Unknown: 010882848B960C121824
                    IE: Unknown: 030101
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    IE: WPA Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK

发现WPA2-PSK/WPA-PSK 加密网络 TP-LINK_22E1D2,因为加密方式为 WPA-PSK 所以得用 wpa_supplicant 而不能使用 iwconfig wlan0 key xxx 形式,iwconfig key方式适用于WEP。

4、连接网络

# 连接开放的WI-FI网络
iwconfig wlan0 essid "WIFI名称" 

5、配置 wpa_supplicant
新建文件 /etc/my_wpa_supplicant.conf

ctrl_interface=/var/run/wpa_supplicant
network={
	ssid="TP-LINK_22E1D2"
	psk="密码"
}

5、连接wlan0到网络,并以daemon方式运行

wpa_supplicant -B -i wlan0 -c /etc/my_wpa_supplicant.conf

-B Background 在后台以daemon 运行
-i interface
-c 配置文件

6、设置IP地址

ifconfig wlan0 192.168.1.131

ifconfig 查看状态

wlan0     Link encap:Ethernet  HWaddr 54:E6:FC:03:0D:6B  
          inet addr:192.168.1.131  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:95 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:32434 (31.6 Kb)  TX bytes:576 (576.0 b)

7、加入网关到路由
我的网关的是192.168.1.1,添加通过wlan0访问的网关

route add default gw 192.168.1.1 dev wlan0

查看状态

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 wlan0
default         192.168.1.1     0.0.0.0         UG    0      0        0 wlan0

8、设置完毕
以后上线只需运行以下命令即可

wpa_supplicant -B -i wlan0 -c /etc/my_wpa_supplicant.conf
ifconfig wlan0 192.168.1.131
route add default gw 192.168.1.1 dev wlan0
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文艺小少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值