How to write Linux device drivers

Source: Internet
Author: User
Tags linux

Preface

Linux is a variant of the UNIX operating system, and the principle and idea of writing a driver in Linux is similar to that of other Unix systems, but it is very different for drivers in DOS or window environments. Design the driver in the Linux environment, the thought is concise, the operation is convenient, the function is also very formidable, but the support function is few, can only depend on the function in the kernel, some commonly used operation to write by oneself, and debugging also inconvenient. I have this week for the laboratory developed a multimedia card to develop a driver, gained some experience, is willing to share with Linux fans, there are improper, please correct me.

Some of the following text is mainly from Khg,johnsonm write Linux device Driver,brennan ' s Guide to Inline assembly,the Linux A-Z, and Tsinghua BBS on the device Some information about driver. Some of these materials are outdated and some are still wrong, and I have revised them according to my own test results.

First, the concept of Linux device driver

The system call is the interface between the operating system kernel and the application, and the device driver is the interface between the operating system kernel and the machine hardware. The device driver masks the details of the hardware for the application so that, in the view of the application, the hardware device is just a device file. An application can operate on a hardware device as if it were a normal file. The device driver is part of the kernel, which completes the following functions:

1. Initialize and release the equipment.

2. Transfer data from the kernel to the hardware and read the data from the hardware.

3. Read data that the application sends to device files and loopback application requests.

4. Detection and handling of equipment errors.

There are two main types of device files under the Linux operating system, one is a character device, the other is a block device. The main difference between a character device and a block device is that when a read/write request is made to a character device, the actual hardware I/O is typically followed by a block device, which uses a system memory as a buffer. When a user process requests the device to meet the requirements of the user, the requested data is returned and, if not, the request function is invoked to perform the actual I/O operation. The block device is designed primarily for slow-speed devices such as disks, lest it consume too much CPU time to wait.

It has been mentioned that the user process deals with the actual hardware through device files. Each device file has its file attributes (c/b), which means that the character device is also 蔤 strong Qiang? In addition, each file has two device numbers, the first is the main device number, identifies the driver, and the second is from the device number, Identifies different hardware devices that use the same device driver. For example, there are two floppy disks that can be used to differentiate them from the device number. The primary device number for the device file must be the same as the primary number that the device driver requested when registering, otherwise the user process will not be able to access the driver.

Finally, it must be mentioned that when the user process invokes the driver, the system enters the kernel mindset, This is no longer a preemptive dispatch. In other words, the system must return the child function of your driver before doing anything else. If your driver is stuck in a dead loop, unfortunately you only have to reboot the machine, and then the long Fsck.//hehe

When read/write, it first looks at the contents of the buffer, if the data of the buffer

How to write a device driver under a Linux operating system

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.