设备文件的一些
#include <uapi/linux/kdev_t.h>
#define MINORBITS 20
#define MINORMASK ((1U << MINORBITS) - 1)
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
#define print_dev_t(buffer, dev) \
sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))
#define format_dev_t(buffer, dev) \
({ \
sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev)); \
buffer; \
})
主设备号和次设备号
MAJOR 获得主设备号
MINOR 获得次设备号
不得不说,内核的位运算真用的很到位的.
然后是一些基本的数据结构:
首先是file_operation这个结构体:这个结构体就是用来将设备的编号与驱动程序建立链接的
<span style="font-size:18px;">struct file_operations {
struct module *owner; //指向拥有该结构的模块的指针,防止正在运行过程中被卸载 初始化为THIS_MODULE
loff_t (*llseek) (struct file *, loff_t, int); //记录读写位置,返回新的位置可类比APUE的lseek函数
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 (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);//初始化一个异步读取操作
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, 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 *);//多路复用的集合后端,为NULL 可读可写不阻塞
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 *); //对文件进行打开操作,NULL:永远可以打开成功但是不报告驱动程序
int (*flush) (struct file *, fl_owner_t id); //关闭进程设备文件描述符副本的时候执行
int (*release) (struct inode *, struct file *); //当file结构被释放时,调用这个操作,与OPEN 类似。
int (*fsync) (struct file *, loff_t, loff_t, int datasync);//系统调用fsync的后端实现,刷新待处理数据
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);</span></span>
<span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space:pre"> </span>//在目的地址空间中找到一个合适的位置,以遍将底层设备中的内存段映射到该位置。通常由内存管理代码完成这个任务,大部分驱动设置为NULL;
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
};</span>
这个结构看起来复杂,但是其实就是系统调用和描述符建立的一种映射关系,从代码中可以看出,这些都是open ,read ,lock 等等一系列函数的映射。
还有第二重要的数据结构
<span style="font-size:18px;">struct file {
union {
struct llist_node fu_llist;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
struct inode *f_inode; /* cached value */
const struct file_operations *f_op; //上面介绍过的文件结构 *************************************
/*
* Protects f_ep_links, f_flags.
* Must not be taken from IRQ context.
*/
spinlock_t f_lock;
atomic_long_t f_count;
unsigned int f_flags; //文件标识
fmode_t f_mode; //读写权限。很常见
struct mutex f_pos_lock;
loff_t f_pos;
struct fown_struct f_owner;
const struct cred *f_cred;
struct file_ra_state f_ra;
u64 f_version;
#ifdef CONFIG_SECURITY
void *f_security;
#endif
/* needed for tty driver, and maybe others */
void *private_data;
#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links;
struct list_head f_tfile_llink;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
} __attribute__((aligned(4))); /* lest something weird decides that 2 is OK */</span>
struct file 是一个内核结构,它不会出现在用户程序中。
另外又增加了两个新的宏:
unsigned int iminor(struct inode *inode);
unsigned int imajor(struct inode *inode);
字符设备的注册:
内核内部使用struct cdev 结构来表示设备。在内核调用设备的操作之前,必须分配并注册一个或者多个上述结构,所以我们的代码应该包含<linux/cdev.h>
容我去找一下这个结构体。....... 这个文件真的很小。全源码贴上来吧!
#ifndef _LINUX_CDEV_H
#define _LINUX_CDEV_H
#include <linux/kobject.h>
#include <linux/kdev_t.h>
#include <linux/list.h>
struct file_operations;
struct inode;
struct module;
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
void cdev_init(struct cdev *, const struct file_operations *);//初始化驱动设备结构体
struct cdev *cdev_alloc(void);//动态分配设备文件
void cdev_put(struct cdev *p);
int cdev_add(struct cdev *, dev_t, unsigned); //注册一个驱动设备
void cdev_del(struct cdev *); //删除一个驱动设备
void cd_forget(struct inode *);
#endif
SCULL内存的使用:
其中调用了两个重要的内核函数:
void * kmalloc(size_t size, int flags);
void kfree(void *ptr);
这两个函数分别是分配内存与释放内存的内核级函数,关于这两个函数在这里不进行展开,后续我会实现下这类分配内存的函数。
版权声明:本文为博主原创文章,未经博主允许不得转载。