Character device-driven composition

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.