WG225模块(SDIO WIFI)调试记录

由于笔记是在wznote做的,图片部分无法粘贴过来,如需要看图可以点击此处查看

主芯片:TI-DM8168
Linux版本:2.6.37

WG225是深圳天工测控做的一款wifi+蓝牙的芯片模组

首先解压官方提供的WIFI驱动,编译一个.ko模块
RTL8821CS_WiFi_linux_v5.2.8_21706_COEX20170310-1212.20170323
document中的Quick_Start_Guide_for_Driver_Compilation_and_Installation会教你如何快速编译一个KO文件
首先进入驱动的目录

work@zigsun:~/ayu/sdiowifi/RTL8821CS_WiFi_linux_v5.2.8_21706_COEX20170310-1212.20170323/driver/rtl8821CS_linux_v5.2.8_21706.20170323_COEX20170310-1212$ ls -l
总用量 112
-rwxr-xr-x 1 work work    64  3月 23  2017 clean
drwxr-xr-x 3 work work  4096  9月 25 15:35 core
drwxr-xr-x 9 work work  4096  9月 25 15:35 hal
-rwxr-xr-x 1 work work    54  3月 23  2017 ifcfg-wlan0
drwxr-xr-x 4 work work 12288  3月 23  2017 include
-rwxr-xr-x 1 work work    94  3月 23  2017 Kconfig
-rwxr-xr-x 1 work work 60904  9月 17 14:33 Makefile
drwxr-xr-x 3 work work  4096  9月 25 15:35 os_dep
drwxr-xr-x 2 work work  4096  9月 25 15:35 platform
-rwxr-xr-x 1 work work  2330  3月 23  2017 rtl8821c.mk
-rwxr-xr-x 1 work work   423  3月 23  2017 runwpa
-rwxr-xr-x 1 work work   294  3月 23  2017 wlan0dhcp

修改它的makefile
1.在50行左右,关掉节能模式CONFIG_POWER_SAVING=n,其余不修改

########################## Features ###########################
CONFIG_MP_INCLUDED = y
CONFIG_POWER_SAVING = n
CONFIG_USB_AUTOSUSPEND = n
 CONFIG_HW_PWRP_DETECTION = n
CONFIG_WIFI_TEST = n
CONFIG_BT_COEXIST = y
CONFIG_INTEL_WIDI = n
CONFIG_WAPI_SUPPORT = n

2.105行左右增加自己的平台,将默认的x86平台设为n(此处只能有一个平台是y)

###################### Platform Related #######################
CONFIG_PLATFORM_TI_DM8168 = y
CONFIG_PLATFORM_I386_PC = n

3.在合适的位置添加编译参数,推荐在1063行附近,按照原有的格式和位置添加

ifeq ($(CONFIG_PLATFORM_TI_DM8168), y)
EXTRA_CFLAGS += -DCONFIG_LITTLE_ENDIAN
ARCH = arm
CROSS_COMPILE = /opt/CodeSourcery/arm-2009q1/bin/arm-none-linux-gnueabi-
KSRC := /zigsun/8003/DVRRDK_04.00.00.03/ti_tools/linux-dvr-rdk
MODULE_NAME := wlan
endif
EXTRA_CFLAGS:配置为小端序
ARCH:ARM平台
CROSS_COMPILE:gcc工具链,根据个人情况,使用绝对路径
KSRC:linux内核源码,根据个人情况,使用绝对路径
MODULE_NAME:生成的.ko名字

内核自带sdio的驱动,一般情况下都能识别到sdio设备,但是我这边识别失败了

mmc0: card claims to support voltages below the defined range. These will be ignored.
mmc0: unrecognised CCCR structure version 3
mmc0: error -22 whilst initialising SDIO card

在未识别到sdio设备的情况下安装之前编译的wifi驱动,打印信息如下,虽然不报错,但是无法打开网卡wlan0

root@ti81xx:/mnt/net# insmod wlan.ko
RTW: module init start
RTW: rtl8821cs v5.2.8_21706.20170323_COEX20170310-1212
RTW: build time: Sep 17 2019 13:09:53
RTW: rtl8821cs BT-Coex version = COEX20170310-1212
RTW: module init ret=0

需要修改内核相关的代码,将SDIO_CCCR_REV_1_20这个宏的值改为3

//U:\8003\DVRRDK_04.00.00.03\ti_tools\linux-dvr-rdk\drivers\mmc\core\sdio.c
//line 108
if (cccr_vsn > SDIO_CCCR_REV_1_20) {
    printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
    mmc_hostname(card->host), cccr_vsn);
    return -EINVAL;
 }

修改之后,重新烧录内核,启动后成功识别sdio设备

mmc0: card claims to support voltages below the defined range. These will be ignored.
mmc0: new high speed SDIO card at address 0001

