Doubt regarding Minor Numbers and alloc_chrdev_region

Sankar P sankar.curiosity at gmail.com
Tue Feb 8 05:56:56 EST 2011


Hi,

I am going through LDD3 (scull0 section) and have a doubt regarding
minor numbers. I have the following code-snippet:

============================================
#define FIRST_MINOR_NUMBER 0
#define COUNT_OF_CONTIGOUS_DEVICES 1

/* Device that we will use */
dev_t helloworld_device;

/* Initialization Function for the kernel module */
static int helloworld_driver_init(void)
{
        printk(KERN_INFO "Hello World");


	/* Allocate a dynamic device number for your driver. If you want to hardcode a fixed major number for your 
	 * driver, you should use different APIs 'register_chrdev_region', 'MKDEV'. But the following is better. */
	if (!alloc_chrdev_region(&helloworld_device, FIRST_MINOR_NUMBER, COUNT_OF_CONTIGOUS_DEVICES, "HelloWorldDriver")) {
		printk(KERN_INFO "Device Number successfully allocated.");

		/* If you do a cat /proc/devices you should be able to find our Driver registered. */
	
	} else
		printk(KERN_ALERT "HelloWorldDriver could not get a Device number");

        return 0;
}
======================================

Now if I compile and insert the .ko file for this module, the driver
gets registered with a major number of 250 (seen from /proc/devices). 

However, I don't understand the significance of the FIRST_MINOR_NUMBER
and the COUNT_OF_CONTIGOUS_DEVICES that we pass to the
alloc_chrdev_region function. 

If I try to create a file using, 'mknod /dev/scull2 c 250 7' , the
character device file gets created with a minor number of 7. 

I have set COUNT_OF_CONTIGOUS_DEVICES to 1. However, I am able to create
multiple files /dev/scull[1-10] using this driver and mknod. 

So, what is the significance of these two variables (first minor number
and count of devices) that we pass to the alloc_chrdev_region api, if
they can be over-ridden on mknod time ? Or have I misunderstood
something ?

Any help will be appreciated. Thanks.

-- 
Sankar P
http://psankar.blogspot.com 





More information about the Kernelnewbies mailing list