Re: function of major/minor device number

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



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

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


[Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Photo]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Networking]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]

Add to Google Powered by Linux