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

Last change on this file was a635535, checked in by Jiri Svoboda <jiri@…>, 19 months ago

Move console/con_srv out of libc into a separate library

  • Property mode set to 100644
File size: 12.7 KB
RevLine 
[2ecb5ec]1/*
2 * Copyright (c) 2011 Jan Vesely
[15c5418]3 * Copyright (c) 2017 Jiri Svoboda
[2ecb5ec]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 */
[c8ea6eca]29/** @addtogroup ps2mouse
[2ecb5ec]30 * @{
31 */
32/** @file
[15c5418]33 * @brief PS/2 mouse driver.
[2ecb5ec]34 */
35
[3e6a98c5]36#include <stdbool.h>
[2ecb5ec]37#include <errno.h>
[dd8ab1c]38#include <str_error.h>
[2ecb5ec]39#include <ddf/log.h>
40#include <io/keycode.h>
[75751db6]41#include <io/chardev.h>
[2ecb5ec]42#include <ipc/mouseev.h>
43#include <abi/ipc/methods.h>
44
45#include "ps2mouse.h"
46
[c637571]47#define PS2_MOUSE_GET_DEVICE_ID 0xf2
48#define PS2_MOUSE_SET_SAMPLE_RATE 0xf3
49#define PS2_MOUSE_ENABLE_DATA_REPORT 0xf4
[af7b85b]50#define PS2_MOUSE_DISABLE_DATA_REPORT 0xf5
[c637571]51#define PS2_MOUSE_ACK 0xfa
[2ecb5ec]52
[c637571]53#define PS2_BUFSIZE 3
54#define INTELLIMOUSE_BUFSIZE 4
[2ecb5ec]55
[c322fd6]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 */
[2ecb5ec]61
62#define BUTTON_LEFT 0
63#define BUTTON_RIGHT 1
64#define BUTTON_MIDDLE 2
[c637571]65#define PS2_BUTTON_COUNT 3
[c322fd6]66
67#define INTELLIMOUSE_ALWAYS_ZERO (0xc0)
68#define INTELLIMOUSE_BUTTON_4 (1 << 4) /* 4th byte */
69#define INTELLIMOUSE_BUTTON_5 (1 << 5) /* 4th byte */
[c637571]70#define INTELLIMOUSE_BUTTON_COUNT 5
[2ecb5ec]71
[c637571]72#define PS2_BUTTON_MASK(button) (1 << button)
[2ecb5ec]73
[15c5418]74#define MOUSE_READ_BYTE_TEST(mouse, value_) \
[c637571]75do { \
[d87561c]76 uint8_t value = (value_); \
[c637571]77 uint8_t data = 0; \
[5d50c419]78 size_t nread; \
[f2d88f3]79 const errno_t rc = chardev_read((mouse)->chardev, &data, 1, &nread, \
80 chardev_f_none); \
[5d50c419]81 if (rc != EOK) { \
[dd8ab1c]82 ddf_msg(LVL_ERROR, "Failed reading byte: %s", str_error_name(rc));\
[5d50c419]83 return rc; \
[c637571]84 } \
[d87561c]85 if (data != value) { \
[68c60b0]86 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
[d87561c]87 data, value); \
[c637571]88 return EIO; \
89 } \
90} while (0)
[a455321]91
[15c5418]92#define MOUSE_WRITE_BYTE(mouse, value_) \
[c637571]93do { \
[d87561c]94 uint8_t value = (value_); \
[c322fd6]95 uint8_t data = (value); \
[5d50c419]96 size_t nwr; \
[b7fd2a0]97 const errno_t rc = chardev_write((mouse)->chardev, &data, 1, &nwr); \
[5d50c419]98 if (rc != EOK) { \
[dd8ab1c]99 ddf_msg(LVL_ERROR, "Failed writing byte: %s", str_error_name(rc)); \
[5d50c419]100 return rc; \
[c637571]101 } \
102} while (0)
[8bb9540]103
[b7fd2a0]104static errno_t polling_ps2(void *);
105static errno_t polling_intellimouse(void *);
106static errno_t probe_intellimouse(ps2_mouse_t *, bool);
[984a9ba]107static void default_connection_handler(ddf_fun_t *, ipc_call_t *);
[8bb9540]108
[acac2ef]109/** ps/2 mouse driver ops. */
[2ecb5ec]110static ddf_dev_ops_t mouse_ops = {
111 .default_handler = default_connection_handler
112};
[8bb9540]113
[acac2ef]114/** Initialize mouse driver structure.
[15c5418]115 *
116 * Connects to parent, creates keyboard function, starts polling fibril.
117 *
[acac2ef]118 * @param kbd Mouse driver structure to initialize.
119 * @param dev DDF device structure.
120 *
[15c5418]121 * @return EOK on success or non-zero error code
[acac2ef]122 */
[b7fd2a0]123errno_t ps2_mouse_init(ps2_mouse_t *mouse, ddf_dev_t *dev)
[2ecb5ec]124{
[15c5418]125 async_sess_t *parent_sess;
126 bool bound = false;
[b7fd2a0]127 errno_t rc;
[15c5418]128
[e98fe28c]129 mouse->client_sess = NULL;
[15c5418]130
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;
136 }
137
138 rc = chardev_open(parent_sess, &mouse->chardev);
139 if (rc != EOK) {
140 ddf_msg(LVL_ERROR, "Failed opening character device.");
141 goto error;
142 }
[2ecb5ec]143
144 mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
[15c5418]145 if (mouse->mouse_fun == NULL) {
146 ddf_msg(LVL_ERROR, "Error creating mouse function.");
147 rc = ENOMEM;
148 goto error;
[2ecb5ec]149 }
[15c5418]150
[56fd7cf]151 ddf_fun_set_ops(mouse->mouse_fun, &mouse_ops);
[2ecb5ec]152
[15c5418]153 rc = ddf_fun_bind(mouse->mouse_fun);
154 if (rc != EOK) {
155 ddf_msg(LVL_ERROR, "Failed binding mouse function.");
156 goto error;
[2ecb5ec]157 }
158
[15c5418]159 bound = true;
160
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;
[2ecb5ec]165 }
[15c5418]166
[af7b85b]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;
175 }
176
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;
186 }
187 } while (nread > 0);
188
[c637571]189 /* Probe IntelliMouse extensions. */
[3bacee1]190 errno_t (*polling_f)(void *) = polling_ps2;
[15c5418]191 if (probe_intellimouse(mouse, false) == EOK) {
[c637571]192 ddf_msg(LVL_NOTE, "Enabled IntelliMouse extensions");
193 polling_f = polling_intellimouse;
[15c5418]194 if (probe_intellimouse(mouse, true) == EOK)
[c322fd6]195 ddf_msg(LVL_NOTE, "Enabled 4th and 5th button.");
[c637571]196 }
[15c5418]197
[2ecb5ec]198 /* Enable mouse data reporting. */
[af7b85b]199 report = PS2_MOUSE_ENABLE_DATA_REPORT;
[5d50c419]200 rc = chardev_write(mouse->chardev, &report, 1, &nwr);
201 if (rc != EOK) {
[c637571]202 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
[15c5418]203 rc = EIO;
204 goto error;
[2ecb5ec]205 }
[d56ab85]206
[f2d88f3]207 rc = chardev_read(mouse->chardev, &report, 1, &nread, chardev_f_none);
[5d50c419]208 if (rc != EOK || report != PS2_MOUSE_ACK) {
[c637571]209 ddf_msg(LVL_ERROR, "Failed to confirm data reporting: %hhx.",
210 report);
[15c5418]211 rc = EIO;
212 goto error;
[2ecb5ec]213 }
214
[c637571]215 mouse->polling_fibril = fibril_create(polling_f, mouse);
[15c5418]216 if (mouse->polling_fibril == 0) {
217 rc = ENOMEM;
218 goto error;
[2ecb5ec]219 }
[15c5418]220
[2ecb5ec]221 fibril_add_ready(mouse->polling_fibril);
222 return EOK;
[15c5418]223error:
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;
229 }
230
231 chardev_close(mouse->chardev);
232 mouse->chardev = NULL;
233 return rc;
[2ecb5ec]234}
[8bb9540]235
[c657bd7]236/** Read fixed-size mouse packet.
237 *
238 * Continue reading until entire packet is received.
239 *
240 * @param mouse Mouse device
241 * @param pbuf Buffer for storing packet
242 * @param psize Packet size
243 *
244 * @return EOK on success or non-zero error code
245 */
[b7fd2a0]246static errno_t ps2_mouse_read_packet(ps2_mouse_t *mouse, void *pbuf, size_t psize)
[c657bd7]247{
[b7fd2a0]248 errno_t rc;
[c657bd7]249 size_t pos;
250 size_t nread;
251
252 pos = 0;
253 while (pos < psize) {
254 rc = chardev_read(mouse->chardev, pbuf + pos, psize - pos,
[f2d88f3]255 &nread, chardev_f_none);
[c657bd7]256 if (rc != EOK) {
257 ddf_msg(LVL_WARN, "Error reading packet.");
258 return rc;
259 }
260
261 pos += nread;
262 }
263
264 return EOK;
265}
266
[acac2ef]267/** Get data and parse ps2 protocol packets.
268 * @param arg Pointer to ps2_mouse_t structure.
269 * @return Never.
270 */
[b7fd2a0]271errno_t polling_ps2(void *arg)
[2ecb5ec]272{
[15c5418]273 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
[b7fd2a0]274 errno_t rc;
[2ecb5ec]275
[3bacee1]276 bool buttons[PS2_BUTTON_COUNT] = { };
[76d0981d]277 while (true) {
[3bacee1]278 uint8_t packet[PS2_BUFSIZE] = { };
[c657bd7]279 rc = ps2_mouse_read_packet(mouse, packet, PS2_BUFSIZE);
280 if (rc != EOK)
[2ecb5ec]281 continue;
[5d50c419]282
[2ecb5ec]283 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
284 packet[0], packet[1], packet[2]);
285
286 async_exch_t *exch =
[e98fe28c]287 async_exchange_begin(mouse->client_sess);
[2ecb5ec]288 if (!exch) {
289 ddf_msg(LVL_ERROR,
[e98fe28c]290 "Failed creating exchange.");
[2ecb5ec]291 continue;
292 }
[db9ef0c]293
[2ecb5ec]294 /* Buttons */
[c637571]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;
[2ecb5ec]299 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
300 buttons[i]);
301 }
302 }
[db9ef0c]303
[ec3b125]304 /* Movement */
[db9ef0c]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
[2ecb5ec]310 if (move_x != 0 || move_y != 0) {
[db9ef0c]311 async_msg_2(exch, MOUSEEV_MOVE_EVENT, move_x, -move_y);
[2ecb5ec]312 }
313 async_exchange_end(exch);
314 }
[15c5418]315
316 return 0;
[2ecb5ec]317}
[8bb9540]318
[c322fd6]319/** Get data and parse ps2 protocol with IntelliMouse extension packets.
[c637571]320 * @param arg Pointer to ps2_mouse_t structure.
321 * @return Never.
322 */
[b7fd2a0]323static errno_t polling_intellimouse(void *arg)
[c637571]324{
[15c5418]325 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
[b7fd2a0]326 errno_t rc;
[c637571]327
[3bacee1]328 bool buttons[INTELLIMOUSE_BUTTON_COUNT] = { };
[76d0981d]329 while (true) {
[3bacee1]330 uint8_t packet[INTELLIMOUSE_BUFSIZE] = { };
[c657bd7]331 rc = ps2_mouse_read_packet(mouse, packet, INTELLIMOUSE_BUFSIZE);
332 if (rc != EOK)
[c637571]333 continue;
[c657bd7]334
[c637571]335 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx:%hhx.",
336 packet[0], packet[1], packet[2], packet[3]);
337
338 async_exch_t *exch =
[e98fe28c]339 async_exchange_begin(mouse->client_sess);
[c637571]340 if (!exch) {
341 ddf_msg(LVL_ERROR,
[e98fe28c]342 "Failed creating exchange.");
[c637571]343 continue;
344 }
[c322fd6]345
346 /* Buttons */
[6ff23ff]347 /*
348 * NOTE: Parsing 4th and 5th button works even if this extension
[c322fd6]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
[6ff23ff]354 * down on wheel only extension.
355 */
[c322fd6]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,
363 };
364 for (unsigned i = 0; i < INTELLIMOUSE_BUTTON_COUNT; ++i) {
365 if (buttons[i] != status[i]) {
366 buttons[i] = status[i];
[c637571]367 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
368 buttons[i]);
369 }
370 }
371
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);
385 }
386 async_exchange_end(exch);
387 }
[15c5418]388
389 return 0;
[c637571]390}
[8bb9540]391
[c322fd6]392/** Send magic sequence to initialize IntelliMouse extensions.
[2f79a38]393 * @param exch IPC exchange to the parent device.
[c322fd6]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.
397 */
[b7fd2a0]398static errno_t probe_intellimouse(ps2_mouse_t *mouse, bool buttons)
[c637571]399{
[15c5418]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);
404
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);
409
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);
414
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);
[c637571]418
419 return EOK;
420}
[8bb9540]421
[acac2ef]422/** Default handler for IPC methods not handled by DDF.
423 *
[984a9ba]424 * @param fun Device function handling the call.
425 * @param icall Call data.
426 *
[acac2ef]427 */
[984a9ba]428void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
[2ecb5ec]429{
[fafb8e5]430 const sysarg_t method = ipc_get_imethod(icall);
[56fd7cf]431 ps2_mouse_t *mouse = ddf_dev_data_get(ddf_fun_get_dev(fun));
[338d54a7]432 async_sess_t *sess;
[2ecb5ec]433
434 switch (method) {
[338d54a7]435 case IPC_M_CONNECT_TO_ME:
[6ff23ff]436 /*
437 * This might be ugly but async_callback_receive_start makes no
438 * difference for incorrect call and malloc failure.
439 */
[338d54a7]440 sess = async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
[2ecb5ec]441 /* Probably ENOMEM error, try again. */
442 if (sess == NULL) {
443 ddf_msg(LVL_WARN,
[e98fe28c]444 "Failed creating client callback session");
[984a9ba]445 async_answer_0(icall, EAGAIN);
[2ecb5ec]446 break;
447 }
[e98fe28c]448 if (mouse->client_sess == NULL) {
449 mouse->client_sess = sess;
450 ddf_msg(LVL_DEBUG, "Set client session");
[984a9ba]451 async_answer_0(icall, EOK);
[2ecb5ec]452 } else {
[e98fe28c]453 ddf_msg(LVL_ERROR, "Client session already set");
[984a9ba]454 async_answer_0(icall, ELIMIT);
[2ecb5ec]455 }
456 break;
457 default:
[9b56a8dd]458 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
[984a9ba]459 async_answer_0(icall, EINVAL);
[2ecb5ec]460 break;
461 }
462}
Note: See TracBrowser for help on using the repository browser.