Linux Driver Development Overview

Source: Internet
Author: User

Original source: http://www.cnblogs.com/jacklu/p/4722563.html

Linux Device classification

The driver for the device also needs to perform some hardware operations like a bare-metal program, but the drivers need to be "fused into the kernel", so it is necessary to include the operating system-specified interfaces in the driver, which are device-independent. While the operating system brings "hassle" to driver designers, it brings "convenience" to application designers.

The devices under Linux are divided into three categories: character devices, block devices, and network devices .

A character device is a device that must be accessed in serial order, such as a touch screen; a block device is a device that can be accessed in any order, that is, a unit of action, such as a keyboard;

The character device does not pass the cache, and the block device data passes through the cache. The driver design of the two is very different. In addition to network devices, the drivers for character devices and block devices are mapped to the file system and can be accessed by calling open, read, write, close. It should be explained that the C language fopen, Fread, fwrite, fclose are actually making the corresponding system calls. is a structure diagram of different drive types under Linux:

Knowledge reserves required to drive development

To develop the driver, it is necessary for developers to have a good base of hardware ,C language Foundation ,Linux kernel Foundation and Multi-task concurrency and control .

Browse the kernel source code on Linux, the recommended tool is vim+cscope or vim+ctags.

There are no operating system driver differences

The following is an example of LED drivers to illustrate whether there is a difference between operating systems.

The generic processor has a gpio with two registers, that is, the control register and the data register.

When there is no operating system, there are generally three functions required, i.e.

Lightinit ()// set control register to output mode Lighton ()/// turn on LEDLightoff ()/ / off led


Linux operating system, you can use the character device driver framework to write, according to the programming habits under Linux, you can re-name it as Light_init (), Light_on,light_off

The code structure is as follows:

//define the device structure bodystructlight_dev{structCdev Cdev;//character device structure bodyunsignedCharvalue;}structLight_dev *Light_devp;intLight_major =Light_major; Module_author (" "); Module_license (" "); intLight_open (structInode *inode,structFile *Filp) {}intLight_release (structInode *inode,structFile *Filp) {}//reading and writing equipmentssize_t Light_read (structFile *filp,Char_user *buf, size_t count, loff_t *F_pos) {}ssize_t Light_write (structFile *filp,Const Char_user *buf, size_t count, loff_t *F_pos) {}//IOCTLintLight_ioctl (structInode *inode,structFile *flip, unsignedintCMD, unsignedLongArg) {}structFile_operations light_fops={. ower=This_module; . Read=Light_read; . Write=Light_write; . Open=Light_open; . Release=light_release; . IOCTL=Light_ioctl;}//set character device Cdev structure bodyStatic voidLight_setup_cdev (structLight_dev *dev,intindex) {}//Module Load FunctionintLight_init (void)//Module Unload functionintLight_cleanup (void) Module_init (light_init) module_exit (light_cleanup)


This is just the structure of a program, as you can see, Linux driver code is much more complex than a bare-metal driver.

Hardware Fundamentals of Linux device-driven development

RISC and CISC computer differences: RISC instruction cycle is short, the code is large, the CISC instruction is complex, the instruction period is long, and the code volume is small.

Currently ROM basically uses flash,nor (or non) and NAND (and non) are two mainstream flash.

NOR Flash is characterized by the implementation of the code in the chip , the interface is simple, do not need to add additional control chip , NAND Flash is the feature of block access , interface need to add the control chip, can not execute code inside the chip. The probability of the occurrence of a bit reversal of NAND is greater than nor, and when used, the error detection and error correction algorithm (EDC/ECC) should be used. Flash can only write 1 to 0, before burning, you need to flash the full position, all bytes are 0xff.

DRAM is stored in a capacitor in charge and needs to be refreshed periodically, and SDRAM is also the category of DRAM. Cam is a special type of RAM addressed by the content, input the data that needs to be queried, output data address and matching identity, have a great advantage in the data retrieval.

* Open-drain output, open collector output means that a pull-up resistor is required to output a high level.

The driver engineer is more macroscopic to the hardware than the IC engineer. Drive engineers generally do not need to analyze timing diagrams, but many enterprise driver engineers also need to take on board debugging, so you need to understand some of the circuit timing analysis.

The actual circuit must meet the minimum requirements for settling time and holding time on the chip manual. When viewing datasheet, it is not necessary to read through the full screen, to learn to view the main information content.

Linux Kernel code structure
    • Arch: Code related to different CPU architectures
    • BLOCK: Unit Drive IO scheduling
    • Crypto: Related algorithms, including encryption, hashing, compression, CRC check, etc.
    • Document: Comments and explanations for each part of the kernel
    • Drivers: drive, different drives have different subfolders
    • FS: File System
    • Include: Header file
    • Init: Initializing related code
    • IPC: Process Communication
    • Kernel: Kernel part
    • LIB: library file code
    • MM: Memory Management code
    • NET: Network-related code to implement various protocols
    • Scripts: Configuring Kernel Scripts
    • Security: Safety-related
    • Sound: Audio Device driver
    • USR: cpio for packaging compression

Linux kernel mainly consists of process scheduling , memory management , virtual file system , network interface , process communication .

Kernel Space and user space

Different operating modes are often implemented within the CPU.

For example, ARM's seven modes of operation:

    • User mode (USR) most applications run in this mode
    • Fast Interrupt Mode (FIQ) for high-speed data transfer
    • External interrupt mode (IRP) for general interrupt handling
    • Management Mode (SVC)
    • Data access mode (ABT)
    • System Mode (SYS)
    • No instruction termination mode defined (und)

The Arm+linux uses SWI to enter the SVC mode from USR mode; x86 processor contains 4 different privilege levels (0-3), Linux user code runs at privileged Level 3, system kernel runs at privileged level 0

Linux can only complete control transitions from user space to kernel space through system calls or hardware interrupts.

Kernel Compilation and loading

To add a program to the Linux kernel, you need to complete the following 3 tasks:

Add the code to the appropriate directory for Linux;

Add the appropriate compilation configuration options to the kconfig of the directory;

Add a compilation entry for the new project in the makefile of the directory.

c Coding style under Linux

Under Windows, the macro is all uppercase, the first word of the variable is lowercase, the first letter of each word is capitalized, and the first letter of each word in the function name is capitalized.

For example:

#define Mylinuxint  mylinux; int Mylinux (void);

Under Linux, the following styles are used:

#define Mylinuxint  my_linux; int my_linux (void);

The Linux code indentation uses 8 characters, and the function has another row for the struct, if, etc. {not another line.

Do{}while (0) is primarily used in macro definitions, and is used solely to ensure that macros are defined without errors in compilation.

Goto is only used when error resolution errors occur.

Resources:

Linux device Driver Development detailed Song Baohua

Linux Driver Development Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.