`

Porting WiFi drivers to Android

    博客分类:
  • Wifi
阅读更多
转自:http://blog.linuxconsulting.ro/2010/04/porting-wifi-drivers-to-android.html

For mini-box.com picoPC we want to support several USB and miniPCI WiFi dongles, this guide provides a step by step explanation of what's involved in adding a new wifi driver and making wifi work in a custom Android build  (this guide was written for android 2.1 but should be applicable to previous android releases and hopefully future releases).
Contents
0. Understand how Android WiFi works.
1. Enable building of wpa_supplicant in your BoardConfig.mk
2. (Optional) Enable debug for wpa_supplicant.
3. Provide a proper wpa_supplicant.conf for your device
4. Have the correct paths and permissions created from init.rc
5. Make sure your wpa_supplicant and dhcpcd (optional) are starting from init.rc 
6. Provide your driver either as a module or built in kernel and proper kernel support for it and modify Android source code accordingly.
7. Provide a firmware if your module needs it.
8. Make your driver work with Android custom wpa_supplicant commands and SIOCSIWPRIV ioctl

Now onto details.

0. Understand how Android WiFi works.
Android uses a modified wpa_supplicant ( external/wpa_supplicant ) daemon for wifi support which is controlled through a socket by hardware/libhardware_legacy/wifi/wifi.c (WiFiHW) that gets controlled from Android UI through android.net.wifi package from frameworks/base/wifi/java/android/net/wifi/ and it's corresponding jni implementation in frameworks/base/core/jni/android_net_wifi_Wifi.cpp Higher level network management is done in frameworks/base/core/java/android/net

1. Enable building of wpa_supplicant in your BoardConfig.mk
This is by simply adding: BOARD_WPA_SUPPLICANT_DRIVER := WEXT to your BoardConfig.mk . This will set  WPA_BUILD_SUPPLICANT to true in external/wpa_supplicant/Android.mk enabling building of driver_wext.c
If you have a custom wpa_supplicant driver (like madwifi or my custom android private commands emulation - see last paragraph) you can replace WEXT with AWEXT or your driver name (MADWIFI, PRISM etc).

2. (Optional) Enable debug for wpa_supplicant.
By default wpa_supplicant is set to MSG_INFO that doesn't tell much.
To enable more messages:
  2.1 modify common.c and set wpa_debug_level = MSG_DEBUG
  2.2 modify common.h and change #define wpa_printf from if ((level) >= MSG_INFO) to if ((level) >= MSG_DEBUG)

3. Provide a proper wpa_supplicant.conf for your device
Providing a wpa_supplicant.conf it's important because the control socket for android is specified in this file (ctrl_interface= ). This file should be copied by your AndroidBoard.mk to $(TARGET_OUT_ETC)/wifi (usually /system/etc/wifi/wpa_supplicant.conf ). This location will be used on wpa_supplicant service from init.rc.
There are two different ways in which wpa_supplicant can be configured, one is to use a "private" socket in android namespace, created by socket_local_client_connect() function in wpa_ctrl.c and another is by using a standard unix socket.

Minimum required config options in wpa_supplicant.conf :
- Android private socket

ctrl_interface=wlan0
update_config=1

- Unix standard socket

ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=wifi
update_config=1


Depending on your driver you might also want to add:
ap_scan=1

If you have AP association problems with should change to ap_scan=0 to let the driver do the association instead of wpa_supplicant.
If you want to let wpa_supplicant connect to non-WPA or open wireless networks (by default it skips these kind) add:


network={
key_mgmt=NONE
}


4. Have the correct permissions and paths created from init.rc
Incorrect permisions will result in wpa_supplicant not being able to create/open the control socket and libhardware_legacy/wifi/wifi.c won't connect.
Since Google modified wpa_supplicant to run as wifi user/group the directory structure and file ownership should belong to wifi user/group (see os_program_init() function in wpa_supplicant/os_unix.c ).

Otherwise errors like:
E/WifiHW  (  ): Unable to open connection to supplicant on "/data/system/wpa_supplicant/wlan0": No such file or directory will appear.

Also wpa_supplicant.conf should belong to wifi user/group because wpa_supplicant will want to modify this file. If your system has /system as read-only use a location like /data/misc/wifi/wpa_supplicant.conf and modify wpa_supplicant service in init.rc with new location.
Make sure the paths are correctly created in init.rc:


mkdir /system/etc/wifi 0770 wifi wifi
chmod 0770 /system/etc/wifi
chmod 0660 /system/etc/wifi/wpa_supplicant.conf
chown wifi wifi /system/etc/wifi/wpa_supplicant.conf
#wpa_supplicant control socket for android wifi.c (android private socket)
mkdir /data/misc/wifi 0770 wifi wifi
mkdir /data/misc/wifi/sockets 0770 wifi wifi
chmod 0770 /data/misc/wifi
chmod 0660 /data/misc/wifi/wpa_supplicant.conf
chown wifi wifi /data/misc/wifi
chown wifi wifi /data/misc/wifi/wpa_supplicant.conf


If you use a Unix standard socket in wpa_supplicant.conf (see above) add:

# wpa_supplicant socket (unix socket mode)
mkdir /data/system/wpa_supplicant 0771 wifi wifi
chmod 0771 /data/system/wpa_supplicant
chown wifi wifi /data/system/wpa_supplicant

Do not add these if you use Android private socket because it will make wpa_supplicant non-functional, because hardware/libhardware_legacy/wifi/wifi.c check for existence of the /data/system/wpa_supplicant folder and will pass a wrong interface name to wpa_ctrl_open() function.

5. Make sure your wpa_supplicant and dhcpcd are starting from init.rc

For wpa_supplicant the init.rc startup like should be depending on which path you chosen:
- Android private socket:

service wpa_supplicant /system/bin/wpa_supplicant -dd -Dwext -iwlan0 -c /system/etc/wifi/wpa_supplicant.conf
socket wpa_wlan0 dgram 660 wifi wifi
group system wifi inet
disabled
oneshot

- Unix standard socket:

service wpa_supplicant /system/bin/wpa_supplicant -dd -Dwext -iwlan0 -c /system/etc/wifi/wpa_supplicant.conf
group system wifi inet
disabled
oneshot

If your wifi driver creates a wifi interface with other name than wlan0 you will have to modify the above line accordingly.
You also should have dhcpcd starting from init.rc

service dhcpcd /system/bin/dhcpcd wlan0
group system dhcp
disabled
oneshot


6. Provide your driver either as a module or built in kernel and proper kernel support for it.
First make sure that  CONFIG_PACKET and CONFIG_NET_RADIO (wireless extensions)  are enabled in your kernel. The driver can be built as module (default android way) or built in kernel (if you want to rely in kernel auto probing to support multiple driver eg. USB wifi) but will require source code modifications (see below).
- As kernel module:
   Define in your BoardConfig.mk :
   1. WIFI_DRIVER_MODULE_PATH := path to the module to be loaded
       You need to specify module name in that path too, usually should look something like /system/lib/modules/wlan.ko
   2.  WIFI_DRIVER_MODULE_NAME:= the name of the network interface that the driver creates, for example wlan0
   3. WIFI_DRIVER_MODULE_ARG:= any arguments that you want to pass to the driver on insmod, for example nohwcrypt
  
   Make sure you copy your kernel module when building android to the correct location.
- As built in kernel:
  - First init.rc needs to be modified to inform hardware/libhardware_legacy/wifi/wifi.c about the name of the interface, that the driver  is already loaded and set the status of wpa_supplicant to running:

setprop wifi.interface "wlan0"
setprop wlan.driver.status "ok"


Do NOT add setprop init.svc.wpa_supplicant "running" as I previously mentioned as it will prevent wpa_supplicant from starting from init.
- Secondly hardware/libhardware_legacy/wifi/wifi.c need to be modified so the functions insmod() and rmmod() return 0 (simply add return 0; as the first line in functions since they are not needed when driver is built in kernel) and return before checking for /proc/modules in  check_driver_loaded() function.
You might encounter problems with WifiHW module not being able to connect to wpa_supplicant socket even with the correct permisions. Try to turn off / turn on Wifi from the GUI.

7. Provide a firmware if your driver needs it

If your driver needs a firmware you will have to copy this firmware file to /etc/firmware on your android build. Android doesn't use a standard hotplug binary (although there is an implementation available on android-x86 system/code/toolbox/hotplug.c ) instead the init process takes care of firmware events and loads the firmware file from /etc/firmware (see: system/core/init/devices.c handle_firmware_event() function).
Firmware file name is defined by the driver and might also contain a folder like: RTL8192SU/rtl8192sfw.bin, entire file path should be available in /etc/firmware

8. Make your driver work with Android custom wpa_supplicant commands and SIOCSIWPRIV ioctl.

Android uses SIOCSIWPRIV ioctl to send commands to modify driver behaviour and receive information like signal strength, mac address of the AP, link speed etc. This ioctl is usually not implemented in any known wireless drivers except bcm4329 which is in google msm kernel branch .
The errors from not having this ioctl implemented will look like:
E/wpa_supplicant(  ): wpa_driver_priv_driver_cmd failed wpa_driver_priv_driver_cmd RSSI len = 4096 
E/wpa_supplicant(  ): wpa_driver_priv_driver_cmd failed 
D/wpa_supplicant(  ): wpa_driver_priv_driver_cmd LINKSPEED len = 4096
E/wpa_supplicant(  ): wpa_driver_priv_driver_cmd failed 
I/wpa_supplicant(  ): CTRL-EVENT-DRIVER-STATE HANGED

After 4, WEXT_NUMBER_SEQUENTIAL_ERRORS errors, android will abort using the device.

To quickly test your wifi from interface you can disable error checking in external/wpa_supplicant/driver_wext.c by simply making ret = 0; in wpa_driver_priv_driver_cmd() function after the SIOCSIWPRIV ioctl call. This will make all access points in android UI appear without signal or MAC address.
To proper implement the ioctl you will need to modify your kernel driver to reply to SIOCSIWPRIV ioctl with RSSI (signal strength) and MACADDR commands being the most important.
A better way is to add a custom driver_xxx.c to google external/wpa_supplicant/ implementing wpa_driver_priv_driver_cmd() function that will take care of RSSI, MACADDR and others, through calls to SIOCGIWSTATS, SIOCGIFHWADDR ioctls, with the rest of the functions being called from driver_wext.c.
Below is a link to a patch for wpa_supplicant that I did for mini-box.com picoPC Android build. It creates a new driver awext which "emulates" android driver commands using wireless extensions ioctls.

How to use the new driver:

1. In your BoardConfig.mk define:  BOARD_WPA_SUPPLICANT_DRIVER := AWEXT
2. Change init.rc wpa_supplicant service command line by replacind -Dwext with -Dawext

AWEXT driver patch download:   android_wext_emulation_driver_awext.patch
分享到:
评论

相关推荐

    Porting device drivers to the 2.6 kernel

    ### 设备驱动程序移植到2.6内核 #### 概述 本文档是一份针对设备驱动程序开发者的重要参考资料,其主要内容聚焦于如何将现有的驱动程序迁移到Linux 2.6内核。随着Linux内核版本的不断演进,驱动程序的编写方式也...

    Android Wifi Driver Porting

    标题与描述概述的知识点主要涉及Android系统中Wi-Fi驱动的移植过程,这在移动设备开发领域是一项关键技能,尤其对于那些希望优化或自定义设备Wi-Fi功能的开发者来说至关重要。以下是对这一知识点的深入解析: ### ...

    android_wifi_porting_eng.doc

    ### Android WiFi移植指南 本文档将引导您逐步了解如何在定制版Android系统中添加新的WiFi驱动并确保WiFi功能正常运行。此文档最初是为Android 2.1版本编写的,但其中的概念和技术同样适用于之前的Android版本,...

    Embedded Android - Porting, Extending and Customizing

    综上所述,《嵌入式Android - Porting, Extending and Customizing》这本书不仅深入浅出地讲解了如何将Android系统移植到各种嵌入式设备上,还提供了大量的实践案例和实用技巧,对于希望在这个领域有所作为的专业...

    Embedded Android Porting, Extending, and Customizing 2013最新版

    ### 嵌入式Android移植、扩展与定制 #### 知识点概览 1. **嵌入式Android概述** 2. **移植Android系统至不同硬件平台** 3. **定制化Android系统的方法** 4. **扩展Android功能的技术** 5. **Android构建系统的深入...

    Android Porting Guide

    ### Android Porting Guide详解 #### 一、概览 本文档是关于如何为特定硬件平台进行Android系统移植的指南。Android作为一个开源项目,其灵活性允许开发者根据不同的硬件需求进行定制和优化。本指南由Rockie Cheng...

    porting-android-x86.rar_android_android x86_android 移植

    综上所述,"porting-android-x86.rar"文件提供的"happy-porting-x86-application-to-android.ppt"很可能是一个关于如何将Android应用程序移植到x86平台的教程或演讲稿。这个过程涉及到多方面的技术知识和实践,包括...

    A Guide to Porting C C++ to Rust 等53本

    A Guide to Porting C C++ to Rust.epub Build a Node.js Project from Scratch.epub Build your applications with Webpack.epub Build Your Own Lisp 中文版.epub C 语言进阶.epub Ceph 运维手册.epub Chromium...

    Porting Windows MFC applications to Linux.doc

    开发人员在将Windows环境下的Microsoft Foundation Classes (MFC)应用程序移植到Linux时,会面临一系列技术和策略上的挑战。本文旨在为这些开发者提供一些思路和指导,帮助他们在保持原有MFC应用的基础上,支持Linux...

    Embedded Android Porting,Extending, and Customizing

    标题和描述中提到的《Embedded Android Porting, Extending, and Customizing》是一本与嵌入式Android移植、扩展和定制相关的专业书籍。这本书被视为创建基于Android系统的系统开发者的重要参考资源。由于本书不是由...

    Android-Porting-on-Embedded-Platform.rar_android_android porting

    总的来说,"Android-Porting-on-Embedded-Platform"涵盖了Android在嵌入式平台移植的全貌,包括硬件驱动开发、内核移植、系统编译、UI适配以及测试调试等多个层面的知识。无论是对硬件工程师还是软件开发者,深入...

    Porting Application to Qtopia

    Porting Application to Qtopia

    porting android

    【Porting Android】是指将Android操作系统移植到不同的硬件平台或者设备上,使其能够在这些平台上运行。这个过程涉及到多个层面的技术工作,包括驱动程序开发、编译系统调整、内核适配以及用户空间应用的兼容性处理...

    Porting SICL Application to VISA

    将SICL应用程序移植到VISA的详尽指南 标题:"将SICL应用程序移植到VISA" 描述:"将SICL应用程序移植到VISA" 本文档旨在为那些需要将C或C++编写的SICL(Signal Conditioning Library)程序移植到VISA(Virtual ...

    Android_Porting台湾 移植与研究实行.zip

    《Android移植与研究实行》是针对Android操作系统在台湾地区的移植和优化进行深入探讨的专业资料。这份压缩包包含的PDF文档,很可能是详细讲解了如何将Android系统适应台湾的硬件环境、语言需求以及本地化服务的过程...

    Android Porting On Real Target

    "Android Porting On Real Target" Android 作为一个移动设备软件栈,包括操作系统、中间件和关键应用程序。本文档将详细介绍 Android 架构和将其移植到真实硬件目标的过程。 Android 架构 Android 的核心组件...

Global site tag (gtag.js) - Google Analytics