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
354
355
356
357
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Memory.h>
#include <AK/Singleton.h>
#include <Kernel/Devices/PS2MouseDevice.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/IO.h>
namespace Kernel {
#define IRQ_MOUSE 12
#define I8042_BUFFER 0x60
#define I8042_STATUS 0x64
#define I8042_ACK 0xFA
#define I8042_BUFFER_FULL 0x01
#define I8042_WHICH_BUFFER 0x20
#define I8042_MOUSE_BUFFER 0x20
#define I8042_KEYBOARD_BUFFER 0x00
#define PS2MOUSE_SET_RESOLUTION 0xE8
#define PS2MOUSE_STATUS_REQUEST 0xE9
#define PS2MOUSE_REQUEST_SINGLE_PACKET 0xEB
#define PS2MOUSE_GET_DEVICE_ID 0xF2
#define PS2MOUSE_SET_SAMPLE_RATE 0xF3
#define PS2MOUSE_ENABLE_PACKET_STREAMING 0xF4
#define PS2MOUSE_DISABLE_PACKET_STREAMING 0xF5
#define PS2MOUSE_SET_DEFAULTS 0xF6
#define PS2MOUSE_RESEND 0xFE
#define PS2MOUSE_RESET 0xFF
#define PS2MOUSE_INTELLIMOUSE_ID 0x03
#define PS2MOUSE_INTELLIMOUSE_EXPLORER_ID 0x04
//#define PS2MOUSE_DEBUG
static auto s_the = AK::make_singleton<PS2MouseDevice>();
PS2MouseDevice::PS2MouseDevice()
: IRQHandler(IRQ_MOUSE)
, CharacterDevice(10, 1)
{
initialize();
}
PS2MouseDevice::~PS2MouseDevice()
{
}
void PS2MouseDevice::create()
{
s_the.ensure_instance();
}
PS2MouseDevice& PS2MouseDevice::the()
{
return *s_the;
}
void PS2MouseDevice::handle_irq(const RegisterState&)
{
if (auto* backdoor = VMWareBackdoor::the()) {
if (backdoor->vmmouse_is_absolute()) {
IO::in8(I8042_BUFFER);
auto packet = backdoor->receive_mouse_packet();
m_entropy_source.add_random_event(packet);
if (packet.has_value())
m_queue.enqueue(packet.value());
return;
}
}
for (;;) {
u8 status = IO::in8(I8042_STATUS);
if (!(((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) && (status & I8042_BUFFER_FULL)))
return;
u8 data = IO::in8(I8042_BUFFER);
m_data[m_data_state] = data;
auto commit_packet = [&] {
m_data_state = 0;
#ifdef PS2MOUSE_DEBUG
dbg() << "PS2Mouse: " << m_data[1] << ", " << m_data[2] << " " << ((m_data[0] & 1) ? "Left" : "") << " " << ((m_data[0] & 2) ? "Right" : "") << " (buffered: " << m_queue.size() << ")";
#endif
m_entropy_source.add_random_event(*(u32*)m_data);
parse_data_packet();
};
switch (m_data_state) {
case 0:
if (!(data & 0x08)) {
dbg() << "PS2Mouse: Stream out of sync.";
break;
}
++m_data_state;
break;
case 1:
++m_data_state;
break;
case 2:
if (m_has_wheel) {
++m_data_state;
break;
}
commit_packet();
break;
case 3:
ASSERT(m_has_wheel);
commit_packet();
break;
}
}
}
void PS2MouseDevice::parse_data_packet()
{
int x = m_data[1];
int y = m_data[2];
int z = 0;
if (m_has_wheel)
z = (char)m_data[3];
bool x_overflow = m_data[0] & 0x40;
bool y_overflow = m_data[0] & 0x80;
bool x_sign = m_data[0] & 0x10;
bool y_sign = m_data[0] & 0x20;
if (x && x_sign)
x -= 0x100;
if (y && y_sign)
y -= 0x100;
if (x_overflow || y_overflow) {
x = 0;
y = 0;
}
MousePacket packet;
packet.x = x;
packet.y = y;
packet.z = z;
packet.buttons = m_data[0] & 0x07;
if (m_has_five_buttons) {
if (m_data[3] & 0x10)
packet.buttons |= MousePacket::BackButton;
if (m_data[3] & 0x20)
packet.buttons |= MousePacket::ForwardButton;
}
packet.is_relative = true;
#ifdef PS2MOUSE_DEBUG
dbg() << "PS2 Relative Mouse: Buttons " << String::format("%x", packet.buttons);
dbg() << "Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z;
#endif
m_queue.enqueue(packet);
}
void PS2MouseDevice::wait_then_write(u8 port, u8 data)
{
prepare_for_output();
IO::out8(port, data);
}
u8 PS2MouseDevice::wait_then_read(u8 port)
{
prepare_for_input();
return IO::in8(port);
}
void PS2MouseDevice::initialize()
{
// Enable PS aux port
wait_then_write(I8042_STATUS, 0xa8);
check_device_presence();
if (m_device_present)
initialize_device();
}
void PS2MouseDevice::check_device_presence()
{
mouse_write(PS2MOUSE_REQUEST_SINGLE_PACKET);
u8 maybe_ack = mouse_read();
if (maybe_ack == I8042_ACK) {
m_device_present = true;
klog() << "PS2MouseDevice: Device detected";
// the mouse will send a packet of data, since that's what we asked
// for. we don't care about the content.
mouse_read();
mouse_read();
mouse_read();
} else {
m_device_present = false;
klog() << "PS2MouseDevice: Device not detected";
}
}
u8 PS2MouseDevice::get_device_id()
{
mouse_write(PS2MOUSE_GET_DEVICE_ID);
expect_ack();
return mouse_read();
}
void PS2MouseDevice::set_sample_rate(u8 rate)
{
mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
expect_ack();
mouse_write(rate);
expect_ack();
}
void PS2MouseDevice::initialize_device()
{
if (!m_device_present)
return;
// Enable interrupts
wait_then_write(I8042_STATUS, 0x20);
// Enable the PS/2 mouse IRQ (12).
// NOTE: The keyboard uses IRQ 1 (and is enabled by bit 0 in this register).
u8 status = wait_then_read(I8042_BUFFER) | 2;
wait_then_write(I8042_STATUS, 0x60);
wait_then_write(I8042_BUFFER, status);
// Set default settings.
mouse_write(PS2MOUSE_SET_DEFAULTS);
expect_ack();
// Enable.
mouse_write(PS2MOUSE_ENABLE_PACKET_STREAMING);
expect_ack();
u8 device_id = get_device_id();
if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
// Send magical wheel initiation sequence.
set_sample_rate(200);
set_sample_rate(100);
set_sample_rate(80);
device_id = get_device_id();
}
if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
m_has_wheel = true;
klog() << "PS2MouseDevice: Mouse wheel enabled!";
} else {
klog() << "PS2MouseDevice: No mouse wheel detected!";
}
if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
// Try to enable 5 buttons as well!
set_sample_rate(200);
set_sample_rate(200);
set_sample_rate(80);
device_id = get_device_id();
}
if (device_id == PS2MOUSE_INTELLIMOUSE_EXPLORER_ID) {
m_has_five_buttons = true;
klog() << "PS2MouseDevice: 5 buttons enabled!";
}
enable_irq();
}
void PS2MouseDevice::expect_ack()
{
u8 data = mouse_read();
ASSERT(data == I8042_ACK);
}
void PS2MouseDevice::prepare_for_input()
{
for (;;) {
if (IO::in8(I8042_STATUS) & 1)
return;
}
}
void PS2MouseDevice::prepare_for_output()
{
for (;;) {
if (!(IO::in8(I8042_STATUS) & 2))
return;
}
}
void PS2MouseDevice::mouse_write(u8 data)
{
prepare_for_output();
IO::out8(I8042_STATUS, 0xd4);
prepare_for_output();
IO::out8(I8042_BUFFER, data);
}
u8 PS2MouseDevice::mouse_read()
{
prepare_for_input();
return IO::in8(I8042_BUFFER);
}
bool PS2MouseDevice::can_read(const FileDescription&, size_t) const
{
return !m_queue.is_empty();
}
KResultOr<size_t> PS2MouseDevice::read(FileDescription&, size_t, u8* buffer, size_t size)
{
ASSERT(size > 0);
size_t nread = 0;
size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
while (!m_queue.is_empty() && remaining_space_in_buffer) {
auto packet = m_queue.dequeue();
#ifdef PS2MOUSE_DEBUG
dbg() << "PS2 Mouse Read: Buttons " << String::format("%x", packet.buttons);
dbg() << "PS2 Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z << " Relative " << packet.buttons;
dbg() << "PS2 Mouse Read: Filter packets";
#endif
size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
memcpy(buffer + nread, &packet, bytes_read_from_packet);
nread += bytes_read_from_packet;
remaining_space_in_buffer -= bytes_read_from_packet;
}
return nread;
}
KResultOr<size_t> PS2MouseDevice::write(FileDescription&, size_t, const u8*, size_t)
{
return 0;
}
}
|