Overview of Linux driver framework and driver Loading

Source: Internet
Author: User
Tags apm

This section describes the Linux Device Driver framework, driver configuration files, and common methods for loading drivers. It also describes how Red Hat Linux installer loads drivers, we can put the driver into the boot disk by ourselves; after installing the system, we can use kudzu to automatically configure the hardware program.

  Linux Device Driver Overview

1. kernel and Driver Module

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. Just as when we look at the documents on the screen, we don't have to worry about whether to use the NVIDIA chip or the display card of the ATI chip. after entering the command, we only need to know that the required text is displayed on the screen. The hardware driver is the most basic component of the operating system and also occupies a high proportion in the Linux kernel source program.

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.

If you need a function, such as accessing an NTFS partition, load the corresponding NTFS module. This design can make the Kernel File not too large, but also support a lot of features, dynamic loading if necessary. This is not the same as the microkernel design, but it is a practical kernel design solution.

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

It is important to understand this. Therefore, the kernel module is loaded when the driver is loaded. Next, let's take a look at the commands related to the modules and use them when loading drivers: lsmod, modprob, insmod, rmmod, and modinfo.

Lsmod lists the modules loaded in the current system, for example:

# Lsmod (consistent with CAT/proc/modules) module size used by not taintedradeon 115364 1 agpgart 56664 3 nls_iso8859-1 3516 1 (autoclean) loop 12120 3 (autoclean) smbfs 44528 2 (autoclean) parport_pc 19076 1 (autoclean) LP 9028 0 (autoclean) parport 37088 1 (autoclean) [parport_pc LP] autofs 13364 0 (autoclean) (unused) DS 8704 2 yenta_socket 13760 2 pcmcia_core 57184 0 [DS yenta_socket] tg3 55112 1 SG 36940 0 (autoclean) sr_mod 18104 0 (autoclean) microcode 4724 0 (autoclean) IDE-SCSI 12208 0 scsi_mod 108968 3 [SG sr_mod ide-SCSI] ide-CD 35680 0 CDROM 33696 0 [sr_mod ide-CD] nls_cp936 124988 1 (autoclean) nls_cp437 5148 1 (autoclean) vfat 13004 1 (autoclean) fat 38872 0 (autoclean) [vfat] keybdev 2976 0 (unused) mousedev 5524 1 hid 22212 0 (unused) input 5888 0 [keybdev mousedev hid] EHCI-HCD 20104 0 (unused) USB-uhci 26412 0 (unused) USB core 79392 1 [hid EHCI-hcd usb-uhci] ext3 91592 2 jbd 52336 2 [ext3]

The above shows the loaded modules in the current system. The first column on the left is the module name, the second column is the module size, and the third column is the number of modules used.

If unused is followed, it indicates that this module is not in use currently. If autoclean is followed, the module can be automatically cleaned by the rmmod-a command. The rmmod-a command uninstalls the autoclean module. If a module is not used at this time, it is marked as autoclean. If a module name exists in [] brackets at the end of a row, the module in brackets depends on this module. For example:

cdrom 34144 0 [sr_mod ide-cd]

The IDE-Cd and sr_mod modules depend on the CDROM module.

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. in the DEP file.

Note that this file not only writes the dependency between modules, but also the kernel search module is in this file. You can use the modprobe command to intelligently insert modules based on the dependency between modules, and/etc/modules. the content smart insertion module in the conf file. For example, if you want to load the drive of the IDE, run the following command:

# modprobe ide-cd

In this case, the CDROM module is automatically inserted.

Insmod is also a command for inserting modules, but it does not automatically resolve dependencies. Therefore, the command used to load kernel modules is modprobe.

Rmmod can delete modules, but it can only delete unused modules.

Modinfo is used to view module information, such as modinfo-d cdrom.
In Linux, the module commands are in the RPM package of modutils.

2. Device Files

After we load the device driver module, how can we 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 (network plug-in) files) type Device File. 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.

