summaryrefslogtreecommitdiff
path: root/sound/core/memory.c
blob: d683442b4c978c2dfe7c87b76900d38d44acd87d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
 * 
 *  Misc memory accessors
 */

#include <linux/export.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <sound/core.h>
#include <sound/pcm.h>

/**
 * copy_to_user_fromio - copy data from mmio-space to user-space
 * @dst: the destination pointer on user-space
 * @src: the source pointer on mmio
 * @count: the data size to copy in bytes
 *
 * Copies the data from mmio-space to user-space.
 *
 * Return: Zero if successful, or non-zero on failure.
 */
int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
{
	struct iov_iter iter;

	if (import_ubuf(ITER_DEST, dst, count, &iter))
		return -EFAULT;
	if (copy_to_iter_fromio((const void __iomem *)src, count, &iter) != count)
		return -EFAULT;
	return 0;
}
EXPORT_SYMBOL(copy_to_user_fromio);

/**
 * copy_to_iter_fromio - copy data from mmio-space to iov_iter
 * @src: the source pointer on mmio
 * @count: the data size to copy in bytes
 * @dst: the destination iov_iter
 *
 * Copies the data from mmio-space to iov_iter.
 *
 * Return: number of bytes to be copied
 */
size_t copy_to_iter_fromio(const void __iomem *src, size_t count,
			   struct iov_iter *dst)
{
#if defined(__i386__) || defined(CONFIG_SPARC32)
	return copy_to_iter((const void __force *)src, count, dst);
#else
	char buf[256];
	size_t res = 0;

	while (count) {
		size_t c = count;
		if (c > sizeof(buf))
			c = sizeof(buf);
		memcpy_fromio(buf, (void __iomem *)src, c);
		if (copy_to_iter(buf, c, dst) != c)
			return res;
		count -= c;
		src += c;
		res += c;
	}
	return res;
#endif
}
EXPORT_SYMBOL(copy_to_iter_fromio);

/**
 * copy_from_user_toio - copy data from user-space to mmio-space
 * @dst: the destination pointer on mmio-space
 * @src: the source pointer on user-space
 * @count: the data size to copy in bytes
 *
 * Copies the data from user-space to mmio-space.
 *
 * Return: Zero if successful, or non-zero on failure.
 */
int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
{
	struct iov_iter iter;

	if (import_ubuf(ITER_SOURCE, (void __user *)src, count, &iter))
		return -EFAULT;
	if (copy_from_iter_toio((void __iomem *)dst, count, &iter) != count)
		return -EFAULT;
	return 0;
}
EXPORT_SYMBOL(copy_from_user_toio);

/**
 * copy_from_iter_toio - copy data from iov_iter to mmio-space
 * @dst: the destination pointer on mmio-space
 * @count: the data size to copy in bytes
 * @src: the source iov_iter
 *
 * Copies the data from iov_iter to mmio-space.
 *
 * Return: number of bytes to be copied
 */
size_t copy_from_iter_toio(void __iomem *dst, size_t count,
			   struct iov_iter *src)
{
#if defined(__i386__) || defined(CONFIG_SPARC32)
	return copy_from_iter((void __force *)dst, count, src);
#else
	char buf[256];
	size_t res = 0;

	while (count) {
		size_t c = count;
		if (c > sizeof(buf))
			c = sizeof(buf);
		if (copy_from_iter(buf, c, src) != c)
			return res;
		memcpy_toio(dst, buf, c);
		count -= c;
		dst += c;
		res += c;
	}
	return res;
#endif
}
EXPORT_SYMBOL(copy_from_iter_toio);