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
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Jiri Svoboda
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 */
29/** @addtogroup ps2mouse
30 * @{
31 */
32/** @file
33 * @brief PS/2 mouse driver.
34 */
35
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>
44
45#include "ps2mouse.h"
46
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
52
53#define PS2_BUFSIZE 3
54#define INTELLIMOUSE_BUFSIZE 4
55
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 */
61
62#define BUTTON_LEFT 0
63#define BUTTON_RIGHT 1
64#define BUTTON_MIDDLE 2
65#define PS2_BUTTON_COUNT 3
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 */
70#define INTELLIMOUSE_BUTTON_COUNT 5
71
72#define PS2_BUTTON_MASK(button) (1 << button)
73
74#define MOUSE_READ_BYTE_TEST(mouse, value_) \
75do { \
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)
91
92#define MOUSE_WRITE_BYTE(mouse, value_) \
93do { \
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; \
101 } \
102} while (0)
103
104static errno_t polling_ps2(void *);
105static errno_t polling_intellimouse(void *);
106static errno_t probe_intellimouse(ps2_mouse_t *, bool);
107static void default_connection_handler(ddf_fun_t *, ipc_call_t *);
108
109/** ps/2 mouse driver ops. */
110static ddf_dev_ops_t mouse_ops = {
111 .default_handler = default_connection_handler
112};
113
114/** Initialize mouse driver structure.
115 *
116 * Connects to parent, creates keyboard function, starts polling fibril.
117 *
118 * @param kbd Mouse driver structure to initialize.
119 * @param dev DDF device structure.
120 *
121 * @return EOK on success or non-zero error code
122 */
123errno_t ps2_mouse_init(ps2_mouse_t *mouse, ddf_dev_t *dev)
124{
125 async_sess_t *parent_sess;
126 bool bound = false;
127 errno_t rc;
128
129 mouse->client_sess = NULL;
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 }
143
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;
149 }
150
151 ddf_fun_set_ops(mouse->mouse_fun, &mouse_ops);
152
153 rc = ddf_fun_bind(mouse->mouse_fun);
154 if (rc != EOK) {
155 ddf_msg(LVL_ERROR, "Failed binding mouse function.");
156 goto error;
157 }
158
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;
165 }
166
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
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.");
196 }
197
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;
205 }
206
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;
213 }
214
215 mouse->polling_fibril = fibril_create(polling_f, mouse);
216 if (mouse->polling_fibril == 0) {
217 rc = ENOMEM;
218 goto error;
219 }
220
221 fibril_add_ready(mouse->polling_fibril);
222 return EOK;
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;
234}
235
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 */
246static errno_t ps2_mouse_read_packet(ps2_mouse_t *mouse, void *pbuf, size_t psize)
247{
248 errno_t rc;
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,
255 &nread, chardev_f_none);
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
267/** Get data and parse ps2 protocol packets.
268 * @param arg Pointer to ps2_mouse_t structure.
269 * @return Never.
270 */
271errno_t polling_ps2(void *arg)
272{
273 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
274 errno_t rc;
275
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;
282
283 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
284 packet[0], packet[1], packet[2]);
285
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;
292 }
293
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]);
301 }
302 }
303
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);
312 }
313 async_exchange_end(exch);
314 }
315
316 return 0;
317}
318
319/** Get data and parse ps2 protocol with IntelliMouse extension packets.
320 * @param arg Pointer to ps2_mouse_t structure.
321 * @return Never.
322 */
323static errno_t polling_intellimouse(void *arg)
324{
325 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
326 errno_t rc;
327
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;
334
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 =
339 async_exchange_begin(mouse->client_sess);
340 if (!exch) {
341 ddf_msg(LVL_ERROR,
342 "Failed creating exchange.");
343 continue;
344 }
345
346 /* Buttons */
347 /*
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.
355 */
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];
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 }
388
389 return 0;
390}
391
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.
397 */
398static errno_t probe_intellimouse(ps2_mouse_t *mouse, bool buttons)
399{
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);
418
419 return EOK;
420}
421
422/** Default handler for IPC methods not handled by DDF.
423 *
424 * @param fun Device function handling the call.
425 * @param icall Call data.
426 *
427 */
428void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
429{
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;
433
434 switch (method) {
435 case IPC_M_CONNECT_TO_ME:
436 /*
437 * This might be ugly but async_callback_receive_start makes no
438 * difference for incorrect call and malloc failure.
439 */
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;
447 }
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);
455 }
456 break;
457 default:
458 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
459 async_answer_0(icall, EINVAL);
460 break;
461 }
462}
Note: See TracBrowser for help on using the repository browser.