1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/slab.h>
21#include "tpm-dev.h"
22
23static int tpm_open(struct inode *inode, struct file *file)
24{
25 struct tpm_chip *chip;
26 struct file_priv *priv;
27
28 chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
29
30
31
32
33 if (test_and_set_bit(0, &chip->is_open)) {
34 dev_dbg(&chip->dev, "Another process owns this TPM\n");
35 return -EBUSY;
36 }
37
38 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
39 if (priv == NULL)
40 goto out;
41
42 tpm_common_open(file, chip, priv);
43
44 return 0;
45
46 out:
47 clear_bit(0, &chip->is_open);
48 return -ENOMEM;
49}
50
51static ssize_t tpm_write(struct file *file, const char __user *buf,
52 size_t size, loff_t *off)
53{
54 return tpm_common_write(file, buf, size, off, NULL);
55}
56
57
58
59
60static int tpm_release(struct inode *inode, struct file *file)
61{
62 struct file_priv *priv = file->private_data;
63
64 tpm_common_release(file, priv);
65 clear_bit(0, &priv->chip->is_open);
66 kfree(priv);
67
68 return 0;
69}
70
71const struct file_operations tpm_fops = {
72 .owner = THIS_MODULE,
73 .llseek = no_llseek,
74 .open = tpm_open,
75 .read = tpm_common_read,
76 .write = tpm_write,
77 .release = tpm_release,
78};
79