Linux Kernel 学习笔记13:混杂设备模型
2017-07-04 19:19
288 查看
(本章基于:Linux-4.4.0-37)
Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述),它是一种特殊的字符设备。miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同。 所有的miscdevice设备形成了一个链表,对设备访问时内核根据次设备号查找对应的miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。
数据结构
<linux/miscdevice.h>
在内核中用struct miscdevice表示miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。
操作函数
注册
int misc_register(struct miscdevice * misc);
成功注册后会在/dev目录下自动生成一个字符设备文件,主设备号为10,次设备号与文件与之前设置的一致;
注销
int misc_deregister(struct miscdevice *misc);
例:
内核层:
用户层:
Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述),它是一种特殊的字符设备。miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同。 所有的miscdevice设备形成了一个链表,对设备访问时内核根据次设备号查找对应的miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。
数据结构
<linux/miscdevice.h>
在内核中用struct miscdevice表示miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。
struct miscdevice { int minor; const char *name; const struct file_operations *fops; struct list_head list; struct device *parent; struct device *this_device; const char *nodename; umode_t mode; };
操作函数
注册
int misc_register(struct miscdevice * misc);
成功注册后会在/dev目录下自动生成一个字符设备文件,主设备号为10,次设备号与文件与之前设置的一致;
注销
int misc_deregister(struct miscdevice *misc);
例:
内核层:
#include <linux/init.h> #include <linux/module.h> #include <linux/miscdevice.h> #include <linux/fs.h> int my_open(struct inode *inode, struct file *file) { printk(KERN_INFO "This is miscdevice open\n"); return 0; } int my_close(struct inode *inode, struct file *file) { printk(KERN_INFO "This is miscdevice close\n"); return 0; } const struct file_operations my_fops = { .open = my_open, .release = my_close, }; struct miscdevice my_misc = { .minor = 201, .name = "stone", .fops = &my_fops, }; static __init int hello_init(void) { misc_register(&my_misc); printk(KERN_ALERT "helloworld!\n"); return 0; } static __exit void hello_exit(void) { misc_deregister(&my_misc); printk(KERN_ALERT "helloworld exit!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Stone");
用户层:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(void) { int fd; fd = open("/dev/stone", O_RDWR); if(fd < 0) { perror("open"); exit(EXIT_FAILURE); } close(fd); return 0; }
相关文章推荐
- Linux Kernel 学习笔记16:总线设备驱动模型
- Linux设备模型学习笔记——结构体描述
- Linux设备驱动程式学习(13)-Linux设备模型(总线、设备、驱动程式和类)
- linux设备模型学习笔记——理论篇
- Linux Kernel 学习笔记4:自动创建设备节点
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)
- 嵌入式学习-驱动开发-lesson3-混杂设备驱动模型与linux中断处理流程
- Linux设备驱动模型学习笔记(kobject,kset,subsystem子系统)
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)
- Windows 8 Directx 开发学习笔记(二)建立模型及初始化设备
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)
- 学习笔记——《LINUX设备驱动程序(第三版)》Linux设备模型:内核添加、删除设备、驱动程序
- LDD3学习笔记设备模型之底层实现(kobject,kset) 收藏
- Linux Kernel 学习笔记5:字符设备
- Linux驱动开发-混杂字符设备驱动模型笔记 4
- Linux设备驱动程序学习(13) -Linux设备模型(总线、设备、驱动程序和类)
- linux学习笔记2--diver初始化及设备模型
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)【转】
- 我的内核学习笔记2:platform设备模型
- 《Linux设备设备驱动程序(第三版)》学习笔记之三:sleepy设备的使用