How to build an Android kernel with USB WiFi adapter support – Part 2

droid_kernel

Note:- This is multipart guide and it is incomplete. I will not be responsible for what happens to your device or computer.

Getting the kernel source

It is very important to find the right kernel for your ROM. Usually they differ by kernel version , module configuration  or driver updates. You can find different kernels with tons of mods and optimizations in XDA Forums. Lot of them come with link to github repository. If you are looking kernel for stock ROMS you will have to go to device manufacturer developer portal and find the right kernel for your device. For Samsung devices you can head to opensource.samsung.com. I should tell you it is a very very slow site, have some patience. One more thing regarding stock kernels usually it will be bundled with bugs and if you are very serious stock kernels you will have to check other kernels and pick the patch and apply to stock kernel. please read README file bundled with our download. it will have information that will save your time and hair later. For Cyanogen kernels you get it directly from cyanogen github the generic format for kernel is like https://github.com/CyanogenMod/android_kernel_vendorer_device-borad for example for my galaxy note 2 which is based on smdk4412 board the repo is https://github.com/CyanogenMod/android_kernel_samsung_smdk4412 .

Android Bootimage

When you say you want to build kernel for android what you will be doing is creating a android boot image which can be flashed on to a specific partition which will loaded by bootloader when you switch on the device. Android boot image is nothing but a linux kernel and initial ramdisk ( aka initrd ). Linux kernel will initialize the basic OS features and device drivers. then ramdisk take over control. In android initrd will mount the system and other  partitions, loads android specific driver modules if any, setup android system services ( Not application services) then comes Zygote. All these stuff will be in a set of init*.rc script inside initrd.

So you will compile linux kernel with a build system and pack it with initrd to get boot image. some kernel will have the initrd along with it and some dont. If you dont have it the trick is to extract from an existing boot image and then pack it with yours. Once you have kernel you need to build it to make sure everything is working fine. I have seen some kernels  ( usually custom kernels ) comes with build script. These scripts will make your life very easy only thing you have to do is edit some variables to set up paths pointing to cross compiler and initrd path etc. then run the script you will have the kernel image ready in minutes. Some of the build scripts are so cool they even produce recovery flashable zip. In case you dont have build script with your kernel follow the steps to build kernel.

Building the kernel

First thing in manual compiling is identifying the config file for your device. config file contains information which is used at compile time to turn on/off/modularize different kernel features. Read the README or any other notice files with the source to get the name of the config file. Still no luck in finding the right config check out foler $KERNEL_SOURCE/arch/arm/configs folder if you find a file name with a close resemblance to you device name/ code name, that is it. For example in my case for galaxy note 2 (n7100) cyanogenmode kernel source config file name is cyanogenmod_n7100_defconfig  and for stock rom it is t0_04_defconfig. Before starts building you need to create a configuration. for that execute these command from the root of your kernel source.

 

where

  • $CC is the cross compiler path for instance /cyg/system/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-
  • $KERNEL_DIR is the kernel source root full path
  • $K_CONFIG is the name of the config file ( just the name not the full path )

This will configure kernel source with configuration specified in the config file. you can see a new file .config in the root path. To start the building execute these commands from root of the kernel source.

 

if everything is fine you will have your zImage ready in minutes.

