Linux Driver starter Chapter (III): Basic character Device Module (2)

Source: Internet
Author: User

Tag: His error embedded too Owner file operation false Note location

The request and release of the device number is described in the previous section, and this section begins to learn about the operation of the character device.

Locate the <linux/cdev.h> file first, and view the interface that the kernel provides to the character device.

Cdev Structure
struct Cdev {struct Kobject kobj;//embedded kobject object struct module *owner;//This structure belongs to module const struct File_operations *ops;// File operation structure struct List_head list;//Universal doubly linked list dev_t dev;//device number unsigned int count;};

The owner member is generally initialized to this_module,this_module as a struct-module structure pointer to the current module, which refers to the current module.

function interfaces for character devices
void Cdev_init (struct Cdev *, const struct file_operations *), struct Cdev *cdev_alloc (void), int cdev_add (struct Cdev *, de v_t, unsigned); void Cdev_del (struct cdev *);

The above is a partial function interface provided by <linux/cdev.h>. The next one to settle them out.

The first is the Cdev_init function, first look at the source code.

void Cdev_init (struct cdev *cdev, const struct file_operations *fops) {memset (cdev, 0, sizeof *cdev); Init_list_head (& cdev->list); Kobject_init (&cdev->kobj, &ktype_cdev_default); cdev->ops = FoPs;}

The function of this function is to initialize a CDEV structure. The parameter cdev is the structure to be initialized, and the parameter fops is the file_operations of the device (the function is left behind).

You can see that the primary function of Cdev_init is to initialize the CDEV structure and connect the FoPs pointer to the CDEV structure. Be prepared to add this device to the system.

Next look at the Cdev_alloc function.

struct Cdev *cdev_alloc (void) {struct Cdev *p = kzalloc (sizeof (struct cdev), Gfp_kernel), if (p) {Init_list_head (&p-& gt;list); Kobject_init (&p->kobj, &ktype_cdev_dynamic);} return p;}

This function requests a piece of memory for the CDEV structure, returns its address, and returns null if it fails.

The next function is Cdev_add, which adds a character device to the system.

int cdev_add (struct Cdev *p, dev_t Dev, unsigned count) {int Error;p->dev = Dev;p->count = Count;error = Kobj_map (CD Ev_map, Dev, Count, NULL, Exact_match, Exact_lock, p), if (error) return Error;kobject_get (p->kobj.parent); return 0;} 

The parameter p is the struct Cdev pointer of the device, Dev is the first device number, and count is the number of successive secondary device numbers. The function describes the addition of the device by adding the pointer p to the system to make the device effective immediately. If the add fails, the error code for the negative number is returned.

The last function is Cdev_del, and its function is to remove a CDEV structure from the system.

void Cdev_del (struct Cdev *p) {cdev_unmap (P->dev, P->count); Kobject_put (&p->kobj);}

This function removes the pointer p from the system, and it is possible to release the structure that the P points to.

Registration and logoff of character devices

#include <linux/cdev.h> #include <linux/device.h> #include <linux/fs.h> #include <linux/init.h    > #include <linux/module.h> dev_t devno; Device number static struct class *my_class;static struct cdev my_cdev;static struct file_operations my_fops;static int __init Myc        Dev_init (void) {int ret;        ret = alloc_chrdev_region (&devno, 0, 1, "Mycdev");                if (ret! = 0) {PRINTK (kern_noteice "Alloc device number failed.");        return-1; }//begins implementation of Cdev_setup () Cdev_init (&my_cdev, &my_fops); my_cdev.owner = This_module;ret = Cdev_add (&my_cdev,                Devno, 1), if (Ret < 0) {PRINTK (kern_noteice "Add Cdev failed."); Return-2;}        Cdev_setup () End my_class = Class_create (This_module, "Mycdev");         Device_create (My_class, NULL, DEVNO, NULL, "Mycdev"); return 0;} static void Mycdev_exit (void) {//mycdev_del () implements Cdev_del (&my_cdev);//mycdev_del () End Device_destroy (My_class, Dev        NO); Class_dEstroy (My_class); Unregister_chrdev_region (DEVNO);} Module_init (Mycdev_init); Module_exit (Mycdev_exit); Module_license ("Dual BSD/GPL");

Now we have completed a basic character device module, which implements the application and logoff of the device number, the creation and destruction of the device file, and the initialization, registration and logoff of the character device.

  However, this is not enough. Our goal is to use a character device, at least to read or write this device. How to make the character device module provide read and write function? This is related to the struct file_operations structure and is left to the next section for detailed description.

Linux Driver starter Chapter (III): Basic character Device Module (2)

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.