2 * Copyright (C) 2004 IBM Corporation
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>
9 * Copyright (C) 2013 Obsidian Research Corp
10 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12 * Device file system interface to the TPM
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
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
25 struct tpm_chip *chip;
27 /* Data passed to and from the tpm via the read/write calls */
29 struct mutex buffer_mutex;
31 struct timer_list user_read_timer; /* user needs to claim result */
32 struct work_struct work;
34 u8 data_buffer[TPM_BUFSIZE];
37 static void user_reader_timeout(unsigned long ptr)
39 struct file_priv *priv = (struct file_priv *)ptr;
41 schedule_work(&priv->work);
44 static void timeout_work(struct work_struct *work)
46 struct file_priv *priv = container_of(work, struct file_priv, work);
48 mutex_lock(&priv->buffer_mutex);
49 priv->data_pending = 0;
50 memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
51 mutex_unlock(&priv->buffer_mutex);
54 static int tpm_open(struct inode *inode, struct file *file)
56 struct tpm_chip *chip =
57 container_of(inode->i_cdev, struct tpm_chip, cdev);
58 struct file_priv *priv;
60 /* It's assured that the chip will be opened just once,
61 * by the check of is_open variable, which is protected
63 if (test_and_set_bit(0, &chip->is_open)) {
64 dev_dbg(&chip->dev, "Another process owns this TPM\n");
68 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
70 clear_bit(0, &chip->is_open);
75 mutex_init(&priv->buffer_mutex);
76 setup_timer(&priv->user_read_timer, user_reader_timeout,
78 INIT_WORK(&priv->work, timeout_work);
80 file->private_data = priv;
84 static ssize_t tpm_read(struct file *file, char __user *buf,
85 size_t size, loff_t *off)
87 struct file_priv *priv = file->private_data;
91 del_singleshot_timer_sync(&priv->user_read_timer);
92 flush_work(&priv->work);
93 mutex_lock(&priv->buffer_mutex);
95 if (priv->data_pending) {
96 ret_size = min_t(ssize_t, size, priv->data_pending);
97 rc = copy_to_user(buf, priv->data_buffer, ret_size);
98 memset(priv->data_buffer, 0, priv->data_pending);
102 priv->data_pending = 0;
105 mutex_unlock(&priv->buffer_mutex);
109 static ssize_t tpm_write(struct file *file, const char __user *buf,
110 size_t size, loff_t *off)
112 struct file_priv *priv = file->private_data;
113 size_t in_size = size;
116 if (in_size > TPM_BUFSIZE)
119 mutex_lock(&priv->buffer_mutex);
121 /* Cannot perform a write until the read has cleared either via
122 * tpm_read or a user_read_timer timeout. This also prevents split
123 * buffered writes from blocking here.
125 if (priv->data_pending != 0) {
126 mutex_unlock(&priv->buffer_mutex);
131 (priv->data_buffer, (void __user *) buf, in_size)) {
132 mutex_unlock(&priv->buffer_mutex);
137 in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) {
138 mutex_unlock(&priv->buffer_mutex);
142 /* atomic tpm command send and result receive. We only hold the ops
143 * lock during this period so that the tpm can be unregistered even if
144 * the char dev is held open.
146 if (tpm_try_get_ops(priv->chip)) {
147 mutex_unlock(&priv->buffer_mutex);
150 out_size = tpm_transmit(priv->chip, priv->data_buffer,
151 sizeof(priv->data_buffer), 0);
153 tpm_put_ops(priv->chip);
155 mutex_unlock(&priv->buffer_mutex);
159 priv->data_pending = out_size;
160 mutex_unlock(&priv->buffer_mutex);
162 /* Set a timeout by which the reader must come claim the result */
163 mod_timer(&priv->user_read_timer, jiffies + (60 * HZ));
169 * Called on file close
171 static int tpm_release(struct inode *inode, struct file *file)
173 struct file_priv *priv = file->private_data;
175 del_singleshot_timer_sync(&priv->user_read_timer);
176 flush_work(&priv->work);
177 file->private_data = NULL;
178 priv->data_pending = 0;
179 clear_bit(0, &priv->chip->is_open);
184 const struct file_operations tpm_fops = {
185 .owner = THIS_MODULE,
190 .release = tpm_release,