Next part will cover how to custom configure kernel and general procedure of packaging with initrd.

  • Pingback: How to build an Android kernel with USB WiFi adapter support – Part 1 | Maxters Inc()

  • flavio

    so,i run this command 2 times ?

    • maxters

      sorry about that last command will not have configuration file as argument. corrected.
      thank you

  • iT

    Ok so you have to compile the kernel before adding and enaabling any modules? I thought you need to make xconfig and enable the compiled drivers before you build the kernel… probably what I was doing wrong. Couldnt get the ath9k modules to compile. I tried everything!! Thanks for taking the time to do this write up. Will really help 🙂

    • maxters

      you are correct . but here we are making sure that kernel is compiling correctly. after this we need to run xconfig and change the config file then again we need to re compile the kernel.

  • flavio

    i think is done …

    • xperia gamer

      hey i am getting “arch/x86/kernel/asm-offsets.s” this error..what should i do to fix it??

  • flavio

    Part 3 please : )

  • flavio

    Could you attach the .config file you’re using?

    • maxters

      hi, sorry for the delay i was little busy as well as i am not having access to my build machine , as soon as get back will post it.

      • 007

        Part 3 Please

        • maxters

          I haven’t started part 3. as of now i haven’t planned for it. please stop asking for it on all the posts. I am sorry..

          • 007

            Please started soon that is my humble request

          • 007

            Whan You Will Upload Part 3 Please

  • rips

    When you reckon you will finish this “how to”? Trying to compile stock kernel with support for AR9271 ath9k_htc but cant find in config? any tips?
    cheers

  • nannaniel

    It’s been almost a month since your last post !!!!! Please can you provide the last part ! Btw i realy love your site and a big thank you for your time !!! 😉

    • iT

      I’ve been eagerly waiting for it as well. From what I understand is if you can successfully compile, from the root of your source tree you can run menuconfig to alter the wireless driver modules, then from the root of the source tree compile your wireless drivers, then compile the kernel, take the .ko for the wireless modules and insmod them after flashing your kernel (or you can create a flashable zip that will put your .ko in the proper place, as well as a start/stop script so you can easily run it).

  • Claude

    Is this project still alive. Im vergangenen interrested in compiling my own usb wifi enabled Kernel

    • Jean

      im still interested, my plan is enabling monitor mode for the Note 10.1 N-8000 with kali

  • flavio

    Are you alive?
    joke…

  • iT

    Hoping for part 3 to be up here… I started on an S3 (d2vzw) then moved on to a S4(vzw) and now a Note 3. Would be worth rooting my phone if I can get the finishing touches on this guide… I can almost taste it. I hope to hear from you so I can start adding Wireless drivers to samsung devices (S3, S4, Note 3)…

  • m00nl33n

    I added usb adapter device support to kernel successfully but then my wifi stopped working here is the dmesg of turning wifi on

    [CODE][ 2273.866333] wlan: disagrees about version of symbol cfg80211_ready_on_channel

    [ 2273.866455] wlan: Unknown symbol cfg80211_ready_on_channel (err -22)

    [ 2273.866729] wlan: disagrees about version of symbol __ieee80211_get_channel

    [ 2273.866821] wlan: Unknown symbol __ieee80211_get_channel (err -22)

    [ 2273.867065] wlan: disagrees about version of symbol cfg80211_cqm_rssi_notify

    [ 2273.867187] wlan: Unknown symbol cfg80211_cqm_rssi_notify (err -22)

    [ 2273.867370] wlan: disagrees about version of symbol cfg80211_roamed

    [ 2273.867523] wlan: Unknown symbol cfg80211_roamed (err -22)

    [ 2273.867614] wlan: disagrees about version of symbol cfg80211_pmksa_candidate_notify

    [ 2273.867736] wlan: Unknown symbol cfg80211_pmksa_candidate_notify (err -22)

    [ 2273.867858] wlan: disagrees about version of symbol wiphy_register

    [ 2273.868011] wlan: Unknown symbol wiphy_register (err -22)

    [ 2273.868133] wlan: disagrees about version of symbol cfg80211_disconnected

    [ 2273.868286] wlan: Unknown symbol cfg80211_disconnected (err -22)

    [ 2273.868499] wlan: disagrees about version of symbol cfg80211_new_sta

    [ 2273.868591] wlan: Unknown symbol cfg80211_new_sta (err -22)

    [ 2273.868774] wlan: disagrees about version of symbol cfg80211_tdls_oper_request

    [ 2273.868865] wlan: Unknown symbol cfg80211_tdls_oper_request (err -22)

    [ 2273.869018] wlan: disagrees about version of symbol cfg80211_connect_result

    [ 2273.869171] wlan: Unknown symbol cfg80211_connect_result (err -22)

    [ 2273.869262] wlan: disagrees about version of symbol cfg80211_inform_bss_frame

    [ 2273.869415] wlan: Unknown symbol cfg80211_inform_bss_frame (err -22)

    [ 2273.869506] wlan: disagrees about version of symbol wiphy_new

    [ 2273.869659] wlan: Unknown symbol wiphy_new (err -22)

    [ 2273.869781] wlan: disagrees about version of symbol cfg80211_rx_mgmt

    [ 2273.869903] wlan: Unknown symbol cfg80211_rx_mgmt (err -22)

    [ 2273.870117] wlan: disagrees about version of symbol cfg80211_send_unprot_deauth

    [ 2273.870239] wlan: Unknown symbol cfg80211_send_unprot_deauth (err -22)

    [ 2273.870361] wlan: disagrees about version of symbol cfg80211_mgmt_tx_status

    [ 2273.870513] wlan: Unknown symbol cfg80211_mgmt_tx_status (err -22)

    [ 2273.870727] wlan: disagrees about version of symbol cfg80211_inform_bss

    [ 2273.870819] wlan: Unknown symbol cfg80211_inform_bss (err -22)

    [ 2273.871002] wlan: disagrees about version of symbol wireless_send_event

    [ 2273.871093] wlan: Unknown symbol wireless_send_event (err -22)

    [ 2273.871246] wlan: disagrees about version of symbol wiphy_free

    [ 2273.871337] wlan: Unknown symbol wiphy_free (err -22)

    [ 2273.871520] wlan: disagrees about version of symbol cfg80211_scan_done

    [ 2273.871612] wlan: Unknown symbol cfg80211_scan_done (err -22)

    [ 2273.871795] wlan: disagrees about version of symbol regulatory_hint

    [ 2273.871856] wlan: Unknown symbol regulatory_hint (err -22)

    [ 2273.872039] wlan: disagrees about version of symbol cfg80211_get_bss

    [ 2273.872100] wlan: Unknown symbol cfg80211_get_bss (err -22)

    [ 2273.872283] wlan: disagrees about version of symbol cfg80211_michael_mic_failure

    [ 2273.872436] wlan: Unknown symbol cfg80211_michael_mic_failure (err -22)

    [ 2273.872528] wlan: disagrees about version of symbol cfg80211_ibss_joined

    [ 2273.872680] wlan: Unknown symbol cfg80211_ibss_joined (err -22)

    [ 2273.872833] wlan: disagrees about version of symbol cfg80211_del_sta

    [ 2273.872985] wlan: Unknown symbol cfg80211_del_sta (err -22)

    [ 2273.880584] wlan: disagrees about version of symbol cfg80211_remain_on_channel_expired

    [ 2273.880737] wlan: Unknown symbol cfg80211_remain_on_channel_expired (err -22)

    [ 2273.880889] wlan: disagrees about version of symbol wiphy_unregister

    [ 2273.880950] wlan: Unknown symbol wiphy_unregister (err -22)

    [ 2277.103332] SLIM_CL: skip reconfig sequence

    [ 2336.262878] SLIM_CL: skip reconfig sequence

    [ 2341.206115] SLIM_CL: skip reconfig sequence

    [ 2372.867187] init: untracked pid 7791 exited

    [ 2375.211242] SLIM_CL: skip reconfig sequence

    [ 2378.713043] SLIM_CL: skip reconfig sequence

    [ 2385.924896] check_recover_vbus_collapse: VBUS input current still limiting to 700 mA. Retry set

    [ 2397.413330] SLIM_CL: skip reconfig sequence

    [ 2464.730682] set_usb_max_current: setting current max to 1500

    [ 2544.752166] set_usb_max_current: setting current max to 1500

    [ 2604.769744] set_usb_max_current: setting current max to 1500

    [ 2674.810363] set_usb_max_current: setting current max to 1500

    [ 2686.035491] SLIM_CL: skip reconfig sequence

    [ 2690.290008] qup_i2c qup_i2c.0: QUP: I2C status flags :0x1300c8, irq:226

    [ 2690.290191] qup_i2c qup_i2c.0: I2C slave addr:0x28 not connected

    [ 2690.300445] pn544 0-0028: pn544_dev_write: i2c write err -107, but retry 1

    [ 2691.778778] qup_i2c qup_i2c.0: QUP: I2C status flags :0x1343c8, irq:226

    [ 2691.778961] qup_i2c qup_i2c.0: I2C slave addr:0x28 not connected

    [ 2691.789093] pn544 0-0028: pn544_dev_write: i2c write err -107, but retry 1

    [ 2694.949035] SLIM_CL: skip reconfig sequence

    [ 2714.834625] set_usb_max_current: setting current max to 1500

    [ 2715.883575] SLIM_CL: skip reconfig sequence

    [ 2843.803405] SLIM_CL: skip reconfig sequence

    [ 2854.273345] SLIM_CL: skip reconfig sequence

    [ 2955.025512] msm_otg msm_otg: USB exited from low power mode

    [ 2955.026306] msm_otg msm_otg: b_idle work, inputs=0x00000001

    [ 2955.026580] msm_otg msm_otg: Avail curr from USB = 0

    [ 2955.026885] msm_otg msm_otg: phy_reset: success

    [ 2955.136352] msm_otg msm_otg: USB in low power mode

    [ 2969.677581] msm_otg msm_otg: USB exited from low power mode

    [ 2969.678131] msm_otg msm_otg: b_idle work, inputs=0x00000003

    [ 2969.853424] msm_otg msm_otg: chg_type = USB_SDP_CHARGER

    [ 2969.853576] msm_otg msm_otg: b_idle work, inputs=0x00000003

    [ 2969.859283] msm_hsusb msm_hsusb: vbus online

    [ 2969.859436] msm_hsusb msm_hsusb: CI13XXX_CONTROLLER_RESET_EVENT received

    [ 2969.859558] msm_otg msm_otg: changed to b_peripheral, from b_idle

    [ 2970.140228] msm_hsusb msm_hsusb: reset

    [ 2970.140686] android_work: android_work: did not send uevent (0 0 (null))

    [ 2970.178863] android_work: android_work: sent uevent USB_STATE=CONNECTED

    [ 2970.182403] msm_hsusb msm_hsusb: reset

    [ 2970.182891] android_work: android_work: sent uevent USB_STATE=DISCONNECTED

    [ 2970.261993] android_work: android_work: sent uevent USB_STATE=CONNECTED

    [ 2971.677764] android_usb gadget: high-speed config #1: android_usb

    [ 2971.677917] msm_otg msm_otg: Avail curr from USB = 500

    [ 2971.733306] android_work: android_work: sent uevent USB_STATE=CONFIGURED

    [ 2971.880920] mtp_open

    [ 2974.863128] msm_ta_detect_work: USB exit ta detection – frindex

    [ 3000.568115] init: untracked pid 8878 exited

    [ 3003.624877] init: untracked pid 8948 exited

    [ 3005.894012] SLIM_CL: skip reconfig sequence[/CODE]

    I think problem occurs when I enable “cfg80211 wireless extensions compatibility” but unless enabling it i cannot see the wifi adapter in airmon-ng

    if CONFIG_CFG80211_WEXT=y = inner WLAN wont work

    if CONFIG_CFG80211_WEXT=n = usb wifi adapter wont work with aircrack, reaver

    here are documentations about it but they are too complex for me :

    [url]https://community.freescale.com/docs/DOC-93603[/url]

    [url]http://blog.linuxconsulting.ro/2010/04/porting-wifi-drivers-to-android.html[/url]

  • augusto higuti

    HI,Please help ! which kernel is right for N7100XXALJ2 android 4.1.2?

    • maxters

      I have no idea which one goes with 4.1.2. I never used samsung rom except I had to test my apps. Once i used kernel source from samsung open source to build one kernel and I ended up having kernel with lot of bugs, so used better kernel for stock rom from here https://github.com/AndreiLux/Perseus-S3

  • 007

    Part 3 Please Maxters

  • 007

    Part 3 Please

  • 007

    Whan You Will Start Part 3 Please Start Soon Thanks