source: mainline/uspace/drv/hid/ps2mouse/ps2mouse.c@ 19ea61d

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

Less is sometimes more. Need chardev_read to be able to return less bytes than requested if less is available. Otherwise cannot read variable-sized packets except yte-by-byte.

  • Property mode set to 100644
File size: 12.1 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 */
[acac2ef]29/** @addtogroup drvmouse
[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>
38#include <ddf/log.h>
39#include <io/keycode.h>
[75751db6]40#include <io/chardev.h>
[2ecb5ec]41#include <io/console.h>
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
50#define PS2_MOUSE_ACK 0xfa
[2ecb5ec]51
[c637571]52#define PS2_BUFSIZE 3
53#define INTELLIMOUSE_BUFSIZE 4
[2ecb5ec]54
[c322fd6]55#define Z_SIGN (1 << 3) /* 4th byte */
56#define X_SIGN (1 << 4) /* 1st byte */
57#define Y_SIGN (1 << 5) /* 1st byte */
58#define X_OVERFLOW (1 << 6) /* 1st byte */
59#define Y_OVERFLOW (1 << 7) /* 1st byte */
[2ecb5ec]60
61#define BUTTON_LEFT 0
62#define BUTTON_RIGHT 1
63#define BUTTON_MIDDLE 2
[c637571]64#define PS2_BUTTON_COUNT 3
[c322fd6]65
66#define INTELLIMOUSE_ALWAYS_ZERO (0xc0)
67#define INTELLIMOUSE_BUTTON_4 (1 << 4) /* 4th byte */
68#define INTELLIMOUSE_BUTTON_5 (1 << 5) /* 4th byte */
[c637571]69#define INTELLIMOUSE_BUTTON_COUNT 5
[2ecb5ec]70
[c637571]71#define PS2_BUTTON_MASK(button) (1 << button)
[2ecb5ec]72
[15c5418]73#define MOUSE_READ_BYTE_TEST(mouse, value_) \
[c637571]74do { \
[d87561c]75 uint8_t value = (value_); \
[c637571]76 uint8_t data = 0; \
[5d50c419]77 size_t nread; \
78 const int rc = chardev_read((mouse)->chardev, &data, 1, &nread); \
79 if (rc != EOK) { \
80 ddf_msg(LVL_ERROR, "Failed reading byte: %d", rc);\
81 return rc; \
[c637571]82 } \
[d87561c]83 if (data != value) { \
[68c60b0]84 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
[d87561c]85 data, value); \
[c637571]86 return EIO; \
87 } \
88} while (0)
[a455321]89
[15c5418]90#define MOUSE_WRITE_BYTE(mouse, value_) \
[c637571]91do { \
[d87561c]92 uint8_t value = (value_); \
[c322fd6]93 uint8_t data = (value); \
[5d50c419]94 size_t nwr; \
95 const int rc = chardev_write((mouse)->chardev, &data, 1, &nwr); \
96 if (rc != EOK) { \
97 ddf_msg(LVL_ERROR, "Failed writing byte: %d", rc); \
98 return rc; \
[c637571]99 } \
100} while (0)
[8bb9540]101
[2ecb5ec]102static int polling_ps2(void *);
[c637571]103static int polling_intellimouse(void *);
[15c5418]104static int probe_intellimouse(ps2_mouse_t *, bool);
[acac2ef]105static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
[8bb9540]106
[acac2ef]107/** ps/2 mouse driver ops. */
[2ecb5ec]108static ddf_dev_ops_t mouse_ops = {
109 .default_handler = default_connection_handler
110};
[8bb9540]111
[acac2ef]112/** Initialize mouse driver structure.
[15c5418]113 *
114 * Connects to parent, creates keyboard function, starts polling fibril.
115 *
[acac2ef]116 * @param kbd Mouse driver structure to initialize.
117 * @param dev DDF device structure.
118 *
[15c5418]119 * @return EOK on success or non-zero error code
[acac2ef]120 */
[2ecb5ec]121int ps2_mouse_init(ps2_mouse_t *mouse, ddf_dev_t *dev)
122{
[15c5418]123 async_sess_t *parent_sess;
124 bool bound = false;
125 int rc;
126
[e98fe28c]127 mouse->client_sess = NULL;
[15c5418]128
129 parent_sess = ddf_dev_parent_sess_get(dev);
130 if (parent_sess == NULL) {
131 ddf_msg(LVL_ERROR, "Failed getting parent session.");
132 rc = ENOMEM;
133 goto error;
134 }
135
136 rc = chardev_open(parent_sess, &mouse->chardev);
137 if (rc != EOK) {
138 ddf_msg(LVL_ERROR, "Failed opening character device.");
139 goto error;
140 }
[2ecb5ec]141
142 mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
[15c5418]143 if (mouse->mouse_fun == NULL) {
144 ddf_msg(LVL_ERROR, "Error creating mouse function.");
145 rc = ENOMEM;
146 goto error;
[2ecb5ec]147 }
[15c5418]148
[56fd7cf]149 ddf_fun_set_ops(mouse->mouse_fun, &mouse_ops);
[2ecb5ec]150
[15c5418]151 rc = ddf_fun_bind(mouse->mouse_fun);
152 if (rc != EOK) {
153 ddf_msg(LVL_ERROR, "Failed binding mouse function.");
154 goto error;
[2ecb5ec]155 }
156
[15c5418]157 bound = true;
158
159 rc = ddf_fun_add_to_category(mouse->mouse_fun, "mouse");
160 if (rc != EOK) {
161 ddf_msg(LVL_ERROR, "Failed adding mouse function to category.");
162 goto error;
[2ecb5ec]163 }
[15c5418]164
[c637571]165 /* Probe IntelliMouse extensions. */
166 int (*polling_f)(void*) = polling_ps2;
[15c5418]167 if (probe_intellimouse(mouse, false) == EOK) {
[c637571]168 ddf_msg(LVL_NOTE, "Enabled IntelliMouse extensions");
169 polling_f = polling_intellimouse;
[15c5418]170 if (probe_intellimouse(mouse, true) == EOK)
[c322fd6]171 ddf_msg(LVL_NOTE, "Enabled 4th and 5th button.");
[c637571]172 }
[15c5418]173
[2ecb5ec]174 /* Enable mouse data reporting. */
[c637571]175 uint8_t report = PS2_MOUSE_ENABLE_DATA_REPORT;
[5d50c419]176 size_t nwr;
177 rc = chardev_write(mouse->chardev, &report, 1, &nwr);
178 if (rc != EOK) {
[c637571]179 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
[15c5418]180 rc = EIO;
181 goto error;
[2ecb5ec]182 }
[d56ab85]183
[5d50c419]184 size_t nread;
185 rc = chardev_read(mouse->chardev, &report, 1, &nread);
186 if (rc != EOK || report != PS2_MOUSE_ACK) {
[c637571]187 ddf_msg(LVL_ERROR, "Failed to confirm data reporting: %hhx.",
188 report);
[15c5418]189 rc = EIO;
190 goto error;
[2ecb5ec]191 }
192
[c637571]193 mouse->polling_fibril = fibril_create(polling_f, mouse);
[15c5418]194 if (mouse->polling_fibril == 0) {
195 rc = ENOMEM;
196 goto error;
[2ecb5ec]197 }
[15c5418]198
[2ecb5ec]199 fibril_add_ready(mouse->polling_fibril);
200 return EOK;
[15c5418]201error:
202 if (bound)
203 ddf_fun_unbind(mouse->mouse_fun);
204 if (mouse->mouse_fun != NULL) {
205 ddf_fun_destroy(mouse->mouse_fun);
206 mouse->mouse_fun = NULL;
207 }
208
209 chardev_close(mouse->chardev);
210 mouse->chardev = NULL;
211 return rc;
[2ecb5ec]212}
[8bb9540]213
[c657bd7]214/** Read fixed-size mouse packet.
215 *
216 * Continue reading until entire packet is received.
217 *
218 * @param mouse Mouse device
219 * @param pbuf Buffer for storing packet
220 * @param psize Packet size
221 *
222 * @return EOK on success or non-zero error code
223 */
224static int ps2_mouse_read_packet(ps2_mouse_t *mouse, void *pbuf, size_t psize)
225{
226 int rc;
227 size_t pos;
228 size_t nread;
229
230 pos = 0;
231 while (pos < psize) {
232 rc = chardev_read(mouse->chardev, pbuf + pos, psize - pos,
233 &nread);
234 if (rc != EOK) {
235 ddf_msg(LVL_WARN, "Error reading packet.");
236 return rc;
237 }
238
239 pos += nread;
240 }
241
242 return EOK;
243}
244
[acac2ef]245/** Get data and parse ps2 protocol packets.
246 * @param arg Pointer to ps2_mouse_t structure.
247 * @return Never.
248 */
[2ecb5ec]249int polling_ps2(void *arg)
250{
[15c5418]251 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
[5d50c419]252 int rc;
[2ecb5ec]253
[c637571]254 bool buttons[PS2_BUTTON_COUNT] = {};
[2ecb5ec]255 while (1) {
[c637571]256 uint8_t packet[PS2_BUFSIZE] = {};
[c657bd7]257 rc = ps2_mouse_read_packet(mouse, packet, PS2_BUFSIZE);
258 if (rc != EOK)
[2ecb5ec]259 continue;
[5d50c419]260
[2ecb5ec]261 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
262 packet[0], packet[1], packet[2]);
263
264 async_exch_t *exch =
[e98fe28c]265 async_exchange_begin(mouse->client_sess);
[2ecb5ec]266 if (!exch) {
267 ddf_msg(LVL_ERROR,
[e98fe28c]268 "Failed creating exchange.");
[2ecb5ec]269 continue;
270 }
[db9ef0c]271
[2ecb5ec]272 /* Buttons */
[c637571]273 for (unsigned i = 0; i < PS2_BUTTON_COUNT; ++i) {
274 const bool status = (packet[0] & PS2_BUTTON_MASK(i));
275 if (buttons[i] != status) {
276 buttons[i] = status;
[2ecb5ec]277 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
278 buttons[i]);
279 }
280 }
[db9ef0c]281
[ec3b125]282 /* Movement */
[db9ef0c]283 const int16_t move_x =
284 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
285 const int16_t move_y =
286 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
287 //TODO: Consider overflow bit
[2ecb5ec]288 if (move_x != 0 || move_y != 0) {
[db9ef0c]289 async_msg_2(exch, MOUSEEV_MOVE_EVENT, move_x, -move_y);
[2ecb5ec]290 }
291 async_exchange_end(exch);
292 }
[15c5418]293
294 return 0;
[2ecb5ec]295}
[8bb9540]296
[c322fd6]297/** Get data and parse ps2 protocol with IntelliMouse extension packets.
[c637571]298 * @param arg Pointer to ps2_mouse_t structure.
299 * @return Never.
300 */
301static int polling_intellimouse(void *arg)
302{
[15c5418]303 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
[5d50c419]304 int rc;
[c637571]305
306 bool buttons[INTELLIMOUSE_BUTTON_COUNT] = {};
307 while (1) {
308 uint8_t packet[INTELLIMOUSE_BUFSIZE] = {};
[c657bd7]309 rc = ps2_mouse_read_packet(mouse, packet, INTELLIMOUSE_BUFSIZE);
310 if (rc != EOK)
[c637571]311 continue;
[c657bd7]312
[c637571]313 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx:%hhx.",
314 packet[0], packet[1], packet[2], packet[3]);
315
316 async_exch_t *exch =
[e98fe28c]317 async_exchange_begin(mouse->client_sess);
[c637571]318 if (!exch) {
319 ddf_msg(LVL_ERROR,
[e98fe28c]320 "Failed creating exchange.");
[c637571]321 continue;
322 }
[c322fd6]323
324 /* Buttons */
325 /* NOTE: Parsing 4th and 5th button works even if this extension
326 * is not supported and whole 4th byte should be interpreted
327 * as Z-axis movement. the upper 4 bits are just a sign
328 * extension then. + sign is interpreted as "button up"
329 * (i.e no change since that is the default) and - sign fails
330 * the "imb" condition. Thus 4th and 5th buttons are never
331 * down on wheel only extension. */
332 const bool imb = (packet[3] & INTELLIMOUSE_ALWAYS_ZERO) == 0;
333 const bool status[] = {
334 [0] = packet[0] & PS2_BUTTON_MASK(0),
335 [1] = packet[0] & PS2_BUTTON_MASK(1),
336 [2] = packet[0] & PS2_BUTTON_MASK(2),
337 [3] = (packet[3] & INTELLIMOUSE_BUTTON_4) && imb,
338 [4] = (packet[3] & INTELLIMOUSE_BUTTON_5) && imb,
339 };
340 for (unsigned i = 0; i < INTELLIMOUSE_BUTTON_COUNT; ++i) {
341 if (buttons[i] != status[i]) {
342 buttons[i] = status[i];
[c637571]343 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
344 buttons[i]);
345 }
346 }
347
348 /* Movement */
349 const int16_t move_x =
350 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
351 const int16_t move_y =
352 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
353 const int8_t move_z =
354 (((packet[3] & Z_SIGN) ? 0xf0 : 0) | (packet[3] & 0xf));
355 ddf_msg(LVL_DEBUG2, "Parsed moves: %d:%d:%hhd", move_x, move_y,
356 move_z);
357 //TODO: Consider overflow bit
358 if (move_x != 0 || move_y != 0 || move_z != 0) {
359 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
360 move_x, -move_y, -move_z);
361 }
362 async_exchange_end(exch);
363 }
[15c5418]364
365 return 0;
[c637571]366}
[8bb9540]367
[c322fd6]368/** Send magic sequence to initialize IntelliMouse extensions.
[2f79a38]369 * @param exch IPC exchange to the parent device.
[c322fd6]370 * @param buttons True selects magic sequence for 4th and 5th button,
371 * false selects wheel support magic sequence.
372 * See http://www.computer-engineering.org/ps2mouse/ for details.
373 */
[15c5418]374static int probe_intellimouse(ps2_mouse_t *mouse, bool buttons)
[c637571]375{
[15c5418]376 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_SET_SAMPLE_RATE);
377 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
378 MOUSE_WRITE_BYTE(mouse, 200);
379 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
380
381 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_SET_SAMPLE_RATE);
382 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
383 MOUSE_WRITE_BYTE(mouse, buttons ? 200 : 100);
384 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
385
386 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_SET_SAMPLE_RATE);
387 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
388 MOUSE_WRITE_BYTE(mouse, 80);
389 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
390
391 MOUSE_WRITE_BYTE(mouse, PS2_MOUSE_GET_DEVICE_ID);
392 MOUSE_READ_BYTE_TEST(mouse, PS2_MOUSE_ACK);
393 MOUSE_READ_BYTE_TEST(mouse, buttons ? 4 : 3);
[c637571]394
395 return EOK;
396}
[8bb9540]397
[acac2ef]398/** Default handler for IPC methods not handled by DDF.
399 *
400 * @param fun Device function handling the call.
401 * @param icallid Call id.
402 * @param icall Call data.
403 */
[2ecb5ec]404void default_connection_handler(ddf_fun_t *fun,
405 ipc_callid_t icallid, ipc_call_t *icall)
406{
407 const sysarg_t method = IPC_GET_IMETHOD(*icall);
[56fd7cf]408 ps2_mouse_t *mouse = ddf_dev_data_get(ddf_fun_get_dev(fun));
[2ecb5ec]409
410 switch (method) {
411 /* This might be ugly but async_callback_receive_start makes no
412 * difference for incorrect call and malloc failure. */
413 case IPC_M_CONNECT_TO_ME: {
414 async_sess_t *sess =
415 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
416 /* Probably ENOMEM error, try again. */
417 if (sess == NULL) {
418 ddf_msg(LVL_WARN,
[e98fe28c]419 "Failed creating client callback session");
[2ecb5ec]420 async_answer_0(icallid, EAGAIN);
421 break;
422 }
[e98fe28c]423 if (mouse->client_sess == NULL) {
424 mouse->client_sess = sess;
425 ddf_msg(LVL_DEBUG, "Set client session");
[2ecb5ec]426 async_answer_0(icallid, EOK);
427 } else {
[e98fe28c]428 ddf_msg(LVL_ERROR, "Client session already set");
[2ecb5ec]429 async_answer_0(icallid, ELIMIT);
430 }
431 break;
432 }
433 default:
[9b56a8dd]434 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
[2ecb5ec]435 async_answer_0(icallid, EINVAL);
436 break;
437 }
438}
Note: See TracBrowser for help on using the repository browser.