Compiling modules for the stock Raspberry Pi kernel
I recently picked up a CoCoSDC board and after assembling it, the first thing I did was load it up with games. I quickly discovered though I wasn’t going to be doing any gaming without a joystick. I don’t own a Tandy CoCo compatible joystick, so I did what any normal person would, I figured out how to convert something I already had: an Atari 2600 joystick.
Atari 2600 joysticks are absolutely horrible though, so it didn’t take very long for me to want to go back to my ninepin project and see if I could use a USB joystick instead. The joystick interface on the CoCo isn't digital like an Atari or Commodore 64, it's analog like on the Apple II. A few months back I had purchased some AD5282 digital potentiometers which happen to have an I²C driver already in the kernel. I got them out, wired them up according to the datasheet, and then discovered that for whatever reason the AD525x modules had not been compiled or included in Raspbian.
I ended up spending the next three days trying to get the AD525x kernel modules compiled so that I could use them on the stock kernel that comes with Raspbian. When I was working on the IEC driver for the ninepin project, I had no problem with getting the correct source for the stock kernel and compiling kernel modules against it. Sometime in the last year or so though things changed and the EXACT kernel sources which are used to compile the stock kernel are no longer provided†. And not only that, there’s no official documentation as to how you’re supposed to modify the linux kernel sources to build an identical version.
For some reason Raspbian doesn’t provide the kernel source as a package. There’s also no package which includes just the headers for the running kernel. To compile modules the only thing really needed is the matching headers and the Module.symvers file, but since they are not provided, you’re going to have to build the entire Linux kernel just to compile a couple of modules.
Much much searching and I found this post on the Raspberry Pi forums which describes where and how to get the kernel. It gets you close, but not all the way there. More searching and it turns out that there’s a change to the Makefile you have to make, which for some reason isn’t part of the git repository. You need to change the line EXTRAVERSION to include the + sign. I also found that someone has put together a script called rpi-source which will take care of getting the correct kernel sources based on your running kernel as well as make the necessary adjustment to the Makefile.
After you’ve got the sources though, you still need to compile. The rpi-source script assumes you will want to compile directly on the Raspberry Pi. You really don’t want to do that though since a compile will take over 12 hours‡.
There’s a set a of pre-built tools for doing cross compiling, but for whatever reason, they aren’t statically linked. That means unless you’re using the exact right distro, the tools aren’t going to run because they are going to need libraries you don’t have. Instead of spending another several days trying to get the tools built, I used the same method to install a raspbian distro on my quad core x86 computer that I used back when I was trying to get MAME to compile.
With the matching version of the Linux kernel sources and the EXTRAVERSION tweak in the Makefile, the modules finally loaded on the Raspberry Pi without giving me "Error: could not insert module: Invalid module format."
† Honestly, this seems almost like a violation of the GPL.
‡ When I first started using Linux I was using a 386 and it took a mere 8 hours to compile the kernel!