tpm-dev.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. * Authors:
  5. * Leendert van Doorn <[email protected]>
  6. * Dave Safford <[email protected]>
  7. * Reiner Sailer <[email protected]>
  8. * Kylene Hall <[email protected]>
  9. *
  10. * Copyright (C) 2013 Obsidian Research Corp
  11. * Jason Gunthorpe <[email protected]>
  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. .llseek = no_llseek,
  52. .open = tpm_open,
  53. .read = tpm_common_read,
  54. .write = tpm_common_write,
  55. .poll = tpm_common_poll,
  56. .release = tpm_release,
  57. };