Building kernel modules - notro/fbtft-spindle GitHub Wiki

Important: From the 2014-06-14 rpi-update release, rpi-source is used.

This page details how to build kernel modules.

Install gcc version 4.7

Use the same version of gcc that was used to build the kernel.

Version used to build the kernel

$ cat /proc/version
Linux version 3.10.32+ (pi@raspi2) (gcc version 4.7.1 20120402 (prerelease) (crosstool-NG 1.15.2) ) #2 PREEMPT Fri Mar 7 01:33:27 CET 2014

Current gcc version

$ gcc --version | grep gcc
gcc (Debian 4.6.3-14+rpi1) 4.6.3

Install 4.7

$ sudo apt-get install gcc-4.7 g++-4.7

$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7

Choose 4.7

$ sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-4.6   60        auto mode
  1            /usr/bin/gcc-4.6   60        manual mode
  2            /usr/bin/gcc-4.7   40        manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/gcc-4.7 to provide /usr/bin/gcc (gcc) in manual mode

Current gcc version

$ gcc --version | grep gcc
gcc (Debian 4.7.2-5+rpi1) 4.7.2

link

Install kernel source

# make sure there's ~700M available
$ df -h
Filesystem              Size  Used Avail Use% Mounted on
rootfs                  3.6G  2.4G 1021M  71% /

# install (this takes a while, depending on internet connection and SD card)
$ sudo /root/.rpi-firmware/extra/install_kernel_source

Build an out of tree module

This module is already installed. It is used here as an example.

$ git clone https://github.com/notro/fbtft_tools
$ cd fbtft_tools/gpio_keys_device
$ make
make -C /lib/modules/3.10.32+/build M=/home/pi/fbtft_tools/gpio_keys_device modules
make[1]: Entering directory `/home/pi/linux-439def4b519a036cd5b12f88f7b90a4a833ffe9a'
  CC [M]  /home/pi/fbtft_tools/gpio_keys_device/gpio_keys_device.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/pi/fbtft_tools/gpio_keys_device/gpio_keys_device.mod.o
  LD [M]  /home/pi/fbtft_tools/gpio_keys_device/gpio_keys_device.ko
make[1]: Leaving directory `/home/pi/linux-439def4b519a036cd5b12f88f7b90a4a833ffe9a'

$ sudo make install
make -C /lib/modules/3.10.32+/build M=/home/pi/fbtft_tools/gpio_keys_device modules_install
make[1]: Entering directory `/home/pi/linux-439def4b519a036cd5b12f88f7b90a4a833ffe9a'
  INSTALL /home/pi/fbtft_tools/gpio_keys_device/gpio_keys_device.ko
  DEPMOD  3.10.32
make[1]: Leaving directory `/home/pi/linux-439def4b519a036cd5b12f88f7b90a4a833ffe9a'

Build an in-tree module

Build a missing module from the Linux source tree.
The module can't depend upon missing modules that has to be builtin (bool config).

As an example we use the ENC28J60 SPI Ethernet driver.
Forum post: ENC28J60 SPI Ethernet

# install prerequisites
$ sudo apt-get install libncurses5-dev

$ cd linux-*

# backup .config
$ cp .config .config.original

# Enable ENC28J60
# Device Drivers -> Network device support -> Ethernet driver support -> Microchip devices -> <M> ENC28J60 support
$ make menuconfig

# show config diff
$ scripts/diffconfig
 ENC28J60 n -> m
+ENC28J60_WRITEVERIFY n

# prepare for build (always after config change)
$ make prepare

# build module
$ make SUBDIRS=drivers/net/ethernet/microchip modules

# test that the driver loads
$ sudo insmod drivers/net/ethernet/microchip/enc28j60.ko
$ lsmod
Module                  Size  Used by
enc28j60               17844  0

$ sudo rmmod enc28j60

# install the new driver
$ sudo install -d -m 755 /lib/modules/$(uname -r)/kernel/drivers/net/ethernet/microchip
$ sudo install -m 644 drivers/net/ethernet/microchip/enc28j60.ko /lib/modules/$(uname -r)/kernel/drivers/net/ethernet/microchip
$ sudo depmod

# load
$ sudo modprobe enc28j60
$ lsmod
Module                  Size  Used by
enc28j60               17844  0

piwik