Linux device driver development environment build

  
 

After two weeks of exploration, I finally got a preliminary understanding of Linux device driver development. The following is a summary of the Linux device driver development environment to facilitate future inquiries, but also to the same beginners. A little help.

When you first get into the Linux device driver, beginners often don't understand how to compile the driver, let alone compile into the kernel or load the test. Generally, I find the simplest helloworld driver on the Internet, and then compile it in strict accordance with the steps mentioned on the Internet. The result is a lot of errors that I have never seen before, let alone solve the problem based on the error message. People don't know how to go down when they get here. I have been stuck here for a long time ten days ago. Now I know that I have written it down, and it is helpful to some of my fellow practitioners.

A basic Linux device driver development environment consists of a host machine and a target machine. The host machine is used to drive the development work. The target machine is used to run and test the device driver host. Need to have development tools (gcc, gdb, make, etc.) and linux source code (version should correspond to the Linux kernel on the target machine), and the target machine can run Linux. Because the steps are different, the following is divided into two common situations: general Linux device driver development and embedded Linux device driver development to describe the environment construction and driver compilation:

(1) Ordinary Linux device driver development

Ordinary Linux is mainly distinguished from embedded Linux (generally referred to as uClinux). In this development, the host and the target machine can be a host, that is, the compiler is developed on the local machine and then loaded on the local machine. Running (Linux device driver can also be compiled directly into the kernel, but for the convenience of development work, generally adopt dynamic loading method), of course, it can also be two hosts. If it is two hosts, it is necessary to ensure the Linux source code on the host. The version number is the same as the linux kernel version in the target machine. The general Linux device driver development steps are as follows:

Install the development tools on the host machine and download the Linux source code (requires the version number and the Linux kernel version on the target machine). The development tools mainly include gcc, gdb, make, etc. These tools are installed by default in redhat or fc. In debian or Ubuntu, you can install it by the following command: apt-get install build-essentiallinux source code can be obtained through the following ways: : Go directly to www.kernel.org to download the source code through the package management tool. In debian and Ubuntu, you can download it via the following command, apt-get install linux-source- (version number), and the downloaded file is in /usr/src In the directory, extract to the directory.

After extracting the source code into the /usr/src/directory, go to the linux-source- (version number) directory and execute the following commands: make oldconfigmake preparemake scripts

Write a Linux driver, taking the simplest hello.c as an example. The contents of hello.c are as follows: #include "linux/init.h"#include "linux/module.h"

static int hello_init(void){printk(KERN_ALERT "Hello World linux_driver_module\ ");return 0;}

static void hello_exit(void){printk(KERN_ALERT "Goodbey linux_driver_module\ " );}

m Odule_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("lpj");

Write the Makefile, an example is as follows, the parameters are changed according to the actual situation: # Sample driver moduleobj-m := hello.oKDIR = /usr/src/linux-source-2.6.24/

all:$(MAKE) -C $(KDIR) M=$(PWD)

.PHONY:cleanclean:rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions

Compile, execute make in the directory where hello.c and Makefile are located Then, compile and generate hello.ko file in the current directory to load and test: load using insmod or modprobe command to achieve, such as executing the following code in the current path: insmod hello.ko or modprobe hello Note, if the kernel is loaded in the virtual terminal The kernel print information will not be visible, because the kernel print information will not be output to the virtual terminal, but will be output to the /proc/kmsg file, so the kernel information can be viewed in the following way: cat /proc/kmsg will always print, need Ctrl-C to manually terminate dmesg or dmesg

Copyright © Windows knowledge All Rights Reserved