First character device driver

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/ruoyunliufeng/article/details/45054183


Linux drivers are divided into character devices, block device drivers, network drivers, three, which is the most simple character driven. Speaking to write drive naturally think of writing from the character device driver. Look at the Development Board of the official driver code, for the novice is simply a nightmare. It's not easy for a novice to read it. It contains a lot of knowledge and design ideas. So I think as far as possible from easy to difficult to write this series, believe me, I will try to tell everyone I know to clear.

First, the driver code

/* * This is the free software; Can redistribute it and/or modify * it under the terms of the GNU general public License version 2 as * published by T He free software Foundation. * */#include <linux/kernel.h> #include <linux/module.h> #include <linux/miscdevice.h> #include < linux/fs.h> #include <linux/types.h> #include <linux/moduleparam.h> #include <linux/slab.h># Include <linux/ioctl.h> #include <linux/cdev.h> #include <linux/delay.h> #include <linux/ device.h> #define HELLO_CNT 1//number of consecutive devices requested static int hello_major = 0; The default kernel automatically assigns the static int hello_minor = 0; Default this device number starts with 0 static struct Cdev hello_cdev;static struct class *hello_class;static int hello_open (struct inode *inode, str UCT file *file) {printk ("Hello open\n"); return 0;} static struct File_operations Hello_fops = {. Owner = This_module,.open = hello_open,};static int Hello_char_init (void) {de v_t devid;int err,result;/*1. Assigning the main device number */if (hello_major) {devid = MKDEV(Hello_major, hello_minor); result = Register_chrdev_region (devid,hello_cnt, "Hello");} Else{result = Alloc_chrdev_region (&devid,hello_minor,hello_cnt, "Hello"); hello_major = major (Devid);} if (Result < 0) {PRINTK (kern_warning "Hello:can ' t get major%d\n", hello_major); return result;} /*2. Registering character device driver */cdev_init (&hello_cdev, &hello_fops); err = Cdev_add (&hello_cdev, Devid, hello_cnt); if (err) PRINTK (kern_warning "Error%d adding Hello", err);/*3. Create Device */hello_class = Class_create (this_module, "Hello"); if (Is_err (Hello_class)) {PRINTK (kern_warning "Class_create () failed for hello_class\n");} Device_create (Hello_class, NULL, MKDEV (hello_major, 0), NULL, "Hello0"); /*/dev/hello0 */return 0;} static void Hello_char_exit (void) {Device_destroy (Hello_class, MKDEV (hello_major, 0)); Class_destroy (Hello_class); Cdev_del (&hello_cdev); Unregister_chrdev_region (MKDEV (hello_major, 0), hello_cnt);} Module_init (Hello_char_init); Module_exit (Hello_char_exit); Module_license ("GPL"); Module_author ("RuoyunliufeNg "); 


Second, the test procedure

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include < sys/stat.h> #include <fcntl.h>/*  * hello_test/dev/hello0 */void print_usage (char *file) {printf ("%s < dev>\n ", file);} int main (int argc, char **argv) {int fd;if (argc! = 2) {print_usage (argv[0]); return 0;} FD = open (argv[1], O_RDWR), if (FD < 0) printf ("Can ' t open%s\n", argv[1]); elseprintf ("Can open%s\n", argv[1]); return 0; }


Third, the driving explanation

1. Drive Inlet and outlet

In applications written in C, the entrance to our program is the main () function. The driver also has the entrance, that is Module_init (); the function inside the parentheses is the entry function, with the entrance naturally there is the exit module_exit (); Whenever Insmod Xxx.ko, the driver enters the entry function. The entry function mainly does some initialization work. Rmmod Xxx.ko, the driver will call the Exit function, the Export function group to do some logoff and cleanup work.


2. Character-driven initialization

The framework for character device initialization is basically the case, and can be used directly as a template, in three steps:

1. Assigned equipment number

The main device number can be defined by itself or given to the kernel to help you assign it, but it is generally recommended that the kernel is allocated, so there is an if statement to handle both cases separately.

2. Registering character drivers

You must register your driver to let the kernel know it. Call Cdev_init (); Cdev_add () two functions

3. Create a Device node

Here you choose the way to create automatically, you can also completely do not write in the driver, and then to manually create. First Class_create () and then Device_create (). This will make your device appear under the/dev/. This allows you to perform a series of read and write operations on your device.

3. How to call the driver

The application calls the Open function to invoke the Hello_open in the file_operations hello_fops that you write in the driver. The application and the driver establish a connection, and so are the other functions. The rest of the implementation is the same, such as the need to write data, the write function is added in the Hello_fops. The driver provides a mechanism that does not provide a policy. Policy is what the application should do. "What functionality is required" is the mechanism, "How to use these features" as a policy. Each function has its function, here to be careful not to write. Otherwise your driver will look like a lump.


Note: I do not have a detailed analysis of the parameters of each function, meaning. I think these things can be seen in the kernel itself, I do not want to turn my article into a translation kernel comments, documents. I would like to see my article you can understand the framework of the specific details of their own to see the kernel bar. Source code before, no secret.


Reference: LDD3

First character device driver

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.