aboutsummaryrefslogtreecommitdiff
path: root/int15ps2.h
blob: 438dfd8576e0136fb124402472fa332f0ba0245a (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
/*
 * VBMouse - Routines to access the PS2 BIOS services
 * Copyright (C) 2022 Javier S. Pedro
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef INT15PS2_H
#define INT15PS2_H

#include <stdbool.h>
#include <stdint.h>

/** Standard PS/2 mouse IRQ. At least on VirtualBox. */
#define PS2_MOUSE_IRQ        12
/** The corresponding interrupt vector for IRQ 12. */
#define PS2_MOUSE_INT_VECTOR 0x74

typedef uint8_t ps2m_err;
enum ps2m_errors {
	PS2M_ERR_INVALID_FUNCTION = 1,
	PS2M_ERR_INVALID_INPUT    = 2,
	PS2M_ERR_INTERFACE_ERROR  = 3,
	PS2M_ERR_RESEND           = 4,
	PS2M_ERR_NO_CALLBACK      = 5,
};

enum ps2m_status {
	PS2M_STATUS_BUTTON_1 = 1 << 0,
	PS2M_STATUS_BUTTON_2 = 1 << 1,
	PS2M_STATUS_BUTTON_3 = 1 << 2,
	PS2M_STATUS_X_NEG    = 1 << 4,
	PS2M_STATUS_Y_NEG    = 1 << 5,
	PS2M_STATUS_X_OVF    = 1 << 6,
	PS2M_STATUS_Y_OVF    = 1 << 7,
};

enum ps2m_packet_size {
	PS2M_PACKET_SIZE_STREAMING = 1,
	PS2M_PACKET_SIZE_PLAIN     = 3,
	PS2M_PACKET_SIZE_EXT       = 4,
};

enum ps2m_device_ids {
	/** Standard PS/2 mouse, 2 buttons. */
	PS2M_DEVICE_ID_PLAIN = 0,
	/** IntelliMouse PS/2, with wheel. */
	PS2M_DEVICE_ID_IMPS2 = 3,
	/** IntelliMouse Explorer, wheel and 5 buttons. */
	PS2M_DEVICE_ID_IMEX = 4,
	/** IntelliMouse Explorer, wheel, 5 buttons, and horizontal scrolling. */
	PS2M_DEVICE_ID_IMEX_HORZ = 5
};

/** Valid PS/2 mouse resolutions in DPI. */
enum ps2m_resolution {
	PS2M_RESOLUTION_25  = 0,
	PS2M_RESOLUTION_50  = 1,
	PS2M_RESOLUTION_100 = 2,
	PS2M_RESOLUTION_200 = 3
};

/** Valid PS/2 mouse sampling rates in Hz. */
enum ps2m_sample_rate {
	PS2M_SAMPLE_RATE_10  = 0,
	PS2M_SAMPLE_RATE_20  = 1,
	PS2M_SAMPLE_RATE_40  = 2,
	PS2M_SAMPLE_RATE_60  = 3,
	PS2M_SAMPLE_RATE_80  = 4,
	PS2M_SAMPLE_RATE_100 = 5,
	PS2M_SAMPLE_RATE_200 = 6
};

/** Invoked by the BIOS when there is a mouse event. */
typedef void (__far * LPFN_PS2CALLBACK)();

static ps2m_err ps2m_init(uint8_t packet_size);
#pragma aux ps2m_init = \
	"stc"               /* If nothing happens, assume failure */ \
	"mov ax, 0xC205"    /* Pointing device: initialization (packet size in bh) */ \
	"int 0x15" \
	"jnc end"           /* Success */ \
	"fail: test ah, ah" /* Ensure we have some error code back, set ah to -1 if we don't */ \
	"jnz end" \
	"dec ah" \
	"end:" \
	__parm [bh] \
	__value [ah] \
	__modify [ax bx]

static ps2m_err ps2m_reset(void);
#pragma aux ps2m_reset = \
	"stc" \
	"mov ax, 0xC201"    /* Pointing device: reset */ \
	"int 0x15" \
	"jnc end" \
	"fail: test ah, ah" \
	"jnz end" \
	"dec ah" \
	"end:" \
	__value [ah] \
	__modify [ax bx]

static ps2m_err ps2m_get_device_id(uint8_t __far *device_id);
#pragma aux ps2m_get_device_id = \
	"stc" \
	"mov ax, 0xC204"    /* Pointing device: get device ID */ \
	"int 0x15" \
	"mov es:[di], bh"   /* Save returned value */ \
	"jnc end" \
	"fail: test ah, ah" \
	"jnz end" \
	"dec ah" \
	"end:" \
	__parm [es di] \
	__value [ah] \
	__modify [ax bx]

static ps2m_err ps2m_set_resolution(uint8_t resolution);
#pragma aux ps2m_set_resolution = \
	"stc" \
	"mov ax, 0xC203"    /* Pointing device: set resolution (in bh) */ \
	"int 0x15" \
	"jnc end" \
	"fail: test ah, ah" \
	"jnz end" \
	"dec ah" \
	"end:" \
	__parm [bh] \
	__value [ah] \
	__modify [ax]

static ps2m_err ps2m_set_sample_rate(uint8_t sample_rate);
#pragma aux ps2m_set_sample_rate = \
	"stc" \
	"mov ax, 0xC202"    /* Pointing device: set sample rate (in bh) */ \
	"int 0x15" \
	"jnc end" \
	"fail: test ah, ah" \
	"jnz end" \
	"dec ah" \
	"end:" \
	__parm [bh] \
	__value [ah] \
	__modify [ax]

static ps2m_err ps2m_set_scaling_factor(uint8_t scaling_factor);
#pragma aux ps2m_set_scaling_factor = \
	"stc" \
	"mov ax, 0xC206"    /* Pointing device: extended commands (with bh > 0, set scaling factor) */ \
	"int 0x15" \
	"jnc end" \
	"fail: test ah, ah" \
	"jnz end" \
	"dec ah" \
	"end:" \
	__parm [bh] \
	__value [ah] \
	__modify [ax]

static ps2m_err ps2m_set_callback(LPFN_PS2CALLBACK callback);
#pragma aux ps2m_set_callback = \
	"stc" \
	"mov ax, 0xC207"   /* Pointing device: set interrupt callback (in es:bx) */ \
	"int 0x15" \
	"jnc end" \
	"fail: test ah, ah" \
	"jnz end" \
	"dec ah" \
	"end:" \
	__parm [es bx] \
	__value [ah] \
	__modify [ax]

static ps2m_err ps2m_enable(bool enable);
#pragma aux ps2m_enable = \
	"test bh, bh"      /* Ensure enable is either 1 or 0 */ \
	"setnz bh" \
	"stc" \
	"mov ax, 0xC200"   /* Pointing device enable/disable (in bh) */ \
	"int 0x15" \
	"jnc end" \
	"fail: test ah, ah" \
	"jnz end" \
	"dec ah" \
	"end:" \
	__parm [bh] \
	__value [ah] \
	__modify [ax]

/** Sends the magic sequence to switch the mouse to the IMPS2 protocol. */
static void ps2m_send_imps2_sequence(void)
{
	ps2m_set_sample_rate(PS2M_SAMPLE_RATE_200);
	ps2m_set_sample_rate(PS2M_SAMPLE_RATE_100);
	ps2m_set_sample_rate(PS2M_SAMPLE_RATE_80);
}

/** Detects whether we have a IMPS2 mouse with wheel support. */
static bool ps2m_detect_wheel(void)
{
	int err;
	uint8_t device_id;

	// Get the initial mouse device id
	err = ps2m_get_device_id(&device_id);
	if (err) {
		return false;
	}

	if (device_id == PS2M_DEVICE_ID_IMPS2) {
		// Already wheel
		return true;
	}

	if (device_id != PS2M_DEVICE_ID_PLAIN) {
		// TODO: Likely we have to accept more device IDs here
		dlog_print("Unknown initial mouse device_id=");
		dlog_printx(device_id);
		dlog_endline();
		return false;
	}

	// Send the knock sequence to activate the extended packet
	ps2m_send_imps2_sequence();

	// Now check if the device id has changed
	err = ps2m_get_device_id(&device_id);

	return err == 0 && device_id == PS2M_DEVICE_ID_IMPS2;
}

#endif /* INT15PS2_H */