除了看系统log来判断,也可以通过 /sys/bus/mmc/devices下面是否有内容来判断是否识别,比如我这边识别后可以看到mmc0:0001
此时,安装刚刚编译好的驱动,我这边的打印信息如下,有报错,但不影响使用

root@ti81xx:/mnt/net# insmod wlan.ko
RTW: module init start
RTW: rtl8821cs v5.2.8_21706.20170323_COEX20170310-1212
RTW: build time: Sep 20 2019 19:21:43
RTW: rtl8821cs BT-Coex version = COEX20170310-1212
RTW: == SDIO Card Info ==
RTW:   clock: 50000000 Hz
RTW:   timing spec :sd high-speed
RTW:   sd3_bus_mode: FALSE
RTW: ================
RTW: CHIP TYPE: RTL8821C
RTW: ERROR [HALMAC]13348M
RTW: ERROR [HALMAC]HALMAC_MAJOR_VER = 1
RTW: ERROR [HALMAC]HALMAC_PROTOTYPE_VER = 3
RTW: ERROR [HALMAC]HALMAC_MINOR_VER = 5
RTW: ERROR [HALMAC]HALMAC_PATCH_VER = 3
RTW: ERROR [HALMAC]13359M
RTW: ERROR [HALMAC]HALMAC_MAJOR_VER_88XX = 1
RTW: ERROR [HALMAC]HALMAC_PROTOTYPE_88XX = 3
RTW: ERROR [HALMAC]HALMAC_MINOR_VER_88XX = 5
RTW: ERROR [HALMAC]HALMAC_PATCH_VER_88XX = 3
RTW: rtw_hal_config_rftype RF_Type is 3 TotalTxPath is 1
RTW: Chip Version Info: CHIP_8821C_Normal_Chip_UMC_B_CUT_1T1R_RomVer(1)
RTW: SetHwReg: bMacPwrCtrlOn=1
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(7416)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(7416)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(8240)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:42 dbg_rtw_zmalloc(5840)
RTW: DBG_MEM_ALLOC sdio_write_data_rsvd_page:58 dbg_rtw_mfree(5840)
RTW: rtl8821c_fw_dl Download Firmware from array success
RTW: NIC FW Version:9 SubVersion:7
RTW: c2h_mac_hidden_rpt_hdl: 0x2B
RTW: c2h_mac_hidden_rpt_hdl: 0x4A
RTW: c2h_mac_hidden_rpt_hdl: 0x04
RTW: c2h_mac_hidden_rpt_hdl: 0x14
RTW: c2h_mac_hidden_rpt_hdl: 0xF6
RTW: c2h_mac_hidden_rpt_hdl: 0xFF
RTW: c2h_mac_hidden_rpt_hdl: 0x37
RTW: c2h_mac_hidden_rpt_hdl: 0xFF
RTW: uuid x:0x2b y:0x4a z:0x4 crc:0xa0
RTW: hci_type:0x6
RTW: package_type:0x7
RTW: tr_switch:0x1
RTW: wl_func:0xf
RTW: hw_stype:0xf
RTW: bw:0x7
RTW: fab:0x2
RTW: ant_num:0x1
RTW: protocol:0x3
RTW: nic:0x3
RTW: c2h_mac_hidden_rpt_2_hdl: 0xFF
RTW: c2h_mac_hidden_rpt_2_hdl: 0xFF
RTW: c2h_mac_hidden_rpt_2_hdl: 0xF8
RTW: c2h_mac_hidden_rpt_2_hdl: 0xFC
RTW: c2h_mac_hidden_rpt_2_hdl: 0xFF
RTW: hal_read_mac_hidden_rpt OK! (1, 70ms), fwdl:1, id:0x19
RTW: is_valid_id_status: HALMAC_FEATURE_DUMP_LOGICAL_EFUSE
RTW: HW EFUSE
RTW: 0x000: 29 81 00 BC 09 10 28 00    AA 04 AD 75 10 8E 30 0B
RTW: 0x010: FF FF FF FF FF FF FF FF    FF FF FF 02 FF FF FF FF
RTW: 0x020: FF FF 27 25 26 25 23 24    23 26 28 2A 27 28 2A 28
RTW: 0x030: 21 FF FF FF FF FF 40 FF    FF FF 2A 2A 2A 2A 2A 2A
RTW: 0x040: 31 31 31 30 30 00 FF FF    FF FF FF FF FF FF FF FF
RTW: 0x050: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x060: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x070: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x080: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x090: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x0a0: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x0b0: FF FF FF FF FF FF FF FF    7F 37 1F 00 FF FF FF FF
RTW: 0x0c0: FF 21 00 41 00 00 00 00    00 FF 02 FF FF FF FF FF
RTW: 0x0d0: 7E D1 01 23 34 FF FF FF    20 04 4C 02 21 C8 21 02
RTW: 0x0e0: 0C 00 22 04 00 08 00 32    FF 21 02 0C 00 22 2A 01
RTW: 0x0f0: 01 00 00 00 00 00 00 00    00 00 00 00 02 00 FF FF
RTW: 0x100: 00 00 00 00 00 00 00 00    00 00 00 00 00 00 00 00
RTW: 0x110: 00 EB 00 6E 01 00 00 00    00 FF 30 EB 1F 0C CD 86
RTW: 0x120: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x130: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x140: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x150: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x160: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x170: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x180: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x190: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x1a0: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x1b0: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x1c0: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x1d0: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x1e0: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: 0x1f0: FF FF FF FF FF FF FF FF    FF FF FF FF FF FF FF FF
RTW: rtw_read_efuse_from_file /system/etc/wifi/wifi_efuse_8821cs.map is not readable
RTW: rtl8821c_read_efuse: <WARN> invalid phy efuse and read from file fail, will use driver default!!
RTW: EEPROM ID = 0x8129
RTW: EEPROM Version = 0
RTW: [A] 2G G00 CCK-1T base:45 from IC_DEF
RTW: [A] 2G G01 CCK-1T base:45 from IC_DEF
RTW: [A] 2G G02 CCK-1T base:45 from IC_DEF
RTW: [A] 2G G03 CCK-1T base:45 from IC_DEF
RTW: [A] 2G G04 CCK-1T base:45 from IC_DEF
RTW: [A] 2G G05 CCK-1T base:45 from IC_DEF
RTW: [A] 2G G00 BW40-1S base:45 from IC_DEF
RTW: [A] 2G G01 BW40-1S base:45 from IC_DEF
RTW: [A] 2G G02 BW40-1S base:45 from IC_DEF
RTW: [A] 2G G03 BW40-1S base:45 from IC_DEF
RTW: [A] 2G G04 BW40-1S base:45 from IC_DEF
RTW: EEPROM Regulatory=0x01
RTW: EEPROM Board Type=0x01
RTW: EEPROM Enable BT-coex, ant_num=1
RTW: hal_com_config_channel_plan chplan:0x7F
RTW: EEPROM ChannelPlan=0x7f
RTW: EEPROM crystal_cap=0x37
RTW: EEPROM ThermalMeter=0x1f
RTW: EEPROM Customer ID=0x00
RTW: EEPROM SupportRemoteWakeup=0
RTW: EEPROM rfe_type=0x2
RTW: WIFI Module is iPA/iLNA
RTW: EEPROM tx_bbswing_24G =0x00
RTW: EEPROM tx_bbswing_5G =0x00
RTW: SetHwReg: bMacPwrCtrlOn=0
RTW: rtw_hal_read_chip_info in 630 ms
RTW: init_channel_set((null)) ChannelPlan ID:0x7f, ch num:37
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(20992)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(5632)
RTW: DBG_MEM_ALLOC rtw_os_xmit_resource_alloc:121 dbg_rtw_zmalloc(5632)
RTW: rtw_alloc_macid((null)) if1, hwaddr:ff:ff:ff:ff:ff:ff macid:1
RTW: init_phydm_cominfo: fab_ver=1 cut_ver=1
RTW: rtw_regsty_chk_target_tx_power_valid return _FALSE for band:0, path:0, rs:0, t:-1
RTW: phy_ConfigBBWithPgParaFile(): No File PHY_REG_PG.txt, Load from HWImg Array!
RTW: default power by rate loaded
RTW: phy_txpwr_by_rate_chk_for_path_dup duplicate 2.4G [A] to [B]
RTW: rtw_macaddr_cfg mac addr:30:eb:1f:0c:cd:86
RTW: disable_interrupt: update SDIO HIMR=0
RTW: bDriverStopped:True, bSurpriseRemoved:False, bup:0, hw_init_completed:0
RTW: rtw_ndev_init(wlan0) if1 mac_addr=30:eb:1f:0c:cd:86
RTW: rtw_ndev_notifier_call(wlan0) state:16
RTW: rtw_ndev_notifier_call(wlan0) state:5
RTW: module init ret=0

