blob: 36c0f1a2e189e7dd6a7aceb325be3abbc6f8688d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Takashi Iwaie38e0cf2005-10-10 11:59:52 +02004 * Misc memory accessors
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/io.h>
25#include <asm/uaccess.h>
Takashi Iwai9004acc2008-01-08 18:13:27 +010026#include <sound/core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * copy_to_user_fromio - copy data from mmio-space to user-space
30 * @dst: the destination pointer on user-space
31 * @src: the source pointer on mmio
32 * @count: the data size to copy in bytes
33 *
34 * Copies the data from mmio-space to user-space.
35 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +010036 * Return: Zero if successful, or non-zero on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
39{
40#if defined(__i386__) || defined(CONFIG_SPARC32)
Clemens Ladisch4d233592005-09-05 10:35:20 +020041 return copy_to_user(dst, (const void __force*)src, count) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#else
43 char buf[256];
44 while (count) {
45 size_t c = count;
46 if (c > sizeof(buf))
47 c = sizeof(buf);
48 memcpy_fromio(buf, (void __iomem *)src, c);
49 if (copy_to_user(dst, buf, c))
50 return -EFAULT;
51 count -= c;
52 dst += c;
53 src += c;
54 }
55 return 0;
56#endif
57}
58
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020059EXPORT_SYMBOL(copy_to_user_fromio);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/**
62 * copy_from_user_toio - copy data from user-space to mmio-space
63 * @dst: the destination pointer on mmio-space
64 * @src: the source pointer on user-space
65 * @count: the data size to copy in bytes
66 *
67 * Copies the data from user-space to mmio-space.
68 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +010069 * Return: Zero if successful, or non-zero on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
71int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
72{
73#if defined(__i386__) || defined(CONFIG_SPARC32)
Clemens Ladisch4d233592005-09-05 10:35:20 +020074 return copy_from_user((void __force *)dst, src, count) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#else
76 char buf[256];
77 while (count) {
78 size_t c = count;
79 if (c > sizeof(buf))
80 c = sizeof(buf);
81 if (copy_from_user(buf, src, c))
82 return -EFAULT;
83 memcpy_toio(dst, buf, c);
84 count -= c;
85 dst += c;
86 src += c;
87 }
88 return 0;
89#endif
90}
Takashi Iwaic0d3fb32006-04-28 15:13:39 +020091
92EXPORT_SYMBOL(copy_from_user_toio);