pmsg.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if (!buffer)
  35. return -ENOMEM;
  36. mutex_lock(&pmsg_lock);
  37. for (i = 0; i < count; ) {
  38. size_t c = min(count - i, buffer_size);
  39. u64 id;
  40. long ret;
  41. ret = __copy_from_user(buffer, buf + i, c);
  42. if (unlikely(ret != 0)) {
  43. mutex_unlock(&pmsg_lock);
  44. vfree(buffer);
  45. return -EFAULT;
  46. }
  47. psinfo->write_buf(PSTORE_TYPE_PMSG, 0, &id, 0, buffer, 0, c,
  48. psinfo);
  49. i += c;
  50. }
  51. mutex_unlock(&pmsg_lock);
  52. vfree(buffer);
  53. return count;
  54. }
  55. static const struct file_operations pmsg_fops = {
  56. .owner = THIS_MODULE,
  57. .llseek = noop_llseek,
  58. .write = write_pmsg,
  59. };
  60. static struct class *pmsg_class;
  61. static int pmsg_major;
  62. #define PMSG_NAME "pmsg"
  63. #undef pr_fmt
  64. #define pr_fmt(fmt) PMSG_NAME ": " fmt
  65. static char *pmsg_devnode(struct device *dev, umode_t *mode)
  66. {
  67. if (mode)
  68. *mode = 0220;
  69. return NULL;
  70. }
  71. void pstore_register_pmsg(void)
  72. {
  73. struct device *pmsg_device;
  74. pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
  75. if (pmsg_major < 0) {
  76. pr_err("register_chrdev failed\n");
  77. goto err;
  78. }
  79. pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
  80. if (IS_ERR(pmsg_class)) {
  81. pr_err("device class file already in use\n");
  82. goto err_class;
  83. }
  84. pmsg_class->devnode = pmsg_devnode;
  85. pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
  86. NULL, "%s%d", PMSG_NAME, 0);
  87. if (IS_ERR(pmsg_device)) {
  88. pr_err("failed to create device\n");
  89. goto err_device;
  90. }
  91. return;
  92. err_device:
  93. class_destroy(pmsg_class);
  94. err_class:
  95. unregister_chrdev(pmsg_major, PMSG_NAME);
  96. err:
  97. return;
  98. }
  99. void pstore_unregister_pmsg(void)
  100. {
  101. device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
  102. class_destroy(pmsg_class);
  103. unregister_chrdev(pmsg_major, PMSG_NAME);
  104. }