目录
- 前言
- USB无线网卡选择
- MT7601驱动移植
- 1. 资源下载
- 2. 驱动源码修改
- 3. 编译及加载
- 测试
- 1. 源码下载
- 2. 编译
- 3. 测试
- 1. STA模式测试:
- 2. AP模式测试:
前言
因最近本人最近手里两款设备需要上网,一台是Android4.4, 一台是Linux,但是两台设备都没有WiFi模块,好在有USB口,所以本人想到是否可尝试移植一个USB无线网卡设备,结果成功移植,在此记录一下,供有需要的朋友参考。
本文主要你介绍两个模块:
- MT7601无线网卡的驱动移植。
- wireless tool工具的移植。
本人在Android/Linux平台下都成功进行了移植,有一定小差异,但不是特别大,根据情况进行修改即可。
USB无线网卡选择
其实没多选择,手边刚好有一款必联的USB无线网卡,如下图所示:
然后查询下这款网卡的芯片:
在网上查了下,貌似当前市面上经济实惠的小米随身Wifi、腾讯全民Wifi、360Wifi、百度小度Wifi等都是基于MT7601U无线网卡制作的,这个有待考证。
关于MT7601U IC的驱动移植,网上参考资料很多,但感觉深度不够或者写的比较笼统,可能因人而异吧。另外网上的驱动源码资源也参差不齐。所以想自己整理总结下。
MT7601驱动移植
1. 资源下载
网上的资源还是比较多的,为了方便,我这里把我修改成功的资源上传,有需要的朋友根据自己的情况下载。
MTU7601U STA Linux3.x.x版本:
MTU7601U AP Linux3.x.x版本:
2. 驱动源码修改
STA和AP驱动源码的修改基本是一致的,所以不做区分。
- 修改顶层Makefile
修改 Makefile,设置平台:PLATFORM = IMX6DQ
PLATFORM = IMX6DQ
......
ifeq ($(PLATFORM),IMX6DQ)
export ARCH=arm
CROSS_COMPILE =/home/danny/android4.4.3/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/arm-linux-androideabi-
LINUX_SRC = $(ANDROID_BUILD_TOP)/kernel_imx
endif
- 修改config.mk
修改os/linux/config.mk
#Android平台需要打开以下几个选项
HAS_WPA_SUPPLICANT=y
HAS_NATIVE_WPA_SUPPLICANT_SUPPORT=y
HAS_ANDROID_SUPPORT=y
ifeq ($(PLATFORM),IMX6DQ)
EXTRA_CFLAGS := -D__KERNEL__ -DMODULE $(WFLAGS) -fno-pic #-Wno-error=date-time
export EXTRA_CFLAGS
endif
注意:
Android平台编译时会报错cc1: error: -Werror=date-time: no option -Wdate-time,需要注释掉-Wno-error=date-time
Linux平台编译时需要加上-Wno-error=date-time,否则会有__DATE__,__TIME__此类异常。
- 修改配置文件路径
AP模式,修改include/os/rt_linux.h
#define AP_PROFILE_PATH “/etc/wifi/RT2870AP.dat”
STA模式,修改include/os/rt_linux.h
#define STA_PROFILE_PATH “/etc/wifi/RT2870STA.dat” - 修改DEV ID
这个主要是针对基于MT7601U开发的不同的无线网卡,如果没有定义VID ,PID的话,可通过common\rtusb_dev_id.c增加:
/* module table */
USB_DEVICE_ID rtusb_dev_id[] = {
#ifdef RT6570
{USB_DEVICE(0x148f,0x6570)}, /* Ralink 6570 */
#endif /* RT6570 */
{USB_DEVICE(0x148f, 0x7650)}, /* MT7650 */
#ifdef MT7601U
{USB_DEVICE(0x148f,0x6370)}, /* Ralink 6370 */
{USB_DEVICE(0x148f,0x7601)}, /* MT 6370 */
#endif /* MT7601U */
{USB_DEVICE(0x0e8d,0x760c)},/* 360 Wifi 2 Gen */ { }/* Terminating entry */
};
- 修改WLAN名称
这个是修改驱动成功加载后ifconfig看到的设备名称,可不进行修改,有需要的可以视情况修改:
#define INF_MAIN_DEV_NAME "wlan"
#define INF_MBSSID_DEV_NAME "wlan"
3. 编译及加载
$ make
STA编译成功生成会在os/linux/目录下生成mt7601Usta.ko
AP编译成功生成会在os/linux/目录下生成mt7601Uap.ko
将KO文件及RT2870STA.dat, RT2870AP.dat根据AP_PROFILE_PATH和_PROFILE_PATH设置的路径拷贝到对应的目录下,然后执行insmod进行加载。
网卡设备USB驱动加载成功日志如下:
root@sabresd_6dl_wh_psa:/ # dmesg
<6>[ 37.858583] usb 1-1: new high-speed USB device number 3 using ci_hdrc
<6>[ 38.148734] usb 1-1: New USB device found, idVendor=148f, idProduct=7601
<6>[ 38.148753] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
<6>[ 38.148765] usb 1-1: Product: 802.11 n WLAN
<6>[ 38.148774] usb 1-1: SerialNumber: 1.0
STA驱动加载成功日志如下:
root@sabresd_6dl_wh_psa:/system/lib/modules # dmesg
<4>[11217.280349] rtusb init rt2870 --->
<4>[11217.280455] ===>rt2870_probe()!
<4>[11217.280467] --> RTMPAllocAdapterBlock
<4>[11217.282020]
<4>[11217.282020] === pAd = c4017000, size = 850408 ===
<4>[11217.282020]
<4>[11217.282049] --> RTMPAllocTxRxRingMemory
<4>[11217.282116] <-- RTMPAllocTxRxRingMemory, Status=0
<4>[11217.282537] <-- RTMPAllocAdapterBlock, Status=0
<4>[11217.282546] NumEndpoints=8
<4>[11217.282554] BULK IN MaxPacketSize = 512
<4>[11217.282561] EP address = 0x84
<4>[11217.282567] BULK IN MaxPacketSize = 512
<4>[11217.282572] EP address = 0x85
<4>[11217.282579] BULK OUT MaxPacketSize = 512
<4>[11217.282585] EP address = 0x 8
<4>[11217.282590] BULK OUT MaxPacketSize = 512
<4>[11217.282596] EP address = 0x 4
<4>[11217.282602] BULK OUT MaxPacketSize = 512
<4>[11217.282607] EP address = 0x 5
<4>[11217.282614] BULK OUT MaxPacketSize = 512
<4>[11217.282619] EP address = 0x 6
<4>[11217.282625] BULK OUT MaxPacketSize = 512
<4>[11217.282631] EP address = 0x 7
<4>[11217.282636] BULK OUT MaxPacketSize = 512
<4>[11217.282642] EP address = 0x 9
<4>[11217.282652] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x8
<4>[11217.282659] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x4
<4>[11217.282665] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x5
<4>[11217.282672] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x6
<4>[11217.282678] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x7
<4>[11217.282684] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x9
<4>[11217.282693] STA Driver version-3.0.0.3
<4>[11217.283251] -->MT7601_Init():
<4>[11217.283373] Chip specific bbpRegTbSize=0!
<4>[11217.283382] Chip VCO calibration mode = 0!
<4>[11217.283624] NVM is EFUSE
<4>[11217.283636] Efuse Size=0x1d [Range:1e0-1fc]
<4>[11217.283643] Endpoint(8) is for In-band Command
<4>[11217.283650] Endpoint(4) is for WMM0 AC0
<4>[11217.283657] Endpoint(5) is for WMM0 AC1
<4>[11217.283663] Endpoint(6) is for WMM0 AC2
<4>[11217.283669] Endpoint(7) is for WMM0 AC3
<4>[11217.283676] Endpoint(9) is for WMM1 AC0
<4>[11217.283682] Endpoint(84) is for Data-In
<4>[11217.283688] Endpoint(85) is for Command Rsp
<4>[11217.283698] Allocate a net device with private data size=0!
<4>[11217.283820] Allocate net device ops success!
<4>[11217.283833] The name of the new wlan interface is wlan0...
<4>[11217.283844] RtmpOSNetDevAttach()--->
<4>[11217.286230] <---RtmpOSNetDevAttach(), ret=0
<4>[11217.286244] <===rt2870_probe()!
<6>[11217.286390] usbcore: registered new interface driver rt2870
<7>[11217.286567] initcall rtusb_init+0x0/0x38 [mt7601Usta] returned 0 after 5915 usecs @ 1348
AP模式加载成功日志如下:
<4>[ 137.422112] rtusb init rt2870 --->
<4>[ 137.424243]
<4>[ 137.424243]
<4>[ 137.424243] === pAd = c4139000, size = 1233280 ===
<4>[ 137.424243]
<4>[ 137.424329] <-- RTMPAllocTxRxRingMemory, Status=0
<4>[ 137.424796] <-- RTMPAllocAdapterBlock, Status=0
<4>[ 137.424818] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x8
<4>[ 137.424826] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x4
<4>[ 137.424832] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x5
<4>[ 137.424838] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x6
<4>[ 137.424845] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x7
<4>[ 137.424851] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x9
<4>[ 137.425706] NVM is EFUSE
<4>[ 137.425722] Endpoint(8) is for In-band Command
<4>[ 137.425730] Endpoint(4) is for WMM0 AC0
<4>[ 137.425737] Endpoint(5) is for WMM0 AC1
<4>[ 137.425743] Endpoint(6) is for WMM0 AC2
<4>[ 137.425749] Endpoint(7) is for WMM0 AC3
<4>[ 137.425756] Endpoint(9) is for WMM1 AC0
<4>[ 137.425762] Endpoint(84) is for Data-In
<4>[ 137.425768] Endpoint(85) is for Command Rsp
<6>[ 137.432567] usbcore: registered new interface driver rt2870
<7>[ 137.432801] initcall rtusb_init+0x0/0x38 [mt7601Uap] returned 0 after 10231 usecs @
<4>[ 137.422112] rtusb init rt2870 --->
<4>[ 137.424243]
<4>[ 137.424243]
<4>[ 137.424243] === pAd = c4139000, size = 1233280 ===
<4>[ 137.424243]
<4>[ 137.424329] <-- RTMPAllocTxRxRingMemory, Status=0
<4>[ 137.424796] <-- RTMPAllocAdapterBlock, Status=0
<4>[ 137.424818] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x8
<4>[ 137.424826] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x4
<4>[ 137.424832] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x5
<4>[ 137.424838] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x6
<4>[ 137.424845] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x7
<4>[ 137.424851] RTMP_COM_IoctlHandle():pAd->BulkOutEpAddr=0x9
<4>[ 137.425706] NVM is EFUSE
<4>[ 137.425722] Endpoint(8) is for In-band Command
<4>[ 137.425730] Endpoint(4) is for WMM0 AC0
<4>[ 137.425737] Endpoint(5) is for WMM0 AC1
<4>[ 137.425743] Endpoint(6) is for WMM0 AC2
<4>[ 137.425749] Endpoint(7) is for WMM0 AC3
<4>[ 137.425756] Endpoint(9) is for WMM1 AC0
<4>[ 137.425762] Endpoint(84) is for Data-In
<4>[ 137.425768] Endpoint(85) is for Command Rsp
<6>[ 137.432567] usbcore: registered new interface driver rt2870
<7>[ 137.432801] initcall rtusb_init+0x0/0x38 [mt7601Uap] returned 0 after 10231 usecs @ 1106
注意:
insmod ko文件时,可能出现内存不足,分配内存空间失败问题,如下所示:
[ 92.104651] --> RTMPAllocTxRxRingMemory
[ 92.105325] <-- ERROR in Alloc Bulk buffer for HTTxContext!
[ 92.105347] ---> RTMPFreeTxRxRingMemory
[ 92.105372] <--- RTMPFreeTxRxRingMemory
[ 92.105383] ERROR!!! Failed to allocate memory - TxRxRing
[ 92.105984] <-- RTMPAllocAdapterBlock, Status=3
[ 92.106049] rt2870: probe of 1-1:1.0 failed with error -1
[ 92.108850] usbcore: registered new interface driver rt2870
需要在uboot CMDLINE中增加coherent_pool=2M,一般在.h文件中,如下所示:
setenv bootargs console==ttymxc3,115200 coherent_pool=2M init=/init video=mxcfb0:dev=lcd,TIANMA-FVGA,if=RGB24,bpp=32 video=mxcfb1:off video=mxcfb2:off video=mxcfb3:off fbmem=32M vmalloc=400M
测试
wifi网络的测试使用了wireless-tools.
1. 源码下载
下载地址:http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/wireless_tools.29.tar.gz
2. 编译
Linux环境下可根据平台修改Makefile 后make 即可.
## Compiler to use (modify this for cross compile).
CC = arm-none-eabi-gcc
## Other tools you need to modify for cross compile (static lib only).
AR = arm-none-eabi-ar
RANLIB = arm-none-eabi-ranlib
Android环境下需要增加Android.mk,内容如下:
LOCAL_PATH:=$(call my-dir)
############# iwlib ############
include $(CLEAR_VARS)
LOCAL_SRC_FILES := iwlib.c
LOCAL_CFLAGS += -Wno-parentheses-equality -Wno-tautological-compare -Wno-self-assign \
-Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
-Winline -MMD -fPIC
LOCAL_STATIC_LIBRARIES := libcutils libc libm
LOCAL_MODULE := libiw
LOCAL_MODULE_TAGS :=optional
include $(BUILD_STATIC_LIBRARY)
############# iwconfig ############
include $(CLEAR_VARS)
LOCAL_SRC_FILES := iwconfig.c
LOCAL_CFLAGS += -Wno-parentheses-equality -Wno-tautological-compare -Wno-self-assign \
-Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
-Winline -MMD -fPIC
LOCAL_STATIC_LIBRARIES := libcutils libc libm libiw
#LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) # install to system/xbin
LOCAL_MODULE:= iwconfig
LOCAL_MODULE_TAGS :=optional
include $(BUILD_EXECUTABLE)
# iwevent
include $(CLEAR_VARS)
LOCAL_SRC_FILES := iwevent.c
LOCAL_CFLAGS += -Wno-parentheses-equality -Wno-tautological-compare -Wno-self-assign \
-Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
-Winline -MMD -fPIC
LOCAL_STATIC_LIBRARIES := libcutils libc libm libiw
#LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) # install to system/xbin
LOCAL_MODULE:= iwevent
LOCAL_MODULE_TAGS :=optional
include $(BUILD_EXECUTABLE)
############# iwgetid ############
include $(CLEAR_VARS)
LOCAL_SRC_FILES := iwgetid.c
LOCAL_CFLAGS += -Wno-parentheses-equality -Wno-tautological-compare -Wno-self-assign \
-Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
-Winline -MMD -fPIC
LOCAL_STATIC_LIBRARIES := libcutils libc libm libiw
#LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) # install to system/xbin
LOCAL_MODULE:= iwgetid
LOCAL_MODULE_TAGS :=optional
include $(BUILD_EXECUTABLE)
############# iwlist ############
include $(CLEAR_VARS)
LOCAL_SRC_FILES := iwlist.c
LOCAL_CFLAGS += -Wno-parentheses-equality -Wno-tautological-compare -Wno-self-assign \
-Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
-Winline -MMD -fPIC
LOCAL_STATIC_LIBRARIES := libcutils libc libm libiw
#LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) # install to system/xbin
LOCAL_MODULE:= iwlist
LOCAL_MODULE_TAGS :=optional
include $(BUILD_EXECUTABLE)
############# iwpriv ############
include $(CLEAR_VARS)
LOCAL_SRC_FILES := iwpriv.c
LOCAL_CFLAGS += -Wno-parentheses-equality -Wno-tautological-compare -Wno-self-assign \
-Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
-Winline -MMD -fPIC
LOCAL_STATIC_LIBRARIES := libcutils libc libm libiw
#LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) # install to system/xbin
LOCAL_MODULE:= iwpriv
LOCAL_MODULE_TAGS :=optional
include $(BUILD_EXECUTABLE)
############# iwspy ############
include $(CLEAR_VARS)
LOCAL_SRC_FILES := iwspy.c
LOCAL_CFLAGS += -Wno-parentheses-equality -Wno-tautological-compare -Wno-self-assign \
-Wstrict-prototypes -Wmissing-prototypes -Wshadow -Wpointer-arith -Wcast-qual \
-Winline -MMD -fPIC
LOCAL_STATIC_LIBRARIES := libcutils libc libm libiw
#LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) # install to system/xbin
LOCAL_MODULE:= iwspy
LOCAL_MODULE_TAGS :=optional
include $(BUILD_EXECUTABLE)
然后运行mm -B 编译即可生成wconfig,iwevent,iwgetid,iwlist,iwpriv,iwspy
3. 测试
1. STA模式测试:
- 启动无线网卡:
$ ifconfig wlan0 up
- 测试
可通过iwlist查看channel, wifi节点,速率等信息.
root@sabresd_6dl_wh_psa:/system/lib/modules # iwlist wlan0 channel
wlan0 14 channels in total; available frequencies :
Channel 01 : 2.412 GHz
Channel 02 : 2.417 GHz
Channel 03 : 2.422 GHz
Channel 04 : 2.427 GHz
Channel 05 : 2.432 GHz
Channel 06 : 2.437 GHz
Channel 07 : 2.442 GHz
Channel 08 : 2.447 GHz
Channel 09 : 2.452 GHz
Channel 10 : 2.457 GHz
Channel 11 : 2.462 GHz
Channel 12 : 2.467 GHz
Channel 13 : 2.472 GHz
Channel 14 : 2.484 GHz
Current Frequency:2.412 GHz (Channel 1)
root@sabresd_6dl_wh_psa:/system/lib/modules # iwlist wlan0 bitrate
wlan0 unknown bit-rate information.
Current Bit Rate:1 Mb/s
root@sabresd_6dl_wh_psa:/system/lib/modules # iwlist wlan0 scan
wlan0 Scan completed :
Cell 01 - Address: 68:93:20:EE:98:B3
Protocol:11b/g/n BW20
ESSID:"WHLMGH"
Mode:Managed
Frequency:2.412 GHz (Channel 1)
Quality=78/100 Signal level=-59 dBm Noise level=-92 dBm
Encryption key:on
Bit Rates:144 Mb/s
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
Cell 02 - Address: 68:93:20:EE:98:B4
Protocol:11b/g/n BW20
ESSID:"KETT231"
Mode:Managed
Frequency:2.412 GHz (Channel 1)
Quality=99/100 Signal level=-51 dBm Noise level=-92 dBm
Encryption key:on
Bit Rates:144 Mb/s
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : TKIP
Pairwise Ciphers (2) : TKIP CCMP
Authentication Suites (1) : PSK
........
- WiFi连接
$ iwpriv wlan0 set NetworkType=Infra
$ iwpriv wlan0 set AuthMode=WPA2PSK
$ iwpriv wlan0 set EncrypType=AES
$ iwpriv wlan0 set SSID="WHLMGH"
$ iwpriv wlan0 set WPAPSK="12345678"
- 启动udhcpc配置wlan0网卡
$ udhcpc -i wlan0 &
此时设备就可以通过WiFi模块连接WiFi上网了。
2. AP模式测试:
- 启动无线网卡:
$ ifconfig wlan0 up
- 配置WiFi热点信息.
$ iwpriv wlan0 set NetworkType=Infra
$ iwpriv wlan0 set AuthMode=WPA2PSK
$ iwpriv wlan0 set EncrypType=AES
$ iwpriv wlan0 set SSID=mt7601ap_test
$ iwpriv wlan0 set WPAPSK="12345678"
SSID:WiFi热点名称,WPAPSK:WiFi密码
- 开始DHCP服务
$ ifconfig wlan0 192.168.100.1 netmask 255.255.255.0 up
增加dhcp服务config文件udhcpd_wifi.conf,内容如下:
start 192.168.100.2
end 192.168.100.254
interface wlan0
opt router 192.168.100.1
opt subnet 255.255.255.0
opt dns 202.96.134.133
- 开启DHCPD服务:
udhcpd -f /etc/udhcpd_wifi.conf &
此时在手机端WiFi列表中可以看到mt7601ap_test,输入密码可成功连接.