pmsg.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. static ssize_t write_pmsg(struct file *file, const char __user *buf,
  21. size_t count, loff_t *ppos)
  22. {
  23. u64 id;
  24. int ret;
  25. if (!count)
  26. return 0;
  27. /* check outside lock, page in any data. write_buf_user also checks */
  28. if (!access_ok(VERIFY_READ, buf, count))
  29. return -EFAULT;
  30. mutex_lock(&pmsg_lock);
  31. ret = psinfo->write_buf_user(PSTORE_TYPE_PMSG, 0, &id, 0, buf, 0, count,
  32. psinfo);
  33. mutex_unlock(&pmsg_lock);
  34. return ret ? ret : count;
  35. }
  36. static const struct file_operations pmsg_fops = {
  37. .owner = THIS_MODULE,
  38. .llseek = noop_llseek,
  39. .write = write_pmsg,
  40. };
  41. static struct class *pmsg_class;
  42. static int pmsg_major;
  43. #define PMSG_NAME "pmsg"
  44. #undef pr_fmt
  45. #define pr_fmt(fmt) PMSG_NAME ": " fmt
  46. static char *pmsg_devnode(struct device *dev, umode_t *mode)
  47. {
  48. if (mode)
  49. *mode = 0220;
  50. return NULL;
  51. }
  52. void pstore_register_pmsg(void)
  53. {
  54. struct device *pmsg_device;
  55. pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
  56. if (pmsg_major < 0) {
  57. pr_err("register_chrdev failed\n");
  58. goto err;
  59. }
  60. pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
  61. if (IS_ERR(pmsg_class)) {
  62. pr_err("device class file already in use\n");
  63. goto err_class;
  64. }
  65. pmsg_class->devnode = pmsg_devnode;
  66. pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
  67. NULL, "%s%d", PMSG_NAME, 0);
  68. if (IS_ERR(pmsg_device)) {
  69. pr_err("failed to create device\n");
  70. goto err_device;
  71. }
  72. return;
  73. err_device:
  74. class_destroy(pmsg_class);
  75. err_class:
  76. unregister_chrdev(pmsg_major, PMSG_NAME);
  77. err:
  78. return;
  79. }
  80. void pstore_unregister_pmsg(void)
  81. {
  82. device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
  83. class_destroy(pmsg_class);
  84. unregister_chrdev(pmsg_major, PMSG_NAME);
  85. }