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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c8ea6eca was c8ea6eca, checked in by Jakub Jermar <jakub@…>, 7 years ago

Improve doxygen documentation

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