2019-06-01 08:08:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-11-26 20:30:40 +00: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 03:08:53 +00:00
|
|
|
#include "tpm-dev.h"
|
2013-11-26 20:30:40 +00:00
|
|
|
|
|
|
|
static int tpm_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-01-11 03:08:53 +00:00
|
|
|
struct tpm_chip *chip;
|
2013-11-26 20:30:45 +00:00
|
|
|
struct file_priv *priv;
|
2013-11-26 20:30:40 +00:00
|
|
|
|
2017-01-11 03:08:53 +00:00
|
|
|
chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
|
|
|
|
|
2013-11-26 20:30:40 +00: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-02-29 17:29:47 +00:00
|
|
|
dev_dbg(&chip->dev, "Another process owns this TPM\n");
|
2013-11-26 20:30:40 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2013-11-26 20:30:45 +00:00
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
2017-01-11 03:08:53 +00:00
|
|
|
if (priv == NULL)
|
|
|
|
goto out;
|
2013-11-26 20:30:40 +00:00
|
|
|
|
2018-09-10 17:18:28 +00:00
|
|
|
tpm_common_open(file, chip, priv, NULL);
|
2013-11-26 20:30:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2017-01-11 03:08:53 +00:00
|
|
|
out:
|
|
|
|
clear_bit(0, &chip->is_open);
|
|
|
|
return -ENOMEM;
|
2013-11-26 20:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called on file close
|
|
|
|
*/
|
|
|
|
static int tpm_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2013-11-26 20:30:45 +00:00
|
|
|
struct file_priv *priv = file->private_data;
|
2013-11-26 20:30:40 +00:00
|
|
|
|
2017-01-11 03:08:53 +00:00
|
|
|
tpm_common_release(file, priv);
|
2013-11-26 20:30:45 +00:00
|
|
|
clear_bit(0, &priv->chip->is_open);
|
|
|
|
kfree(priv);
|
2017-01-11 03:08:53 +00:00
|
|
|
|
2013-11-26 20:30:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-12 19:46:37 +00:00
|
|
|
const struct file_operations tpm_fops = {
|
2013-11-26 20:30:40 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.open = tpm_open,
|
2017-01-11 03:08:53 +00:00
|
|
|
.read = tpm_common_read,
|
2018-09-10 17:18:28 +00:00
|
|
|
.write = tpm_common_write,
|
2018-09-10 17:18:33 +00:00
|
|
|
.poll = tpm_common_poll,
|
2013-11-26 20:30:40 +00:00
|
|
|
.release = tpm_release,
|
|
|
|
};
|