xref: /linux/drivers/char/tpm/tpmrm-dev.c (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 James.Bottomley@HansenPartnership.com
4  */
5 #include <linux/slab.h>
6 #include "tpm-dev.h"
7 
8 struct tpmrm_priv {
9 	struct file_priv priv;
10 	struct tpm_space space;
11 };
12 
13 static int tpmrm_open(struct inode *inode, struct file *file)
14 {
15 	struct tpm_chip *chip;
16 	struct tpmrm_priv *priv;
17 	int rc;
18 
19 	chip = container_of(inode->i_cdev, struct tpm_chip, cdevs);
20 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
21 	if (priv == NULL)
22 		return -ENOMEM;
23 
24 	rc = tpm2_init_space(&priv->space, TPM2_SPACE_BUFFER_SIZE);
25 	if (rc) {
26 		kfree(priv);
27 		return -ENOMEM;
28 	}
29 
30 	tpm_common_open(file, chip, &priv->priv, &priv->space);
31 
32 	return 0;
33 }
34 
35 static int tpmrm_release(struct inode *inode, struct file *file)
36 {
37 	struct file_priv *fpriv = file->private_data;
38 	struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
39 
40 	tpm_common_release(file, fpriv);
41 	tpm2_del_space(fpriv->chip, &priv->space);
42 	kfree(priv);
43 
44 	return 0;
45 }
46 
47 const struct file_operations tpmrm_fops = {
48 	.owner = THIS_MODULE,
49 	.llseek = no_llseek,
50 	.open = tpmrm_open,
51 	.read = tpm_common_read,
52 	.write = tpm_common_write,
53 	.poll = tpm_common_poll,
54 	.release = tpmrm_release,
55 };
56