How to Transplant Driver Into iMX8MP

When clients use Forlinx iMX8MP development board, it may need to transplant driver. This article will take a hello driver as an example to demonstrate the process of transplanting driver.

OKMX8MP-C Development Board

Enter drivers directory of source code and create a directory named hello:

forlinx@ubuntu:~$ cd /home/forlinx/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$mkdir hello

Enter hello directory and create hello.c:

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ cd hello

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi hello.c

Write the following content in hello.c:

#include

#include

static int hello_init(void)

{

printk(KERN_ALERT "Hello world\n");

return 0;

}

static void hello_exit(void)

{

printk(KERN_ALERT "Goodbye world\n");

}

module_init(hello_init);

module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");

Program meaning: Print Hello world when insmod driver is mounted, print Goodbye world when rmmod driver is uninstalled

Create Kconfig and Makefile under this folder.

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Kconfig

Write the following content in Kconfig file:

config HAVE_HELLO

tristate "hello driver"

help

This hello driver is just to show how to develop driver process.

This driver can also be built as a module. If so, the module will be called.

default y

#endmenu

Meaning: if CONFIG_HAVE_HELLO is enabled, hellodrivers menu will be displayed in kernel tailoring configuration file, and compiled into kernel by default:

y: compiled into kernel

m: Compile to a module .ko file

n: not compiling and not enabled.

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ vi Kconfig

Write the following content in Makefile file:

obj-$(CONFIG_HAVE_HELLO) += hello.o

Notice:

The name of macro definition should be the same as in Kconfig. Add the name of file to be compiled later, because kernel will automatically add prefix CONFIG, so we also add CONFIG_ in front of the name here, indicating that when CONFIG_HAVE_HELLO is enabled, the file specified by compilation rule is hello.c.

Give the three added file permissions:

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 hello.c

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 Kconfig

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ chmod 777 Makefile

Edit Kconfig and Makefile files at the top level of drivers.

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/hello$ cd ..

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ vi Kconfig

Write the following content in Kconfig file:

source "drivers/counter/Kconfig"

source "drivers/mxc/Kconfig"

source "drivers/hello/Kconfig" //Add the configuration file analysis of hello folder before endmenu

endmenu

Thus, configuration system will parse Kconfig under hello folder according to this configuration.

Edit Makefile:

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ vi Makefile

Write the following content in Makefile file:

obj-$(CONFIG_COUNTER)                     += counter/

obj-y                                                         += mxc/

obj-$(CONFIG_HAVE_HELLO)               += hello/         // Add this line at the end of Makefile

This line of code is where to find the source file when CONFIG_HAVE_HELLO is enabled. Combined with the module Makefile under hello file, a hierarchical Makefile is formed. Be careful not to miss /, add the name of custom folder here, which means compiling this folder into kernel.

Start compiling:

forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel/drivers$ cd ../..
forlinx@ubuntu:~/work/OK8MP-linux-sdk$ . /opt/fsl-imx-xwayland/5.4-zeus/environment-setup-aarch64-poky-linux
forlinx@ubuntu:~/work/OK8MP-linux-sdk$ . environment-setup-aarch64-poky-linux
forlinx@ubuntu:~/work/OK8MP-linux-sdk$ cd OK8MP-linux-kernel
forlinx@ubuntu:~/work/OK8MP-linux-sdk/OK8MP-linux-kernel$ make modules
scripts/kconfig/conf --syncconfig Kconfig
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
drivers/hello/Kconfig:7:warning: ignoring unsupported character '�'
*
* Restart config...
*
*
* Device Drivers
*
Trust the bootloader to initialize Linux's CRNG (RANDOM_TRUST_BOOTLOADER) [N/y/?] n
Platform support for Chrome hardware (transitional) (MFD_CROS_EC) [Y/n/m/?] y
Trusted Execution Environment support (TEE) [Y/n/m/?] y
hello driver (HAVE_HELLO) [Y/n/m/?] (NEW) m //Compile hello driver into kernel and configure it as m
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
CHK include/generated/compile.h
GZIP kernel/config_data.gz

After the compilation is complete, you can see the compiled driver in OK8MP-linux-kernel/drivers/hello:

Copy hello.ko to development board by USB disk or TF card for verification:

root@OK8MP:~# cd /run/media/sda1/ //Enter the path of USB disk
root@OK8MP:/run/media/sda1# insmod hello.ko //mount hello.ko [ 138.679964] Hello world //Mount driver print information
root@OK8MP:/run/media/sda1# rmmod hello.ko //uninstall hello.ko
[142.022115] Goodbye world // Uninstall driver print information
root@OK8MP:/run/media/sda1#

It can be seen from above test that hello.ko driver can run normally.

This is the process of writing and adding a driver that Forlinx demonstrates for you. If you want to transplant a module, you can ask module manufacturer for a ready-made driver.c file, and then configure Makefile and Kconfig according to above steps.