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 LGPLv3 license.
8 #include "biosvar.h" // GET_EBDA
9 #include "util.h" // debug_isr
10 #include "pic.h" // eoi_pic2
11 #include "bregs.h" // struct bregs
12 #include "ps2port.h" // aux_command
17 if (! CONFIG_PS2_MOUSE)
19 dprintf(3, "init mouse\n");
20 // pointing device installed
21 SETBITS_BDA(equipment_list_flags, 0x04);
22 enable_hwirq(12, entry_74);
25 #define RET_SUCCESS 0x00
26 #define RET_EINVFUNCTION 0x01
27 #define RET_EINVINPUT 0x02
28 #define RET_EINTERFACE 0x03
29 #define RET_ENEEDRESEND 0x04
30 #define RET_ENOHANDLER 0x05
33 disable_mouse(u16 ebda_seg)
35 u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
36 ps2ctr |= I8042_CTR_AUXDIS;
37 ps2ctr &= ~I8042_CTR_AUXINT;
38 SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);
40 return aux_command(PSMOUSE_CMD_DISABLE, NULL);
45 mouse_15c20000(struct bregs *regs)
47 u16 ebda_seg = get_ebda_seg();
48 int ret = disable_mouse(ebda_seg);
50 set_code_fail(regs, RET_ENEEDRESEND);
52 set_code_success(regs);
57 mouse_15c20001(struct bregs *regs)
59 u16 ebda_seg = get_ebda_seg();
60 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
61 if ((mouse_flags_2 & 0x80) == 0) {
62 set_code_fail(regs, RET_ENOHANDLER);
66 u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
67 ps2ctr &= ~I8042_CTR_AUXDIS;
68 ps2ctr |= I8042_CTR_AUXINT;
69 SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);
71 int ret = aux_command(PSMOUSE_CMD_ENABLE, NULL);
73 set_code_fail(regs, RET_ENEEDRESEND);
75 set_code_success(regs);
79 mouse_15c200XX(struct bregs *regs)
81 set_code_fail(regs, RET_EINVFUNCTION);
84 // Disable/Enable Mouse
86 mouse_15c200(struct bregs *regs)
89 case 0x00: mouse_15c20000(regs); break;
90 case 0x01: mouse_15c20001(regs); break;
91 default: mouse_15c200XX(regs); break;
97 mouse_15c201(struct bregs *regs)
100 int ret = aux_command(PSMOUSE_CMD_RESET_BAT, param);
101 if (ret != 0 && ret != 2) {
102 set_code_fail(regs, RET_ENEEDRESEND);
107 set_code_success(regs);
112 mouse_15c202(struct bregs *regs)
114 static u8 VAR16 sample_rates[7] = {10, 20, 40, 60, 80, 100, 200};
115 if (regs->bh >= ARRAY_SIZE(sample_rates)) {
116 set_code_fail(regs, RET_EINVINPUT);
119 u8 mouse_data1 = GET_GLOBAL(sample_rates[regs->bh]);
120 int ret = aux_command(PSMOUSE_CMD_SETRATE, &mouse_data1);
122 set_code_fail(regs, RET_ENEEDRESEND);
124 set_code_success(regs);
129 mouse_15c203(struct bregs *regs)
132 // 0 = 25 dpi, 1 count per millimeter
133 // 1 = 50 dpi, 2 counts per millimeter
134 // 2 = 100 dpi, 4 counts per millimeter
135 // 3 = 200 dpi, 8 counts per millimeter
137 set_code_fail(regs, RET_EINVINPUT);
141 int ret = aux_command(PSMOUSE_CMD_SETRES, ¶m);
143 set_code_fail(regs, RET_ENEEDRESEND);
145 set_code_success(regs);
150 mouse_15c204(struct bregs *regs)
153 int ret = aux_command(PSMOUSE_CMD_GETID, param);
155 set_code_fail(regs, RET_ENEEDRESEND);
159 set_code_success(regs);
164 mouse_15c205(struct bregs *regs)
167 set_code_fail(regs, RET_EINTERFACE);
170 u16 ebda_seg = get_ebda_seg();
171 SET_EBDA2(ebda_seg, mouse_flag1, 0x00);
172 SET_EBDA2(ebda_seg, mouse_flag2, regs->bh);
180 mouse_15c20600(struct bregs *regs)
183 int ret = aux_command(PSMOUSE_CMD_GETINFO, param);
185 set_code_fail(regs, RET_ENEEDRESEND);
191 set_code_success(regs);
194 // Set Scaling Factor to 1:1
196 mouse_15c20601(struct bregs *regs)
198 int ret = aux_command(PSMOUSE_CMD_SETSCALE11, NULL);
200 set_code_fail(regs, RET_ENEEDRESEND);
202 set_code_success(regs);
205 // Set Scaling Factor to 2:1
207 mouse_15c20602(struct bregs *regs)
209 int ret = aux_command(PSMOUSE_CMD_SETSCALE21, NULL);
211 set_code_fail(regs, RET_ENEEDRESEND);
213 set_code_success(regs);
217 mouse_15c206XX(struct bregs *regs)
219 set_code_fail(regs, RET_EINVFUNCTION);
222 // Return Status & Set Scaling Factor...
224 mouse_15c206(struct bregs *regs)
227 case 0x00: mouse_15c20600(regs); break;
228 case 0x01: mouse_15c20601(regs); break;
229 case 0x02: mouse_15c20602(regs); break;
230 default: mouse_15c206XX(regs); break;
234 // Set Mouse Handler Address
236 mouse_15c207(struct bregs *regs)
238 u32 farptr = (regs->es << 16) | regs->bx;
239 u16 ebda_seg = get_ebda_seg();
240 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
243 if ((mouse_flags_2 & 0x80) != 0) {
244 mouse_flags_2 &= ~0x80;
245 disable_mouse(ebda_seg);
248 /* install handler */
249 mouse_flags_2 |= 0x80;
251 SET_EBDA2(ebda_seg, mouse_flag2, mouse_flags_2);
252 SET_EBDA2(ebda_seg, far_call_pointer, farptr);
253 set_code_success(regs);
257 mouse_15c2XX(struct bregs *regs)
259 set_code_fail(regs, RET_EINVFUNCTION);
263 handle_15c2(struct bregs *regs)
267 if (! CONFIG_PS2_MOUSE) {
268 set_code_fail(regs, RET_EUNSUPPORTED);
275 case 0x00: mouse_15c200(regs); break;
276 case 0x01: mouse_15c201(regs); break;
277 case 0x02: mouse_15c202(regs); break;
278 case 0x03: mouse_15c203(regs); break;
279 case 0x04: mouse_15c204(regs); break;
280 case 0x05: mouse_15c205(regs); break;
281 case 0x06: mouse_15c206(regs); break;
282 case 0x07: mouse_15c207(regs); break;
283 default: mouse_15c2XX(regs); break;
288 process_mouse(u8 data)
290 u16 ebda_seg = get_ebda_seg();
291 u8 mouse_flags_1 = GET_EBDA2(ebda_seg, mouse_flag1);
292 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
294 if (! (mouse_flags_2 & 0x80))
295 // far call handler not installed
298 u8 package_count = mouse_flags_2 & 0x07;
299 u8 index = mouse_flags_1 & 0x07;
300 SET_EBDA2(ebda_seg, mouse_data[index], data);
302 if ((index+1) < package_count) {
304 SET_EBDA2(ebda_seg, mouse_flag1, mouse_flags_1);
308 //BX_DEBUG_INT74("int74_function: make_farcall=1\n");
309 u16 status = GET_EBDA2(ebda_seg, mouse_data[0]);
310 u16 X = GET_EBDA2(ebda_seg, mouse_data[1]);
311 u16 Y = GET_EBDA2(ebda_seg, mouse_data[2]);
312 SET_EBDA2(ebda_seg, mouse_flag1, 0);
314 u32 func = GET_EBDA2(ebda_seg, far_call_pointer);
319 "pushw %w1\n" // status
327 : "r"(func), "r"(status), "r"(X), "r"(Y)
333 // INT74h : PS/2 mouse hardware interrupt
337 debug_isr(DEBUG_ISR_74);
338 if (! CONFIG_PS2_MOUSE)
341 u8 v = inb(PORT_PS2_STATUS);
342 if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
343 != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
344 dprintf(1, "mouse irq but no mouse data.\n");
347 v = inb(PORT_PS2_DATA);