static ssize_t tpm_write(struct file *file, const char __user *buf, size_t size, loff_t *off) { struct file_priv *priv = file->private_data; size_t in_size = size; ssize_t out_size; /* cannot perform a write until the read has cleared either via tpm_read or a user_read_timer timeout. This also prevents splitted buffered writes from blocking here. */ if (atomic_read(&priv->data_pending) != 0) return -EBUSY; if (in_size > TPM_BUFSIZE) return -E2BIG; mutex_lock(&priv->buffer_mutex); if (copy_from_user (priv->data_buffer, (void __user *) buf, in_size)) { mutex_unlock(&priv->buffer_mutex); return -EFAULT; } /* atomic tpm command send and result receive. We only hold the ops * lock during this period so that the tpm can be unregistered even if * the char dev is held open. */ if (tpm_try_get_ops(priv->chip)) { mutex_unlock(&priv->buffer_mutex); return -EPIPE; } out_size = tpm_transmit(priv->chip, priv->data_buffer, sizeof(priv->data_buffer)); tpm_put_ops(priv->chip); if (out_size < 0) { mutex_unlock(&priv->buffer_mutex); return out_size; } atomic_set(&priv->data_pending, out_size); mutex_unlock(&priv->buffer_mutex); /* Set a timeout by which the reader must come claim the result */ mod_timer(&priv->user_read_timer, jiffies + (60 * HZ)); return in_size; }
ssize_t tpm_common_write(struct file *file, const char __user *buf, size_t size, loff_t *off) { struct file_priv *priv = file->private_data; int ret = 0; if (size > TPM_BUFSIZE) return -E2BIG; mutex_lock(&priv->buffer_mutex); /* Cannot perform a write until the read has cleared either via * tpm_read or a user_read_timer timeout. This also prevents split * buffered writes from blocking here. */ if ((!priv->response_read && priv->response_length) || priv->command_enqueued) { ret = -EBUSY; goto out; } if (copy_from_user(priv->data_buffer, buf, size)) { ret = -EFAULT; goto out; } if (size < 6 || size < be32_to_cpu(*((__be32 *)(priv->data_buffer + 2)))) { ret = -EINVAL; goto out; } /* atomic tpm command send and result receive. We only hold the ops * lock during this period so that the tpm can be unregistered even if * the char dev is held open. */ if (tpm_try_get_ops(priv->chip)) { ret = -EPIPE; goto out; } priv->response_length = 0; priv->response_read = false; *off = 0; /* * If in nonblocking mode schedule an async job to send * the command return the size. * In case of error the err code will be returned in * the subsequent read call. */ if (file->f_flags & O_NONBLOCK) { priv->command_enqueued = true; queue_work(tpm_dev_wq, &priv->async_work); mutex_unlock(&priv->buffer_mutex); return size; } ret = tpm_dev_transmit(priv->chip, priv->space, priv->data_buffer, sizeof(priv->data_buffer)); tpm_put_ops(priv->chip); if (ret > 0) { priv->response_length = ret; mod_timer(&priv->user_read_timer, jiffies + (120 * HZ)); ret = size; } out: mutex_unlock(&priv->buffer_mutex); return ret; }