Linux device driver First article: Introduction to device drivers

Source: Internet
Author: User
Tags network function

First, we know that the driver is part of the kernel, so what role does the driver play in the kernel?

The role of the device driver in the kernel: they are separate "black boxes" that enable a particular hardware to respond to a well-defined internal programming interface that completely hides the work details of the device. (Plainly, the driver does not have any implementation details that are invisible to the application in addition to providing a specific interface to the outside.) The actions of the user are performed through a set of standardized calls that are independent of the specific driver. The task of the driver is to map these standardized calls to the device-specific operations of the actual hardware.

When writing drivers, programmers should pay particular attention to the following concept: When writing kernel code that accesses hardware, do not impose any specific policy on the user. I understand that the driver is only responsible for the implementation of the most basic hardware functions, and who use this hardware, how to use, generally do not consider.

Drivers without policy include some typical features: simultaneous and asynchronous operations, the ability of drivers to be opened multiple times, the full use of hardware features, and the ability to "simplify tasks" or provide policy-related software layers.

The driver is a software layer between the application and the actual hardware, the same hardware, and different drivers may provide different functions. The actual driver design needs to be balanced among the many factors to be considered. In general, the driver program design is mainly to consider the following three factors: provide users with as many options as possible, write the driver to take the time and try to keep the program simple and not error-ridden.

Understanding the role of the driver in the kernel, we also have a simple understanding of the other modules included in the kernel. In general, the kernel functions are divided into the following parts: Process management, memory management, file system, device control, network function. Operating system principle of the operating system is divided into: processor management, process management, file management, storage management, equipment management, network and communication management, user interface, in which the processor management is actually process management. Because the processor is allocated and executed in a process-based unit. and storage management means memory management.

Further, the computer has a network card, video card, sound card, etc., you can also external USB flash drive, printer and so on peripherals, so many devices have no classification? Obviously, according to the interface of the device, we can be divided into USB device, serial device, PCI device, SPI device, i²c device and so on, then there is a kind of partition in the Linux kernel? The following is a classification of all the devices in Linux, and describes the simple differences between each other.

Classification of devices and modules in Linux:

Character Device : A character device is a device that can be accessed like a byte stream (like a file) and has a character device driver to implement this feature. Character device drivers typically implement at least open, close, read, write system calls. Character devices can be accessed through file system nodes, the only difference between these device files and normal files is that access to ordinary files can be moved back and forth, while most character devices are a data channel that can be accessed sequentially. A character device is a byte throttle device, access to the device can only be accessed sequentially in bytes, not random access, the character device does not request a buffer, all access requests are executed sequentially. But in fact some advanced character devices can also read one piece of data at a time from a specified location.

Block Devices: block devices are also accessed through device nodes. Can accommodate file systems on block devices. In most Unix systems, the block device can only transfer one or more complete blocks at a time when I/O operations, and each block contains 512 bytes (or more than 2 of the higher Power bytes of data). Linux allows applications to read and write block devices like character devices, allowing arbitrary multibyte data to be passed at once. Thus, the difference between a block device and a character device is simply the way in which the kernel manages the data, which is the software interface between the kernel and the driver, and these differences are transparent to the user. In the kernel, the block driver has a completely different interface than the character driver. Storage devices generally belong to the block device, the block device has a request buffer, and support random access without having to access the data in sequence, such as you can first access the data behind, and then access the previous data, which is not possible for the character device. Disk devices under Linux are block devices, and although there is a block device node under Linux, the application typically accesses the block device through the file system and its cache, rather than reading and writing data on the block device directly through the device node.

Network devices: network devices differ from character devices and block devices, which are message-oriented rather than stream-oriented, and do not support random access or request buffers. Because it is not a stream-oriented device, it is difficult to map a network interface to a node in a file system. The communication between the kernel and the network device driver is completely different from the kernel and character and the communication between the block driver, and the kernel calls a set of functions related to packet transport rather than read,write. The network interface does not have the same device number as the character device and the block device, only a unique name, such as eth0, eth1, etc., and the name does not need to correspond to the device file node.

The difference between a character device and a block device is summarized above: 1, the character device is stream-oriented, the minimum access unit is byte, and the block device is block-oriented, and the minimum Access unit is 512 bytes or 2 higher power. 2. Character devices can only be accessed sequentially by byte, while block devices are randomly accessible. 3, block device can accommodate file system, Access form, the character device through the device node access, while the block device can also be accessed through the device node, but generally through the file system to access data.

And the network equipment does not have the device node because, the network equipment is facing the message, it is very difficult to implement the related read, write and other file read and write functions. So the implementation of the drive is also different from the character device and the block device.

Linux device driver First article: Introduction to device drivers

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.