一、获取主设备号的源码:
struct file_operations结构体中的成员函数是字符设备驱动的主体内容,在系统调用时会最终被
内核调用,它定义了字符设备驱动提供给虚拟文件系统的接口函数。例如:
ioctl方法提供了一种执行设备特定命令的方法(比如格式化软盘的某个磁道,这既不是读操作也不是写操作)
以上两个函数包含__user字符串,是一种形式的文档而已,表明指针是一个用户空间地址,因此不能
被直接引用,可以通过copy_to_user()、copy_from_user()等函数来实现。
2)、file 结构体:
源码中通过struct globalmem_dev *dev = filp->private_data;来获取globalmem_dev的实例指针。
实际上,大多数linux驱动都遵循一个潜规则,那就是将文件的私有数据private_data指向设备结构体,
再用read()、write()等函数通过private_data访问设备结构体。
三、和struct cdev结构相关的操作,该结构用来表示字符设备,包含实现内核和设备间的接口:
if (xxx_major) {
dev = MKDEV(xxx_major, xxx_minor);
retsult = register_chrdev_region(dev, xxx_nr_devs, DEV_NAME);
}
else {
retsult = alloc_chrdev_region(&dev, xxx_minor, xxx_nr_devs, DEV_NAME);
xxx_major = MAJOR(dev);
}
if (retsult < 0) {
printk(KERN_WARNING "DEV_NAME: can't get major %d\n", xxx_major);
return retsult;
}
二、以下将给出三个驱动程序操作涉及的内核数据结构,其中struct file_operations结构体保存了
字符驱动的方法,struct file 表示一个打开的文件, struct inode 表示一个磁盘文件。
1)、struct file_operations结构体:struct file_operations结构体中的成员函数是字符设备驱动的主体内容,在系统调用时会最终被
内核调用,它定义了字符设备驱动提供给虚拟文件系统的接口函数。例如:
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *filp, loff_t, int);
ssize_t (*read) (struct file *filp, char __user *buf, size_t count, loff_t *f_ops);
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);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
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 (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, 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 (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
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 (*dir_notify)(struct file *filp, unsigned long arg);
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);
};
其中方法poll、epoll、select这三个系统调用可用来查询某个或多个文件描述符上的读取或是写入是否会被阻塞,
ioctl方法提供了一种执行设备特定命令的方法(比如格式化软盘的某个磁道,这既不是读操作也不是写操作)
以上两个函数包含__user字符串,是一种形式的文档而已,表明指针是一个用户空间地址,因此不能
被直接引用,可以通过copy_to_user()、copy_from_user()等函数来实现。
2)、file 结构体:
struct file {
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head fu_list;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
#define f_dentry f_path.dentry
#define f_vfsmnt f_path.mnt
const struct file_operations *f_op;
atomic_t f_count;
unsigned int f_flags;
mode_t f_mode;
loff_t f_pos;
struct fown_struct f_owner;
unsigned int f_uid, f_gid;
struct file_ra_state f_ra;
unsigned long 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;
spinlock_t f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
};
其中void *private_data; private_data用来保存跨系统调用时保存的状态信息的非常有用的资源,
源码中通过struct globalmem_dev *dev = filp->private_data;来获取globalmem_dev的实例指针。
实际上,大多数linux驱动都遵循一个潜规则,那就是将文件的私有数据private_data指向设备结构体,
再用read()、write()等函数通过private_data访问设备结构体。
三、和struct cdev结构相关的操作,该结构用来表示字符设备,包含实现内核和设备间的接口:
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev; //表示设备号
unsigned int count;
};
完成cdev的初始化并添加到系统中的源码如下:
static void scull_setup_cdev(struct scull_dev *dev, int index)
{
int err, devno = MKDEV(scull_major, scull_minor + index);
cdev_init(&dev->cdev, &scull_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &scull_fops;
err = cdev_add(&dev->cdev, devno, 1);
if (err)
printk(KERN_NOTICE "Error %d adding scull %d", err, index);
}
四、read和write实现:
1)、read实现
static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
//*ppos是要读的位置相对于文件开头的偏移
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data;
if (p > GLOBALMEM_XIZE)
return 0;
if (count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
if (copy_to_user(buf, dev->mem + p, count) {
ret = -EFAULT;
}
else {
*ppos += count;
ret = count;
printk(KERN_INFO "read %u byte(s) from %lu\n", count, p);
}
return ret;
}
2)、write实现
static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
//*ppos是要读的位置相对于文件开头的偏移
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data;
if (p > GLOBALMEM_XIZE)
return 0;
if (count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
if (copy_from_user(dev->mem + p, buf, count) {
ret = -EFAULT;
}
else {
*ppos += count;
ret = count;
printk(KERN_INFO "written %u byte(s) from %lu\n", count, p);
}
return ret;
}