Linux misc机制创建设备

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
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值