如果安装驱动时报错如下,可能是由于menuconfig更改了内核wireless或者MMC/SD部分的配置
解决办法:将驱动make clean,然后重新编译

wlan: disagrees about version of symbol netif_napi_add
wlan: Unknown symbol netif_napi_add (err -22)
wlan: disagrees about version of symbol netif_receive_skb
wlan: Unknown symbol netif_receive_skb (err -22)
wlan: disagrees about version of symbol netif_napi_del
wlan: Unknown symbol netif_napi_del (err -22)
wlan: disagrees about version of symbol free_netdev
wlan: Unknown symbol free_netdev (err -22)
wlan: disagrees about version of symbol register_netdevice
wlan: Unknown symbol register_netdevice (err -22)
wlan: disagrees about version of symbol __napi_schedule
wlan: Unknown symbol __napi_schedule (err -22)
wlan: disagrees about version of symbol netif_device_attach
wlan: Unknown symbol netif_device_attach (err -22)
wlan: disagrees about version of symbol dev_alloc_name
wlan: Unknown symbol dev_alloc_name (err -22)
wlan: disagrees about version of symbol dev_get_by_name
wlan: Unknown symbol dev_get_by_name (err -22)
wlan: disagrees about version of symbol netif_rx
wlan: Unknown symbol netif_rx (err -22)
wlan: disagrees about version of symbol dev_kfree_skb_any
wlan: Unknown symbol dev_kfree_skb_any (err -22)
wlan: disagrees about version of symbol wireless_send_event
wlan: Unknown symbol wireless_send_event (err -22)
wlan: disagrees about version of symbol napi_complete
wlan: Unknown symbol napi_complete (err -22)
wlan: disagrees about version of symbol unregister_netdevice_queue
wlan: Unknown symbol unregister_netdevice_queue (err -22)
wlan: disagrees about version of symbol __netif_schedule
wlan: Unknown symbol __netif_schedule (err -22)
wlan: disagrees about version of symbol unregister_netdev
wlan: Unknown symbol unregister_netdev (err -22)
wlan: disagrees about version of symbol register_netdev
wlan: Unknown symbol register_netdev (err -22)
wlan: disagrees about version of symbol napi_gro_receive
wlan: Unknown symbol napi_gro_receive (err -22)
insmod: error inserting 'wlan.ko': -1 Invalid parameters

