tpm-chip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. * Authors:
  6. * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  7. * Leendert van Doorn <leendert@watson.ibm.com>
  8. * Dave Safford <safford@watson.ibm.com>
  9. * Reiner Sailer <sailer@watson.ibm.com>
  10. * Kylene Hall <kjhall@us.ibm.com>
  11. *
  12. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  13. *
  14. * TPM chip management routines.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation, version 2 of the
  19. * License.
  20. *
  21. */
  22. #include <linux/poll.h>
  23. #include <linux/slab.h>
  24. #include <linux/mutex.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/freezer.h>
  27. #include <linux/major.h>
  28. #include "tpm.h"
  29. #include "tpm_eventlog.h"
  30. static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
  31. static LIST_HEAD(tpm_chip_list);
  32. static DEFINE_SPINLOCK(driver_lock);
  33. struct class *tpm_class;
  34. dev_t tpm_devt;
  35. /*
  36. * tpm_chip_find_get - return tpm_chip for a given chip number
  37. * @chip_num the device number for the chip
  38. */
  39. struct tpm_chip *tpm_chip_find_get(int chip_num)
  40. {
  41. struct tpm_chip *pos, *chip = NULL;
  42. rcu_read_lock();
  43. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  44. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  45. continue;
  46. if (try_module_get(pos->pdev->driver->owner)) {
  47. chip = pos;
  48. break;
  49. }
  50. }
  51. rcu_read_unlock();
  52. return chip;
  53. }
  54. /**
  55. * tpm_dev_release() - free chip memory and the device number
  56. * @dev: the character device for the TPM chip
  57. *
  58. * This is used as the release function for the character device.
  59. */
  60. static void tpm_dev_release(struct device *dev)
  61. {
  62. struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev);
  63. spin_lock(&driver_lock);
  64. clear_bit(chip->dev_num, dev_mask);
  65. spin_unlock(&driver_lock);
  66. kfree(chip);
  67. }
  68. /**
  69. * tpmm_chip_alloc() - allocate a new struct tpm_chip instance
  70. * @dev: device to which the chip is associated
  71. * @ops: struct tpm_class_ops instance
  72. *
  73. * Allocates a new struct tpm_chip instance and assigns a free
  74. * device number for it. Caller does not have to worry about
  75. * freeing the allocated resources. When the devices is removed
  76. * devres calls tpmm_chip_remove() to do the job.
  77. */
  78. struct tpm_chip *tpmm_chip_alloc(struct device *dev,
  79. const struct tpm_class_ops *ops)
  80. {
  81. struct tpm_chip *chip;
  82. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  83. if (chip == NULL)
  84. return ERR_PTR(-ENOMEM);
  85. mutex_init(&chip->tpm_mutex);
  86. INIT_LIST_HEAD(&chip->list);
  87. chip->ops = ops;
  88. spin_lock(&driver_lock);
  89. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  90. spin_unlock(&driver_lock);
  91. if (chip->dev_num >= TPM_NUM_DEVICES) {
  92. dev_err(dev, "No available tpm device numbers\n");
  93. kfree(chip);
  94. return ERR_PTR(-ENOMEM);
  95. }
  96. set_bit(chip->dev_num, dev_mask);
  97. scnprintf(chip->devname, sizeof(chip->devname), "tpm%d", chip->dev_num);
  98. chip->pdev = dev;
  99. dev_set_drvdata(dev, chip);
  100. chip->dev.class = tpm_class;
  101. chip->dev.release = tpm_dev_release;
  102. chip->dev.parent = chip->pdev;
  103. if (chip->dev_num == 0)
  104. chip->dev.devt = MKDEV(MISC_MAJOR, TPM_MINOR);
  105. else
  106. chip->dev.devt = MKDEV(MAJOR(tpm_devt), chip->dev_num);
  107. dev_set_name(&chip->dev, "%s", chip->devname);
  108. device_initialize(&chip->dev);
  109. cdev_init(&chip->cdev, &tpm_fops);
  110. chip->cdev.owner = chip->pdev->driver->owner;
  111. chip->cdev.kobj.parent = &chip->dev.kobj;
  112. return chip;
  113. }
  114. EXPORT_SYMBOL_GPL(tpmm_chip_alloc);
  115. static int tpm_dev_add_device(struct tpm_chip *chip)
  116. {
  117. int rc;
  118. rc = cdev_add(&chip->cdev, chip->dev.devt, 1);
  119. if (rc) {
  120. dev_err(&chip->dev,
  121. "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
  122. chip->devname, MAJOR(chip->dev.devt),
  123. MINOR(chip->dev.devt), rc);
  124. device_unregister(&chip->dev);
  125. return rc;
  126. }
  127. rc = device_add(&chip->dev);
  128. if (rc) {
  129. dev_err(&chip->dev,
  130. "unable to device_register() %s, major %d, minor %d, err=%d\n",
  131. chip->devname, MAJOR(chip->dev.devt),
  132. MINOR(chip->dev.devt), rc);
  133. return rc;
  134. }
  135. return rc;
  136. }
  137. static void tpm_dev_del_device(struct tpm_chip *chip)
  138. {
  139. cdev_del(&chip->cdev);
  140. device_unregister(&chip->dev);
  141. }
  142. static int tpm1_chip_register(struct tpm_chip *chip)
  143. {
  144. int rc;
  145. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  146. return 0;
  147. rc = tpm_sysfs_add_device(chip);
  148. if (rc)
  149. return rc;
  150. rc = tpm_add_ppi(chip);
  151. if (rc) {
  152. tpm_sysfs_del_device(chip);
  153. return rc;
  154. }
  155. chip->bios_dir = tpm_bios_log_setup(chip->devname);
  156. return 0;
  157. }
  158. static void tpm1_chip_unregister(struct tpm_chip *chip)
  159. {
  160. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  161. return;
  162. if (chip->bios_dir)
  163. tpm_bios_log_teardown(chip->bios_dir);
  164. tpm_remove_ppi(chip);
  165. tpm_sysfs_del_device(chip);
  166. }
  167. /*
  168. * tpm_chip_register() - create a character device for the TPM chip
  169. * @chip: TPM chip to use.
  170. *
  171. * Creates a character device for the TPM chip and adds sysfs attributes for
  172. * the device. As the last step this function adds the chip to the list of TPM
  173. * chips available for in-kernel use.
  174. *
  175. * This function should be only called after the chip initialization is
  176. * complete.
  177. */
  178. int tpm_chip_register(struct tpm_chip *chip)
  179. {
  180. int rc;
  181. rc = tpm1_chip_register(chip);
  182. if (rc)
  183. return rc;
  184. rc = tpm_dev_add_device(chip);
  185. if (rc)
  186. goto out_err;
  187. /* Make the chip available. */
  188. spin_lock(&driver_lock);
  189. list_add_rcu(&chip->list, &tpm_chip_list);
  190. spin_unlock(&driver_lock);
  191. chip->flags |= TPM_CHIP_FLAG_REGISTERED;
  192. return 0;
  193. out_err:
  194. tpm1_chip_unregister(chip);
  195. return rc;
  196. }
  197. EXPORT_SYMBOL_GPL(tpm_chip_register);
  198. /*
  199. * tpm_chip_unregister() - release the TPM driver
  200. * @chip: TPM chip to use.
  201. *
  202. * Takes the chip first away from the list of available TPM chips and then
  203. * cleans up all the resources reserved by tpm_chip_register().
  204. *
  205. * NOTE: This function should be only called before deinitializing chip
  206. * resources.
  207. */
  208. void tpm_chip_unregister(struct tpm_chip *chip)
  209. {
  210. if (!(chip->flags & TPM_CHIP_FLAG_REGISTERED))
  211. return;
  212. spin_lock(&driver_lock);
  213. list_del_rcu(&chip->list);
  214. spin_unlock(&driver_lock);
  215. synchronize_rcu();
  216. tpm1_chip_unregister(chip);
  217. tpm_dev_del_device(chip);
  218. }
  219. EXPORT_SYMBOL_GPL(tpm_chip_unregister);