tpm-dev.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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->pdev, "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. get_device(chip->pdev);
  70. return 0;
  71. }
  72. static ssize_t tpm_read(struct file *file, char __user *buf,
  73. size_t size, loff_t *off)
  74. {
  75. struct file_priv *priv = file->private_data;
  76. ssize_t ret_size;
  77. int rc;
  78. del_singleshot_timer_sync(&priv->user_read_timer);
  79. flush_work(&priv->work);
  80. ret_size = atomic_read(&priv->data_pending);
  81. if (ret_size > 0) { /* relay data */
  82. ssize_t orig_ret_size = ret_size;
  83. if (size < ret_size)
  84. ret_size = size;
  85. mutex_lock(&priv->buffer_mutex);
  86. rc = copy_to_user(buf, priv->data_buffer, ret_size);
  87. memset(priv->data_buffer, 0, orig_ret_size);
  88. if (rc)
  89. ret_size = -EFAULT;
  90. mutex_unlock(&priv->buffer_mutex);
  91. }
  92. atomic_set(&priv->data_pending, 0);
  93. return ret_size;
  94. }
  95. static ssize_t tpm_write(struct file *file, const char __user *buf,
  96. size_t size, loff_t *off)
  97. {
  98. struct file_priv *priv = file->private_data;
  99. size_t in_size = size;
  100. ssize_t out_size;
  101. /* cannot perform a write until the read has cleared
  102. either via tpm_read or a user_read_timer timeout.
  103. This also prevents splitted buffered writes from blocking here.
  104. */
  105. if (atomic_read(&priv->data_pending) != 0)
  106. return -EBUSY;
  107. if (in_size > TPM_BUFSIZE)
  108. return -E2BIG;
  109. mutex_lock(&priv->buffer_mutex);
  110. if (copy_from_user
  111. (priv->data_buffer, (void __user *) buf, in_size)) {
  112. mutex_unlock(&priv->buffer_mutex);
  113. return -EFAULT;
  114. }
  115. /* atomic tpm command send and result receive */
  116. out_size = tpm_transmit(priv->chip, priv->data_buffer,
  117. sizeof(priv->data_buffer));
  118. if (out_size < 0) {
  119. mutex_unlock(&priv->buffer_mutex);
  120. return out_size;
  121. }
  122. atomic_set(&priv->data_pending, out_size);
  123. mutex_unlock(&priv->buffer_mutex);
  124. /* Set a timeout by which the reader must come claim the result */
  125. mod_timer(&priv->user_read_timer, jiffies + (60 * HZ));
  126. return in_size;
  127. }
  128. /*
  129. * Called on file close
  130. */
  131. static int tpm_release(struct inode *inode, struct file *file)
  132. {
  133. struct file_priv *priv = file->private_data;
  134. del_singleshot_timer_sync(&priv->user_read_timer);
  135. flush_work(&priv->work);
  136. file->private_data = NULL;
  137. atomic_set(&priv->data_pending, 0);
  138. clear_bit(0, &priv->chip->is_open);
  139. put_device(priv->chip->pdev);
  140. kfree(priv);
  141. return 0;
  142. }
  143. const struct file_operations tpm_fops = {
  144. .owner = THIS_MODULE,
  145. .llseek = no_llseek,
  146. .open = tpm_open,
  147. .read = tpm_read,
  148. .write = tpm_write,
  149. .release = tpm_release,
  150. };