Create a character device in the Linux 2.6 kernel, automatically obtain the device number, and create a simple example of a device Node

Source: Internet
Author: User

 

I forgot a very simple C code from where I found it a long time ago, completed the character device registration, automatically obtained the device number (Master/Slave), and automatically created the device node.

I have posted some similar questions for your reference.

Thanks to the original author of the Code: zengxiaolong

The following code is compiled in the 6.35-22 kernel.

Create_chrdev.c:

// Create_chrdev.c
//---------------------------------------------
# Include <Linux/types. h> // dev_t
# Include <Linux/cdev. h> // struct cdev
# Include <Linux/fs. h> // alloc_chrdev_region ()
# Include <Linux/device. h> // class_create ()

Dev_t devid;
Static struct cdev * led_cdev;
Static int led_major = 0;
Static int led_minor = 0;
Static struct class * led_class;

Static struct file_operations led_fops = {
. Owner = this_module,
};

Static int _ init hello_init (void)
{
Int err;

// Initialize cdev
Led_cdev = cdev_alloc ();
Cdev_init (led_cdev, & led_fops );
Led_cdev-> owner = this_module;

// Obtain the master device number dynamically (dev_t devid contains the "master device number" and "sub-device number" Information)
Alloc_chrdev_region (& devid, 66, 1, "led"); // assign one device number from 66
Led_major = major (devid );
Led_minor = minor (devid );
Printk (kern_info "I was assigned major Number % d. \ n", led_major );
Printk (kern_info "I was assigned minor Number % d. \ n", led_minor );

// Register character devices (1)
Err = cdev_add (led_cdev, devid, 1 );
If (ERR ){
Printk (kern_notice "error % d adding device \ n", err );
Return-1;
}

Led_class = class_create (this_module, "led_class1"); // a directory named led_class1 is created under the/sys directory.
If (is_err (led_class )){
Printk (kern_info "create class error \ n ");
Return-1;
}
Device_create (led_class, null, devid, null, "led" "% d", minor (devid ));

// Class_device_create should be used in earlier versions 2.6, before 2.6.27? 2.6.29? Forgot. In this step, an led66 device file is created under the/dev directory.

Return 0;
}

Static void _ exit hello_exit (void)
{
Unregister_chrdev_region (devid, 1 );
Cdev_del (led_cdev );

Device_destroy (led_class, devid); // class_device_destroy should be used in earlier versions than 2.6.27? 2.6.29? Forgot.
Class_destroy (led_class );
}

Module_init (hello_init );
Module_exit (hello_exit );

Module_license ("GPL ");
Module_author ("zengxiaolong ");
Module_description ("A sample driver ");
Module_supported_device ("testdevice ");

 

Makefile (for PC ):

OBJ-M: = create_chrdev.o
Kerneldir: =/lib/modules/$ (shell uname-R)/build
Default:
Make-C $ (kerneldir) M = $ (shell PWD) Modules
Install:
Insmod create_chrdev.ko
Uninstall:
Rmmod create_chrdev
Clean:
Make-C $ (kerneldir) M = $ (shell PWD) clean

Makefile (for ARM ):

OBJ-M: = create_chrdev.o
Kerneldir: =/usr/local/src/arm2440/kernel-2.6.29 # It is your arm Kernel folder
Default:
Make-C $ (kerneldir) M = $ (shell PWD) Modules
Install:
Insmod create_chrdev.ko
Uninstall:
Rmmod create_chrdev
Clean:
Make-C $ (kerneldir) M = $ (shell PWD) clean

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.