ps2_mouse.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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<avr/io.h>
  16. #include<util/delay.h>
  17. #include "ps2_mouse.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. #define PS2_MOUSE_SEND(command, message) \
  26. do { \
  27. uint8_t rcv = ps2_host_send(command); \
  28. if (debug_mouse) { \
  29. print((message)); \
  30. xprintf(" command: %X, result: %X, error: %X \n", command, rcv, ps2_error); \
  31. } \
  32. } while(0)
  33. #define PS2_MOUSE_SEND_SAFE(command, message) \
  34. do { \
  35. if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
  36. ps2_mouse_disable_data_reporting(); \
  37. } \
  38. PS2_MOUSE_SEND(command, message); \
  39. if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
  40. ps2_mouse_enable_data_reporting(); \
  41. } \
  42. } while(0)
  43. #define PS2_MOUSE_SET_SAFE(command, value, message) \
  44. do { \
  45. if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
  46. ps2_mouse_disable_data_reporting(); \
  47. } \
  48. PS2_MOUSE_SEND(command, message); \
  49. PS2_MOUSE_SEND(value, "Sending value"); \
  50. if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
  51. ps2_mouse_enable_data_reporting(); \
  52. } \
  53. } while(0)
  54. #define PS2_MOUSE_RECEIVE(message) \
  55. do { \
  56. uint8_t rcv = ps2_host_recv_response(); \
  57. if (debug_mouse) { \
  58. print((message)); \
  59. xprintf(" result: %X, error: %X \n", rcv, ps2_error); \
  60. } \
  61. } while(0)
  62. static enum ps2_mouse_mode_e {
  63. PS2_MOUSE_STREAM_MODE,
  64. PS2_MOUSE_REMOTE_MODE,
  65. } ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
  66. static report_mouse_t mouse_report = {};
  67. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report);
  68. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report);
  69. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report);
  70. static inline void ps2_mouse_enable_scrolling(void);
  71. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report);
  72. /* ============================= IMPLEMENTATION ============================ */
  73. /* supports only 3 button mouse at this time */
  74. void ps2_mouse_init(void) {
  75. ps2_host_init();
  76. _delay_ms(1000); // wait for powering up
  77. PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset");
  78. PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT");
  79. PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID");
  80. #ifdef PS2_MOUSE_USE_REMOTE_MODE
  81. ps2_mouse_set_remote_mode();
  82. #else
  83. ps2_mouse_enable_data_reporting();
  84. #endif
  85. #ifdef PS2_MOUSE_ENABLE_SCROLLING
  86. ps2_mouse_enable_scrolling();
  87. #endif
  88. #ifdef PS2_MOUSE_USE_2_1_SCALING
  89. ps2_mouse_set_scaling_2_1();
  90. #endif
  91. }
  92. void ps2_mouse_task(void) {
  93. static uint8_t buttons_prev = 0;
  94. /* receives packet from mouse */
  95. uint8_t rcv;
  96. rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
  97. if (rcv == PS2_ACK) {
  98. mouse_report.buttons = ps2_host_recv_response();
  99. mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
  100. mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
  101. #ifdef PS2_MOUSE_ENABLE_SCROLLING
  102. mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
  103. #endif
  104. } else {
  105. if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
  106. return;
  107. }
  108. /* if mouse moves or buttons state changes */
  109. if (mouse_report.x || mouse_report.y || mouse_report.v ||
  110. ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
  111. #ifdef PS2_MOUSE_DEBUG_RAW
  112. // Used to debug raw ps2 bytes from mouse
  113. ps2_mouse_print_report(&mouse_report);
  114. #endif
  115. buttons_prev = mouse_report.buttons;
  116. ps2_mouse_convert_report_to_hid(&mouse_report);
  117. #if PS2_MOUSE_SCROLL_BTN_MASK
  118. ps2_mouse_scroll_button_task(&mouse_report);
  119. #endif
  120. #ifdef PS2_MOUSE_DEBUG_HID
  121. // Used to debug the bytes sent to the host
  122. ps2_mouse_print_report(&mouse_report);
  123. #endif
  124. host_mouse_send(&mouse_report);
  125. }
  126. ps2_mouse_clear_report(&mouse_report);
  127. }
  128. void ps2_mouse_disable_data_reporting(void) {
  129. PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting");
  130. }
  131. void ps2_mouse_enable_data_reporting(void) {
  132. PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting");
  133. }
  134. void ps2_mouse_set_remote_mode(void) {
  135. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode");
  136. ps2_mouse_mode = PS2_MOUSE_REMOTE_MODE;
  137. }
  138. void ps2_mouse_set_stream_mode(void) {
  139. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_STREAM_MODE, "ps2 mouse set stream mode");
  140. ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
  141. }
  142. void ps2_mouse_set_scaling_2_1(void) {
  143. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1");
  144. }
  145. void ps2_mouse_set_scaling_1_1(void) {
  146. PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1");
  147. }
  148. void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) {
  149. PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution");
  150. }
  151. void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) {
  152. PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate");
  153. }
  154. /* ============================= HELPERS ============================ */
  155. #define X_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_X_SIGN))
  156. #define Y_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_Y_SIGN))
  157. #define X_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_X_OVFLW))
  158. #define Y_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_Y_OVFLW))
  159. static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
  160. // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
  161. // bit: 8 7 ... 0
  162. // sign \8-bit/
  163. //
  164. // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
  165. //
  166. // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
  167. mouse_report->x = X_IS_NEG ?
  168. ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) :
  169. ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127);
  170. mouse_report->y = Y_IS_NEG ?
  171. ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) :
  172. ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127);
  173. // remove sign and overflow flags
  174. mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
  175. // invert coordinate of y to conform to USB HID mouse
  176. mouse_report->y = -mouse_report->y;
  177. }
  178. static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) {
  179. mouse_report->x = 0;
  180. mouse_report->y = 0;
  181. mouse_report->v = 0;
  182. mouse_report->h = 0;
  183. mouse_report->buttons = 0;
  184. }
  185. static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) {
  186. if (!debug_mouse) return;
  187. print("ps2_mouse: [");
  188. phex(mouse_report->buttons); print("|");
  189. print_hex8((uint8_t)mouse_report->x); print(" ");
  190. print_hex8((uint8_t)mouse_report->y); print(" ");
  191. print_hex8((uint8_t)mouse_report->v); print(" ");
  192. print_hex8((uint8_t)mouse_report->h); print("]\n");
  193. }
  194. static inline void ps2_mouse_enable_scrolling(void) {
  195. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate");
  196. PS2_MOUSE_SEND(200, "200");
  197. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  198. PS2_MOUSE_SEND(100, "100");
  199. PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
  200. PS2_MOUSE_SEND(80, "80");
  201. PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel");
  202. _delay_ms(20);
  203. }
  204. #define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK)
  205. #define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK)
  206. static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) {
  207. static enum {
  208. SCROLL_NONE,
  209. SCROLL_BTN,
  210. SCROLL_SENT,
  211. } scroll_state = SCROLL_NONE;
  212. static uint16_t scroll_button_time = 0;
  213. if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) {
  214. // All scroll buttons are pressed
  215. if (scroll_state == SCROLL_NONE) {
  216. scroll_button_time = timer_read();
  217. scroll_state = SCROLL_BTN;
  218. }
  219. // If the mouse has moved, update the report to scroll instead of move the mouse
  220. if (mouse_report->x || mouse_report->y) {
  221. scroll_state = SCROLL_SENT;
  222. mouse_report->v = -mouse_report->y/(PS2_MOUSE_SCROLL_DIVISOR_V);
  223. mouse_report->h = mouse_report->x/(PS2_MOUSE_SCROLL_DIVISOR_H);
  224. mouse_report->x = 0;
  225. mouse_report->y = 0;
  226. }
  227. } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) {
  228. // None of the scroll buttons are pressed
  229. #if PS2_MOUSE_SCROLL_BTN_SEND
  230. if (scroll_state == SCROLL_BTN
  231. && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
  232. PRESS_SCROLL_BUTTONS;
  233. host_mouse_send(mouse_report);
  234. _delay_ms(100);
  235. RELEASE_SCROLL_BUTTONS;
  236. }
  237. #endif
  238. scroll_state = SCROLL_NONE;
  239. }
  240. RELEASE_SCROLL_BUTTONS;
  241. }