xref: /openbmc/linux/drivers/char/tpm/tpm-dev.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2afdba32eSJason Gunthorpe /*
3afdba32eSJason Gunthorpe  * Copyright (C) 2004 IBM Corporation
4afdba32eSJason Gunthorpe  * Authors:
5afdba32eSJason Gunthorpe  * Leendert van Doorn <leendert@watson.ibm.com>
6afdba32eSJason Gunthorpe  * Dave Safford <safford@watson.ibm.com>
7afdba32eSJason Gunthorpe  * Reiner Sailer <sailer@watson.ibm.com>
8afdba32eSJason Gunthorpe  * Kylene Hall <kjhall@us.ibm.com>
9afdba32eSJason Gunthorpe  *
10afdba32eSJason Gunthorpe  * Copyright (C) 2013 Obsidian Research Corp
11afdba32eSJason Gunthorpe  * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12afdba32eSJason Gunthorpe  *
13afdba32eSJason Gunthorpe  * Device file system interface to the TPM
14afdba32eSJason Gunthorpe  */
15afdba32eSJason Gunthorpe #include <linux/slab.h>
16ecb38e2fSJames Bottomley #include "tpm-dev.h"
17afdba32eSJason Gunthorpe 
tpm_open(struct inode * inode,struct file * file)18afdba32eSJason Gunthorpe static int tpm_open(struct inode *inode, struct file *file)
19afdba32eSJason Gunthorpe {
20ecb38e2fSJames Bottomley 	struct tpm_chip *chip;
21e3302e0dSJason Gunthorpe 	struct file_priv *priv;
22afdba32eSJason Gunthorpe 
23ecb38e2fSJames Bottomley 	chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
24ecb38e2fSJames Bottomley 
25afdba32eSJason Gunthorpe 	/* It's assured that the chip will be opened just once,
26afdba32eSJason Gunthorpe 	 * by the check of is_open variable, which is protected
27afdba32eSJason Gunthorpe 	 * by driver_lock. */
28afdba32eSJason Gunthorpe 	if (test_and_set_bit(0, &chip->is_open)) {
298cfffc9dSJason Gunthorpe 		dev_dbg(&chip->dev, "Another process owns this TPM\n");
30afdba32eSJason Gunthorpe 		return -EBUSY;
31afdba32eSJason Gunthorpe 	}
32afdba32eSJason Gunthorpe 
33e3302e0dSJason Gunthorpe 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
34ecb38e2fSJames Bottomley 	if (priv == NULL)
35ecb38e2fSJames Bottomley 		goto out;
36ecb38e2fSJames Bottomley 
37c3d477a7STadeusz Struk 	tpm_common_open(file, chip, priv, NULL);
38ecb38e2fSJames Bottomley 
39ecb38e2fSJames Bottomley 	return 0;
40ecb38e2fSJames Bottomley 
41ecb38e2fSJames Bottomley  out:
42afdba32eSJason Gunthorpe 	clear_bit(0, &chip->is_open);
43afdba32eSJason Gunthorpe 	return -ENOMEM;
44afdba32eSJason Gunthorpe }
45afdba32eSJason Gunthorpe 
46afdba32eSJason Gunthorpe /*
47afdba32eSJason Gunthorpe  * Called on file close
48afdba32eSJason Gunthorpe  */
tpm_release(struct inode * inode,struct file * file)49afdba32eSJason Gunthorpe static int tpm_release(struct inode *inode, struct file *file)
50afdba32eSJason Gunthorpe {
51e3302e0dSJason Gunthorpe 	struct file_priv *priv = file->private_data;
52afdba32eSJason Gunthorpe 
53ecb38e2fSJames Bottomley 	tpm_common_release(file, priv);
54e3302e0dSJason Gunthorpe 	clear_bit(0, &priv->chip->is_open);
55e3302e0dSJason Gunthorpe 	kfree(priv);
56ecb38e2fSJames Bottomley 
57afdba32eSJason Gunthorpe 	return 0;
58afdba32eSJason Gunthorpe }
59afdba32eSJason Gunthorpe 
60313d21eeSJarkko Sakkinen const struct file_operations tpm_fops = {
61afdba32eSJason Gunthorpe 	.owner = THIS_MODULE,
62afdba32eSJason Gunthorpe 	.llseek = no_llseek,
63afdba32eSJason Gunthorpe 	.open = tpm_open,
64ecb38e2fSJames Bottomley 	.read = tpm_common_read,
65c3d477a7STadeusz Struk 	.write = tpm_common_write,
669e1b74a6STadeusz Struk 	.poll = tpm_common_poll,
67afdba32eSJason Gunthorpe 	.release = tpm_release,
68afdba32eSJason Gunthorpe };
69