input-compat.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * 32bit compatibility wrappers for the input subsystem.
  4. *
  5. * Very heavily based on evdev.c - Copyright (c) 1999-2002 Vojtech Pavlik
  6. */
  7. #include <linux/export.h>
  8. #include <linux/sprintf.h>
  9. #include <linux/uaccess.h>
  10. #include "input-compat.h"
  11. #ifdef CONFIG_COMPAT
  12. int input_event_from_user(const char __user *buffer,
  13. struct input_event *event)
  14. {
  15. if (in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
  16. struct input_event_compat compat_event;
  17. if (copy_from_user(&compat_event, buffer,
  18. sizeof(struct input_event_compat)))
  19. return -EFAULT;
  20. event->input_event_sec = compat_event.sec;
  21. event->input_event_usec = compat_event.usec;
  22. event->type = compat_event.type;
  23. event->code = compat_event.code;
  24. event->value = compat_event.value;
  25. } else {
  26. if (copy_from_user(event, buffer, sizeof(struct input_event)))
  27. return -EFAULT;
  28. }
  29. return 0;
  30. }
  31. int input_event_to_user(char __user *buffer,
  32. const struct input_event *event)
  33. {
  34. if (in_compat_syscall() && !COMPAT_USE_64BIT_TIME) {
  35. struct input_event_compat compat_event;
  36. compat_event.sec = event->input_event_sec;
  37. compat_event.usec = event->input_event_usec;
  38. compat_event.type = event->type;
  39. compat_event.code = event->code;
  40. compat_event.value = event->value;
  41. if (copy_to_user(buffer, &compat_event,
  42. sizeof(struct input_event_compat)))
  43. return -EFAULT;
  44. } else {
  45. if (copy_to_user(buffer, event, sizeof(struct input_event)))
  46. return -EFAULT;
  47. }
  48. return 0;
  49. }
  50. int input_ff_effect_from_user(const char __user *buffer, size_t size,
  51. struct ff_effect *effect)
  52. {
  53. if (in_compat_syscall()) {
  54. struct ff_effect_compat *compat_effect;
  55. if (size != sizeof(struct ff_effect_compat))
  56. return -EINVAL;
  57. /*
  58. * It so happens that the pointer which needs to be changed
  59. * is the last field in the structure, so we can retrieve the
  60. * whole thing and replace just the pointer.
  61. */
  62. compat_effect = (struct ff_effect_compat *)effect;
  63. if (copy_from_user(compat_effect, buffer,
  64. sizeof(struct ff_effect_compat)))
  65. return -EFAULT;
  66. if (compat_effect->type == FF_PERIODIC &&
  67. compat_effect->u.periodic.waveform == FF_CUSTOM)
  68. effect->u.periodic.custom_data =
  69. compat_ptr(compat_effect->u.periodic.custom_data);
  70. } else {
  71. if (size != sizeof(struct ff_effect))
  72. return -EINVAL;
  73. if (copy_from_user(effect, buffer, sizeof(struct ff_effect)))
  74. return -EFAULT;
  75. }
  76. return 0;
  77. }
  78. int input_bits_to_string(char *buf, int buf_size, unsigned long bits,
  79. bool skip_empty)
  80. {
  81. int len = 0;
  82. if (in_compat_syscall()) {
  83. u32 dword = bits >> 32;
  84. if (dword || !skip_empty)
  85. len += snprintf(buf, buf_size, "%x ", dword);
  86. dword = bits & 0xffffffffUL;
  87. if (dword || !skip_empty || len)
  88. len += snprintf(buf + len, max(buf_size - len, 0),
  89. "%x", dword);
  90. } else {
  91. if (bits || !skip_empty)
  92. len += snprintf(buf, buf_size, "%lx", bits);
  93. }
  94. return len;
  95. }
  96. #else
  97. int input_event_from_user(const char __user *buffer,
  98. struct input_event *event)
  99. {
  100. if (copy_from_user(event, buffer, sizeof(struct input_event)))
  101. return -EFAULT;
  102. return 0;
  103. }
  104. int input_event_to_user(char __user *buffer,
  105. const struct input_event *event)
  106. {
  107. if (copy_to_user(buffer, event, sizeof(struct input_event)))
  108. return -EFAULT;
  109. return 0;
  110. }
  111. int input_ff_effect_from_user(const char __user *buffer, size_t size,
  112. struct ff_effect *effect)
  113. {
  114. if (size != sizeof(struct ff_effect))
  115. return -EINVAL;
  116. if (copy_from_user(effect, buffer, sizeof(struct ff_effect)))
  117. return -EFAULT;
  118. return 0;
  119. }
  120. int input_bits_to_string(char *buf, int buf_size, unsigned long bits,
  121. bool skip_empty)
  122. {
  123. return bits || !skip_empty ?
  124. snprintf(buf, buf_size, "%lx", bits) : 0;
  125. }
  126. #endif /* CONFIG_COMPAT */
  127. EXPORT_SYMBOL_GPL(input_event_from_user);
  128. EXPORT_SYMBOL_GPL(input_event_to_user);
  129. EXPORT_SYMBOL_GPL(input_ff_effect_from_user);