FS/char_dev.c
/**
* _ Register_chrdev ()-create and register a cdev occupying a range of minors
* @ Major: Major device number or 0 for Dynamic Allocation
* @ Baseminor: First of the requested range of minor numbers
* @ Count: the number of minor numbers required
* @ Name: name of this range of devices
* @ Fops: file operations associated with this Devices
*
* If @ major = 0 this functions will dynamically allocate a major and return
* Its number.
*
* If @ major> 0 this function will attempt to reserve a device with the given
* Major number and will return zero on success.
*
* Returns a-ve errno on failure.
*
* The name of this device has nothing to do with the name of the device in
*/Dev. It only helps to keep track of the different owners of devices. If
* Your module name has only one type of devices it's OK to use e.g. The name
* Of the module here.
*/
Int _ register_chrdev (unsigned int major, unsigned int baseminor,
Unsigned int count, const char * Name,
Const struct file_operations * FoPs)
{
Struct char_device_struct * CD;
Struct cdev * cdev;
Int err =-enomem;
Cd = _ register_chrdev_region (Major, baseminor, Count, name );
If (is_err (CD ))
Return ptr_err (CD );
Cdev = cdev_alloc ();
If (! Cdev)
Goto out2;
Cdev-> owner = fops-> owner;
Cdev-> Ops = fops;
Kobject_set_name (& cdev-> kobj, "% s", name );
Err = cdev_add (cdev, mkdev (CD-> major, baseminor), count );
If (ERR)
Goto out;
CD-> cdev = cdev;
Return major? 0: CD-> Major;
Out:
Kobject_put (& cdev-> kobj );
Out2:
Kfree (_ unregister_chrdev_region (CD-> major, baseminor, count ));
Return err;
}