tpmrm-dev.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2017 James.Bottomley@HansenPartnership.com
  3. *
  4. * GPLv2
  5. */
  6. #include <linux/slab.h>
  7. #include "tpm-dev.h"
  8. struct tpmrm_priv {
  9. struct file_priv priv;
  10. struct tpm_space space;
  11. };
  12. static int tpmrm_open(struct inode *inode, struct file *file)
  13. {
  14. struct tpm_chip *chip;
  15. struct tpmrm_priv *priv;
  16. int rc;
  17. chip = container_of(inode->i_cdev, struct tpm_chip, cdevs);
  18. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  19. if (priv == NULL)
  20. return -ENOMEM;
  21. rc = tpm2_init_space(&priv->space);
  22. if (rc) {
  23. kfree(priv);
  24. return -ENOMEM;
  25. }
  26. tpm_common_open(file, chip, &priv->priv);
  27. return 0;
  28. }
  29. static int tpmrm_release(struct inode *inode, struct file *file)
  30. {
  31. struct file_priv *fpriv = file->private_data;
  32. struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
  33. tpm_common_release(file, fpriv);
  34. tpm2_del_space(fpriv->chip, &priv->space);
  35. kfree(priv);
  36. return 0;
  37. }
  38. static ssize_t tpmrm_write(struct file *file, const char __user *buf,
  39. size_t size, loff_t *off)
  40. {
  41. struct file_priv *fpriv = file->private_data;
  42. struct tpmrm_priv *priv = container_of(fpriv, struct tpmrm_priv, priv);
  43. return tpm_common_write(file, buf, size, off, &priv->space);
  44. }
  45. const struct file_operations tpmrm_fops = {
  46. .owner = THIS_MODULE,
  47. .llseek = no_llseek,
  48. .open = tpmrm_open,
  49. .read = tpm_common_read,
  50. .write = tpmrm_write,
  51. .release = tpmrm_release,
  52. };