tpm-dev.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. * Authors:
  5. * Leendert van Doorn <leendert@watson.ibm.com>
  6. * Dave Safford <safford@watson.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Copyright (C) 2013 Obsidian Research Corp
  11. * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
  12. *
  13. * Device file system interface to the TPM
  14. */
  15. #include <linux/slab.h>
  16. #include "tpm-dev.h"
  17. static int tpm_open(struct inode *inode, struct file *file)
  18. {
  19. struct tpm_chip *chip;
  20. struct file_priv *priv;
  21. chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
  22. /* It's assured that the chip will be opened just once,
  23. * by the check of is_open variable, which is protected
  24. * by driver_lock. */
  25. if (test_and_set_bit(0, &chip->is_open)) {
  26. dev_dbg(&chip->dev, "Another process owns this TPM\n");
  27. return -EBUSY;
  28. }
  29. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  30. if (priv == NULL)
  31. goto out;
  32. tpm_common_open(file, chip, priv, NULL);
  33. return 0;
  34. out:
  35. clear_bit(0, &chip->is_open);
  36. return -ENOMEM;
  37. }
  38. /*
  39. * Called on file close
  40. */
  41. static int tpm_release(struct inode *inode, struct file *file)
  42. {
  43. struct file_priv *priv = file->private_data;
  44. tpm_common_release(file, priv);
  45. clear_bit(0, &priv->chip->is_open);
  46. kfree(priv);
  47. return 0;
  48. }
  49. const struct file_operations tpm_fops = {
  50. .owner = THIS_MODULE,
  51. .open = tpm_open,
  52. .read = tpm_common_read,
  53. .write = tpm_common_write,
  54. .poll = tpm_common_poll,
  55. .release = tpm_release,
  56. };