source: mainline/uspace/drv/hid/ps2mouse/ps2mouse.c@ af7b85b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since af7b85b was af7b85b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

ps2mouse needs to deal with pre-enabled reporting

Grub 2 enables ps2 mouse reporting which can get in the way of
ps2 mouse initialization where we expect a certain response.
What we need to do is first disable reporting, then drain the
input buffer, then initialize the mouse.

  • Property mode set to 100644
File size: 12.7 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup ps2mouse
30 * @{
31 */
32/** @file
33 * @brief PS/2 mouse driver.
34 */
35
36#include <stdbool.h>
37#include <errno.h>
38#include <str_error.h>
39#include <ddf/log.h>
40#include <io/keycode.h>
41#include <io/chardev.h>
42#include <io/console.h>
43#include <ipc/mouseev.h>
44#include <abi/ipc/methods.h>
45
46#include "ps2mouse.h"
47
48#define PS2_MOUSE_GET_DEVICE_ID 0xf2
49#define PS2_MOUSE_SET_SAMPLE_RATE 0xf3
50#define PS2_MOUSE_ENABLE_DATA_REPORT 0xf4
51#define PS2_MOUSE_DISABLE_DATA_REPORT 0xf5
52#define PS2_MOUSE_ACK 0xfa
53
54#define PS2_BUFSIZE 3
55#define INTELLIMOUSE_BUFSIZE 4
56
57#define Z_SIGN (1 << 3) /* 4th byte */
58#define X_SIGN (1 << 4) /* 1st byte */
59#define Y_SIGN (1 << 5) /* 1st byte */
60#define X_OVERFLOW (1 << 6) /* 1st byte */
61#define Y_OVERFLOW (1 << 7) /* 1st byte */
62
63#define BUTTON_LEFT 0
64#define BUTTON_RIGHT 1
65#define BUTTON_MIDDLE 2
66#define PS2_BUTTON_COUNT 3
67
68#define INTELLIMOUSE_ALWAYS_ZERO (0xc0)
69#define INTELLIMOUSE_BUTTON_4 (1 << 4) /* 4th byte */
70#define INTELLIMOUSE_BUTTON_5 (1 << 5) /* 4th byte */
71#define INTELLIMOUSE_BUTTON_COUNT 5
72
73#define PS2_BUTTON_MASK(button) (1 << button)
74
75#define MOUSE_READ_BYTE_TEST(mouse, value_) \
76do { \
77 uint8_t value = (value_); \
78 uint8_t data = 0; \
79 size_t nread; \
80 const errno_t rc = chardev_read((mouse)->chardev, &data, 1, &nread, \
81 chardev_f_none); \
82 if (rc != EOK) { \
83 ddf_msg(LVL_ERROR, "Failed reading byte: %s", str_error_name(rc));\
84 return rc; \
85 } \
86 if (data != value) { \
87 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
88 data, value); \
89 return EIO; \
90 } \
91} while (0)
92
93#define MOUSE_WRITE_BYTE(mouse, value_) \
94do { \
95 uint8_t value = (value_); \
96 uint8_t data = (value); \
97 size_t nwr; \
98 const errno_t rc = chardev_write((mouse)->chardev, &data, 1, &nwr); \
99 if (rc != EOK) { \
100 ddf_msg(LVL_ERROR, "Failed writing byte: %s", str_error_name(rc)); \
101 return rc; \
102 } \
103} while (0)
104
105static errno_t polling_ps2(void *);
106static errno_t polling_intellimouse(void *);
107static errno_t probe_intellimouse(ps2_mouse_t *, bool);
108static void default_connection_handler(ddf_fun_t *, ipc_call_t *);
109
110/** ps/2 mouse driver ops. */
111static ddf_dev_ops_t mouse_ops = {
112 .default_handler = default_connection_handler
113};
114
115/** Initialize mouse driver structure.
116 *
117 * Connects to parent, creates keyboard function, starts polling fibril.
118 *
119 * @param kbd Mouse driver structure to initialize.
120 * @param dev DDF device structure.
121 *
122 * @return EOK on success or non-zero error code
123 */
124errno_t ps2_mouse_init(ps2_mouse_t *mouse, ddf_dev_t *dev)
125{
126 async_sess_t *parent_sess;
127 bool bound = false;
128 errno_t rc;
129
130 mouse->client_sess = NULL;
131
132 parent_sess = ddf_dev_parent_sess_get(dev);
133 if (parent_sess == NULL) {
134 ddf_msg(LVL_ERROR, "Failed getting parent session.");
135 rc = ENOMEM;
136 goto error;
137 }
138
139 rc = chardev_open(parent_sess, &mouse->chardev);
140 if (rc != EOK) {
141 ddf_msg(LVL_ERROR, "Failed opening character device.");
142 goto error;
143 }
144
145 mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
146 if (mouse->mouse_fun == NULL) {
147 ddf_msg(LVL_ERROR, "Error creating mouse function.");
148 rc = ENOMEM;
149 goto error;
150 }
151
152 ddf_fun_set_ops(mouse->mouse_fun, &mouse_ops);
153
154 rc = ddf_fun_bind(mouse->mouse_fun);
155 if (rc != EOK) {
156 ddf_msg(LVL_ERROR, "Failed binding mouse function.");
157 goto error;
158 }
159
160 bound = true;
161
162 rc = ddf_fun_add_to_category(mouse->mouse_fun, "mouse");
163 if (rc != EOK) {
164 ddf_msg(LVL_ERROR, "Failed adding mouse function to category.");
165 goto error;
166 }
167
168 /* Disable mouse data reporting. */
169 uint8_t report = PS2_MOUSE_ENABLE_DATA_REPORT;
170 size_t nwr;
171 rc = chardev_write(mouse->chardev, &report, 1, &nwr);
172 if (rc != EOK) {
173 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
174 rc = EIO;
175 goto error;
176 }
177
178 /* Drain input buffer */
179 size_t nread;
180 uint8_t b;
181 do {
182 rc = chardev_read(mouse->chardev, &b, 1, &nread, chardev_f_nonblock);
183 if (rc != EOK) {
184 ddf_msg(LVL_ERROR, "Failed to drain input buffer.\n");
185 rc = EIO;
186 goto error;
187 }
188 } while (nread > 0);
189
190 /* Probe IntelliMouse extensions. */
191 errno_t (*polling_f)(void *) = polling_ps2;
192 if (probe_intellimouse(mouse, false) == EOK) {
193 ddf_msg(LVL_NOTE, "Enabled IntelliMouse extensions");
194 polling_f = polling_intellimouse;
195 if (probe_intellimouse(mouse, true) == EOK)
196 ddf_msg(LVL_NOTE, "Enabled 4th and 5th button.");
197 }
198
199 /* Enable mouse data reporting. */
200 report = PS2_MOUSE_ENABLE_DATA_REPORT;
201 rc = chardev_write(mouse->chardev, &report, 1, &nwr);
202 if (rc != EOK) {
203 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
204 rc = EIO;
205 goto error;
206 }
207
208 rc = chardev_read(mouse->chardev, &report, 1, &nread, chardev_f_none);
209 if (rc != EOK || report != PS2_MOUSE_ACK) {
210 ddf_msg(LVL_ERROR, "Failed to confirm data reporting: %hhx.",
211 report);
212 rc = EIO;
213 goto error;
214 }
215
216 mouse->polling_fibril = fibril_create(polling_f, mouse);
217 if (mouse->polling_fibril == 0) {
218 rc = ENOMEM;
219 goto error;
220 }
221
222 fibril_add_ready(mouse->polling_fibril);
223 return EOK;
224error:
225 if (bound)
226 ddf_fun_unbind(mouse->mouse_fun);
227 if (mouse->mouse_fun != NULL) {
228 ddf_fun_destroy(mouse->mouse_fun);
229 mouse->mouse_fun = NULL;
230 }
231
232 chardev_close(mouse->chardev);
233 mouse->chardev = NULL;
234 return rc;
235}
236
237/** Read fixed-size mouse packet.
238 *
239 * Continue reading until entire packet is received.
240 *
241 * @param mouse Mouse device
242 * @param pbuf Buffer for storing packet
243 * @param psize Packet size
244 *
245 * @return EOK on success or non-zero error code
246 */
247static errno_t ps2_mouse_read_packet(ps2_mouse_t *mouse, void *pbuf, size_t psize)
248{
249 errno_t rc;
250 size_t pos;
251 size_t nread;
252
253 pos = 0;
254 while (pos < psize) {
255 rc = chardev_read(mouse->chardev, pbuf + pos, psize - pos,
256 &nread, chardev_f_none);
257 if (rc != EOK) {
258 ddf_msg(LVL_WARN, "Error reading packet.");
259 return rc;
260 }
261
262 pos += nread;
263 }
264
265 return EOK;
266}
267
268/** Get data and parse ps2 protocol packets.
269 * @param arg Pointer to ps2_mouse_t structure.
270 * @return Never.
271 */
272errno_t polling_ps2(void *arg)
273{
274 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
275 errno_t rc;
276
277 bool buttons[PS2_BUTTON_COUNT] = { };
278 while (true) {
279 uint8_t packet[PS2_BUFSIZE] = { };
280 rc = ps2_mouse_read_packet(mouse, packet, PS2_BUFSIZE);
281 if (rc != EOK)
282 continue;
283
284 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
285 packet[0], packet[1], packet[2]);
286
287 async_exch_t *exch =
288 async_exchange_begin(mouse->client_sess);
289 if (!exch) {
290 ddf_msg(LVL_ERROR,
291 "Failed creating exchange.");
292 continue;
293 }
294
295 /* Buttons */
296 for (unsigned i = 0; i < PS2_BUTTON_COUNT; ++i) {
297 const bool status = (packet[0] & PS2_BUTTON_MASK(i));
298 if (buttons[i] != status) {
299 buttons[i] = status;
300 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
301 buttons[i]);
302 }
303 }
304
305 /* Movement */
306 const int16_t move_x =
307 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
308 const int16_t move_y =
309 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
310 //TODO: Consider overflow bit
311 if (move_x != 0 || move_y != 0) {
312 async_msg_2(exch, MOUSEEV_MOVE_EVENT, move_x, -move_y);
313 }
314 async_exchange_end(exch);
315 }
316
317 return 0;
318}
319
320/** Get data and parse ps2 protocol with IntelliMouse extension packets.
321 * @param arg Pointer to ps2_mouse_t structure.
322 * @return Never.
323 */
324static errno_t polling_intellimouse(void *arg)
325{
326 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
327 errno_t rc;
328
329 bool buttons[INTELLIMOUSE_BUTTON_COUNT] = { };
330 while (true) {
331 uint8_t packet[INTELLIMOUSE_BUFSIZE] = { };
332 rc = ps2_mouse_read_packet(mouse, packet, INTELLIMOUSE_BUFSIZE);
333 if (rc != EOK)
334 continue;
335
336 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx:%hhx.",
337 packet[0], packet[1], packet[2], packet[3]);
338
339 async_exch_t *exch =
340 async_exchange_begin(mouse->client_sess);
341 if (!exch) {
342 ddf_msg(LVL_ERROR,
343 "Failed creating exchange.");
344 continue;
345 }
346
347 /* Buttons */
348 /*
349 * NOTE: Parsing 4th and 5th button works even if this extension
350 * is not supported and whole 4th byte should be interpreted
351 * as Z-axis movement. the upper 4 bits are just a sign
352 * extension then. + sign is interpreted as "button up"
353 * (i.e no change since that is the default) and - sign fails
354 * the "imb" condition. Thus 4th and 5th buttons are never
355 * down on wheel only extension.
356 */
357 const bool imb = (packet[3] & INTELLIMOUSE_ALWAYS_ZERO) == 0;
358 const bool status[] = {
359 [0] = packet[0] & PS2_BUTTON_MASK(0),
360 [1] = packet[0] & PS2_BUTTON_MASK(1),
361 [2] = packet[0] & PS2_BUTTON_MASK(2),
362 [3] = (packet[3] & INTELLIMOUSE_BUTTON_4) && imb,
363 [4] = (packet[3] & INTELLIMOUSE_BUTTON_5) && imb,
364 };
365 for (unsigned i = 0; i < INTELLIMOUSE_BUTTON_COUNT; ++i) {
366 if (buttons[i] != status[i]) {
367 buttons[i] = status[i];
368 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
369 buttons[i]);
370 }
371 }
372
373 /* Movement */
374 const int16_t move_x =
375 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
376 const int16_t move_y =
377 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
378 const int8_t move_z =
379 (((packet[3] & Z_SIGN) ? 0xf0 : 0) | (packet[3] & 0xf));
380 ddf_msg(LVL_DEBUG2, "Parsed moves: %d:%d:%hhd", move_x, move_y,
381 move_z);
382 //TODO: Consider overflow bit
383 if (move_x != 0 || move_y != 0 || move_z != 0) {
384 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
385 move_x, -move_y, -move_z);
386 }
387 async_exchange_end(exch);
388 }
389
390 return 0;
391}
392
393/** Send magic sequence to initialize IntelliMouse extensions.
394 * @param exch IPC exchange to the parent device.
395 * @param buttons True selects magic sequence for 4th and 5th button,
396 * false selects wheel support magic sequence.
397 * See http://www.computer-engineering.org/ps2mouse/ for details.
398 */
399static errno_t probe_intellimouse(ps2_mouse_t *mouse, bool buttons)
400{
401 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_SET_SAMPLE_RATE);
402 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
403 MOUSE_WRITE_BYTE(mouse, 200);
404 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
405
406 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_SET_SAMPLE_RATE);
407 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
408 MOUSE_WRITE_BYTE(mouse, buttons ? 200 : 100);
409 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
410
411 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_SET_SAMPLE_RATE);
412 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
413 MOUSE_WRITE_BYTE(mouse, 80);
414 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
415
416 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_GET_DEVICE_ID);
417 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
418 MOUSE_READ_BYTE_TEST(mouse, buttons ? 4 : 3);
419
420 return EOK;
421}
422
423/** Default handler for IPC methods not handled by DDF.
424 *
425 * @param fun Device function handling the call.
426 * @param icall Call data.
427 *
428 */
429void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
430{
431 const sysarg_t method = IPC_GET_IMETHOD(*icall);
432 ps2_mouse_t *mouse = ddf_dev_data_get(ddf_fun_get_dev(fun));
433 async_sess_t *sess;
434
435 switch (method) {
436 case IPC_M_CONNECT_TO_ME:
437 /*
438 * This might be ugly but async_callback_receive_start makes no
439 * difference for incorrect call and malloc failure.
440 */
441 sess = async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
442 /* Probably ENOMEM error, try again. */
443 if (sess == NULL) {
444 ddf_msg(LVL_WARN,
445 "Failed creating client callback session");
446 async_answer_0(icall, EAGAIN);
447 break;
448 }
449 if (mouse->client_sess == NULL) {
450 mouse->client_sess = sess;
451 ddf_msg(LVL_DEBUG, "Set client session");
452 async_answer_0(icall, EOK);
453 } else {
454 ddf_msg(LVL_ERROR, "Client session already set");
455 async_answer_0(icall, ELIMIT);
456 }
457 break;
458 default:
459 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
460 async_answer_0(icall, EINVAL);
461 break;
462 }
463}
Note: See TracBrowser for help on using the repository browser.