mem_op.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * mem_op.h
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * The initial developer of the original code is David A. Hinds
  9. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  10. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  11. *
  12. * (C) 1999 David A. Hinds
  13. */
  14. #ifndef _LINUX_MEM_OP_H
  15. #define _LINUX_MEM_OP_H
  16. #include <asm/uaccess.h>
  17. #include <asm/io.h>
  18. /*
  19. If UNSAFE_MEMCPY is defined, we use the (optimized) system routines
  20. to copy between a card and kernel memory. These routines do 32-bit
  21. operations which may not work with all PCMCIA controllers. The
  22. safe versions defined here will do only 8-bit and 16-bit accesses.
  23. */
  24. #ifdef UNSAFE_MEMCPY
  25. #define copy_from_pc memcpy_fromio
  26. #define copy_to_pc memcpy_toio
  27. static inline void copy_pc_to_user(void *to, const void *from, size_t n)
  28. {
  29. size_t odd = (n & 3);
  30. n -= odd;
  31. while (n) {
  32. put_user(__raw_readl(from), (int *)to);
  33. (char *)from += 4; (char *)to += 4; n -= 4;
  34. }
  35. while (odd--)
  36. put_user(readb((char *)from++), (char *)to++);
  37. }
  38. static inline void copy_user_to_pc(void *to, const void *from, size_t n)
  39. {
  40. int l;
  41. char c;
  42. size_t odd = (n & 3);
  43. n -= odd;
  44. while (n) {
  45. get_user(l, (int *)from);
  46. __raw_writel(l, to);
  47. (char *)to += 4; (char *)from += 4; n -= 4;
  48. }
  49. while (odd--) {
  50. get_user(c, (char *)from++);
  51. writeb(c, (char *)to++);
  52. }
  53. }
  54. #else /* UNSAFE_MEMCPY */
  55. static inline void copy_from_pc(void *to, void __iomem *from, size_t n)
  56. {
  57. __u16 *t = to;
  58. __u16 __iomem *f = from;
  59. size_t odd = (n & 1);
  60. for (n >>= 1; n; n--)
  61. *t++ = __raw_readw(f++);
  62. if (odd)
  63. *(__u8 *)t = readb(f);
  64. }
  65. static inline void copy_to_pc(void __iomem *to, const void *from, size_t n)
  66. {
  67. __u16 __iomem *t = to;
  68. const __u16 *f = from;
  69. size_t odd = (n & 1);
  70. for (n >>= 1; n ; n--)
  71. __raw_writew(*f++, t++);
  72. if (odd)
  73. writeb(*(__u8 *)f, t);
  74. }
  75. static inline void copy_pc_to_user(void __user *to, void __iomem *from, size_t n)
  76. {
  77. __u16 __user *t = to;
  78. __u16 __iomem *f = from;
  79. size_t odd = (n & 1);
  80. for (n >>= 1; n ; n--)
  81. put_user(__raw_readw(f++), t++);
  82. if (odd)
  83. put_user(readb(f), (char __user *)t);
  84. }
  85. static inline void copy_user_to_pc(void __iomem *to, void __user *from, size_t n)
  86. {
  87. __u16 __user *f = from;
  88. __u16 __iomem *t = to;
  89. short s;
  90. char c;
  91. size_t odd = (n & 1);
  92. for (n >>= 1; n; n--) {
  93. get_user(s, f++);
  94. __raw_writew(s, t++);
  95. }
  96. if (odd) {
  97. get_user(c, (char __user *)f);
  98. writeb(c, t);
  99. }
  100. }
  101. #endif /* UNSAFE_MEMCPY */
  102. #endif /* _LINUX_MEM_OP_H */