1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2014 Google, Inc.
6 #include <linux/cdev.h>
7 #include <linux/device.h>
9 #include <linux/uaccess.h>
10 #include <linux/rtmutex.h>
13 static DEFINE_RT_MUTEX(pmsg_lock);
15 static ssize_t write_pmsg(struct file *file, const char __user *buf,
16 size_t count, loff_t *ppos)
18 struct pstore_record record;
24 pstore_record_init(&record, psinfo);
25 record.type = PSTORE_TYPE_PMSG;
28 /* check outside lock, page in any data. write_user also checks */
29 if (!access_ok(buf, count))
32 rt_mutex_lock(&pmsg_lock);
33 ret = psinfo->write_user(&record, buf);
34 rt_mutex_unlock(&pmsg_lock);
35 return ret ? ret : count;
38 static const struct file_operations pmsg_fops = {
40 .llseek = noop_llseek,
44 static struct class *pmsg_class;
45 static int pmsg_major;
46 #define PMSG_NAME "pmsg"
48 #define pr_fmt(fmt) PMSG_NAME ": " fmt
50 static char *pmsg_devnode(const struct device *dev, umode_t *mode)
57 void pstore_register_pmsg(void)
59 struct device *pmsg_device;
61 pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
63 pr_err("register_chrdev failed\n");
67 pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
68 if (IS_ERR(pmsg_class)) {
69 pr_err("device class file already in use\n");
72 pmsg_class->devnode = pmsg_devnode;
74 pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
75 NULL, "%s%d", PMSG_NAME, 0);
76 if (IS_ERR(pmsg_device)) {
77 pr_err("failed to create device\n");
83 class_destroy(pmsg_class);
85 unregister_chrdev(pmsg_major, PMSG_NAME);
90 void pstore_unregister_pmsg(void)
92 device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
93 class_destroy(pmsg_class);
94 unregister_chrdev(pmsg_major, PMSG_NAME);