blob: ebd74ab5abef8fa7a1fa2dc5596109916a5da520 [file] [log] [blame]
Jason Gunthorpeafdba322013-11-26 13:30:40 -07001/*
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 Gunthorpeafdba322013-11-26 13:30:40 -070020#include <linux/slab.h>
James Bottomleyecb38e22017-01-10 19:08:53 -080021#include "tpm-dev.h"
Jason Gunthorpeafdba322013-11-26 13:30:40 -070022
23static int tpm_open(struct inode *inode, struct file *file)
24{
James Bottomleyecb38e22017-01-10 19:08:53 -080025 struct tpm_chip *chip;
Jason Gunthorpee3302e02013-11-26 13:30:45 -070026 struct file_priv *priv;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070027
James Bottomleyecb38e22017-01-10 19:08:53 -080028 chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
29
Jason Gunthorpeafdba322013-11-26 13:30:40 -070030 /* 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 Gunthorpe8cfffc92016-02-29 12:29:47 -050034 dev_dbg(&chip->dev, "Another process owns this TPM\n");
Jason Gunthorpeafdba322013-11-26 13:30:40 -070035 return -EBUSY;
36 }
37
Jason Gunthorpee3302e02013-11-26 13:30:45 -070038 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
James Bottomleyecb38e22017-01-10 19:08:53 -080039 if (priv == NULL)
40 goto out;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070041
James Bottomleyecb38e22017-01-10 19:08:53 -080042 tpm_common_open(file, chip, priv);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070043
Jason Gunthorpeafdba322013-11-26 13:30:40 -070044 return 0;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070045
James Bottomleyecb38e22017-01-10 19:08:53 -080046 out:
47 clear_bit(0, &chip->is_open);
48 return -ENOMEM;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070049}
50
51static ssize_t tpm_write(struct file *file, const char __user *buf,
52 size_t size, loff_t *off)
53{
James Bottomleyecb38e22017-01-10 19:08:53 -080054 return tpm_common_write(file, buf, size, off, NULL);
Jason Gunthorpeafdba322013-11-26 13:30:40 -070055}
56
57/*
58 * Called on file close
59 */
60static int tpm_release(struct inode *inode, struct file *file)
61{
Jason Gunthorpee3302e02013-11-26 13:30:45 -070062 struct file_priv *priv = file->private_data;
Jason Gunthorpeafdba322013-11-26 13:30:40 -070063
James Bottomleyecb38e22017-01-10 19:08:53 -080064 tpm_common_release(file, priv);
Jason Gunthorpee3302e02013-11-26 13:30:45 -070065 clear_bit(0, &priv->chip->is_open);
Jason Gunthorpee3302e02013-11-26 13:30:45 -070066 kfree(priv);
James Bottomleyecb38e22017-01-10 19:08:53 -080067
Jason Gunthorpeafdba322013-11-26 13:30:40 -070068 return 0;
69}
70
Jarkko Sakkinen313d21e2014-12-12 11:46:37 -080071const struct file_operations tpm_fops = {
Jason Gunthorpeafdba322013-11-26 13:30:40 -070072 .owner = THIS_MODULE,
73 .llseek = no_llseek,
74 .open = tpm_open,
James Bottomleyecb38e22017-01-10 19:08:53 -080075 .read = tpm_common_read,
Jason Gunthorpeafdba322013-11-26 13:30:40 -070076 .write = tpm_write,
77 .release = tpm_release,
78};