1 /////////////////////////////////////////////////////////////////////////
2 // $Id: busmouse.cc 14163 2021-02-26 20:37:49Z vruppert $
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2004-2021 The Bochs Project
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21
22 // Initial/additional code by Ben Lunt 'fys at fysnet dot net'
23 // http://www.fysnet.net
24
25 // Define BX_PLUGGABLE in files that can be compiled into plugins. For
26 // platforms that require a special tag on exported symbols, BX_PLUGGABLE
27 // is used to know when we are exporting symbols and when we are importing.
28 #define BX_PLUGGABLE
29
30 #include "iodev.h"
31 #include "busmouse.h"
32
33 #if BX_SUPPORT_BUSMOUSE
34
35 #define LOG_THIS theBusMouse->
36
37 bx_busm_c *theBusMouse = NULL;
38
39 // There is a difference between older hardware and newer hardware.
40 // Set this flag to indicate older hardware. I will explain the difference
41 // at each item this flag is used.
42 #define LEGACY_BUSMOUSE 0
43
44 #define BUS_MOUSE_IRQ 5
45 #define IRQ_MASK ((1<<5) >> BUS_MOUSE_IRQ)
46
47 // MS Inport Bus Mouse Adapter
48 #define INP_PORT_CONTROL 0x023C
49 #define INP_PORT_DATA 0x023D
50 #define INP_PORT_SIGNATURE 0x023E
51 #define INP_PORT_CONFIG 0x023F
52
53 #define INP_CTRL_READ_BUTTONS 0x00
54 #define INP_CTRL_READ_X 0x01
55 #define INP_CTRL_READ_Y 0x02
56 #define INP_CTRL_COMMAND 0x07
57 #define INP_CTRL_RAISE_IRQ 0x16
58 #define INP_CTRL_RESET 0x80
59
60 #define INP_HOLD_COUNTER (1 << 5)
61 #define INP_ENABLE_IRQ (1 << 0)
62
63 // MS/Logictech Standard Bus Mouse Adapter
64 #define BUSM_PORT_DATA 0x023C
65 #define BUSM_PORT_SIGNATURE 0x023D
66 #define BUSM_PORT_CONTROL 0x023E
67 #define BUSM_PORT_CONFIG 0x023F
68
69 #define HOLD_COUNTER (1 << 7)
70 #define READ_X (0 << 6)
71 #define READ_Y (1 << 6)
72 #define READ_LOW (0 << 5)
73 #define READ_HIGH (1 << 5)
74 #define DISABLE_IRQ (1 << 4)
75
76 #define READ_X_LOW (READ_X | READ_LOW)
77 #define READ_X_HIGH (READ_X | READ_HIGH)
78 #define READ_Y_LOW (READ_Y | READ_LOW)
79 #define READ_Y_HIGH (READ_Y | READ_HIGH)
80
81
PLUGIN_ENTRY_FOR_MODULE(busmouse)82 PLUGIN_ENTRY_FOR_MODULE(busmouse)
83 {
84 if (mode == PLUGIN_INIT) {
85 // Create one instance of the busmouse device object.
86 theBusMouse = new bx_busm_c();
87 // Register this device.
88 BX_REGISTER_DEVICE_DEVMODEL(plugin, type, theBusMouse, BX_PLUGIN_BUSMOUSE);
89 } else if (mode == PLUGIN_FINI) {
90 delete theBusMouse;
91 } else if (mode == PLUGIN_PROBE) {
92 return (int)PLUGTYPE_OPTIONAL;
93 }
94 return 0; // Success
95 }
96
bx_busm_c()97 bx_busm_c::bx_busm_c()
98 {
99 put("busmouse", "BUSM");
100 }
101
~bx_busm_c()102 bx_busm_c::~bx_busm_c()
103 {
104 SIM->get_bochs_root()->remove("busmouse");
105 BX_DEBUG(("Exit"));
106 }
107
init(void)108 void bx_busm_c::init(void)
109 {
110 BX_DEBUG(("Init $Id: busmouse.cc 14163 2021-02-26 20:37:49Z vruppert $"));
111
112 BX_BUSM_THIS type = SIM->get_param_enum(BXPN_MOUSE_TYPE)->get();
113
114 DEV_register_irq(BUS_MOUSE_IRQ, "Bus Mouse");
115
116 // Call our timer routine at 30hz
117 BX_BUSM_THIS timer_index =
118 DEV_register_timer(this, timer_handler, 33334, 1, 1, "bus mouse timer");
119
120 for (int i=0x23C; i<=0x23F; i++) {
121 DEV_register_ioread_handler(this, read_handler, i, "Bus Mouse", 1);
122 DEV_register_iowrite_handler(this, write_handler, i, "Bus Mouse", 1);
123 }
124 DEV_register_default_mouse(this, mouse_enq_static, NULL);
125
126 BX_BUSM_THIS mouse_delayed_dx = 0;
127 BX_BUSM_THIS mouse_delayed_dy = 0;
128 BX_BUSM_THIS mouse_buttons = 0;
129 BX_BUSM_THIS current_x =
130 BX_BUSM_THIS current_y =
131 BX_BUSM_THIS current_b = 0;
132
133 if (BX_BUSM_THIS type == BX_MOUSE_TYPE_INPORT) {
134 BX_BUSM_THIS control_val = 0; // the control port value
135 BX_BUSM_THIS mouse_buttons_last = 0;
136 } else {
137 BX_BUSM_THIS control_val = 0x1f; // the control port value
138 BX_BUSM_THIS config_val = 0x0e; // the config port value
139 BX_BUSM_THIS sig_val = 0; // the signature port value
140 }
141 BX_BUSM_THIS command_val = 0; // command byte
142 BX_BUSM_THIS toggle_counter = 0; // signature byte / IRQ bit toggle
143 BX_BUSM_THIS interrupts = 0; // interrupts off
144
145 if (BX_BUSM_THIS type == BX_MOUSE_TYPE_INPORT) {
146 BX_INFO(("MS Inport BusMouse initialized"));
147 } else {
148 BX_INFO(("Standard MS/Logitech BusMouse initialized"));
149 }
150 }
151
register_state(void)152 void bx_busm_c::register_state(void)
153 {
154 bx_list_c *list = new bx_list_c(SIM->get_bochs_root(), "busmouse", "Busmouse State");
155 BXRS_DEC_PARAM_FIELD(list, mouse_delayed_dx, BX_BUSM_THIS mouse_delayed_dx);
156 BXRS_DEC_PARAM_FIELD(list, mouse_delayed_dy, BX_BUSM_THIS mouse_delayed_dy);
157 BXRS_HEX_PARAM_FIELD(list, mouse_buttons, BX_BUSM_THIS mouse_buttons);
158 BXRS_HEX_PARAM_FIELD(list, mouse_buttons_last, BX_BUSM_THIS mouse_buttons_last);
159 BXRS_HEX_PARAM_FIELD(list, current_x, BX_BUSM_THIS current_x);
160 BXRS_HEX_PARAM_FIELD(list, current_y, BX_BUSM_THIS current_y);
161 BXRS_HEX_PARAM_FIELD(list, current_b, BX_BUSM_THIS current_b);
162
163 BXRS_HEX_PARAM_FIELD(list, control_val, BX_BUSM_THIS control_val);
164 BXRS_HEX_PARAM_FIELD(list, command_val, BX_BUSM_THIS command_val);
165 BXRS_HEX_PARAM_FIELD(list, toggle_counter, BX_BUSM_THIS toggle_counter);
166 BXRS_PARAM_BOOL(list, interrupts, BX_BUSM_THIS interrupts);
167
168 BXRS_HEX_PARAM_FIELD(list, config_val, BX_BUSM_THIS config_val);
169 BXRS_HEX_PARAM_FIELD(list, sig_val, BX_BUSM_THIS sig_val);
170 }
171
172 // static IO port read callback handler
173 // redirects to non-static class handler to avoid virtual functions
read_handler(void * this_ptr,Bit32u address,unsigned io_len)174 Bit32u bx_busm_c::read_handler(void *this_ptr, Bit32u address, unsigned io_len)
175 {
176 #if !BX_USE_BUSM_SMF
177 bx_busm_c *class_ptr = (bx_busm_c *) this_ptr;
178 return class_ptr->read(address, io_len);
179 }
180
read(Bit32u address,unsigned io_len)181 Bit32u bx_busm_c::read(Bit32u address, unsigned io_len)
182 {
183 #else
184 UNUSED(this_ptr);
185 #endif // !BX_USE_BUSM_SMF
186
187 Bit8u value = 0;
188
189 if (BX_BUSM_THIS type == BX_MOUSE_TYPE_INPORT) {
190 switch (address) {
191 case INP_PORT_CONTROL:
192 value = BX_BUSM_THIS control_val;
193 break;
194 case INP_PORT_DATA:
195 switch (BX_BUSM_THIS command_val) {
196 case INP_CTRL_READ_BUTTONS:
197 value = BX_BUSM_THIS current_b;
198 #if !LEGACY_BUSMOUSE
199 value |= 0x40; // Newer Logitech mouse drivers look for bit 6 set
200 #endif
201 break;
202 case INP_CTRL_READ_X:
203 value = BX_BUSM_THIS current_x;
204 break;
205 case INP_CTRL_READ_Y:
206 value = BX_BUSM_THIS current_y;
207 break;
208 case INP_CTRL_COMMAND:
209 value = BX_BUSM_THIS control_val;
210 break;
211 default:
212 BX_ERROR(("Reading data port in unsupported mode 0x%02x", BX_BUSM_THIS control_val));
213 }
214 break;
215 case INP_PORT_SIGNATURE:
216 if (!BX_BUSM_THIS toggle_counter) {
217 value = 0xDE;
218 } else {
219 value = 0x12; // Manufacturer id ?
220 }
221 BX_BUSM_THIS toggle_counter ^= 1;
222 break;
223 case INP_PORT_CONFIG:
224 BX_ERROR(("Unsupported read from port 0x%04x", address));
225 break;
226 }
227 } else {
228 switch (address) {
229 case BUSM_PORT_CONTROL:
230 value = BX_BUSM_THIS control_val;
231 // this is to allow the driver to see which IRQ the card has "jumpered"
232 // only happens if IRQ's are enabled
233 BX_BUSM_THIS control_val |= 0x0F;
234 if ((BX_BUSM_THIS toggle_counter > 0x3FF)
235 // newer hardware only changes the bit when interrupts are on
236 // older hardware changes the bit no matter if interrupts are on or not
237 #if !LEGACY_BUSMOUSE
238 && BX_BUSM_THIS interrupts
239 #endif
240 )
241 BX_BUSM_THIS control_val &= ~IRQ_MASK;
242 BX_BUSM_THIS toggle_counter = (BX_BUSM_THIS toggle_counter + 1) & 0x7FF;
243 break;
244 case BUSM_PORT_DATA:
245 switch (BX_BUSM_THIS control_val & 0x60) {
246 case READ_X_LOW:
247 value = BX_BUSM_THIS current_x & 0x0F;
248 break;
249 case READ_X_HIGH:
250 value = (BX_BUSM_THIS current_x >> 4) & 0x0F;
251 break;
252 case READ_Y_LOW:
253 value = BX_BUSM_THIS current_y & 0x0F;
254 break;
255 case READ_Y_HIGH:
256 value = ((BX_BUSM_THIS current_b ^ 7) << 5) | ((BX_BUSM_THIS current_y >> 4) & 0x0F);
257 break;
258 default:
259 BX_ERROR(("Reading data port in unsupported mode 0x%02x", BX_BUSM_THIS control_val));
260 }
261 break;
262 case BUSM_PORT_CONFIG:
263 value = BX_BUSM_THIS config_val;
264 break;
265 case BUSM_PORT_SIGNATURE:
266 value = BX_BUSM_THIS sig_val;
267 break;
268 }
269 }
270
271 BX_DEBUG(("read from address 0x%04x, value = 0x%02x ", address, value));
272
273 return value;
274 }
275
276 // static IO port write callback handler
277 // redirects to non-static class handler to avoid virtual functions
278
write_handler(void * this_ptr,Bit32u address,Bit32u value,unsigned io_len)279 void bx_busm_c::write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len)
280 {
281 #if !BX_USE_BUSM_SMF
282 bx_busm_c *class_ptr = (bx_busm_c *) this_ptr;
283 class_ptr->write(address, value, io_len);
284 }
285
write(Bit32u address,Bit32u value,unsigned io_len)286 void bx_busm_c::write(Bit32u address, Bit32u value, unsigned io_len)
287 {
288 #else
289 UNUSED(this_ptr);
290 #endif // !BX_USE_BUSM_SMF
291
292 BX_DEBUG(("write to address 0x%04x, value = 0x%02x ", address, value));
293
294 if (BX_BUSM_THIS type == BX_MOUSE_TYPE_INPORT) {
295 switch (address) {
296 case INP_PORT_CONTROL:
297 switch (value) {
298 case INP_CTRL_RESET:
299 BX_BUSM_THIS control_val = 0;
300 BX_BUSM_THIS command_val = 0;
301 break;
302 case INP_CTRL_COMMAND:
303 case INP_CTRL_READ_BUTTONS:
304 case INP_CTRL_READ_X:
305 case INP_CTRL_READ_Y:
306 BX_BUSM_THIS command_val = value;
307 break;
308 case 0x87: // ???????
309 BX_BUSM_THIS control_val = 0;
310 BX_BUSM_THIS command_val = 0x07;
311 break;
312 default:
313 BX_ERROR(("Unsupported command written to port 0x%04x (value = 0x%02x)", address, value));
314 }
315 break;
316 case INP_PORT_DATA:
317 DEV_pic_lower_irq(BUS_MOUSE_IRQ);
318 if (value == INP_CTRL_RAISE_IRQ) {
319 DEV_pic_raise_irq(BUS_MOUSE_IRQ);
320 } else {
321 switch (BX_BUSM_THIS command_val) {
322 case INP_CTRL_COMMAND:
323 BX_BUSM_THIS control_val = value;
324 BX_BUSM_THIS interrupts = (value & INP_ENABLE_IRQ) > 0;
325 break;
326 default:
327 BX_ERROR(("Unsupported write to port 0x%04x (value = 0x%02x)", address, value));
328 }
329 }
330 break;
331 case INP_PORT_SIGNATURE:
332 case INP_PORT_CONFIG:
333 BX_ERROR(("Unsupported write to port 0x%04x (value = 0x%02x)", address, value));
334 break;
335 }
336 } else {
337 switch (address) {
338 case BUSM_PORT_CONTROL:
339 BX_BUSM_THIS control_val = value | 0x0F;
340 BX_BUSM_THIS interrupts = (value & DISABLE_IRQ) == 0;
341 DEV_pic_lower_irq(BUS_MOUSE_IRQ);
342 break;
343 case BUSM_PORT_CONFIG:
344 BX_BUSM_THIS config_val = value;
345 break;
346 case BUSM_PORT_SIGNATURE:
347 BX_BUSM_THIS sig_val = value;
348 break;
349 case BUSM_PORT_DATA:
350 BX_ERROR(("Unsupported write to port 0x%04x (value = 0x%02x)", address, value));
351 break;
352 }
353 }
354 }
355
mouse_enq_static(void * dev,int delta_x,int delta_y,int delta_z,unsigned button_state,bool absxy)356 void bx_busm_c::mouse_enq_static(void *dev, int delta_x, int delta_y, int delta_z, unsigned button_state, bool absxy)
357 {
358 ((bx_busm_c*)dev)->mouse_enq(delta_x, delta_y, delta_z, button_state);
359 }
360
mouse_enq(int delta_x,int delta_y,int delta_z,unsigned button_state)361 void bx_busm_c::mouse_enq(int delta_x, int delta_y, int delta_z, unsigned button_state)
362 {
363 // scale down the motion
364 if ((delta_x < -1) || (delta_x > 1))
365 delta_x /= 2;
366 if ((delta_y < -1) || (delta_y > 1))
367 delta_y /= 2;
368
369 if (delta_x > 127) delta_x =127;
370 if (delta_y > 127) delta_y =127;
371 if (delta_x < -128) delta_x = -128;
372 if (delta_y < -128) delta_y = -128;
373
374 BX_BUSM_THIS mouse_delayed_dx += delta_x;
375 BX_BUSM_THIS mouse_delayed_dy -= delta_y;
376
377 // change button_state MRL to LMR
378 BX_BUSM_THIS mouse_buttons = (Bit8u) (((button_state & 1) << 2) |
379 ((button_state & 4) >> 1) | ((button_state & 2) >> 1));
380
381 if (BX_BUSM_THIS type == BX_MOUSE_TYPE_INPORT) {
382 // The InPort button status is as so
383 // 00lmrLMR
384 // where lower case letters are changed in state
385 // where upper case letters are the state of that button
386 // TODO:
387 // Bochs needs to call this function one more time on a button release so that
388 // bits 5:3 get cleared. However, as is, it works with the code I tested it on,
389 // so no worries, so far.
390 if ((BX_BUSM_THIS mouse_buttons & (1<<2)) ||
391 ((BX_BUSM_THIS mouse_buttons_last & (1<<2)) && !(BX_BUSM_THIS mouse_buttons & (1<<2))))
392 BX_BUSM_THIS mouse_buttons |= (1<<5);
393 if ((BX_BUSM_THIS mouse_buttons & (1<<1)) ||
394 ((BX_BUSM_THIS mouse_buttons_last & (1<<1)) && !(BX_BUSM_THIS mouse_buttons & (1<<1))))
395 BX_BUSM_THIS mouse_buttons |= (1<<4);
396 if ((BX_BUSM_THIS mouse_buttons & (1<<0)) ||
397 ((BX_BUSM_THIS mouse_buttons_last & (1<<0)) && !(BX_BUSM_THIS mouse_buttons & (1<<0))))
398 BX_BUSM_THIS mouse_buttons |= (1<<3);
399 BX_BUSM_THIS mouse_buttons_last = BX_BUSM_THIS mouse_buttons;
400 }
401 }
402
update_mouse_data()403 void bx_busm_c::update_mouse_data()
404 {
405 int delta_x, delta_y;
406 bool hold;
407
408 if (BX_BUSM_THIS mouse_delayed_dx > 127) {
409 delta_x = 127;
410 BX_BUSM_THIS mouse_delayed_dx -= 127;
411 } else if (BX_BUSM_THIS mouse_delayed_dx < -128) {
412 delta_x = -128;
413 BX_BUSM_THIS mouse_delayed_dx += 128;
414 } else {
415 delta_x = BX_BUSM_THIS mouse_delayed_dx;
416 BX_BUSM_THIS mouse_delayed_dx = 0;
417 }
418 if (BX_BUSM_THIS mouse_delayed_dy > 127) {
419 delta_y = 127;
420 BX_BUSM_THIS mouse_delayed_dy -= 127;
421 } else if (BX_BUSM_THIS mouse_delayed_dy < -128) {
422 delta_y = -128;
423 BX_BUSM_THIS mouse_delayed_dy += 128;
424 } else {
425 delta_y = BX_BUSM_THIS mouse_delayed_dy;
426 BX_BUSM_THIS mouse_delayed_dy = 0;
427 }
428
429 if (BX_BUSM_THIS type == BX_MOUSE_TYPE_INPORT) {
430 hold = (BX_BUSM_THIS control_val & INP_HOLD_COUNTER) > 0;
431 } else {
432 hold = (BX_BUSM_THIS control_val & HOLD_COUNTER) > 0;
433 }
434 if (!hold) {
435 BX_BUSM_THIS current_x = (Bit8u) delta_x;
436 BX_BUSM_THIS current_y = (Bit8u) delta_y;
437 BX_BUSM_THIS current_b = BX_BUSM_THIS mouse_buttons;
438 }
439 }
440
timer_handler(void * this_ptr)441 void bx_busm_c::timer_handler(void *this_ptr)
442 {
443 bx_busm_c *class_ptr = (bx_busm_c *) this_ptr;
444 class_ptr->busm_timer();
445 }
446
447 // Called at 30hz
busm_timer(void)448 void bx_busm_c::busm_timer(void)
449 {
450 // The controller updates the data on every interrupt
451 // We just don't copy it to the current_X if the 'hold' bit is set
452 BX_BUSM_THIS update_mouse_data();
453
454 // if interrupts are on, fire the interrupt
455 if (BX_BUSM_THIS interrupts) {
456 DEV_pic_raise_irq(BUS_MOUSE_IRQ);
457 BX_DEBUG(("Interrupt Fired..."));
458 }
459 }
460
461 #endif // BX_SUPPORT_BUSMOUSE
462