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" // ps2_mouse_command
13 #include "usb-hid.h" // usb_mouse_command
20 dprintf(3, "init mouse\n");
21 // pointing device installed
22 SETBITS_BDA(equipment_list_flags, 0x04);
26 mouse_command(int command, u8 *param)
28 if (usb_mouse_active())
29 return usb_mouse_command(command, param);
30 return ps2_mouse_command(command, param);
33 #define RET_SUCCESS 0x00
34 #define RET_EINVFUNCTION 0x01
35 #define RET_EINVINPUT 0x02
36 #define RET_EINTERFACE 0x03
37 #define RET_ENEEDRESEND 0x04
38 #define RET_ENOHANDLER 0x05
42 mouse_15c20000(struct bregs *regs)
44 int ret = mouse_command(PSMOUSE_CMD_DISABLE, NULL);
46 set_code_invalid(regs, RET_ENEEDRESEND);
48 set_code_success(regs);
53 mouse_15c20001(struct bregs *regs)
55 u8 mouse_flags_2 = GET_EBDA(mouse_flag2);
56 if ((mouse_flags_2 & 0x80) == 0) {
57 set_code_invalid(regs, RET_ENOHANDLER);
61 int ret = mouse_command(PSMOUSE_CMD_ENABLE, NULL);
63 set_code_invalid(regs, RET_ENEEDRESEND);
65 set_code_success(regs);
69 mouse_15c200XX(struct bregs *regs)
71 set_code_unimplemented(regs, RET_EINVFUNCTION);
74 // Disable/Enable Mouse
76 mouse_15c200(struct bregs *regs)
79 case 0x00: mouse_15c20000(regs); break;
80 case 0x01: mouse_15c20001(regs); break;
81 default: mouse_15c200XX(regs); break;
87 mouse_15c201(struct bregs *regs)
90 int ret = mouse_command(PSMOUSE_CMD_RESET_BAT, param);
92 set_code_invalid(regs, RET_ENEEDRESEND);
97 set_code_success(regs);
102 mouse_15c202(struct bregs *regs)
104 static u8 VAR16 sample_rates[7] = {10, 20, 40, 60, 80, 100, 200};
105 if (regs->bh >= ARRAY_SIZE(sample_rates)) {
106 set_code_invalid(regs, RET_EINVINPUT);
109 u8 mouse_data1 = GET_GLOBAL(sample_rates[regs->bh]);
110 int ret = mouse_command(PSMOUSE_CMD_SETRATE, &mouse_data1);
112 set_code_invalid(regs, RET_ENEEDRESEND);
114 set_code_success(regs);
119 mouse_15c203(struct bregs *regs)
122 // 0 = 25 dpi, 1 count per millimeter
123 // 1 = 50 dpi, 2 counts per millimeter
124 // 2 = 100 dpi, 4 counts per millimeter
125 // 3 = 200 dpi, 8 counts per millimeter
127 set_code_invalid(regs, RET_EINVINPUT);
131 int ret = mouse_command(PSMOUSE_CMD_SETRES, ¶m);
133 set_code_invalid(regs, RET_ENEEDRESEND);
135 set_code_success(regs);
140 mouse_15c204(struct bregs *regs)
143 int ret = mouse_command(PSMOUSE_CMD_GETID, param);
145 set_code_invalid(regs, RET_ENEEDRESEND);
149 set_code_success(regs);
154 mouse_15c205(struct bregs *regs)
157 set_code_invalid(regs, RET_EINTERFACE);
160 u16 ebda_seg = get_ebda_seg();
161 SET_EBDA2(ebda_seg, mouse_flag1, 0x00);
162 SET_EBDA2(ebda_seg, mouse_flag2, regs->bh);
170 mouse_15c20600(struct bregs *regs)
173 int ret = mouse_command(PSMOUSE_CMD_GETINFO, param);
175 set_code_invalid(regs, RET_ENEEDRESEND);
181 set_code_success(regs);
184 // Set Scaling Factor to 1:1
186 mouse_15c20601(struct bregs *regs)
188 int ret = mouse_command(PSMOUSE_CMD_SETSCALE11, NULL);
190 set_code_invalid(regs, RET_ENEEDRESEND);
192 set_code_success(regs);
195 // Set Scaling Factor to 2:1
197 mouse_15c20602(struct bregs *regs)
199 int ret = mouse_command(PSMOUSE_CMD_SETSCALE21, NULL);
201 set_code_invalid(regs, RET_ENEEDRESEND);
203 set_code_success(regs);
207 mouse_15c206XX(struct bregs *regs)
209 set_code_unimplemented(regs, RET_EINVFUNCTION);
212 // Return Status & Set Scaling Factor...
214 mouse_15c206(struct bregs *regs)
217 case 0x00: mouse_15c20600(regs); break;
218 case 0x01: mouse_15c20601(regs); break;
219 case 0x02: mouse_15c20602(regs); break;
220 default: mouse_15c206XX(regs); break;
224 // Set Mouse Handler Address
226 mouse_15c207(struct bregs *regs)
228 struct segoff_s farptr = SEGOFF(regs->es, regs->bx);
229 u16 ebda_seg = get_ebda_seg();
230 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
231 if (! farptr.segoff) {
233 if ((mouse_flags_2 & 0x80) != 0) {
234 mouse_flags_2 &= ~0x80;
235 mouse_command(PSMOUSE_CMD_DISABLE, NULL);
238 /* install handler */
239 mouse_flags_2 |= 0x80;
241 SET_EBDA2(ebda_seg, mouse_flag2, mouse_flags_2);
242 SET_EBDA2(ebda_seg, far_call_pointer, farptr);
243 set_code_success(regs);
247 mouse_15c2XX(struct bregs *regs)
249 set_code_unimplemented(regs, RET_EINVFUNCTION);
253 handle_15c2(struct bregs *regs)
257 if (! CONFIG_MOUSE) {
258 set_code_invalid(regs, RET_EUNSUPPORTED);
263 case 0x00: mouse_15c200(regs); break;
264 case 0x01: mouse_15c201(regs); break;
265 case 0x02: mouse_15c202(regs); break;
266 case 0x03: mouse_15c203(regs); break;
267 case 0x04: mouse_15c204(regs); break;
268 case 0x05: mouse_15c205(regs); break;
269 case 0x06: mouse_15c206(regs); break;
270 case 0x07: mouse_15c207(regs); break;
271 default: mouse_15c2XX(regs); break;
276 process_mouse(u8 data)
281 u16 ebda_seg = get_ebda_seg();
282 u8 mouse_flags_1 = GET_EBDA2(ebda_seg, mouse_flag1);
283 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
285 if (! (mouse_flags_2 & 0x80))
286 // far call handler not installed
289 u8 package_count = mouse_flags_2 & 0x07;
290 u8 index = mouse_flags_1 & 0x07;
291 SET_EBDA2(ebda_seg, mouse_data[index], data);
293 if ((index+1) < package_count) {
295 SET_EBDA2(ebda_seg, mouse_flag1, mouse_flags_1);
299 u16 status = GET_EBDA2(ebda_seg, mouse_data[0]);
300 u16 X = GET_EBDA2(ebda_seg, mouse_data[1]);
301 u16 Y = GET_EBDA2(ebda_seg, mouse_data[2]);
302 SET_EBDA2(ebda_seg, mouse_flag1, 0);
304 struct segoff_s func = GET_EBDA2(ebda_seg, far_call_pointer);
305 dprintf(16, "mouse farcall s=%04x x=%04x y=%04x func=%04x:%04x\n"
306 , status, X, Y, func.seg, func.offset);
313 "pushw %w1\n" // status
323 : "+a"(func.segoff), "+c"(status), "+d"(X), "+b"(Y)
325 : "edi", "esi", "cc", "memory");