成功加载内核后,打开wlan0网卡

ifconfig wlan0 up

有时候在加载wlan.ko时,RTW: module init ret=0结束后,还会打印两句话

RTW: rtw_ndev_notifier_call(wlan1) state:10
udev[292]: renamed network interface wlan0 to wlan1

这时候的网卡设备是wlan1,要使用ifconfig wlan1 up才能打开网卡
我的处理是写一个脚本,每次开机时,先删除规则文件

rm /etc/udev/rules.d/70-persistent-net.rules    #防止生成的网卡不是wlan0

然后检测dmesg的renamed network有关的信息,如果没有,则启用wlan0,否则,启用其它对应的网卡

下面是如何连接周围的热点,可以参考文档Quick_Start_Guide_for_Station_Mode.pdf
需要使用到wpa_supplicant系列的工具,一般linux系统是带这个工具的
如果没有,在驱动目录V:\天工测控WIFI\WG225驱动\RTL8821CS_WiFi_linux_v5.2.8_21706_COEX20170310-1212.20170323\wpa_supplicant_hostapd中,编译出wpa_supplicant,wpa_cli等,然后将其应用程序和依赖的库、配置文件移植到linux系统
wpa_supplicant 是依赖两个库,需要先编译2个库libnl和openssl,最后编译wpa_supplicant

打开 /etc/wpa_supplicant.conf,设置wpa_supplicant的参数

ctrl_interface=/var/run/wpa_supplicant
update_config=1
ap_scan=1
network={
        ssid="zigsun"       #wifi名字
        psk="zigsun123"     #wifi密码
        priority=2          #多个wifi时需要设置优先级,数字小的优先
}

手机开一个wifi,然后执行下面的语句,即可自动连接wifi
wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf –B
也可以通过wpa_cli工具,进行手动搜索和连接,具体操作可以百度
调试时碰到如下信息,是因为重复打开wpa_supplicant 应用的原因

root@ti81xx:~# wpa_supplicant -iwlan0 -Dwext -c /etc/wpa_supplicant.conf -B
ioctl[SIOCSIWAP]: Operation not permitted
ctrl_iface exists and seems to be in use - cannot override it
Delete '/var/run/wpa_supplicant/wlan0' manually if it is not used anymore
Failed to initialize control interface '/var/run/wpa_supplicant'.
You may have another wpa_supplicant process already running or the file was
left by an unclean termination of wpa_supplicant in which case you will need
to manually remove this file before starting wpa_supplicant again.

他会把网卡wlan给关掉,可以用killall wpa_supplicant把应用关掉,然后把网卡打开后,再尝试重新打开wpa_supplicant

如何利用wlan0建立一个AP热点
可以参考文档Quick_Start_Guide_for_SoftAP.pdf
下面首先要使用的是hostapd工具,来建立一个热点
如果系统没有这个工具,将wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz这个解压后进行编译
目前我系统自带的hostapd是不支持这款WIFI模块,所以我使用官方驱动中的wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz
编译成功后,目录如下

