source: mainline/uspace/drv/char/ps2mouse/ps2mouse.c@ 48fa501

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 48fa501 was 3e6a98c5, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Standards-compliant boolean type.

  • Property mode set to 100644
File size: 11.5 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 <stdbool.h>
36#include <errno.h>
37#include <ddf/log.h>
38#include <io/keycode.h>
39#include <io/console.h>
40#include <ipc/mouseev.h>
41#include <abi/ipc/methods.h>
42
43#include "ps2mouse.h"
44#include "chardev.h"
45
46#define PS2_MOUSE_GET_DEVICE_ID 0xf2
47#define PS2_MOUSE_SET_SAMPLE_RATE 0xf3
48#define PS2_MOUSE_ENABLE_DATA_REPORT 0xf4
49#define PS2_MOUSE_ACK 0xfa
50
51#define PS2_BUFSIZE 3
52#define INTELLIMOUSE_BUFSIZE 4
53
54#define Z_SIGN (1 << 3) /* 4th byte */
55#define X_SIGN (1 << 4) /* 1st byte */
56#define Y_SIGN (1 << 5) /* 1st byte */
57#define X_OVERFLOW (1 << 6) /* 1st byte */
58#define Y_OVERFLOW (1 << 7) /* 1st byte */
59
60#define BUTTON_LEFT 0
61#define BUTTON_RIGHT 1
62#define BUTTON_MIDDLE 2
63#define PS2_BUTTON_COUNT 3
64
65#define INTELLIMOUSE_ALWAYS_ZERO (0xc0)
66#define INTELLIMOUSE_BUTTON_4 (1 << 4) /* 4th byte */
67#define INTELLIMOUSE_BUTTON_5 (1 << 5) /* 4th byte */
68#define INTELLIMOUSE_BUTTON_COUNT 5
69
70#define PS2_BUTTON_MASK(button) (1 << button)
71
72#define MOUSE_READ_BYTE_TEST(sess, value) \
73do { \
74 uint8_t data = 0; \
75 const ssize_t size = chardev_read(sess, &data, 1); \
76 if (size != 1) { \
77 ddf_msg(LVL_ERROR, "Failed reading byte: %zd)", size);\
78 return size < 0 ? size : EIO; \
79 } \
80 if (data != (value)) { \
81 ddf_msg(LVL_DEBUG, "Failed testing byte: got %hhx vs. %hhx)", \
82 data, (value)); \
83 return EIO; \
84 } \
85} while (0)
86
87#define MOUSE_WRITE_BYTE(sess, value) \
88do { \
89 uint8_t data = (value); \
90 const ssize_t size = chardev_write(sess, &data, 1); \
91 if (size < 0 ) { \
92 ddf_msg(LVL_ERROR, "Failed writing byte: %hhx", value); \
93 return size; \
94 } \
95} while (0)
96
97static int polling_ps2(void *);
98static int polling_intellimouse(void *);
99static int probe_intellimouse(async_exch_t *, bool);
100static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
101
102/** ps/2 mouse driver ops. */
103static ddf_dev_ops_t mouse_ops = {
104 .default_handler = default_connection_handler
105};
106
107/** Initialize mouse driver structure.
108 * @param kbd Mouse driver structure to initialize.
109 * @param dev DDF device structure.
110 *
111 * Connects to parent, creates keyboard function, starts polling fibril.
112 */
113int ps2_mouse_init(ps2_mouse_t *mouse, ddf_dev_t *dev)
114{
115 mouse->client_sess = NULL;
116 mouse->parent_sess = ddf_dev_parent_sess_create(dev, EXCHANGE_SERIALIZE);
117 if (!mouse->parent_sess)
118 return ENOMEM;
119
120 mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
121 if (!mouse->mouse_fun) {
122 return ENOMEM;
123 }
124 ddf_fun_set_ops(mouse->mouse_fun, &mouse_ops);
125
126 int ret = ddf_fun_bind(mouse->mouse_fun);
127 if (ret != EOK) {
128 ddf_fun_destroy(mouse->mouse_fun);
129 return ENOMEM;
130 }
131
132 ret = ddf_fun_add_to_category(mouse->mouse_fun, "mouse");
133 if (ret != EOK) {
134 ddf_fun_unbind(mouse->mouse_fun);
135 ddf_fun_destroy(mouse->mouse_fun);
136 return ENOMEM;
137 }
138 /* Probe IntelliMouse extensions. */
139 int (*polling_f)(void*) = polling_ps2;
140 async_exch_t *exch = async_exchange_begin(mouse->parent_sess);
141 if (probe_intellimouse(exch, false) == EOK) {
142 ddf_msg(LVL_NOTE, "Enabled IntelliMouse extensions");
143 polling_f = polling_intellimouse;
144 if (probe_intellimouse(exch, true) == EOK)
145 ddf_msg(LVL_NOTE, "Enabled 4th and 5th button.");
146 }
147 /* Enable mouse data reporting. */
148 uint8_t report = PS2_MOUSE_ENABLE_DATA_REPORT;
149 ssize_t size = chardev_write(exch, &report, 1);
150 if (size != 1) {
151 ddf_msg(LVL_ERROR, "Failed to enable data reporting.");
152 async_exchange_end(exch);
153 ddf_fun_unbind(mouse->mouse_fun);
154 ddf_fun_destroy(mouse->mouse_fun);
155 return EIO;
156 }
157
158 size = chardev_read(exch, &report, 1);
159 async_exchange_end(exch);
160 if (size != 1 || report != PS2_MOUSE_ACK) {
161 ddf_msg(LVL_ERROR, "Failed to confirm data reporting: %hhx.",
162 report);
163 ddf_fun_unbind(mouse->mouse_fun);
164 ddf_fun_destroy(mouse->mouse_fun);
165 return EIO;
166 }
167
168 mouse->polling_fibril = fibril_create(polling_f, mouse);
169 if (!mouse->polling_fibril) {
170 ddf_fun_unbind(mouse->mouse_fun);
171 ddf_fun_destroy(mouse->mouse_fun);
172 return ENOMEM;
173 }
174 fibril_add_ready(mouse->polling_fibril);
175 return EOK;
176}
177
178/** Get data and parse ps2 protocol packets.
179 * @param arg Pointer to ps2_mouse_t structure.
180 * @return Never.
181 */
182int polling_ps2(void *arg)
183{
184 assert(arg);
185 const ps2_mouse_t *mouse = arg;
186
187 assert(mouse->parent_sess);
188 bool buttons[PS2_BUTTON_COUNT] = {};
189 async_exch_t *parent_exch = async_exchange_begin(mouse->parent_sess);
190 while (1) {
191
192 uint8_t packet[PS2_BUFSIZE] = {};
193 const ssize_t size =
194 chardev_read(parent_exch, packet, PS2_BUFSIZE);
195
196 if (size != PS2_BUFSIZE) {
197 ddf_msg(LVL_WARN, "Incorrect packet size: %zd.", size);
198 continue;
199 }
200 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
201 packet[0], packet[1], packet[2]);
202
203 async_exch_t *exch =
204 async_exchange_begin(mouse->client_sess);
205 if (!exch) {
206 ddf_msg(LVL_ERROR,
207 "Failed creating exchange.");
208 continue;
209 }
210
211 /* Buttons */
212 for (unsigned i = 0; i < PS2_BUTTON_COUNT; ++i) {
213 const bool status = (packet[0] & PS2_BUTTON_MASK(i));
214 if (buttons[i] != status) {
215 buttons[i] = status;
216 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
217 buttons[i]);
218 }
219 }
220
221 /* Movement */
222 const int16_t move_x =
223 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
224 const int16_t move_y =
225 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
226 //TODO: Consider overflow bit
227 if (move_x != 0 || move_y != 0) {
228 async_msg_2(exch, MOUSEEV_MOVE_EVENT, move_x, -move_y);
229 }
230 async_exchange_end(exch);
231 }
232 async_exchange_end(parent_exch);
233}
234
235/** Get data and parse ps2 protocol with IntelliMouse extension packets.
236 * @param arg Pointer to ps2_mouse_t structure.
237 * @return Never.
238 */
239static int polling_intellimouse(void *arg)
240{
241 assert(arg);
242 const ps2_mouse_t *mouse = arg;
243
244 assert(mouse->parent_sess);
245 bool buttons[INTELLIMOUSE_BUTTON_COUNT] = {};
246 async_exch_t *parent_exch = NULL;
247 while (1) {
248 if (!parent_exch)
249 parent_exch = async_exchange_begin(mouse->parent_sess);
250
251 uint8_t packet[INTELLIMOUSE_BUFSIZE] = {};
252 const ssize_t size = chardev_read(
253 parent_exch, packet, INTELLIMOUSE_BUFSIZE);
254
255 if (size != INTELLIMOUSE_BUFSIZE) {
256 ddf_msg(LVL_WARN, "Incorrect packet size: %zd.", size);
257 continue;
258 }
259 ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx:%hhx.",
260 packet[0], packet[1], packet[2], packet[3]);
261
262 async_exch_t *exch =
263 async_exchange_begin(mouse->client_sess);
264 if (!exch) {
265 ddf_msg(LVL_ERROR,
266 "Failed creating exchange.");
267 continue;
268 }
269
270 /* Buttons */
271 /* NOTE: Parsing 4th and 5th button works even if this extension
272 * is not supported and whole 4th byte should be interpreted
273 * as Z-axis movement. the upper 4 bits are just a sign
274 * extension then. + sign is interpreted as "button up"
275 * (i.e no change since that is the default) and - sign fails
276 * the "imb" condition. Thus 4th and 5th buttons are never
277 * down on wheel only extension. */
278 const bool imb = (packet[3] & INTELLIMOUSE_ALWAYS_ZERO) == 0;
279 const bool status[] = {
280 [0] = packet[0] & PS2_BUTTON_MASK(0),
281 [1] = packet[0] & PS2_BUTTON_MASK(1),
282 [2] = packet[0] & PS2_BUTTON_MASK(2),
283 [3] = (packet[3] & INTELLIMOUSE_BUTTON_4) && imb,
284 [4] = (packet[3] & INTELLIMOUSE_BUTTON_5) && imb,
285 };
286 for (unsigned i = 0; i < INTELLIMOUSE_BUTTON_COUNT; ++i) {
287 if (buttons[i] != status[i]) {
288 buttons[i] = status[i];
289 async_msg_2(exch, MOUSEEV_BUTTON_EVENT, i + 1,
290 buttons[i]);
291 }
292 }
293
294 /* Movement */
295 const int16_t move_x =
296 ((packet[0] & X_SIGN) ? 0xff00 : 0) | packet[1];
297 const int16_t move_y =
298 (((packet[0] & Y_SIGN) ? 0xff00 : 0) | packet[2]);
299 const int8_t move_z =
300 (((packet[3] & Z_SIGN) ? 0xf0 : 0) | (packet[3] & 0xf));
301 ddf_msg(LVL_DEBUG2, "Parsed moves: %d:%d:%hhd", move_x, move_y,
302 move_z);
303 //TODO: Consider overflow bit
304 if (move_x != 0 || move_y != 0 || move_z != 0) {
305 async_msg_3(exch, MOUSEEV_MOVE_EVENT,
306 move_x, -move_y, -move_z);
307 }
308 async_exchange_end(exch);
309 }
310 async_exchange_end(parent_exch);
311}
312
313/** Send magic sequence to initialize IntelliMouse extensions.
314 * @param exch IPC exchange to the parent device.
315 * @param buttons True selects magic sequence for 4th and 5th button,
316 * false selects wheel support magic sequence.
317 * See http://www.computer-engineering.org/ps2mouse/ for details.
318 */
319static int probe_intellimouse(async_exch_t *exch, bool buttons)
320{
321 assert(exch);
322
323 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_SET_SAMPLE_RATE);
324 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
325 MOUSE_WRITE_BYTE(exch, 200);
326 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
327
328 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_SET_SAMPLE_RATE);
329 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
330 MOUSE_WRITE_BYTE(exch, buttons ? 200 : 100);
331 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
332
333 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_SET_SAMPLE_RATE);
334 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
335 MOUSE_WRITE_BYTE(exch, 80);
336 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
337
338 MOUSE_WRITE_BYTE(exch, PS2_MOUSE_GET_DEVICE_ID);
339 MOUSE_READ_BYTE_TEST(exch, PS2_MOUSE_ACK);
340 MOUSE_READ_BYTE_TEST(exch, buttons ? 4 : 3);
341
342 return EOK;
343}
344
345/** Default handler for IPC methods not handled by DDF.
346 *
347 * @param fun Device function handling the call.
348 * @param icallid Call id.
349 * @param icall Call data.
350 */
351void default_connection_handler(ddf_fun_t *fun,
352 ipc_callid_t icallid, ipc_call_t *icall)
353{
354 const sysarg_t method = IPC_GET_IMETHOD(*icall);
355 ps2_mouse_t *mouse = ddf_dev_data_get(ddf_fun_get_dev(fun));
356
357 switch (method) {
358 /* This might be ugly but async_callback_receive_start makes no
359 * difference for incorrect call and malloc failure. */
360 case IPC_M_CONNECT_TO_ME: {
361 async_sess_t *sess =
362 async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
363 /* Probably ENOMEM error, try again. */
364 if (sess == NULL) {
365 ddf_msg(LVL_WARN,
366 "Failed creating client callback session");
367 async_answer_0(icallid, EAGAIN);
368 break;
369 }
370 if (mouse->client_sess == NULL) {
371 mouse->client_sess = sess;
372 ddf_msg(LVL_DEBUG, "Set client session");
373 async_answer_0(icallid, EOK);
374 } else {
375 ddf_msg(LVL_ERROR, "Client session already set");
376 async_answer_0(icallid, ELIMIT);
377 }
378 break;
379 }
380 default:
381 ddf_msg(LVL_ERROR, "Unknown method: %d.", (int)method);
382 async_answer_0(icallid, EINVAL);
383 break;
384 }
385}
Note: See TracBrowser for help on using the repository browser.