[Desire Android Development] Myshkinbob:
Nah i just used the Saga 2.6.35 source from developer.htc.com, you could use the evo 4g source or any armv7 phone's kernel source that uses the same bcm4329 wifi chipset and htc's customised driver for it.

Ok i just checked out the guide at http://www.tuxtopia.nl/2011/05/21/guide-building-android-2-3-4-on-ubuntu-11-04-64-bit/

1. build environment set up

It looks like that's for building an entire ROM, not just the kernel. It's not entirely clear whether that guide includes building the kernel or just the apk's, since it's all obscured with build scripts to handle the details.

So, I'll explain from the point of having gentoo installed, where you next install some build tools. In the guide above that's after the line "Fire up a terminal, and download the following packages to start with:" near the top of the page. Those packages give you a basic build environment for x86. Personally i ran...

sudo apt-get install git-core gnupg flex bison gperf libsdl1.2-dev libesd0-dev libwxgtk2.6-dev squashfs-tools build-essential zip curl libncurses5-dev zlib1g-dev sun-java6-jdk pngcrush

Some of those names might be slightly different, since i use debian, which is really similar to gentoo. Anyhow...

You want to build the modules for an arm-based phone, not an x86 pc, so next up is to download some additional build tools for the arm architecture.

Download http://dl.google.com/android/ndk/android-ndk-r6-linux-x86.tar.bz2, which is from the android ndk page. Extract that somewhere, just remember where. In my case it's in /home/desire-build/Downloads/.

That's it for build environment set-up. Only needs doing once. The following steps need completing each time you make a module for a new kernel.

2a. Download the kernel source

First of all download and extract the kernel source for the kernel being used on the phone. Let's use the netarchy kernel source for examples sake. to get the right source go to https://github.com/netarchy/nexus-s and click the download button. It'll ask you which branch you want to download, and your phone is using 1.3.7-cfs according to the Software information in About Phone, so you'll need to click the view 5 more downloads link at the bottom of the popup and select the 1.3.7-cfs version (as tar.gz).

Once the source is extracted from the .tar.gz or tar.bz it came in, change directory into the kernel source directory, in my case it's:

cd /home/desire-build/Downloads/netarchy-nexus-s-76707fa/

When you're typing paths remember you can use the tab key for autocomplete, it's not only quicker but also guards against typos. :)

2b. Patch the bcm4329 driver

Download the source code patch for the kernel from a few posts back in this thread. Netarchy is an AOSP kernel so you want the aosp patch. Extract that and you'll end up with a .diff file. Put the .diff file in the kernel source directory we just cd'd into. To apply the patch diff to the kernel source run the following command:

patch -p0 < pmfast_2.6.35.13_aosp.diff

It should say it patched two files. That's the patch applied. :)

It will apply cleanly on this kernel, but on future kernel sources it may have difficulty locating the correct lines to change. If this happens you'll see output mentioning rejected patch. Don't worry if that happens, just run this command from the kernel source directory:

grep -n "PM_MAX" drivers/net/wireless/bcm4329/*

The output will tell you which files and lines PM_MAX was found on, and you can manually go in and change PM_MAX into PM_FAST. This won't happen on 2.6.35 kernels though.

2c. Configuring the kernel source to match the phone's kernel

Next up get the config.gz off your phone and inside linux somehow, through email or dropbox or whatever. Extract the config file out of config.gz.

Now copy the config file from the phone's config.gz that you extracted, into the kernel source root dir as '.config' (it must be coped to .config and not just config). Basically this is to configure the kernel source so when it's built it matches your current kernel, ie. uses the same configuration. In my case the exact command is to look two directories higher for it (assuming the terminal current dir is still the kernel source directory):

cp ../../config .config

This next step isn't neccessarily needed, but it's best to be safe. Sometimes there are options in the kernel source that aren't covered by the .config we took from the phone's config.gz. Run the following command:

make ARCH=arm oldconfig

As the name suggests, this configures the kernel source based on the existing .config file. you may get prompted for a y/n/m response if an option isn't selected in the .config, just hit enter for the default and you should be fine.

We're almost ready to build the module but there's one more step. Remember the first time i posted the module for your kernel and it failed? That's because the kernel on the phone won't load modules unless they're made for the exact same kernel. In the dmesg output from the phone this showed up as:

That's the kernel on the phone saying it ain't loading no module that wasn't made for it. If you look closely you'll see the text following "should be" matches what you'll see on the phone if you go to About Phone - Software information. Also worth noting that "preempt mod_unload ARMv7" is describing how the kernel was built, and 2.6.35.13 is the kernel version. The part we care about is inbetween those two pieces of information, the "-netarchy-nexus-1.3.7-cfs-2.3.4+", this is called the localversion string.

So grab your phone and go to the About Phone - Software information and check the localversion string within the kernel information section. Run the following command in the linux terminal:

make ARCH=arm menuconfig

Use the arrow keys and enter to navigate to General setup - Local version. This should be the third item down in the General setup section. In the case of the netarchy kernel source, if you look back to your phone's screen then you'll see the phone's localversion has a + symbol added to the end of it that wasn't present in the config.gz from the phone. I've no idea why the config.gz localversion isn't accurate but it's a common issue. Anyway, add the + to the end of the localversion in the kernel config in the terminal, then hit enter to apply it, then exit configuration and select Yes to save the new kernel configuration.

2d. Build the bcm4329 driver module

Now we're good to build the module, finally! You should still be in the kernel source directory in the terminal when you run this next command (though read below before you run it).

make ARCH=arm CROSS_COMPILE=/home/desire-build/Downloads/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi- modules

You need to pay attention to the "CROSS_COMPILE=" argument in the command particularly, this is the absolute path to the build tools used for compiling for the phone (arm cpu) and not your computer (x86 cpu). The start of the path will be different for you. You'll need to replace "/home/desire-build/Downloads/" with the path to where you extracted the android NDK in part 1 of this guide. It'll probably still begin with /home/ and then your username and then wherever you put the NDK. My advice is heavy use of tab complete to fill out the path for you. The "android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-" part should be identical however.

The module should build pretty quickly, and you'll find it at drivers/net/wireless/bcm4329/bcm4329.ko. One more additional command to reduce the filesize, although not neccessary, will stop it using as much space on the phone:

/home/desire-build/Downloads/android-ndk-r6/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-strip --strip-debug drivers/net/wireless/bcm4329/bcm4329.ko

Again you'll need to adjust the beginning of the path to wherever your extracted the NDK in part 1. All the above command does is strip out some unneeded debug information from the module.

Now you just have to put the bcm4329.ko on your phone. Job's done. :)

Additional note for anyone using this guide to make a module for a HTC kernel - you need to use the htc version of the patch diff, not the AOSP. Also the htc bcm4329 driver is in drivers/net/wireless/bcm4329_204. Just remember the driver path has _204 appended to it.