Drive overview of Linux driver development

Source: Internet
Author: User

Driver Overviewwhere is the drive used? The preparation of non-standard type equipment and the drive transplant of standard class equipment. drivers are actually accompanied by the Linux kernel. A piece of code can control our hardware to work, to move, this code is called our driver code. technology is just a means, a skill, we should use technology to make products. 5.1.1_2. What is drive 1_25.1.1.1, understanding-driven concepts(1) The literal meaning of the word driving(2) Physical drive(3) drivers in hardware(4) Linux kernel driver. Software-level drivers broadly refers to: this piece of code to operate the hardware to move, so this piece of code is called hardware driver. (essentially power is provided, and the driver provides an operational logic approach)a narrow-sense driver is a part of the code that refers to the logical method used to manipulate hardware in the operating system. 5.1.1.2, Linux architecture (lower layer to implement upper layer, upper layer to invoke lower layer)(1) Layered thinking(2) above the driver is the system call API (driver pair on the system call API responsible for the API is responsible for the application)(3) below the drive is the hardware (drive to the next management hardware, essentially the driver is the control of the hardware register)(4) The drive itself is also layered ()  5.1.3. Modular Design5.1.3.1, microkernel, and macro cores (two ways of operating system design)(1) macro kernel (also known as a single core): The kernel is implemented as a large process as a whole (it can be understood that the whole kernel is a bare-metal program, the whole kernel is written together), and runs in a separate address space at the same time. All kernel services run in one address space, "call functions with each other", are simple and efficient, and the architecture is relatively simple to design. (coupling is relatively high, but defective, one does not work the other does not work)(2) micro-core: function is divided into independent process, between the process through the "IPC interprocess communication" for communication. A high degree of modularity (low coupling), one service failure does not affect another service. Typical such as Windows(3) Linux: essentially a macro core, but it absorbs the modularity of the microkernel and is represented in 2 levels5.1.3.2, static modularity: At compile time (source level) implementation can be cropped, the feature is to want to change the function of the entire kernel must be recompiled, more complex. Get a new zimage to replace the old zimage, and then burn it to make the effect. "Android-like card brush"In this way, the driver module belongs to the kernel. 5.1.3.3, dynamic modularity: Zimage can not be recompiled burning, and even can not shut down the restart to implement the module installation and uninstallation. "Similar to the Android line brush" This way in fact, this driver module is not a kernel, it can be used when needed, can be uninstalled when not needed.   5.1.4.linux device driver Classification5.1.4.1, Drive classification(1) divided into 3 categories: Character device driver, block device driver, network device driver(2) Classification principle: The device itself read and write operation of the characteristics of the difference (hardware to determine how to write, because the hardware is classified, so the control hardware software program (ie, driver) is also classified)It's like shoes and feet (shoes are made to fit the shape of the foot)5.1.4.2, three-class driver detailed comparative analysis(1) character device, it should be called "byte device", that is, the device read and write: Software operating equipment is in "bytes" for the unit. Typical such as LCD, serial, LED, buzzer, touch screen ...(2) block device, block device is defined relative to the character device, the block device is operated by the software as a "block (a unit of multiple bytes)" unit. The block size of the device is the design of the device itself is defined, is the hardware, the software can not be changed, the size of the different devices can not be the same, but the overall size of the block is fixed.    Common block devices are storage class devices, such as: hard disks, Nandflash, Inand, SD (Block devices cannot be read in bytes) block devices are primarily for storage devices.  The block device reads the entire block into memory and then navigates to a byte in memory to read and write. (3) network equipment, network equipment is "designed for network card design" of the driving model, Linux network device drivers are mainly designed to support the API socket related functions work. "Because network communications have a dedicated interface and architecture, they are placed in a single category." "5.1.4.3, why character device drivers are the most important(1) Common large number of devices are character devices(2) Example of "non-standard type" character device driverstandard equipment refers to the more common equipment, rather than the standard type of equipment is for some proprietary function, a specific production device.  5.1.5. Driver Security requirements (write driver is equivalent to writing kernel)5.1.5.1, Drive is part of the kernel(1) drive has become the largest component in the kernel(2) The kernel calls the driver code directly as a function call (the kernel dies if there is a problem)(3) dynamic installation and uninstallation of the driver will "change" the kernel (a bit like a modified car)5.1.5.2, Drive-to-kernel impact(1) Driver crashes can even cause kernel crashes (some precautions are also made)(2) The efficiency of the drive will affect the overall efficiency of the core(3) Driver vulnerabilities can cause kernel security vulnerabilities5.1.5.3, common drive security issues(1) uninitialized pointer (wild pointer)(2) malicious user program(3) buffer overflow (defining a buffer to receive data from the application layer, but the application layer is sending more data than you define BUF, which can be avoided if some tests are made in the driver)(4) Competition status (multiple processes competing for a common resource phenomenon at the same time) "The driver receives the parameters passed in by the application layer. " 5.1.6. The driver should learn that way.5.1.6.1, learn C language first (the most important premise is C language)5.1.6.2, master relevant preparatory knowledge(1) hardware Operation(2) Application Layer API5.1.6.3, driving the learning stage(1) Pay attention to practice, step by step write drive (write yourself)(2) Framework thinking (consider the upper layer of this code who is called and who is called below), consider the whole (whole Linux system) and the upper and lower layers(3) The Linux "drive Frame" is first learned through a simple device(4) Learn to summarize and record, which will help to understand   Linux operating systems include (Linux kernel +linux applications), where the Linux kernel includes process scheduling, memory management, virtual file systems (character device drivers and block device drivers), network interfaces (network device drivers), and process communication five subsystems, Applications such as shells.  

 (1)