#ls -l /dev/hda /dev/video0 /dev/logbrw-rw---- 1 root disk 3, 0 Sep 15 2003 /dev/hdasrw-rw-rw- 1 root root 0 Jun 3 16:55 /dev/logcrw------- 1 root root 81, 0 Sep 15 2003 /dev/video0

The preceding three device files are displayed. Note that the block device is B, the character device is C, and the socket device is S.

It can be seen that all the device files are stored in the/dev directory. For example, the hard disk is represented by/dev/HD *, And/dev/hda indicates the primary device of the first ide interface, /dev/hda1 indicates the first partition on the first hard disk, while/dev/HDC indicates the master device of the second ide interface. Run the following command:

#dd if=/dev/hda of=/root/a.img bs=446 count=1

Import the MBR information of the first 446 bytes to the. imgfile.

For block and character devices, use the major and minor device numbers to describe the devices. The device ID indicates a driver. All devices controlled by the driver module of the same device have a common device ID, while the auxiliary device number is used to distinguish different devices under the controller, for example,/dev/hda1 (Block 3/1),/dev/hda2 (Block 3/2), And/dev/hda3 (block3/3) all represent three partitions of the same hard disk, their primary device numbers are all 3, and the secondary device numbers are respectively 1, 2, and 3. These device special files are created using the mknod command:

# mknod harddisk b 3 0

We create a file at the current location that is the same as/dev/hda and can access the master hard disk of the first ide device. The file name is harddisk.

Run the following command to view the device Number:

#file /dev/hda/dev/hda: block special (3/0)

Block indicates that/dev/hda is the system's block-type (Block-type) device file. Its primary device number is 3, and its secondary device number is 0.

#ls -l /dev/hda /dev/hdb brw-rw---- 1 root disk 3, 0 Sep 15 2003 /dev/hdabrw-rw---- 1 root disk 3, 64 Sep 15 2003 /dev/hdb

Using LS-l, you can also see the device number./dev/HDB indicates that the slave device (slave) of the First ide interface is also a block device, with the ID (3/64 ), another device file is/dev/tty *. Run the following command:

#echo "hello tty1" > /dev/tty1

Output the string "Hello tty1" to the first virtual console represented by/dev/tty1. Press "Alt + F1" to display the character on the screen, this special file represents our first virtual console.

#file /dev/tty1/dev/tty1: character special (4/1)

It can be seen from the above that its type is character (character type) device file, the main device number is 4, the auxiliary device number is 1. Similarly,/dev/tty2 represents the second virtual console, which is a character device and is numbered (4/2 ).

When/dev/CDROM is loaded to/mnt/CDROM, as long as the/mnt/CDROM system is accessed, it will be automatically introduced to the driver corresponding to/dev/CDROM, access the actual data.

For device file numbers, see kernel documentation/usr/src/linux-2. */documentation/devices.txt file (in the documentation directory after the source file of the kernel is unwrapped), which details the meaning of the various device file numbers.

3. Use the file 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 finds the value in the/proc directory to determine which driver modules are used to 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 specifically to access the kernel. For example, accessing the/proc/cpuinfo file is actually accessing the current CPU parameters, during each system startup, the system automatically loads the proc file system to the/proc directory through the information set in/etc/fstab:

# grep proc /etc/fstabnone /proc proc defaults 0 0

In addition, you can also use the mount command to manually load:

# mount -t proc none /proc

You can access or change kernel parameters through files in the/proc directory, and query driver information through the/proc directory. Next let's take a look at the information in the/proc directory:

# ls /proc1 4725 5032 5100 5248 5292 crypto kcore partitions14 4794 5044 5110 5250 5293 devices kmsg pci2 4810 5075 5122 5252 5295 dma ksyms self3 4820 5079 5132 5254 5345 driver loadavg slabinfo4 4831 5080 5151 5256 6 execdomains locks stat4316 4910 5081 5160 5258 7 fb lvm swaps4317 4912 5082 5170 5262 70 filesystems mdstat sys4318 4924 5083 5180 5271 8 fs meminfo sysrq-trigger4319 4950 5084 5189 5287 9 ide misc sysvipc4620 4963 5085 5232 5288 apm interrupts modules tty4676 5 5086 5242 5289 bus iomem mounts uptime4680 5005 5087 5244 5290 cmdline ioports mtrr version4706 5018 5088 5246 5291 cpuinfo irq net

