Linux Device Driver

Source: Internet
Author: User

I. Introduction

The operating system controls hardware through various drivers. It shields users from a variety of devices. The driving hardware is the most basic function of the operating system and provides a unified operation mode. As we can see the screen
When using the documentation, you don't have to worry about whether to use the NVIDIA chip or the display card of the ATI chip. You only need to know the input command and the required text will be displayed on the screen. Hardware drivers are the most basic operating system
In the Linux kernel source program.



Ii. Basic Principles

The device consists of two parts: one is the electrical part called the controller and the other is the mechanical part. The Controller is loaded
Computer. Typically, a set of non-conflicting register groups are assigned to each controller. The I/O port consists of four registers: Status Register, control register, data input register, and data output register. The Status Register has the (Status) bits that can be read by the host.
Indicates whether the current command has been executed, whether bytes can be read or written, and any error messages. The control register is written by the host.
To start a command or change the device's (working) mode. The data input register is used to obtain the input, and the data output register is sent to the host.
Sending result. Therefore, the basic interface between the processor and the device is the control and Status Register. When the processor executes the program and encounters device-related commands
It sends a command to the corresponding device to execute the command. The Controller executes the required action and sets the special location of the Status Register,
Then, enter the waiting state. The processor is responsible for checking the status of the device until the discovery operation is completed. For example, the parallel port driver (used by the printer) usually
Poll the printer to see if the printer is ready. If the printer is not ready, the driver will sleep for a while (the processor will do it now
This process will repeat until the printer is ready. This round robin mechanism can improve the system performance. Another method is
The system performs unnecessary "unnecessarily waiting" without any useful work.
Registers have clearly defined address ranges in the I/O space. These addresses are usually allocated at startup and are defined in the configuration file using a group
. If the device is statically loaded, the address range of each device may be pre-allocated. This means that the kernel contains the driver of an existing device.
The allocated I/O ports can be stored in the proc directory. You can run "cat/proc/ioports"
Check the address range used by the command synchronization. The output in the first column shows the port range, while the second column shows the devices that use these ports. Some operating systems have the ability to dynamically load device driver modules during operation. Therefore, any new device can run in the system through Dynamic Loading modules.
And can be controlled and accessed.
The concept of a device driver is very abstract and is at the lowest layer of the software running on a computer. Limitations due to direct access to hardware features of devices
. Each device driver manages only one type of device. These types may be portable, fast, or network type. If an application
The program puts forward (operation) requirements to the device. The kernel will be connected to the corresponding device driver, and the device driver then sends commands to specific devices. Device drive
A function is a set of functions. It contains many call portals, such as open, close, read, write, ioctl, and llseek. When you insert
When your module is removed, the init_module () function is called. When the module is removed, the cleanup_module () function is called. The device is in
The init_module () routine of the device driver is registered.
When the device logs in init_module (), the device resources such as the I/O port, memory and interrupt number are also allocated in this function.
The driver must be able to operate the device correctly. If you allocate any wrong memory address, the system displays the error message.Segmentation fault.
For the I/O port, the system does not provide any similarWrong I/O portBut assign any end that is used by the existing device.
The system will crash. When you remove a module, the device should be canceled. More specifically, the master (device) Number and resources will be released in the cleanup_module () function.
Read/write Io ports when device drivers work most frequently. Therefore, your driver should be sure to be perfect, and the port address used by the device is exclusive.
This address range is not used by any other device. To confirm this, the driver should first check whether the address is in use.
When this address is not used, you can request the kernel to allocate this address to the device.

Iii. kernel and Driver Module


The Linux kernel adopts a loadable modular design (lkms and loadable kernel modules). Generally, the compiled Linux kernel supports plug-in modules, that is, compile the most basic core code in the kernel. Other code can be either in the kernel or compiled into a module file of the kernel.

Our common drivers are dynamically loaded as kernel modules, such as sound card drivers and nic drivers. Linux's most basic drivers, such as CPU, PCI bus, TCP/IP protocol, and APM
(Advanced Power Management), VFS and other drivers are compiled in the kernel file. Sometimes kernel modules are called drivers, but the driver content is not necessarily hardware, such as the drive of the ext3 file system.
.
Therefore, the kernel module is loaded when the driver is loaded.

Commands for related modules include lsmod, modprob, insmod, rmmod, and modinfo.


Lsmod lists the modules loaded in the current system
Rmmod uninstall loaded modules
Insmod: inserts a module, but it does not automatically resolve the dependency. Therefore, the command used to load the kernel module is modprobe.

Modprobe: intelligent insertion module, that is, the intelligent insertion module based on the dependency between modules and the content in the/etc/modules. conf file

Modinfo, View module information


The system module files are stored in the/lib/modules/2.4.xxx/kerne Directory, which are in the FS, net, and other sub-directories by category, their interdependence is stored in/lib/modules/2.4.xxx/modules. dep File

Iv. Device Files


After the device driver module is loaded, how can I access these devices? Linux is a UNIX system. A basic feature of UNIX is that everything is a file. It abstracts the processing of devices and treats all hardware devices as common files, that is to say, the hardware can be opened, closed, and read/write like normal files.

Devices in the system are represented by a special device file, which is also divided into block-type device files, character-type device files, and socket files.
(Network plug-in) device files. Block device files often specify the devices that need to be written in blocks (such as 512 bytes), such as IDE hard disks, SCSI hard disks, and optical drives. Character-type device files often specify direct read/write, and devices without a buffer, such as parallel port and virtual console. The socket (network plug-in) device file specifies the BSD socket interface accessed by the network device.
All the device files are stored in the/dev directory.

5. Use the files in the/proc directory to monitor the driver status

How can I access the corresponding driver through the device file? There is a bridge between them, that is, the proc file system, which is usually loaded to the/proc directory. When accessing a device file, the operating system usually
Find the value in the/proc directory to determine which driver modules will complete the task. If the proc file system is not loaded, an error occurs when accessing the device file.

In Linux, the proc file system is a virtual Kernel File System. All the files are virtualized in the kernel, and various files are actually parameters of the current kernel in the memory. It is like
A door opened by accessing the kernel, such as accessing the/proc/cpuinfo file, is actually the parameter used to access the current CPU. The system passes
The information set in/etc/fstab automatically loads the proc file system to the/proc directory.

You can access or change kernel parameters through files in the/proc directory, and query driver information through the/proc directory.

Files in the/proc/sys directory can be directly changed, which is equivalent to directly changing the kernel running parameters,
Commands such as free are generally used to query files in the/proc directory and return results.

Refer:
Http://www.linuxfocus.org/ChineseGB/November2002/article264.shtml

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.