source: mainline/uspace/drv/char/ps2mouse/ps2mouse.c@ dcffe95

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dcffe95 was 68c60b0, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

ps2mouse: Lower testing message to debug instead of error.

If the test failure indicates error, there should be additional more elaborate error message

  • Property mode set to 100644
File size: 12.1 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/** @addtogroup drvmouse
29 * @{
30 */
31/** @file
32 * @brief ps2 mouse driver.
33 */
34
35#include <bool.h>
36#include <errno.h>
37#include <devman.h>
38#include <ddf/log.h>
39#include <io/keycode.h>
40#include <io/console.h>
41#include <ipc/mouseev.h>
42#include <abi/ipc/methods.h>
43
44#include "ps2mouse.h"
45#include "chardev.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(sess, value) \
74do { \
75 uint8_t data = 0; \
76 const ssize_t size = chardev_read(sess, &data, 1); \
77 if (size != 1) { \
78 ddf_msg(LVL_ERROR, "Failed reading byte: %d)", size);\
79 return size < 0 ? size : EIO; \
80 } \
81 if (data != (value)) { \
82 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
83 data, (value)); \
84 return EIO; \
85 } \
86} while (0)
87
88#define MOUSE_WRITE_BYTE(sess, value) \
89do { \
90 uint8_t data = (value); \
91 const ssize_t size = chardev_write(sess, &data, 1); \
92 if (size < 0 ) { \
93 ddf_msg(LVL_ERROR, "Failed writing byte: %hhx", value); \
94 return size; \
95 } \
96} while (0)
97
98static int polling_ps2(void *);
99static int polling_intellimouse(void *);
100static int probe_intellimouse(async_exch_t *, bool);
101static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
102
103/** ps/2 mouse driver ops. */
104static ddf_dev_ops_t mouse_ops = {
105 .default_handler = default_connection_handler
106};
107
108/** Initialize mouse driver structure.
109 * @param kbd Mouse driver structure to initialize.
110 * @param dev DDF device structure.
111 *
112 * Connects to parent, creates keyboard function, starts polling fibril.
113 */
114int ps2_mouse_init(ps2_mouse_t *mouse, ddf_dev_t *dev)
115{
116 assert(mouse);
117 assert(dev);
118 mouse->client_sess = NULL;
119 mouse->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
120 dev->handle, IPC_FLAG_BLOCKING);
121 if (!mouse->parent_sess)
122 return ENOMEM;
123
124 mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
125 if (!mouse->mouse_fun) {
126 async_hangup(mouse->parent_sess);
127 return ENOMEM;
128 }
129 mouse->mouse_fun->ops = &mouse_ops;
130 mouse->mouse_fun->driver_data = mouse;
131
132 int ret = ddf_fun_bind(mouse->mouse_fun);
133 if (ret != EOK) {
134 async_hangup(mouse->parent_sess);
135 mouse->mouse_fun->driver_data = NULL;
136 ddf_fun_destroy(mouse->mouse_fun);
137 return ENOMEM;
138 }
139
140 ret = ddf_fun_add_to_category(mouse->mouse_fun, "mouse");
141 if (ret != EOK) {
142 async_hangup(mouse->parent_sess);
143 ddf_fun_unbind(mouse->mouse_fun);
144 mouse->mouse_fun->driver_data = NULL;
145 ddf_fun_destroy(mouse->mouse_fun);
146 return ENOMEM;
147 }
148 /* Probe IntelliMouse extensions. */
149 int (*polling_f)(void*) = polling_ps2;
150 async_exch_t *exch = async_exchange_begin(mouse->parent_sess);
151 if (probe_intellimouse(exch, false) == EOK) {
152 ddf_msg(LVL_NOTE, "Enabled IntelliMouse extensions");
153 polling_f = polling_intellimouse;
154 if (probe_intellimouse(exch, true) == EOK)
155 ddf_msg(LVL_NOTE, "Enabled 4th and 5th button.");
156 }
157 /* Enable mouse data reporting. */
158 uint8_t report = PS2_MOUSE_ENABLE_DATA_REPORT;
159 ssize_t size = chardev_write(exch, &report, 1);
160 if (size != 1) {
161 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
162 async_exchange_end(exch);
163 async_hangup(mouse->parent_sess);
164 ddf_fun_unbind(mouse->mouse_fun);
165 mouse->mouse_fun->driver_data = NULL;
166 ddf_fun_destroy(mouse->mouse_fun);
167 return EIO;
168 }
169
170 size = chardev_read(exch, &report, 1);
171 async_exchange_end(exch);
172 if (size != 1 || report != PS2_MOUSE_ACK) {
173 ddf_msg(LVL_ERROR, "Failed to confirm data reporting: %hhx.",
174 report);
175 async_hangup(mouse->parent_sess);
176 ddf_fun_unbind(mouse->mouse_fun);
177 mouse->mouse_fun->driver_data = NULL;
178 ddf_fun_destroy(mouse->mouse_fun);
179 return EIO;
180 }
181
182 mouse->polling_fibril = fibril_create(polling_f, mouse);
183 if (!mouse->polling_fibril) {
184 async_hangup(mouse->parent_sess);
185 ddf_fun_unbind(mouse->mouse_fun);
186 mouse->mouse_fun->driver_data = NULL;
187 ddf_fun_destroy(mouse->mouse_fun);
188 return ENOMEM;
189 }
190 fibril_add_ready(mouse->polling_fibril);
191 return EOK;
192}
193
194/** Get data and parse ps2 protocol packets.
195 * @param arg Pointer to ps2_mouse_t structure.
196 * @return Never.
197 */
198int polling_ps2(void *arg)
199{
200 assert(arg);
201 const ps2_mouse_t *mouse = arg;
202
203 assert(mouse->parent_sess);
204 bool buttons[PS2_BUTTON_COUNT] = {};
205 async_exch_t *parent_exch = async_exchange_begin(mouse->parent_sess);
206 while (1) {
207
208 uint8_t packet[PS2_BUFSIZE] = {};
209 const ssize_t size =
210 chardev_read(parent_exch, packet, PS2_BUFSIZE);
211
212 if (size != PS2_BUFSIZE) {
213 ddf_msg(LVL_WARN, "Incorrect packet size: %zd.", size);
214 continue;
215 }
216 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
217 packet[0], packet[1], packet[2]);
218
219 async_exch_t *exch =
220 async_exchange_begin(mouse->client_sess);
221 if (!exch) {
222 ddf_msg(LVL_ERROR,
223 "Failed creating exchange.");
224 continue;
225 }
226
227 /* Buttons */
228 for (unsigned i = 0; i < PS2_BUTTON_COUNT; ++i) {
229 const bool status = (packet[0] & PS2_BUTTON_MASK(i));
230 if (buttons[i] != status) {
231 buttons[i] = status;
232 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
233 buttons[i]);
234 }
235 }
236
237 /* Movement */
238 const int16_t move_x =
239 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
240 const int16_t move_y =
241 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
242 //TODO: Consider overflow bit
243 if (move_x != 0 || move_y != 0) {
244 async_msg_2(exch, MOUSEEV_MOVE_EVENT, move_x, -move_y);
245 }
246 async_exchange_end(exch);
247 }
248 async_exchange_end(parent_exch);
249}
250
251/** Get data and parse ps2 protocol with IntelliMouse extension packets.
252 * @param arg Pointer to ps2_mouse_t structure.
253 * @return Never.
254 */
255static int polling_intellimouse(void *arg)
256{
257 assert(arg);
258 const ps2_mouse_t *mouse = arg;
259
260 assert(mouse->parent_sess);
261 bool buttons[INTELLIMOUSE_BUTTON_COUNT] = {};
262 async_exch_t *parent_exch = NULL;
263 while (1) {
264 if (!parent_exch)
265 parent_exch = async_exchange_begin(mouse->parent_sess);
266
267 uint8_t packet[INTELLIMOUSE_BUFSIZE] = {};
268 const ssize_t size = chardev_read(
269 parent_exch, packet, INTELLIMOUSE_BUFSIZE);
270
271 if (size != INTELLIMOUSE_BUFSIZE) {
272 ddf_msg(LVL_WARN, "Incorrect packet size: %zd.", size);
273 continue;
274 }
275 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx:%hhx.",
276 packet[0], packet[1], packet[2], packet[3]);
277
278 async_exch_t *exch =
279 async_exchange_begin(mouse->client_sess);
280 if (!exch) {
281 ddf_msg(LVL_ERROR,
282 "Failed creating exchange.");
283 continue;
284 }
285
286 /* Buttons */
287 /* NOTE: Parsing 4th and 5th button works even if this extension
288 * is not supported and whole 4th byte should be interpreted
289 * as Z-axis movement. the upper 4 bits are just a sign
290 * extension then. + sign is interpreted as "button up"
291 * (i.e no change since that is the default) and - sign fails
292 * the "imb" condition. Thus 4th and 5th buttons are never
293 * down on wheel only extension. */
294 const bool imb = (packet[3] & INTELLIMOUSE_ALWAYS_ZERO) == 0;
295 const bool status[] = {
296 [0] = packet[0] & PS2_BUTTON_MASK(0),
297 [1] = packet[0] & PS2_BUTTON_MASK(1),
298 [2] = packet[0] & PS2_BUTTON_MASK(2),
299 [3] = (packet[3] & INTELLIMOUSE_BUTTON_4) && imb,
300 [4] = (packet[3] & INTELLIMOUSE_BUTTON_5) && imb,
301 };
302 for (unsigned i = 0; i < INTELLIMOUSE_BUTTON_COUNT; ++i) {
303 if (buttons[i] != status[i]) {
304 buttons[i] = status[i];
305 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
306 buttons[i]);
307 }
308 }
309
310 /* Movement */
311 const int16_t move_x =
312 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
313 const int16_t move_y =
314 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
315 const int8_t move_z =
316 (((packet[3] & Z_SIGN) ? 0xf0 : 0) | (packet[3] & 0xf));
317 ddf_msg(LVL_DEBUG2, "Parsed moves: %d:%d:%hhd", move_x, move_y,
318 move_z);
319 //TODO: Consider overflow bit
320 if (move_x != 0 || move_y != 0 || move_z != 0) {
321 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
322 move_x, -move_y, -move_z);
323 }
324 async_exchange_end(exch);
325 }
326 async_exchange_end(parent_exch);
327}
328
329/** Send magic sequence to initialize IntelliMouse extensions.
330 * @param exch IPC exchange to the parent device.
331 * @param buttons True selects magic sequence for 4th and 5th button,
332 * false selects wheel support magic sequence.
333 * See http://www.computer-engineering.org/ps2mouse/ for details.
334 */
335static int probe_intellimouse(async_exch_t *exch, bool buttons)
336{
337 assert(exch);
338
339 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_SET_SAMPLE_RATE);
340 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
341 MOUSE_WRITE_BYTE(exch, 200);
342 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
343
344 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_SET_SAMPLE_RATE);
345 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
346 MOUSE_WRITE_BYTE(exch, buttons ? 200 : 100);
347 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
348
349 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_SET_SAMPLE_RATE);
350 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
351 MOUSE_WRITE_BYTE(exch, 80);
352 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
353
354 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_GET_DEVICE_ID);
355 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
356 MOUSE_READ_BYTE_TEST(exch, buttons ? 4 : 3);
357
358 return EOK;
359}
360
361/** Default handler for IPC methods not handled by DDF.
362 *
363 * @param fun Device function handling the call.
364 * @param icallid Call id.
365 * @param icall Call data.
366 */
367void default_connection_handler(ddf_fun_t *fun,
368 ipc_callid_t icallid, ipc_call_t *icall)
369{
370 if (fun == NULL || fun->driver_data == NULL) {
371 ddf_msg(LVL_ERROR, "%s: Missing parameter.", __FUNCTION__);
372 async_answer_0(icallid, EINVAL);
373 return;
374 }
375
376 const sysarg_t method = IPC_GET_IMETHOD(*icall);
377 ps2_mouse_t *mouse = fun->driver_data;
378
379 switch (method) {
380 /* This might be ugly but async_callback_receive_start makes no
381 * difference for incorrect call and malloc failure. */
382 case IPC_M_CONNECT_TO_ME: {
383 async_sess_t *sess =
384 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
385 /* Probably ENOMEM error, try again. */
386 if (sess == NULL) {
387 ddf_msg(LVL_WARN,
388 "Failed creating client callback session");
389 async_answer_0(icallid, EAGAIN);
390 break;
391 }
392 if (mouse->client_sess == NULL) {
393 mouse->client_sess = sess;
394 ddf_msg(LVL_DEBUG, "Set client session");
395 async_answer_0(icallid, EOK);
396 } else {
397 ddf_msg(LVL_ERROR, "Client session already set");
398 async_answer_0(icallid, ELIMIT);
399 }
400 break;
401 }
402 default:
403 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
404 async_answer_0(icallid, EINVAL);
405 break;
406 }
407}
Note: See TracBrowser for help on using the repository browser.