You need to know that these files are virtual files generated in real time. accessing them is to access real data in the memory. The data is generated in real time. You can use the following command to view the specific file values:

# cat /proc/interruptsCPU00: 50662 XT-PIC timer1: 3 XT-PIC keyboard2: 0 XT-PIC cascade5: 618 XT-PIC ehci-hcd, eth18: 1 XT-PIC rtc9: 0 XT-PIC usb-uhci, usb-uhci11: 50 XT-PIC usb-uhci, eth012: 16 XT-PIC PS/2 Mouse14: 8009 XT-PIC ide015: 0 XT-PIC ide1NMI: 0ERR: 0

The comments of other files are shown in table 1.

Files in the/proc/sys directory can be directly changed, which is equivalent to directly changing the running parameters of the kernel, for example:

# echo 1 > /proc/sys/net/ipv4/ip_forward

The above Code enables the packet forwarding function in the kernel.

In addition, some commands are provided in Linux to query the status of the system. For example, free can view the current memory usage, and ide_info can view the information of the IDE device, for example: # ide_info/dev/had. Similar Commands include scsi_info to view information about SCSI devices. These commands are usually used to query files in the/proc directory and return results.

Driver Installation during system initialization

During Linux installation, the system hardware is detected. Based on the detected results, the installer determines which modules need to be loaded during boot. The installation program of Red Hat is Anaconda, which provides an automatic hardware detection and installation mechanism.

However, if some hardware in the computer does not have a default driver, for example, a scsicard, We can enter "Linux dd" at the boot prompt after startup. After loading the kernel, the system will automatically prompt you to insert the drive disk, and then you will have the opportunity to mount the Linux driver of the hardware.

If, during system installation, a certain hardware is always unable to drive normally due to interruption conflicts (ISA bus devices are common, such as an ISA Nic), or the driver is missing, enter "Linux noprobe" at the boot prompt ". In this mode, the installer does not automatically configure the found hardware. You can choose an existing driver, configure the driver parameters, or load the driver on a CD or floppy disk.

Custom boot disk

How does one load the driver when the system starts? Next let's take a look at how the Red Hat installation CD is guided. When the Linux installation CD is started, the Kernel File vmlinuz located in isolinux on the CD is loaded. After the kernel is run, the Virtual File System of initrd. IMG is loaded into the memory. This file is an image of the ext2 file system. After gzip compression, you can view the image content by following these steps:

# mount /mnt/cdrom# mkdir /mnt/imgdir# gunzip < /mnt/cdrom/isolinux/initrd.img > /ext2img# mount -t ext2 -o loop /ext2img /mnt/imgdir# cd /mnt/imgdir# ls -Fbin@dev/etc/linuxrc@lost+found/modules/proc/sbin/tmp/var/# cd modules# ls module-infomodules.cgzmodules.depmodules.pcimappcitable

Among them, modules. Dep is the module registration file, and there are dependencies between various modules. Modules. cgz is a cpio package file. The actual driver modules are in this file. Run the following command to unpackage:

# cpio -idmv < modules.cgz

The directory 2.4.21-4xxx is displayed. Go to the i386 directory under this directory and you will see the drivers supported in the current Boot Disk:

# ls 3c59x.o3w-xxxx.o8139cp.o8139too.o8390.oaacraid.oacenic.oaic79xx.o……

If you want to add the required driver to the system, you can modify these files accordingly, such as in modules. add the name and dependency of the module to Dep, and add the compiled driver module File to modules. cgz.

The System Boot Process on the hard disk is similar to the above, but the initrd image file is simpler, generally in the Virtual File System of the initrd-2.4.XXX.img, only in the/lib directory contains ext3.o jbd. O lvm-mod.o and other files, used to drive the ext3 File System on the hard disk. After the file system is loaded, you can use the modules. Dep file under/lib/modules/2.4.xxx/and various driver files in the kernel directory.

Copy search

Copy search

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.