1 /*
2  * Copyright (C) 2017 James.Bottomley@HansenPartnership.com
3  *
4  * GPLv2
5  */
6 #include <linux/slab.h>
7 #include "tpm-dev.h"
8 
9 struct tpmrm_priv {
10 	struct file_priv priv;
11 	struct tpm_space space;
12 };
13 
tpmrm_open(struct inode * inode,struct file * file)14 static int tpmrm_open(struct inode *inode, struct file *file)
15 {
16 	struct tpm_chip *chip;
17 	struct tpmrm_priv *priv;
18 	int rc;
19 
20 	chip = container_of(inode->i_cdev, struct tpm_chip, cdevs);
21 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
22 	if (priv == NULL)
23 		return -ENOMEM;
24 
25 	rc = tpm2_init_space(&priv->space);
26 	if (rc) {
27 		kfree(priv);
28 		return -ENOMEM;
29 	}
30 
31 	tpm_common_open(file, chip, &priv->priv);
32 
33 	return 0;
34 }
35 
tpmrm_release(struct inode * inode,struct file * file)36 static int tpmrm_release(struct inode *inode, struct file *file)
37 {
38 	struct file_priv *fpriv = file->private_data;
39 	struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
40 
41 	tpm_common_release(file, fpriv);
42 	tpm2_del_space(fpriv->chip, &priv->space);
43 	kfree(priv);
44 
45 	return 0;
46 }
47 
tpmrm_write(struct file * file,const char __user * buf,size_t size,loff_t * off)48 static ssize_t tpmrm_write(struct file *file, const char __user *buf,
49 		   size_t size, loff_t *off)
50 {
51 	struct file_priv *fpriv = file->private_data;
52 	struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
53 
54 	return tpm_common_write(file, buf, size, off, &priv->space);
55 }
56 
57 const struct file_operations tpmrm_fops = {
58 	.owner = THIS_MODULE,
59 	.llseek = no_llseek,
60 	.open = tpmrm_open,
61 	.read = tpm_common_read,
62 	.write = tpmrm_write,
63 	.release = tpmrm_release,
64 };
65 
66