Linux Driver Development Essentials __linux

Source: Internet
Author: User

The development of Linux drivers differs greatly from application development, which leads to the essential difference between writing a Linux device driver and writing an application.

First, user state and kernel state

The Linux operating system is divided into user state and kernel state . The kernel state completes the interaction with the hardware, such as reading and writing memory, reading the data on the hard disk to memory, and so on. The driver interacts with the hardware at the bottom, so it works in a kernel state. User state can be understood as an upper-level application, which can be Java applications, QT applications, Python applications, and so on. The reason the Linux operating system is divided into two states is that even if the user-state application is abnormal, it does not cause the operating system to crash, which is due to the strong protection of the operating system by the kernel state.
  
On the other hand, the reason the Linux operating system is divided into two States is primarily to provide a unified computer hardware abstraction for the application. Applications that work in user-state can be completely off the ground with hardware operations, which are done by a kernel-state program. Most of these kernel-state programs are device drivers. The application can operate the hardware well without knowing how the hardware works, without making the hardware devices go into an illegal state.
  
It is noteworthy that the user state and the kernel state can be converted to each other. Every time the application executes a system call or is suspended by a hardware interrupt, the Linux operating system switches from user state to kernel state, and the operating system returns to the user state from the kernel state and continues executing the application when the system call completes or the interrupt processing completes.

Second, the module mechanism

The module is the code that can be added to the kernel at runtime, which is a very good feature of Linux, which makes it easy to enlarge or shrink the kernel, and the kernel can be enlarged by enlarging the core, reducing the kernel size. The Linux kernel supports a variety of modules, the driver is one of the most important, each module consists of compiled target code, using the Insmod command to join the module in the running kernel, using the Rmmod command to remove an unused module from the kernel.
  
The module is loaded as a static Mount when the kernel is started, and the load is called dynamic loading When the kernel is already running. Modules can augment any functionality expected by the kernel, but are typically used to implement device drivers.
  
The most basic framework code for a module is as follows:

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

static The initialization of int __init xxx_init (void)
{/
    * module Loading */return
    0;
}

The destroy work of the static void __exit xxx_exit (void)
{/
    * module uninstall/
}

module_init (xxx_init);  /* Macro
/Module_exit (xxx_exit) for the initialization function of the specified module;  /* The Unload function of the specified module
/module_license ("Dual BSD/GPL");/* Specify License Right/*
Third, bus, equipment, drive

To harness Linux-driven development, you must have a deep understanding of the Linux bus device-driven framework. This framework is created primarily for the reusability of the code because the driver and device relationships are one-to-many. As with the main device number and the secondary device number, the main device number represents the driver, and the secondary device number indicates the specific device.

In addition to improving driver portability, Linux splits the resources (such as GPIO and interrupts) that are used to drive the device to manage it. That is, the device contains its own device properties and includes the resources it uses to connect to the SOC. and drive the focus on the operation of the process and methods. The role of the

Bus is to manage devices and drivers at the software level. Equipment to allow the system to perceive its own existence, the device needs to register themselves to the bus; Likewise, drivers need to register themselves with the bus in order for the system to perceive its own existence. The device and the bus must be initialized to identify which bus they are. Therefore, in order to achieve operational consistency, Linux invented a virtual bus, called the platform bus .

Multiple devices and multiple drivers are registered on the same bus, how does the device find the most suitable driver, or how can the driver find the device it supports? This is also the bus is responsible for, the bus is like a matchmaker, responsible for the device and drive the strings. The device will present itself to the bus on the driver's condition (the simplest and most accurate is to specify the name of the other), and the driver will tell the bus the conditions of the device they can support (usually the model ID, the simplest of which can also be the name of the device). When the device is registered, the bus will traverse the driver registered on it, find the driver that works best for this device, and then fill in the structure members of the device; When the driver registers, the bus also traverses the device that is registered on it, finds the device it supports (can be multiple, the driver and the device relationship is 1:n), and fill the device into the driver's support list. We call the bus the behavior of the strings is match. After pulling the line, the interactive matchmaker between the device and the driver is out of the way.

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.