memory.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. *
  4. * Misc memory accessors
  5. *
  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. #include <linux/export.h>
  23. #include <linux/io.h>
  24. #include <linux/uaccess.h>
  25. #include <sound/core.h>
  26. /**
  27. * copy_to_user_fromio - copy data from mmio-space to user-space
  28. * @dst: the destination pointer on user-space
  29. * @src: the source pointer on mmio
  30. * @count: the data size to copy in bytes
  31. *
  32. * Copies the data from mmio-space to user-space.
  33. *
  34. * Return: Zero if successful, or non-zero on failure.
  35. */
  36. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
  37. {
  38. #if defined(__i386__) || defined(CONFIG_SPARC32)
  39. return copy_to_user(dst, (const void __force*)src, count) ? -EFAULT : 0;
  40. #else
  41. char buf[256];
  42. while (count) {
  43. size_t c = count;
  44. if (c > sizeof(buf))
  45. c = sizeof(buf);
  46. memcpy_fromio(buf, (void __iomem *)src, c);
  47. if (copy_to_user(dst, buf, c))
  48. return -EFAULT;
  49. count -= c;
  50. dst += c;
  51. src += c;
  52. }
  53. return 0;
  54. #endif
  55. }
  56. EXPORT_SYMBOL(copy_to_user_fromio);
  57. /**
  58. * copy_from_user_toio - copy data from user-space to mmio-space
  59. * @dst: the destination pointer on mmio-space
  60. * @src: the source pointer on user-space
  61. * @count: the data size to copy in bytes
  62. *
  63. * Copies the data from user-space to mmio-space.
  64. *
  65. * Return: Zero if successful, or non-zero on failure.
  66. */
  67. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
  68. {
  69. #if defined(__i386__) || defined(CONFIG_SPARC32)
  70. return copy_from_user((void __force *)dst, src, count) ? -EFAULT : 0;
  71. #else
  72. char buf[256];
  73. while (count) {
  74. size_t c = count;
  75. if (c > sizeof(buf))
  76. c = sizeof(buf);
  77. if (copy_from_user(buf, src, c))
  78. return -EFAULT;
  79. memcpy_toio(dst, buf, c);
  80. count -= c;
  81. dst += c;
  82. src += c;
  83. }
  84. return 0;
  85. #endif
  86. }
  87. EXPORT_SYMBOL(copy_from_user_toio);