tpm-dev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. atomic_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. atomic_set(&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. atomic_set(&priv->data_pending, 0);
  64. mutex_init(&priv->buffer_mutex);
  65. setup_timer(&priv->user_read_timer, user_reader_timeout,
  66. (unsigned long)priv);
  67. INIT_WORK(&priv->work, timeout_work);
  68. file->private_data = priv;
  69. return 0;
  70. }
  71. static ssize_t tpm_read(struct file *file, char __user *buf,
  72. size_t size, loff_t *off)
  73. {
  74. struct file_priv *priv = file->private_data;
  75. ssize_t ret_size;
  76. int rc;
  77. del_singleshot_timer_sync(&priv->user_read_timer);
  78. flush_work(&priv->work);
  79. ret_size = atomic_read(&priv->data_pending);
  80. if (ret_size > 0) { /* relay data */
  81. ssize_t orig_ret_size = ret_size;
  82. if (size < ret_size)
  83. ret_size = size;
  84. mutex_lock(&priv->buffer_mutex);
  85. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  86. memset(priv->data_buffer, 0, orig_ret_size);
  87. if (rc)
  88. ret_size = -EFAULT;
  89. mutex_unlock(&priv->buffer_mutex);
  90. }
  91. atomic_set(&priv->data_pending, 0);
  92. return ret_size;
  93. }
  94. static ssize_t tpm_write(struct file *file, const char __user *buf,
  95. size_t size, loff_t *off)
  96. {
  97. struct file_priv *priv = file->private_data;
  98. size_t in_size = size;
  99. ssize_t out_size;
  100. /* cannot perform a write until the read has cleared
  101. either via tpm_read or a user_read_timer timeout.
  102. This also prevents splitted buffered writes from blocking here.
  103. */
  104. if (atomic_read(&priv->data_pending) != 0)
  105. return -EBUSY;
  106. if (in_size > TPM_BUFSIZE)
  107. return -E2BIG;
  108. mutex_lock(&priv->buffer_mutex);
  109. if (copy_from_user
  110. (priv->data_buffer, (void __user *) buf, in_size)) {
  111. mutex_unlock(&priv->buffer_mutex);
  112. return -EFAULT;
  113. }
  114. if (in_size < 6 ||
  115. in_size < be32_to_cpu(*((__be32 *) (priv->data_buffer + 2)))) {
  116. mutex_unlock(&priv->buffer_mutex);
  117. return -EINVAL;
  118. }
  119. /* atomic tpm command send and result receive. We only hold the ops
  120. * lock during this period so that the tpm can be unregistered even if
  121. * the char dev is held open.
  122. */
  123. if (tpm_try_get_ops(priv->chip)) {
  124. mutex_unlock(&priv->buffer_mutex);
  125. return -EPIPE;
  126. }
  127. out_size = tpm_transmit(priv->chip, priv->data_buffer,
  128. sizeof(priv->data_buffer), 0);
  129. tpm_put_ops(priv->chip);
  130. if (out_size < 0) {
  131. mutex_unlock(&priv->buffer_mutex);
  132. return out_size;
  133. }
  134. atomic_set(&priv->data_pending, out_size);
  135. mutex_unlock(&priv->buffer_mutex);
  136. /* Set a timeout by which the reader must come claim the result */
  137. mod_timer(&priv->user_read_timer, jiffies + (60 * HZ));
  138. return in_size;
  139. }
  140. /*
  141. * Called on file close
  142. */
  143. static int tpm_release(struct inode *inode, struct file *file)
  144. {
  145. struct file_priv *priv = file->private_data;
  146. del_singleshot_timer_sync(&priv->user_read_timer);
  147. flush_work(&priv->work);
  148. file->private_data = NULL;
  149. atomic_set(&priv->data_pending, 0);
  150. clear_bit(0, &priv->chip->is_open);
  151. kfree(priv);
  152. return 0;
  153. }
  154. const struct file_operations tpm_fops = {
  155. .owner = THIS_MODULE,
  156. .llseek = no_llseek,
  157. .open = tpm_open,
  158. .read = tpm_read,
  159. .write = tpm_write,
  160. .release = tpm_release,
  161. };