- inform kernel about what operations the driver provide for a device
-
struct file_operations
in include/linux/fs.h
- error handling (goto's, errno)
- carefull: driver can be called as soon as it has registered! (
cdev_add
)
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);
- cdev: the initalized struct cdev
- num: first device number of device
- count: number of minor numbers
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);