Linux core -- 9. Device Driver

Source: Internet
Author: User
Article Title: Linux core-9. Device Driver. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Original: David A Rusling
Translation: Banyan & fifa
Chapter 8 Device Drivers
  
One of the purposes of the operating system is to hide the details of the system hardware from the user's sight. For example, the Virtual File System provides a unified view for various types of installed file systems, shielding the specific underlying details. This chapter describes how Linux core manages physical devices in the system.
  
The CPU is not the only smart device in the system. Each physical device has its own controller. The keyboard, mouse, and serial port are centrally managed by an advanced I/O chip. The IDE controller controls the IDE Hard Disk while the SCSI controller controls the SCSI hard disk. Each hardware controller has its own control and Status Register (CSR), which are different from each other. For example, the csr of the Adaptec 2940 SCSI controller is totally different from that of the NCR 810 SCSI controller. These CSR are used to start and stop devices, initialize devices, and diagnose devices. In Linux, the codes used to manage hardware device controllers are not stored in every application, but managed by the kernel. The software that processes and manages hardware controllers is the device driver. Linux core device drivers are a set of privileged-level memory that reside in the underlying hardware processing shared libraries. They manage various devices.
A basic feature of device drivers is the abstract concept of device processing. All hardware devices are regarded as common files. devices can be opened, closed, read, and written by calling the same standard system as common files. Each device in the system uses a special device-related file. For example, the first IDE hard disk in the system is expressed as/dev/hda. You can use the mknod command to create device-related files for block (Disk) devices and character devices, and use the Master/Slave Device number to describe the device. Network devices are also represented by device-related files, but such files are created only when the network device is searched and initialized in Linux. All devices driven by the same device have the same master device number. The device number is used to distinguish different devices with the same master device number and controlled by the same device driver. For example, the number of slave devices in each partition of the primary IDE disk is different. For example,/dev/hda2 indicates that the master device Number of the primary IDE hard disk is 3 and the slave device number is 2. Linux maps device-related files contained in system calls (such as mounting a file system to a block device) to device drivers and a large number of system tables by using the master/Slave Device number, such as the character device table, chrdevs.
  
Linux supports three types of hardware: character, block, and network devices. Character devices are devices that do not need to buffer and read/write directly, such as system serial devices/dev/cua0 and/dev/cua1. Block devices can only read and write data in blocks. The typical block size is 512 or 1024 bytes. Block Device Access is carried out through the buffer cache and can be accessed randomly, that is, the block can be read and written no matter where it is located. Block devices can be accessed through the file system. Only Block devices can support file system installation. Network devices can be accessed through BSD interfaces. We will discuss network subsystems in the network chapter.
  
Although there are many different device drivers in the Linux kernel, they share some common characteristics:
Core code
Device Drivers are a part of the core. Like other code in the core, an error will cause serious damage to the system. A device driver that writes an odd code can even crash the system and cause damage to the file system and data loss.
    
Core interface
The device driver must provide a standard interface for the Linux core or its sub-system. For example, the Terminal Driver provides a file I/O interface for the Linux core, and the SCSI Device Driver provides a SCSI device interface for the SCSI subsystem, at the same time, this subsystem provides core file I/O and buffer cache interfaces.
Core mechanisms and services
    
Device drivers can use standard core services such as memory allocation, interrupted sending, and waiting queue.
Dynamic Loading
Most Linux device drivers can be loaded when the core module sends a load request and is not used any more. In this way, the core can effectively use system resources.
    
Configurable
The Linux device driver can be connected to the core. When the core is compiled, which core is connected to the core is configurable.
    
Dynamic
When the system is started and the device driver is initialized, it searches for the hardware devices it controls. If the driver of a device is empty, there is no problem. At this time, the device driver is only a redundant program, it will not cause any harm to the system except occupying a small amount of system memory.
  
8.1 polling and interruption
When a device is executing a command, such as "moving the read head to the 42nd sector of a floppy disk ", the device driver can select a method from the polling mode and interrupt mode to determine whether the device has completed the command.
The polling method means that the device status needs to be read frequently until the device status indicates that the request has been completed. If the device driver is connected to the core, the polling method will have disastrous consequences: the core will do nothing in the process until the device completes the request. However, by using a system timer, the core periodically calls a routine in the device driver to check the device status. The timer can check the command status and the operating status of the Linux Floppy Drive. The timer is the best method for polling, but the more effective method is to use interruption.
  
The interrupt-based device driver will cause a hardware interruption when the hardware device it controls requires services. For example, when an Ethernet device driver receives an Ethernet data report from the network, it will interrupt. The Linux core needs to transmit interruptions from hardware devices to the corresponding device driver. This process is completed by the device driver registering with the core for the interruption of its use. The address and interrupt Number of the interrupt processing routine will be recorded. In the/proc/interrupts file, you can see the interrupt number and type of the device driver:
  
