^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2004 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Leendert van Doorn <leendert@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Dave Safford <safford@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Reiner Sailer <sailer@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Kylene Hall <kjhall@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2013 Obsidian Research Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Device file system interface to the TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "tpm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "tpm-dev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static struct workqueue_struct *tpm_dev_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static DEFINE_MUTEX(tpm_dev_wq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u8 *buf, size_t bufsiz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct tpm_header *header = (void *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ssize_t ret, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ret = tpm2_prepare_space(chip, space, buf, bufsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* If the command is not implemented by the TPM, synthesize a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * response with a TPM2_RC_COMMAND_CODE return for user-space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (ret == -EOPNOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) header->length = cpu_to_be32(sizeof(*header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) TSS2_RESMGR_TPM_RC_LAYER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ret = sizeof(*header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) goto out_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) len = tpm_transmit(chip, buf, bufsiz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ret = tpm2_commit_space(chip, space, buf, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) out_rc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return ret ? ret : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void tpm_dev_async_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct file_priv *priv =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) container_of(work, struct file_priv, async_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) mutex_lock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) priv->command_enqueued = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = tpm_try_get_ops(priv->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) priv->response_length = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) sizeof(priv->data_buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) tpm_put_ops(priv->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * If ret is > 0 then tpm_dev_transmit returned the size of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * response. If ret is < 0 then tpm_dev_transmit failed and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * returned an error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) priv->response_length = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) mutex_unlock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) wake_up_interruptible(&priv->async_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static void user_reader_timeout(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct file_priv *priv = from_timer(priv, t, user_read_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) task_tgid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) schedule_work(&priv->timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static void tpm_timeout_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct file_priv *priv = container_of(work, struct file_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mutex_lock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) priv->response_read = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) priv->response_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) mutex_unlock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) wake_up_interruptible(&priv->async_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void tpm_common_open(struct file *file, struct tpm_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct file_priv *priv, struct tpm_space *space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) priv->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) priv->space = space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) priv->response_read = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) mutex_init(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) timer_setup(&priv->user_read_timer, user_reader_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) INIT_WORK(&priv->timeout_work, tpm_timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) INIT_WORK(&priv->async_work, tpm_dev_async_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) init_waitqueue_head(&priv->async_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) file->private_data = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ssize_t tpm_common_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) size_t size, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct file_priv *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) ssize_t ret_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) mutex_lock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (priv->response_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) priv->response_read = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ret_size = min_t(ssize_t, size, priv->response_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (ret_size <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) priv->response_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) rc = copy_to_user(buf, priv->data_buffer + *off, ret_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) memset(priv->data_buffer, 0, TPM_BUFSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) priv->response_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret_size = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) memset(priv->data_buffer + *off, 0, ret_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) priv->response_length -= ret_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *off += ret_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!priv->response_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) del_singleshot_timer_sync(&priv->user_read_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) flush_work(&priv->timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) mutex_unlock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return ret_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ssize_t tpm_common_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) size_t size, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct file_priv *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (size > TPM_BUFSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) mutex_lock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Cannot perform a write until the read has cleared either via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * tpm_read or a user_read_timer timeout. This also prevents split
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * buffered writes from blocking here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if ((!priv->response_read && priv->response_length) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) priv->command_enqueued) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (copy_from_user(priv->data_buffer, buf, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (size < 6 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) priv->response_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) priv->response_read = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * If in nonblocking mode schedule an async job to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * the command return the size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * In case of error the err code will be returned in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * the subsequent read call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) priv->command_enqueued = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) queue_work(tpm_dev_wq, &priv->async_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) mutex_unlock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* atomic tpm command send and result receive. We only hold the ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * lock during this period so that the tpm can be unregistered even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * the char dev is held open.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (tpm_try_get_ops(priv->chip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ret = -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) sizeof(priv->data_buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) tpm_put_ops(priv->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) priv->response_length = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) mutex_unlock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) __poll_t tpm_common_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct file_priv *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) __poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) poll_wait(file, &priv->async_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) mutex_lock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * The response_length indicates if there is still response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * (or part of it) to be consumed. Partial reads decrease it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * by the number of bytes read, and write resets it the zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (priv->response_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) mask = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) mask = EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) mutex_unlock(&priv->buffer_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * Called on file close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) void tpm_common_release(struct file *file, struct file_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) flush_work(&priv->async_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) del_singleshot_timer_sync(&priv->user_read_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) flush_work(&priv->timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) priv->response_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int __init tpm_dev_common_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return !tpm_dev_wq ? -ENOMEM : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) void __exit tpm_dev_common_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (tpm_dev_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) destroy_workqueue(tpm_dev_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) tpm_dev_wq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }