"Linux drivers" Automatically create device nodes

Source: Internet
Author: User
Tags goto

When you start learning to drive, you compile the driver into a module and then manually set up the device node with the Mknod command to make it available to the application call. This is a common way to start debugging a driver. However, when there is a need to have the driver ready when the system starts, supply the layer program call. At this point, you can no longer manually set up the device node, but must automatically create the device node (do not need artificial operation).

★ Registration Class

The purpose of the registration class is to enable Mdev to establish a device node in the/dev/directory.

First, you define a class that takes advantage of the struct class struct body. This struct is defined in the header file include/linux/device.h

struct class {const char* name;struct module* owner;struct subsystemsubsys;struct list_headchildren;struct list_ Headdevices;struct list_headinterfaces;struct semaphoresem;/* Locks Both the children and interfaces lists */struct Kobje Ct*virtual_dir;struct class_attribute* class_attrs;struct class_device_attribute* class_dev_attrs;struct device_ attribute* Dev_attrs;int (*uevent) (struct class_device *dev, char **envp,   int num_envp, char *buffer, int buffer_size ); Int (*dev_uevent) (struct device *dev, char **envp, int num_envp,char *buffer, int buffer_size); void (*release) (struct Class_device *dev), Void (*class_release) (struct class *class), void (*dev_release) (struct device *dev), int (*suspend) ( struct device *, pm_message_t State); Int (*resume) (struct device *);}


Then use

Complete the registration of the class. The first of these parameters is generally: this_module. The second parameter is: the name of the device node

As an example:

★ Create Device Node

To create a function for a device node:

<pre name= "code" class= "CPP" >struct device *device_create (struct class *class, struct device *parent,dev_t devt, CO NST Char *fmt, ...) {va_list args;struct device *dev = null;int retval =-enodev;if (class = = NULL | | Is_err (Class)) goto Error;dev = Kzalloc (sizeof (*dev), Gfp_kernel), if (!dev) {retval =-enomem;goto error;} dev->devt = Devt;dev->class = Class;dev->parent = Parent;dev->release = Device_create_release;va_start ( args, FMT); vsnprintf (dev->bus_id, Bus_id_size, FMT, args); Va_end (args); retval = Device_register (dev); if (retval) Goto Error;return dev;error:kfree (dev); return err_ptr (retval);}


The function has four parameters left to right to create the class to which the device node belongs, the parent node of the device (if it is not specified as null), the device number, the device name, and the secondary device number.

★ Destroy class and Device nodes

Be careful not to forget to also destroy the class and destroy the device node.

Destroy class: parameter is a variable defined with struct class struct body

void Class_destroy (struct class *cls) {if (CLS = = NULL) | | (Is_err (CLS))) Return;class_unregister (CLS);}
destroying device nodes:

void Device_destroy (struct class *class, dev_t devt) {struct device *dev = null;struct device *dev_tmp;down (&class-> , SEM), list_for_each_entry (dev_tmp, &class->devices, node) {if (dev_tmp->devt = = devt) {dev = dev_tmp;break;}} Up (&class->sem); if (dev) device_unregister (dev);}

★ Example (self-written delay driver)

#include <linux/miscdevice.h> #include <linux/delay.h> #include <asm/irq.h> #include <linux/kern el.h> #include <linux/module.h> #include <linux/delay.h> #include <linux/unistd.h> #include < linux/string.h> #include <linux/fcntl.h> #include <asm/uaccess.h> #include <linux/kdev_t.h># Include <linux/fs.h> #include <linux/init.h> #include <linux/version.h> #include <linux/config.h > #include <linux/kernel.h> #include <linux/interrupt.h> #include <linux/sched.h> #include < linux/device.h> #define NAME "Ralink_drive_delay" #define RALINK_GPIO_DEVNAME "my_delay" #define DELAY_US 0//most le AST is ten us#define Delay_ms 1//msint delay_major = 109; Module_license ("Dual BSD/GPL"); static long Ralink_delay_ioctl (struct inode * inode, struct file * file, unsigned int cmd,u                nsigned long Arg) {switch (cmd) {case Delay_us:udelay (+ arg); ReTurn 0;                Case Delay_ms:udelay (1000);            return 0;    default:return-1; }}static struct File_operations my_delay_fops = {. Owner = This_module,. IOCTL = ralink_delay_ioctl,};static struct    Class *delay_class;static int __init my_delay_init (void) {int ret = 0;    ret = Register_chrdev (delay_major, ralink_gpio_devname,&my_delay_fops);        if (Ret < 0) {PRINTK ("Unable to register character device\n");    return ret;    } if (delay_major = = 0) {delay_major = ret;    PRINTK (Kern_debug NAME ": Got dynamic Major%d\n", ret);    }//Register a class so that Mdev can establish a device node in the "/dev/directory" Delay_class = Class_create (This_module, ralink_gpio_devname);        if (Is_err (Delay_class)) {PRINTK ("failed in my_led class.\n");                           return-1; The parent node of this device, not specified is null     The third parameter is the device number//fourth parameter is the device name//fifth parameter is from the device number PRINTK ("My_delay driver initialized\n"); return 0;}    void __exit my_delay_exit (void) {Unregister_chrdev (delay_major,ralink_gpio_devname); Device_destroy (Delay_class,mkdev (delay_major,0));//Unregister Device node Class_destroy (delay_class);//Destroy Class PRINTK ("My_delay Drive    R exited\n "); }module_init (My_delay_init); Module_exit (My_delay_exit);







Related Article

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.