linux Driver Development Notes

Linux driver development has a specific mode. In my opinion, linux driver only does one thing,
Unified conversion of devices into unified class 3 virtual devices
namely:
1. Character device
2. Block equipment
3. Network equipment

linux driver development needs to be implemented in Ubuntu system

(it is usually developed under Ubuntu and then compiled. Of course, it can also be cross compiled under windows, but it is generally developed under Ubuntu in order to avoid rampant incidents.)

Environmental preparation
When developing, you need to download the linux SDK first
At present, the compressed package is 15GB, and 11gb is required for decompression
Why do I need the Linux SDK? Because it contains all kinds of header files needed to call Linux Written in. c + + I can do linux driver development in c + +. Although I can't do c + +, I can only do c, but after reading the driver development, I learned c + + The video tutorial I watch is the video provided by punctual atom For novices, it's best to learn c first and use c language struct and pointer Then look at linux driver development, and your c + + skills will be improved Otherwise it may break you down

Development steps
File form of linux Driver
1. linux drivers can be compiled into the kernel, that is, zimage files,
2. It can also be compiled separately ko file. When testing, you only need to load ko file

Drive loading
After the driver is compiled, the file extension is ko, there are two commands to load modules, insmod and modprobe

insmod led.ko 

perhaps

modprobe led

The difference between the two methods
Similar to insmod, modprobe is used to dynamically load driver modules. The difference is that modprobe can solve the dependency when loading modules through / lib / modules / #uname - R / modules Dep (. BB) file to find the of dependencies; Insmod cannot solve the dependency problem.

In other words, if you are sure that the driver module you want to load does not depend on other driver modules, you can either insmod or modprobe. Of course, insmod can be executed in any directory, which is more convenient. If the driver module you want to load also depends on other ko driver modules, you can only copy the module to the above specific directory, depmod and then modprobe.

Before loading the model, perform the following steps:

depmod

depmod will generate modules in the / lib/modules/#uname -r# / directory Dep and modules Dep.bb file, indicating the dependency of the module

Then execute the command to load the LED Ko drive

modprobe led

lsmod is used to query which drivers are available

lsmod

Unload driver

modprobe -r led.ko

perhaps

rmmod led.ko

The above is the preparatory work. Now let's officially start writing the driver
After one day's test, it is found that the Firefly-RK3399pro development board is different from that of punctual atom
When encountering various problems, these problems will not be listed one by one, and the correct code and steps will be directly

The first step is to download the Linux SDK. I downloaded the Linux SDK provided by Firefly
Preparation of development environment, refer to Firefly compiles Ubuntu firmware (GPT)

The official documents are not clear
I'll revise it here
Build SDK compilation environment

sudo apt-get update

sudo apt-get install repo git-core gitk git-gui gcc-arm-linux-gnueabihf u-boot-tools device-tree-compiler \
gcc-aarch64-linux-gnu mtools parted libudev-dev libusb-1.0-0-dev python-linaro-image-tools \
linaro-image-tools gcc-arm-linux-gnueabihf libssl-dev liblz4-tool genext2fs lib32stdc++6 \
gcc-aarch64-linux-gnu g+conf autotools-dev libsigsegv2 m4 intltool libdrm-dev curl sed make \
binutils build-essential gcc g++ bash patch gzip bzip2 perl tar cpio python unzip rsync file bc wget \
libncurses5 libqt4-dev libglib2.0-dev libgtk2.0-dev libglade2-dev cvs git mercurial rsync openssh-client \
subversion asciidoc w3m dblatex graphviz python-matplotlib libssl-dev texinfo fakeroot \
libparse-yapp-perl default-jre patchutils swig chrpath diffstat gawk time expect-dev

I also encountered the need to install openssl and another compression tool lz4. It is recommended to directly execute the following two commands

sudo apt-get install libssl-dev
sudo apt-get install liblz4-tool

Note: ubuntu17 04 or higher systems also need the following dependent packages:

sudo apt-get install lib32gcc-7-dev g++-7 libstdc++-7-dev

Download Firefly_Linux_SDK sub volume compressed package
Because firefly_ Linux_ The SDK source package is relatively large. Some users' computers do not support files above 4G or a single file, and the network transmission is slow, so we use the method of volume compression to package the SDK. Users can get firefly in the following ways_ Linux_ SDK source package: Firefly_Linux_SDK source package

After downloading, first verify the MD5 Code:

