Device driver basics
Devices are treated as special files under the /dev directory. Hence, every device has a name and an associated inode or index node under its root file system. The file system entry point of every device contains major and minor numbers.
A major number is used to find this device uniquely in the system. It is an index into device switch table. Device switch table contains pointers to the device-specific methods that are essentially implementations of generic file system functions. In this manner, a file system call issued by a user process on a given device is resolved into a call to the appropriate device driver function. A minor number is used by the driver internally to distinguish between logical channels for multiplexed devices.
A major number can be obtained by a library call,
int genmajor(char* name_of_device_driver)
A minor number can be obtained by a library call,
It generates either the smallest unused minor number available for a device, a preferred minor number if it is available, or a set of unused minor numbers for a device.
int *genminor ( char * device_instance, /* Points to a character string containing the device instance name.*/ int major_no, /* The major number of the device instance.*/ int preferred_minor, /* Contains a single preferred minor number or a starting minor number for generating a set of numbers. */ int minors_in_grp, /* Indicates how many minor numbers are to be allocated. */ int inc_within_grp, /* Indicates the interval between minor numbers.*/ int inc_btwn_grp /* Indicates the interval between groups of minor numbers.*/ );
Major and minor numbers for a given device needs to be packaged into a 32- or 64-bit integer, depending on the architecture. This integer has a data type
makedev() is a macro that creates
dev_t from the given major and minor numbers. It is defined under
makedev64() is used to create
dev_t for a 64-bit environment.
dev_t makedev64(int major, int minor);
It is defined in
<sys/stat.h>. It is used to create an ordinary file, first-in-first-out (FIFO), or a special file. It requires root privilege to use
mknod() to create a device special file.
Syntax of mknod() is:
int mknod ( char *Path, /* Names the new device special file. */ int Mode, /* Specifies the file type, attributes, and access permissions. */ dev_t Device /* Device number generated by makedev() subroutine. */ );