Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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