perhaps confused about operation of alloc_chrdev_region()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

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


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux