Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between register_chrdev_region and alloc_chrdev_region to allocate device numbers?

I want to know the difference between these two functions:

int register_chrdev_region(dev_t first, unsigned int count, char *name);

int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);
like image 283
user1287763 Avatar asked Mar 23 '12 08:03

user1287763


People also ask

What does alloc_ chrdev_ region do?

Description. Allocates a range of char device numbers. The major number will be chosen dynamically, and returned (along with the first minor number) in dev . Returns zero or a negative error code.

What happens if you pass major number as 0 in Register_chrdev_region ()?

The return value from register_chrdev_region will be 0 if the allocation was successfully performed. In case of error, a negative error code will be returned, and you will not have access to the requested region. The dev_t type (defined in <linux/types.

What is Register_chrdev?

register_chrdev(major, name, fops); stores the given name (a string) and fops (a struct file_operations *) in the entry of the array chrdevs[] indexed by the integer major , the major device number of the device. (Devices have a number, the device number, a combination of major and minor device number.


1 Answers

See here for details on these two functions.

Registration is only really useful if you know in advance which major number you want to start with. With registration, you tell the kernel what device numbers you want (the start major/minor number and count) and it either gives them to you or not (depending on availability).

With allocation, you tell the kernel how many device numbers you need (the starting minor number and count) and it will find a starting major number for you, if one is available, of course.

Partially to avoid conflict with other device drivers, it's considered preferable to use the allocation function, which will dynamically allocate the device numbers for you.

From the link given above:

Some major device numbers are statically assigned to the most common devices. A list of those devices can be found in Documentation/devices.txt within the kernel source tree. The chances of a static number having already been assigned for the use of your new driver are small, however, and new numbers are not being assigned. So, as a driver writer, you have a choice: you can simply pick a number that appears to be unused, or you can allocate major numbers in a dynamic manner.

Picking a number may work as long as the only user of your driver is you; once your driver is more widely deployed, a randomly picked major number will lead to conflicts and trouble.

Thus, for new drivers, we strongly suggest that you use dynamic allocation to obtain your major device number, rather than choosing a number randomly from the ones that are currently free. In other words, your drivers should almost certainly be using alloc_chrdev_region rather than register_chrdev_region.

The disadvantage of dynamic assignment is that you can't create the device nodes in advance, because the major number assigned to your module will vary. For normal use of the driver, this is hardly a problem, because once the number has been assigned, you can read it from /proc/devices.

There's a related, but not technically duplicate, question here.

like image 92
paxdiablo Avatar answered Oct 13 '22 10:10

paxdiablo