(2) kernel-driven hardware direct relationship:Linux drivers are software programs that deal directly with hardware. On the hierarchy it is between the operating system and the hardware. the relationship between the drive and the Linux operating system kernel1. The driver provides a set of device-driven interface functions Devicedriverinterface to the operating system. In Linux this set of device-driven interface functions typically includes open,close,read,write,ioctl and so on. This set of functions is registered to the Linux kernel through a struct called fileoperations. The Linux kernel provides specific system function functions to register the driver. Provide device driver file name when registering the device number corresponds to the fileoperations structure body fileoperations A set of device-driven interface function pointers is stored in the structure body. 2. The driver also needs to provide 2 module interface functions to the operating system. Linux device drivers exist as a Linux kernel module. Modules have 2 interface functions---module initialization function and module Exit function. The registration of the driver mentioned above is generally implemented by the module initialization function. The module exit function is used to cancel the kernel registration to release resources. It can be seen that only the driver is able to register the kernel to find the device driver after the module initialization function is run. So when does the module initialization function get run? • When the Insmode is run on dynamic load. • The module initialization function is called automatically when the module is compiled into the kernel system during static loading. 3. The driver-to-application relationship is driven by the device files that the driver corresponds to for the application. The application is driven using the API calls provided by the Linux system, and we write the application to write the specific driver, which is the kernel. The application uses the driver through the Linux file operating system call. This means that the device driver is no different from the user operating a file. The driver is the device file that represents the drive. 4. Drive-to-hardware relationship hardware can be abstracted to a driver as a set of registers and an interrupt source that needs to be responded to. For unified addressing systems such as ARM, this set of registers is an address space. Drivers are read or written to these address spaces in accordance with the principles set out in the chip manual. The interrupt source is the hardware-generated interrupt interrupt that is required by the kernel to register an interrupt handler for this interrupt. This can be done by invoking the Linux kernel to provide specific system function functions. The general registration interrupt handler function can be implemented in the module initialization function or in the device driver interface program open. ----------------------------------------------------------------------------------------------------------- -----------------------------------------------Linux driven Learning:(1) The driver is the soul of the hardware and the bridge between the hardware and the system. (2) The object of the driver is generally the memory and the external device. Linux divides these devices into 3 main categories: "character devices, block devices, and network devices"A character device is a device that can read data one byte at a byte, and a character device generally needs to implement open (), close (), read (), write (), and IOCTL () functions in the drive layer. (3) The Linux kernel contains many modules that implement specific functions, including file systems, network protocol stacks, device drivers, kernel scheduling, memory management, and process management. (4) User state and kernel state:Linux operating system is divided into the user state and the kernel state, the User state processing the upper layer of software work. The kernel state is used to manage the user-state of the program, to complete the user-state request work. The driver interacts with the underlying hardware, so it works in the kernel state. (5) The module mechanism is the code that can be added to the kernel at runtime, which makes the kernel easy to be clipped. (6) Static loading and dynamic loading:When the module is loaded at the kernel boot, it is called a static mount (in the form of a mirrored image), and the load of the module when the kernel is already running is called dynamic loading. (7) The header file contained in the kernel program refers to the kernel header file in the kernel code tree, not the external header file when the application is developed, eg: in the kernelThe print function in the implemented library function PRINTK () is the kernel version of the C language library function printf (). (8) The Linux operating system structure consists of 4 layers: The user layer, the kernel layer, the driving layer and the hardware layer. (9) When the kernel starts, the first thing to do is to look for the root file system (which contains the main program Eg:shell and data that makes the system run) to the storage device (boot device), and the other normal file system will be mounted to the root file system in the future. (10) The first program that runs after the kernel starts is init, which launches the shell program in the root file system and provides a friendly interface to the user. (11) The root file system organizes the structure of directories and files in a tree structure, and when the system starts, the root file system is attached to the root directory "/", when the root directory contains the directories and files of the root file system. (12) We often use the BusyBox tool to build the root file system, and can download its corresponding version from Http://www.busybox.net/downloads. (13) When building a driver module, you must consider the compatibility of the driver with the kernel. Even if the module code is identical, the module format of the standard kernel module and the specific vendor's kernel module is different. (14) The components of the drive module:1. Header file (required) #include <linux/module.h> and #include<linux/init.h> are required2, module parameters (optional) When the driver module is loaded, the parameters that need to be passed to the driver module3, module function function (optional)4. Other (optional)5, module load function (must) module load function, a bit similar to the main () function6. The module unload function (must) executes and clears the resources allocated in the LOAD function .7. The module License Statement (required) indicates the extent to which the module is supported by the kernel, using module_license () to indicate the extent of licensing permissions. (15) operation of the module:1) Insmod command loading module2) Rmmod command unload module3) Lsmod View module loading informationthe drivers under Linux are mainly divided into character device driver, block device driver and network device interface driver.     Our learning drive is to learn the interfaces that these three types of devices provide to us. (17) static compilation (re-download compiled Linux kernel) and dynamic compilation. The development phase is dominated by dynamic compilation (without restarting the kernel). Dynamically loaded in the same way as the module. (18) The main device number distinguishes device drivers. The secondary device number distinguishes between multiple devices created by the same driver. Common to multiple serial ports and hard disk partitions. Secondary device numbers usually correspond to multiple devices of the same type in turn. (19) device-driven framework:1, the start of the driver------device Registration2. Registered Structure3. Registered FOP pointer (function table)(20) The kernel programmer obtains the memory by Kmalloc (the allocated address space is physically contiguous), using a method similar to the malloc version of the user space. Kmalloc Pass different flags, resulting in different behavior of the function:Gfp_kernelgfp_atomicGfp_user_GFP_DMA 2.4 Register--char in the kernel2.6 The initialization of the Cdev struct is called in the kernel, and the FOP pointer (function table) is embedded in the Cdev struct.                    

Drive overview of Linux driver development

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.