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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d87561c was d87561c, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

C style: type-casting etc.

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