ps2_mouse.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.h"
  18. #include "ps2_mouse.h"
  19. #include "report.h"
  20. #include "host.h"
  21. #include "timer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #ifndef PS2_INIT_DELAY
  25. #define PS2_INIT_DELAY 1000
  26. #endif
  27. static report_mouse_t mouse_report = {};
  28. static void print_usb_data(void);
  29. /* supports only 3 button mouse at this time */
  30. uint8_t ps2_mouse_init(void) {
  31. uint8_t rcv;
  32. ps2_host_init();
  33. _delay_ms(PS2_INIT_DELAY); // wait for powering up
  34. // send Reset
  35. rcv = ps2_host_send(0xFF);
  36. print("ps2_mouse_init: send Reset: ");
  37. phex(rcv); phex(ps2_error); print("\n");
  38. // read completion code of BAT
  39. rcv = ps2_host_recv_response();
  40. print("ps2_mouse_init: read BAT: ");
  41. phex(rcv); phex(ps2_error); print("\n");
  42. // read Device ID
  43. rcv = ps2_host_recv_response();
  44. print("ps2_mouse_init: read DevID: ");
  45. phex(rcv); phex(ps2_error); print("\n");
  46. // send Set Remote mode
  47. rcv = ps2_host_send(0xF0);
  48. print("ps2_mouse_init: send 0xF0: ");
  49. phex(rcv); phex(ps2_error); print("\n");
  50. return 0;
  51. }
  52. #define X_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_X_SIGN))
  53. #define Y_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_Y_SIGN))
  54. #define X_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_X_OVFLW))
  55. #define Y_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_Y_OVFLW))
  56. void ps2_mouse_task(void)
  57. {
  58. enum { SCROLL_NONE, SCROLL_BTN, SCROLL_SENT };
  59. static uint8_t scroll_state = SCROLL_NONE;
  60. static uint8_t buttons_prev = 0;
  61. /* receives packet from mouse */
  62. uint8_t rcv;
  63. rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
  64. if (rcv == PS2_ACK) {
  65. mouse_report.buttons = ps2_host_recv_response();
  66. mouse_report.x = ps2_host_recv_response();
  67. mouse_report.y = ps2_host_recv_response();
  68. } else {
  69. if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
  70. return;
  71. }
  72. xprintf("%ud ", timer_read());
  73. print("ps2_mouse raw: [");
  74. phex(mouse_report.buttons); print("|");
  75. print_hex8((uint8_t)mouse_report.x); print(" ");
  76. print_hex8((uint8_t)mouse_report.y); print("]\n");
  77. /* if mouse moves or buttons state changes */
  78. if (mouse_report.x || mouse_report.y ||
  79. ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
  80. #ifdef PS2_MOUSE_DEBUG
  81. print("ps2_mouse raw: [");
  82. phex(mouse_report.buttons); print("|");
  83. print_hex8((uint8_t)mouse_report.x); print(" ");
  84. print_hex8((uint8_t)mouse_report.y); print("]\n");
  85. #endif
  86. buttons_prev = mouse_report.buttons;
  87. // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
  88. // bit: 8 7 ... 0
  89. // sign \8-bit/
  90. //
  91. // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
  92. //
  93. // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
  94. mouse_report.x = X_IS_NEG ?
  95. ((!X_IS_OVF && -127 <= mouse_report.x && mouse_report.x <= -1) ? mouse_report.x : -127) :
  96. ((!X_IS_OVF && 0 <= mouse_report.x && mouse_report.x <= 127) ? mouse_report.x : 127);
  97. mouse_report.y = Y_IS_NEG ?
  98. ((!Y_IS_OVF && -127 <= mouse_report.y && mouse_report.y <= -1) ? mouse_report.y : -127) :
  99. ((!Y_IS_OVF && 0 <= mouse_report.y && mouse_report.y <= 127) ? mouse_report.y : 127);
  100. // remove sign and overflow flags
  101. mouse_report.buttons &= PS2_MOUSE_BTN_MASK;
  102. // invert coordinate of y to conform to USB HID mouse
  103. mouse_report.y = -mouse_report.y;
  104. #if PS2_MOUSE_SCROLL_BTN_MASK
  105. static uint16_t scroll_button_time = 0;
  106. if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == (PS2_MOUSE_SCROLL_BTN_MASK)) {
  107. if (scroll_state == SCROLL_NONE) {
  108. scroll_button_time = timer_read();
  109. scroll_state = SCROLL_BTN;
  110. }
  111. // doesn't send Scroll Button
  112. //mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  113. if (mouse_report.x || mouse_report.y) {
  114. scroll_state = SCROLL_SENT;
  115. mouse_report.v = -mouse_report.y/(PS2_MOUSE_SCROLL_DIVISOR_V);
  116. mouse_report.h = mouse_report.x/(PS2_MOUSE_SCROLL_DIVISOR_H);
  117. mouse_report.x = 0;
  118. mouse_report.y = 0;
  119. //host_mouse_send(&mouse_report);
  120. }
  121. }
  122. else if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == 0) {
  123. #if PS2_MOUSE_SCROLL_BTN_SEND
  124. if (scroll_state == SCROLL_BTN &&
  125. TIMER_DIFF_16(timer_read(), scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
  126. // send Scroll Button(down and up at once) when not scrolled
  127. mouse_report.buttons |= (PS2_MOUSE_SCROLL_BTN_MASK);
  128. host_mouse_send(&mouse_report);
  129. _delay_ms(100);
  130. mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  131. }
  132. #endif
  133. scroll_state = SCROLL_NONE;
  134. }
  135. // doesn't send Scroll Button
  136. mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
  137. #endif
  138. host_mouse_send(&mouse_report);
  139. print_usb_data();
  140. }
  141. // clear report
  142. mouse_report.x = 0;
  143. mouse_report.y = 0;
  144. mouse_report.v = 0;
  145. mouse_report.h = 0;
  146. mouse_report.buttons = 0;
  147. }
  148. static void print_usb_data(void)
  149. {
  150. if (!debug_mouse) return;
  151. print("ps2_mouse usb: [");
  152. phex(mouse_report.buttons); print("|");
  153. print_hex8((uint8_t)mouse_report.x); print(" ");
  154. print_hex8((uint8_t)mouse_report.y); print(" ");
  155. print_hex8((uint8_t)mouse_report.v); print(" ");
  156. print_hex8((uint8_t)mouse_report.h); print("]\n");
  157. }
  158. /* PS/2 Mouse Synopsis
  159. * http://www.computer-engineering.org/ps2mouse/
  160. *
  161. * Command:
  162. * 0xFF: Reset
  163. * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
  164. * 0xF5: Disable Data Reporting
  165. * 0xF4: Enable Data Reporting
  166. * 0xF3: Set Sample Rate
  167. * 0xF2: Get Device ID
  168. * 0xF0: Set Remote Mode
  169. * 0xEB: Read Data
  170. * 0xEA: Set Stream Mode
  171. * 0xE9: Status Request
  172. * 0xE8: Set Resolution
  173. * 0xE7: Set Scaling 2:1
  174. * 0xE6: Set Scaling 1:1
  175. *
  176. * Mode:
  177. * Stream Mode: devices sends the data when it changs its state
  178. * Remote Mode: host polls the data periodically
  179. *
  180. * This code uses Remote Mode and polls the data with Read Data(0xEB).
  181. *
  182. * Data format:
  183. * byte|7 6 5 4 3 2 1 0
  184. * ----+--------------------------------------------------------------
  185. * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left
  186. * 1| X movement
  187. * 2| Y movement
  188. */