aboutsummaryrefslogtreecommitdiff
path: root/src/mouse.c
blob: 2f997c1f342c4ce5c70980e3a248d62e24a4f1b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// 16bit code to handle mouse events.
//
// Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
// Copyright (C) 2002  MandrakeSoft S.A.
//
// This file may be distributed under the terms of the GNU LGPLv3 license.

#include "biosvar.h" // GET_EBDA
#include "util.h" // debug_isr
#include "pic.h" // eoi_pic2
#include "bregs.h" // struct bregs
#include "ps2port.h" // aux_command

void
mouse_setup()
{
    if (! CONFIG_PS2_MOUSE)
        return;
    dprintf(3, "init mouse\n");
    // pointing device installed
    SETBITS_BDA(equipment_list_flags, 0x04);
    enable_hwirq(12, entry_74);
}

#define RET_SUCCESS      0x00
#define RET_EINVFUNCTION 0x01
#define RET_EINVINPUT    0x02
#define RET_EINTERFACE   0x03
#define RET_ENEEDRESEND  0x04
#define RET_ENOHANDLER   0x05

static int
disable_mouse(u16 ebda_seg)
{
    u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
    ps2ctr |= I8042_CTR_AUXDIS;
    ps2ctr &= ~I8042_CTR_AUXINT;
    SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);

    return aux_command(PSMOUSE_CMD_DISABLE, NULL);
}

// Disable Mouse
static void
mouse_15c20000(struct bregs *regs)
{
    u16 ebda_seg = get_ebda_seg();
    int ret = disable_mouse(ebda_seg);
    if (ret)
        set_code_fail(regs, RET_ENEEDRESEND);
    else
        set_code_success(regs);
}

// Enable Mouse
static void
mouse_15c20001(struct bregs *regs)
{
    u16 ebda_seg = get_ebda_seg();
    u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
    if ((mouse_flags_2 & 0x80) == 0) {
        set_code_fail(regs, RET_ENOHANDLER);
        return;
    }

    u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
    ps2ctr &= ~I8042_CTR_AUXDIS;
    ps2ctr |= I8042_CTR_AUXINT;
    SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);

    int ret = aux_command(PSMOUSE_CMD_ENABLE, NULL);
    if (ret)
        set_code_fail(regs, RET_ENEEDRESEND);
    else
        set_code_success(regs);
}

static void
mouse_15c200XX(struct bregs *regs)
{
    set_code_fail(regs, RET_EINVFUNCTION);
}

// Disable/Enable Mouse
static void
mouse_15c200(struct bregs *regs)
{
    switch (regs->bh) {
    case 0x00: mouse_15c20000(regs); break;
    case 0x01: mouse_15c20001(regs); break;
    default:   mouse_15c200XX(regs); break;
    }
}

// Reset Mouse
static void
mouse_15c201(struct bregs *regs)
{
    u8 param[2];
    int ret = aux_command(PSMOUSE_CMD_RESET_BAT, param);
    if (ret) {
        set_code_fail(regs, RET_ENEEDRESEND);
        return;
    }
    regs->bl = param[0];
    regs->bh = param[1];
    set_code_success(regs);
}

// Set Sample Rate
static void
mouse_15c202(struct bregs *regs)
{
    static u8 VAR16 sample_rates[7] = {10, 20, 40, 60, 80, 100, 200};
    if (regs->bh >= ARRAY_SIZE(sample_rates)) {
        set_code_fail(regs, RET_EINVINPUT);
        return;
    }
    u8 mouse_data1 = GET_GLOBAL(sample_rates[regs->bh]);
    int ret = aux_command(PSMOUSE_CMD_SETRATE, &mouse_data1);
    if (ret)
        set_code_fail(regs, RET_ENEEDRESEND);
    else
        set_code_success(regs);
}

// Set Resolution
static void
mouse_15c203(struct bregs *regs)
{
    // BH:
    //      0 =  25 dpi, 1 count  per millimeter
    //      1 =  50 dpi, 2 counts per millimeter
    //      2 = 100 dpi, 4 counts per millimeter
    //      3 = 200 dpi, 8 counts per millimeter
    if (regs->bh >= 4) {
        set_code_fail(regs, RET_EINVINPUT);
        return;
    }
    u8 param = regs->bh;
    int ret = aux_command(PSMOUSE_CMD_SETRES, &param);
    if (ret)
        set_code_fail(regs, RET_ENEEDRESEND);
    else
        set_code_success(regs);
}

// Get Device ID
static void
mouse_15c204(struct bregs *regs)
{
    u8 param[2];
    int ret = aux_command(PSMOUSE_CMD_GETID, param);
    if (ret) {
        set_code_fail(regs, RET_ENEEDRESEND);
        return;
    }
    regs->bh = param[0];
    set_code_success(regs);
}

// Initialize Mouse
static void
mouse_15c205(struct bregs *regs)
{
    if (regs->bh != 3) {
        set_code_fail(regs, RET_EINTERFACE);
        return;
    }
    u16 ebda_seg = get_ebda_seg();
    SET_EBDA2(ebda_seg, mouse_flag1, 0x00);
    SET_EBDA2(ebda_seg, mouse_flag2, regs->bh);

    // Reset Mouse
    mouse_15c201(regs);
}

