Write basic driver for BPI R64 ubuntu 18.04 kernel 5.4.0

i try to write basic driver for BPI-R64 in ubuntu 18.04 kernel 5.4.0: my hello-world-kernel.c : " #include<linux/kernel.h>

#include<linux/init.h>

#include<linux/module.h>

static int hello_init(void)

{

printk(KERN_ALERT "Start successful \n");

return 0;

}

static void hello_exit(void)

{

printk(KERN_ALERT "End successful \n");

}

module_init(hello_init);

module_exit(hello_exit); "

my makefile: " obj-m += hello_kernel_module.o

all:

  make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:

  make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

"

when i run command “make”, it prints “/lib/modules/5.4.0-BPI-R64-Kernel/build No such file or dictionary . Stop” what a surprise, when i do “cd /lib/modules/5.4.0-BPI-R64-Kernel”. it exist “build” dictionary ? what happend ?

[second problem]

i replace “/lib/modules/$(shell uname -r)/build” by “/usr/src/BPI-R64-bsp-5.4_built/linux-mt” in Makefile. it work. i insmod file .ko . it still work. but when i rmmod, console prints modules used.if i used modprobe, console prints no modules found, how to uninstall module ?

It’s the answer to your firs question - /lib/modules/<version>/build - a symlink that should point to sources, you may just replace this symlink with correct one (/usr/src/BPI-R64-bsp-5.4_built/linux-mt).

to use modprobe you should to install this module to /lib/modules. Usually make modules_install. IDK why you cant unload exactly your module, but you can alway use rmmod -f to force.

tks bro. i tried rmmod -f but console prints “function not implemented” . i search gg and resulte is : set CONFIG_MODULE_UNLOAD in kernel . How to set CONFIG_MODULE_UNLOAD = yes in kernel ?

how to install modules to /lib/modules ? copy file .ko to /lib/modules and insmod ?

  • using make menuconfig (./build.sh config in Frank’s baranch, i don’t have R64, so idk if any scripts are exists in official BSP)

  • manually edit .config in your src dir (/usr/src/BPI-R64-bsp-5.4_built/linux-mt/.config should work for you)

simple way - copy it to /lib/modules/<ver>/extra (extra is common dir for 3rd party modules, but as i know you can use any). then run depmod and you can use modprobe.

also you can implement the same in your Makefile

I might miss some details, also it’s a general kernel development quietion so you may refer to kernel docs :wink:

Afair build.sh in official repo does not accept params and replace .config via cp with defconfig.

But after starting build.sh there is a menu where you can select change kernel config or similar…then search for the option alex mentioned by pressing / and press 1 to jump to it. Then select it (*) and exit with saving config

Btw do you compile kernel on r64 directly or on host? I guess build-script is only intended for crosscompile (not native on r64)

i do : bpi-copy file .img to SD card

Have you set the option and do you see your module as .ko file?

Kernel is no img file…it is uImage dtb and modules (ko files +some other files)

your mean is : Module.symvers, modules.builtin,modules.builtin.modinfo,modules.order,System.map,vmlinux ,vmlinux.o ?

i run ./build.sh and press 4 for modified, save and load. then i still run ./build.sh and press 3 for Build kernel only. after 15 minutes, it prints Build success! . i check current folder, i dont see new file . i check folder linux-mt and see a list : modules.builtin,modules.builtin.modinfo,modules.order,System.map,vmlinux ,vmlinux.o .

Idk how the official kernel was installed. But you can try my debian-image with my kernel-repo (module_remove already active). My build.sh asks for installing to sdcard after build…only need to adjust uenv.txt after install

ok, tks u. i have 1 question. after i choose 1 (run ./build.sh), i have folder SD. how to create file .img(i use bpi-copy tools to SDCard) or how write to SDCard?

Build.sh from my kernel-repo? 1 is pack there…you have to use 2=install

from source : https://github.com/BPI-SINOVOIP/BPI-R64-bsp-5.4

what do u mean ?

Ok,you still use official kernel-repo…there i do not know how to install

I guess it’s legacy boot,so you need uImage and dtb on BPI-BOOT partition and your modules (with additional files) in BPI-ROOT/lib/modules/kernelname/

Afair there was a uenv.txt too if filename of uImage or dtb is different

tks u so much . have good a day bro

Seems like option 1 creates tgz files which needs to be unpacked to sdcard (like my option 1,but here multiple)

Comment states that you can install it via bpi-tools

Afaik you need to unpack

  • $SD/BPI-BOOT-${board}-${service}.tgz to BPI-BOOT
  • $SD/${kernel}.tgz to BPI-ROOT

btw. tried your code and compiled in my tree…at least “MODULE_LICENSE(“GPL”);” was missing

diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index bd1c444e1170..8640523fcb13 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -57,3 +57,5 @@ obj-$(CONFIG_UACCE)           += uacce/
 obj-$(CONFIG_XILINX_SDFEC)     += xilinx_sdfec.o
 obj-$(CONFIG_HISI_HIKEY_USB)   += hisi_hikey_usb.o
 obj-$(CONFIG_MTK_COMBO)        += mediatek/
+
+obj-m+= mydriver.o
diff --git a/drivers/misc/mydriver.c b/drivers/misc/mydriver.c
new file mode 100644
index 000000000000..31e98f8eebd6
--- /dev/null
+++ b/drivers/misc/mydriver.c
@@ -0,0 +1,19 @@
+#include<linux/kernel.h>
+#include<linux/init.h>
+#include<linux/module.h>
+
+static int hello_init(void)
+{
+       printk(KERN_ALERT "Start successful \n");
+       return 0;
+}
+
+static void hello_exit(void)
+{
+       printk(KERN_ALERT "End successful \n");
+}
+
+module_init(hello_init);
+module_exit(hello_exit);
+
+MODULE_LICENSE("GPL");

i used makefile from drivers/misc to place my module there.

https://tldp.org/LDP/lkmpg/2.6/html/x279.html shows __init and __exit statements…imho they are for reducing memory consumption.btw. there is a nice tutorial here

tried to load it:

root@bpi-r2:~# modprobe mydriver                                                                                                     
[  112.868991] Start successful                                                                                                      
root@bpi-r2:~# modprobe -r mydriver                                                                                                  
[  116.607067] End successful                                                                                                        
root@bpi-r2:~#