pmsg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2014 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/cdev.h>
  14. #include <linux/device.h>
  15. #include <linux/fs.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/vmalloc.h>
  18. #include "internal.h"
  19. static DEFINE_MUTEX(pmsg_lock);
  20. #define PMSG_MAX_BOUNCE_BUFFER_SIZE (2*PAGE_SIZE)
  21. static ssize_t write_pmsg(struct file *file, const char __user *buf,
  22. size_t count, loff_t *ppos)
  23. {
  24. size_t i, buffer_size;
  25. char *buffer;
  26. if (!count)
  27. return 0;
  28. if (!access_ok(VERIFY_READ, buf, count))
  29. return -EFAULT;
  30. buffer_size = count;
  31. if (buffer_size > PMSG_MAX_BOUNCE_BUFFER_SIZE)
  32. buffer_size = PMSG_MAX_BOUNCE_BUFFER_SIZE;
  33. buffer = vmalloc(buffer_size);
  34. mutex_lock(&pmsg_lock);
  35. for (i = 0; i < count; ) {
  36. size_t c = min(count - i, buffer_size);
  37. u64 id;
  38. long ret;
  39. ret = __copy_from_user(buffer, buf + i, c);
  40. if (unlikely(ret != 0)) {
  41. mutex_unlock(&pmsg_lock);
  42. vfree(buffer);
  43. return -EFAULT;
  44. }
  45. psinfo->write_buf(PSTORE_TYPE_PMSG, 0, &id, 0, buffer, 0, c,
  46. psinfo);
  47. i += c;
  48. }
  49. mutex_unlock(&pmsg_lock);
  50. vfree(buffer);
  51. return count;
  52. }
  53. static const struct file_operations pmsg_fops = {
  54. .owner = THIS_MODULE,
  55. .llseek = noop_llseek,
  56. .write = write_pmsg,
  57. };
  58. static struct class *pmsg_class;
  59. static int pmsg_major;
  60. #define PMSG_NAME "pmsg"
  61. #undef pr_fmt
  62. #define pr_fmt(fmt) PMSG_NAME ": " fmt
  63. static char *pmsg_devnode(struct device *dev, umode_t *mode)
  64. {
  65. if (mode)
  66. *mode = 0220;
  67. return NULL;
  68. }
  69. void pstore_register_pmsg(void)
  70. {
  71. struct device *pmsg_device;
  72. pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
  73. if (pmsg_major < 0) {
  74. pr_err("register_chrdev failed\n");
  75. goto err;
  76. }
  77. pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
  78. if (IS_ERR(pmsg_class)) {
  79. pr_err("device class file already in use\n");
  80. goto err_class;
  81. }
  82. pmsg_class->devnode = pmsg_devnode;
  83. pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
  84. NULL, "%s%d", PMSG_NAME, 0);
  85. if (IS_ERR(pmsg_device)) {
  86. pr_err("failed to create device\n");
  87. goto err_device;
  88. }
  89. return;
  90. err_device:
  91. class_destroy(pmsg_class);
  92. err_class:
  93. unregister_chrdev(pmsg_major, PMSG_NAME);
  94. err:
  95. return;
  96. }