Doubt regarding Minor Numbers and alloc_chrdev_region

Smital Desai Smital.Desai at lntinfotech.com
Tue Feb 8 10:33:50 EST 2011


_______________________________________
From: kernelnewbies-bounces at kernelnewbies.org [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:

============================================
>#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.

  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"

______________________________________________________________________



More information about the Kernelnewbies mailing list