confused by char dev registration in a gpio driver

Robert P. J. Day rpjday at crashcourse.ca
Tue May 3 17:21:11 EDT 2011


  i'm sure i'm going to embarrass myself here but i was perusing the
char drivers for nice examples, and i ran across this excerpt in
pc8736x_gpio.c:

===== begin =====

        if (major) {
                devid = MKDEV(major, 0);
                rc = register_chrdev_region(devid, PC8736X_GPIO_CT, DEVNAME);
        } else {
                rc = alloc_chrdev_region(&devid, 0, PC8736X_GPIO_CT, DEVNAME);
                major = MAJOR(devid);
        }

        if (rc < 0) {
                dev_err(&pdev->dev, "register-chrdev failed: %d\n", rc);
                goto undo_request_region;
        }
        if (!major) {
                major = rc;
                dev_dbg(&pdev->dev, "got dynamic major %d\n", major);
        }

===== end =====

  i'm good with most of that -- if the (parameter) major is explicit,
then a dev_t of "devid" is created and register_chrdev_region() is
used.

  on the other hand, if major is zero, then alloc_chrdev_region() is
used for *dynamic* allocation of the major number.  in both cases, the
return code "rc" is saved and, if it's < 0, we have an error.  and
that's where the confusion comes in.

  i always thought both of those routines returned a simple zero to
indicate success.  but look at those last few lines -- that return
code is assigned to "major", at which point it's *that* value that's
printed.  wouldn't that just be zero all the time?

  and wouldn't it also print that this was a "dynamic" major even if
the user specified an explicit major number at load time?  this second
point is more nitpicky, but what about that first point?  wouldn't a
successful registration always print an allocated major number of
zero?

rday


-- 

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

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



More information about the Kernelnewbies mailing list