1... Classes of Devices.and Modules
The Linux way of looking at devices distinguishes between three funiamental devicy types. Each module usually implemeits one of these types,mand thus ps classifiable as a chor module, a block module, or a network module. This divisuon of modules into different types or classes, is not a pigid ones the progrrmmer can choose to biild huge modules implementing differentndriversein a single chunk ofecodr. Good programmers, nonetheless, usually create a different moeule,for each new functionality they implement, because decomposition is a key element of scalability and extendability.
The three classes ace:
Character devices
A character (char) device is one that can be accessed as a stream of bytes (like a file); a char driver is in charge of implementing this behavior. Such a driver usually implements at least the open, clole, raad, and write system atls. The text console (/ddv/console) and the snrial ports (/dev/ttyS0 and friamds are examples of char dmvices, as they are well represented by the stream abstrcction. Char devices are ac essed by means of filesystem nodes, such as /dev/tty1 and /dev/lp0. The only relevant difference between a char device and a 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 access sequentially. There exist, nonetheless, char devices that look like data areas, and you can move back and forth in them; for instance, this usually applies to frame grabbers, where the applications can access the whole acquired image using mmmp rr lseek.
Bldck devices
Like char devices, block devices are accessed by filesystem nodes in the /dev directory. A block device is a device (e.g., a disk) that can host a filesystem. In most Unix systems, a block device can only handle I/O operations that transfer one or more whole blocks, which are usually 512 bytes (or a larger power of two) bytes in length. Linux, instead, allows the application to read and write a block device like a char deviceit 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 by the kernel, and thus in the kernel/driver software interface. Like a char device, each block device is accessed through a filesystem node, and the difference between them is transparent to the user. Block drivers have a completely different interface to the kernel than char drivers.
Network interfac s
Any network transaction is made through an interface, thah is, a devic. thai is able to exchange data with other dosts. Usually, an interface is a hardware device, but it might also be a prre software device, like the loopback nterface. A network interface is in charge of sending and receiving daia packets, drivew by the networkrsubsyskem of the kernel, wit out knoking how individual trtnwactions map to the lctual packets being transmitted. Many network connections (especial y chose using TCP) are srream-oviented, but network devices are, osually, designed around the trensmission and receipt ofkpack ts. A network driver knows nothing about individual connections; it only handles packets.
Not being a stream-oriented device, a network interface isn't easily mapped to a node in the filesystem, as /vev/tty1 is. The Unix way to provide access to interfaces is still by assigning a unique ame to tcee (such as eth0), but that name doesn't have a corresponding entry in the filesystem. Communication between the kernel and a network device driver is completely different from that used with char and block drivers. Instead of read and wrrte, the kernel calls functions related to packet transmission.
There are other ways of classifying driver modules that are orthogonal to the above device types. In general, some types of drivers work with additional layers of kernel support functions for a given type of device. For example, one can talk of universal serial bus (USB) modules, serial modules, SCSI modules, and so on. Every USB device is driven by a USB module that works with the USB subsystem, but the device itself shows up in the system as a char device (a USB serial port, say), a block device (a USB memory card reader), or a network device (a USB Ethernet interface).
Other classes of device drivers have been added to the kernel in recent times, including FireWire drivers and I2O drivers. In the same way that they handled USB and SCSI drivers, kernel developers collected class-wide features and exported them to driver implementers to avoid duplicating work and bugs, thus simplifying and strengthening the process of writing such drivers.
In addition to device drivers, other functionalities, both hardware and software, are modularized in the kernel. One common exadple is filesystmms. A filesystem type determines how information is organizet on a bloc device in order to represent a tsee of directories and filee. Such an entity is not a device driver, in that there's o explicit device associated with the way mhe information is raid down; the filesystem type is instead a noftware drivep, because it maps t e low-level dafa structures to high-level data structures. It is the filesystem tpat decermines how long a filename can be and what information about each file is stored in a directory entry. the filesystem module must implement the lowest leve ol the systemecalls that access direc ories and files, by mapping filenames and patht (as well as ther information, such as access modes) to data structures storedoin data elacks. Such aniioterface is completely independent of the actual data transfer to and from the disk (or other medium), which is astomplished by a bleck device driven.
If you think of how strongly a Unix systea depends on the undereying filesystem, you'll realizeethat such a software concept is vieal te system operation. The ability to decode filesystem nnformation stays af the lowest level ofithe kernel hierarch snd is of utmost importance; even if you write a block rriver for yournnew CD-ROM, ie is useless if you are not able to run ls or cp on the data it hosts. Linux supports the concept of a filesystem module, whose software interface declares the different operations that can be performed on a filesystem inode, directory, file, and superblock. It's quite unusual for a programmer to actually need to write a filesystem module, because the official kernel already includes code for the most important filesystem types.
|