blob: 65baa20dda212412c7d35005f938dcb05e007e85 [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
Kevin O'Connor9521e262008-07-04 13:04:29 -04008#include "biosvar.h" // GET_EBDA
Kevin O'Connor15c1f222008-06-12 22:59:43 -04009#include "util.h" // debug_isr
Kevin O'Connord21c0892008-11-26 17:02:43 -050010#include "pic.h" // eoi_pic2
Kevin O'Connor9521e262008-07-04 13:04:29 -040011#include "bregs.h" // struct bregs
Kevin O'Connor3b897192008-07-20 10:08:59 -040012#include "ps2port.h" // aux_command
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050013
Kevin O'Connorf54c1502008-06-14 15:56:16 -040014void
15mouse_setup()
16{
17 if (! CONFIG_PS2_MOUSE)
18 return;
19 dprintf(3, "init mouse\n");
20 // pointing device installed
21 SETBITS_BDA(equipment_list_flags, 0x04);
Kevin O'Connord21c0892008-11-26 17:02:43 -050022 enable_hwirq(12, entry_74);
Kevin O'Connorf54c1502008-06-14 15:56:16 -040023}
24
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050025#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
31
Kevin O'Connor3b897192008-07-20 10:08:59 -040032static int
Kevin O'Connor08815372008-12-29 21:16:31 -050033disable_mouse(u16 ebda_seg)
Kevin O'Connor3b897192008-07-20 10:08:59 -040034{
Kevin O'Connor08815372008-12-29 21:16:31 -050035 u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
Kevin O'Connor3b897192008-07-20 10:08:59 -040036 ps2ctr |= I8042_CTR_AUXDIS;
37 ps2ctr &= ~I8042_CTR_AUXINT;
Kevin O'Connor08815372008-12-29 21:16:31 -050038 SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);
Kevin O'Connor3b897192008-07-20 10:08:59 -040039
40 return aux_command(PSMOUSE_CMD_DISABLE, NULL);
41}
42
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050043// Disable Mouse
44static void
45mouse_15c20000(struct bregs *regs)
46{
Kevin O'Connor08815372008-12-29 21:16:31 -050047 u16 ebda_seg = get_ebda_seg();
48 int ret = disable_mouse(ebda_seg);
Kevin O'Connor3b897192008-07-20 10:08:59 -040049 if (ret)
50 set_code_fail(regs, RET_ENEEDRESEND);
51 else
52 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050053}
54
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050055// Enable Mouse
56static void
57mouse_15c20001(struct bregs *regs)
58{
Kevin O'Connor08815372008-12-29 21:16:31 -050059 u16 ebda_seg = get_ebda_seg();
60 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050061 if ((mouse_flags_2 & 0x80) == 0) {
Kevin O'Connor6c781222008-03-09 12:19:23 -040062 set_code_fail(regs, RET_ENOHANDLER);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050063 return;
64 }
Kevin O'Connor3b897192008-07-20 10:08:59 -040065
Kevin O'Connor08815372008-12-29 21:16:31 -050066 u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
Kevin O'Connor3b897192008-07-20 10:08:59 -040067 ps2ctr &= ~I8042_CTR_AUXDIS;
68 ps2ctr |= I8042_CTR_AUXINT;
Kevin O'Connor08815372008-12-29 21:16:31 -050069 SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);
Kevin O'Connor3b897192008-07-20 10:08:59 -040070
71 int ret = aux_command(PSMOUSE_CMD_ENABLE, NULL);
72 if (ret)
73 set_code_fail(regs, RET_ENEEDRESEND);
74 else
Kevin O'Connor6c781222008-03-09 12:19:23 -040075 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050076}
77
78static void
79mouse_15c200XX(struct bregs *regs)
80{
Kevin O'Connor6c781222008-03-09 12:19:23 -040081 set_code_fail(regs, RET_EINVFUNCTION);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -050082}
83
84// Disable/Enable Mouse
85static void
86mouse_15c200(struct bregs *regs)
87{
88 switch (regs->bh) {
89 case 0x00: mouse_15c20000(regs); break;
90 case 0x01: mouse_15c20001(regs); break;
91 default: mouse_15c200XX(regs); break;
92 }
93}
94
95// Reset Mouse
96static void
97mouse_15c201(struct bregs *regs)
98{
Kevin O'Connor3b897192008-07-20 10:08:59 -040099 u8 param[2];
100 int ret = aux_command(PSMOUSE_CMD_RESET_BAT, param);
Kevin O'Connorf13b0082008-08-17 11:26:42 -0400101 if (ret != 0 && ret != 2) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400102 set_code_fail(regs, RET_ENEEDRESEND);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500103 return;
104 }
Kevin O'Connor3b897192008-07-20 10:08:59 -0400105 regs->bl = param[0];
106 regs->bh = param[1];
Kevin O'Connor6c781222008-03-09 12:19:23 -0400107 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500108}
109
110// Set Sample Rate
111static void
112mouse_15c202(struct bregs *regs)
113{
Kevin O'Connor3b897192008-07-20 10:08:59 -0400114 static u8 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);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500117 return;
118 }
Kevin O'Connor15157a32008-12-13 11:10:37 -0500119 u8 mouse_data1 = GET_GLOBAL(sample_rates[regs->bh]);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400120 int ret = aux_command(PSMOUSE_CMD_SETRATE, &mouse_data1);
121 if (ret)
122 set_code_fail(regs, RET_ENEEDRESEND);
123 else
124 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500125}
126
127// Set Resolution
128static void
129mouse_15c203(struct bregs *regs)
130{
131 // BH:
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
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500136 if (regs->bh >= 4) {
Kevin O'Connor3b897192008-07-20 10:08:59 -0400137 set_code_fail(regs, RET_EINVINPUT);
138 return;
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500139 }
Kevin O'Connor3b897192008-07-20 10:08:59 -0400140 u8 param = regs->bh;
141 int ret = aux_command(PSMOUSE_CMD_SETRES, &param);
142 if (ret)
143 set_code_fail(regs, RET_ENEEDRESEND);
144 else
145 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500146}
147
148// Get Device ID
149static void
150mouse_15c204(struct bregs *regs)
151{
Kevin O'Connor3b897192008-07-20 10:08:59 -0400152 u8 param[2];
153 int ret = aux_command(PSMOUSE_CMD_GETID, param);
154 if (ret) {
155 set_code_fail(regs, RET_ENEEDRESEND);
156 return;
157 }
158 regs->bh = param[0];
Kevin O'Connor6c781222008-03-09 12:19:23 -0400159 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500160}
161
162// Initialize Mouse
163static void
164mouse_15c205(struct bregs *regs)
165{
166 if (regs->bh != 3) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400167 set_code_fail(regs, RET_EINTERFACE);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500168 return;
169 }
Kevin O'Connor08815372008-12-29 21:16:31 -0500170 u16 ebda_seg = get_ebda_seg();
171 SET_EBDA2(ebda_seg, mouse_flag1, 0x00);
172 SET_EBDA2(ebda_seg, mouse_flag2, regs->bh);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500173
174 // Reset Mouse
175 mouse_15c201(regs);
176}
177
178// Return Status
179static void
180mouse_15c20600(struct bregs *regs)
181{
Kevin O'Connor3b897192008-07-20 10:08:59 -0400182 u8 param[3];
183 int ret = aux_command(PSMOUSE_CMD_GETINFO, param);
184 if (ret) {
185 set_code_fail(regs, RET_ENEEDRESEND);
186 return;
187 }
188 regs->bl = param[0];
189 regs->cl = param[1];
190 regs->dl = param[2];
Kevin O'Connor6c781222008-03-09 12:19:23 -0400191 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500192}
193
194// Set Scaling Factor to 1:1
195static void
196mouse_15c20601(struct bregs *regs)
197{
Kevin O'Connor3b897192008-07-20 10:08:59 -0400198 int ret = aux_command(PSMOUSE_CMD_SETSCALE11, NULL);
199 if (ret)
200 set_code_fail(regs, RET_ENEEDRESEND);
201 else
202 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500203}
204
205// Set Scaling Factor to 2:1
206static void
207mouse_15c20602(struct bregs *regs)
208{
Kevin O'Connor3b897192008-07-20 10:08:59 -0400209 int ret = aux_command(PSMOUSE_CMD_SETSCALE21, NULL);
210 if (ret)
211 set_code_fail(regs, RET_ENEEDRESEND);
212 else
213 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500214}
215
216static void
217mouse_15c206XX(struct bregs *regs)
218{
Kevin O'Connor3b897192008-07-20 10:08:59 -0400219 set_code_fail(regs, RET_EINVFUNCTION);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500220}
221
222// Return Status & Set Scaling Factor...
223static void
224mouse_15c206(struct bregs *regs)
225{
226 switch (regs->bh) {
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;
231 }
232}
233
234// Set Mouse Handler Address
235static void
236mouse_15c207(struct bregs *regs)
237{
238 u32 farptr = (regs->es << 16) | regs->bx;
Kevin O'Connor08815372008-12-29 21:16:31 -0500239 u16 ebda_seg = get_ebda_seg();
240 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500241 if (! farptr) {
242 /* remove handler */
243 if ((mouse_flags_2 & 0x80) != 0) {
244 mouse_flags_2 &= ~0x80;
Kevin O'Connor08815372008-12-29 21:16:31 -0500245 disable_mouse(ebda_seg);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500246 }
247 } else {
248 /* install handler */
249 mouse_flags_2 |= 0x80;
250 }
Kevin O'Connor08815372008-12-29 21:16:31 -0500251 SET_EBDA2(ebda_seg, mouse_flag2, mouse_flags_2);
252 SET_EBDA2(ebda_seg, far_call_pointer, farptr);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400253 set_code_success(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500254}
255
256static void
257mouse_15c2XX(struct bregs *regs)
258{
Kevin O'Connor6c781222008-03-09 12:19:23 -0400259 set_code_fail(regs, RET_EINVFUNCTION);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500260}
261
262void
263handle_15c2(struct bregs *regs)
264{
Kevin O'Connorc65a3802008-03-02 13:58:23 -0500265 //debug_stub(regs);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500266
267 if (! CONFIG_PS2_MOUSE) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400268 set_code_fail(regs, RET_EUNSUPPORTED);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500269 return;
270 }
271
Kevin O'Connor3b897192008-07-20 10:08:59 -0400272 irq_enable();
273
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500274 switch (regs->al) {
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;
284 }
285}
286
287static void
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500288int74_function()
289{
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500290 u8 v = inb(PORT_PS2_STATUS);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400291 if ((v & 0x21) != 0x21) {
292 dprintf(1, "int74 but no mouse data.\n");
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500293 return;
Kevin O'Connor3b897192008-07-20 10:08:59 -0400294 }
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500295 v = inb(PORT_PS2_DATA);
296
Kevin O'Connor08815372008-12-29 21:16:31 -0500297 u16 ebda_seg = get_ebda_seg();
298 u8 mouse_flags_1 = GET_EBDA2(ebda_seg, mouse_flag1);
299 u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500300
Kevin O'Connor65e63422008-07-19 14:12:32 -0400301 if (! (mouse_flags_2 & 0x80))
302 // far call handler not installed
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500303 return;
304
305 u8 package_count = mouse_flags_2 & 0x07;
306 u8 index = mouse_flags_1 & 0x07;
Kevin O'Connor08815372008-12-29 21:16:31 -0500307 SET_EBDA2(ebda_seg, mouse_data[index], v);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500308
309 if ((index+1) < package_count) {
310 mouse_flags_1++;
Kevin O'Connor08815372008-12-29 21:16:31 -0500311 SET_EBDA2(ebda_seg, mouse_flag1, mouse_flags_1);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500312 return;
313 }
314
315 //BX_DEBUG_INT74("int74_function: make_farcall=1\n");
Kevin O'Connor08815372008-12-29 21:16:31 -0500316 u16 status = GET_EBDA2(ebda_seg, mouse_data[0]);
317 u16 X = GET_EBDA2(ebda_seg, mouse_data[1]);
318 u16 Y = GET_EBDA2(ebda_seg, mouse_data[2]);
319 SET_EBDA2(ebda_seg, mouse_flag1, 0);
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500320
Kevin O'Connor08815372008-12-29 21:16:31 -0500321 u32 func = GET_EBDA2(ebda_seg, far_call_pointer);
Kevin O'Connora83ff552009-01-01 21:00:59 -0500322
323 irq_enable();
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500324 asm volatile(
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500325 "pushl %0\n"
326 "pushw %w1\n" // status
327 "pushw %w2\n" // X
328 "pushw %w3\n" // Y
329 "pushw $0\n" // Z
330 "lcallw *8(%%esp)\n"
331 "addl $12, %%esp\n"
Kevin O'Connor7a558e42008-03-11 20:38:33 -0400332 "cld\n"
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500333 :
Kevin O'Connora83ff552009-01-01 21:00:59 -0500334 : "r"(func), "r"(status), "r"(X), "r"(Y)
335 : "cc"
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500336 );
Kevin O'Connora83ff552009-01-01 21:00:59 -0500337 irq_disable();
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500338}
339
340// INT74h : PS/2 mouse hardware interrupt
Kevin O'Connor19786762008-03-05 21:09:59 -0500341void VISIBLE16
Kevin O'Connored128492008-03-11 11:14:59 -0400342handle_74()
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500343{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400344 debug_isr(DEBUG_ISR_74);
Kevin O'Connor40967022008-07-21 22:23:05 -0400345 if (! CONFIG_PS2_MOUSE)
346 goto done;
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500347
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500348 int74_function();
Kevin O'Connor4d6dbc62008-03-02 08:43:44 -0500349
Kevin O'Connor40967022008-07-21 22:23:05 -0400350done:
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400351 eoi_pic2();
Kevin O'Connor4b60c002008-02-25 22:29:55 -0500352}