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
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 drvmouse
30 * @{
31 */
32/** @file
33 * @brief PS/2 mouse driver.
34 */
35
36#include <stdbool.h>
37#include <errno.h>
38#include <ddf/log.h>
39#include <io/keycode.h>
40#include <io/chardev.h>
41#include <io/console.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_ACK 0xfa
51
52#define PS2_BUFSIZE 3
53#define INTELLIMOUSE_BUFSIZE 4
54
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 */
60
61#define BUTTON_LEFT 0
62#define BUTTON_RIGHT 1
63#define BUTTON_MIDDLE 2
64#define PS2_BUTTON_COUNT 3
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 */
69#define INTELLIMOUSE_BUTTON_COUNT 5
70
71#define PS2_BUTTON_MASK(button) (1 << button)
72
73#define MOUSE_READ_BYTE_TEST(mouse, value_) \
74do { \
75 uint8_t value = (value_); \
76 uint8_t data = 0; \
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; \
82 } \
83 if (data != value) { \
84 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
85 data, value); \
86 return EIO; \
87 } \
88} while (0)
89
90#define MOUSE_WRITE_BYTE(mouse, value_) \
91do { \
92 uint8_t value = (value_); \
93 uint8_t data = (value); \
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; \
99 } \
100} while (0)
101
102static int polling_ps2(void *);
103static int polling_intellimouse(void *);
104static int probe_intellimouse(ps2_mouse_t *, bool);
105static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
106
107/** ps/2 mouse driver ops. */
108static ddf_dev_ops_t mouse_ops = {
109 .default_handler = default_connection_handler
110};
111
112/** Initialize mouse driver structure.
113 *
114 * Connects to parent, creates keyboard function, starts polling fibril.
115 *
116 * @param kbd Mouse driver structure to initialize.
117 * @param dev DDF device structure.
118 *
119 * @return EOK on success or non-zero error code
120 */
121int ps2_mouse_init(ps2_mouse_t *mouse, ddf_dev_t *dev)
122{
123 async_sess_t *parent_sess;
124 bool bound = false;
125 int rc;
126
127 mouse->client_sess = NULL;
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 }
141
142 mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
143 if (mouse->mouse_fun == NULL) {
144 ddf_msg(LVL_ERROR, "Error creating mouse function.");
145 rc = ENOMEM;
146 goto error;
147 }
148
149 ddf_fun_set_ops(mouse->mouse_fun, &mouse_ops);
150
151 rc = ddf_fun_bind(mouse->mouse_fun);
152 if (rc != EOK) {
153 ddf_msg(LVL_ERROR, "Failed binding mouse function.");
154 goto error;
155 }
156
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;
163 }
164
165 /* Probe IntelliMouse extensions. */
166 int (*polling_f)(void*) = polling_ps2;
167 if (probe_intellimouse(mouse, false) == EOK) {
168 ddf_msg(LVL_NOTE, "Enabled IntelliMouse extensions");
169 polling_f = polling_intellimouse;
170 if (probe_intellimouse(mouse, true) == EOK)
171 ddf_msg(LVL_NOTE, "Enabled 4th and 5th button.");
172 }
173
174 /* Enable mouse data reporting. */
175 uint8_t report = PS2_MOUSE_ENABLE_DATA_REPORT;
176 size_t nwr;
177 rc = chardev_write(mouse->chardev, &report, 1, &nwr);
178 if (rc != EOK) {
179 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
180 rc = EIO;
181 goto error;
182 }
183
184 size_t nread;
185 rc = chardev_read(mouse->chardev, &report, 1, &nread);
186 if (rc != EOK || report != PS2_MOUSE_ACK) {
187 ddf_msg(LVL_ERROR, "Failed to confirm data reporting: %hhx.",
188 report);
189 rc = EIO;
190 goto error;
191 }
192
193 mouse->polling_fibril = fibril_create(polling_f, mouse);
194 if (mouse->polling_fibril == 0) {
195 rc = ENOMEM;
196 goto error;
197 }
198
199 fibril_add_ready(mouse->polling_fibril);
200 return EOK;
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;
212}
213
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
245/** Get data and parse ps2 protocol packets.
246 * @param arg Pointer to ps2_mouse_t structure.
247 * @return Never.
248 */
249int polling_ps2(void *arg)
250{
251 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
252 int rc;
253
254 bool buttons[PS2_BUTTON_COUNT] = {};
255 while (1) {
256 uint8_t packet[PS2_BUFSIZE] = {};
257 rc = ps2_mouse_read_packet(mouse, packet, PS2_BUFSIZE);
258 if (rc != EOK)
259 continue;
260
261 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
262 packet[0], packet[1], packet[2]);
263
264 async_exch_t *exch =
265 async_exchange_begin(mouse->client_sess);
266 if (!exch) {
267 ddf_msg(LVL_ERROR,
268 "Failed creating exchange.");
269 continue;
270 }
271
272 /* Buttons */
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;
277 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
278 buttons[i]);
279 }
280 }
281
282 /* Movement */
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
288 if (move_x != 0 || move_y != 0) {
289 async_msg_2(exch, MOUSEEV_MOVE_EVENT, move_x, -move_y);
290 }
291 async_exchange_end(exch);
292 }
293
294 return 0;
295}
296
297/** Get data and parse ps2 protocol with IntelliMouse extension packets.
298 * @param arg Pointer to ps2_mouse_t structure.
299 * @return Never.
300 */
301static int polling_intellimouse(void *arg)
302{
303 ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
304 int rc;
305
306 bool buttons[INTELLIMOUSE_BUTTON_COUNT] = {};
307 while (1) {
308 uint8_t packet[INTELLIMOUSE_BUFSIZE] = {};
309 rc = ps2_mouse_read_packet(mouse, packet, INTELLIMOUSE_BUFSIZE);
310 if (rc != EOK)
311 continue;
312
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 =
317 async_exchange_begin(mouse->client_sess);
318 if (!exch) {
319 ddf_msg(LVL_ERROR,
320 "Failed creating exchange.");
321 continue;
322 }
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];
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 }
364
365 return 0;
366}
367
368/** Send magic sequence to initialize IntelliMouse extensions.
369 * @param exch IPC exchange to the parent device.
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 */
374static int probe_intellimouse(ps2_mouse_t *mouse, bool buttons)
375{
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);
394
395 return EOK;
396}
397
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 */
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);
408 ps2_mouse_t *mouse = ddf_dev_data_get(ddf_fun_get_dev(fun));
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,
419 "Failed creating client callback session");
420 async_answer_0(icallid, EAGAIN);
421 break;
422 }
423 if (mouse->client_sess == NULL) {
424 mouse->client_sess = sess;
425 ddf_msg(LVL_DEBUG, "Set client session");
426 async_answer_0(icallid, EOK);
427 } else {
428 ddf_msg(LVL_ERROR, "Client session already set");
429 async_answer_0(icallid, ELIMIT);
430 }
431 break;
432 }
433 default:
434 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
435 async_answer_0(icallid, EINVAL);
436 break;
437 }
438}
Note: See TracBrowser for help on using the repository browser.