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
########################################################################
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章