2013-11-27 04:30:40 +08: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
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation, version 2 of the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/slab.h>
|
2017-01-11 11:08:53 +08:00
|
|
|
#include "tpm-dev.h"
|
2013-11-27 04:30:40 +08:00
|
|
|
|
|
|
|
static int tpm_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-01-11 11:08:53 +08:00
|
|
|
struct tpm_chip *chip;
|
2013-11-27 04:30:45 +08:00
|
|
|
struct file_priv *priv;
|
2013-11-27 04:30:40 +08:00
|
|
|
|
2017-01-11 11:08:53 +08:00
|
|
|
chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
|
|
|
|
|
2013-11-27 04:30:40 +08: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 01:29:47 +08:00
|
|
|
dev_dbg(&chip->dev, "Another process owns this TPM\n");
|
2013-11-27 04:30:40 +08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:30:45 +08:00
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
2017-01-11 11:08:53 +08:00
|
|
|
if (priv == NULL)
|
|
|
|
goto out;
|
2013-11-27 04:30:40 +08:00
|
|
|
|
2018-09-11 01:18:28 +08:00
|
|
|
tpm_common_open(file, chip, priv, NULL);
|
2013-11-27 04:30:40 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2017-01-11 11:08:53 +08:00
|
|
|
out:
|
|
|
|
clear_bit(0, &chip->is_open);
|
|
|
|
return -ENOMEM;
|
2013-11-27 04:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called on file close
|
|
|
|
*/
|
|
|
|
static int tpm_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2013-11-27 04:30:45 +08:00
|
|
|
struct file_priv *priv = file->private_data;
|
2013-11-27 04:30:40 +08:00
|
|
|
|
2017-01-11 11:08:53 +08:00
|
|
|
tpm_common_release(file, priv);
|
2013-11-27 04:30:45 +08:00
|
|
|
clear_bit(0, &priv->chip->is_open);
|
|
|
|
kfree(priv);
|
2017-01-11 11:08:53 +08:00
|
|
|
|
2013-11-27 04:30:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-13 03:46:37 +08:00
|
|
|
const struct file_operations tpm_fops = {
|
2013-11-27 04:30:40 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.open = tpm_open,
|
2017-01-11 11:08:53 +08:00
|
|
|
.read = tpm_common_read,
|
2018-09-11 01:18:28 +08:00
|
|
|
.write = tpm_common_write,
|
2018-09-11 01:18:33 +08:00
|
|
|
.poll = tpm_common_poll,
|
2013-11-27 04:30:40 +08:00
|
|
|
.release = tpm_release,
|
|
|
|
};
|