Top 15 Linux Device Drivers Interview Questions You Must Prepare 29.Nov.2023

The minor number is used only by the driver itself to differentiate which device it's operating on, just in case the driver handles more than one device.

(or)

one driver can control more than one device .minor will be used to distinguish the one device from other devices   .

mknod is a command  which used create the device file (or) node in Linux file system.

In unix or linux we will represent everything as a file .

syntax: mknod Name { b | c }  Major  Minor

Name : name of the device file

{ b | c } : type of device (ex; char or block device)

Major  : Major number of the device file

Minor  : Minor number of the device file

ex : $  mknod /dev/rama  c  12  5

MKDEV(int major, int minor);

It's an  integer number  mainly used to provide the association between the device driver and device file . this number is used by kernel .

                (or)

The major number tells you which driver handles which device file. 

if you have the major and minor numbers and need to turn them into a dev_t, use:

register_chrdev_region works well if you know ahead of time exactly which device numbers you want. Often, however, you will not know which major numbers your device will use; there is a constant effort within the Linux kernel development community to move over to the use of dynamically-allocated device numbers.

allocation : init function of a module

freeing : cleanup function of a module

register_chrdev_region() function will statically allocate device numbers. which is declared in <linux/fs.h>

int register_chrdev_region(dev_t first, unsigned int count, char *name);

Return values : In case of success "0" will return , In case of failure  "-1 or negative value " will return 

Here

  1. first is the beginning device number of the range you would like to allocate. The minor number portion of first is often @
  2. count is the total number of contiguous device numbers you are requesting.
  3. name is the name of the device that should be associated with this number range. it will appear in /proc/devices and sysfs.

In 2 ways we can allocate device numbers

  1. statically
  2. dynamically

void unregister_chrdev_region(dev_t first, unsigned int count);

To obtain the major or minor number  of a dev_t, use:

MAJOR(dev_t dev); // to obtain major number

MINOR(dev_t dev);  // to obtain minor number

int major=MAJOR(dev_t dev);

int minor =MINOR(dev_t dev);

alloc_chrdev_region()will dynamically  allocate device numbers.

int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, 

                        unsigned int count, char *name);

Here

  1. dev is an output-only parameter that will, on successful completion, hold the first number in your allocated range. 
  2. firstminor should be the requested first minor number to use; it is usually 0
  3. count is the total number of contiguous device numbers you are requesting.
  4. name is the name of the device that should be associated with this number range. it will appear in /proc/devices and sysfs.

Some major device numbers are statically assigned to the most common devices. A list of those devices can be found in Documentation/devices.txt within the kernel source tree.

The disadvantage of dynamic assignment is that you can't create the device nodes in advance, because the major number assigned to your module will vary.

This is used to hold device numbers—both the major and minor parts.