work@zigsun:/rootfs/hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostap                                           d$ ls -l
总用量 2352
-rwxr-xr-x 1 root root   16913  8月 12  2013 Android.mk
-rwxr-xr-x 1 root root    1817  8月 12  2013 bsd_hostapd.conf
-rwxr-xr-x 1 root root   33567  8月 12  2013 ChangeLog
-rwxr-xr-x 1 root root   58233  8月 12  2013 config_file.c
-rw-r--r-- 1 root root     617  9月 19 18:30 config_file.d
-rwxr-xr-x 1 root root     562  8月 12  2013 config_file.h
-rw-r--r-- 1 root root   88496  9月 19 18:30 config_file.o
-rwxr-xr-x 1 root root   27967  8月 12  2013 ctrl_iface.c
-rw-r--r-- 1 root root     773  9月 19 18:30 ctrl_iface.d
-rwxr-xr-x 1 root root     880  8月 12  2013 ctrl_iface.h
-rw-r--r-- 1 root root   68044  9月 19 18:30 ctrl_iface.o
-rwxr-xr-x 1 root root    6764  8月 12  2013 defconfig
-rwxr-xr-x 1 root root    4788  8月 12  2013 dump_state.c
-rw-r--r-- 1 root root     652  9月 19 18:30 dump_state.d
-rwxr-xr-x 1 root root     549  8月 12  2013 dump_state.h
-rw-r--r-- 1 root root   38752  9月 19 18:30 dump_state.o
-rwxr-xr-x 1 root root    3056  8月 12  2013 eap_register.c
-rw-r--r-- 1 root root     262  9月 19 18:30 eap_register.d
-rwxr-xr-x 1 root root     528  8月 12  2013 eap_register.h
-rw-r--r-- 1 root root    4776  9月 19 18:30 eap_register.o
-rwxr-xr-x 1 root root    2581  8月 12  2013 eap_testing.txt
-rwxr-xr-x 1 root root   15511  8月 12  2013 hlr_auc_gw.c
-rwxr-xr-x 1 root root     734  8月 12  2013 hlr_auc_gw.milenage_db
-rwxr-xr-x 1 root root 1604366  9月 19 18:30 hostapd
-rwxr-xr-x 1 root root    1514  8月 12  2013 hostapd.8
-rwxr-xr-x 1 root root     276  8月 12  2013 hostapd.accept
-rwxr-xr-x 1 root root   57830  9月 19 18:30 hostapd_cli
-rwxr-xr-x 1 root root    1853  8月 12  2013 hostapd_cli.1
-rwxr-xr-x 1 root root   25218  8月 12  2013 hostapd_cli.c
-rw-r--r-- 1 root root     261  9月 19 18:30 hostapd_cli.d
-rw-r--r-- 1 root root   43176  9月 19 18:30 hostapd_cli.o
-rwxr-xr-x 1 root root   42845  8月 12  2013 hostapd.conf
-rwxr-xr-x 1 root root     144  8月 12  2013 hostapd.deny
-rwxr-xr-x 1 root root    3975  8月 12  2013 hostapd.eap_user
-rwxr-xr-x 1 root root     142  8月 12  2013 hostapd.radius_clients
-rwxr-xr-x 1 root root     409  8月 12  2013 hostapd.sim_db
-rwxr-xr-x 1 root root     282  8月 12  2013 hostapd.vlan
-rwxr-xr-x 1 root root     549  8月 12  2013 hostapd.wpa_psk
drwxr-xr-x 2 root root    4096  8月 12  2013 logwatch
-rwxr-xr-x 1 root root   13662  8月 12  2013 main.c
-rw-r--r-- 1 root root     669  9月 19 18:30 main.d
-rw-r--r-- 1 root root   53304  9月 19 18:30 main.o
-rwxr-xr-x 1 root root   17937  9月 19 18:29 Makefile
-rwxr-xr-x 1 root root    1141  8月 12  2013 nt_password_hash.c
-rwxr-xr-x 1 root root   17625  8月 12  2013 README
-rwxr-xr-x 1 root root   11456  8月 12  2013 README-WPS
lrwxrwxrwx 1 root root       6  9月 11  2013 src -> ../src
-rwxr-xr-x 1 root root    1152  8月 12  2013 wired.conf

编译后生成的二进制程序hostapd、hostapd_cli以及配置文件hostapd.conf需要移植到linux系统
hostapd、hostapd_cli放在/usr/sbin
hostapd.conf放在/etc

cp hostapd /usr/sbin/
cp hostapd_cli /usr/sbin/
cp hostapd.conf /etc/

修改配置文件hostapd.conf的内容如下,目的是生成一个开放WIFI

ctrl_interface=/var/run/hostapd
interface=wlan0
driver=rtl871xdrv
ssid=glx_test
channel=3
hw_mode=g

运行hostapd /etc/hostapd.conf后,已经可以通过手机搜索到相应的WIFI,但是一般情况下很难连接成功,因为AP需要用dhcpd工具给手机分配IP
可以在后面加上&让此程序后台执行

