2019-06-01 15:08:55 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-11-27 03:30:40 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2004 IBM Corporation
|
|
|
|
* Authors:
|
|
|
|
* Leendert van Doorn <leendert@watson.ibm.com>
|
|
|
|
* Dave Safford <safford@watson.ibm.com>
|
|
|
|
* Reiner Sailer <sailer@watson.ibm.com>
|
|
|
|
* Kylene Hall <kjhall@us.ibm.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Obsidian Research Corp
|
|
|
|
* Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
|
|
|
|
*
|
|
|
|
* Device file system interface to the TPM
|
|
|
|
*/
|
|
|
|
#include <linux/slab.h>
|
2017-01-11 10:08:53 +07:00
|
|
|
#include "tpm-dev.h"
|
2013-11-27 03:30:40 +07:00
|
|
|
|
|
|
|
static int tpm_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-01-11 10:08:53 +07:00
|
|
|
struct tpm_chip *chip;
|
2013-11-27 03:30:45 +07:00
|
|
|
struct file_priv *priv;
|
2013-11-27 03:30:40 +07:00
|
|
|
|
2017-01-11 10:08:53 +07:00
|
|
|
chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
|
|
|
|
|
2013-11-27 03:30:40 +07:00
|
|
|
/* It's assured that the chip will be opened just once,
|
|
|
|
* by the check of is_open variable, which is protected
|
|
|
|
* by driver_lock. */
|
|
|
|
if (test_and_set_bit(0, &chip->is_open)) {
|
2016-03-01 00:29:47 +07:00
|
|
|
dev_dbg(&chip->dev, "Another process owns this TPM\n");
|
2013-11-27 03:30:40 +07:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:30:45 +07:00
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
2017-01-11 10:08:53 +07:00
|
|
|
if (priv == NULL)
|
|
|
|
goto out;
|
2013-11-27 03:30:40 +07:00
|
|
|
|
2018-09-11 00:18:28 +07:00
|
|
|
tpm_common_open(file, chip, priv, NULL);
|
2013-11-27 03:30:40 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2017-01-11 10:08:53 +07:00
|
|
|
out:
|
|
|
|
clear_bit(0, &chip->is_open);
|
|
|
|
return -ENOMEM;
|
2013-11-27 03:30:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called on file close
|
|
|
|
*/
|
|
|
|
static int tpm_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2013-11-27 03:30:45 +07:00
|
|
|
struct file_priv *priv = file->private_data;
|
2013-11-27 03:30:40 +07:00
|
|
|
|
2017-01-11 10:08:53 +07:00
|
|
|
tpm_common_release(file, priv);
|
2013-11-27 03:30:45 +07:00
|
|
|
clear_bit(0, &priv->chip->is_open);
|
|
|
|
kfree(priv);
|
2017-01-11 10:08:53 +07:00
|
|
|
|
2013-11-27 03:30:40 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-13 02:46:37 +07:00
|
|
|
const struct file_operations tpm_fops = {
|
2013-11-27 03:30:40 +07:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.open = tpm_open,
|
2017-01-11 10:08:53 +07:00
|
|
|
.read = tpm_common_read,
|
2018-09-11 00:18:28 +07:00
|
|
|
.write = tpm_common_write,
|
2018-09-11 00:18:33 +07:00
|
|
|
.poll = tpm_common_poll,
|
2013-11-27 03:30:40 +07:00
|
|
|
.release = tpm_release,
|
|
|
|
};
|