You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
9.9 KiB

  1. /*
  2. Copyright 2011,2013 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdbool.h>
  15. #if defined(__AVR__)
  16. # include <avr/io.h>
  17. #endif
  18. #include "ps2_mouse.h"
  19. #include "wait.h"
  20. #include "host.h"
  21. #include "timer.h"
  22. #include "print.h"
  23. #include "report.h"
  24. #include "debug.h"
  25. #include "ps2.h"
  26. /* ============================= MACROS ============================ */
  27. static report_mouse_t mouse_report = {};
  28. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report);
  29. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report);
  30. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report);
  31. static inline void ps2_mouse_enable_scrolling(void);
  32. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report);
  33. /* ============================= IMPLEMENTATION ============================ */
  34. /* supports only 3 button mouse at this time */
  35. void ps2_mouse_init(void) {
  36. ps2_host_init();
  37. wait_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up
  38. PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset");
  39. PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT");
  40. PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID");
  41. #ifdef PS2_MOUSE_USE_REMOTE_MODE
  42. ps2_mouse_set_remote_mode();
  43. #else
  44. ps2_mouse_enable_data_reporting();
  45. #endif
  46. #ifdef PS2_MOUSE_ENABLE_SCROLLING
  47. ps2_mouse_enable_scrolling();
  48. #endif
  49. #ifdef PS2_MOUSE_USE_2_1_SCALING
  50. ps2_mouse_set_scaling_2_1();
  51. #endif
  52. ps2_mouse_init_user();
  53. }
  54. __attribute__((weak)) void ps2_mouse_init_user(void) {}
  55. __attribute__((weak)) void ps2_mouse_moved_user(report_mouse_t *mouse_report) {}
  56. void ps2_mouse_task(void) {
  57. static uint8_t buttons_prev = 0;
  58. extern int tp_buttons;
  59. /* receives packet from mouse */
  60. uint8_t rcv;
  61. rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
  62. if (rcv == PS2_ACK) {
  63. mouse_report.buttons = ps2_host_recv_response() | tp_buttons;
  64. mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
  65. mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
  66. #ifdef PS2_MOUSE_ENABLE_SCROLLING
  67. mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
  68. #endif
  69. } else {
  70. if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
  71. return;
  72. }
  73. /* if mouse moves or buttons state changes */
  74. if (mouse_report.x || mouse_report.y || mouse_report.v || ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
  75. #ifdef PS2_MOUSE_DEBUG_RAW
  76. // Used to debug raw ps2 bytes from mouse
  77. ps2_mouse_print_report(&mouse_report);
  78. #endif
  79. buttons_prev = mouse_report.buttons;
  80. ps2_mouse_convert_report_to_hid(&mouse_report);
  81. #if PS2_MOUSE_SCROLL_BTN_MASK
  82. ps2_mouse_scroll_button_task(&mouse_report);
  83. #endif
  84. if (mouse_report.x || mouse_report.y || mouse_report.v) {
  85. ps2_mouse_moved_user(&mouse_report);
  86. }
  87. #ifdef PS2_MOUSE_DEBUG_HID
  88. // Used to debug the bytes sent to the host
  89. ps2_mouse_print_report(&mouse_report);
  90. #endif
  91. host_mouse_send(&mouse_report);
  92. }
  93. ps2_mouse_clear_report(&mouse_report);
  94. }
  95. void ps2_mouse_disable_data_reporting(void) { PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting"); }
  96. void ps2_mouse_enable_data_reporting(void) { PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting"); }
  97. void ps2_mouse_set_remote_mode(void) {
  98. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode");
  99. ps2_mouse_mode = PS2_MOUSE_REMOTE_MODE;
  100. }
  101. void ps2_mouse_set_stream_mode(void) {
  102. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_STREAM_MODE, "ps2 mouse set stream mode");
  103. ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
  104. }
  105. void ps2_mouse_set_scaling_2_1(void) { PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1"); }
  106. void ps2_mouse_set_scaling_1_1(void) { PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1"); }
  107. void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) { PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution"); }
  108. void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) { PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate"); }
  109. /* ============================= HELPERS ============================ */
  110. #define X_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_X_SIGN))
  111. #define Y_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_Y_SIGN))
  112. #define X_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_X_OVFLW))
  113. #define Y_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_Y_OVFLW))
  114. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
  115. // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
  116. // bit: 8 7 ... 0
  117. // sign \8-bit/
  118. //
  119. // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
  120. //
  121. // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
  122. mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127);
  123. mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127);
  124. #ifdef PS2_MOUSE_INVERT_BUTTONS
  125. // swap left & right buttons
  126. uint8_t needs_left = mouse_report->buttons & PS2_MOUSE_BTN_RIGHT;
  127. uint8_t needs_right = mouse_report->buttons & PS2_MOUSE_BTN_LEFT;
  128. mouse_report->buttons = (mouse_report->buttons & ~(PS2_MOUSE_BTN_MASK)) | (needs_left ? PS2_MOUSE_BTN_LEFT : 0) | (needs_right ? PS2_MOUSE_BTN_RIGHT : 0);
  129. #else
  130. // remove sign and overflow flags
  131. mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
  132. #endif
  133. #ifdef PS2_MOUSE_INVERT_X
  134. mouse_report->x = -mouse_report->x;
  135. #endif
  136. #ifndef PS2_MOUSE_INVERT_Y // NOTE if not!
  137. // invert coordinate of y to conform to USB HID mouse
  138. mouse_report->y = -mouse_report->y;
  139. #endif
  140. #ifdef PS2_MOUSE_ROTATE
  141. int8_t x = mouse_report->x;
  142. int8_t y = mouse_report->y;
  143. # if PS2_MOUSE_ROTATE == 90
  144. mouse_report->x = y;
  145. mouse_report->y = -x;
  146. # elif PS2_MOUSE_ROTATE == 180
  147. mouse_report->x = -x;
  148. mouse_report->y = -y;
  149. # elif PS2_MOUSE_ROTATE == 270
  150. mouse_report->x = -y;
  151. mouse_report->y = x;
  152. # endif
  153. #endif
  154. }
  155. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) {
  156. mouse_report->x = 0;
  157. mouse_report->y = 0;
  158. mouse_report->v = 0;
  159. mouse_report->h = 0;
  160. mouse_report->buttons = 0;
  161. }
  162. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) {
  163. if (!debug_mouse) return;
  164. print("ps2_mouse: [");
  165. print_hex8(mouse_report->buttons);
  166. print("|");
  167. print_hex8((uint8_t)mouse_report->x);
  168. print(" ");
  169. print_hex8((uint8_t)mouse_report->y);
  170. print(" ");
  171. print_hex8((uint8_t)mouse_report->v);
  172. print(" ");
  173. print_hex8((uint8_t)mouse_report->h);
  174. print("]\n");
  175. }
  176. static inline void ps2_mouse_enable_scrolling(void) {
  177. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate");
  178. PS2_MOUSE_SEND(200, "200");
  179. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  180. PS2_MOUSE_SEND(100, "100");
  181. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  182. PS2_MOUSE_SEND(80, "80");
  183. PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel");
  184. wait_ms(20);
  185. }
  186. #define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK)
  187. #define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK)
  188. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) {
  189. static enum {
  190. SCROLL_NONE,
  191. SCROLL_BTN,
  192. SCROLL_SENT,
  193. } scroll_state = SCROLL_NONE;
  194. static uint16_t scroll_button_time = 0;
  195. if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) {
  196. // All scroll buttons are pressed
  197. if (scroll_state == SCROLL_NONE) {
  198. scroll_button_time = timer_read();
  199. scroll_state = SCROLL_BTN;
  200. }
  201. // If the mouse has moved, update the report to scroll instead of move the mouse
  202. if (mouse_report->x || mouse_report->y) {
  203. scroll_state = SCROLL_SENT;
  204. mouse_report->v = -mouse_report->y / (PS2_MOUSE_SCROLL_DIVISOR_V);
  205. mouse_report->h = mouse_report->x / (PS2_MOUSE_SCROLL_DIVISOR_H);
  206. mouse_report->x = 0;
  207. mouse_report->y = 0;
  208. #ifdef PS2_MOUSE_INVERT_H
  209. mouse_report->h = -mouse_report->h;
  210. #endif
  211. #ifdef PS2_MOUSE_INVERT_V
  212. mouse_report->v = -mouse_report->v;
  213. #endif
  214. }
  215. } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) {
  216. // None of the scroll buttons are pressed
  217. #if PS2_MOUSE_SCROLL_BTN_SEND
  218. if (scroll_state == SCROLL_BTN && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
  219. PRESS_SCROLL_BUTTONS;
  220. host_mouse_send(mouse_report);
  221. wait_ms(100);
  222. RELEASE_SCROLL_BUTTONS;
  223. }
  224. #endif
  225. scroll_state = SCROLL_NONE;
  226. }
  227. RELEASE_SCROLL_BUTTONS;
  228. }