root@ti81xx:/mnt/net/tmp# hostapd /etc/hostapd.conf 
Configuration file: /etc/hostapd.conf
drv->ifindex=4
l2_sock_recv==l2_sock_xmit=0x0x67640
+rtl871x_sta_deauth_ops, ff:ff:ff:ff:ff:ff is deauth, reason=2
rtl871x_set_key_ops
rtl871x_set_key_ops
rtl871x_set_key_ops
rtl871x_set_key_ops
Using interface wlan0 with hwaddr 30:eb:1f:0c:cd:86 and ssid 'glx_test'
rtl871x_set_wps_assoc_resp_ie
rtl871x_set_wps_beacon_ie
rtl871x_set_wps_probe_resp_ie
rtl871x_set_beacon_ops
rtl871x_set_hidden_ssid ignore_broadcast_ssid:0, glx_test,8
rtl871x_set_acl
+rtl871x_get_sta_wpaie, b4:86:55:39:f3:cf is sta's address
wlan0: STA b4:86:55:39:f3:cf IEEE 802.11: associated
wlan0: STA b4:86:55:39:f3:cf RADIUS: starting accounting session 5105E60E-00000000
+rtl871x_get_sta_wpaie, b4:86:55:39:f3:cf is sta's address
wlan0: STA b4:86:55:39:f3:cf IEEE 802.11: associated
wlan0: STA b4:86:55:39:f3:cf RADIUS: starting accounting session 5105E60E-00000001
+rtl871x_get_sta_wpaie, b4:86:55:39:f3:cf is sta's address
wlan0: STA b4:86:55:39:f3:cf IEEE 802.11: associated
wlan0: STA b4:86:55:39:f3:cf RADIUS: starting accounting session 5105E60E-00000002

udhcpd一般linux是自带的,如果没有,需要先编译,再将应用程序移植到linux
其配置文件也是放在/etc/udhcpd.conf,内容如下

# Sample udhcpd configuration file (/etc/udhcpd.conf)
# The start and end of the IP lease block
start       192.168.2.2 #default: 192.168.0.20
end     192.168.2.254   #default: 192.168.0.254
# The interface that udhcpd will use
interface   wlan0       #default: eth0
# The maximim number of leases (includes addressesd reserved
# by OFFER's, DECLINE's, and ARP conficts
max_leases  253     #default: 254
# If remaining is true (default), udhcpd will store the time
# remaining for each lease in the udhcpd leases file. This is
# for embedded systems that cannot keep time between reboots.
# If you set remaining to no, the absolute time that the lease
# expires at will be stored in the dhcpd.leases file.
remaining   yes     #default: yes
# The time period at which udhcpd will write out a dhcpd.leases
# file. If this is 0, udhcpd will never automatically write a
# lease file. (specified in seconds)
auto_time   7200        #default: 7200 (2 hours)
# The amount of time that an IP will be reserved (leased) for if a
# DHCP decline message is received (seconds).
decline_time    3600        #default: 3600 (1 hour)
# The amount of time that an IP will be reserved (leased) for if an
# ARP conflct occurs. (seconds
conflict_time   3600        #default: 3600 (1 hour)
# How long an offered address is reserved (leased) in seconds
offer_time  60      #default: 60 (1 minute)
# If a lease to be given is below this value, the full lease time is
# instead used (seconds).
min_lease   60      #defult: 60
# The location of the leases file
lease_file  /var/lib/misc/udhcpd.leases #defualt: /var/lib/misc/udhcpd.leases
# The location of the pid file
pidfile /var/run/udhcpd-wlan2.pid   #default: /var/run/udhcpd.pid
# Every time udhcpd writes a leases file, the below script will be called.
# Useful for writing the lease file to flash every few hours.
#notify_file                #default: (no script)
#notify_file    dumpleases  # <--- useful for debugging
# The following are bootp specific options, setable by udhcpd.
#siaddr     192.168.0.22        #default: 0.0.0.0
#sname      zorak           #default: (none)
#boot_file  /var/nfs_root       #default: (none)
# The remainer of options are DHCP options and can be specifed with the
# keyword 'opt' or 'option'. If an option can take multiple items, such
# as the dns option, they can be listed on the same line, or multiple
# lines. The only option with a default is 'lease'.
#Examles
opt dns 114.114.114.114 #192.168.2.1
option  subnet  255.255.255.0
option  domain  local   #atherosowl.com
option  lease   864000      # 10 days of seconds
# Currently supported options, for more info, see options.c
#opt subnet
#opt timezone
opt router 192.168.2.1
#opt timesvr
#opt namesvr
#opt dns
#opt logsvr
#opt cookiesvr
#opt lprsvr
#opt bootsize
#opt domain
#opt swapsvr
#opt rootpath
#opt ipttl
#opt mtu
#opt broadcast
#opt wins
#opt lease
#opt ntpsrv
#opt tftp
#opt bootfile
# Static leases map
#static_lease 00:60:08:11:CE:4E 192.168.0.54
#static_lease 00:60:08:11:CE:3E 192.168.0.44

