Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between cdev_alloc and cdev_init

I'm creating a character device. I found two way to initialize char device

cdev_alloc

and

cdev_init

According to book, if i'm embedding struct cdev in my device struct then i should use cdev_init

Can any one tell me that what are difference between them?

like image 527
Kumar Gaurav Avatar asked Dec 26 '13 11:12

Kumar Gaurav


1 Answers

you can use either:

struct cdev my_cdev;

in this case you don't need to call cdev_alloc because memory is already allocated. Instead you must call cdev_init(&my_cdev, &fops). and then my_cdev.owner = THIS_MODULE;

OR

you can use:

struct cdev *my_cdev_p;

in this case you must call cdev_alloc() to allocate memory. Then, you have to initialize my_cdev_p->ops=&fops; and my_cdev_p->owner = THIS_MODULE;. Never use cdev_init() in this case!

Note that the 2 above methods don't belong to the old mechanism.

like image 125
Mahmoud Morsy Avatar answered Oct 02 '22 22:10

Mahmoud Morsy