^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2014 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static DEFINE_MUTEX(pmsg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static ssize_t write_pmsg(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct pstore_record record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) pstore_record_init(&record, psinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) record.type = PSTORE_TYPE_PMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) record.size = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* check outside lock, page in any data. write_user also checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!access_ok(buf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) mutex_lock(&pmsg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ret = psinfo->write_user(&record, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) mutex_unlock(&pmsg_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static const struct file_operations pmsg_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .write = write_pmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static struct class *pmsg_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int pmsg_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define PMSG_NAME "pmsg"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #undef pr_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define pr_fmt(fmt) PMSG_NAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static char *pmsg_devnode(struct device *dev, umode_t *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *mode = 0220;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void pstore_register_pmsg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct device *pmsg_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (pmsg_major < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) pr_err("register_chrdev failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (IS_ERR(pmsg_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pr_err("device class file already in use\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) goto err_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pmsg_class->devnode = pmsg_devnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) NULL, "%s%d", PMSG_NAME, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (IS_ERR(pmsg_device)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) pr_err("failed to create device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) goto err_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) err_device:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) class_destroy(pmsg_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) err_class:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unregister_chrdev(pmsg_major, PMSG_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void pstore_unregister_pmsg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) class_destroy(pmsg_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unregister_chrdev(pmsg_major, PMSG_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }