function of major/minor device number
Hi, all I have a question about major/minor device number. I know that one can use int register_chrdev_region(dev_t from, unsigned count, const char *name); to register a driver with a unique pair of major/minor device number. But what's the difference between two drivers: in one case they have same major device number but different minor device number; on another case they have different major device number but same minor device number. When I searched the Internet, it said that major device number is to identify the driver while the minor device number is to identify specific hardware. But I'm still confused because it seems legal to share the major device number with different drivers. Please help me!Thank you!
2012/2/12 hz hanks <hankshz@gmail.com>:
Hi, all
I have a question about major/minor device number. I know that one can use int register_chrdev_region(dev_t from, unsigned count, const char *name); to register a driver with a unique pair of major/minor device number. But what's the difference between two drivers: in one case they have same major device number but different minor device number; on another case they have different major device number but same minor device number. When I searched the Internet, it said that major device number is to identify the driver while the minor device number is to identify specific hardware. But I'm still confused because it seems legal to share the major device number with different drivers.
Please help me!Thank you!
Hello! I would redirect you to the /dev fs to do a long listing of its contents and notice the pattern. Devices with the same major, are usually handled by the same device driver. If that device driver needs to handle several devices, it uses an array of minors. Example: crw-rw---- 1 root dialout 4, 64 2011-12-12 16:25 ttyS0 crw-rw---- 1 root dialout 4, 65 2011-12-12 16:25 ttyS1 crw-rw---- 1 root dialout 4, 66 2011-12-12 16:25 ttyS2 crw-rw---- 1 root dialout 4, 67 2011-12-12 16:25 ttyS3 Notice that all the Serial ports have the same major (4) but different minors (64-67). If you do a "cat /proc/devices" you will notice that the major 4 is registered by ttyS device driver. "ttyS" is the name variable in the register_chrdev_region call. A device driver can register zero, one ore more majors. And for each of that major, it can a number of minors (the count parameter in register_chrdev_region). Two devices having the same minor but different majors is just a coincidence, they have nothing in common. It's just an obvious thing because a major has more minors so there will be more minors that majors. Hope this helps. -- Alexandru Juncu ROSEdu http://rosedu.org
On Sun, 12 Feb 2012, Alexandru Juncu wrote:
2012/2/12 hz hanks <hankshz@gmail.com>:
Hi, all
I have a question about major/minor device number. I know that one can use int register_chrdev_region(dev_t from, unsigned count, const char *name); to register a driver with a unique pair of major/minor device number. But what's the difference between two drivers: in one case they have same major device number but different minor device number; on another case they have different major device number but same minor device number. When I searched the Internet, it said that major device number is to identify the driver while the minor device number is to identify specific hardware. But I'm still confused because it seems legal to share the major device number with different drivers.
you're talking about the "misc" character device driver here, which allows you to register very simple and independent character drivers that need only a single minor number all at major number 10. look in /dev for how many char device files have major number 10. that's the perfect example of what you're talking about. see the source in drivers/char/misc.c. rday -- ======================================================================== Robert P. J. Day Ottawa, Ontario, CANADA http://crashcourse.ca Twitter: http://twitter.com/rpjday LinkedIn: http://ca.linkedin.com/in/rpjday ========================================================================
Hi, rday Thank you for your help. Yes! You did point out my question. I will do read drivers/char/misc.c for a better understanding. Best, 2012/2/12 Robert P. J. Day <rpjday@crashcourse.ca>:
On Sun, 12 Feb 2012, Alexandru Juncu wrote:
2012/2/12 hz hanks <hankshz@gmail.com>:
Hi, all
I have a question about major/minor device number. I know that one can use int register_chrdev_region(dev_t from, unsigned count, const char *name); to register a driver with a unique pair of major/minor device number. But what's the difference between two drivers: in one case they have same major device number but different minor device number; on another case they have different major device number but same minor device number. When I searched the Internet, it said that major device number is to identify the driver while the minor device number is to identify specific hardware. But I'm still confused because it seems legal to share the major device number with different drivers.
you're talking about the "misc" character device driver here, which allows you to register very simple and independent character drivers that need only a single minor number all at major number 10.
look in /dev for how many char device files have major number 10. that's the perfect example of what you're talking about. see the source in drivers/char/misc.c.
rday
--
======================================================================== Robert P. J. Day Ottawa, Ontario, CANADA http://crashcourse.ca
Twitter: http://twitter.com/rpjday LinkedIn: http://ca.linkedin.com/in/rpjday ========================================================================
On Sun, 12 Feb 2012, hz hanks wrote:
Hi, rday
Thank you for your help. Yes! You did point out my question. I will do read drivers/char/misc.c for a better understanding.
as part of my kernel programming course, i show how to allocate and register a simple char misc device: ===== start ===== #include <linux/module.h> #include <linux/miscdevice.h> #include <linux/fs.h> #include <linux/uaccess.h> static ssize_t read(struct file *file, char __user *user, size_t size, loff_t *o) { return 0; } static ssize_t write(struct file *file, const char __user *in, size_t size, loff_t *off) { return 0; } static struct file_operations mymisc_fops = { .owner = THIS_MODULE, .write = write, .read = read, }; static struct miscdevice mymisc_dev = { .minor = MISC_DYNAMIC_MINOR, .name = "mymisc", .fops = &mymisc_fops, }; int __init mymisc_device_init(void) { return misc_register(&mymisc_dev); } void __exit mymisc_device_remove(void) { misc_deregister(&mymisc_dev); } module_init(mymisc_device_init); module_exit(mymisc_device_remove); MODULE_LICENSE("Dual BSD/GPL"); ===== end ===== when you register a misc device, it will automatically be given a major number of 10 and any available minor number. you can check afterwards the contents of /proc/misc in userspace what minor number you got. i leave as an exercise for the reader to examine the kernel source file drivers/char/misc.c to see how numerous independent drivers can all share the single driver at major number 10. and also to fill in the read and write routines. rday -- ======================================================================== Robert P. J. Day Ottawa, Ontario, CANADA http://crashcourse.ca Twitter: http://twitter.com/rpjday LinkedIn: http://ca.linkedin.com/in/rpjday ========================================================================
participants (3)
-
Alexandru Juncu -
hz hanks -
Robert P. J. Day