Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2004 IBM Corporation
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
9 *
10 * Copyright (C) 2013 Obsidian Research Corp
11 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12 *
13 * Device file system interface to the TPM
14 */
15#include <linux/slab.h>
16#include "tpm-dev.h"
17
18static int tpm_open(struct inode *inode, struct file *file)
19{
20 struct tpm_chip *chip;
21 struct file_priv *priv;
22
23 chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
24
25 /* It's assured that the chip will be opened just once,
26 * by the check of is_open variable, which is protected
27 * by driver_lock. */
28 if (test_and_set_bit(0, &chip->is_open)) {
29 dev_dbg(&chip->dev, "Another process owns this TPM\n");
30 return -EBUSY;
31 }
32
33 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
34 if (priv == NULL)
35 goto out;
36
37 tpm_common_open(file, chip, priv, NULL);
38
39 return 0;
40
41 out:
42 clear_bit(0, &chip->is_open);
43 return -ENOMEM;
44}
45
46/*
47 * Called on file close
48 */
49static int tpm_release(struct inode *inode, struct file *file)
50{
51 struct file_priv *priv = file->private_data;
52
53 tpm_common_release(file, priv);
54 clear_bit(0, &priv->chip->is_open);
55 kfree(priv);
56
57 return 0;
58}
59
60const struct file_operations tpm_fops = {
61 .owner = THIS_MODULE,
62 .llseek = no_llseek,
63 .open = tpm_open,
64 .read = tpm_common_read,
65 .write = tpm_common_write,
66 .poll = tpm_common_poll,
67 .release = tpm_release,
68};
1/*
2 * Copyright (C) 2004 IBM Corporation
3 * Authors:
4 * Leendert van Doorn <leendert@watson.ibm.com>
5 * Dave Safford <safford@watson.ibm.com>
6 * Reiner Sailer <sailer@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
9 * Copyright (C) 2013 Obsidian Research Corp
10 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
11 *
12 * Device file system interface to the TPM
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation, version 2 of the
17 * License.
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 /* It's assured that the chip will be opened just once,
31 * by the check of is_open variable, which is protected
32 * by driver_lock. */
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 * Called on file close
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};