struct globalmem_dev *globalmem_devp; /*设备结构体指针*/ /*设备驱动模块加载函数*/int globalmem_init(void){ /* 动态申请设备结构体的内存*/ globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL);
} /*文件打开函数*/ int globalmem_open(struct inode *inode, struct file *filp) //inode便是设备节点 { /*将设备结构体指针赋值给文件私有数据指针,private_data可以存放任何数据*/ filp-> private_data = globalmem_devp; return 0; } /*写函数*/static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos){ unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; /*获得设备结构体指针*/ *.....* *}* *我的理解是这样的:*filp*是一个结构体,他有一个*private_data。在write里面的使用,是为了把init里已经初始化的globalmem_devp 赋值给这个wirte里的dev ,这样便是使用初始化了的同一个dev结构体。 code :https://gitcafe.com/iZobs/arm-char-device/blob/version2.0/modules/globalmem.c *iZobs Lin* *Appreciate Daily Life* 在 2014年7月9日 下午7:56,Kroderia <krode...@gmail.com>写道: > 我说的不是这个file_operations, 而是 > static ssize_t foo_write(struct file *filp, > const char __user *buffer, > size_t count, > loff_t *offp) > 这个filp > > > On 07 09, 2014, at 19:49, iZobs Lin <ivincent...@gmail.com> wrote: > > > > /*文件操作结构体*/ > > static const struct file_operations globalmem_fops = > > { > > .owner = THIS_MODULE, > > .llseek = globalmem_llseek, > > .read = globalmem_read, > > .write = globalmem_write, > > .ioctl = globalmem_ioctl, > > .open = globalmem_open, > > .release = globalmem_release, > > .poll = globalmem_poll, > > .fasync = globalmem_fasync, > > }; > > 如上,那个fd指向的就是这么个结构体,这个结构体封装了与应用层交互的函数。 > > > > 在 14/7/9,K roderia<krode...@gmail.com> 写道: > >> 是自己写的, 正在学. MISC的操作已经写了, 直接返回一个字符串什么的. 现在的问题是这个filp的含义不太明白, > >> 它到底是一个fd指向一个文件还是一个指针指向一个内存空间, 然后如何对它进行操作. 我看ldd3的讲解, 似乎是一个内部动态分配的内存空间, > >> 那么它内置的f_ops函数指针是用来干嘛的呢 > >> > >> > >> 2014-07-09 19:07 GMT+08:00 iZobs Lin <ivincent...@gmail.com>: > >> > >>> 那个module是你自己写的吗?read/write需要自己在module里写。你搜一下char > >>> driver,字符设备。有讲这个file_operations的东西。 > >>> > >>> 在 14/7/9,Kroderia<krode...@gmail.com> 写道: > >>>> 找了老半天没找到能看的样例程序, 来求救 > >>>> > >>>> Linux module里面的file_operations的读写函数, 要对struct file *filp进行读写, 要调用那些函数? > >>>> > >>> > 刚直接用filp->f_ops->read/write把虚拟机都搞崩了...看到有个版本是直接用simple_read_from_buffer对filp->private_data进行写入, > >>>> 不过似乎要在init里面进行内存分配, 这个做法可以吗? > >>>> > >>>> -- > >>>> 您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。 > >>>> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到gzlug+unsubscr...@googlegroups.com。 > >>>> 要向此网上论坛发帖,请发送电子邮件至 gzlug@googlegroups.com。 > >>>> 通过以下网址访问此论坛:http://groups.google.com/group/gzlug。 > >>>> 要查看更多选项,请访问 https://groups.google.com/d/optout。 > >>>> > >>> > >>> > >>> -- > >>> *iZobs Lin* > >>> *Appreciate Daily Life* > >>> > >>> -- > >>> 您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。 > >>> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到gzlug+unsubscr...@googlegroups.com。 > >>> 要向此网上论坛发帖,请发送电子邮件至 gzlug@googlegroups.com。 > >>> 通过以下网址访问此论坛:http://groups.google.com/group/gzlug。 > >>> 要查看更多选项,请访问 https://groups.google.com/d/optout。 > >>> > >> > >> -- > >> 您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。 > >> 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到gzlug+unsubscr...@googlegroups.com。 > >> 要向此网上论坛发帖,请发送电子邮件至 gzlug@googlegroups.com。 > >> 通过以下网址访问此论坛:http://groups.google.com/group/gzlug。 > >> 要查看更多选项,请访问 https://groups.google.com/d/optout。 > >> > > > > > > -- > > *iZobs Lin* > > *Appreciate Daily Life* > > > > -- > > 您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。 > > 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到gzlug+unsubscr...@googlegroups.com。 > > 要向此网上论坛发帖,请发送电子邮件至 gzlug@googlegroups.com。 > > 通过以下网址访问此论坛:http://groups.google.com/group/gzlug。 > > 要查看更多选项,请访问 https://groups.google.com/d/optout。 > > -- > 您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。 > 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到gzlug+unsubscr...@googlegroups.com。 > 要向此网上论坛发帖,请发送电子邮件至 gzlug@googlegroups.com。 > 通过以下网址访问此论坛:http://groups.google.com/group/gzlug。 > 要查看更多选项,请访问 https://groups.google.com/d/optout。 > -- 您收到此邮件是因为您订阅了 Google 网上论坛的“广州 GNU/Linux 用户组”论坛。 要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到gzlug+unsubscr...@googlegroups.com。 要向此网上论坛发帖,请发送电子邮件至 gzlug@googlegroups.com。 通过以下网址访问此论坛:http://groups.google.com/group/gzlug。 要查看更多选项,请访问 https://groups.google.com/d/optout。