下面是之前我遇到过的一些使用private_data的一些文件:
1、tty设备
static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
loff_t *ppos)
{
int i;
struct tty_struct * tty;
struct inode *inode;
struct tty_ldisc *ld;
tty = (struct tty_struct *)file->private_data;
2、tun/tap设备
static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
unsigned long count, loff_t pos)
{
struct file *file = iocb->ki_filp;
struct tun_struct *tun = file->private_data;
3、套接口文件
static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
struct file *file, const struct iovec *iov,
unsigned long nr_segs)
{
struct socket *sock = file->private_data;
size_t size = 0;
4、epoll文件
static int ep_eventpoll_close(struct inode *inode, struct file *file)
{
struct eventpoll *ep = file->private_data;
5、shm文件
long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
{
……
file->private_data = sfd;
打开设备:
open 方法提供给驱动来做任何的初始化来准备后续的操作. open 方法的原型是:
int (*open)(struct inode *inode, struct file *filp);
inode 参数有我们需要的信息,以它的 i_cdev 成员的形式, 里面包含我们之前建立的cdev 结构. 唯一的问题是通常我们不想要 cdev 结构本身, 我们需要的是包含 cdev 结构的 device_private 结构.
static int device_open(struct inode *inode, struct file *filp)
{
struct device_private *private;
private= container_of(inode->i_cdev, struct device_private, my_cdev);
filp->private_data = private;
private->open_flag++;
try_module_get(THIS_MODULE);
...
return 0;
}
释放设备: