registering a character driver without cdev_init()/cdev_add()
continuing my perusal of various character drivers under drivers/char/ to find good examples for an intro class, i notice dsp56k.c, whose init routine contains the snippet: ===== start ===== if(register_chrdev(DSP56K_MAJOR, "dsp56k", &dsp56k_fops)) { printk("DSP56k driver: Unable to register driver\n"); return -ENODEV; } dsp56k_class = class_create(THIS_MODULE, "dsp56k"); if (IS_ERR(dsp56k_class)) { err = PTR_ERR(dsp56k_class); goto out_chrdev; } device_create(dsp56k_class, NULL, MKDEV(DSP56K_MAJOR, 0), NULL, "dsp56k"); printk(banner); goto out; out_chrdev: unregister_chrdev(DSP56K_MAJOR, "dsp56k"); out: return err; } ===== end ==== so rather than the canonical combination of cdev_init() and cdev_add(), this appears to register the pre-defined DSP56K major number, then goes straight to registering the driver with sysfs. is this now an alternative way of registering character drivers, if you want to register them with sysfs as well? as i read it, device_create() will create the /dev file so is this just a newer way to register character drivers? i don't think i've run across any other drivers that have this structure but, then again, i haven't been looking that hard. yet. rday -- ======================================================================== Robert P. J. Day Ottawa, Ontario, CANADA http://crashcourse.ca Twitter: http://twitter.com/rpjday LinkedIn: http://ca.linkedin.com/in/rpjday ========================================================================
so rather than the canonical combination of cdev_init() and cdev_add(), this appears to register the pre-defined DSP56K major number, then goes straight to registering the driver with sysfs.
Check register_chrdev code. [1] As you can see register_chrdev does cdev_alloc + cdev_add.
is this now an alternative way of registering character drivers, if you want to register them with sysfs as well? as i read it, device_create() will create the /dev file so is this just a newer way to register character drivers?
Yes, it is an alternative way of registering chrdev, but it doesn't have anything to do with sysfs layer. * device_create * creates a device object and link it into sysfs tree. * generates an event -> udev creates /dev/ file in userspace. * register_chrdev - reserves major/minor and sets up char device file operations.
i don't think i've run across any other drivers that have this structure but, then again, i haven't been looking that hard. yet.
thanks, Daniel. [1] http://lxr.linux.no/linux+v2.6.38/fs/char_dev.c#L264
On Wed, 4 May 2011, Daniel Baluta wrote:
so rather than the canonical combination of cdev_init() and cdev_add(), this appears to register the pre-defined DSP56K major number, then goes straight to registering the driver with sysfs.
Check register_chrdev code. [1] As you can see register_chrdev does cdev_alloc + cdev_add.
is this now an alternative way of registering character drivers, if you want to register them with sysfs as well? as i read it, device_create() will create the /dev file so is this just a newer way to register character drivers?
Yes, it is an alternative way of registering chrdev, but it doesn't have anything to do with sysfs layer.
* device_create * creates a device object and link it into sysfs tree. * generates an event -> udev creates /dev/ file in userspace. * register_chrdev - reserves major/minor and sets up char device file operations.
i don't think i've run across any other drivers that have this structure but, then again, i haven't been looking that hard. yet.
thanks, Daniel.
yes, shortly after i asked, i refreshed my memory and teased out most of the above, nice to have it confirmed, though. thanks. rday -- ======================================================================== Robert P. J. Day Ottawa, Ontario, CANADA http://crashcourse.ca Twitter: http://twitter.com/rpjday LinkedIn: http://ca.linkedin.com/in/rpjday ========================================================================
On Wed, May 04, 2011 at 06:55:17AM -0400, Robert P. J. Day wrote:
so rather than the canonical combination of cdev_init() and cdev_add(), this appears to register the pre-defined DSP56K major number, then goes straight to registering the driver with sysfs.
is this now an alternative way of registering character drivers, if you want to register them with sysfs as well? as i read it, device_create() will create the /dev file so is this just a newer way to register character drivers?
device_create will create the /dev file, but you have to have registered that major/minor number before calling it, it does not do the registering for you. thanks, greg k-h
participants (3)
-
Daniel Baluta -
Greg KH -
Robert P. J. Day