Linux设备驱动开发 - 混杂设备驱动
2024-09-20
混杂设备共享一个主设备号MISC_MAJOR(10),次设备号不同。
混杂设备本质上仍然是一个字符设备,所以混杂设备的操作跟字符设备基本相同。
在字符设备中,描述字符设备的结构体为struct cdev,在混杂设备中同样有一个描述混杂设备的结构体struct miscdevice。
1 struct miscdevice {
2 int minor; /* 次设备号 */
3 const char *name; /* 设备名 */
4 const struct file_operations *fops; /* 文件操作 */
5 struct list_head list;
6 struct device *parent;
7 struct device *this_device;
8 const char *nodename;
9 mode_t mode;
10 };
因为混杂设备共享一个主设备号10,所以在struct miscdevice结构体里面只要指定次设备号,如果次设备号为MISC_DYNAMIC_MINOR系统会自动分配一个次设备号,同时在/dev目录下根据struct miscdevice结构体的name成员创建一个设备节点/dev/'name'。
和字符设备一样要指定一个设备文件操作fops。
1 struct file_operations {
2 struct module *owner;
3 loff_t (*llseek) (struct file *, loff_t, int);
4 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
5 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
6 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
7 ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
8 int (*readdir) (struct file *, void *, filldir_t);
9 unsigned int (*poll) (struct file *, struct poll_table_struct *);
10 long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
11 long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
12 int (*mmap) (struct file *, struct vm_area_struct *);
13 int (*open) (struct inode *, struct file *);
14 int (*flush) (struct file *, fl_owner_t id);
15 int (*release) (struct inode *, struct file *);
16 int (*fsync) (struct file *, int datasync);
17 int (*aio_fsync) (struct kiocb *, int datasync);
18 int (*fasync) (int, struct file *, int);
19 int (*lock) (struct file *, int, struct file_lock *);
20 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
21 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
22 int (*check_flags)(int);
23 int (*flock) (struct file *, int, struct file_lock *);
24 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
25 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
26 int (*setlease)(struct file *, long, struct file_lock **);
27 long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);
28 };
在模块加载时需要注册混杂设备驱动,相对于字符设备驱动而言混杂设备驱动显得更为简单,只要一个misc_register()函数并指定要操作的miscdevice结构体即可。
1 int misc_register(struct miscdevice * misc);
成功返回0,失败返回一个负的错误号。
在模块卸载时需要卸载混杂设备驱动,使用函数misc_deregister()。
1 int misc_deregister(struct miscdevice *misc);
混杂设备驱动模板:
1 static struct file_operations xxx_fops =
2 {
3 .owner = THIS_MODULE,
4 .open = xxx_open,
5 .read = xxx_read,
6 ...
7 };
8
9 static struct miscdevice xxx_misc =
10 {
11 .minor = MISC_DYNAMIC_MINOR,
12 .name = NAME,
13 .fops = &xxx_fops,
14 };
15
16 static int __init xxx_init(void)
17 {
18 ...
19 /* 注册混杂设备驱动 */
20 misc_register(&xxx_misc);
21 ...
22 }
23
24 static void __exit xxx_exit(void)
25 {
26 ...
27 /* 卸载混杂设备驱动 */
28 misc_deregister(&xxx_misc);
29 ...
30 }
31
32 module_init(xxx_init);
33 module_exit(xxx_exit);
34 MODULE_LICENSE('GPL');
- Linux帧缓冲设备驱动程序框架及图形界面GUI的移植
- Linux Kernel之flush_cache_all在ARM平台下是如何实现的
- 手把手教你写Linux设备驱动---中断(三)--workqueue实现(基于友善之臂4412开发板)
- makefile初步制作,arm-linux- (gcc/ld/objcopy/objdump)详解
- Ubuntu下安装arm-linux-gnueabi-xxx编译器
- 用Qemu运行/调试arm linux
- Linux内核异常处理体系结构详解(一)
- arm linux 移植 mtd-utils 1.x
- 基于gnu-arm-linux的LPC2220的简单工程模板
- Linux下制作logo并显示到开发板上