Linux Hybrid character device

Source: Internet
Author: User

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, &param,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, &param,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

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.