^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2004 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Leendert van Doorn <leendert@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Dave Safford <safford@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Reiner Sailer <sailer@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Kylene Hall <kjhall@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2013 Obsidian Research Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Device file system interface to the TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "tpm-dev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static int tpm_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct tpm_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct file_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* It's assured that the chip will be opened just once,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * by the check of is_open variable, which is protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * by driver_lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (test_and_set_bit(0, &chip->is_open)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) dev_dbg(&chip->dev, "Another process owns this TPM\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) priv = kzalloc(sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (priv == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) tpm_common_open(file, chip, priv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) clear_bit(0, &chip->is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Called on file close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int tpm_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct file_priv *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) tpm_common_release(file, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) clear_bit(0, &priv->chip->is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) const struct file_operations tpm_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .open = tpm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .read = tpm_common_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .write = tpm_common_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .poll = tpm_common_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .release = tpm_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };