blob: fd462193bac753e6d1fe1bf2c4cdf9d6f890a173 [file] [log] [blame]
Kevin O'Connor4b60c002008-02-25 22:29:55 -05001// 16bit code to handle mouse events.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2002 MandrakeSoft S.A.
5//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
8#include "biosvar.h" // struct bregs
9#include "util.h" // debug_enter
10
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050011static char panic_msg_keyb_buffer_full[] = "%s: keyboard input buffer full\n";
12
13static void
14set_kbd_command_byte(u8 command_byte)
Kevin O'Connor4b60c002008-02-25 22:29:55 -050015{
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050016 if (inb(PORT_PS2_STATUS) & 0x02)
17 BX_PANIC(panic_msg_keyb_buffer_full, "setkbdcomm");
18 outb(0xD4, PORT_PS2_STATUS);
19
20 outb(0x60, PORT_PS2_STATUS); // write command byte
21 outb(command_byte, PORT_PS2_DATA);
Kevin O'Connor4b60c002008-02-25 22:29:55 -050022}
23
24static u8
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050025inhibit_mouse_int_and_events()
26{
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)
32 ;
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;
43}
44
45static void
46enable_mouse_int_and_events()
47{
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)
53 ;
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);
62}
63
64static void
65send_to_mouse_ctrl(u8 sendbyte)
66{
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);
72}
73
74static void
75get_mouse_data(u8 *data)
76{
77 while ((inb(PORT_PS2_STATUS) & 0x21) != 0x21)
78 ;
79 *data = inb(PORT_PS2_DATA);
80}
81
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
88
89// Disable Mouse
90static void
91mouse_15c20000(struct bregs *regs)
92{
93 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
94 send_to_mouse_ctrl(0xF5); // disable mouse command
95 u8 mouse_data1;
96 get_mouse_data(&mouse_data1);
97 handle_ret(regs, RET_SUCCESS);
98}
99
100#define BX_DEBUG_INT15(args...)
101
102// Enable Mouse
103static void
104mouse_15c20001(struct bregs *regs)
105{
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);
110 return;
111 }
112 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
113 send_to_mouse_ctrl(0xF4); // enable mouse command
114 u8 mouse_data1;
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);
119 return;
120 }
121 handle_ret(regs, RET_ENEEDRESEND);
122}
123
124static void
125mouse_15c200XX(struct bregs *regs)
126{
127 handle_ret(regs, RET_EINVFUNCTION);
128}
129
130// Disable/Enable Mouse
131static void
132mouse_15c200(struct bregs *regs)
133{
134 switch (regs->bh) {
135 case 0x00: mouse_15c20000(regs); break;
136 case 0x01: mouse_15c20001(regs); break;
137 default: mouse_15c200XX(regs); break;
138 }
139}
140
141// Reset Mouse
142static void
143mouse_15c201(struct bregs *regs)
144{
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);
152 return;
153 }
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);
164}
165
166// Set Sample Rate
167static void
168mouse_15c202(struct bregs *regs)
169{
170 if (regs->bh >= 7) {
171 handle_ret(regs, RET_EUNSUPPORTED);
172 return;
173 }
174 u8 mouse_data1 = regs->bh * 20;
175 if (!mouse_data1)
176 mouse_data1 = 10;
177 send_to_mouse_ctrl(0xF3); // set sample rate command
178 u8 mouse_data2;
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);
183}
184
185// Set Resolution
186static void
187mouse_15c203(struct bregs *regs)
188{
189 // BH:
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
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500194 u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
195 if (regs->bh >= 4) {
196 handle_ret(regs, RET_EUNSUPPORTED);
197 goto done;
198 }
199 send_to_mouse_ctrl(0xE8); // set resolution command
200 u8 mouse_data1;
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);
211
212done:
213 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
214}
215
216// Get Device ID
217static void
218mouse_15c204(struct bregs *regs)
219{
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);
227}
228
229// Initialize Mouse
230static void
231mouse_15c205(struct bregs *regs)
232{
233 if (regs->bh != 3) {
234 handle_ret(regs, RET_EINTERFACE);
235 return;
236 }
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500237 SET_EBDA(mouse_flag1, 0x00);
238 SET_EBDA(mouse_flag2, regs->bh);
239
240 // Reset Mouse
241 mouse_15c201(regs);
242}
243
244// Return Status
245static void
246mouse_15c20600(struct bregs *regs)
247{
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
263}
264
265static void
266set_scaling(struct bregs *regs, u8 cmd)
267{
268 u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
269 send_to_mouse_ctrl(0xE6);
270 u8 mouse_data1;
271 get_mouse_data(&mouse_data1);
272 if (mouse_data1 != 0xFA)
273 handle_ret(regs, RET_EUNSUPPORTED);
274 else
275 handle_ret(regs, RET_SUCCESS);
276 set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
277}
278
279// Set Scaling Factor to 1:1
280static void
281mouse_15c20601(struct bregs *regs)
282{
283 set_scaling(regs, 0xE6);
284}
285
286// Set Scaling Factor to 2:1
287static void
288mouse_15c20602(struct bregs *regs)
289{
290 set_scaling(regs, 0xE7);
291}
292
293static void
294mouse_15c206XX(struct bregs *regs)
295{
296 BX_PANIC("INT 15h C2 AL=6, BH=%02x\n", regs->bh);
297}
298
299// Return Status & Set Scaling Factor...
300static void
301mouse_15c206(struct bregs *regs)
302{
303 switch (regs->bh) {
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;
308 }
309}
310
311// Set Mouse Handler Address
312static void
313mouse_15c207(struct bregs *regs)
314{
315 u32 farptr = (regs->es << 16) | regs->bx;
316 SET_EBDA(far_call_pointer, farptr);
317 u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
318 if (! farptr) {
319 /* remove handler */
320 if ((mouse_flags_2 & 0x80) != 0) {
321 mouse_flags_2 &= ~0x80;
322 inhibit_mouse_int_and_events(); // disable IRQ12 and packets
323 }
324 } else {
325 /* install handler */
326 mouse_flags_2 |= 0x80;
327 }
328 SET_EBDA(mouse_flag2, mouse_flags_2);
329 handle_ret(regs, RET_SUCCESS);
330}
331
332static void
333mouse_15c2XX(struct bregs *regs)
334{
335 handle_ret(regs, RET_EINVFUNCTION);
336}
337
338void
339handle_15c2(struct bregs *regs)
340{
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500341 //debug_stub(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500342
343 if (! CONFIG_PS2_MOUSE) {
344 handle_ret(regs, RET_EUNSUPPORTED);
345 return;
346 }
347
348 switch (regs->al) {
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;
358 }
359}
360
361static void
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500362int74_function()
363{
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500364 u8 v = inb(PORT_PS2_STATUS);
365 if ((v & 0x21) != 0x21)
366 return;
367
368 v = inb(PORT_PS2_DATA);
369
370 u8 mouse_flags_1 = GET_EBDA(mouse_flag1);
371 u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
372
373 if ((mouse_flags_2 & 0x80) != 0x80)
374 return;
375
376 u8 package_count = mouse_flags_2 & 0x07;
377 u8 index = mouse_flags_1 & 0x07;
378 SET_EBDA(mouse_data[index], v);
379
380 if ((index+1) < package_count) {
381 mouse_flags_1++;
382 SET_EBDA(mouse_flag1, mouse_flags_1);
383 return;
384 }
385
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))
393 return;
394
395 u32 func = GET_EBDA(far_call_pointer);
396 asm volatile(
397 "pushl %%ebp\n"
398 "pushfl\n"
399 "pushl %0\n"
400 "pushw %w1\n" // status
401 "pushw %w2\n" // X
402 "pushw %w3\n" // Y
403 "pushw $0\n" // Z
404 "lcallw *8(%%esp)\n"
405 "addl $12, %%esp\n"
406 "popfl\n"
407 "popl %%ebp\n"
408 : "+a" (func), "+b" (status), "+c" (X), "+d" (Y)
409 :
410 : "esi", "edi"
411 );
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500412}
413
414// INT74h : PS/2 mouse hardware interrupt
415void VISIBLE
416handle_74(struct bregs *regs)
417{
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500418 //debug_isr(regs);
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500419
420 irq_enable();
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500421 int74_function();
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500422 irq_disable();
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500423
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500424 eoi_both_pics();
425}