logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

ps2_mouse.c (11597B)


  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. #include "ps2_mouse.h"
  16. #include "wait.h"
  17. #include "gpio.h"
  18. #include "host.h"
  19. #include "timer.h"
  20. #include "print.h"
  21. #include "report.h"
  22. #include "debug.h"
  23. #include "ps2.h"
  24. /* ============================= MACROS ============================ */
  25. static report_mouse_t mouse_report = {};
  26. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report);
  27. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report);
  28. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report);
  29. static inline void ps2_mouse_enable_scrolling(void);
  30. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report);
  31. /* ============================= IMPLEMENTATION ============================ */
  32. /* supports only 3 button mouse at this time */
  33. void ps2_mouse_init(void) {
  34. ps2_host_init();
  35. wait_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up
  36. PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset");
  37. PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT");
  38. PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID");
  39. #ifdef PS2_MOUSE_USE_REMOTE_MODE
  40. ps2_mouse_set_remote_mode();
  41. #else
  42. ps2_mouse_enable_data_reporting();
  43. ps2_mouse_set_stream_mode();
  44. #endif
  45. #ifdef PS2_MOUSE_ENABLE_SCROLLING
  46. ps2_mouse_enable_scrolling();
  47. #endif
  48. #ifdef PS2_MOUSE_USE_2_1_SCALING
  49. ps2_mouse_set_scaling_2_1();
  50. #endif
  51. ps2_mouse_init_user();
  52. }
  53. __attribute__((weak)) void ps2_mouse_init_user(void) {}
  54. __attribute__((weak)) void ps2_mouse_moved_user(report_mouse_t *mouse_report) {}
  55. void ps2_mouse_task(void) {
  56. static uint8_t buttons_prev = 0;
  57. extern int tp_buttons;
  58. /* receives packet from mouse */
  59. #ifdef PS2_MOUSE_USE_REMOTE_MODE
  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();
  64. mouse_report.x = ps2_host_recv_response();
  65. mouse_report.y = ps2_host_recv_response();
  66. # ifdef PS2_MOUSE_ENABLE_SCROLLING
  67. mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK);
  68. # endif
  69. } else {
  70. if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
  71. /* return here to avoid updating the mouse button state */
  72. return;
  73. }
  74. #else
  75. if (pbuf_has_data()) {
  76. mouse_report.buttons = ps2_host_recv_response();
  77. mouse_report.x = ps2_host_recv_response();
  78. mouse_report.y = ps2_host_recv_response();
  79. # ifdef PS2_MOUSE_ENABLE_SCROLLING
  80. mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK);
  81. # endif
  82. } else {
  83. if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
  84. /* return here to avoid updating the mouse button state */
  85. return;
  86. }
  87. #endif
  88. mouse_report.buttons |= tp_buttons;
  89. /* if mouse moves or buttons state changes */
  90. if (mouse_report.x || mouse_report.y || mouse_report.v || ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
  91. #ifdef PS2_MOUSE_DEBUG_RAW
  92. // Used to debug raw ps2 bytes from mouse
  93. ps2_mouse_print_report(&mouse_report);
  94. #endif
  95. buttons_prev = mouse_report.buttons;
  96. ps2_mouse_convert_report_to_hid(&mouse_report);
  97. #if PS2_MOUSE_SCROLL_BTN_MASK
  98. ps2_mouse_scroll_button_task(&mouse_report);
  99. #endif
  100. if (mouse_report.x || mouse_report.y || mouse_report.v) {
  101. ps2_mouse_moved_user(&mouse_report);
  102. }
  103. #ifdef PS2_MOUSE_DEBUG_HID
  104. // Used to debug the bytes sent to the host
  105. ps2_mouse_print_report(&mouse_report);
  106. #endif
  107. host_mouse_send(&mouse_report);
  108. }
  109. ps2_mouse_clear_report(&mouse_report);
  110. }
  111. void ps2_mouse_disable_data_reporting(void) {
  112. PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting");
  113. }
  114. void ps2_mouse_enable_data_reporting(void) {
  115. PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting");
  116. }
  117. void ps2_mouse_set_remote_mode(void) {
  118. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode");
  119. ps2_mouse_mode = PS2_MOUSE_REMOTE_MODE;
  120. }
  121. void ps2_mouse_set_stream_mode(void) {
  122. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_STREAM_MODE, "ps2 mouse set stream mode");
  123. ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
  124. }
  125. void ps2_mouse_set_scaling_2_1(void) {
  126. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1");
  127. }
  128. void ps2_mouse_set_scaling_1_1(void) {
  129. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1");
  130. }
  131. void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) {
  132. PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution");
  133. }
  134. void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) {
  135. PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate");
  136. }
  137. /* ============================= HELPERS ============================ */
  138. #define X_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_X_SIGN))
  139. #define Y_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_Y_SIGN))
  140. #define X_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_X_OVFLW))
  141. #define Y_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_Y_OVFLW))
  142. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
  143. #ifndef MOUSE_EXTENDED_REPORT
  144. // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
  145. // bit: 8 7 ... 0
  146. // sign \8-bit/
  147. //
  148. // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
  149. //
  150. // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
  151. mouse_report->x *= PS2_MOUSE_X_MULTIPLIER;
  152. mouse_report->y *= PS2_MOUSE_Y_MULTIPLIER;
  153. 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);
  154. 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);
  155. #else
  156. // Sign extend if negative, otherwise leave positive 8-bits as-is
  157. mouse_report->x = X_IS_NEG ? (mouse_report->x | ~0xFF) : mouse_report->x;
  158. mouse_report->y = Y_IS_NEG ? (mouse_report->y | ~0xFF) : mouse_report->y;
  159. mouse_report->x *= PS2_MOUSE_X_MULTIPLIER;
  160. mouse_report->y *= PS2_MOUSE_Y_MULTIPLIER;
  161. #endif
  162. mouse_report->v *= PS2_MOUSE_V_MULTIPLIER;
  163. #ifdef PS2_MOUSE_INVERT_BUTTONS
  164. // swap left & right buttons
  165. bool needs_left = mouse_report->buttons & (1 << PS2_MOUSE_BTN_RIGHT);
  166. bool needs_right = mouse_report->buttons & (1 << PS2_MOUSE_BTN_LEFT);
  167. mouse_report->buttons = (mouse_report->buttons & ~((1 << PS2_MOUSE_BTN_LEFT) | (1 << PS2_MOUSE_BTN_RIGHT))) | (needs_left << PS2_MOUSE_BTN_LEFT) | (needs_right << PS2_MOUSE_BTN_RIGHT);
  168. #endif
  169. // remove sign and overflow flags
  170. mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
  171. #ifdef PS2_MOUSE_INVERT_X
  172. mouse_report->x = -mouse_report->x;
  173. #endif
  174. #ifndef PS2_MOUSE_INVERT_Y // NOTE if not!
  175. // invert coordinate of y to conform to USB HID mouse
  176. mouse_report->y = -mouse_report->y;
  177. #endif
  178. #ifdef PS2_MOUSE_ROTATE
  179. mouse_xy_report_t x = mouse_report->x;
  180. mouse_xy_report_t y = mouse_report->y;
  181. # if PS2_MOUSE_ROTATE == 90
  182. mouse_report->x = y;
  183. mouse_report->y = -x;
  184. # elif PS2_MOUSE_ROTATE == 180
  185. mouse_report->x = -x;
  186. mouse_report->y = -y;
  187. # elif PS2_MOUSE_ROTATE == 270
  188. mouse_report->x = -y;
  189. mouse_report->y = x;
  190. # endif
  191. #endif
  192. }
  193. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) {
  194. mouse_report->x = 0;
  195. mouse_report->y = 0;
  196. mouse_report->v = 0;
  197. mouse_report->h = 0;
  198. mouse_report->buttons = 0;
  199. }
  200. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) {
  201. if (!debug_mouse) return;
  202. print("ps2_mouse: [");
  203. print_hex8(mouse_report->buttons);
  204. print("|");
  205. print_hex8((uint8_t)mouse_report->x);
  206. print(" ");
  207. print_hex8((uint8_t)mouse_report->y);
  208. print(" ");
  209. print_hex8((uint8_t)mouse_report->v);
  210. print(" ");
  211. print_hex8((uint8_t)mouse_report->h);
  212. print("]\n");
  213. }
  214. static inline void ps2_mouse_enable_scrolling(void) {
  215. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate");
  216. PS2_MOUSE_SEND(200, "200");
  217. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  218. PS2_MOUSE_SEND(100, "100");
  219. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  220. PS2_MOUSE_SEND(80, "80");
  221. PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel");
  222. wait_ms(20);
  223. }
  224. #define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK)
  225. #define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK)
  226. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) {
  227. static enum {
  228. SCROLL_NONE,
  229. SCROLL_BTN,
  230. SCROLL_SENT,
  231. } scroll_state = SCROLL_NONE;
  232. static uint16_t scroll_button_time = 0;
  233. static int16_t scroll_x, scroll_y;
  234. if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) {
  235. // All scroll buttons are pressed
  236. if (scroll_state == SCROLL_NONE) {
  237. scroll_button_time = timer_read();
  238. scroll_state = SCROLL_BTN;
  239. scroll_x = 0;
  240. scroll_y = 0;
  241. }
  242. // If the mouse has moved, update the report to scroll instead of move the mouse
  243. if (mouse_report->x || mouse_report->y) {
  244. scroll_state = SCROLL_SENT;
  245. scroll_y += mouse_report->y;
  246. scroll_x += mouse_report->x;
  247. mouse_report->v = -scroll_y / (PS2_MOUSE_SCROLL_DIVISOR_V);
  248. mouse_report->h = scroll_x / (PS2_MOUSE_SCROLL_DIVISOR_H);
  249. scroll_y += (mouse_report->v * (PS2_MOUSE_SCROLL_DIVISOR_V));
  250. scroll_x -= (mouse_report->h * (PS2_MOUSE_SCROLL_DIVISOR_H));
  251. mouse_report->x = 0;
  252. mouse_report->y = 0;
  253. #ifdef PS2_MOUSE_INVERT_H
  254. mouse_report->h = -mouse_report->h;
  255. #endif
  256. #ifdef PS2_MOUSE_INVERT_V
  257. mouse_report->v = -mouse_report->v;
  258. #endif
  259. }
  260. } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) {
  261. // None of the scroll buttons are pressed
  262. #if PS2_MOUSE_SCROLL_BTN_SEND
  263. if (scroll_state == SCROLL_BTN && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
  264. PRESS_SCROLL_BUTTONS;
  265. host_mouse_send(mouse_report);
  266. wait_ms(100);
  267. RELEASE_SCROLL_BUTTONS;
  268. }
  269. #endif
  270. scroll_state = SCROLL_NONE;
  271. }
  272. RELEASE_SCROLL_BUTTONS;
  273. }