Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 6: Device Drivers


The first problem can be eliminated by revising the driver, but the second is of a more fundamental
nature. To come to terms with the new situation, the kernel uses the division of the userspace-visible
device number data typeu32shown in Figure 6-4.

❑ In the kernel itself the bit range 0 — 19, that is, 20 bits are used for the minor number. This leaves
12 bits in the range 20 — 31 for the major number.
❑ When it is necessary to represent adev_texternally, the 8 bits from the range 0 — 7 are used for
the first part of the minor number, the next 12 bits (range 8 — 19) for the major number, and the
last 12 bits (range 20 — 31) for the missing part of the minor number.
The old layout consists of 16 bits evenly split between major and minor numbers. If both major
and minor are less than 255, the new representation is compatible with the old version.

Code that sticks to these functions to convert betweendev_tand external representations will require no
changes should the internal data type be changed once more in the future.

Old representation
0

0

7

7

15

19 31

01931

External
representation

Internal representation

Minor

Major

Figure 6-4: Division of a device number into major and minor parts.

The advantage of this split is that the first 16 bits of the data structure can be interpreted as an old device
specification; this is important for reasons of compatibility.

The kernel provides the functions/macros listed below (and defined in<kdev_t.h>) to extract informa-
tion from theu32representation and convert betweenu32anddev_t.

❑ MAJORandMINORextract the major and minor number, respectively, from adev_t.
❑ MKDEV(major, minor)generates adev_ttype from the major and minor numbers.
❑ new_encode_devconvertsdev_ttou32in the external representation mentioned above.
❑ new_decode_devconverts the external representation todev_t.
❑ old_encode_devandold_decode_devswitches between a number of typeu16,thatis,theold
representation, and the moderndev_trepresentation.

The prototypes are as follows.

<kdev_t.h>
u16 old_encode_dev(dev_t dev);
dev_t old_decode_dev(u16 val);
u32 new_encode_dev(dev_t dev);
dev_t new_decode_dev(u32 dev);
Free download pdf