Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 2 | /* |
| 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 Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 14 | */ |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 16 | #include "tpm-dev.h" |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 17 | |
| 18 | static int tpm_open(struct inode *inode, struct file *file) |
| 19 | { |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 20 | struct tpm_chip *chip; |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 21 | struct file_priv *priv; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 22 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 23 | chip = container_of(inode->i_cdev, struct tpm_chip, cdev); |
| 24 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 25 | /* 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 Gunthorpe | 8cfffc9 | 2016-02-29 12:29:47 -0500 | [diff] [blame] | 29 | dev_dbg(&chip->dev, "Another process owns this TPM\n"); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 30 | return -EBUSY; |
| 31 | } |
| 32 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 33 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 34 | if (priv == NULL) |
| 35 | goto out; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 36 | |
Tadeusz Struk | c3d477a | 2018-09-10 10:18:28 -0700 | [diff] [blame] | 37 | tpm_common_open(file, chip, priv, NULL); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 38 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 39 | return 0; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 40 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 41 | out: |
| 42 | clear_bit(0, &chip->is_open); |
| 43 | return -ENOMEM; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Called on file close |
| 48 | */ |
| 49 | static int tpm_release(struct inode *inode, struct file *file) |
| 50 | { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 51 | struct file_priv *priv = file->private_data; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 52 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 53 | tpm_common_release(file, priv); |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 54 | clear_bit(0, &priv->chip->is_open); |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 55 | kfree(priv); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 56 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 60 | const struct file_operations tpm_fops = { |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 61 | .owner = THIS_MODULE, |
| 62 | .llseek = no_llseek, |
| 63 | .open = tpm_open, |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame] | 64 | .read = tpm_common_read, |
Tadeusz Struk | c3d477a | 2018-09-10 10:18:28 -0700 | [diff] [blame] | 65 | .write = tpm_common_write, |
Tadeusz Struk | 9e1b74a | 2018-09-10 10:18:33 -0700 | [diff] [blame] | 66 | .poll = tpm_common_poll, |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 67 | .release = tpm_release, |
| 68 | }; |