$ md5sum rk3399pro_linux_release_v2.5.1_20210304_split_dir/*firefly_split*
The results should be as follows
743e23a6119eb83b1f877db815c5031c  rk3399pro_linux_release_v2.5.1_20210304_firefly_split.file0
3bb8b245377e659b225019dfcedce7a9  rk3399pro_linux_release_v2.5.1_20210304_firefly_split.file1
88c368a0b62058dbe500cc626bd8b01f  rk3399pro_linux_release_v2.5.1_20210304_firefly_split.file2
3b7b379da70e2fee785117e1ae2610bf  rk3399pro_linux_release_v2.5.1_20210304_firefly_split.file3
eaccf65d80c8f70ad781d2b876361e34  rk3399pro_linux_release_v2.5.1_20210304_firefly_split.file4
81eaafd1e511e29474fe4445e78d2862  rk3399pro_linux_release_v2.5.1_20210304_firefly_split.file5

Unzip Firefly_Linux_SDK sub volume compressed package
md5 can be decompressed after verification

cat rk3399pro_linux_release_v2.5.1_20210304_split_dir/*firefly_split* | tar -xzv

# This SDK folder contains a repo directory. After decompression, perform the following operations in the current directory
cd rk3399pro_linux_release_v2.5.1_20210304
ls -al

.repo/repo/repo sync -l
.repo/repo/repo sync -c --no-tags
.repo/repo/repo start firefly --all

Update Firefly_Linux_SDK

You can update the SDK later with the following command

.repo/repo/repo sync -c --no-tags

Linux_SDK directory introduction

├── linux_sdk
│   ├── app
│   ├── buildroot buildroot                                      Compilation directory of root file system
│   ├── build.sh -> device/rockchip/common/build.sh              Fully automatic Script Compilation
│   ├── device                                                   Compile related configuration files
│   ├── distro debian                                            Root file system build directory
│   ├── docs                                                     file
│   ├── envsetup.sh -> buildroot/build/envsetup.sh
│   ├── external
│   ├── kernel                                                   kernel
│   ├── Makefile -> buildroot/build/Makefile
│   ├── mkfirmware.sh -> device/rockchip/common/mkfirmware.sh    rockdev Link update script
│   ├── prebuilts
│   ├── rkbin
│   ├── rkflash.sh -> device/rockchip/common/rkflash.sh          Burn script
│   ├── rootfs                                                   debian Root file system compilation directory
│   ├── tools                                                    Burning and packing tools
│   └── u-boot

So far, the environment is basically ready There are some messy instructions in the official documents, which can compile kernel, u-boot, recovery and rootfs respectively Friends in need can go to the official website But this is not the focus of this article What we want is how to write the driver Here's how to start writing your own driver

What I do is make a ko file, then copy the ko file to the board, and then load the driver on the board Such a routine is relatively simple You don't need to rewrite it all I'm afraid that burning will spoil what I've written After all, a lot of code is on it

The following is mainly for reference Firefly-RK3399 first driver programming This article

Officially start writing driver

Create your own folder and put it in your own project directory instead of Linux SDK directory
Write the main driver led c

#include <linux/init.h>
#include <linux/module.h>



static int  __init led_init(void){
    printk("led_init ");
    return 0; 
}

static void  __exit  led_exit(void){

    printk("led_exit ");
    return  ;

}


module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");

Write Makefile
Note that the biggest problem in the makefile here may be that KERNELDIR must be specified in the kernel directory Just do it

 #!/bin/bash

# CROSS_COMPILE:= aarch64-linux-gnu-
# ARCH:= arm64
# CC:= $(CROSS_COMPILE)gcc
# LD:= $(CROSS_COMPILE)ld
PWD :=$(shell pwd)

obj-m += led.o 


KERNELDIR :=/home/roota/Desktop/AI/LinuxSDK/rk3399pro_linux_release_v2.5.1_20210304/kernel


all: 
	make -C $(KERNELDIR) M=$(PWD) modules
	rm -f *.o
	rm -f *.symvers
	rm -f *.order
	rm -f *.mod.c
	rm -f .led.ko.cmd
	rm -f .led.mod.o.cmd
	rm -f .led.o.cmd

clean:
# $(MAKE) -C $(KERNELDIR) M=$(CURRENT_DIR) clean
	rm -f *.o
	rm -f *.symvers
	rm -f *.order
	rm -f *.ko
	rm -f *.mod.c
	rm -f .led.ko.cmd
	rm -f .led.mod.o.cmd
	rm -f .led.o.cmd

# --------
# Copyright notice: This article is the original article of CSDN blogger "Baxi Wang", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
# Original link: https://blog.csdn.net/zwwang2014/article/details/88394210

compile

make 

The compiler results will appear led ko

implement
Turn the LED Copy the KO file to the development board
Loading module

sudo insmod led.ko 

View module

cat /proc/modules 
 or
 lsmod

Uninstall module

sudo rmmod led

In addition, firefly has promoted a relatively good video tutorial
https://dev.t-firefly.com/thread-100207-1-1.html

Keywords: Linux Operation & Maintenance

Added by binumathew on Wed, 23 Feb 2022 03:32:16 +0200