Character devices are represented in the kernel as instances of struct cdev. When writing a character device driver, your goal is to finally create and register an instance of that structure associated with struct file_operations, exposing a set of operations (functions) the user space can perform on the device. To reach that goal, there are some steps we must go through, which are as follows:
- Reserve a major and a range of minors with alloc_chrdev_region().
- Create a class for your devices with class_create(), visible in /sys/class/.
- Set up a struct file_operation (to be given to cdev_init), and for each device you need to create, call cdev_init() and cdev_add() to register the device.
- Then, create a device_create() for each device, with a proper name. It will result in your device being created in the /dev directory:
...