您的位置:首页 > 运维架构 > Linux

[linux device driver] Chapter 03:scullc_open理解

2015-02-27 22:27 429 查看
在阅读源代码时,到scullc_open的时候,有的地方不是很好理解。记录思考的过程,便于以后查找。

scullc_open源码如下:

int scullc_open (struct inode *inode, struct file *filp)
{
struct scullc_dev *dev; /* device information */

/*  Find the device */
dev = container_of(inode->i_cdev, struct scullc_dev, cdev);

/* now trim to 0 the length of the device if open was write-only */
if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
if (down_interruptible (&dev->sem))
return -ERESTARTSYS;
scullc_trim(dev); /* ignore errors */
up (&dev->sem);
}

/* and use filp->private_data to point to the device data */
filp->private_data = dev;

return 0;          /* success */
}


在上面的代码中,filp->f_flags & O_ACCMODE是为了获得f_flags低2位,然后判断文件的属性是什么样的,具体如下:

#define O_ACCMODE	   0003
#define O_RDONLY	     00
#define O_WRONLY	     01
#define O_RDWR		     02


有三类属性:只读、只写、可读可写。

最后调用scullc_trim(dev)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: