Linux character devices-Automatically create device numbers and device nodes

Source: Internet
Author: User

Linux character Devices - automatically create device numbers and device nodes

First write an example of automatically assigning a character device number and device node and the APP

Manual Installation steps:

Insmod My_char_dev.ko

No more device nodes to install

Then the test app .

./my_char_dev_app 1

1#include <linux/module.h>2#include <linux/init.h>3#include <linux/io.h>4#include <linux/fs.h>5#include <asm/device.h>//The following three header files are created due to the need to add dynamic6#include <linux/device.h>7#include <linux/cdev.h>8#include"my_cdev.h"9 structCdev Cdev;Tendev_t Devno;//here is the dynamic allocation of the device number and the dynamic creation of device nodes need to use One struct class*Cdev_class; A  - intMy_cdev_open (structInode *node,structFile *Filp) - { thePrintk"My_cdev_open sucess!\n"); -     return 0; - } -  + LongMy_cdev_ioctl (structFile *filp, unsignedintCMD, unsignedLongArg) - { +     Switch(CMD) A     { at          Caseled_on: -Printk"led_on is set!\n"); -             return 0; -          CaseLed_off: -Printk"Led_off is set!\n"); -             return 0; in         default : -             return-EINVAL; to     } + } -  the structFile_operations my_cdev_fops= * { $. Open =My_cdev_open,Panax Notoginseng. Unlocked_ioctl =My_cdev_ioctl, -  the }; +  A Static intMy_cdev_init (void) the { +     intret; -     /** Dynamic allocation of device numbers*/ $ret = Alloc_chrdev_region (&devno,0,1,"My_chardev"); $     if(ret) -     { -Printk"alloc_chrdev_region fail!\n"); theUnregister_chrdev_region (Devno,1); -         returnret;Wuyi     } the     Else -     { WuPrintk"alloc_chrdev_region sucess!\n"); -     } About     /** Description of structure initialization*/ $Cdev_init (&cdev,&my_cdev_fops); -     /** Describe structure registration*/ -ret = Cdev_add (&cdev,devno,1); -     if(ret) A     { +Printk"Cdev Add fail.\n"); theUnregister_chrdev_region (Devno,1); -         returnret; $     } the     Else the     { thePrintk"Cdev Add sucess!\n"); the     } -  inCdev_class = Class_create (This_module,"My_chardev"); the     if(Is_err (cdev_class)) the     { AboutPrintk"Create class fail!\n"); theUnregister_chrdev_region (Devno,1); the         return-1; the     } +     Else -     { thePrintk"Create class sucess!\n");Bayi     } the  theDevice_create (Cdev_class,null,devno,0,"My_chardev"); -      -     return 0; the } the Static voidMy_cdev_exit (void) the { the Device_destroy (CDEV_CLASS,DEVNO); - Class_destroy (cdev_class); theCdev_del (&Cdev); theUnregister_chrdev_region (Devno,1); thePrintk"my_cdev_exit sucess!\n");94 } the Module_init (my_cdev_init); the Module_exit (my_cdev_exit); theModule_license ("GPL");98Module_author ("Yefei"); AboutModule_description ("Yefei Driver");

1 #ifndef __my_cdev_h__ 2 #define __my_cdev_h__34#define led_magic ' L '5#define led_on _ IO (led_magic,0)6#define led_off _io (led_magic,1)78#endif

1#include <sys/stat.h>2#include <sys/types.h>3#include <sys/ioctl.h>4#include <fcntl.h>5#include <stdio.h>6#include"my_cdev.h"7 8 intMainintargcChar*argv[])9 {Ten     intFD; One     intcmd; A     if(ARGC <2) -     { -printf"Please enter secend param!\n"); the         return 0; -     } -cmd = atoi (argv[1]); -FD = open ("/dev/my_chardev", O_RDWR); +     if(FD <0) -     { +printf"Open Dev/my_chardev fail!\n"); A Close (FD); at         return 0; -     } -     Switch(CMD) -     { -          Case 1: - IOCTL (fd,led_on); in              Break; -          Case 2: to IOCTL (fd,led_off); +              Break; -         default: the              Break; *     } $ Close (FD);Panax Notoginseng     return 0; -}

1 obj-m: = my_char_dev.o2 kdir: =/home/win/dn377org/trunk/bcm7252/linux/3  All:4     make-c $ (kdir) m=$ (PWD) modules cross_compile=arm-linux-arch=arm5  Clean:6     rm-f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order

Linux character devices-Automatically create device numbers and device nodes

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.