Reputation: 131
I have a question about a char driver. A char driver using GPIO pins to communicate with a hardware device, including interrupt interfacing. The driver's "release ()" method is missing. What order should function elements put in?
A. Delete cdev and unregister device
B. Free GPIO Resources
C. freeing IRQ resource
D. Unregistrer major / minor number
In which order in the "release()" method?
Thanks
Upvotes: 2
Views: 1009
Reputation: 2944
Actually, some of these things may be done in the release()
function, and some of these things must be done in the module_exit()
function. It all depends on what you do where.
First, some terminology: module_init()
is called when the module is loaded with insmod. The opposite function is module_exit()
which is called when the module is unloaded with rmmod. open()
is called when a user process tries to open a device file with the open()
system call, and the opposite function release()
is called when the process that opened the device file (as well as all processes that were branched from that original process) call the close()
system call on the file descriptor.
The module_exit() function is the opposite of the module_init() function. Assuming you are using the CDev API, in the module init function you must register a major / minor numbers (D) first with alloc_chrdev_region()
or register_chrdev_region()
before adding the cdev to the system with cdev_init()
and then cdev_add()
.
It stands to reason that when module_exit()
is called, you should undo what you did in the reverse order; i.e. remove the cdev first with cdev_del()
and then unregister the major/minor number with unregister_chrdev_region()
.
At some point in the module_init()
function you may request the GPIO resources with request_mem_region()
& ioremap()
, and then the IRQ resource with request_irq()
. On the other hand you may request the GPIO resources and the IRQ resource in the open()
function instead.
If you request these resources in the module_init()
function, then you should release these resources in the module_exit()
function. However, if you do it in open()
then you should keep track of how many processes have the device file open, and when all of them have released the device file, release the resources in the release()
function.
Again, whatever order you requested the resources in, in general you should release the resources in the opposite order. I will say however, that almost always it is incorrect to release the memory resources (in your case the GPIO resources) before releasing the IRQ resource, since the IRQ will most likely want to talk to the hardware, either in the top half or the bottom half handler.
In summary, the order depends on how you implemented the driver to request the resources in the first place, however, if you implement your drivers like I do, then in general, perform C then B in release()
, and perform A then D in module_exit()
.
Upvotes: 0
Reputation: 1763
As per my understanding correct order looks like C, B, A and D :-). Explanation: Need to free the IRQ since gpio pin (used as an interrupt pin), IRQ number is got from passing this gpio pin to gpio_to_irq and after this only you can go ahead in freeing up the gpio stuff. After that deletion of cdev come into picture to which file operations, device node info(dev_t, 32bit unsigned integer. In which 12 bit is used for major no and remaining 20 bit is used for minor no) and minor number info (minor no start value and how many minor no's asked for) are associated. At-last go ahead and unregister the driver.
Upvotes: 1