e3c1789743a51cf545f1d03a9209acb76f052dcf
[seabios.git] / src / mouse.c
1 // 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
11 static char panic_msg_keyb_buffer_full[] = "%s: keyboard input buffer full\n";
12
13 static void
14 set_kbd_command_byte(u8 command_byte)
15 {
16     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);
22 }
23
24 static u8
25 inhibit_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
45 static void
46 enable_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
64 static void
65 send_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
74 static void
75 get_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
90 static void
91 mouse_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
103 static void
104 mouse_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
124 static void
125 mouse_15c200XX(struct bregs *regs)
126 {
127     handle_ret(regs, RET_EINVFUNCTION);
128 }
129
130 // Disable/Enable Mouse
131 static void
132 mouse_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
142 static void
143 mouse_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
167 static void
168 mouse_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
186 static void
187 mouse_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
194     bprintf(0, "ebda_seg=%x\n", GET_BDA(ebda_seg));
195     u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
196     if (regs->bh >= 4) {
197         handle_ret(regs, RET_EUNSUPPORTED);
198         goto done;
199     }
200     send_to_mouse_ctrl(0xE8); // set resolution command
201     u8 mouse_data1;
202     get_mouse_data(&mouse_data1);
203     if (mouse_data1 != 0xfa)
204         BX_PANIC("Mouse status returned %02x (should be ack)\n"
205                  , (unsigned)mouse_data1);
206     send_to_mouse_ctrl(regs->bh);
207     get_mouse_data(&mouse_data1);
208     if (mouse_data1 != 0xfa)
209         BX_PANIC("Mouse status returned %02x (should be ack)\n"
210                  , (unsigned)mouse_data1);
211     handle_ret(regs, RET_SUCCESS);
212
213 done:
214     set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
215 }
216
217 // Get Device ID
218 static void
219 mouse_15c204(struct bregs *regs)
220 {
221     inhibit_mouse_int_and_events(); // disable IRQ12 and packets
222     send_to_mouse_ctrl(0xF2); // get mouse ID command
223     u8 mouse_data1, mouse_data2;
224     get_mouse_data(&mouse_data1);
225     get_mouse_data(&mouse_data2);
226     regs->bh = mouse_data2;
227     handle_ret(regs, RET_SUCCESS);
228 }
229
230 // Initialize Mouse
231 static void
232 mouse_15c205(struct bregs *regs)
233 {
234     if (regs->bh != 3) {
235         handle_ret(regs, RET_EINTERFACE);
236         return;
237     }
238     bprintf(0, "ebda_seg=%x\n", GET_BDA(ebda_seg));
239     SET_EBDA(mouse_flag1, 0x00);
240     SET_EBDA(mouse_flag2, regs->bh);
241
242     // Reset Mouse
243     mouse_15c201(regs);
244 }
245
246 // Return Status
247 static void
248 mouse_15c20600(struct bregs *regs)
249 {
250     u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
251     send_to_mouse_ctrl(0xE9); // get mouse info command
252     u8 mouse_data1, mouse_data2, mouse_data3;
253     get_mouse_data(&mouse_data1);
254     if (mouse_data1 != 0xfa)
255         BX_PANIC("Mouse status returned %02x (should be ack)\n"
256                  , (unsigned)mouse_data1);
257     get_mouse_data(&mouse_data1);
258     get_mouse_data(&mouse_data2);
259     get_mouse_data(&mouse_data3);
260     regs->bl = mouse_data1;
261     regs->cl = mouse_data2;
262     regs->dl = mouse_data3;
263     handle_ret(regs, RET_SUCCESS);
264     set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
265 }
266
267 static void
268 set_scaling(struct bregs *regs, u8 cmd)
269 {
270     u8 comm_byte = inhibit_mouse_int_and_events(); // disable IRQ12 and packets
271     send_to_mouse_ctrl(0xE6);
272     u8 mouse_data1;
273     get_mouse_data(&mouse_data1);
274     if (mouse_data1 != 0xFA)
275         handle_ret(regs, RET_EUNSUPPORTED);
276     else
277         handle_ret(regs, RET_SUCCESS);
278     set_kbd_command_byte(comm_byte); // restore IRQ12 and serial enable
279 }
280
281 // Set Scaling Factor to 1:1
282 static void
283 mouse_15c20601(struct bregs *regs)
284 {
285     set_scaling(regs, 0xE6);
286 }
287
288 // Set Scaling Factor to 2:1
289 static void
290 mouse_15c20602(struct bregs *regs)
291 {
292     set_scaling(regs, 0xE7);
293 }
294
295 static void
296 mouse_15c206XX(struct bregs *regs)
297 {
298     BX_PANIC("INT 15h C2 AL=6, BH=%02x\n", regs->bh);
299 }
300
301 // Return Status & Set Scaling Factor...
302 static void
303 mouse_15c206(struct bregs *regs)
304 {
305     switch (regs->bh) {
306     case 0x00: mouse_15c20600(regs); break;
307     case 0x01: mouse_15c20601(regs); break;
308     case 0x02: mouse_15c20602(regs); break;
309     default:   mouse_15c206XX(regs); break;
310     }
311 }
312
313 // Set Mouse Handler Address
314 static void
315 mouse_15c207(struct bregs *regs)
316 {
317     u32 farptr = (regs->es << 16) | regs->bx;
318     SET_EBDA(far_call_pointer, farptr);
319     u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
320     if (! farptr) {
321         /* remove handler */
322         if ((mouse_flags_2 & 0x80) != 0) {
323             mouse_flags_2 &= ~0x80;
324             inhibit_mouse_int_and_events(); // disable IRQ12 and packets
325         }
326     } else {
327         /* install handler */
328         mouse_flags_2 |= 0x80;
329     }
330     SET_EBDA(mouse_flag2, mouse_flags_2);
331     handle_ret(regs, RET_SUCCESS);
332 }
333
334 static void
335 mouse_15c2XX(struct bregs *regs)
336 {
337     handle_ret(regs, RET_EINVFUNCTION);
338 }
339
340 void
341 handle_15c2(struct bregs *regs)
342 {
343     debug_stub(regs);
344
345     if (! CONFIG_PS2_MOUSE) {
346         handle_ret(regs, RET_EUNSUPPORTED);
347         return;
348     }
349
350     switch (regs->al) {
351     case 0x00: mouse_15c200(regs); break;
352     case 0x01: mouse_15c201(regs); break;
353     case 0x02: mouse_15c202(regs); break;
354     case 0x03: mouse_15c203(regs); break;
355     case 0x04: mouse_15c204(regs); break;
356     case 0x05: mouse_15c205(regs); break;
357     case 0x06: mouse_15c206(regs); break;
358     case 0x07: mouse_15c207(regs); break;
359     default:   mouse_15c2XX(regs); break;
360     }
361 }
362
363 static void
364 int74_function()
365 {
366     u8 v = inb(PORT_PS2_STATUS);
367     if ((v & 0x21) != 0x21)
368         return;
369
370     v = inb(PORT_PS2_DATA);
371
372     u8 mouse_flags_1 = GET_EBDA(mouse_flag1);
373     u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
374
375     if ((mouse_flags_2 & 0x80) != 0x80)
376         return;
377
378     u8 package_count = mouse_flags_2 & 0x07;
379     u8 index = mouse_flags_1 & 0x07;
380     SET_EBDA(mouse_data[index], v);
381
382     if ((index+1) < package_count) {
383         mouse_flags_1++;
384         SET_EBDA(mouse_flag1, mouse_flags_1);
385         return;
386     }
387
388     //BX_DEBUG_INT74("int74_function: make_farcall=1\n");
389     u16 status = GET_EBDA(mouse_data[0]);
390     u16 X      = GET_EBDA(mouse_data[1]);
391     u16 Y      = GET_EBDA(mouse_data[2]);
392     SET_EBDA(mouse_flag1, 0);
393     // check if far call handler installed
394     if (! (mouse_flags_2 & 0x80))
395         return;
396
397     u32 func = GET_EBDA(far_call_pointer);
398     asm volatile(
399         "pushl %%ebp\n"
400         "pushfl\n"
401         "pushl %0\n"
402         "pushw %w1\n"  // status
403         "pushw %w2\n"  // X
404         "pushw %w3\n"  // Y
405         "pushw $0\n"   // Z
406         "lcallw *8(%%esp)\n"
407         "addl $12, %%esp\n"
408         "popfl\n"
409         "popl %%ebp\n"
410         : "+a" (func), "+b" (status), "+c" (X), "+d" (Y)
411         :
412         : "esi", "edi"
413         );
414 }
415
416 // INT74h : PS/2 mouse hardware interrupt
417 void VISIBLE
418 handle_74(struct bregs *regs)
419 {
420     debug_enter(regs);
421
422     irq_enable();
423     int74_function();
424     irq_disable();
425
426     eoi_both_pics();
427 }