This article records the driver framework for character devices:
1. Define the Cdev interface body and class structure body
#define hello_cnt 2staticint0; // The main device number is 0, you need to have the system automatically generate the main device number Static struct Cdev Hello_cdev; Static struct class *cls;
2. Construction of File_operations structural body
struct file_operations hello_fops = { = this_module, . Open = Hello_open,} ; Static int hello_open (structstruct file *file) { printk (" hello_open\n"); return 0 ;}
3. Registration
dev_t Devid;if(Major) {/*The main device number is determined*/Devid= MKDEV (Major,0); Register_chrdev_region (Devid, hello_cnt,"Hello");} Else { /*The main device number is 0, so the system automatically assigns us the main device number*/Alloc_chrdev_region (&devid,0, hello_cnt,"Hello"); Major=MAJOR (devid);} Cdev_init (&hello_cdev, &hello_fops); Cdev_add (&hello_cdev, Devid, hello_cnt);4. Create a Device node
cls = Class_create (this_module, hello );d Evice_create (CLS, NULL, MKDEV ( Major, 0 ), NULL, hello0 ); // device_create (CLS, NULL, MKDEV (Major, 1 ), NULL, hello1 ); // /dev/hello1