Linux Hybrid character device
Hybrid Device Driver Model
Hybrid Equipment Concept
In a Linux system, there are a class of character devices, which have the same main device number (10), but the second device number is different, we call this device a hybrid device (Miscdevice). All promiscuous devices form a linked list, and the kernel locates the corresponding promiscuous device according to the secondary device number when accessing the device.
1. Equipment description
A struct miscdevice is used in Linux to describe a hybrid device.
struct Miscdevice {
int minor; /* Secondary Device number */
const char *name; /* Device Name */
const struct File_operations *fops; /* File Operation */
struct List_head list;
struct device *parent;
struct device *this_device;
};
2. Device Registration
Linux uses the Misc_register function to register a hybrid device driver.
int Misc_register (struct Miscdevice * misc)
Write an example of a kernel-state and user-state data transfer and an 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/miscdevice.h>5#include <linux/fs.h>6#include <asm/uaccess.h>7 8Unsignedintparam=0;9 Ten Static intMy_open (structInode *node,structFile *Filep) One { APrintk"Open My_open sucess!\n"); - return 0; - } the Static intMy_read (structFile *filp,Char__user *buf,size_t size,loff_t *POS) - { -Printk"Read my_read sucess!\n"); -param = -; +Copy_to_user (buf, ¶m,4); - return 0; + } A at - structFile_operations My_miscdev_fops = - { -. Open =My_open, -. Read =My_read, - }; in - structMiscdevice My_miscdev = to { +. minor = $, -. Name ="My_miscdev", the. FoPs = &My_miscdev_fops, * }; $ Static intMy_miscdev_init (void)Panax Notoginseng { - intret; theret = Misc_register (&My_miscdev); + if(Ret! =0) APrintk"Miscdev Register fail.\n!"); the return 0; + - } $ Static voidMy_miscdev_exit (void) $ { -Misc_deregister (&My_miscdev); - the } - WuyiModule_license ("GPL"); the - Module_init (my_miscdev_init); WuModule_exit (My_miscdev_exit);
1#include <stdio.h>2#include <sys/stat.h>3#include <sys/types.h>4#include <sys/ioctl.h>5#include <fcntl.h>6 7 intMainCharargcChar*argv[])8 {9 intFD;Ten intcmd; One intparam =0; A if(ARGC <2) - { -printf"Please enter the second param!\n"); the return 0; - } -cmd = atoi (argv[1]); -FD = open ("/dev/my_miscdev", O_RDWR); + if(FD <0) - { +printf"Open/dev/my_miscdev fail.\n"); A } at - Switch(CMD) - { - Case 1: -printf"Second param is%c\n", *argv[1]); -Read (FD, ¶m,4); inprintf"Read Param is%d.\n", param); - Break; to default : + Break; - } the Close (FD); * return 0; $ Panax Notoginseng}
1 obj-m: = my_miscdev.o2 kdir: =/home/win/dn377org/trunk/bcm7252/linux/3all :4 make-c $ (kdir) m=$ (PWD) modules cross_compile=arm-linux-arch=arm5clean: 6 rm-f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order
Linux Hybrid character device