Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2014  Google, Inc.
0004  */
0005 
0006 #include <linux/cdev.h>
0007 #include <linux/device.h>
0008 #include <linux/fs.h>
0009 #include <linux/uaccess.h>
0010 #include "internal.h"
0011 
0012 static DEFINE_MUTEX(pmsg_lock);
0013 
0014 static ssize_t write_pmsg(struct file *file, const char __user *buf,
0015               size_t count, loff_t *ppos)
0016 {
0017     struct pstore_record record;
0018     int ret;
0019 
0020     if (!count)
0021         return 0;
0022 
0023     pstore_record_init(&record, psinfo);
0024     record.type = PSTORE_TYPE_PMSG;
0025     record.size = count;
0026 
0027     /* check outside lock, page in any data. write_user also checks */
0028     if (!access_ok(buf, count))
0029         return -EFAULT;
0030 
0031     mutex_lock(&pmsg_lock);
0032     ret = psinfo->write_user(&record, buf);
0033     mutex_unlock(&pmsg_lock);
0034     return ret ? ret : count;
0035 }
0036 
0037 static const struct file_operations pmsg_fops = {
0038     .owner      = THIS_MODULE,
0039     .llseek     = noop_llseek,
0040     .write      = write_pmsg,
0041 };
0042 
0043 static struct class *pmsg_class;
0044 static int pmsg_major;
0045 #define PMSG_NAME "pmsg"
0046 #undef pr_fmt
0047 #define pr_fmt(fmt) PMSG_NAME ": " fmt
0048 
0049 static char *pmsg_devnode(struct device *dev, umode_t *mode)
0050 {
0051     if (mode)
0052         *mode = 0220;
0053     return NULL;
0054 }
0055 
0056 void pstore_register_pmsg(void)
0057 {
0058     struct device *pmsg_device;
0059 
0060     pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
0061     if (pmsg_major < 0) {
0062         pr_err("register_chrdev failed\n");
0063         goto err;
0064     }
0065 
0066     pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
0067     if (IS_ERR(pmsg_class)) {
0068         pr_err("device class file already in use\n");
0069         goto err_class;
0070     }
0071     pmsg_class->devnode = pmsg_devnode;
0072 
0073     pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
0074                     NULL, "%s%d", PMSG_NAME, 0);
0075     if (IS_ERR(pmsg_device)) {
0076         pr_err("failed to create device\n");
0077         goto err_device;
0078     }
0079     return;
0080 
0081 err_device:
0082     class_destroy(pmsg_class);
0083 err_class:
0084     unregister_chrdev(pmsg_major, PMSG_NAME);
0085 err:
0086     return;
0087 }
0088 
0089 void pstore_unregister_pmsg(void)
0090 {
0091     device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
0092     class_destroy(pmsg_class);
0093     unregister_chrdev(pmsg_major, PMSG_NAME);
0094 }