Linux character device driver

Source: Internet
Author: User

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  

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.