1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Misc memory accessors
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/export.h>
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/uaccess.h>
11*4882a593Smuzhiyun #include <sound/core.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun /**
14*4882a593Smuzhiyun * copy_to_user_fromio - copy data from mmio-space to user-space
15*4882a593Smuzhiyun * @dst: the destination pointer on user-space
16*4882a593Smuzhiyun * @src: the source pointer on mmio
17*4882a593Smuzhiyun * @count: the data size to copy in bytes
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Copies the data from mmio-space to user-space.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * Return: Zero if successful, or non-zero on failure.
22*4882a593Smuzhiyun */
copy_to_user_fromio(void __user * dst,const volatile void __iomem * src,size_t count)23*4882a593Smuzhiyun int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun #if defined(__i386__) || defined(CONFIG_SPARC32)
26*4882a593Smuzhiyun return copy_to_user(dst, (const void __force*)src, count) ? -EFAULT : 0;
27*4882a593Smuzhiyun #else
28*4882a593Smuzhiyun char buf[256];
29*4882a593Smuzhiyun while (count) {
30*4882a593Smuzhiyun size_t c = count;
31*4882a593Smuzhiyun if (c > sizeof(buf))
32*4882a593Smuzhiyun c = sizeof(buf);
33*4882a593Smuzhiyun memcpy_fromio(buf, (void __iomem *)src, c);
34*4882a593Smuzhiyun if (copy_to_user(dst, buf, c))
35*4882a593Smuzhiyun return -EFAULT;
36*4882a593Smuzhiyun count -= c;
37*4882a593Smuzhiyun dst += c;
38*4882a593Smuzhiyun src += c;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun return 0;
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun EXPORT_SYMBOL(copy_to_user_fromio);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun * copy_from_user_toio - copy data from user-space to mmio-space
47*4882a593Smuzhiyun * @dst: the destination pointer on mmio-space
48*4882a593Smuzhiyun * @src: the source pointer on user-space
49*4882a593Smuzhiyun * @count: the data size to copy in bytes
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * Copies the data from user-space to mmio-space.
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * Return: Zero if successful, or non-zero on failure.
54*4882a593Smuzhiyun */
copy_from_user_toio(volatile void __iomem * dst,const void __user * src,size_t count)55*4882a593Smuzhiyun int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun #if defined(__i386__) || defined(CONFIG_SPARC32)
58*4882a593Smuzhiyun return copy_from_user((void __force *)dst, src, count) ? -EFAULT : 0;
59*4882a593Smuzhiyun #else
60*4882a593Smuzhiyun char buf[256];
61*4882a593Smuzhiyun while (count) {
62*4882a593Smuzhiyun size_t c = count;
63*4882a593Smuzhiyun if (c > sizeof(buf))
64*4882a593Smuzhiyun c = sizeof(buf);
65*4882a593Smuzhiyun if (copy_from_user(buf, src, c))
66*4882a593Smuzhiyun return -EFAULT;
67*4882a593Smuzhiyun memcpy_toio(dst, buf, c);
68*4882a593Smuzhiyun count -= c;
69*4882a593Smuzhiyun dst += c;
70*4882a593Smuzhiyun src += c;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun return 0;
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun EXPORT_SYMBOL(copy_from_user_toio);
76