先ps看一下后台有没有udhcpd和hostapd程序在执行
有的话就killall udhcpd 或者 killall hostapd给关掉

udhcpd -fS /etc/udhcpd.conf&
hostapd -B /etc/hostapd.conf

执行这两个程序,可能udhcpd会报如下的错误,在相应目录新建一个文件即可

root@ti81xx:~# udhcpd (v1.19.4) started
udhcpd: can't open '/var/lib/misc/udhcpd.leases': No such file or directory

-B和&都代表后台运行的意思
&跟程序无关,会在终端打印信息
-B跟程序处理逻辑有关,不会在终端打印信息

执行了以上两个步骤,手机应该可以轻松连上wifi,但是无法访问公网
需要将wlan路由到eth0,通过eth0转发来访问公网
全部过程如下

insmod wlan.ko
ifconfig wlan0 192.168.2.1 netmask 255.255.255.0 up
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
echo "1" >/proc/sys/net/ipv4/ip_forward
route del default gw 192.168.2.1
route add default gw 192.168.2.1
udhcpd -fS /etc/udhcpd.conf&
hostapd -B /etc/hostapd.conf

以上过程应该能生成一个开放热点了,如果不行,参考一下我下面的的脚本

#该脚本运行后,将上图的文件复制到指定位置,方便wg225_load脚本的调用
#!/bin/sh
set -x 
echo "copy wlan.ko for installing wg225 wifi driver"
cp wlan.ko /lib/modules
echo "copy hostapd for AP function"
cp hostapd /usr/sbin/
cp hostapd_cli /usr/sbin/
echo "copy wg225_load.sh for auto load"
cp wg225_load.sh /usr/sbin/
chmod 777 /usr/sbin/wg225_load.sh

安装完成后,在init.d中添加wg225_load脚本,这样开机自动运行
这个脚本我大概说一下写的思路
首先func,ssid,passwd这几个常用的,放在最前面方便更改
需要用sta功能的时候,就屏蔽ap,反之亦然
然后把sta和ap的所有配置写到对应的配置文件里
最后根据功能的选择,和配置的不同,去做不同的操作

#!/bin/sh
#print dubug info
set -x
########################################################################
#wg225_func="sta"
wg225_func="ap"
wg225_ssid="zigsun"
wg225_passwd="zigsun123"
########################################################################
########################################################################
#:<<STA_FUNC_DEFINE
    sta_config_file="/etc/wpa_supplicant.conf"
    rm $sta_config_file
    touch $sta_config_file
    echo "ctrl_interface=/var/run/wpa_supplicant"   >> $sta_config_file
    echo "update_config=1"                          >> $sta_config_file
    echo "ap_scan=1"                                >> $sta_config_file
#:<<open_network
    echo "network={"                                >> $sta_config_file
    echo "    ssid=\"$wg225_ssid\""                 >> $sta_config_file
    echo "    key_mgmt=NONE"                        >> $sta_config_file
    echo "}"                                        >> $sta_config_file
#open_network
#:<<wpa-psk_network
    echo "network={"                                >> $sta_config_file
    echo "    ssid=\"$wg225_ssid\""                 >> $sta_config_file
    echo "    psk=\"$wg225_passwd\""                >> $sta_config_file
    echo "    priority=1"                           >> $sta_config_file #The smaller the number, the higher the priority
    echo "}"                                        >> $sta_config_file
#wpa-psk_network
#STA_FUNC_DEFINE
########################################################################
########################################################################
#:<<AP_FUNC_DEFINE
    udhcpd_config_file="/etc/udhcpd.conf"
    rm $udhcpd_config_file
    touch $udhcpd_config_file
    echo "max_leases 100"           >> $udhcpd_config_file
    echo "start 192.168.10.11"      >> $udhcpd_config_file
    echo "end 192.168.10.110"       >> $udhcpd_config_file
    echo "opt router 192.168.10.1"  >> $udhcpd_config_file
    echo "interface wlan0"          >> $udhcpd_config_file
    echo "remaining yes"            >> $udhcpd_config_file
    echo "auto_time 7200"           >> $udhcpd_config_file
    echo "decline_time 3600"        >> $udhcpd_config_file
    echo "conflict_time 3600"       >> $udhcpd_config_file
    echo "offer_time 60"            >> $udhcpd_config_file
    echo "min_lease 60"             >> $udhcpd_config_file
    echo "opt dns 114.114.114.114"      >> $udhcpd_config_file
    echo "option subnet 255.255.255.0"  >> $udhcpd_config_file
    echo "option domain local"          >> $udhcpd_config_file
    echo "option lease 864000"          >> $udhcpd_config_file
    echo "lease_file /var/lib/misc/udhcpd.leases"   >> $udhcpd_config_file
    echo "pidfile /var/run/udhcpd-wlan2.pid"        >> $udhcpd_config_file
    
    
    hostapd_config_file="/etc/hostapd.conf"
    rm $hostapd_config_file
    touch $hostapd_config_file
    echo "ctrl_interface=/var/run/hostapd"  >> $hostapd_config_file
    echo "interface=wlan0"                  >> $hostapd_config_file
    echo "driver=rtl871xdrv"                >> $hostapd_config_file
    echo "ssid=$wg225_ssid"                 >> $hostapd_config_file
    echo "channel=3"                        >> $hostapd_config_file
    echo "hw_mode=g"                        >> $hostapd_config_file
    echo "auth_algs=3"                      >> $hostapd_config_file