0: 727432 timer
1: 20534 keyboard
2: 0 cascade
3: 79691 + serial
4: 28258 + serial
5: 1 sound blaster
11: 20868 + aic7xxx
13: 1 math error
14: 247 + ide0
15th: 170 + ide1
  
The request for resource interruption is completed during driver initialization. As a legacy of the ibm pc architecture, some interruptions in the system have been fixed. For example, the floppy disk controller always uses interrupt 6. Other interruptions, such as PCI device interruptions, are dynamically allocated at startup. The device driver must find the interrupt number (IRQ) of the device it controls before obtaining ownership of the interrupt ). Linux uses the standard pci bios callback function to determine the interrupt information of the PCI device in the system, including its IRQ Number.
  
How to send an interrupt to the CPU depends on the architecture. However, in most architectures, sending an interrupt in a special mode also prevents other interruptions in the system. The less the device driver works in the process, the better, so that the Linux core can quickly process the interruption and return the status before the interruption. In order to complete a lot of work during reception interruption, the device driver must be able to queue the routines to be called in the future using the core underlying processing routines or task queues.
8.2 Direct Memory Access (DMA)
When the data volume is small, the interrupt driver can smoothly exchange data between the hardware device and the memory. For example, a modem with a baud rate of 9600 can transmit one character every millisecond. If the interruption delay caused by hardware device interruption and Device Driver interruption is large (for example, 2 ms), the overall data transmission rate of the system will be very low. The data transmission at the 9600 baud rate modem can only use 0.002% of the CPU processing time. High-speed devices such as hard drive controllers or Ethernet devices have a higher data transmission rate. The data transmission rate of the SCSI device can reach 40 MB per second.
  
Direct Memory Access (DMA) is an effective solution to such problems. The DMA controller can transmit data between the device and the system memory at high speed without the intervention of the processor. The isa dma controller of the PC has eight DMA channels, seven of which can be driven by the device. Each DMA channel has a 16-bit address register and a 16-bit memory register. To initialize data transmission, the device driver sets the DMA channel address and number register to describe the data transmission direction and read/write type. Then, the device is notified that the DMA Operation can be started at any time. The device will interrupt the PC when the transfer ends. During transmission, the CPU can be transferred to another task.
  
The device driver must be very careful when using DMA. First, the DMA controller does not have any Virtual Memory concept. It only accesses the physical memory in the system. At the same time, the memory space used as the DMA transmission buffer must be a continuous physical memory block. This means that you cannot directly use DMA in the virtual address space of the process. However, you can lock the physical page of the process to prevent it from being switched to the switching device during the DMA Operation. In addition, the physical memory accessed by the DMA controller is limited. The DMA channel address register represents the 16-bit high of the DMA address, while the page register records the remaining 8 digits. Therefore, the DMA request is limited to 16 MB of memory.
  
DMA channels are a very precious resource. There are only seven available channels that cannot be shared between device drivers. Like an interrupt, the device driver must find the DMA channel it should use. Some devices use fixed DMA channels. For example, a floppy disk device always uses DMA Channel 2. Sometimes the DMA channel of a device can be set by a jumper, which is used by many Ethernet devices. A flexible device will tell the system which DMA channel it will use. In this case, the device driver only needs to select from the DMA channel.
  
Linux uses the dma_chan array (one for each DMA channel) to track the usage of the DMA channel. The dma_chan structure contains two fields: one is the pointer to the DMA channel owner and the other indicates whether the DMA channel has been allocated. When cat/proc/dma is typed, the result is the dma_chan structure array.
8.3 memory
The device driver must use the memory with caution. Because it is the core, virtual memory cannot be used. When the system receives the interrupt signal or schedules the underlying task queue processing process, the device driver starts to run, and the current process changes. The device driver cannot depend on any running process, even if it is currently working for it. Like the rest of the core, a device driver uses a Data Structure to describe the devices it controls. These structures are statically allocated by the device driver code, but they increase the core and lead to a waste of space. Most device drivers use core non-page memory to store data.
Linux provides a set of core memory allocation and recovery processes for device drivers. Core Memory is allocated in blocks of the power of 2. For example, 512 or 128 bytes. In this case, even if the device driver needs to be smaller than this quantity, it will be allocated so much. Therefore, the memory allocation request of the device driver can obtain the memory with the block size as the boundary. In this way, it is easier for the core to combine idle blocks.
  
Linux requires a lot of additional work to request core memory allocation. If the number of idle memory in the system is small, some physical pages may need to be discarded or written to the swap device. Generally, Linux suspends the requester and processes

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.