Device Drivers & Kernel Modules

the major and minor number

When a program opens /dev/sda or /dev/ttyS1, the filename is just a label for humans. The kernel needs a numeric way to know which driver owns this device and which specific instance it is. That is what the major and minor numbers are: a two-part numeric ID stamped on every device node. The major number says 'which driver,' and the minor number says 'which device that driver handles.'

Picture a building directory: the major number is the department (say, 'the serial port driver'), and the minor number is the specific office within it (serial port 0, serial port 1, serial port 2). When a device node is created, it carries this pair, packed into a single value called a dev_t. When you open the node, the kernel reads the major number to look up the registered driver in its table, then passes the minor number to that driver so it can tell its devices apart — one serial driver might handle eight ports, each a different minor. A driver claims a range of these numbers when it registers (historically with a fixed major; modern drivers usually ask the kernel to allocate one dynamically with alloc_chrdev_region).

These numbers matter because they are the actual link between a name in /dev and code in the kernel — the node's name is cosmetic, but the numbers are load-bearing. A common confusion: the filename does not have to match anything; you could create a node called /dev/banana with the right major and minor and it would still reach the serial driver. What matters is the numbers, not the name. You can see them with ls -l /dev, where they appear in place of the file size.

$ ls -l /dev/ttyS0 /dev/sda crw-rw---- 1 root tty 4, 64 ... /dev/ttyS0 # char, major 4 minor 64 brw-rw---- 1 root disk 8, 0 ... /dev/sda # block, major 8 minor 0 // leading c = character device, b = block device

The two numbers replace the file size in ls -l; the leading c/b marks character versus block.

The node's filename is cosmetic; only the major/minor numbers route to a driver. Modern drivers usually request a dynamically allocated major rather than hard-coding one to avoid collisions.

Also called
device numbersdev_t裝置編號