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 ps2mouse
33 * @brief PS/2 mouse driver.
38 #include <str_error.h>
40 #include <io/keycode.h>
41 #include <io/chardev.h>
42 #include <ipc/mouseev.h>
43 #include <abi/ipc/methods.h>
47 #define PS2_MOUSE_GET_DEVICE_ID 0xf2
48 #define PS2_MOUSE_SET_SAMPLE_RATE 0xf3
49 #define PS2_MOUSE_ENABLE_DATA_REPORT 0xf4
50 #define PS2_MOUSE_DISABLE_DATA_REPORT 0xf5
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, \
82 ddf_msg(LVL_ERROR, "Failed reading byte: %s", str_error_name(rc));\
85 if (data != value) { \
86 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
92 #define MOUSE_WRITE_BYTE(mouse, value_) \
94 uint8_t value = (value_); \
95 uint8_t data = (value); \
97 const errno_t rc = chardev_write((mouse)->chardev, &data, 1, &nwr); \
99 ddf_msg(LVL_ERROR, "Failed writing byte: %s", str_error_name(rc)); \
104 static errno_t
polling_ps2(void *);
105 static errno_t
polling_intellimouse(void *);
106 static errno_t
probe_intellimouse(ps2_mouse_t
*, bool);
107 static void default_connection_handler(ddf_fun_t
*, ipc_call_t
*);
109 /** ps/2 mouse driver ops. */
110 static ddf_dev_ops_t mouse_ops
= {
111 .default_handler
= default_connection_handler
114 /** Initialize mouse driver structure.
116 * Connects to parent, creates keyboard function, starts polling fibril.
118 * @param kbd Mouse driver structure to initialize.
119 * @param dev DDF device structure.
121 * @return EOK on success or non-zero error code
123 errno_t
ps2_mouse_init(ps2_mouse_t
*mouse
, ddf_dev_t
*dev
)
125 async_sess_t
*parent_sess
;
129 mouse
->client_sess
= NULL
;
131 parent_sess
= ddf_dev_parent_sess_get(dev
);
132 if (parent_sess
== NULL
) {
133 ddf_msg(LVL_ERROR
, "Failed getting parent session.");
138 rc
= chardev_open(parent_sess
, &mouse
->chardev
);
140 ddf_msg(LVL_ERROR
, "Failed opening character device.");
144 mouse
->mouse_fun
= ddf_fun_create(dev
, fun_exposed
, "mouse");
145 if (mouse
->mouse_fun
== NULL
) {
146 ddf_msg(LVL_ERROR
, "Error creating mouse function.");
151 ddf_fun_set_ops(mouse
->mouse_fun
, &mouse_ops
);
153 rc
= ddf_fun_bind(mouse
->mouse_fun
);
155 ddf_msg(LVL_ERROR
, "Failed binding mouse function.");
161 rc
= ddf_fun_add_to_category(mouse
->mouse_fun
, "mouse");
163 ddf_msg(LVL_ERROR
, "Failed adding mouse function to category.");
167 /* Disable mouse data reporting. */
168 uint8_t report
= PS2_MOUSE_ENABLE_DATA_REPORT
;
170 rc
= chardev_write(mouse
->chardev
, &report
, 1, &nwr
);
172 ddf_msg(LVL_ERROR
, "Failed to enable data reporting.");
177 /* Drain input buffer */
181 rc
= chardev_read(mouse
->chardev
, &b
, 1, &nread
, chardev_f_nonblock
);
183 ddf_msg(LVL_ERROR
, "Failed to drain input buffer.\n");
189 /* Probe IntelliMouse extensions. */
190 errno_t (*polling_f
)(void *) = polling_ps2
;
191 if (probe_intellimouse(mouse
, false) == EOK
) {
192 ddf_msg(LVL_NOTE
, "Enabled IntelliMouse extensions");
193 polling_f
= polling_intellimouse
;
194 if (probe_intellimouse(mouse
, true) == EOK
)
195 ddf_msg(LVL_NOTE
, "Enabled 4th and 5th button.");
198 /* Enable mouse data reporting. */
199 report
= PS2_MOUSE_ENABLE_DATA_REPORT
;
200 rc
= chardev_write(mouse
->chardev
, &report
, 1, &nwr
);
202 ddf_msg(LVL_ERROR
, "Failed to enable data reporting.");
207 rc
= chardev_read(mouse
->chardev
, &report
, 1, &nread
, chardev_f_none
);
208 if (rc
!= EOK
|| report
!= PS2_MOUSE_ACK
) {
209 ddf_msg(LVL_ERROR
, "Failed to confirm data reporting: %hhx.",
215 mouse
->polling_fibril
= fibril_create(polling_f
, mouse
);
216 if (mouse
->polling_fibril
== 0) {
221 fibril_add_ready(mouse
->polling_fibril
);
225 ddf_fun_unbind(mouse
->mouse_fun
);
226 if (mouse
->mouse_fun
!= NULL
) {
227 ddf_fun_destroy(mouse
->mouse_fun
);
228 mouse
->mouse_fun
= NULL
;
231 chardev_close(mouse
->chardev
);
232 mouse
->chardev
= NULL
;
236 /** Read fixed-size mouse packet.
238 * Continue reading until entire packet is received.
240 * @param mouse Mouse device
241 * @param pbuf Buffer for storing packet
242 * @param psize Packet size
244 * @return EOK on success or non-zero error code
246 static errno_t
ps2_mouse_read_packet(ps2_mouse_t
*mouse
, void *pbuf
, size_t psize
)
253 while (pos
< psize
) {
254 rc
= chardev_read(mouse
->chardev
, pbuf
+ pos
, psize
- pos
,
255 &nread
, chardev_f_none
);
257 ddf_msg(LVL_WARN
, "Error reading packet.");
267 /** Get data and parse ps2 protocol packets.
268 * @param arg Pointer to ps2_mouse_t structure.
271 errno_t
polling_ps2(void *arg
)
273 ps2_mouse_t
*mouse
= (ps2_mouse_t
*) arg
;
276 bool buttons
[PS2_BUTTON_COUNT
] = { };
278 uint8_t packet
[PS2_BUFSIZE
] = { };
279 rc
= ps2_mouse_read_packet(mouse
, packet
, PS2_BUFSIZE
);
283 ddf_msg(LVL_DEBUG2
, "Got packet: %hhx:%hhx:%hhx.",
284 packet
[0], packet
[1], packet
[2]);
287 async_exchange_begin(mouse
->client_sess
);
290 "Failed creating exchange.");
295 for (unsigned i
= 0; i
< PS2_BUTTON_COUNT
; ++i
) {
296 const bool status
= (packet
[0] & PS2_BUTTON_MASK(i
));
297 if (buttons
[i
] != status
) {
299 async_msg_2(exch
, MOUSEEV_BUTTON_EVENT
, i
+ 1,
305 const int16_t move_x
=
306 ((packet
[0] & X_SIGN
) ? 0xff00 : 0) | packet
[1];
307 const int16_t move_y
=
308 (((packet
[0] & Y_SIGN
) ? 0xff00 : 0) | packet
[2]);
309 //TODO: Consider overflow bit
310 if (move_x
!= 0 || move_y
!= 0) {
311 async_msg_2(exch
, MOUSEEV_MOVE_EVENT
, move_x
, -move_y
);
313 async_exchange_end(exch
);
319 /** Get data and parse ps2 protocol with IntelliMouse extension packets.
320 * @param arg Pointer to ps2_mouse_t structure.
323 static errno_t
polling_intellimouse(void *arg
)
325 ps2_mouse_t
*mouse
= (ps2_mouse_t
*) arg
;
328 bool buttons
[INTELLIMOUSE_BUTTON_COUNT
] = { };
330 uint8_t packet
[INTELLIMOUSE_BUFSIZE
] = { };
331 rc
= ps2_mouse_read_packet(mouse
, packet
, INTELLIMOUSE_BUFSIZE
);
335 ddf_msg(LVL_DEBUG2
, "Got packet: %hhx:%hhx:%hhx:%hhx.",
336 packet
[0], packet
[1], packet
[2], packet
[3]);
339 async_exchange_begin(mouse
->client_sess
);
342 "Failed creating exchange.");
348 * NOTE: Parsing 4th and 5th button works even if this extension
349 * is not supported and whole 4th byte should be interpreted
350 * as Z-axis movement. the upper 4 bits are just a sign
351 * extension then. + sign is interpreted as "button up"
352 * (i.e no change since that is the default) and - sign fails
353 * the "imb" condition. Thus 4th and 5th buttons are never
354 * down on wheel only extension.
356 const bool imb
= (packet
[3] & INTELLIMOUSE_ALWAYS_ZERO
) == 0;
357 const bool status
[] = {
358 [0] = packet
[0] & PS2_BUTTON_MASK(0),
359 [1] = packet
[0] & PS2_BUTTON_MASK(1),
360 [2] = packet
[0] & PS2_BUTTON_MASK(2),
361 [3] = (packet
[3] & INTELLIMOUSE_BUTTON_4
) && imb
,
362 [4] = (packet
[3] & INTELLIMOUSE_BUTTON_5
) && imb
,
364 for (unsigned i
= 0; i
< INTELLIMOUSE_BUTTON_COUNT
; ++i
) {
365 if (buttons
[i
] != status
[i
]) {
366 buttons
[i
] = status
[i
];
367 async_msg_2(exch
, MOUSEEV_BUTTON_EVENT
, i
+ 1,
373 const int16_t move_x
=
374 ((packet
[0] & X_SIGN
) ? 0xff00 : 0) | packet
[1];
375 const int16_t move_y
=
376 (((packet
[0] & Y_SIGN
) ? 0xff00 : 0) | packet
[2]);
377 const int8_t move_z
=
378 (((packet
[3] & Z_SIGN
) ? 0xf0 : 0) | (packet
[3] & 0xf));
379 ddf_msg(LVL_DEBUG2
, "Parsed moves: %d:%d:%hhd", move_x
, move_y
,
381 //TODO: Consider overflow bit
382 if (move_x
!= 0 || move_y
!= 0 || move_z
!= 0) {
383 async_msg_3(exch
, MOUSEEV_MOVE_EVENT
,
384 move_x
, -move_y
, -move_z
);
386 async_exchange_end(exch
);
392 /** Send magic sequence to initialize IntelliMouse extensions.
393 * @param exch IPC exchange to the parent device.
394 * @param buttons True selects magic sequence for 4th and 5th button,
395 * false selects wheel support magic sequence.
396 * See http://www.computer-engineering.org/ps2mouse/ for details.
398 static errno_t
probe_intellimouse(ps2_mouse_t
*mouse
, bool buttons
)
400 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_SET_SAMPLE_RATE
);
401 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
402 MOUSE_WRITE_BYTE(mouse
, 200);
403 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
405 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_SET_SAMPLE_RATE
);
406 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
407 MOUSE_WRITE_BYTE(mouse
, buttons
? 200 : 100);
408 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
410 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_SET_SAMPLE_RATE
);
411 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
412 MOUSE_WRITE_BYTE(mouse
, 80);
413 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
415 MOUSE_WRITE_BYTE(mouse
, PS2_MOUSE_GET_DEVICE_ID
);
416 MOUSE_READ_BYTE_TEST(mouse
, PS2_MOUSE_ACK
);
417 MOUSE_READ_BYTE_TEST(mouse
, buttons
? 4 : 3);
422 /** Default handler for IPC methods not handled by DDF.
424 * @param fun Device function handling the call.
425 * @param icall Call data.
428 void default_connection_handler(ddf_fun_t
*fun
, ipc_call_t
*icall
)
430 const sysarg_t method
= ipc_get_imethod(icall
);
431 ps2_mouse_t
*mouse
= ddf_dev_data_get(ddf_fun_get_dev(fun
));
435 case IPC_M_CONNECT_TO_ME
:
437 * This might be ugly but async_callback_receive_start makes no
438 * difference for incorrect call and malloc failure.
440 sess
= async_callback_receive_start(EXCHANGE_SERIALIZE
, icall
);
441 /* Probably ENOMEM error, try again. */
444 "Failed creating client callback session");
445 async_answer_0(icall
, EAGAIN
);
448 if (mouse
->client_sess
== NULL
) {
449 mouse
->client_sess
= sess
;
450 ddf_msg(LVL_DEBUG
, "Set client session");
451 async_answer_0(icall
, EOK
);
453 ddf_msg(LVL_ERROR
, "Client session already set");
454 async_answer_0(icall
, ELIMIT
);
458 ddf_msg(LVL_ERROR
, "Unknown method: %d.", (int)method
);
459 async_answer_0(icall
, EINVAL
);