// Return Status
static void
mouse_15c20600(struct bregs *regs)
{
    u8 param[3];
    int ret = aux_command(PSMOUSE_CMD_GETINFO, param);
    if (ret) {
        set_code_fail(regs, RET_ENEEDRESEND);
        return;
    }
    regs->bl = param[0];
    regs->cl = param[1];
    regs->dl = param[2];
    set_code_success(regs);
}

// Set Scaling Factor to 1:1
static void
mouse_15c20601(struct bregs *regs)
{
    int ret = aux_command(PSMOUSE_CMD_SETSCALE11, NULL);
    if (ret)
        set_code_fail(regs, RET_ENEEDRESEND);
    else
        set_code_success(regs);
}

// Set Scaling Factor to 2:1
static void
mouse_15c20602(struct bregs *regs)
{
    int ret = aux_command(PSMOUSE_CMD_SETSCALE21, NULL);
    if (ret)
        set_code_fail(regs, RET_ENEEDRESEND);
    else
        set_code_success(regs);
}

static void
mouse_15c206XX(struct bregs *regs)
{
    set_code_fail(regs, RET_EINVFUNCTION);
}

// Return Status & Set Scaling Factor...
static void
mouse_15c206(struct bregs *regs)
{
    switch (regs->bh) {
    case 0x00: mouse_15c20600(regs); break;
    case 0x01: mouse_15c20601(regs); break;
    case 0x02: mouse_15c20602(regs); break;
    default:   mouse_15c206XX(regs); break;
    }
}

// Set Mouse Handler Address
static void
mouse_15c207(struct bregs *regs)
{
    u32 farptr = (regs->es << 16) | regs->bx;
    u16 ebda_seg = get_ebda_seg();
    u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);
    if (! farptr) {
        /* remove handler */
        if ((mouse_flags_2 & 0x80) != 0) {
            mouse_flags_2 &= ~0x80;
            disable_mouse(ebda_seg);
        }
    } else {
        /* install handler */
        mouse_flags_2 |= 0x80;
    }
    SET_EBDA2(ebda_seg, mouse_flag2, mouse_flags_2);
    SET_EBDA2(ebda_seg, far_call_pointer, farptr);
    set_code_success(regs);
}

static void
mouse_15c2XX(struct bregs *regs)
{
    set_code_fail(regs, RET_EINVFUNCTION);
}

void
handle_15c2(struct bregs *regs)
{
    //debug_stub(regs);

    if (! CONFIG_PS2_MOUSE) {
        set_code_fail(regs, RET_EUNSUPPORTED);
        return;
    }

    irq_enable();

    switch (regs->al) {
    case 0x00: mouse_15c200(regs); break;
    case 0x01: mouse_15c201(regs); break;
    case 0x02: mouse_15c202(regs); break;
    case 0x03: mouse_15c203(regs); break;
    case 0x04: mouse_15c204(regs); break;
    case 0x05: mouse_15c205(regs); break;
    case 0x06: mouse_15c206(regs); break;
    case 0x07: mouse_15c207(regs); break;
    default:   mouse_15c2XX(regs); break;
    }
}

static void
process_mouse(u8 data)
{
    u16 ebda_seg = get_ebda_seg();
    u8 mouse_flags_1 = GET_EBDA2(ebda_seg, mouse_flag1);
    u8 mouse_flags_2 = GET_EBDA2(ebda_seg, mouse_flag2);

    if (! (mouse_flags_2 & 0x80))
        // far call handler not installed
        return;

    u8 package_count = mouse_flags_2 & 0x07;
    u8 index = mouse_flags_1 & 0x07;
    SET_EBDA2(ebda_seg, mouse_data[index], data);

    if ((index+1) < package_count) {
        mouse_flags_1++;
        SET_EBDA2(ebda_seg, mouse_flag1, mouse_flags_1);
        return;
    }

    //BX_DEBUG_INT74("int74_function: make_farcall=1\n");
    u16 status = GET_EBDA2(ebda_seg, mouse_data[0]);
    u16 X      = GET_EBDA2(ebda_seg, mouse_data[1]);
    u16 Y      = GET_EBDA2(ebda_seg, mouse_data[2]);
    SET_EBDA2(ebda_seg, mouse_flag1, 0);

    u32 func = GET_EBDA2(ebda_seg, far_call_pointer);

    irq_enable();
    asm volatile(
        "pushl %0\n"
        "pushw %w1\n"  // status
        "pushw %w2\n"  // X
        "pushw %w3\n"  // Y
        "pushw $0\n"   // Z
        "lcallw *8(%%esp)\n"
        "addl $12, %%esp\n"
        "cld\n"
        :
        : "r"(func), "r"(status), "r"(X), "r"(Y)
        : "cc"
        );
    irq_disable();
}

// INT74h : PS/2 mouse hardware interrupt
void VISIBLE16
handle_74()
{
    debug_isr(DEBUG_ISR_74);
    if (! CONFIG_PS2_MOUSE)
        goto done;

    u8 v = inb(PORT_PS2_STATUS);
    if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
        != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
        dprintf(1, "mouse irq but no mouse data.\n");
        goto done;
    }
    v = inb(PORT_PS2_DATA);

    process_mouse(v);

done:
    eoi_pic2();
}