The Register_chrdev and register_chrdev_region of character devices

Source: Internet
Author: User

Before the write character device driver, are used Register_chrdev to the kernel to register the driver built in the file_operations structure, and then create a device file, as long as the main device number is the same (different secondary device number), the binding is the same file_ Operations struct, the application uses a function that is registered in this struct. This is a disadvantage: the same type of character devices (that is, the main device number is the same), will be registered in the kernel 256 (the analysis kernel code), that is, so the device number will be occupied, and in most cases will not use so many times the device number, so it will cause great resource waste. So Register_chrdev at some point is a disadvantage, which is also used in the old version of the kernel.

The registration of Register_chrdev is also divided into static registration and dynamic registration. Register_chrdev_region and alloc_chrdev_region are equivalent to splitting the Register_chrdev, which are static and dynamically registered individuals, but also solve the register_ Chrdev the disadvantages of wasting resources.

Register_chrdev_region (dev_t from, unsigned count, const char * name)

Analyze the function from the parameters:

From: The starting value of the range of device numbers to assign.

Count: The number of consecutive device numbers required.

Name: The device name associated with the number range.

Register_chrdev_region allows the registration of a specified device number range, it is not necessarily the 0~255 of this device number is registered occupancy.

In the kernel after 2.6, a struct CDEV structure is used to describe a character device.

struct Cdev {struct Kobject kobj;struct module *owner;const struct file_operations *ops;struct list_head list;dev_t Dev;un signed int count;};

Here are a few more functions to use:

void Cdev_init (struct Cdev *, const struct file_operations *);//Empty Cdev, and populate file_operations struct int cdev_add (struct Cdev *, D ev_t, unsigned);//Register the character device to the kernel

Example: Write a simple character device driver, the main device number is major, register only 0~1 two this device number, and create a master device number of major, the second device number is created 0,1,2 three device files. Open these three files with the app to see what's going on (all open)

#include  <linux/module.h> #include  <linux/kernel.h> #include  <linux/fs.h># include <linux/init.h> #include  <linux/delay.h> #include  <linux/irq.h> #include  <asm/uaccess.h> #include  <asm/irq.h> #include  <asm/io.h> #include  <asm/ arch/regs-gpio.h> #include  <asm/hardware.h> #include  <linux/poll.h> #include  < Linux/cdev.h>static int hello_open (struct inode *inode, struct file * FILP) {PRINTK ("hello_open\n"); return 0;} Building the file_operations structure body static struct file_operations hello_fops={.owner=this_module,.open    =   hello_open,};static int major;static struct cdev hello _cdev;static struct class* hello_class;static struct class_device* hello_class_ Dev[3];static int hello_init (void) {dev_t devid;if (major==0) {&NBSP;&NBSP;&NBsp;alloc_chrdev_region (&devid,0,2, "Hello");//The main device number is MAJOR, the second device number is 0,1, the pair should be file_operationsmajor=major (devid);// Int alloc_chrdev_region (Dev_t *dev, unsigned baseminor, unsigned count,const  char *name)}else{devid=mkdev (major,0); Register_chrdev_region (devid,2, "Hello");//int register_ Chrdev_region (Dev_t from, unsigned count, const char *name)}cdev_init (&hello _cdev,&hello_fops);//Register Cdev_add (&hello_cdev,devid,2);//Create Class Hello_class=class_create (THIS_MODULE, "Hello" ); Int i;for (i=0;i<3;i++) {   //automatically creates a device Hello_class_dev[i]=class_device_create (Hello_class, Null,mkdev (Major,i), NULL, "hello%d", I);} return 0;} Static void hello_exit (void) {Cdev_del (&hello_cdev); Unregister_chrdev_region (MKDEV (major,0), 2); Int i;for (i=0;i<3;i++) {Class_device_destroy (Hello_class, mkdev (Major, i));} Class_destroy (Hello_class);} Module_init (Hello_init); Module_exit (Hello_exit); Module_license ("GPL"); 

The application is simple:

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h>int Main (int argc, char const *argv[]) {int Fd=open (ARGV[1],O_RDWR), if ( -1==FD) {printf ("Can ' t open!\n"); return;} printf ("Open ok!\n"); return 0;}

Results/phenomenon:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/DE/wKioL1cLcPjAx4EuAABGbbt0aaM352.png "title=" capture. PNG "alt=" Wkiol1clcpjax4euaabgbbt0aam352.png "/>

As you can see, only (252,0) and (252,1) correspond to the file_operations struct in the driver, while (252,2) is also an existing device file, but because the driver does not have its corresponding file_operations structure, So the application was rejected when it was opened.

This article is from the "June Feng Eureka" blog, please be sure to keep this source http://10274409.blog.51cto.com/10264409/1762687

The Register_chrdev and register_chrdev_region of character devices

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.