字符设备驱动程序

来源:互联网 发布:js 改变div class属性 编辑:程序博客网 时间:2024/06/01 10:02

设备文件的一些

#include <uapi/linux/kdev_t.h>#define MINORBITS20#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_MODULEloff_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_MMUunsigned (*mmap_capabilities)(struct file *);#endif};</span>

这个结构看起来复杂,但是其实就是系统调用和描述符建立的一种映射关系,从代码中可以看出,这些都是open ,read ,lock 等等一系列函数的映射。

还有第二重要的数据结构

<span style="font-size:18px;">struct file {union {struct llist_nodefu_llist;struct rcu_head fu_rcuhead;} f_u;struct pathf_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_tf_lock;atomic_long_tf_count;unsigned int f_flags;         //文件标识fmode_tf_mode;          //读写权限。很常见struct mutexf_pos_lock;loff_tf_pos;struct fown_structf_owner;const struct cred*f_cred;struct file_ra_statef_ra;u64f_version;#ifdef CONFIG_SECURITYvoid*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_headf_ep_links;struct list_headf_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);

这两个函数分别是分配内存与释放内存的内核级函数,关于这两个函数在这里不进行展开,后续我会实现下这类分配内存的函数。









0 0