blob: 862d62d2e144f107a76d9c2b0af93e4c95e1096d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 *
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
Takashi Iwaie38e0cf2005-10-10 11:59:52 +020023#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/io.h>
25#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * copy_to_user_fromio - copy data from mmio-space to user-space
29 * @dst: the destination pointer on user-space
30 * @src: the source pointer on mmio
31 * @count: the data size to copy in bytes
32 *
33 * Copies the data from mmio-space to user-space.
34 *
35 * Returns zero if successful, or non-zero on failure.
36 */
37int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
38{
39#if defined(__i386__) || defined(CONFIG_SPARC32)
Clemens Ladisch4d233592005-09-05 10:35:20 +020040 return copy_to_user(dst, (const void __force*)src, count) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#else
42 char buf[256];
43 while (count) {
44 size_t c = count;
45 if (c > sizeof(buf))
46 c = sizeof(buf);
47 memcpy_fromio(buf, (void __iomem *)src, c);
48 if (copy_to_user(dst, buf, c))
49 return -EFAULT;
50 count -= c;
51 dst += c;
52 src += c;
53 }
54 return 0;
55#endif
56}
57
58/**
59 * copy_from_user_toio - copy data from user-space to mmio-space
60 * @dst: the destination pointer on mmio-space
61 * @src: the source pointer on user-space
62 * @count: the data size to copy in bytes
63 *
64 * Copies the data from user-space to mmio-space.
65 *
66 * Returns zero if successful, or non-zero on failure.
67 */
68int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
69{
70#if defined(__i386__) || defined(CONFIG_SPARC32)
Clemens Ladisch4d233592005-09-05 10:35:20 +020071 return copy_from_user((void __force *)dst, src, count) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#else
73 char buf[256];
74 while (count) {
75 size_t c = count;
76 if (c > sizeof(buf))
77 c = sizeof(buf);
78 if (copy_from_user(buf, src, c))
79 return -EFAULT;
80 memcpy_toio(dst, buf, c);
81 count -= c;
82 dst += c;
83 src += c;
84 }
85 return 0;
86#endif
87}