misc device被称为杂项设备,Linux内核把一些不符合预先确定的字符设备划分为杂项设备,这类设备的主设备号是10,Linux内核使用struct miscdevice数据结构描述这类设备
struct miscdevice所对应的结构体和包含路径:
Linux 内核提供了注册杂项设备的两个接口函数,驱动程序采用misc_register()函数来注册。它会自动创建设备节点,不需要使用mknod创建文件节点,因此使用misc机制来创建字符设备驱动是比较方便;
函数的声明在:drivers/char/misc.c
int misc_register(struct miscdevice *misc)
void misc_deregister(struct miscdevice *misc)
从函数的声明可知传入的参数为struct miscdevice类型,大概杂项设备代码逻辑就清晰了,先声明struct miscdevice结构体,并进行初始化;
misc_char.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#define CHAR_NAME "char_dev"
static struct device *drv_device;
static int drv_open(struct inode *inode, struct file *file)
{
int major = MAJOR(inode->i_rdev);
int minor = MINOR(inode->i_rdev);
printk("%s: The device major=%d, minor=%d\n", __func__, major, minor);
return 0;
}
static int drv_release(struct inode *inode, struct file *file)
{
printk("The %s funciton",__func__);
return 0;
}
static ssize_t drv_read(struct file *file, char __user *buf, size_t lbuf, loff_t *ppos)
{
printk("The %s enter\n", __func__);
return 0;
}
static ssize_t drv_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos)
{
printk("The %s enter\n", __func__);
return 0;
}
static const struct file_operations drv_fops = {
.owner = THIS_MODULE,
.open = drv_open,
.release = drv_release,
.read = drv_read,
.write = drv_write
};
static struct miscdevice misc_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = CHAR_NAME,
.fops = &drv_fops,
};
static int __init simple_char_init(void)
{
int ret;
ret = misc_register(&misc_device);
if (ret) {
printk("failed register misc device\n");
return ret;
}
drv_device = misc_device.this_device;
printk("succeeded register char device: %s\n", CHAR_NAME);
return 0;
}
static void __exit simple_char_exit(void)
{
printk("removing device\n");
misc_deregister(&misc_device);
}
module_init(simple_char_init);
module_exit(simple_char_exit);
MODULE_AUTHOR("Curtis.li");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("misc char device");
Makefile:
obj-m += misc_char.o
KBUILD_CFLAGS += -g
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
insmod模块之后可以在/dev目录下找到设备char_dev;
ring3.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define DEMO_DEV_NAME "/dev/char_dev"
int main()
{
char buffer[32];
int fd;
fd = open(DEMO_DEV_NAME, O_RDONLY);
if (fd < 0) {
printf("open device %s failded\n", DEMO_DEV_NAME);
return -1;
}
read(fd, buffer, 32);
close(fd);
return 0;
}
dmesg调试信息:主设备号为10,次设备号随机分配56