tpm-dev.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "tpm-dev.h"
  22. static int tpm_open(struct inode *inode, struct file *file)
  23. {
  24. struct tpm_chip *chip;
  25. struct file_priv *priv;
  26. chip = container_of(inode->i_cdev, struct tpm_chip, cdev);
  27. /* It's assured that the chip will be opened just once,
  28. * by the check of is_open variable, which is protected
  29. * by driver_lock. */
  30. if (test_and_set_bit(0, &chip->is_open)) {
  31. dev_dbg(&chip->dev, "Another process owns this TPM\n");
  32. return -EBUSY;
  33. }
  34. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  35. if (priv == NULL)
  36. goto out;
  37. tpm_common_open(file, chip, priv);
  38. return 0;
  39. out:
  40. clear_bit(0, &chip->is_open);
  41. return -ENOMEM;
  42. }
  43. static ssize_t tpm_write(struct file *file, const char __user *buf,
  44. size_t size, loff_t *off)
  45. {
  46. return tpm_common_write(file, buf, size, off, NULL);
  47. }
  48. /*
  49. * Called on file close
  50. */
  51. static int tpm_release(struct inode *inode, struct file *file)
  52. {
  53. struct file_priv *priv = file->private_data;
  54. tpm_common_release(file, priv);
  55. clear_bit(0, &priv->chip->is_open);
  56. kfree(priv);
  57. return 0;
  58. }
  59. const struct file_operations tpm_fops = {
  60. .owner = THIS_MODULE,
  61. .llseek = no_llseek,
  62. .open = tpm_open,
  63. .read = tpm_common_read,
  64. .write = tpm_write,
  65. .release = tpm_release,
  66. };