2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Jiri Svoboda
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 /** @addtogroup drvmouse
33 * @brief PS/2 mouse driver.
38 #include <str_error.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>
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_ACK 0xfa
54 #define INTELLIMOUSE_BUFSIZE 4
56 #define Z_SIGN (1 << 3) /* 4th byte */
57 #define X_SIGN (1 << 4) /* 1st byte */
58 #define Y_SIGN (1 << 5) /* 1st byte */
59 #define X_OVERFLOW (1 << 6) /* 1st byte */
60 #define Y_OVERFLOW (1 << 7) /* 1st byte */
63 #define BUTTON_RIGHT 1
64 #define BUTTON_MIDDLE 2
65 #define PS2_BUTTON_COUNT 3
67 #define INTELLIMOUSE_ALWAYS_ZERO (0xc0)
68 #define INTELLIMOUSE_BUTTON_4 (1 << 4) /* 4th byte */
69 #define INTELLIMOUSE_BUTTON_5 (1 << 5) /* 4th byte */
70 #define INTELLIMOUSE_BUTTON_COUNT 5
72 #define PS2_BUTTON_MASK(button) (1 << button)
74 #define MOUSE_READ_BYTE_TEST(mouse, value_) \
76 uint8_t value = (value_); \
79 const errno_t rc = chardev_read((mouse)->chardev, &data, 1, &nread); \
81 ddf_msg(LVL_ERROR, "Failed reading byte: %s", str_error_name(rc));\
84 if (data != value) { \
85 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
91 #define MOUSE_WRITE_BYTE(mouse, value_) \
93 uint8_t value = (value_); \
94 uint8_t data = (value); \
96 const errno_t rc = chardev_write((mouse)->chardev, &data, 1, &nwr); \
98 ddf_msg(LVL_ERROR, "Failed writing byte: %s", str_error_name(rc)); \
103 static errno_t
polling_ps2(void *);
104 static errno_t
polling_intellimouse(void *);
105 static errno_t
probe_intellimouse(ps2_mouse_t
*, bool);
106 static void default_connection_handler(ddf_fun_t
*, ipc_callid_t
, ipc_call_t
*);
108 /** ps/2 mouse driver ops. */
109 static ddf_dev_ops_t mouse_ops
= {
110 .default_handler
= default_connection_handler
113 /** Initialize mouse driver structure.
115 * Connects to parent, creates keyboard function, starts polling fibril.
117 * @param kbd Mouse driver structure to initialize.
118 * @param dev DDF device structure.
120 * @return EOK on success or non-zero error code
122 errno_t
ps2_mouse_init(ps2_mouse_t
*mouse
, ddf_dev_t
*dev
)
124 async_sess_t
*parent_sess
;
128 mouse
->client_sess
= NULL
;
130 parent_sess
= ddf_dev_parent_sess_get(dev
);
131 if (parent_sess
== NULL
) {
132 ddf_msg(LVL_ERROR
, "Failed getting parent session.");
137 rc
= chardev_open(parent_sess
, &mouse
->chardev
);
139 ddf_msg(LVL_ERROR
, "Failed opening character device.");
143 mouse
->mouse_fun
= ddf_fun_create(dev
, fun_exposed
, "mouse");
144 if (mouse
->mouse_fun
== NULL
) {
145 ddf_msg(LVL_ERROR
, "Error creating mouse function.");
150 ddf_fun_set_ops(mouse
->mouse_fun
, &mouse_ops
);
152 rc
= ddf_fun_bind(mouse
->mouse_fun
);
154 ddf_msg(LVL_ERROR
, "Failed binding mouse function.");
160 rc
= ddf_fun_add_to_category(mouse
->mouse_fun
, "mouse");
162 ddf_msg(LVL_ERROR
, "Failed adding mouse function to category.");
166 /* Probe IntelliMouse extensions. */
167 errno_t (*polling_f
)(void*) = polling_ps2
;
168 if (probe_intellimouse(mouse
, false) == EOK
) {
169 ddf_msg(LVL_NOTE
, "Enabled IntelliMouse extensions");
170 polling_f
= polling_intellimouse
;
171 if (probe_intellimouse(mouse
, true) == EOK
)
172 ddf_msg(LVL_NOTE
, "Enabled 4th and 5th button.");
175 /* Enable mouse data reporting. */
176 uint8_t report
= PS2_MOUSE_ENABLE_DATA_REPORT
;
178 rc
= chardev_write(mouse
->chardev
, &report
, 1, &nwr
);
180 ddf_msg(LVL_ERROR
, "Failed to enable data reporting.");
186 rc
= chardev_read(mouse
->chardev
, &report
, 1, &nread
);
187 if (rc
!= EOK
|| report
!= PS2_MOUSE_ACK
) {
188 ddf_msg(LVL_ERROR
, "Failed to confirm data reporting: %hhx.",
194 mouse
->polling_fibril
= fibril_create(polling_f
, mouse
);
195 if (mouse
->polling_fibril
== 0) {
200 fibril_add_ready(mouse
->polling_fibril
);
204 ddf_fun_unbind(mouse
->mouse_fun
);
205 if (mouse
->mouse_fun
!= NULL
) {
206 ddf_fun_destroy(mouse
->mouse_fun
);
207 mouse
->mouse_fun
= NULL
;
210 chardev_close(mouse
->chardev
);
211 mouse
->chardev
= NULL
;
215 /** Read fixed-size mouse packet.
217 * Continue reading until entire packet is received.
219 * @param mouse Mouse device
220 * @param pbuf Buffer for storing packet
221 * @param psize Packet size
223 * @return EOK on success or non-zero error code
225 static errno_t
ps2_mouse_read_packet(ps2_mouse_t
*mouse
, void *pbuf
, size_t psize
)
232 while (pos
< psize
) {
233 rc
= chardev_read(mouse
->chardev
, pbuf
+ pos
, psize
- pos
,
236 ddf_msg(LVL_WARN
, "Error reading packet.");
246 /** Get data and parse ps2 protocol packets.
247 * @param arg Pointer to ps2_mouse_t structure.
250 errno_t
polling_ps2(void *arg
)
252 ps2_mouse_t
*mouse
= (ps2_mouse_t
*) arg
;
255 bool buttons
[PS2_BUTTON_COUNT
] = {};
257 uint8_t packet
[PS2_BUFSIZE
] = {};
258 rc
= ps2_mouse_read_packet(mouse
, packet
, PS2_BUFSIZE
);
262 ddf_msg(LVL_DEBUG2
, "Got packet: %hhx:%hhx:%hhx.",
263 packet
[0], packet
[1], packet
[2]);
266 async_exchange_begin(mouse
->client_sess
);
269 "Failed creating exchange.");
274 for (unsigned i
= 0; i
< PS2_BUTTON_COUNT
; ++i
) {
275 const bool status
= (packet
[0] & PS2_BUTTON_MASK(i
));
276 if (buttons
[i
] != status
) {
278 async_msg_2(exch
, MOUSEEV_BUTTON_EVENT
, i
+ 1,
284 const int16_t move_x
=
285 ((packet
[0] & X_SIGN
) ? 0xff00 : 0) | packet
[1];
286 const int16_t move_y
=
287 (((packet
[0] & Y_SIGN
) ? 0xff00 : 0) | packet
[2]);
288 //TODO: Consider overflow bit
289 if (move_x
!= 0 || move_y
!= 0) {
290 async_msg_2(exch
, MOUSEEV_MOVE_EVENT
, move_x
, -move_y
);
292 async_exchange_end(exch
);
298 /** Get data and parse ps2 protocol with IntelliMouse extension packets.
299 * @param arg Pointer to ps2_mouse_t structure.
302 static errno_t
polling_intellimouse(void *arg
)
304 ps2_mouse_t
*mouse
= (ps2_mouse_t
*) arg
;
307 bool buttons
[INTELLIMOUSE_BUTTON_COUNT
] = {};
309 uint8_t packet
[INTELLIMOUSE_BUFSIZE
] = {};
310 rc
= ps2_mouse_read_packet(mouse
, packet
, INTELLIMOUSE_BUFSIZE
);
314 ddf_msg(LVL_DEBUG2
, "Got packet: %hhx:%hhx:%hhx:%hhx.",
315 packet
[0], packet
[1], packet
[2], packet
[3]);
318 async_exchange_begin(mouse
->client_sess
);
321 "Failed creating exchange.");
326 /* NOTE: Parsing 4th and 5th button works even if this extension
327 * is not supported and whole 4th byte should be interpreted
328 * as Z-axis movement. the upper 4 bits are just a sign
329 * extension then. + sign is interpreted as "button up"
330 * (i.e no change since that is the default) and - sign fails
331 * the "imb" condition. Thus 4th and 5th buttons are never
332 * down on wheel only extension. */
333 const bool imb
= (packet
[3] & INTELLIMOUSE_ALWAYS_ZERO
) == 0;
334 const bool status
[] = {
335 [0] = packet
[0] & PS2_BUTTON_MASK(0),
336 [1] = packet
[0] & PS2_BUTTON_MASK(1),
337 [2] = packet
[0] & PS2_BUTTON_MASK(2),
338 [3] = (packet
[3] & INTELLIMOUSE_BUTTON_4
) && imb
,
339 [4] = (packet
[3] & INTELLIMOUSE_BUTTON_5
) && imb
,
341 for (unsigned i
= 0; i
< INTELLIMOUSE_BUTTON_COUNT
; ++i
) {
342 if (buttons
[i
] != status
[i
]) {
343 buttons
[i
] = status
[i
];
344 async_msg_2(exch
, MOUSEEV_BUTTON_EVENT
, i
+ 1,
350 const int16_t move_x
=
351 ((packet
[0] & X_SIGN
) ? 0xff00 : 0) | packet
[1];
352 const int16_t move_y
=
353 (((packet
[0] & Y_SIGN
) ? 0xff00 : 0) | packet
[2]);
354 const int8_t move_z
=
355 (((packet
[3] & Z_SIGN
) ? 0xf0 : 0) | (packet
[3] & 0xf));
356 ddf_msg(LVL_DEBUG2
, "Parsed moves: %d:%d:%hhd", move_x
, move_y
,
358 //TODO: Consider overflow bit
359 if (move_x
!= 0 || move_y
!= 0 || move_z
!= 0) {
360 async_msg_3(exch
, MOUSEEV_MOVE_EVENT
,
361 move_x
, -move_y
, -move_z
);
363 async_exchange_end(exch
);
369 /** Send magic sequence to initialize IntelliMouse extensions.
370 * @param exch IPC exchange to the parent device.
371 * @param buttons True selects magic sequence for 4th and 5th button,
372 * false selects wheel support magic sequence.
373 * See http://www.computer-engineering.org/ps2mouse/ for details.
375 static errno_t
probe_intellimouse(ps2_mouse_t
*mouse
, bool buttons
)
377 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_SET_SAMPLE_RATE
);
378 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
379 MOUSE_WRITE_BYTE(mouse
, 200);
380 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
382 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_SET_SAMPLE_RATE
);
383 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
384 MOUSE_WRITE_BYTE(mouse
, buttons
? 200 : 100);
385 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
387 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_SET_SAMPLE_RATE
);
388 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
389 MOUSE_WRITE_BYTE(mouse
, 80);
390 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
392 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_GET_DEVICE_ID
);
393 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
394 MOUSE_READ_BYTE_TEST(mouse
, buttons
? 4 : 3);
399 /** Default handler for IPC methods not handled by DDF.
401 * @param fun Device function handling the call.
402 * @param icallid Call id.
403 * @param icall Call data.
405 void default_connection_handler(ddf_fun_t
*fun
,
406 ipc_callid_t icallid
, ipc_call_t
*icall
)
408 const sysarg_t method
= IPC_GET_IMETHOD(*icall
);
409 ps2_mouse_t
*mouse
= ddf_dev_data_get(ddf_fun_get_dev(fun
));
412 /* This might be ugly but async_callback_receive_start makes no
413 * difference for incorrect call and malloc failure. */
414 case IPC_M_CONNECT_TO_ME
: {
416 async_callback_receive_start(EXCHANGE_SERIALIZE
, icall
);
417 /* Probably ENOMEM error, try again. */
420 "Failed creating client callback session");
421 async_answer_0(icallid
, EAGAIN
);
424 if (mouse
->client_sess
== NULL
) {
425 mouse
->client_sess
= sess
;
426 ddf_msg(LVL_DEBUG
, "Set client session");
427 async_answer_0(icallid
, EOK
);
429 ddf_msg(LVL_ERROR
, "Client session already set");
430 async_answer_0(icallid
, ELIMIT
);
435 ddf_msg(LVL_ERROR
, "Unknown method: %d.", (int)method
);
436 async_answer_0(icallid
, EINVAL
);