Linux杂项设备

Linux杂项设备驱动简介

什么是杂项设备

杂项设备是字符设备的一种。可以自动生成设备节点。Linux系统里有很多的杂项设备。我们可以输入 cat /proc/misc命令来查看

杂项设备和普通字符设备的区别

  1. 杂项设备比字符设备代码简单
  2. 杂项设备的主设备号是相同的,均为10,次设备号是不同的。主设备号相同就可以节省内核资源。

主设备号和次设备号是什么

  1. 设备号包含主设备号和次设备号,主设备号在Linux系统里面是唯一的,次设备号不一定唯一。
  2. 设备号是计算机识别设备的一种方式,主设备相同的就被试为同一类设备
  3. 主设备号可以通过命令 cat /proc/devices来查看

杂项设备的描述

杂项设备的结构体在源码 include/linux/miscdevice.h。vim include/linux/miscdevice.h 查看

1
2
3
4
5
6
7
8
9
10
11
12
struct miscdevice  {
int minor; //次设备号
const char *name; //设备节点的名字
const struct file_operations *fops; //文件操作集
// 下面的一般不用
struct list_head list;
struct device *parent;
struct device *this_device;
const struct attribute_group **groups;
const char *nodename;
umode_t mode;
};

file_operations结构体在linux源码中 include/linux/fs.h ,该结构体里的每一个结构体成员都对应一个调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
int (*iterate) (struct file *, struct dir_context *);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*mremap)(struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **, void **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
unsigned (*mmap_capabilities)(struct file *);
#endif
};

杂项设备的注册和注销

在源码 include/linux/miscdevice.h 中有函数声明

注册杂项设备

1
extern int misc_register(struct miscdevice *misc);

注销杂项设备

1
extern int misc_deregister(struct miscdevice *misc);

注册杂项设备的流程

  1. 填充miscdevice这个结构体
  2. 填充file_operation这个结构体
  3. 注册杂项设备,并生成设备节点

杂项设备编写例程

目标

按照杂项设备的注册流程注册一个杂项设备,并生成设备节点。

例程代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>

struct file_operations misc_fops = {
.owner = THIS_MODULE
};

struct miscdevice misc_dev ={
.minor = MISC_DYNAMIC_MINOR, //自动分配次设备号
.name = "hello_misc",
.fops = &misc_fops
};

static int misc_init(void)
{
int ret;
ret = misc_register(&misc_dev);
if(ret < 0)
{
printk("misc register is error\n");
return -1;
}
printk("misc is succeed\n");
return 0;
}

static void misc_exit(void)
{
misc_deregister(&misc_dev);
printk("misc by by\n");
}

module_init(misc_init);
module_exit(misc_exit);
MODULE_LICENSE("GPL");