Linux Device Driver series (1) -- Opening

Source: Internet
Author: User

2011-03-24

 

Linux has been a hit recently. As a result, it is still just getting started, barely starting. But I'm glad that I finally started, and everything was hard at the beginning, especially the study of "Linux device drivers. It's also strange that I have had many activities recently. I organized "CS competitions" and bought bicycles. I often went out to ride bicycles with my classmates. In a simple summary, I spent one week in the Teaching and Research Section, I am very happy to be in the status for a day or two. I am not so arrogant. I will start to record the notes for "Linux Device Driver, make a backup. If you forget it later, you can come back and look for it.

 

I. Preparations

To write a "Linux Device Driver", you must first have a suitable environment and a large amount of references.

As for the environment, I am using Ubuntu 11.04 (usually comes with GCC and other compilation tools, if not, please do it yourself)

For more information, see:

1. kernel source code (Linux 2.6.30)

 

The http://www.kernel.org/pub/linux/kernel/ of the kernel source package can definitely find the version you want, I use Linux 2.6.30.

In order to facilitate reading, I provide a website, very powerful, you must try, to read the kernel source code provides a lot of convenience, address for http://lxr.linux.no/linux+v2.6.30/

2. Reference Books

There are too many books. I can't see it. My first book, "Linux Device Driver (Third edition)", translated by Wei Yongming, is known as the Bible of the Linux driver industry, this book is good. It may be difficult to read it at the beginning. This book can also be viewed online at http://oss.org.cn/kernel-book/ldd3/index.html.

Written by Feng guojin, the second book "embedded Linux Driver Design from entry to entry-level proficiency. Books with Chinese characteristics, you know.

2. Knowledge preparation

There is a lot of preparation for learning "Linux device drivers". I can read this article in total. The basic knowledge should be poor. I will not say much about it, let's talk about the basic concepts of "driver and kernel:

Device Driver:The driver plays a special role in the Linux kernel. they are completely different "Black Boxes" that make a special part of the hardware respond to the defined internal programming interface. they completely hide the details of device operations. user activities are carried out through a set of standardized calls, which are independent of the special driver; the role of the device driver is to map these calls to device-related operations on the actual hardware. this programming interface is like this. The driver can be created separately from other parts of the kernel and "inserted" at runtime as needed ". -- Linux Device Program

In fact, the device driver is actually an intermediate layer between the hardware device and the application. Drivers work in the kernel space, while applications generally work in the user space. A device driver is an interface between the operating system kernel and hardware devices.

The essence of the device driver is to convert the logic device to the physical device, start the corresponding I/O device, issue the I/O command, and complete the corresponding I/O operations, it is the core code for data exchange between the kernel and peripheral devices. The device driver shields applications from the details of hardware devices. In the user space application, the hardware device is just a file, and the application deliberately uses the driver to operate the hardware device like an ordinary file.

Comrades who have learned about the "operating system" should be aware of the division of kernel functions: process management, memory management, file system, device control, and network functions.

Map:

So far, the relationship between the kernel and the driver should be clearly explained-for a deeper understanding, let's take a look at the book I recommend.

 

One of the many excellent features of Linux is that it can expand the features provided by the kernel during runtime. this means that you can add (or remove) the kernel function when the system is running ).

Linux-based devices can be divided into three basic device types. each module can be categorized into character modules, block modules, or network modules.

 

Character Device

A character (char) device is a device that can be accessed as a byte stream (like a file). A character driver is responsible for such behavior. such drivers often implement at least open, close, read, and write system calls. the text Console (/dev/console) and serial port (/dev/ttys0 and their friends) are examples of character devices because they demonstrate stream abstraction well. character devices are accessed through file system nodes. For example,/dev/tty1 and/dev/lp0. the only difference between a character device and a common file is that, you can often move in common files, but most character devices are only data channels, and you can only access them in sequence. however, there are character devices that look like data areas. You can move them in. for example, frame grabber is often used in this way, applications can use MMAP or lseek to access the entire requested image.

Block Device

Like character devices, Block devices are accessed through file system nodes in the/dev directory. A block device (such as a disk) should be resident in a file system. in most Unix systems, a block device can only process such I/O operations, and transmits one or more 512 bytes (or a larger 2 power). linux, on the contrary, allows an application to read and write a block device like a character device-it allows a single transmission of any number of bytes. the result is that the difference between a block and a character device is only in the way the kernel manages data internally, and therefore it is different in the kernel/driver software interface. like a character device, each block device is accessed through a file system node, and the difference between them is transparent to users. compared with the character driver, the block driver is completely different from the kernel interface.

Network Interface

 

Any network transaction is performed through an interface, that is, a device that can exchange data with other hosts. generally, an interface is a hardware device, but it may also be a pure software device, such as a loopback interface. A network interface is used to send and receive data packets. Driven by the kernel network subsystem, you do not have to know how a single transaction maps to the actually sent packets. many network connections (especially those using TCP) are stream-oriented, but network devices are often designed to send and receive packets. A network driver knows nothing about a single connection; it only processes packets.

Each piece of code that can be added to the kernel at runtime is called a module. the Linux Kernel provides support for many module types, including but not limited to device drivers. each module consists of the target code (not a complete executable file). It can be dynamically connected to the running kernel, connected through the insmod program, and through the rmmod program.

Next, let's start the first Linux Device Driver-character device driver.

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.