Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2004 IBM Corporation
0004  * Authors:
0005  * Leendert van Doorn <leendert@watson.ibm.com>
0006  * Dave Safford <safford@watson.ibm.com>
0007  * Reiner Sailer <sailer@watson.ibm.com>
0008  * Kylene Hall <kjhall@us.ibm.com>
0009  *
0010  * Copyright (C) 2013 Obsidian Research Corp
0011  * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
0012  *
0013  * Device file system interface to the TPM
0014  */
0015 #include <linux/slab.h>
0016 #include "tpm-dev.h"
0017 
0018 static int tpm_open(struct inode *inode, struct file *file)
0019 {
0020     struct tpm_chip *chip;
0021     struct file_priv *priv;
0022 
0023     chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
0024 
0025     /* It's assured that the chip will be opened just once,
0026      * by the check of is_open variable, which is protected
0027      * by driver_lock. */
0028     if (test_and_set_bit(0, &chip->is_open)) {
0029         dev_dbg(&chip->dev, "Another process owns this TPM\n");
0030         return -EBUSY;
0031     }
0032 
0033     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0034     if (priv == NULL)
0035         goto out;
0036 
0037     tpm_common_open(file, chip, priv, NULL);
0038 
0039     return 0;
0040 
0041  out:
0042     clear_bit(0, &chip->is_open);
0043     return -ENOMEM;
0044 }
0045 
0046 /*
0047  * Called on file close
0048  */
0049 static int tpm_release(struct inode *inode, struct file *file)
0050 {
0051     struct file_priv *priv = file->private_data;
0052 
0053     tpm_common_release(file, priv);
0054     clear_bit(0, &priv->chip->is_open);
0055     kfree(priv);
0056 
0057     return 0;
0058 }
0059 
0060 const struct file_operations tpm_fops = {
0061     .owner = THIS_MODULE,
0062     .llseek = no_llseek,
0063     .open = tpm_open,
0064     .read = tpm_common_read,
0065     .write = tpm_common_write,
0066     .poll = tpm_common_poll,
0067     .release = tpm_release,
0068 };