perhaps confused about operation of alloc_chrdev_region()

Robert P. J. Day rpjday at crashcourse.ca
Thu Apr 21 15:29:31 EDT 2011


  i'm in the midst of updating some basic kernel documentation and i
just ran across something that makes me question my understanding of
the alloc_chrdev_region() kernel routine.  as i've understood it all
this time, that routine is the preferred routine if you want to
allocate some character devices *dynamically* -- that is, to accept
whatever major number(s) the kernel is willing to hand back.  with
that routine, you're not supposed to *specify* a major number that you
want, you just accept what you're given.  at least that's what i
always thought.

  i was perusing the kernel source looking for a simple driver example
that used that routine, and found drivers/char/bsr.c, but the init
routine looks weird, as in:

=====

static int __init bsr_init(void)
{
        struct device_node *np;
        dev_t bsr_dev = MKDEV(bsr_major, 0);   <--- ????????
        int ret = -ENODEV;
        int result;

        np = of_find_compatible_node(NULL, NULL, "ibm,bsr");
        if (!np)
                goto out_err;

        bsr_class = class_create(THIS_MODULE, "bsr");
        if (IS_ERR(bsr_class)) {
                printk(KERN_ERR "class_create() failed for bsr_class\n");
                goto out_err_1;
        }
        bsr_class->dev_attrs = bsr_dev_attrs;

        result = alloc_chrdev_region(&bsr_dev, 0, BSR_MAX_DEVS, "bsr");
        bsr_major = MAJOR(bsr_dev);

=====

  note that early declaration of "dev_t bsr_dev".  why is it being
initialized to what is an uninitialized static variable much earlier
in that source file?  the value of "bsr_major" certainly isn't used
until it's *assigned* in the call:

        result = alloc_chrdev_region(&bsr_dev, 0, BSR_MAX_DEVS, "bsr");
        bsr_major = MAJOR(bsr_dev);

  as best as i can tell, that initial declaration can simply be:

  dev_t bsr_dev;

i see no reason to initialize it to anything since
alloc_chrdev_region() is supposed to treat the dev_t parameter as
output only, is it not?

  am i missing something here?

rday

-- 

========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Tw
itter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================



More information about the Kernelnewbies mailing list