memory.c 1.8 KB

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