Linux Device Drivers 2nd Edition (779877), страница 4
Текст из файла (страница 4)
Additionally, all the routing andaddress resolution issues are implemented within the kernel.Toward the end of this book, in Chapter 16, you’ll find a road map to the Linuxkernel, but these few paragraphs should suffice for now.One of the good features of Linux is the ability to extend at runtime the set of features offered by the kernel. This means that you can add functionality to the kernel while the system is up and running.Each piece of code that can be added to the kernel at runtime is called a module.The Linux kernel offers support for quite a few different types (or classes) of modules, including, but not limited to, device drivers.
Each module is made up ofobject code (not linked into a complete executable) that can be dynamically linkedto the running kernel by the insmod program and can be unlinked by the rmmodprogram.Figure 1-1 identifies different classes of modules in charge of specific tasks—amodule is said to belong to a specific class according to the functionality it offers.The placement of modules in Figure 1-1 covers the most important classes, but isfar from complete because more and more functionality in Linux is being modularized.Classes of Devices and ModulesThe Unix way of looking at devices distinguishes between three device types.Each module usually implements one of these types, and thus is classifiable as achar module, a block module, or a network module.
This division of modules intodifferent types, or classes, is not a rigid one; the programmer can choose to buildhuge modules implementing different drivers in a single chunk of code. Good programmers, nonetheless, usually create a different module for each new functionality they implement, because decomposition is a key element of scalability andextendability.The three classes are the following:Character devicesA character (char) device is one that can be accessed as a stream of bytes (likea file); a char driver is in charge of implementing this behavior. Such a driverusually implements at least the open, close, read, and write system calls.
The622 June 2001 16:32http://openlib.org.uaClasses of Devices and Modulestext console (/dev/console) and the serial ports (/dev/ttyS0 and friends) areexamples of char devices, as they are well represented by the stream abstraction. Char devices are accessed by means of filesystem nodes, such as/dev/tty1 and /dev/lp0. The only relevant difference between a char device anda regular file is that you can always move back and forth in the regular file,whereas most char devices are just data channels, which you can only accesssequentially.
There exist, nonetheless, char devices that look like data areas,and you can move back and forth in them; for instance, this usually applies toframe grabbers, where the applications can access the whole acquired imageusing mmap or lseek.Block devicesLike char devices, block devices are accessed by filesystem nodes in the /devdirectory. A block device is something that can host a filesystem, such as adisk. In most Unix systems, a block device can be accessed only as multiplesof a block, where a block is usually one kilobyte of data or another power of2. Linux allows the application to read and write a block device like a chardevice — it permits the transfer of any number of bytes at a time. As a result,block and char devices differ only in the way data is managed internally bythe kernel, and thus in the kernel/driver software interface.
Like a char device,each block device is accessed through a filesystem node and the differencebetween them is transparent to the user. A block driver offers the kernel thesame interface as a char driver, as well as an additional block-oriented interface that is invisible to the user or applications opening the /dev entry points.That block interface, though, is essential to be able to mount a filesystem.Network interfacesAny network transaction is made through an interface, that is, a device that isable to exchange data with other hosts.
Usually, an interface is a hardwaredevice, but it might also be a pure software device, like the loopback interface. A network interface is in charge of sending and receiving data packets,driven by the network subsystem of the kernel, without knowing how individual transactions map to the actual packets being transmitted. Though both Telnet and FTP connections are stream oriented, they transmit using the samedevice; the device doesn’t see the individual streams, but only the data packets.Not being a stream-oriented device, a network interface isn’t easily mapped toa node in the filesystem, as /dev/tty1 is.
The Unix way to provide access tointerfaces is still by assigning a unique name to them (such as eth0), but thatname doesn’t have a corresponding entry in the filesystem. Communicationbetween the kernel and a network device driver is completely different fromthat used with char and block drivers. Instead of read and write, the kernelcalls functions related to packet transmission.Other classes of driver modules exist in Linux. The modules in each class exploitpublic services the kernel offers to deal with specific types of devices.
Therefore,722 June 2001 16:32http://openlib.org.uaChapter 1: An Introduction to Device Driversone can talk of universal serial bus (USB) modules, serial modules, and so on. Themost common nonstandard class of devices is that of SCSI* drivers. Although everyperipheral connected to the SCSI bus appears in /dev as either a char device or ablock device, the internal organization of the software is different.Just as network interface cards provide the network subsystem with hardwarerelated functionality, so a SCSI controller provides the SCSI subsystem with accessto the actual interface cable. SCSI is a communication protocol between the computer and peripheral devices, and every SCSI device responds to the same protocol, independently of what controller board is plugged into the computer.
TheLinux kernel therefore embeds a SCSI implementation (i.e., the mapping of fileoperations to the SCSI communication protocol). The driver writer has to implement the mapping between the SCSI abstraction and the physical cable. This mapping depends on the SCSI controller and is independent of the devices attached tothe SCSI cable.Other classes of device drivers have been added to the kernel in recent times,including USB drivers, FireWire drivers, and I2O drivers. In the same way that theyhandled SCSI drivers, kernel developers collected class-wide features and exportedthem to driver implementers to avoid duplicating work and bugs, thus simplifyingand strengthening the process of writing such drivers.In addition to device drivers, other functionalities, both hardware and software,are modularized in the kernel.
Beyond device drivers, filesystems are perhaps themost important class of modules in the Linux system. A filesystem type determineshow information is organized on a block device in order to represent a tree ofdirectories and files. Such an entity is not a device driver, in that there’s no explicitdevice associated with the way the information is laid down; the filesystem type isinstead a software driver, because it maps the low-level data structures to higherlevel data structures.
It is the filesystem that determines how long a filename canbe and what information about each file is stored in a directory entry. The filesystem module must implement the lowest level of the system calls that access directories and files, by mapping filenames and paths (as well as other information,such as access modes) to data structures stored in data blocks. Such an interface iscompletely independent of the actual data transfer to and from the disk (or othermedium), which is accomplished by a block device driver.If you think of how strongly a Unix system depends on the underlying filesystem,you’ll realize that such a software concept is vital to system operation. The abilityto decode filesystem information stays at the lowest level of the kernel hierarchyand is of utmost importance; even if you write a block driver for your new CDROM, it is useless if you are not able to run ls or cp on the data it hosts.
Linuxsupports the concept of a filesystem module, whose software interface declaresthe different operations that can be performed on a filesystem inode, directory,* SCSI is an acronym for Small Computer Systems Interface; it is an established standard inthe workstation and high-end server market.822 June 2001 16:32http://openlib.org.uaSecurity Issuesfile, and superblock. It’s quite unusual for a programmer to actually need to writea filesystem module, because the official kernel already includes code for the mostimportant filesystem types.Security IssuesSecurity is an increasingly important concern in modern times. We will discusssecurity-related issues as they come up throughout the book.
There are a few general concepts, however, that are worth mentioning now.Security has two faces, which can be called deliberate and incidental. One securityproblem is the damage a user can cause through the misuse of existing programs,or by incidentally exploiting bugs; a different issue is what kind of (mis)functionality a programmer can deliberately implement.