How to create a device file:
The first is the use of Mknod manual created: Mknod filename type Major minor
The second is to create a device node by itself: Using udev (Mdev) to automate the creation of device files, first of all ensure that udev (embedded system with Mdev) is supported by BusyBox configuration .
About Udev
Udev runs in user mode, not in the kernel . Udev's initialization script creates a device node at system startup, and when the new device is plugged in--and the driver is added--after registering the new data on the SYSFS, Udev innovates The new device node .
Udev is a tool that works in the user space, it can update the device files according to the state of the hardware device, including the creation, deletion, and permission of the device files. These files are usually defined in the/dev directory, but can also be specified in the configuration file. Udev must be supported by SYSFS and TMPFS in the kernel,SYSFS provides the device entry and Uevent channel for Udev, and TMPFS provides storage space for Udev device files .
Note that Udev achieves the purpose of customizing the device files by modifying the device files generated by the kernel or by adding aliases. However, Udev is a user-mode program that does not change the kernel behavior. That is, the kernel will still create device files such as Sda,sdb, and udev can differentiate different devices based on the device's unique information and generate new device files (or links). And in the user's app, just use the newly generated device file
from To Create a device node:
The main thing to do with support for Udev in driving is to call class_create (...) in the code that drives the initialization. Create a class for the device, and then call device_create (...) for each device. Create the corresponding device .
The struct class structure defined in the kernel, as the name implies, a struct class struct type variable corresponding to a class, the kernel also provides class_create (...) function, which can be used to create a class, this class is stored under Sysfs , once the class is created, then the Device_create (...) is called. function to create the appropriate device node in the/dev directory.
In this way, when the module is loaded, udev in the user space automatically responds to the Device_create () function,/sysfs to find the corresponding class to create the device node.
1.class_create (...) function
Function: Create a class;
#define Class_create (owner, name) \ ({ \ staticstruct lock_class_key __key; \ &__key); \ })
Owner:this_module
Name: Names
The source code for __class_create (owner, name, &__key) is as follows:
struct class*__class_create (structModule *owner,Const Char*name,structLock_class_key *key) { struct class*CLS; intretval; CLS= Kzalloc (sizeof(*CLS), Gfp_kernel); if(!CLS) {retval= -Enomem; Gotoerror; } CLS->name =name; CLS->owner =owner; CLS->class_release =class_create_release; retval=__class_register (CLS, key); if(retval)Gotoerror; returnCLS; Error:kfree (CLS); returnerr_ptr (retval); } EXPORT_SYMBOL_GPL (__class_create);
Destroy function:void Class_destroy (struct class *cls)
void Class_destroy (structclass *cls) { if (cls = = NULL) | | (Is_err (CLS))) return ; Class_unregister (CLS); }
2.device_create (...) function
struct Device *device_create (struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *FMT, ...)
Function: Create a character device file
Parameters:
struct Class *class: Class
struct Device *parent:null
dev_t DEVT: Device number
void *drvdata:null,
const CHAR *FMT: Name
Return:
struct Device *
Here is the source code parsing:
struct device *device_create (structclass *classstruct device * Parent, voidconstChar *fmt, ...) { va_list vargs; struct device *Dev; Va_start (Vargs, FMT); = Device_create_vargs (class, Parent, devt, Drvdata, FMT, Vargs); Va_end (Vargs); return Dev; }
Here is an example:
hello.c
#include <linux/module.h>#include<linux/fs.h>#include<linux/cdev.h>#include<linux/device.h>Static intMajor = -; Static intMinor=0; Staticdev_t Devno; Static struct class*CLS; Static structDevice *Test_device; Static intHello_open (structInode *inode,structFile *Filep) {PRINTK ("hello_open \ n"); return 0; } Static structFile_operations hello_ops={. Open=Hello_open,}; Static intHello_init (void) { intret; PRINTK ("hello_init \ n"); Devno=MKDEV (Major,minor); RET= Register_chrdev (Major,"Hello",&hello_ops); CLS= Class_create (This_module,"MyClass"); if(Is_err (CLS)) {Unregister_chrdev (major,"Hello"); return-Ebusy; } Test_device= Device_create (Cls,null,devno,null,"Hello");//Mknod/dev/hello if(Is_err (Test_device)) {Class_destroy (CLS); Unregister_chrdev (Major,"Hello"); return-Ebusy; } return 0; } Static voidHello_exit (void) {Device_destroy (CLS,DEVNO); Class_destroy (CLS); Unregister_chrdev (Major,"Hello"); PRINTK ("hello_exit \ n"); } module_license ("GPL"); Module_init (Hello_init); Module_exit (Hello_exit);
TEST.c
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> main () { int FD ; FD = open ( " /dev/hello " ,o_rdwr); if (Fd<0 ) {perror ( " open fail \ n " ); return ; } close (FD); }
#ubuntu的内核源码树 if you want to compile modules installed in Ubuntu, open these 2 kern_ver = $ (Shell uname-r) kern_dir =/lib/modules/$ (kern_ver)/build # Development Board Li Nux Kernel's source tree directory #kern_dir =/lib/modules/2.6 . 31 -14 -generic/ build#kern_dir =/home/book/per/kernel/linux- 2.6 . 22.6 # /root/driver/kernelobj -M += Hello.oall:make -C $ (kern_dir) M= ' pwd ' modules #cp: # CP *.ko/root/porting_x210/rootfs/rootfs/driver_test. Phony:clean clean:make -C $ (kern_dir) M= ' pwd ' modules clean
Transferred from: http://blog.csdn.net/zqixiao_09/article/details/50864014
http://blog.csdn.net/zqixiao_09/article/details/50849735
How to automatically create a device node