Linux hybrid character Devices
Linux hybrid character Devices
Hybrid Device Driver Model
Hybrid device Concept
In Linux, there is a type of character devices that have the same master device number (10) but different sub-device numbers. We call such devices as miscdevice ). All the devices form a linked list. During device access, the kernel finds the corresponding devices according to the device number.
1. device description
In Linux, struct miscdevice is used to describe a Hybrid device.
Struct miscdevice {
Int minor;/* Device Number */
Const char * name;/* device name */
Const struct file_operations * fops;/* File Operations */
Struct list_head list;
Struct device * parent;
Struct device * this_device;
};
2. device registration
Use the misc_register function in Linux to register a Hybrid device driver.
Int misc_register (struct miscdevice * misc)
First, write an example of intercommunication between kernel-state data and user-state data and an APP
Manual installation steps:
Insmod my_char_dev.ko
No more device node installation required
Then test the app.
./My_char_dev_app 1
# Include <linux/module. h>
# Include <linux/init. h>
# Include <linux/io. h>
# Include <linux/miscdevice. h>
# Include <linux/fs. h>
# Include <asm/uaccess. h>
Unsigned int param = 0;
Static int my_open (struct inode * node, struct file * filep)
{
Printk ("Open my_open sucess! \ N ");
Return 0;
}
Static int my_read (struct file * filp, char _ user * buf, size_t size, loff_t * pos)
{
Printk ("Read my_read sucess! \ N ");
Param = 500;
Copy_to_user (buf, & param, 4 );
Return 0;
}
Struct file_operations my_miscdev_fops =
{
. Open = my_open,
. Read = my_read,
};
Struct miscdevice my_miscdev =
{
. Minor = 200,
. Name = "my_miscdev ",
. Fops = & my_miscdev_fops,
};
Static int my_miscdev_init (void)
{
Int ret;
Ret = misc_register (& my_miscdev );
If (ret! = 0)
Printk ("miscdev register fail. \ n! ");
Return 0;
}
Static void my_miscdev_exit (void)
{
Misc_deregister (& my_miscdev );
}
MODULE_LICENSE ("GPL ");
Module_init (my_miscdev_init );
Module_exit (my_miscdev_exit );
--------------------------------------------------------------------
# Include <stdio. h>
# Include <sys/stat. h>
# Include <sys/types. h>
# Include <sys/ioctl. h>
# Include <fcntl. h>
Int main (char argc, char * argv [])
{
Int fd;
Int cmd;
Int param = 0;
If (argc <2)
{
Printf ("please enter the second param! \ N ");
Return 0;
}
Cmd = atoi (argv [1]);
Fd = open ("/dev/my_miscdev", O_RDWR );
If (fd <0)
{
Printf ("Open/dev/my_miscdev fail. \ n ");
}
Switch (cmd)
{
Case 1:
Printf ("Second param is % c \ n", * argv [1]);
Read (fd, & param, 4 );
Printf ("Read Param is % d. \ n", param );
Break;
Default:
Break;
}
Close (fd );
Return 0;
}
----------------------------------------
1 obj-m: = my_miscdev.o
2 KDIR: =/home/win/dn377org/trunk/bcm7252/linux/
3 all:
4 make-C $ (KDIR) M = $ (PWD) modules CROSS_COMPILE = arm-linux-ARCH = arm
5 clean:
6 rm-f *. ko *. o *. mod. o *. mod. c *. symvers *. bak *. order
This article permanently updates the link address: