blob: e2c0baa69feff1702fec9c69f313129bb498225a [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Jason Gunthorpeafdba322013-11-26 13:30:40 -07002/*
3 * Copyright (C) 2004 IBM Corporation
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
9 *
10 * Copyright (C) 2013 Obsidian Research Corp
11 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12 *
13 * Device file system interface to the TPM
Jason Gunthorpeafdba322013-11-26 13:30:40 -070014 */
Jason Gunthorpeafdba322013-11-26 13:30:40 -070015#include <linux/slab.h>
James Bottomleyecb38e22017-01-10 19:08:53 -080016#include "tpm-dev.h"
Jason Gunthorpeafdba322013-11-26 13:30:40 -070017
18static int tpm_open(struct inode *inode, struct file *file)
19{
James Bottomleyecb38e22017-01-10 19:08:53 -080020 struct tpm_chip *chip;
Jason Gunthorpee3302e02013-11-26 13:30:45 -070021 struct file_priv *priv;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070022
James Bottomleyecb38e22017-01-10 19:08:53 -080023 chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
24
Jason Gunthorpeafdba322013-11-26 13:30:40 -070025 /* It's assured that the chip will be opened just once,
26 * by the check of is_open variable, which is protected
27 * by driver_lock. */
28 if (test_and_set_bit(0, &chip->is_open)) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -050029 dev_dbg(&chip->dev, "Another process owns this TPM\n");
Jason Gunthorpeafdba322013-11-26 13:30:40 -070030 return -EBUSY;
31 }
32
Jason Gunthorpee3302e02013-11-26 13:30:45 -070033 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
James Bottomleyecb38e22017-01-10 19:08:53 -080034 if (priv == NULL)
35 goto out;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070036
Tadeusz Strukc3d477a2018-09-10 10:18:28 -070037 tpm_common_open(file, chip, priv, NULL);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070038
Jason Gunthorpeafdba322013-11-26 13:30:40 -070039 return 0;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070040
James Bottomleyecb38e22017-01-10 19:08:53 -080041 out:
42 clear_bit(0, &chip->is_open);
43 return -ENOMEM;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070044}
45
Jason Gunthorpeafdba322013-11-26 13:30:40 -070046/*
47 * Called on file close
48 */
49static int tpm_release(struct inode *inode, struct file *file)
50{
Jason Gunthorpee3302e02013-11-26 13:30:45 -070051 struct file_priv *priv = file->private_data;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070052
James Bottomleyecb38e22017-01-10 19:08:53 -080053 tpm_common_release(file, priv);
Jason Gunthorpee3302e02013-11-26 13:30:45 -070054 clear_bit(0, &priv->chip->is_open);
Jason Gunthorpee3302e02013-11-26 13:30:45 -070055 kfree(priv);
James Bottomleyecb38e22017-01-10 19:08:53 -080056
Jason Gunthorpeafdba322013-11-26 13:30:40 -070057 return 0;
58}
59
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080060const struct file_operations tpm_fops = {
Jason Gunthorpeafdba322013-11-26 13:30:40 -070061 .owner = THIS_MODULE,
62 .llseek = no_llseek,
63 .open = tpm_open,
James Bottomleyecb38e22017-01-10 19:08:53 -080064 .read = tpm_common_read,
Tadeusz Strukc3d477a2018-09-10 10:18:28 -070065 .write = tpm_common_write,
Tadeusz Struk9e1b74a2018-09-10 10:18:33 -070066 .poll = tpm_common_poll,
Jason Gunthorpeafdba322013-11-26 13:30:40 -070067 .release = tpm_release,
68};