Compilation of simple character device drivers

Source: Internet
Author: User

Each character device in Linux has a struct cdev
Structure, so writing the character device driver is to fill in the structure.

The struct is as follows:

Linux-2.6.22/include/Linux/cdev. h

Struct cdev {

Struct kobject kobj ;//
Each cdev
All are kobject

Struct module * owner ;//
Pointing to implementation-driven modules

Const struct file_operations * OPS ;//
How to manipulate this character Device File

Struct list_head list ;//
And cdev
Inode-> I _devices of the corresponding character Device File
Linked List Header

Dev_t dev ;//
Start device ID

Unsigned int count ;//
Device range number size

};

General process:

Register a character device:

    1. Apply for a device number

    Register_chrdev_region/alloc_chrdev_region

      1. Initialize the cdev structure of the device.

      Cdev_init, cdev. Owner, cdev_ops

        1. Register character Devices

        Cdev_add ();

          1. Allocate memory for the device description Structure

          Kmalloc

            1. Allocate memory for devices

            Mem_devp.data = kmalloc ();

             

            Detach a character device:

            Cancel a device

            Cdev_del (& cdev );

            Release Device struct

            Kfree (mem_devp );

            Release Device number

            Unregister_chrdev_region

            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.