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 static void user_reader_timeout(unsigned long ptr)
27 struct file_priv *priv = (struct file_priv *)ptr;
29 pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
30 task_tgid_nr(current));
32 schedule_work(&priv->work);
35 static void timeout_work(struct work_struct *work)
37 struct file_priv *priv = container_of(work, struct file_priv, work);
39 mutex_lock(&priv->buffer_mutex);
40 priv->data_pending = 0;
41 memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
42 mutex_unlock(&priv->buffer_mutex);
45 void tpm_common_open(struct file *file, struct tpm_chip *chip,
46 struct file_priv *priv)
49 mutex_init(&priv->buffer_mutex);
50 setup_timer(&priv->user_read_timer, user_reader_timeout,
52 INIT_WORK(&priv->work, timeout_work);
54 file->private_data = priv;
57 ssize_t tpm_common_read(struct file *file, char __user *buf,
58 size_t size, loff_t *off)
60 struct file_priv *priv = file->private_data;
64 del_singleshot_timer_sync(&priv->user_read_timer);
65 flush_work(&priv->work);
66 mutex_lock(&priv->buffer_mutex);
68 if (priv->data_pending) {
69 ret_size = min_t(ssize_t, size, priv->data_pending);
70 rc = copy_to_user(buf, priv->data_buffer, ret_size);
71 memset(priv->data_buffer, 0, priv->data_pending);
75 priv->data_pending = 0;
78 mutex_unlock(&priv->buffer_mutex);
82 ssize_t tpm_common_write(struct file *file, const char __user *buf,
83 size_t size, loff_t *off, struct tpm_space *space)
85 struct file_priv *priv = file->private_data;
86 size_t in_size = size;
89 if (in_size > TPM_BUFSIZE)
92 mutex_lock(&priv->buffer_mutex);
94 /* Cannot perform a write until the read has cleared either via
95 * tpm_read or a user_read_timer timeout. This also prevents split
96 * buffered writes from blocking here.
98 if (priv->data_pending != 0) {
99 mutex_unlock(&priv->buffer_mutex);
104 (priv->data_buffer, (void __user *) buf, in_size)) {
105 mutex_unlock(&priv->buffer_mutex);
110 in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) {
111 mutex_unlock(&priv->buffer_mutex);
115 /* atomic tpm command send and result receive. We only hold the ops
116 * lock during this period so that the tpm can be unregistered even if
117 * the char dev is held open.
119 if (tpm_try_get_ops(priv->chip)) {
120 mutex_unlock(&priv->buffer_mutex);
123 out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
124 sizeof(priv->data_buffer), 0);
126 tpm_put_ops(priv->chip);
128 mutex_unlock(&priv->buffer_mutex);
132 priv->data_pending = out_size;
133 mutex_unlock(&priv->buffer_mutex);
135 /* Set a timeout by which the reader must come claim the result */
136 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
142 * Called on file close
144 void tpm_common_release(struct file *file, struct file_priv *priv)
146 del_singleshot_timer_sync(&priv->user_read_timer);
147 flush_work(&priv->work);
148 file->private_data = NULL;
149 priv->data_pending = 0;