1.open函数
我们从inode中获取scull_dev指针,并将其赋给filp->private_data,这样在其它文件函数中便可以知道当前正在操作的设备文件。
如果是只写方式打开文件,我们还要清空设备文件的内存。
2.read函数
从对应设备中读取内容,这里要判断当前文件位置f_pos和读取的长度count在文件中是否有内容,如果没有内容返回0,示意文件结束。
3.write函数
判断要写入的位置是否有内存,如果没有内存要分配内存。写入成功后要更新文件长度dev->size
4.llseek函数
移动filp中的当前文件指针。
更详细的说明在注释中给出。
实现了open,read,write,llseek函数后scull.c代码如下:
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
#include <linux/seq_file.h>
#include <linux/cdev.h>
#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_*_user */
#include "scull.h" /* local definitions */
int scull_major = SCULL_MAJOR;
int scull_minor = 0;
int scull_nr_devs = SCULL_NR_DEVS;
int scull_quantum = SCULL_QUANTUM;
int scull_qset = SCULL_QSET;
module_param(scull_major, int, S_IRUGO);
module_param(scull_minor, int, S_IRUGO);
module_param(scull_nr_devs, int, S_IRUGO);
module_param(scull_quantum, int, S_IRUGO);
module_param(scull_qset, int, S_IRUGO);
MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
struct scull_dev *scull_devices;
/*从scull_dev的内存链表中查找第n个并返回*/
struct scull_qset *scull_follow(struct scull_dev *dev,int n)
{
struct scull_qset *qs = dev->data;
if(!qs) /*设备首元素为空,分配内存*/
{
qs = dev->data = kmalloc(sizeof(struct scull_qset),GFP_KERNEL);
if(NULL == qs)
return NULL;
memset(qs,0,sizeof(struct scull_qset));
}
while(n--) /*遍历链表,返回第n个*/
{
if(!qs->next)
{
qs->next = kmalloc(sizeof(struct scull_qset),GFP_KERNEL);
if(NULL == qs->next)
return NULL;
memset(qs->next,0,sizeof(struct scull_qset));
}
qs = qs->next;
}
return qs;
}
/*清除scull_dev的内存*/
int scull_trim(struct scull_dev *dev)
{
struct scull_qset *next,*dptr;
int qset = dev->qset;
int i;
for(dptr = dev->data;dptr;dptr = next)
{
if(dptr->data)
{
for(i = 0;i < qset;i++)
{
if(dptr->data[i])
kfree(dptr->data[i]);
}
kfree(dptr->data);
}
next = dptr->next;
kfree(dptr);
}
dev->size = 0;
dev->qset = scull_qset;
dev->quantum = scull_quantum;
dev->data = NULL;
return 0;
}
loff_t scull_llseek(struct file *filp,loff_t off,int whence)
{
struct scull_dev *dev = filp->private_data;
loff_t newpos;
switch(whence) {
case 0: /* SEEK_SET */
newpos = off;
break;
case 1: /* SEEK_CUR */
newpos = filp->f_pos + off;
break;
case 2: /* SEEK_END */
newpos = dev->size + off;
break;
default: /* can't happen */
return -EINVAL;
}
if (newpos < 0) return -EINVAL;
filp->f_pos = newpos;
return newpos;
}
ssize_t scull_read(struct file *filp,char __user *buf,size_t count,
loff_t *f_pos)
{
struct scull_dev *dev = (struct scull_dev *)filp->private_data;
struct scull_qset *dptr;
int quantum = dev->quantum,qset = dev->qset;
int itemsize = quantum * qset; /*一个qset的字节大小*/
int item,s_pos,q_pos,rest;
ssize_t retval = 0;
if(down_interruptible(&dev->sem))
return -ERESTARTSYS;
/*当前文件 指针已经超过了文件的长度*/
if(*f_pos >= dev->size)
goto out;
/*要读取的文件长度加上当前文件偏移超长*/
if(*f_pos + count > dev->size)
count = dev->size - *f_pos;
/*计算当前位置在链表的第几项*/
item = (long)*f_pos / itemsize;
/*计算在qset的第几项,和在某项中的起始位置*/
rest = (long)*f_pos % itemsize;
s_pos = rest / quantum; q_pos = rest % quantum;
dptr = scull_follow(dev,item);
/*还没有数据*/
if(dptr == NULL || !dptr->data || !dptr->data[s_pos])
goto out;
/*读取的长度超过了此项的内存大小,只读取本项数据*/
if(count > quantum - q_pos)
count = quantum - q_pos;
/*将数据copy到用户空间buf*/
if(copy_to_user(buf,dptr->data[s_pos]+q_pos,count))
{
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
out:
up(&dev->sem);
return retval;
}
ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
struct scull_dev *dev = filp->private_data;
struct scull_qset *dptr;
int quantum = dev->quantum, qset = dev->qset;
int itemsize = quantum * qset;
int item, s_pos, q_pos, rest;
ssize_t retval = -ENOMEM; /* value used in "goto out" statements */
if (down_interruptible(&dev->sem))
return -ERESTARTSYS;
/* 查找所在的链表位置item,和在某一个qset中的 s_pos和在某一项中的q_pos */
item = (long)*f_pos / itemsize;
rest = (long)*f_pos % itemsize;
s_pos = rest / quantum; q_pos = rest % quantum;
/* 查到位置,如果需要并分配内存*/
dptr = scull_follow(dev, item);
if (dptr == NULL)
goto out;
if (!dptr->data) {
dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
if (!dptr->data)
goto out;
memset(dptr->data, 0, qset * sizeof(char *));
}
if (!dptr->data[s_pos]) {
dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
if (!dptr->data[s_pos])
goto out;
}
/* 如果要写的内容超过了本项的剩余空间,只写满本项*/
if (count > quantum - q_pos)
count = quantum - q_pos;
/*从用户空间拷贝数据*/
if (copy_from_user(dptr->data[s_pos]+q_pos, buf, count)) {
retval = -EFAULT;
goto out;
}
*f_pos += count;
retval = count;
/* update the size */
if (dev->size < *f_pos)
dev->size = *f_pos;
out:
up(&dev->sem);
return retval;
}
int scull_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
printk(KERN_NOTICE "[SCULL] scull_ioctl for minor [%d]\r\n",(int)filp->private_data);
return 0;
}
int scull_open(struct inode *inode, struct file *filp)
{
struct scull_dev *dev;
/*从inode的cdev中得到我们的scull_dev指针*/
dev = container_of(inode->i_cdev,struct scull_dev,cdev);
/* 将scull_dev赋值给private_data,为其它函数准备*/
filp->private_data = dev;
/*如果以只写方式打开文件,将scull_dev的设备内存清空*/
if((filp->f_flags & O_ACCMODE) == O_WRONLY)
{
/*获取信号量失败,返回重启系统调用*/
if(down_interruptible(&dev->sem))
return -ERESTARTSYS;
/*清空内存并释放信号量*/
scull_trim(dev);
up(&dev->sem);
}
return 0;
}
int scull_release(struct inode *inode, struct file *filp)
{
return 0;
}
struct file_operations scull_fops = {
.owner = THIS_MODULE,
.llseek = scull_llseek,
.read = scull_read,
.write = scull_write,
.ioctl = scull_ioctl,
.open = scull_open,
.release = scull_release,
};
void scull_cleanup_module(void)
{
int i;
dev_t devno = MKDEV(scull_major,scull_minor);
if(scull_devices)
{
for(i = 0;i < scull_nr_devs;i++)
{
scull_trim(&scull_devices[i]);
cdev_del(&scull_devices[i].cdev);
}
kfree(scull_devices);
}
unregister_chrdev_region(devno,scull_nr_devs);
}
/*安装scull_dev,将scull_dev驱动程序与设备号建立联系。*/
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 "Err %d adding scull %d\n",err,index);
}
}
int scull_init_module(void)
{
int result,i;
dev_t dev = 0;
if(scull_major) /*静态设备号*/
{
dev = MKDEV(scull_major,scull_minor);
result = register_chrdev_region(dev,scull_nr_devs,"scull");
}
else /*动态设备号*/
{
result = alloc_chrdev_region(&dev,scull_minor,scull_nr_devs,"scull");
scull_major = MAJOR(dev);
}
if(result < 0)
{
printk(KERN_INFO "[SCULL] can't get major %d\n",scull_major);
return result;
}
scull_devices = kmalloc(scull_nr_devs * sizeof(struct scull_dev),GFP_KERNEL);
if(!scull_devices)
{
result = -ENOMEM;
goto fail;
}
memset(scull_devices,0,scull_nr_devs * sizeof(struct scull_dev));
for(i = 0;i < scull_nr_devs;i++)
{
scull_devices[i].quantum = scull_quantum;
scull_devices[i].qset = scull_qset;
init_MUTEX(&scull_devices[i].sem);
scull_setup_cdev(&scull_devices[i],i);
}
return 0;
fail:
scull_cleanup_module();
return result;
}
module_init(scull_init_module);
module_exit(scull_cleanup_module);
验证:
1.编译安装模块
2.创建设备文件/dev/scull0
3.向设备文件写入内容
[root@localhost dev]# echo "1111111111111111111111111111111111111112222222222222222222333333333333" > /dev/scull0
4.读取设备文件内容
[root@localhost dev]# cat /dev/scull0
1111111111111111111111111111111111111112222222222222222222333333333333