Skip to main content.

5.3.3. Registering a character driver

API: Character driver registration

type:

struct cdev

allocate at runtime: (need to add fops by hand)

struct cdev* cdev_alloc(void);
...
my_cdev->ops = &my_fops;

allocate statically and only initalize at runtime:

void cdev_init (struct cdev* cdev, const struct file_operations *fops);

for both cases initalize owner field to THIS_MODULE:

my_cdev.owner = THIS_MODULE;

register the character driver with the kernel:

int cdev_add(struct cdev *dev, dev_t num, unsigned int count);

remove (and if necessary free memory):

void cdev_del(struct cdev *dev);

include/linux/cdev.h

old way (do not use anymore):

int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);
void unregister_chrdev(unsigned int major, const char *name);
5.3.2. Device numbers 1. Denx Training Topics 5.3.4. The open and release methods
Prev Home Next