Allocating and registering a character device
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 tocdev_init
), and for each device you need to create, callcdev_init()
andcdev_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:
#define EEP_NBANK 8 #define EEP_DEVICE_NAME "eep-mem" #define EEP_CLASS "eep-class" struct class *eep_class; struct cdev eep_cdev...