Persist volume configuration
[helenos.git] / uspace / drv / hid / ps2mouse / ps2mouse.c
blob610c08202c2ad1058808086a796b473d24bb4dd1
1 /*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Jiri Svoboda
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
30 * @{
32 /** @file
33 * @brief PS/2 mouse driver.
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 <ipc/mouseev.h>
43 #include <abi/ipc/methods.h>
45 #include "ps2mouse.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
53 #define PS2_BUFSIZE 3
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 */
62 #define BUTTON_LEFT 0
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_) \
75 do { \
76 uint8_t value = (value_); \
77 uint8_t data = 0; \
78 size_t nread; \
79 const errno_t rc = chardev_read((mouse)->chardev, &data, 1, &nread, \
80 chardev_f_none); \
81 if (rc != EOK) { \
82 ddf_msg(LVL_ERROR, "Failed reading byte: %s", str_error_name(rc));\
83 return rc; \
84 } \
85 if (data != value) { \
86 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
87 data, value); \
88 return EIO; \
89 } \
90 } while (0)
92 #define MOUSE_WRITE_BYTE(mouse, value_) \
93 do { \
94 uint8_t value = (value_); \
95 uint8_t data = (value); \
96 size_t nwr; \
97 const errno_t rc = chardev_write((mouse)->chardev, &data, 1, &nwr); \
98 if (rc != EOK) { \
99 ddf_msg(LVL_ERROR, "Failed writing byte: %s", str_error_name(rc)); \
100 return rc; \
102 } while (0)
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;
126 bool bound = false;
127 errno_t rc;
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.");
134 rc = ENOMEM;
135 goto error;
138 rc = chardev_open(parent_sess, &mouse->chardev);
139 if (rc != EOK) {
140 ddf_msg(LVL_ERROR, "Failed opening character device.");
141 goto error;
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.");
147 rc = ENOMEM;
148 goto error;
151 ddf_fun_set_ops(mouse->mouse_fun, &mouse_ops);
153 rc = ddf_fun_bind(mouse->mouse_fun);
154 if (rc != EOK) {
155 ddf_msg(LVL_ERROR, "Failed binding mouse function.");
156 goto error;
159 bound = true;
161 rc = ddf_fun_add_to_category(mouse->mouse_fun, "mouse");
162 if (rc != EOK) {
163 ddf_msg(LVL_ERROR, "Failed adding mouse function to category.");
164 goto error;
167 /* Disable mouse data reporting. */
168 uint8_t report = PS2_MOUSE_ENABLE_DATA_REPORT;
169 size_t nwr;
170 rc = chardev_write(mouse->chardev, &report, 1, &nwr);
171 if (rc != EOK) {
172 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
173 rc = EIO;
174 goto error;
177 /* Drain input buffer */
178 size_t nread;
179 uint8_t b;
180 do {
181 rc = chardev_read(mouse->chardev, &b, 1, &nread, chardev_f_nonblock);
182 if (rc != EOK) {
183 ddf_msg(LVL_ERROR, "Failed to drain input buffer.\n");
184 rc = EIO;
185 goto error;
187 } while (nread > 0);
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);
201 if (rc != EOK) {
202 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
203 rc = EIO;
204 goto error;
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.",
210 report);
211 rc = EIO;
212 goto error;
215 mouse->polling_fibril = fibril_create(polling_f, mouse);
216 if (mouse->polling_fibril == 0) {
217 rc = ENOMEM;
218 goto error;
221 fibril_add_ready(mouse->polling_fibril);
222 return EOK;
223 error:
224 if (bound)
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;
233 return rc;
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)
248 errno_t rc;
249 size_t pos;
250 size_t nread;
252 pos = 0;
253 while (pos < psize) {
254 rc = chardev_read(mouse->chardev, pbuf + pos, psize - pos,
255 &nread, chardev_f_none);
256 if (rc != EOK) {
257 ddf_msg(LVL_WARN, "Error reading packet.");
258 return rc;
261 pos += nread;
264 return EOK;
267 /** Get data and parse ps2 protocol packets.
268 * @param arg Pointer to ps2_mouse_t structure.
269 * @return Never.
271 errno_t polling_ps2(void *arg)
273 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
274 errno_t rc;
276 bool buttons[PS2_BUTTON_COUNT] = { };
277 while (true) {
278 uint8_t packet[PS2_BUFSIZE] = { };
279 rc = ps2_mouse_read_packet(mouse, packet, PS2_BUFSIZE);
280 if (rc != EOK)
281 continue;
283 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
284 packet[0], packet[1], packet[2]);
286 async_exch_t *exch =
287 async_exchange_begin(mouse->client_sess);
288 if (!exch) {
289 ddf_msg(LVL_ERROR,
290 "Failed creating exchange.");
291 continue;
294 /* Buttons */
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) {
298 buttons[i] = status;
299 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
300 buttons[i]);
304 /* Movement */
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);
316 return 0;
319 /** Get data and parse ps2 protocol with IntelliMouse extension packets.
320 * @param arg Pointer to ps2_mouse_t structure.
321 * @return Never.
323 static errno_t polling_intellimouse(void *arg)
325 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
326 errno_t rc;
328 bool buttons[INTELLIMOUSE_BUTTON_COUNT] = { };
329 while (true) {
330 uint8_t packet[INTELLIMOUSE_BUFSIZE] = { };
331 rc = ps2_mouse_read_packet(mouse, packet, INTELLIMOUSE_BUFSIZE);
332 if (rc != EOK)
333 continue;
335 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx:%hhx.",
336 packet[0], packet[1], packet[2], packet[3]);
338 async_exch_t *exch =
339 async_exchange_begin(mouse->client_sess);
340 if (!exch) {
341 ddf_msg(LVL_ERROR,
342 "Failed creating exchange.");
343 continue;
346 /* Buttons */
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,
368 buttons[i]);
372 /* Movement */
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,
380 move_z);
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);
389 return 0;
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);
419 return EOK;
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));
432 async_sess_t *sess;
434 switch (method) {
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. */
442 if (sess == NULL) {
443 ddf_msg(LVL_WARN,
444 "Failed creating client callback session");
445 async_answer_0(icall, EAGAIN);
446 break;
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);
452 } else {
453 ddf_msg(LVL_ERROR, "Client session already set");
454 async_answer_0(icall, ELIMIT);
456 break;
457 default:
458 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
459 async_answer_0(icall, EINVAL);
460 break;