Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about register_chrdev_region() in linux device driver

I'm learning about the registration of a kernel module using register_chrdev_region(dev_t from, unsigned count, const char * name);.

I notice that with or without this function, my kernel module worked as expected. The code I used for testing:

first = MKDEV(MAJOR_NUM, MINOR_NUM);
register_chrdev_region(first, count, DEVICE_NAME);//<---with and without

mycdev=cdev_alloc();
mycdev->ops= &fops;
mycdev->owner = THIS_MODULE;

if (cdev_add(mycdev,first, count) == 0)
{printk(KERN_ALERT "driver loaded\n");}

I commented out the line register_chrdev_region(first, count, DEVICE_NAME);, and the printk message still appeared. I tried to communicate with the driver with or without this from user space, and both are successful.

So my question is, is this function register_chrdev_region() only used to make my driver a good kernel citizen, just like telling the others that "I'm using up the major number, please don't use"?

I tried to have a look in the kernel source char_dev.c to understand the function, but I find it too difficult to understand, anyone that's familiar with this?

Thanks!

like image 909
I'm a frog dragon Avatar asked Nov 24 '11 07:11

I'm a frog dragon


People also ask

What method does Linux use to load device drivers into the kernel?

Linux allows you to include device drivers at kernel build time via its configuration scripts. When these drivers are initialized at boot time they may not discover any hardware to control. Other drivers can be loaded as kernel modules when they are needed.

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 are the two types of drivers in Linux?

The Linux kernel supports two main types of USB drivers: drivers on a host system and drivers on a device. The USB drivers for a host system control the USB devices that are plugged into it, from the host's point of view (a common USB host is a desktop computer.)


2 Answers

That will work because it's not actually necessary to allocate your device numbers up front. In fact, it's considered preferable by many kernel developers to use the dynamic (on-the-fly, as-needed) allocation function alloc_chrdev_region.

Whether you do it statically up front or dynamically as needed, it is something you should do to avoid conflict with other device drivers which may have played by the rules and been allocated the numbers you're trying to use. Even if your driver works perfectly well without it, that won't necessarily be true on every machine or at any time in the future.

The rules are there for a reason and, especially with low-level stuff, you are well advised to follow them.

See here for more details on the set-up process.

like image 150
paxdiablo Avatar answered Oct 24 '22 15:10

paxdiablo


If the major number for your devices clash with any other device already in use, then the driver won't have the allocation done.

If you have already tested which major number is free and used it, it might generally not throw up an error and you will face no problem as u load the driver.

But if you run on various systems and if the major number is already captured and used by some other system., Then your driver loading can fail.

Its always better to use dynamic allocation !!

like image 42
Adit Ya Avatar answered Oct 24 '22 15:10

Adit Ya