1. In the character device driver module load function should implement the device number of the application and Cdev registration, and in the Unload function should implement the release of the device number and Cdev logoff.
1//Device Structural Body2 structxxx_dev_t3 {4 structCdev Cdev;5 ...6} Xxx_dev;7 //device driver module load function8 Static int_ _init Xxx_init (void)9 {Ten ... OneCdev_init (&xxx_dev.cdev, &xxx_fops);//Initialize Cdev AXxx_dev.cdev.owner =This_module; - //Get character device number - if(xxx_major) the { -Register_chrdev_region (Xxx_dev_no,1, dev_name); - } - Else + { -Alloc_chrdev_region (&xxx_dev_no,0,1, dev_name); + } A atret = Cdev_add (&xxx_dev.cdev, Xxx_dev_no,1);//Registering your device - ... - } - /*device driver module unload function*/ - Static void_ _exit Xxx_exit (void) - { inUnregister_chrdev_region (Xxx_dev_no,1);//release the occupied device number -Cdev_del (&xxx_dev.cdev);//Logout Device to ... +}
The member function in the 2.file_operations structure is the interface between the character device driver and the kernel, and the final implementation of the user space system call to Linux. Most character device drivers implement the Read (), write (), and IOCTL () functions.
1 struct file_operations xxx_fops =2 {3 . Owner = this_module,4 . Read = xxx_read,5 . write = xxx_write,6 . IOCTL = Xxx_ioctl,7 ... 8 };
The following is an explanation of the character device driver based on the virtual Globalmem device. Globalmem means "global memory", where the Globalmem character device driver allocates a space of globalmem_ size
, and provides read-write, control, and positioning functions for the memory in the driver, so that the process of user space can access the memory through the Linux system call.
Character device-driven composition