blob: 1a0e97a5da5a452d8aa7b486392ebcad4e1654bc [file] [log] [blame]
James Bottomleyfdc915f2017-01-03 09:07:32 -08001/*
2 * Copyright (C) 2017 James.Bottomley@HansenPartnership.com
3 *
4 * GPLv2
5 */
6#include <linux/slab.h>
7#include "tpm-dev.h"
8
9struct tpmrm_priv {
10 struct file_priv priv;
11 struct tpm_space space;
12};
13
14static 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
36static 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);
James Bottomley4d578562017-01-31 15:47:31 -080042 tpm2_del_space(fpriv->chip, &priv->space);
James Bottomleyfdc915f2017-01-03 09:07:32 -080043 kfree(priv);
44
45 return 0;
46}
47
Peter Huewe5e9fefd2017-05-25 07:43:05 +020048static ssize_t tpmrm_write(struct file *file, const char __user *buf,
James Bottomleyfdc915f2017-01-03 09:07:32 -080049 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
57const 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