How does udev generate Device Files in linux2.6?

Source: Internet
Author: User
Use udev to dynamically generate Device Files under/dev/so that you do not need to manually call mknod.
Http://linux.chinaunix.net/bbs/viewthread.php? Tid = 892777

The kernel API used:

Class_create: Creates a class.
Class_destroy: Destroy class
Class_device_create: Creates a device.
Class_device_destroy: destroy device

Note that these APIs are available at the beginning of 2.6.13 and should be used before 2.6.13

Class_simple_create
Class_simple_destroy
Class_simple_device_add
Class_simple_device_remove evolved into class_createclass_destroyclass_device_createclass_device_destroy to 2.6.27 and changed to class_createclass_destroydevice_createdevice_destroy, which is described in Chapter ldd3. For details, see:

Https://lwn.net/Articles/128644/

Output:
========================================================== ===
[Root @ localhost dynamic_dev_node] # insmod./dummy_dev.ko
[Root @ localhost dynamic_dev_node] # file/dev/dummy_dev0
/Dev/dummy_dev0: character special (250/0)
[Root @ localhost dynamic_dev_node] # rmmod dummy_dev.ko
[Root @ localhost dynamic_dev_node] # file/dev/dummy_dev0
/Dev/dummy_dev0: Error: cannot open '/dev/dummy_dev0' (no such file or directory)

{
Copycode ($ ('code0 '));
} "Href =" http://writeblog.csdn.net/### "> [copy to clipboard] {
Toggle_collapse ('code0 ');
} "Href =" http://writeblog.csdn.net/### "> [-]

Code:

# Include <Linux/kernel. h>
# Include <Linux/module. h>
# Include <Linux/init. h>
# Include <Linux/mm. h>
# Include <Linux/fs. h>
# Include <Linux/types. h>
# Include <Linux/delay. h>
# Include <Linux/moduleparam. h>
# Include <Linux/slab. h>
# Include <Linux/errno. h>
# Include <Linux/IOCTL. h>
# Include <Linux/cdev. h>
# Include <Linux/string. h>
# Include <Linux/list. h>
# Include <Linux/PCI. h>
# Include <ASM/uaccess. h>
# Include <ASM/Atomic. h>
# Include <ASM/unistd. h>

# Define this_description "/
This module is a dummy device driver, it register/n/
/T/Ta char device, and utilize udev to create/destroy/n/
/T/tdevice node under/dev/dynamicallly ."

Module_license ("GPL ");
Module_author ("albcamus <albcamus@gmail.com> ");
Module_description (this_description );

# Define dummy_major 250
# Define dummy_minor 0
# Define dummy_name "dummy_dev"

/**
* The Open routine of 'dummy _ dev'
*/
Static int dummy_open (struct inode * inode, struct file * file)
{
Printk ("Open OK/N ");
Return 0;
}

/**
* The write routine of 'dummy _ dev'
*/
Static ssize_t dummy_write (struct file * filp, const char * bp, size_t count, loff_t * PPOs)
{
Printk ("don't write! /N ");
Return 0;
}

/**
* The read routine of 'dummy _ dev'
*/
Static ssize_t dummy_read (struct file * filp, char * bp, size_t count, loff_t * PPOs)
{
Return 0;
}

/**
* The IOCTL routine of 'dummy _ dev'
*/
Static int dummy_ioctl (struct inode * inode, struct file * filep,
Unsigned int cmd, unsigned long Arg)
{

Return 0;
}

/**
* File_operations of 'dummy _ dev'
*/
Static struct file_operations dummy_dev_ops = {
. Owner = this_module,
. Open = dummy_open,
. Read = dummy_read,
. Write = dummy_write,
. IOCTL = dummy_ioctl,
};

/**
* Struct cdev of 'dummy _ dev'
*/
Struct cdev * my_cdev;
Struct class * my_class;

Static int _ init my_init (void)
{
Int err, devno = mkdev (dummy_major, dummy_minor );

/* Register the 'dummy _ dev' char device */
My_cdev = cdev_alloc ();
Cdev_init (my_cdev, & dummy_dev_ops );

My_cdev-> owner = this_module;

Err = cdev_add (my_cdev, devno, 1 );
If (Err! = 0)
Printk ("dummy PCI device register failed! /N ");

/* Creating your own class */
My_class = class_create (this_module, "dummy_class ");
If (is_err (my_class )){
Printk ("Err: failed in creating class./N ");
Return-1;
}

/* Register Your Own Device in sysfs, and this will cause udevd to create corresponding device node */
Class_device_create (my_class, null, mkdev (dummy_major, dummy_minor), null, dummy_name "% d", dummy_minor );

Return 0;
}

Static void _ exit my_fini (void)
{
Printk ("Bye/N ");

Cdev_del (my_cdev );
// Kfree (my_cdev); no use. Because that cdev_del () will call kfree if neccessary.

Class_device_destroy (my_class, mkdev (dummy_major, dummy_minor ));
Class_destroy (my_class );

}

Module_init (my_init );
Module_exit (my_fini );

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.