Linux Audio

Check our new training course

Loading...
v5.4
 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 "internal.h"
11
12static DEFINE_MUTEX(pmsg_lock);
 
13
14static ssize_t write_pmsg(struct file *file, const char __user *buf,
15			  size_t count, loff_t *ppos)
16{
17	struct pstore_record record;
18	int ret;
19
20	if (!count)
21		return 0;
22
23	pstore_record_init(&record, psinfo);
24	record.type = PSTORE_TYPE_PMSG;
25	record.size = count;
26
27	/* check outside lock, page in any data. write_user also checks */
28	if (!access_ok(buf, count))
29		return -EFAULT;
30
 
 
 
 
 
 
 
31	mutex_lock(&pmsg_lock);
32	ret = psinfo->write_user(&record, buf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33	mutex_unlock(&pmsg_lock);
34	return ret ? ret : count;
 
35}
36
37static const struct file_operations pmsg_fops = {
38	.owner		= THIS_MODULE,
39	.llseek		= noop_llseek,
40	.write		= write_pmsg,
41};
42
43static struct class *pmsg_class;
44static int pmsg_major;
45#define PMSG_NAME "pmsg"
46#undef pr_fmt
47#define pr_fmt(fmt) PMSG_NAME ": " fmt
48
49static char *pmsg_devnode(struct device *dev, umode_t *mode)
50{
51	if (mode)
52		*mode = 0220;
53	return NULL;
54}
55
56void pstore_register_pmsg(void)
57{
58	struct device *pmsg_device;
59
60	pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
61	if (pmsg_major < 0) {
62		pr_err("register_chrdev failed\n");
63		goto err;
64	}
65
66	pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
67	if (IS_ERR(pmsg_class)) {
68		pr_err("device class file already in use\n");
69		goto err_class;
70	}
71	pmsg_class->devnode = pmsg_devnode;
72
73	pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
74					NULL, "%s%d", PMSG_NAME, 0);
75	if (IS_ERR(pmsg_device)) {
76		pr_err("failed to create device\n");
77		goto err_device;
78	}
79	return;
80
81err_device:
82	class_destroy(pmsg_class);
83err_class:
84	unregister_chrdev(pmsg_major, PMSG_NAME);
85err:
86	return;
87}
88
89void pstore_unregister_pmsg(void)
90{
91	device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
92	class_destroy(pmsg_class);
93	unregister_chrdev(pmsg_major, PMSG_NAME);
94}
v4.6
 
  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 <linux/vmalloc.h>
 19#include "internal.h"
 20
 21static DEFINE_MUTEX(pmsg_lock);
 22#define PMSG_MAX_BOUNCE_BUFFER_SIZE (2*PAGE_SIZE)
 23
 24static ssize_t write_pmsg(struct file *file, const char __user *buf,
 25			  size_t count, loff_t *ppos)
 26{
 27	size_t i, buffer_size;
 28	char *buffer;
 29
 30	if (!count)
 31		return 0;
 32
 33	if (!access_ok(VERIFY_READ, buf, count))
 
 
 
 
 
 34		return -EFAULT;
 35
 36	buffer_size = count;
 37	if (buffer_size > PMSG_MAX_BOUNCE_BUFFER_SIZE)
 38		buffer_size = PMSG_MAX_BOUNCE_BUFFER_SIZE;
 39	buffer = vmalloc(buffer_size);
 40	if (!buffer)
 41		return -ENOMEM;
 42
 43	mutex_lock(&pmsg_lock);
 44	for (i = 0; i < count; ) {
 45		size_t c = min(count - i, buffer_size);
 46		u64 id;
 47		long ret;
 48
 49		ret = __copy_from_user(buffer, buf + i, c);
 50		if (unlikely(ret != 0)) {
 51			mutex_unlock(&pmsg_lock);
 52			vfree(buffer);
 53			return -EFAULT;
 54		}
 55		psinfo->write_buf(PSTORE_TYPE_PMSG, 0, &id, 0, buffer, 0, c,
 56				  psinfo);
 57
 58		i += c;
 59	}
 60
 61	mutex_unlock(&pmsg_lock);
 62	vfree(buffer);
 63	return count;
 64}
 65
 66static const struct file_operations pmsg_fops = {
 67	.owner		= THIS_MODULE,
 68	.llseek		= noop_llseek,
 69	.write		= write_pmsg,
 70};
 71
 72static struct class *pmsg_class;
 73static int pmsg_major;
 74#define PMSG_NAME "pmsg"
 75#undef pr_fmt
 76#define pr_fmt(fmt) PMSG_NAME ": " fmt
 77
 78static char *pmsg_devnode(struct device *dev, umode_t *mode)
 79{
 80	if (mode)
 81		*mode = 0220;
 82	return NULL;
 83}
 84
 85void pstore_register_pmsg(void)
 86{
 87	struct device *pmsg_device;
 88
 89	pmsg_major = register_chrdev(0, PMSG_NAME, &pmsg_fops);
 90	if (pmsg_major < 0) {
 91		pr_err("register_chrdev failed\n");
 92		goto err;
 93	}
 94
 95	pmsg_class = class_create(THIS_MODULE, PMSG_NAME);
 96	if (IS_ERR(pmsg_class)) {
 97		pr_err("device class file already in use\n");
 98		goto err_class;
 99	}
100	pmsg_class->devnode = pmsg_devnode;
101
102	pmsg_device = device_create(pmsg_class, NULL, MKDEV(pmsg_major, 0),
103					NULL, "%s%d", PMSG_NAME, 0);
104	if (IS_ERR(pmsg_device)) {
105		pr_err("failed to create device\n");
106		goto err_device;
107	}
108	return;
109
110err_device:
111	class_destroy(pmsg_class);
112err_class:
113	unregister_chrdev(pmsg_major, PMSG_NAME);
114err:
115	return;
116}
117
118void pstore_unregister_pmsg(void)
119{
120	device_destroy(pmsg_class, MKDEV(pmsg_major, 0));
121	class_destroy(pmsg_class);
122	unregister_chrdev(pmsg_major, PMSG_NAME);
123}