blob: 78f6176c020f8256dad089221af85d9560e5ac2f [file] [log] [blame]
Mark Salyzyn9d5438f2015-01-16 16:01:10 -08001/*
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
14#include <linux/cdev.h>
15#include <linux/device.h>
16#include <linux/fs.h>
17#include <linux/uaccess.h>
18#include <linux/vmalloc.h>
19#include "internal.h"
20
21static DEFINE_MUTEX(pmsg_lock);
Mark Salyzyn9d5438f2015-01-16 16:01:10 -080022
23static ssize_t write_pmsg(struct file *file, const char __user *buf,
24 size_t count, loff_t *ppos)
25{
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -070026 u64 id;
27 int ret;
Mark Salyzyn9d5438f2015-01-16 16:01:10 -080028
29 if (!count)
30 return 0;
31
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -070032 /* check outside lock, page in any data. write_buf_user also checks */
Mark Salyzyn9d5438f2015-01-16 16:01:10 -080033 if (!access_ok(VERIFY_READ, buf, count))
34 return -EFAULT;
35
Mark Salyzyn9d5438f2015-01-16 16:01:10 -080036 mutex_lock(&pmsg_lock);
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -070037 ret = psinfo->write_buf_user(PSTORE_TYPE_PMSG, 0, &id, 0, buf, 0, count,
38 psinfo);
Mark Salyzyn9d5438f2015-01-16 16:01:10 -080039 mutex_unlock(&pmsg_lock);
Mark Salyzyn5bf6d1b2016-09-01 08:13:46 -070040 return ret ? ret : count;
Mark Salyzyn9d5438f2015-01-16 16:01:10 -080041}
42
43static const struct file_operations pmsg_fops = {
44 .owner = THIS_MODULE,
45 .llseek = noop_llseek,
46 .write = write_pmsg,
47};
48
49static struct class *pmsg_class;
50static int pmsg_major;
51#define PMSG_NAME "pmsg"
52#undef pr_fmt
53#define pr_fmt(fmt) PMSG_NAME ": " fmt
54
55static char *pmsg_devnode(struct device *dev, umode_t *mode)
56{
57 if (mode)
58 *mode = 0220;
59 return NULL;
60}
61
62void pstore_register_pmsg(void)
63{
64 struct device *pmsg_device;
65
66 pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
67 if (pmsg_major < 0) {
68 pr_err("register_chrdev failed\n");
69 goto err;
70 }
71
72 pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
73 if (IS_ERR(pmsg_class)) {
74 pr_err("device class file already in use\n");
75 goto err_class;
76 }
77 pmsg_class->devnode = pmsg_devnode;
78
79 pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
80 NULL, "%s%d", PMSG_NAME, 0);
81 if (IS_ERR(pmsg_device)) {
82 pr_err("failed to create device\n");
83 goto err_device;
84 }
85 return;
86
87err_device:
88 class_destroy(pmsg_class);
89err_class:
90 unregister_chrdev(pmsg_major, PMSG_NAME);
91err:
92 return;
93}
Geliang Tangee1d2672015-10-20 00:39:03 -070094
95void pstore_unregister_pmsg(void)
96{
97 device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
98 class_destroy(pmsg_class);
99 unregister_chrdev(pmsg_major, PMSG_NAME);
100}