Skip to contentSkip to conversation

Doubt regarding Minor Numbers and alloc_chrdev_region

DISCUSSION Sankar P 3 participants 5 messages Feb 8, 2011

lore
Share & tools
lore

Sankar P

author
#0 lore raw

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.

Signature · 2 lines
Sankar P
http://psankar.blogspot.com 
#1 lore raw
Quoted 2 lines

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.

You can create as much /dev/scull2 files using mknod as you want, but they are not linked with your driver.

[first_minor, count] pair passed to alloc_chrdev_region reserve for you a range of minors that will be later used by your driver.

Next step is to actually create the link between the node and your driver using chrdev_add.

Quoted 6 lines

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

Try to read/write from/to your created nodes and see what happens :d.

thanks,
Daniel.

#4 lore raw
In reply to Sankar P

_______________________________________
From: [email protected] [kernelnewbies-bounces at kernelnewbies.org] On Behalf Of Sankar P [sankar.curiosity at gmail.com]
Sent: Tuesday, February 08, 2011 4:26 PM
To: Kernelnewbies at kernelnewbies.org
Subject: Doubt regarding Minor Numbers and alloc_chrdev_region

Hi,

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

============================================

Quoted 8 lines

#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)

Quoted 8 lines
{
       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. */
Quoted 2 lines
      } else
              printk(KERN_ALERT "HelloWorldDriver could not get a Device number");
        return 0;
}

======================================

Quoted 13 lines

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.

Quoted 7 lines

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.

As far as i know ,

alloc_chardev_region () helps you register your character driver with the kernel using particular major number and minor number .. Thats pretty much of it. It's your responsibility to create a matching char device file in the user space .

When you perform any read / write operations on such a file then kernel first identifies ..Do I have any registrations of such a driver with __these__ major number and minor number. and correspondingly the read / write routines of your driver gets called.

In your case the any IO operations done on scull2 will not be mapped to any character driver in the kernel ..
You can try doing : cat /dev/scull2

Regards,
Smital Desai

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

______________________________________________________________________

The contents of this e-mail and any attachment(s) may contain confidential or privileged information for the intended recipient(s). Unintended recipients are prohibited from taking action on the basis of information in this e-mail and using or disseminating the information, and must notify the sender and delete it from their system. L&T Infotech will not accept responsibility or liability for the accuracy or completeness of, or the presence of any virus or disabling code in this e-mail"

______________________________________________________________________

author
#2 lore raw
In reply to Daniel Baluta
Quoted 13 lines · Daniel Baluta
Quoted 2 lines

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.

You can create as much /dev/scull2 files using mknod as you want, but they are not linked with your driver.

[first_minor, count] pair passed to alloc_chrdev_region reserve for you a range of minors that will be later used by your driver.

Next step is to actually create the link between the node and your driver using chrdev_add.

Ah okay. I understand this now. On reading a few more pages in the book, I see that the authors explain this phenomenon.

However, It seems using register_chrdev is an old way of doing things and new code should use cdev. I just did a grep for cdev on the latest sources and not much seem to be using the cdev* apis.

On a sidenote, I believe this whole scull section can be re-written in a way (in the LDD3 book I meant) such that there are some small milestones, which the reader can implement, understand and proceed further. Rightnow, it is too big and theoretical and difficult to visualize and needs us to read a lot of pages before we touch first line of code.

Thanks for all your help. I shall update once I make progress.

Signature · 5 lines
Sankar P
http://psankar.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110208/cd5c8e19/attachment.html 
#3 lore raw
Quoted 16 lines · Sankar P
Quoted 4 lines · Daniel Baluta
Quoted 3 lines

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.

Quoted 9 lines

You can create as much /dev/scull2 files using mknod as you want, but they are not linked with your driver.

[first_minor, count] pair passed to alloc_chrdev_region reserve for you a range of minors that will be later used by your driver.

Next step is to actually create the link between the node and your driver using chrdev_add.

Quoted 5 lines

Ah okay. I understand this now. On reading a few more pages in the book, I see that the authors explain this phenomenon.
However, It seems using register_chrdev is an old way of doing things and new code should use cdev. I just did a grep for cdev on the latest sources and not much seem to be using the cdev* apis.

The new way now is to find a class for a device and to use that class helpers.

Have a look at miscdev for a simple starter [1].

thanks,
Daniel.

[1] http://lxr.linux.no/linux+v2.6.37/include/linux/miscdevice.h