:<<wep
# this way is NOT recommended
# The key length should be 5, 13, or 16 characters, or 10, 26, or 32
# digits, depending on whether 40-bit (64-bit), 104-bit (128-bit), or
# 128-bit (152-bit) WEP is used.
    echo "wep_default_key=0"                >> $hostapd_config_file
    echo "wep_key0=0123456789"              >> $hostapd_config_file
    echo "wep_key_len_broadcast=13"         >> $hostapd_config_file
    echo "wep_key_len_unicast=13"           >> $hostapd_config_file
    echo "wep_rekey_period=300"             >> $hostapd_config_file
wep
:<<wpa-psk
    echo "wpa=1"                            >> $hostapd_config_file
    echo "wpa_passphrase=$wg225_passwd"         >> $hostapd_config_file
    echo "wpa_key_mgmt=WPA-PSK"             >> $hostapd_config_file
    echo "wpa_pairwise=TKIP CCMP"           >> $hostapd_config_file
wpa-psk
:<<wpa-psk2
    echo "wpa=2"                            >> $hostapd_config_file
    echo "wpa_passphrase=$wg225_passwd"         >> $hostapd_config_file
    echo "wpa_key_mgmt=WPA-PSK"             >> $hostapd_config_file
    echo "rsn_pairwise=TKIP CCMP"           >> $hostapd_config_file
wpa-psk2
#:<<wpa-psk_and_wpa-psk2
#wpa-psk/wpa-psk2 is recommended
    echo "wpa=3"                            >> $hostapd_config_file
    echo "wpa_passphrase=$wg225_passwd"     >> $hostapd_config_file
    echo "wpa_key_mgmt=WPA-PSK WPA-EAP"     >> $hostapd_config_file
    echo "wpa_pairwise=TKIP CCMP"           >> $hostapd_config_file
    echo "rsn_pairwise=TKIP CCMP"           >> $hostapd_config_file
#wpa-psk_and_wpa-psk2
#AP_FUNC_DEFINE
########################################################################
    
    
    
########################################################################
#:<<MAIN
killall wpa_supplicant
killall udhcpc
killall udhcpd
killall hostapd
#delete this rule file to ensure that wlan0 will not be renamed to wlan1 or something 
rm /etc/udev/rules.d/70-persistent-net.rules
#if wg225 is detected, install wlan.ko
wg225_exist=`ls /sys/bus/mmc/devices/`
if [ "${wg225_exist}" ];then
    echo "wg225 module is exist, wlan.ko will install"
    insmod /lib/modules/wlan.ko
    #ifconfig wlan0 up
    wg225_wlan=`dmesg | grep "renamed network interface" | awk '{print $7}'`
    if [ "${wg225_wlan}" = "" ];then
        wg225_wlan="wlan0"
    fi
    ifconfig $wg225_wlan up
    
    
    #select function
    echo set $wg225_wlan az $wg225_func
    if [ $wg225_func = "sta" ];then
        udhcpc -iwlan0&
        wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf -B
    elif [ $wg225_func = "ap" ];then
        ifconfig $wg225_wlan 192.168.10.1 netmask 255.255.255.0 up
        iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
        iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT
        iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
        echo "1" >/proc/sys/net/ipv4/ip_forward
        touch /var/lib/misc/udhcpd.leases
        
        #get network segment
        dot=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 15-15`
        if [ $dot = "." ];then
            eth0=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 14-14`
        fi
        dot=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 16-16`
        if [ $dot = "." ];then
            eth0=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 14-15`
        fi
        dot=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 17-17`
        if [ $dot = "." ];then
            eth0=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 14-16`
        fi
        
        eth0_ip=`ifconfig eth0 | grep "inet addr:" | awk '{print $2}' | cut -c 6-`
        route del default gw 192.168.$eth0.1
        route add default gw 192.168.$eth0.1
        udhcpd -fS /etc/udhcpd.conf&
        hostapd -B /etc/hostapd.conf
    fi
else
    echo "wg225 module is not exist, wlan.ko will not install"
fi
#MAIN
########################################################################

猜你喜欢

转载自blog.csdn.net/whitefish520/article/details/101021374