9 Android WIFI 驅動移植

在 Android 2.2上移植了2個wifi模塊,vt6656和rt2070,總結一下要點。 java

首先,將wifi linux驅動編譯成模塊,並將驅動(vntwusb.ko或rt3070sta.ko放到/system/lib/modules/中 linux

而後,作以下修改: android

1。修改 init.rc:不少文章都有描述,但仍是有些說明不清的地方,我先列出增長項,而後做些說明。
增長: mkdir /system/etc/wifi 0771 wifi wifi
           chmod 0771 /system/etc/wifi
           chmod 0660 /system/etc/wifi/wpa_supplicant.conf
           chown wifi wifi /system/etc/wifi/wpa_supplicant.conf  #wifi的原始配置文件
           # wpa_supplicant socket
           mkdir /data/system/wpa_supplicant 0771 wifi wifi
           chmod 0771 /data/system/wpa_supplicant  #放置wifi interface的地方
           mkdir /data/misc/wifi 0771 wifi wifi
          chmod 0771 /data/misc/wifi
          chmod 0660 /data/misc/wifi/wpa_supplicant.conf  #wifi的配置文件,將由wpa_supplicant根據實際配置寫入該文件
          mkdir /data/misc/wifi/sockets
 0777 wifi wifi  #與上層經過socket通訊的路徑
           # Prepare for wifi
           setprop wifi.interface ra0  #intreface名稱設置,這在framework/base/wifi/java/android/net/wifi /WifiStateTracker.java中會用到,以處理dhcp。rt2070用ra0,而vt6656使用eth1。
這裏0771對 目錄權限的處理是爲了全部用戶能對下一級進行搜索,而紅字特別提醒的權限配置,是由於/data/misc/wifi/sockets目錄不只爲wifi擁有者服務,還由於通訊的緣由要和其餘用戶聯繫,要否則,將會出現Unable to open connection to supplicant on "/data/system/wpa_supplicant/ra0": Connection refused,或permission denied的錯誤。不少人乾脆將上述全部的權限都設爲0777,固然也行,但總以爲有些粗糙。
service的修改:
service wpa_supplicant /system/bin/logwrapper /system/bin/wpa_supplicant /
    -Dwext -ira0 -c/data/misc/wifi/wpa_supplicant.conf  #也能夠用/system/etc/wifi/wpa_supplicant.conf代替
    user root
    group system wifi inet
#    socket wpa_wlan0 dgram 660 wifi wifi   #屏蔽該項是由於這項是用於UDP鏈接的
    disable
    oneshot

service dhcpcd /system/bin/logwrapper /system/bin/dhcpcd -d -B ra0
    group system dhcp wifi
    disabled
    oneshot
2。修改system/etc/wifi/wpa_supplicant.conf (在源碼中是修改external/wpa_supplicant/wpa_supplicant.conf)
將ctrl_interface=wlan0改爲ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=wifi  #這個路徑在wifi.c中用到。
3。修改system/etc/dhcpcd/dhcpcd.conf
將其中的interface名稱改爲ra0
4。修改芯片廠商的配置 BoardConfig.mk。
例如,Freeescale 是在device/fsl/imx51_bbg/BoardConfig.mk,加入:
HAVE_CUSTOM_WIFI_DRIVER_2 := true
BOARD_WPA_SUPPLICANT_DRIVER := WEXT

5。修改hardware/libhardware_legacy/wifi/wifi.c
數據庫

已經定義的wifi驅動的路徑我感到仍是屏蔽爲好,直接在wifi.c中修改不是更直觀些。
ifndef WIFI_DRIVER_MODULE_PATH
#define WIFI_DRIVER_MODULE_PATH         "/system/lib/modules/rt3070sta.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME
#define WIFI_DRIVER_MODULE_NAME         "rt3070sta"
#endif
在 文件中還能夠看到其餘一些信息,如IFACE_DIR[]           = "/data/system/wpa_supplicant", 就是interface的安放的路徑。MODULE_FILE[]         = "/proc/modules",這是insmod安放module的路徑。在調試時能夠查詢是否已經安裝了模塊,接口是否啓動。
6。源碼修改
修改 external/wpa_supplicant/wpa_ctrl.c: 找到chmod那行,將 chmod(ctrl->local.sun_path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);改爲chmod(ctrl->local.sun_path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);  #增長權限這是爲了防止出現ioctl 寫message的錯誤。
app

修改 external/wpa_supplicant/driver_wext.c:這 是爲了不wpa_supplicant與 下層驅動 通信時出現ioctl[SIOCSIWPRIV]錯誤,由於如今大部分wifi模塊對 SIOCSIWPRIV命令不處理,而這個命令要用於偵測wifi強度RSSI的,比較簡單的方法是在wifi驅動中增長個空函數。例如,對於 rt2070,有一個sta_ioctl.c,找到SIOCSIWPRIV,將其對應個空函數

static int handler_SIOCSIWPRIV(struct net_device *dev, struct iw_request_info *info,
                         union iwreq_data *wrqu, char *extra)
{
       return 0;
}
不過,這樣作就有些信息,如RSSI,MAC地址等就無法在上層顯示了。比較好的方法以下:
在struct wpa_driver_wext_data 中增長
    u8 ssid[32];
    unsigned int ssid_len;
2個變量。將原來的wpa_driver_priv_driver_cmd函數改爲以下函數(我從Porting wifi driver to Android抄來稍做修改):
static int wpa_driver_priv_driver_cmd(void *priv, char *cmd, char *buf, size_t buf_len)
{

    struct wpa_driver_wext_data *drv = priv;
    int ret = -1;

    wpa_printf(MSG_DEBUG, "AWEXT: %s %s", __func__, cmd);

    if (os_strcasecmp(cmd, "start") == 0) {
        wpa_printf(MSG_DEBUG,"Start command");
        return (ret);
    }

    if (os_strcasecmp(cmd, "stop") == 0) {
        wpa_printf(MSG_DEBUG,"Stop command");
    }
    else if (os_strcasecmp(cmd, "macaddr") == 0) {
        struct ifreq ifr;
        os_memset(&ifr, 0, sizeof(ifr));
        os_strncpy(ifr.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) < 0) {
            perror("ioctl[SIOCGIFHWADDR]");
            ret = -1;
        } else {
            u8 *macaddr = (u8 *) ifr.ifr_hwaddr.sa_data;
            ret = snprintf(buf, buf_len, "Macaddr = " MACSTR "/n",
                           MAC2STR(macaddr));
        }
    }
    else if (os_strcasecmp(cmd, "scan-passive") == 0) {
        wpa_printf(MSG_DEBUG,"Scan Passive command");
    }
    else if (os_strcasecmp(cmd, "scan-active") == 0) {
        wpa_printf(MSG_DEBUG,"Scan Active command");
    }
    else if (os_strcasecmp(cmd, "linkspeed") == 0) {
        struct iwreq wrq;
        unsigned int linkspeed;
        os_strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);
        wpa_printf(MSG_DEBUG,"Link Speed command");
        if (ioctl(drv->ioctl_sock, SIOCGIWRATE, &wrq) < 0) {
            perror("ioctl[SIOCGIWRATE]");
            ret = -1;
        } else {
            linkspeed = wrq.u.bitrate.value / 1000000;
            ret = snprintf(buf, buf_len, "LinkSpeed %d/n", linkspeed);
        }
    }
    else if (os_strncasecmp(cmd, "scan-channels", 13) == 0) {
    }
    else if ((os_strcasecmp(cmd, "rssi") == 0) || (os_strcasecmp(cmd, "rssi-approx") == 0)) {
        struct iwreq wrq;
        struct iw_statistics stats;
        signed int rssi;
        wpa_printf(MSG_DEBUG, ">>>. DRIVER AWEXT RSSI ");
        wrq.u.data.pointer = (caddr_t) &stats;
        wrq.u.data.length = sizeof(stats);
        wrq.u.data.flags = 1; /* Clear updated flag */
        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIWSTATS, &wrq) < 0) {
            perror("ioctl[SIOCGIWSTATS]");
            ret = -1;
        } else {
            if (stats.qual.updated & IW_QUAL_DBM) {
                /* Values in dBm, stored in u8 with range 63 : -192 */
                rssi = ( stats.qual.level > 63 ) ?
                    stats.qual.level - 0x100 :
                    stats.qual.level;
            } else {
                rssi = stats.qual.level;
            }

            if (drv->ssid_len != 0 && drv->ssid_len < buf_len) {
                os_memcpy((void *) buf, (void *) (drv->ssid),
                        drv->ssid_len );
                ret = drv->ssid_len;
                ret += snprintf(&buf[ret], buf_len-ret,
                        " rssi %d/n", rssi);
                if (ret < (int)buf_len) {
                    return( ret );
                }
                ret = -1;
            }
        }
    }
    else if (os_strncasecmp(cmd, "powermode", 9) == 0) {
    }
    else if (os_strncasecmp(cmd, "getpower", 8) == 0) {
    }
    else if (os_strncasecmp(cmd, "get-rts-threshold", 17) == 0) {
        struct iwreq wrq;
        unsigned int rtsThreshold;

        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &wrq) < 0) {
            perror("ioctl[SIOCGIWRTS]");
            ret = -1;
        } else {
            rtsThreshold = wrq.u.rts.value;
            wpa_printf(MSG_DEBUG,"Get RTS Threshold command = %d",
                rtsThreshold);
            ret = snprintf(buf, buf_len, "rts-threshold = %u/n",
                rtsThreshold);
            if (ret < (int)buf_len) {
                return( ret );
            }
        }
    }
    else if (os_strncasecmp(cmd, "set-rts-threshold", 17) == 0) {
        struct iwreq wrq;
        unsigned int rtsThreshold;
        char *cp = cmd + 17;
        char *endp;

        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (*cp != '/0') {
            rtsThreshold = (unsigned int)strtol(cp, &endp, 0);
            if (endp != cp) {
                wrq.u.rts.value = rtsThreshold;
                wrq.u.rts.fixed = 1;
                wrq.u.rts.disabled = 0;

                if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &wrq) < 0) {
                    perror("ioctl[SIOCGIWRTS]");
                    ret = -1;
                } else {
                    rtsThreshold = wrq.u.rts.value;
                    wpa_printf(MSG_DEBUG,"Set RTS Threshold command = %d", rtsThreshold);
                    ret = 0;
                }
            }
        }
    }
    else if (os_strcasecmp(cmd, "btcoexscan-start") == 0) {
    }
    else if (os_strcasecmp(cmd, "btcoexscan-stop") == 0) {
    }
    else if (os_strcasecmp(cmd, "rxfilter-start") == 0) {
        wpa_printf(MSG_DEBUG,"Rx Data Filter Start command");
    }
    else if (os_strcasecmp(cmd, "rxfilter-stop") == 0) {
        wpa_printf(MSG_DEBUG,"Rx Data Filter Stop command");
    }
    else if (os_strcasecmp(cmd, "rxfilter-statistics") == 0) {
    }
    else if (os_strncasecmp(cmd, "rxfilter-add", 12) == 0 ) {
    }
    else if (os_strncasecmp(cmd, "rxfilter-remove",15) == 0) {
    }
    else if (os_strcasecmp(cmd, "snr") == 0) {
        struct iwreq wrq;
        struct iw_statistics stats;
        int snr, rssi, noise;

        wrq.u.data.pointer = (caddr_t) &stats;
        wrq.u.data.length = sizeof(stats);
        wrq.u.data.flags = 1; /* Clear updated flag */
        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIWSTATS, &wrq) < 0) {
            perror("ioctl[SIOCGIWSTATS]");
            ret = -1;
        } else {
            if (stats.qual.updated & IW_QUAL_DBM) {
                /* Values in dBm, stored in u8 with range 63 : -192 */
                rssi = ( stats.qual.level > 63 ) ?
                    stats.qual.level - 0x100 :
                    stats.qual.level;
                noise = ( stats.qual.noise > 63 ) ?
                    stats.qual.noise - 0x100 :
                    stats.qual.noise;
            } else {
                rssi = stats.qual.level;
                noise = stats.qual.noise;
            }

            snr = rssi - noise;

            ret = snprintf(buf, buf_len, "snr = %u/n", (unsigned int)snr);
            if (ret < (int)buf_len) {
                return( ret );
            }
        }
    }
    else if (os_strncasecmp(cmd, "btcoexmode", 10) == 0) {
    }
    else if( os_strcasecmp(cmd, "btcoexstat") == 0 ) {
    }
    else {
        wpa_printf(MSG_DEBUG,"Unsupported command");
    }
    return (ret);
}
通過這些修改後,wifi應該能夠上去了。
問題留存: 
1。此次修改的是5.x版本的wpa_supplicant,android已經出了6.x版本,我在那裏沒有發現driver_wext.c!,那我上面的修改如何實現?看來還要研究。
2。修改好的driver_wext.c能夠顯示MAC地址,在上層的設置界面也能顯示信號強度,但在主頁面上卻沒有顯示強度,估計還要在java程序中找找緣由。
3。在設置界面,若是將wifi關閉後再打開,wifi連不上,須要手工在調試終端打ifconfig ra0 up才能觸發鏈接上。估計是java程序在關閉wifi時沒有將wifi設置進行初始化操做,要去看看設置的數據庫修改狀況。
socket

相關文章
相關標籤/搜索