Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Takashi Iwai | e38e0cf | 2005-10-10 11:59:52 +0200 | [diff] [blame] | 5 | * Misc memory accessors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Paul Gortmaker | d81a6d7 | 2011-09-22 09:34:58 -0400 | [diff] [blame] | 8 | #include <linux/export.h> |
Takashi Iwai | 6cbbfe1 | 2015-01-28 16:49:33 +0100 | [diff] [blame] | 9 | #include <linux/io.h> |
Takashi Iwai | 976412f | 2015-01-28 17:24:43 +0100 | [diff] [blame] | 10 | #include <linux/uaccess.h> |
Takashi Iwai | 9004acc | 2008-01-08 18:13:27 +0100 | [diff] [blame] | 11 | #include <sound/core.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * copy_to_user_fromio - copy data from mmio-space to user-space |
| 15 | * @dst: the destination pointer on user-space |
| 16 | * @src: the source pointer on mmio |
| 17 | * @count: the data size to copy in bytes |
| 18 | * |
| 19 | * Copies the data from mmio-space to user-space. |
| 20 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 21 | * Return: Zero if successful, or non-zero on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count) |
| 24 | { |
| 25 | #if defined(__i386__) || defined(CONFIG_SPARC32) |
Clemens Ladisch | 4d23359 | 2005-09-05 10:35:20 +0200 | [diff] [blame] | 26 | return copy_to_user(dst, (const void __force*)src, count) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #else |
| 28 | char buf[256]; |
| 29 | while (count) { |
| 30 | size_t c = count; |
| 31 | if (c > sizeof(buf)) |
| 32 | c = sizeof(buf); |
| 33 | memcpy_fromio(buf, (void __iomem *)src, c); |
| 34 | if (copy_to_user(dst, buf, c)) |
| 35 | return -EFAULT; |
| 36 | count -= c; |
| 37 | dst += c; |
| 38 | src += c; |
| 39 | } |
| 40 | return 0; |
| 41 | #endif |
| 42 | } |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 43 | EXPORT_SYMBOL(copy_to_user_fromio); |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /** |
| 46 | * copy_from_user_toio - copy data from user-space to mmio-space |
| 47 | * @dst: the destination pointer on mmio-space |
| 48 | * @src: the source pointer on user-space |
| 49 | * @count: the data size to copy in bytes |
| 50 | * |
| 51 | * Copies the data from user-space to mmio-space. |
| 52 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 53 | * Return: Zero if successful, or non-zero on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
| 55 | int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count) |
| 56 | { |
| 57 | #if defined(__i386__) || defined(CONFIG_SPARC32) |
Clemens Ladisch | 4d23359 | 2005-09-05 10:35:20 +0200 | [diff] [blame] | 58 | return copy_from_user((void __force *)dst, src, count) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #else |
| 60 | char buf[256]; |
| 61 | while (count) { |
| 62 | size_t c = count; |
| 63 | if (c > sizeof(buf)) |
| 64 | c = sizeof(buf); |
| 65 | if (copy_from_user(buf, src, c)) |
| 66 | return -EFAULT; |
| 67 | memcpy_toio(dst, buf, c); |
| 68 | count -= c; |
| 69 | dst += c; |
| 70 | src += c; |
| 71 | } |
| 72 | return 0; |
| 73 | #endif |
| 74 | } |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 75 | EXPORT_SYMBOL(copy_from_user_toio); |