source: mainline/uspace/drv/hid/ps2mouse/ps2mouse.c@ 123cd6d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 123cd6d was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

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