Skip to content

Instantly share code, notes, and snippets.

@Josua-SR
Created June 17, 2020 12:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Josua-SR/3ee497179b75e8e164e508f98b12d810 to your computer and use it in GitHub Desktop.
Save Josua-SR/3ee497179b75e8e164e508f98b12d810 to your computer and use it in GitHub Desktop.
How to build out of tree Kernel Module on Debian

How to build out of tree Kernel Module on Debian

Install Kernel Development files

First get the general build tools for compiling kernel modules: sudo apt install build-essential

Also install the kernel development headers for the currently running kernel:

debian@sr-imx8:~$ uname -a
Linux sr-imx8 4.19.72-imx8-sr #1 SMP PREEMPT Thu Apr 30 03:44:47 UTC 2020 aarch64 GNU/Linux
debian@sr-imx8:~$ apt-cache search imx8-sr
linux-4.19.y-imx8-sr-dev - kernel API development files
linux-headers-4.19.y-imx8-sr - kernel development files
linux-headers-4.19.y-imx8-sr-dbgsym - debug symbols for linux-headers-4.19.y-imx8-sr
linux-image-4.19.y-imx8-sr - LONGNAME kernel
debian@sr-imx8:~$ sudo apt install linux-headers-4.19.y-imx8-sr

Compile the Driver

Unpack sources:

sudo apt install unzip
unzip ~/MMA8652FC-ccaeb5d75dc2.zip
cd MMA8X5X

It appears this driver comes without a Makefile ...

debian@sr-imx8:~/MMA8X5X$ ls
mma8x5x.c  readme.txt

and without a Kbuild file :(

As per https://www.kernel.org/doc/html/latest/kbuild/modules.html the simplified steps are:

  1. Create simple Kbuild file:

    obj-m := mma8x5x.o
    
  2. invoke system-wide kernel Makefile

    debian@sr-imx8:~/MMA8X5X$ uname -r
    4.19.72-imx8-sr
    make -C /lib/modules/4.19.72-imx8-sr/source M=$PWD
    
  3. have fun fixing all the build errors 😅

  4. load the final module

    insmod mma8x5x.ko
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment