Major and Minor number

 

Every Device in Linux is represented as a regular file. These files are called device files. These device files are located under /dev directory. If you execute ls -l command in /dev directory on Linux terminal you can see list of all the available devices in your system.
These devices are accessed by opening their respective device files. Each device file in Linux has two unique number associated with it. These two numbers are major number and minor number.
Major number is a unique number which specifies a particular driver. Every Device driver has their unique major number which helps kernel to identify the driver . When any application in user space open any device file , kernel check major number associated with that device file and identify which driver is responsible for handling this request. After that any subsequent calls for that particular device is passed to same driver.
A single device driver can handle multiple devices or controllers . For example a single USB driver handles multiple USB ports in your PC or same hard disk driver can manage multiple hard disk. So whenever any request for such kind of devices comes to kernel , kernel has to differentiate between devices or controllers. To differentiate between devices , every device in linux system provided a unique number which is called minor device number. Minor number for example COM port1 in our system has different minor number than COM port2. We can say that Minor number represents a device and major number specify a driver. but one thing is to note here is that Two or more device of same type can have same major number but not minor number.

major number and minor number

From above image we can see a driver which has major number 4 is handling three devices (device0, device1, device2) having minor number 3,4 and 5.
Internal representation of Major and minor numbers
Kernel use dev_t type which is defined in </linux/types.h> to hold major and minor number.size of dev_t is 32-bit in which 12 bits are used for major number and 20 bits are used for minor number.
Linux kernel also provide below two macros which can be used in our driver to find major number and minor number.
MAJOR(dev_t dev)
MINOR(dev_t dev)
These macros are defined in </linux/kdev_t.h>
Passing Major number in Registration of Driver
in Init function of driver when we register our driver by using of register_chrdev we pass major as one of the argument . below is the syntax of register_chrdev.
int register_chrdev(unsigned int major,const char *name,struct file_operations *fops)
We can choose any number which is not being used till now by any other driver as a major number. If we pass 0 as a major number then kernel will provide any available major number.
You can go through (===>) for having a look on the usage of major number in writing a device driver. In our example we used 90 as major number.

 

 

 

 

adana elektrikci

', 'auto'); ga('send', 'pageview');