1 // 16bit code to handle mouse events.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "biosvar.h" // struct bregs
9 #include "util.h" // debug_enter
11 static char panic_msg_keyb_buffer_full[] = "%s: keyboard input buffer full\n";
14 set_kbd_command_byte(u8 command_byte)
16 if (inb(PORT_PS2_STATUS) & 0x02)
17 BX_PANIC(panic_msg_keyb_buffer_full, "setkbdcomm");
18 outb(0xD4, PORT_PS2_STATUS);
20 outb(0x60, PORT_PS2_STATUS); // write command byte
21 outb(command_byte, PORT_PS2_DATA);
25 inhibit_mouse_int_and_events()
27 // Turn off IRQ generation and aux data line
28 if (inb(PORT_PS2_STATUS) & 0x02)
29 BX_PANIC(panic_msg_keyb_buffer_full,"inhibmouse");
30 outb(0x20, PORT_PS2_STATUS); // get command byte
31 while ((inb(PORT_PS2_STATUS) & 0x01) != 0x01)
33 u8 prev_command_byte = inb(PORT_PS2_DATA);
34 u8 command_byte = prev_command_byte;
35 //while ( (inb(PORT_PS2_STATUS) & 0x02) );
36 if ( inb(PORT_PS2_STATUS) & 0x02 )
37 BX_PANIC(panic_msg_keyb_buffer_full,"inhibmouse");
38 command_byte &= 0xfd; // turn off IRQ 12 generation
39 command_byte |= 0x20; // disable mouse serial clock line
40 outb(0x60, PORT_PS2_STATUS); // write command byte
41 outb(command_byte, PORT_PS2_DATA);
42 return prev_command_byte;
46 enable_mouse_int_and_events()
48 // Turn on IRQ generation and aux data line
49 if ( inb(PORT_PS2_STATUS) & 0x02 )
50 BX_PANIC(panic_msg_keyb_buffer_full,"enabmouse");
51 outb(0x20, PORT_PS2_STATUS); // get command byte
52 while ((inb(PORT_PS2_STATUS) & 0x01) != 0x01)
54 u8 command_byte = inb(PORT_PS2_DATA);
55 //while ( (inb(PORT_PS2_STATUS) & 0x02) );
56 if (inb(PORT_PS2_STATUS) & 0x02)
57 BX_PANIC(panic_msg_keyb_buffer_full,"enabmouse");
58 command_byte |= 0x02; // turn on IRQ 12 generation
59 command_byte &= 0xdf; // enable mouse serial clock line
60 outb(0x60, PORT_PS2_STATUS); // write command byte
61 outb(command_byte, PORT_PS2_DATA);
65 send_to_mouse_ctrl(u8 sendbyte)
67 // wait for chance to write to ctrl
68 if (inb(PORT_PS2_STATUS) & 0x02)
69 BX_PANIC(panic_msg_keyb_buffer_full,"sendmouse");
70 outb(0xD4, PORT_PS2_STATUS);
71 outb(sendbyte, PORT_PS2_DATA);
75 get_mouse_data(u8 *data)
77 while ((inb(PORT_PS2_STATUS) & 0x21) != 0x21)
79 *data = inb(PORT_PS2_DATA);
82 #define RET_SUCCESS 0x00
83 #define RET_EINVFUNCTION 0x01
84 #define RET_EINVINPUT 0x02
85 #define RET_EINTERFACE 0x03
86 #define RET_ENEEDRESEND 0x04
87 #define RET_ENOHANDLER 0x05
91 mouse_15c20000(struct bregs *regs)
93 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
94 send_to_mouse_ctrl(0xF5); // disable mouse command
96 get_mouse_data(&mouse_data1);
97 handle_ret(regs, RET_SUCCESS);
100 #define BX_DEBUG_INT15(args...)
104 mouse_15c20001(struct bregs *regs)
106 u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
107 if ((mouse_flags_2 & 0x80) == 0) {
108 BX_DEBUG_INT15("INT 15h C2 Enable Mouse, no far call handler\n");
109 handle_ret(regs, RET_ENOHANDLER);
112 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
113 send_to_mouse_ctrl(0xF4); // enable mouse command
115 get_mouse_data(&mouse_data1);
116 if (mouse_data1 == 0xFA) {
117 enable_mouse_int_and_events(); // turn IRQ12 and packet generation on
118 handle_ret(regs, RET_SUCCESS);
121 handle_ret(regs, RET_ENEEDRESEND);
125 mouse_15c200XX(struct bregs *regs)
127 handle_ret(regs, RET_EINVFUNCTION);
130 // Disable/Enable Mouse
132 mouse_15c200(struct bregs *regs)
135 case 0x00: mouse_15c20000(regs); break;
136 case 0x01: mouse_15c20001(regs); break;
137 default: mouse_15c200XX(regs); break;
143 mouse_15c201(struct bregs *regs)
145 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
146 send_to_mouse_ctrl(0xFF); // reset mouse command
147 u8 mouse_data1, mouse_data2, mouse_data3;
148 get_mouse_data(&mouse_data3);
149 // if no mouse attached, it will return RESEND
150 if (mouse_data3 == 0xfe) {
151 handle_ret(regs, RET_ENEEDRESEND);
154 if (mouse_data3 != 0xfa)
155 BX_PANIC("Mouse reset returned %02x (should be ack)\n"
156 , (unsigned)mouse_data3);
157 get_mouse_data(&mouse_data1);
158 get_mouse_data(&mouse_data2);
159 // turn IRQ12 and packet generation on
160 enable_mouse_int_and_events();
161 regs->bl = mouse_data1;
162 regs->bh = mouse_data2;
163 handle_ret(regs, RET_SUCCESS);
168 mouse_15c202(struct bregs *regs)
171 handle_ret(regs, RET_EUNSUPPORTED);
174 u8 mouse_data1 = regs->bh * 20;
177 send_to_mouse_ctrl(0xF3); // set sample rate command
179 get_mouse_data(&mouse_data2);
180 send_to_mouse_ctrl(mouse_data1);
181 get_mouse_data(&mouse_data2);
182 handle_ret(regs, RET_SUCCESS);
187 mouse_15c203(struct bregs *regs)
190 // 0 = 25 dpi, 1 count per millimeter
191 // 1 = 50 dpi, 2 counts per millimeter
192 // 2 = 100 dpi, 4 counts per millimeter
193 // 3 = 200 dpi, 8 counts per millimeter
194 u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
196 handle_ret(regs, RET_EUNSUPPORTED);
199 send_to_mouse_ctrl(0xE8); // set resolution command
201 get_mouse_data(&mouse_data1);
202 if (mouse_data1 != 0xfa)
203 BX_PANIC("Mouse status returned %02x (should be ack)\n"
204 , (unsigned)mouse_data1);
205 send_to_mouse_ctrl(regs->bh);
206 get_mouse_data(&mouse_data1);
207 if (mouse_data1 != 0xfa)
208 BX_PANIC("Mouse status returned %02x (should be ack)\n"
209 , (unsigned)mouse_data1);
210 handle_ret(regs, RET_SUCCESS);
213 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
218 mouse_15c204(struct bregs *regs)
220 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
221 send_to_mouse_ctrl(0xF2); // get mouse ID command
222 u8 mouse_data1, mouse_data2;
223 get_mouse_data(&mouse_data1);
224 get_mouse_data(&mouse_data2);
225 regs->bh = mouse_data2;
226 handle_ret(regs, RET_SUCCESS);
231 mouse_15c205(struct bregs *regs)
234 handle_ret(regs, RET_EINTERFACE);
237 SET_EBDA(mouse_flag1, 0x00);
238 SET_EBDA(mouse_flag2, regs->bh);
246 mouse_15c20600(struct bregs *regs)
248 u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
249 send_to_mouse_ctrl(0xE9); // get mouse info command
250 u8 mouse_data1, mouse_data2, mouse_data3;
251 get_mouse_data(&mouse_data1);
252 if (mouse_data1 != 0xfa)
253 BX_PANIC("Mouse status returned %02x (should be ack)\n"
254 , (unsigned)mouse_data1);
255 get_mouse_data(&mouse_data1);
256 get_mouse_data(&mouse_data2);
257 get_mouse_data(&mouse_data3);
258 regs->bl = mouse_data1;
259 regs->cl = mouse_data2;
260 regs->dl = mouse_data3;
261 handle_ret(regs, RET_SUCCESS);
262 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
266 set_scaling(struct bregs *regs, u8 cmd)
268 u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
269 send_to_mouse_ctrl(0xE6);
271 get_mouse_data(&mouse_data1);
272 if (mouse_data1 != 0xFA)
273 handle_ret(regs, RET_EUNSUPPORTED);
275 handle_ret(regs, RET_SUCCESS);
276 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
279 // Set Scaling Factor to 1:1
281 mouse_15c20601(struct bregs *regs)
283 set_scaling(regs, 0xE6);
286 // Set Scaling Factor to 2:1
288 mouse_15c20602(struct bregs *regs)
290 set_scaling(regs, 0xE7);
294 mouse_15c206XX(struct bregs *regs)
296 BX_PANIC("INT 15h C2 AL=6, BH=%02x\n", regs->bh);
299 // Return Status & Set Scaling Factor...
301 mouse_15c206(struct bregs *regs)
304 case 0x00: mouse_15c20600(regs); break;
305 case 0x01: mouse_15c20601(regs); break;
306 case 0x02: mouse_15c20602(regs); break;
307 default: mouse_15c206XX(regs); break;
311 // Set Mouse Handler Address
313 mouse_15c207(struct bregs *regs)
315 u32 farptr = (regs->es << 16) | regs->bx;
316 SET_EBDA(far_call_pointer, farptr);
317 u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
320 if ((mouse_flags_2 & 0x80) != 0) {
321 mouse_flags_2 &= ~0x80;
322 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
325 /* install handler */
326 mouse_flags_2 |= 0x80;
328 SET_EBDA(mouse_flag2, mouse_flags_2);
329 handle_ret(regs, RET_SUCCESS);
333 mouse_15c2XX(struct bregs *regs)
335 handle_ret(regs, RET_EINVFUNCTION);
339 handle_15c2(struct bregs *regs)
343 if (! CONFIG_PS2_MOUSE) {
344 handle_ret(regs, RET_EUNSUPPORTED);
349 case 0x00: mouse_15c200(regs); break;
350 case 0x01: mouse_15c201(regs); break;
351 case 0x02: mouse_15c202(regs); break;
352 case 0x03: mouse_15c203(regs); break;
353 case 0x04: mouse_15c204(regs); break;
354 case 0x05: mouse_15c205(regs); break;
355 case 0x06: mouse_15c206(regs); break;
356 case 0x07: mouse_15c207(regs); break;
357 default: mouse_15c2XX(regs); break;
364 u8 v = inb(PORT_PS2_STATUS);
365 if ((v & 0x21) != 0x21)
368 v = inb(PORT_PS2_DATA);
370 u8 mouse_flags_1 = GET_EBDA(mouse_flag1);
371 u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
373 if ((mouse_flags_2 & 0x80) != 0x80)
376 u8 package_count = mouse_flags_2 & 0x07;
377 u8 index = mouse_flags_1 & 0x07;
378 SET_EBDA(mouse_data[index], v);
380 if ((index+1) < package_count) {
382 SET_EBDA(mouse_flag1, mouse_flags_1);
386 //BX_DEBUG_INT74("int74_function: make_farcall=1\n");
387 u16 status = GET_EBDA(mouse_data[0]);
388 u16 X = GET_EBDA(mouse_data[1]);
389 u16 Y = GET_EBDA(mouse_data[2]);
390 SET_EBDA(mouse_flag1, 0);
391 // check if far call handler installed
392 if (! (mouse_flags_2 & 0x80))
395 u32 func = GET_EBDA(far_call_pointer);
400 "pushw %w1\n" // status
408 : "+a" (func), "+b" (status), "+c" (X), "+d" (Y)
414 // INT74h : PS/2 mouse hardware interrupt
416 handle_74(struct bregs *regs)