RE: Doubt regarding Minor Numbers and alloc_chrdev_region

2011-02-08 Thread Smital Desai
___
From: kernelnewbies-boun...@kernelnewbies.org 
[kernelnewbies-boun...@kernelnewbies.org] On Behalf Of Sankar P 
[sankar.curios...@gmail.com]
Sent: Tuesday, February 08, 2011 4:26 PM
To: Kernelnewbies@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@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. LT 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

__

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Doubt regarding Minor Numbers and alloc_chrdev_region

2011-02-08 Thread Sankar P
On Tue, Feb 8, 2011 at 5:36 PM, Daniel Baluta daniel.bal...@gmail.comwrote:

  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.

-- 
Sankar P
http://psankar.blogspot.com
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies