PS:实际上标准的做法是在调用misc_open之前将file->private_data置为空,然后在调用misc_open的时候通过container_of函数得到驱动私有数据结构体的地址,然后让file->private_data指向私有数据结构体以供fops中的其他函数使用。尝试了一些办法,还是这个方法比较高效。
Author: Samu Onkalo <samu.p.onkalo@nokia.com> AuthorDate: Mon May 24 14:33:10 2010 -0700 Committer: Linus Torvalds <torvalds@linux-foundation.org> CommitDate: Tue May 25 08:07:03 2010 -0700 drivers: misc: pass miscdevice pointer via file private data For misc devices, inode->i_cdev doesn't point to the device drivers own data. Link between file operations and device driver internal data is lost. Pass pointer to misc device struct via file private data for driver open function use. Signed-off-by: Samu Onkalo <samu.p.onkalo@nokia.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Christoph Hellwig <hch@lst.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> --- drivers/char/misc.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/drivers/char/misc.c b/drivers/char/misc.c index 92ab03d..cd650ca 100644 --- a/drivers/char/misc.c +++ b/drivers/char/misc.c @@ -144,6 +144,7 @@ static int misc_open(struct inode * inode, struct file * file) old_fops = file->f_op; file->f_op = new_fops; if (file->f_op->open) {
+ /* 重要:这里最好加一个name的判断,或者打印出申请者的name,用于DEBUG */ + file->private_data = c; //增加这句,c指向misc_register时传进来的参数 err=file->f_op->open(inode,file); if (err) { fops_put(file->f_op); --
原文地址:http://kerneltrap.org/mailarchive/git-commits-head/2010/5/25/39820