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