Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 IBM Corporation |
| 3 | * Authors: |
| 4 | * Leendert van Doorn <leendert@watson.ibm.com> |
| 5 | * Dave Safford <safford@watson.ibm.com> |
| 6 | * Reiner Sailer <sailer@watson.ibm.com> |
| 7 | * Kylene Hall <kjhall@us.ibm.com> |
| 8 | * |
| 9 | * Copyright (C) 2013 Obsidian Research Corp |
| 10 | * Jason Gunthorpe <jgunthorpe@obsidianresearch.com> |
| 11 | * |
| 12 | * Device file system interface to the TPM |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation, version 2 of the |
| 17 | * License. |
| 18 | * |
| 19 | */ |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 21 | #include "tpm-dev.h" |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 22 | |
| 23 | static int tpm_open(struct inode *inode, struct file *file) |
| 24 | { |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 25 | struct tpm_chip *chip; |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 26 | struct file_priv *priv; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 27 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 28 | chip = container_of(inode->i_cdev, struct tpm_chip, cdev); |
| 29 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 30 | /* It's assured that the chip will be opened just once, |
| 31 | * by the check of is_open variable, which is protected |
| 32 | * by driver_lock. */ |
| 33 | if (test_and_set_bit(0, &chip->is_open)) { |
Jason Gunthorpe | 8cfffc9 | 2016-02-29 12:29:47 -0500 | [diff] [blame] | 34 | dev_dbg(&chip->dev, "Another process owns this TPM\n"); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 35 | return -EBUSY; |
| 36 | } |
| 37 | |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 38 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 39 | if (priv == NULL) |
| 40 | goto out; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 41 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 42 | tpm_common_open(file, chip, priv); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 43 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 44 | return 0; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 45 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 46 | out: |
| 47 | clear_bit(0, &chip->is_open); |
| 48 | return -ENOMEM; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static ssize_t tpm_write(struct file *file, const char __user *buf, |
| 52 | size_t size, loff_t *off) |
| 53 | { |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 54 | return tpm_common_write(file, buf, size, off, NULL); |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Called on file close |
| 59 | */ |
| 60 | static int tpm_release(struct inode *inode, struct file *file) |
| 61 | { |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 62 | struct file_priv *priv = file->private_data; |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 63 | |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 64 | tpm_common_release(file, priv); |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 65 | clear_bit(0, &priv->chip->is_open); |
Jason Gunthorpe | e3302e0 | 2013-11-26 13:30:45 -0700 | [diff] [blame] | 66 | kfree(priv); |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 67 | |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
Jarkko Sakkinen | 313d21e | 2014-12-12 11:46:37 -0800 | [diff] [blame] | 71 | const struct file_operations tpm_fops = { |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 72 | .owner = THIS_MODULE, |
| 73 | .llseek = no_llseek, |
| 74 | .open = tpm_open, |
James Bottomley | ecb38e2 | 2017-01-10 19:08:53 -0800 | [diff] [blame^] | 75 | .read = tpm_common_read, |
Jason Gunthorpe | afdba32 | 2013-11-26 13:30:40 -0700 | [diff] [blame] | 76 | .write = tpm_write, |
| 77 | .release = tpm_release, |
| 78 | }; |