tpm-dev.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  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. */
  20. #include <linux/slab.h>
  21. #include <linux/uaccess.h>
  22. #include "tpm.h"
  23. struct file_priv {
  24. struct tpm_chip *chip;
  25. /* Data passed to and from the tpm via the read/write calls */
  26. size_t data_pending;
  27. struct mutex buffer_mutex;
  28. struct timer_list user_read_timer; /* user needs to claim result */
  29. struct work_struct work;
  30. u8 data_buffer[TPM_BUFSIZE];
  31. };
  32. static void user_reader_timeout(unsigned long ptr)
  33. {
  34. struct file_priv *priv = (struct file_priv *)ptr;
  35. schedule_work(&priv->work);
  36. }
  37. static void timeout_work(struct work_struct *work)
  38. {
  39. struct file_priv *priv = container_of(work, struct file_priv, work);
  40. mutex_lock(&priv->buffer_mutex);
  41. priv->data_pending = 0;
  42. memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
  43. mutex_unlock(&priv->buffer_mutex);
  44. }
  45. static int tpm_open(struct inode *inode, struct file *file)
  46. {
  47. struct tpm_chip *chip =
  48. container_of(inode->i_cdev, struct tpm_chip, cdev);
  49. struct file_priv *priv;
  50. /* It's assured that the chip will be opened just once,
  51. * by the check of is_open variable, which is protected
  52. * by driver_lock. */
  53. if (test_and_set_bit(0, &chip->is_open)) {
  54. dev_dbg(&chip->dev, "Another process owns this TPM\n");
  55. return -EBUSY;
  56. }
  57. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  58. if (priv == NULL) {
  59. clear_bit(0, &chip->is_open);
  60. return -ENOMEM;
  61. }
  62. priv->chip = chip;
  63. mutex_init(&priv->buffer_mutex);
  64. setup_timer(&priv->user_read_timer, user_reader_timeout,
  65. (unsigned long)priv);
  66. INIT_WORK(&priv->work, timeout_work);
  67. file->private_data = priv;
  68. return 0;
  69. }
  70. static ssize_t tpm_read(struct file *file, char __user *buf,
  71. size_t size, loff_t *off)
  72. {
  73. struct file_priv *priv = file->private_data;
  74. ssize_t ret_size = 0;
  75. int rc;
  76. del_singleshot_timer_sync(&priv->user_read_timer);
  77. flush_work(&priv->work);
  78. mutex_lock(&priv->buffer_mutex);
  79. if (priv->data_pending) {
  80. ret_size = min_t(ssize_t, size, priv->data_pending);
  81. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  82. memset(priv->data_buffer, 0, priv->data_pending);
  83. if (rc)
  84. ret_size = -EFAULT;
  85. priv->data_pending = 0;
  86. }
  87. mutex_unlock(&priv->buffer_mutex);
  88. return ret_size;
  89. }
  90. static ssize_t tpm_write(struct file *file, const char __user *buf,
  91. size_t size, loff_t *off)
  92. {
  93. struct file_priv *priv = file->private_data;
  94. size_t in_size = size;
  95. ssize_t out_size;
  96. if (in_size > TPM_BUFSIZE)
  97. return -E2BIG;
  98. mutex_lock(&priv->buffer_mutex);
  99. /* Cannot perform a write until the read has cleared either via
  100. * tpm_read or a user_read_timer timeout. This also prevents split
  101. * buffered writes from blocking here.
  102. */
  103. if (priv->data_pending != 0) {
  104. mutex_unlock(&priv->buffer_mutex);
  105. return -EBUSY;
  106. }
  107. if (copy_from_user
  108. (priv->data_buffer, (void __user *) buf, in_size)) {
  109. mutex_unlock(&priv->buffer_mutex);
  110. return -EFAULT;
  111. }
  112. /* atomic tpm command send and result receive. We only hold the ops
  113. * lock during this period so that the tpm can be unregistered even if
  114. * the char dev is held open.
  115. */
  116. if (tpm_try_get_ops(priv->chip)) {
  117. mutex_unlock(&priv->buffer_mutex);
  118. return -EPIPE;
  119. }
  120. out_size = tpm_transmit(priv->chip, priv->data_buffer,
  121. sizeof(priv->data_buffer), 0);
  122. tpm_put_ops(priv->chip);
  123. if (out_size < 0) {
  124. mutex_unlock(&priv->buffer_mutex);
  125. return out_size;
  126. }
  127. priv->data_pending = out_size;
  128. mutex_unlock(&priv->buffer_mutex);
  129. /* Set a timeout by which the reader must come claim the result */
  130. mod_timer(&priv->user_read_timer, jiffies + (60 * HZ));
  131. return in_size;
  132. }
  133. /*
  134. * Called on file close
  135. */
  136. static int tpm_release(struct inode *inode, struct file *file)
  137. {
  138. struct file_priv *priv = file->private_data;
  139. del_singleshot_timer_sync(&priv->user_read_timer);
  140. flush_work(&priv->work);
  141. file->private_data = NULL;
  142. priv->data_pending = 0;
  143. clear_bit(0, &priv->chip->is_open);
  144. kfree(priv);
  145. return 0;
  146. }
  147. const struct file_operations tpm_fops = {
  148. .owner = THIS_MODULE,
  149. .llseek = no_llseek,
  150. .open = tpm_open,
  151. .read = tpm_read,
  152. .write = tpm_write,
  153. .release = tpm_release,
  154. };