在 Android 2.2上移植了2个wifi模块,vt6656和rt2070,总结一下要点。
首先,将wifi linux驱动编译成模块,并将驱动(vntwusb.ko或rt3070sta.ko放到/system/lib/modules/中。
然后,做如下修改:
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的错误。
修改 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设置进行初始化操作,要去看看设置的数据库修改情况。