Thursday, June 15, 2017

Compiling Device Driver for Android (e.g. new wireless driver)

This is a quick tutorial on compiling a device driver / kernel module for Android. For the purpose of the tutorial, I'll be using a wireless driver (mt7601u) and android-x86 as an example. But the steps should work with the standard/arm version as well. Just make sure to use the correct compiler/gnu tool chain.

I had a USB wifi adapter like the one below which was mediatek's mt7601u based.
Googling around I found that the support for this is present from version 4.2+ (Reference: MediaTek MT7601 USB WIFI on the Raspberry Pi ). I also found multiple drivers for the same like, https://github.com/imZack/mt7601 and https://github.com/kuba-moo/mt7601u . Since the kernel that I was using was 4.0.x, the later worked. We'll be using that going forward.

There are two ways to get the kernel headers to compile the driver. The fool proof way is to compile the kernel that we are going to use with Android either just the kernel or the entire Android. Both methods has its own advantages. Another hackish way is to find the kernel headers closest to the kernel that we'd like to compile against. This should work mostly, but then...

What I did was to get the Android-x86 entirely - in my case I wanted to get an entire custom android build. Follow instructions on Android-x86 - Get Source

Once compiled clone the repo https://github.com/kuba-moo/mt7601u. You could try compiling on native linux for testing. Post that, edit the Makefile to force the path:

KDIR = /home/muthu/android-x86/out/target/product/x86/obj/kernel

run (after making sure you are using the correct gnu tool chain if you are cross compiling for arm)
make clean
make

you have the required mt7601u.ko file. Push that to Android (which is rooted), and push also the firmware bin file (e.g. you can get it from https://github.com/imZack/mt7601/blob/master/src/mcu/bin/MT7601.bin) to

/lib/firmware/mt7601u.bin

After which load the drivers (mt7601u depends on mac80211)
modprobe mac80211
insmod mt7601u.ko

Then insert the usb dongle to check if the wifi is working.
ip addr
ifconfig




No comments: