Character Device Drivers

Handling a character device is relatively easy, since usually sophisticated buffering strategies are not needed and disk caches are not involved. Of course, character devices differ in their requirements: some of them must implement a sophisticated communication protocol to drive the hardware device, while others just have to read a few values from a couple of I/O ports of the hardware devices. For instance, the device driver of a multiport serial card device (a hardware device offering many serial ports) is much more complicated than the device driver of a bus mouse.

A small complication, however, comes from the fact that the same major number might be allocated to several different device drivers. For instance, the major number 10 is used by many different device drivers, such as a real-time clock and a PS/2 mouse.

To keep track of which character device drivers are currently in use, the kernel uses a hash table indexed by the major and minor numbers.[97] The hash table array is stored in cdev_hashtable variable; it includes 64 lists of character device descriptors. Each descriptor is a char_device data structure, whose fields are shown in Table 13-10.

Table 13-10. The fields of the character device descriptor

Type

Field

Description

struct list_head

hash

Pointers for the hash table list

atomic_t

count

Usage counter for the character device descriptor

dev_t

dev

Major and minor numbers of the character device

atomic_t

openers ...

Get Understanding the Linux Kernel, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.