Learn the Linux drive automatically creating device nodes in one step

Source: Internet
Author: User

<span style= "FONT-SIZE:12PX;" >extern struct device *device_create (struct class *cls, struct device *parent,    dev_t devt, void *drvdata,    cons T char *fmt, ...)    __ATTRIBUTE__ (Format (printf, 5, 6));</span>
function Function:The function device_create () is used to dynamically establish a logical device, and initializes the new logical device class, associating it with the logical class represented by the first parameter of the function, and then adding the logical device to the device driver model of the Linux kernel system. function to automatically create a new logical device directory under the/sys/devices/virtual directory, and create a device file corresponding to the logical class in the/dev directoryparameter Description:struct class CLS: A logical class related to the amount of logical devices that are about to be created. dev_t Dev: device numbera pointer of type void *drvdata:void that represents the input parameter of the callback functionconst char *FMT: The device name of the logical device, which is the directory name of the logical device directory created in directory/sys/devices/virtual.
#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 <linux/poll.h> #include <linux/device.h>static struct class *   Myleddrv_class; Auto-register drive main device static struct device *myleddrv_dev;volatile unsigned long *gpbcon = NULL; Control register volatile unsigned long *gpbdat = NULL; data register static int myleddrv_open (void) {PRINTK ("Hello Linux world!\n"); return 0;} static int myleddrv_write (void) {return 0;}    static struct File_operations Myleddrv_fops = {. Owner = This_module,/* This is a macro that is automatically created when you push the module to compile the __this_module variable */ . open = Myleddrv_open,. Write=myleddrv_write,};static int major; global variable static int myleddrv_init (void) {major = Register_chrdev (0, "Myleddrv", &myleddrv_fops);//register, tell the kernel myleddrv_ class = Class_create (This_module, "myleddrv"); Myleddrv_dev = Device_create (MYleddrv_class, NULL, MKDEV (major, 0), NULL, "Myleddrv"); /*/dev/myleddrv */return 0;} static void Myleddrv_exit (void) {Unregister_chrdev (Major, "myleddrv");//Unload PRINTK ("Myleddrv has been unregistered!\n") ;d Evice_unregister (Myleddrv_dev);d Evice_destroy (myleddrv_class,major);} Module_init (Myleddrv_init); Module_exit (Myleddrv_exit); Module_license ("GPL");


Learn the Linux drive automatically creating device nodes in one step

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.