1 | /*
|
---|
2 | * Copyright (c) 2006 Josef Cejka
|
---|
3 | * Copyright (c) 2011 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 |
|
---|
30 | /**
|
---|
31 | * @addtogroup inputgen generic
|
---|
32 | * @brief HelenOS input server.
|
---|
33 | * @ingroup input
|
---|
34 | * @{
|
---|
35 | */
|
---|
36 | /** @file
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <adt/list.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <fibril_synch.h>
|
---|
42 | #include <ipc/services.h>
|
---|
43 | #include <ipc/input.h>
|
---|
44 | #include <config.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <ns.h>
|
---|
48 | #include <async.h>
|
---|
49 | #include <errno.h>
|
---|
50 | #include <adt/fifo.h>
|
---|
51 | #include <io/console.h>
|
---|
52 | #include <io/keycode.h>
|
---|
53 | #include <loc.h>
|
---|
54 | #include <str_error.h>
|
---|
55 | #include <char_dev_iface.h>
|
---|
56 | #include <fibril.h>
|
---|
57 | #include "layout.h"
|
---|
58 | #include "kbd.h"
|
---|
59 | #include "kbd_port.h"
|
---|
60 | #include "kbd_ctl.h"
|
---|
61 | #include "mouse.h"
|
---|
62 | #include "mouse_proto.h"
|
---|
63 | #include "serial.h"
|
---|
64 | #include "input.h"
|
---|
65 |
|
---|
66 | #define NUM_LAYOUTS 4
|
---|
67 |
|
---|
68 | static layout_ops_t *layout[NUM_LAYOUTS] = {
|
---|
69 | &us_qwerty_ops,
|
---|
70 | &us_dvorak_ops,
|
---|
71 | &cz_ops,
|
---|
72 | &ar_ops
|
---|
73 | };
|
---|
74 |
|
---|
75 | typedef struct {
|
---|
76 | /** Link into the list of clients */
|
---|
77 | link_t link;
|
---|
78 |
|
---|
79 | /** Indicate whether the client is active */
|
---|
80 | bool active;
|
---|
81 |
|
---|
82 | /** Client callback session */
|
---|
83 | async_sess_t *sess;
|
---|
84 | } client_t;
|
---|
85 |
|
---|
86 | /** List of clients */
|
---|
87 | static list_t clients;
|
---|
88 | static client_t *active_client = NULL;
|
---|
89 |
|
---|
90 | /** Kernel override */
|
---|
91 | static bool active = true;
|
---|
92 |
|
---|
93 | /** Serial console specified by the user */
|
---|
94 | static char *serial_console;
|
---|
95 |
|
---|
96 | /** List of keyboard devices */
|
---|
97 | static list_t kbd_devs;
|
---|
98 |
|
---|
99 | /** List of mouse devices */
|
---|
100 | static list_t mouse_devs;
|
---|
101 |
|
---|
102 | /** List of serial devices */
|
---|
103 | static list_t serial_devs;
|
---|
104 |
|
---|
105 | static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
|
---|
106 |
|
---|
107 | static void *client_data_create(void)
|
---|
108 | {
|
---|
109 | client_t *client = (client_t *) calloc(1, sizeof(client_t));
|
---|
110 | if (client == NULL)
|
---|
111 | return NULL;
|
---|
112 |
|
---|
113 | link_initialize(&client->link);
|
---|
114 | client->active = false;
|
---|
115 | client->sess = NULL;
|
---|
116 |
|
---|
117 | list_append(&client->link, &clients);
|
---|
118 |
|
---|
119 | return client;
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void client_data_destroy(void *data)
|
---|
123 | {
|
---|
124 | client_t *client = (client_t *) data;
|
---|
125 |
|
---|
126 | list_remove(&client->link);
|
---|
127 | free(client);
|
---|
128 | }
|
---|
129 |
|
---|
130 | void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
|
---|
131 | {
|
---|
132 | (*kdev->ctl_ops->parse)(data);
|
---|
133 | }
|
---|
134 |
|
---|
135 | void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
|
---|
136 | {
|
---|
137 | (*mdev->proto_ops->parse)(data);
|
---|
138 | }
|
---|
139 |
|
---|
140 | void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
|
---|
141 | {
|
---|
142 | kbd_event_t ev;
|
---|
143 | unsigned int mod_mask;
|
---|
144 |
|
---|
145 | switch (key) {
|
---|
146 | case KC_LCTRL:
|
---|
147 | mod_mask = KM_LCTRL;
|
---|
148 | break;
|
---|
149 | case KC_RCTRL:
|
---|
150 | mod_mask = KM_RCTRL;
|
---|
151 | break;
|
---|
152 | case KC_LSHIFT:
|
---|
153 | mod_mask = KM_LSHIFT;
|
---|
154 | break;
|
---|
155 | case KC_RSHIFT:
|
---|
156 | mod_mask = KM_RSHIFT;
|
---|
157 | break;
|
---|
158 | case KC_LALT:
|
---|
159 | mod_mask = KM_LALT;
|
---|
160 | break;
|
---|
161 | case KC_RALT:
|
---|
162 | mod_mask = KM_RALT;
|
---|
163 | break;
|
---|
164 | default:
|
---|
165 | mod_mask = 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (mod_mask != 0) {
|
---|
169 | if (type == KEY_PRESS)
|
---|
170 | kdev->mods = kdev->mods | mod_mask;
|
---|
171 | else
|
---|
172 | kdev->mods = kdev->mods & ~mod_mask;
|
---|
173 | }
|
---|
174 |
|
---|
175 | switch (key) {
|
---|
176 | case KC_CAPS_LOCK:
|
---|
177 | mod_mask = KM_CAPS_LOCK;
|
---|
178 | break;
|
---|
179 | case KC_NUM_LOCK:
|
---|
180 | mod_mask = KM_NUM_LOCK;
|
---|
181 | break;
|
---|
182 | case KC_SCROLL_LOCK:
|
---|
183 | mod_mask = KM_SCROLL_LOCK;
|
---|
184 | break;
|
---|
185 | default:
|
---|
186 | mod_mask = 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (mod_mask != 0) {
|
---|
190 | if (type == KEY_PRESS) {
|
---|
191 | /*
|
---|
192 | * Only change lock state on transition from released
|
---|
193 | * to pressed. This prevents autorepeat from messing
|
---|
194 | * up the lock state.
|
---|
195 | */
|
---|
196 | kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
|
---|
197 | kdev->lock_keys = kdev->lock_keys | mod_mask;
|
---|
198 |
|
---|
199 | /* Update keyboard lock indicator lights. */
|
---|
200 | (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
|
---|
201 | } else {
|
---|
202 | kdev->lock_keys = kdev->lock_keys & ~mod_mask;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | // TODO: More elegant layout switching
|
---|
207 |
|
---|
208 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
209 | (key == KC_F1)) {
|
---|
210 | layout_destroy(kdev->active_layout);
|
---|
211 | kdev->active_layout = layout_create(layout[0]);
|
---|
212 | return;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
216 | (key == KC_F2)) {
|
---|
217 | layout_destroy(kdev->active_layout);
|
---|
218 | kdev->active_layout = layout_create(layout[1]);
|
---|
219 | return;
|
---|
220 | }
|
---|
221 |
|
---|
222 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
223 | (key == KC_F3)) {
|
---|
224 | layout_destroy(kdev->active_layout);
|
---|
225 | kdev->active_layout = layout_create(layout[2]);
|
---|
226 | return;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) &&
|
---|
230 | (key == KC_F4)) {
|
---|
231 | layout_destroy(kdev->active_layout);
|
---|
232 | kdev->active_layout = layout_create(layout[3]);
|
---|
233 | return;
|
---|
234 | }
|
---|
235 |
|
---|
236 | ev.type = type;
|
---|
237 | ev.key = key;
|
---|
238 | ev.mods = kdev->mods;
|
---|
239 |
|
---|
240 | ev.c = layout_parse_ev(kdev->active_layout, &ev);
|
---|
241 |
|
---|
242 | list_foreach(clients, link, client_t, client) {
|
---|
243 | if (client->active) {
|
---|
244 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
245 | async_msg_4(exch, INPUT_EVENT_KEY, ev.type, ev.key, ev.mods, ev.c);
|
---|
246 | async_exchange_end(exch);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Mouse pointer has moved (relative mode). */
|
---|
252 | void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
|
---|
253 | {
|
---|
254 | list_foreach(clients, link, client_t, client) {
|
---|
255 | if (client->active) {
|
---|
256 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
257 |
|
---|
258 | if ((dx) || (dy))
|
---|
259 | async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
|
---|
260 |
|
---|
261 | if (dz) {
|
---|
262 | // TODO: Implement proper wheel support
|
---|
263 | keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
|
---|
264 |
|
---|
265 | for (unsigned int i = 0; i < 3; i++)
|
---|
266 | async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
|
---|
267 |
|
---|
268 | async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
|
---|
269 | }
|
---|
270 |
|
---|
271 | async_exchange_end(exch);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | /** Mouse pointer has moved (absolute mode). */
|
---|
277 | void mouse_push_event_abs_move(mouse_dev_t *mdev, unsigned int x, unsigned int y,
|
---|
278 | unsigned int max_x, unsigned int max_y)
|
---|
279 | {
|
---|
280 | list_foreach(clients, link, client_t, client) {
|
---|
281 | if (client->active) {
|
---|
282 | if ((max_x) && (max_y)) {
|
---|
283 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
284 | async_msg_4(exch, INPUT_EVENT_ABS_MOVE, x, y, max_x, max_y);
|
---|
285 | async_exchange_end(exch);
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | /** Mouse button has been pressed. */
|
---|
292 | void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
|
---|
293 | {
|
---|
294 | list_foreach(clients, link, client_t, client) {
|
---|
295 | if (client->active) {
|
---|
296 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
297 | async_msg_2(exch, INPUT_EVENT_BUTTON, bnum, press);
|
---|
298 | async_exchange_end(exch);
|
---|
299 | }
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | /** Arbitrate client actiovation */
|
---|
304 | static void client_arbitration(void)
|
---|
305 | {
|
---|
306 | /* Mutual exclusion of active clients */
|
---|
307 | list_foreach(clients, link, client_t, client)
|
---|
308 | client->active = ((active) && (client == active_client));
|
---|
309 |
|
---|
310 | /* Notify clients about the arbitration */
|
---|
311 | list_foreach(clients, link, client_t, client) {
|
---|
312 | async_exch_t *exch = async_exchange_begin(client->sess);
|
---|
313 | async_msg_0(exch, client->active ?
|
---|
314 | INPUT_EVENT_ACTIVE : INPUT_EVENT_DEACTIVE);
|
---|
315 | async_exchange_end(exch);
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | /** New client connection */
|
---|
320 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
321 | {
|
---|
322 | client_t *client = (client_t *) async_get_client_data();
|
---|
323 | if (client == NULL) {
|
---|
324 | async_answer_0(iid, ENOMEM);
|
---|
325 | return;
|
---|
326 | }
|
---|
327 |
|
---|
328 | async_answer_0(iid, EOK);
|
---|
329 |
|
---|
330 | while (true) {
|
---|
331 | ipc_call_t call;
|
---|
332 | ipc_callid_t callid = async_get_call(&call);
|
---|
333 |
|
---|
334 | if (!IPC_GET_IMETHOD(call)) {
|
---|
335 | if (client->sess != NULL) {
|
---|
336 | async_hangup(client->sess);
|
---|
337 | client->sess = NULL;
|
---|
338 | }
|
---|
339 |
|
---|
340 | async_answer_0(callid, EOK);
|
---|
341 | return;
|
---|
342 | }
|
---|
343 |
|
---|
344 | async_sess_t *sess =
|
---|
345 | async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
|
---|
346 | if (sess != NULL) {
|
---|
347 | if (client->sess == NULL) {
|
---|
348 | client->sess = sess;
|
---|
349 | async_answer_0(callid, EOK);
|
---|
350 | } else
|
---|
351 | async_answer_0(callid, ELIMIT);
|
---|
352 | } else {
|
---|
353 | switch (IPC_GET_IMETHOD(call)) {
|
---|
354 | case INPUT_ACTIVATE:
|
---|
355 | active_client = client;
|
---|
356 | client_arbitration();
|
---|
357 | async_answer_0(callid, EOK);
|
---|
358 | break;
|
---|
359 | default:
|
---|
360 | async_answer_0(callid, EINVAL);
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | static void kconsole_event_handler(ipc_callid_t callid, ipc_call_t *call,
|
---|
367 | void *arg)
|
---|
368 | {
|
---|
369 | if (IPC_GET_ARG1(*call)) {
|
---|
370 | /* Kernel console activated */
|
---|
371 | active = false;
|
---|
372 | } else {
|
---|
373 | /* Kernel console deactivated */
|
---|
374 | active = true;
|
---|
375 | }
|
---|
376 |
|
---|
377 | client_arbitration();
|
---|
378 | }
|
---|
379 |
|
---|
380 | static kbd_dev_t *kbd_dev_new(void)
|
---|
381 | {
|
---|
382 | kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
|
---|
383 | if (kdev == NULL) {
|
---|
384 | printf("%s: Error allocating keyboard device. "
|
---|
385 | "Out of memory.\n", NAME);
|
---|
386 | return NULL;
|
---|
387 | }
|
---|
388 |
|
---|
389 | link_initialize(&kdev->link);
|
---|
390 |
|
---|
391 | kdev->mods = KM_NUM_LOCK;
|
---|
392 | kdev->lock_keys = 0;
|
---|
393 | kdev->active_layout = layout_create(layout[0]);
|
---|
394 |
|
---|
395 | return kdev;
|
---|
396 | }
|
---|
397 |
|
---|
398 | static mouse_dev_t *mouse_dev_new(void)
|
---|
399 | {
|
---|
400 | mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
|
---|
401 | if (mdev == NULL) {
|
---|
402 | printf("%s: Error allocating mouse device. "
|
---|
403 | "Out of memory.\n", NAME);
|
---|
404 | return NULL;
|
---|
405 | }
|
---|
406 |
|
---|
407 | link_initialize(&mdev->link);
|
---|
408 |
|
---|
409 | return mdev;
|
---|
410 | }
|
---|
411 |
|
---|
412 | static serial_dev_t *serial_dev_new(void)
|
---|
413 | {
|
---|
414 | serial_dev_t *sdev = calloc(1, sizeof(serial_dev_t));
|
---|
415 | if (sdev == NULL) {
|
---|
416 | printf("%s: Error allocating serial device. "
|
---|
417 | "Out of memory.\n", NAME);
|
---|
418 | return NULL;
|
---|
419 | }
|
---|
420 |
|
---|
421 | sdev->kdev = kbd_dev_new();
|
---|
422 | if (sdev->kdev == NULL) {
|
---|
423 | free(sdev);
|
---|
424 | return NULL;
|
---|
425 | }
|
---|
426 |
|
---|
427 | link_initialize(&sdev->link);
|
---|
428 |
|
---|
429 | return sdev;
|
---|
430 | }
|
---|
431 |
|
---|
432 | /** Add new legacy keyboard device. */
|
---|
433 | static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
|
---|
434 | {
|
---|
435 | kbd_dev_t *kdev = kbd_dev_new();
|
---|
436 | if (kdev == NULL)
|
---|
437 | return;
|
---|
438 |
|
---|
439 | kdev->port_ops = port;
|
---|
440 | kdev->ctl_ops = ctl;
|
---|
441 | kdev->svc_id = 0;
|
---|
442 |
|
---|
443 | /* Initialize port driver. */
|
---|
444 | if ((*kdev->port_ops->init)(kdev) != 0)
|
---|
445 | goto fail;
|
---|
446 |
|
---|
447 | /* Initialize controller driver. */
|
---|
448 | if ((*kdev->ctl_ops->init)(kdev) != 0) {
|
---|
449 | /* XXX Uninit port */
|
---|
450 | goto fail;
|
---|
451 | }
|
---|
452 |
|
---|
453 | list_append(&kdev->link, &kbd_devs);
|
---|
454 | return;
|
---|
455 |
|
---|
456 | fail:
|
---|
457 | free(kdev);
|
---|
458 | }
|
---|
459 |
|
---|
460 | /** Add new legacy mouse device. */
|
---|
461 | static void mouse_add_dev(mouse_port_ops_t *port, mouse_proto_ops_t *proto)
|
---|
462 | {
|
---|
463 | mouse_dev_t *mdev = mouse_dev_new();
|
---|
464 | if (mdev == NULL)
|
---|
465 | return;
|
---|
466 |
|
---|
467 | mdev->port_ops = port;
|
---|
468 | mdev->proto_ops = proto;
|
---|
469 | mdev->svc_id = 0;
|
---|
470 |
|
---|
471 | /* Initialize port driver. */
|
---|
472 | if ((*mdev->port_ops->init)(mdev) != 0)
|
---|
473 | goto fail;
|
---|
474 |
|
---|
475 | /* Initialize protocol driver. */
|
---|
476 | if ((*mdev->proto_ops->init)(mdev) != 0) {
|
---|
477 | /* XXX Uninit port */
|
---|
478 | goto fail;
|
---|
479 | }
|
---|
480 |
|
---|
481 | list_append(&mdev->link, &mouse_devs);
|
---|
482 | return;
|
---|
483 |
|
---|
484 | fail:
|
---|
485 | free(mdev);
|
---|
486 | }
|
---|
487 |
|
---|
488 | /** Add new kbdev device.
|
---|
489 | *
|
---|
490 | * @param service_id Service ID of the keyboard device
|
---|
491 | *
|
---|
492 | */
|
---|
493 | static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
|
---|
494 | {
|
---|
495 | kbd_dev_t *kdev = kbd_dev_new();
|
---|
496 | if (kdev == NULL)
|
---|
497 | return -1;
|
---|
498 |
|
---|
499 | kdev->svc_id = service_id;
|
---|
500 | kdev->port_ops = NULL;
|
---|
501 | kdev->ctl_ops = &kbdev_ctl;
|
---|
502 |
|
---|
503 | int rc = loc_service_get_name(service_id, &kdev->svc_name);
|
---|
504 | if (rc != EOK) {
|
---|
505 | kdev->svc_name = NULL;
|
---|
506 | goto fail;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /* Initialize controller driver. */
|
---|
510 | if ((*kdev->ctl_ops->init)(kdev) != 0) {
|
---|
511 | goto fail;
|
---|
512 | }
|
---|
513 |
|
---|
514 | list_append(&kdev->link, &kbd_devs);
|
---|
515 | *kdevp = kdev;
|
---|
516 | return EOK;
|
---|
517 |
|
---|
518 | fail:
|
---|
519 | if (kdev->svc_name != NULL)
|
---|
520 | free(kdev->svc_name);
|
---|
521 | free(kdev);
|
---|
522 | return -1;
|
---|
523 | }
|
---|
524 |
|
---|
525 | /** Add new mousedev device.
|
---|
526 | *
|
---|
527 | * @param service_id Service ID of the mouse device
|
---|
528 | *
|
---|
529 | */
|
---|
530 | static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
|
---|
531 | {
|
---|
532 | mouse_dev_t *mdev = mouse_dev_new();
|
---|
533 | if (mdev == NULL)
|
---|
534 | return -1;
|
---|
535 |
|
---|
536 | mdev->svc_id = service_id;
|
---|
537 | mdev->port_ops = NULL;
|
---|
538 | mdev->proto_ops = &mousedev_proto;
|
---|
539 |
|
---|
540 | int rc = loc_service_get_name(service_id, &mdev->svc_name);
|
---|
541 | if (rc != EOK) {
|
---|
542 | mdev->svc_name = NULL;
|
---|
543 | goto fail;
|
---|
544 | }
|
---|
545 |
|
---|
546 | /* Initialize controller driver. */
|
---|
547 | if ((*mdev->proto_ops->init)(mdev) != 0) {
|
---|
548 | goto fail;
|
---|
549 | }
|
---|
550 |
|
---|
551 | list_append(&mdev->link, &mouse_devs);
|
---|
552 | *mdevp = mdev;
|
---|
553 | return EOK;
|
---|
554 |
|
---|
555 | fail:
|
---|
556 | free(mdev);
|
---|
557 | return -1;
|
---|
558 | }
|
---|
559 |
|
---|
560 | static int serial_consumer(void *arg)
|
---|
561 | {
|
---|
562 | serial_dev_t *sdev = (serial_dev_t *) arg;
|
---|
563 |
|
---|
564 | while (true) {
|
---|
565 | uint8_t data;
|
---|
566 |
|
---|
567 | char_dev_read(sdev->sess, &data, sizeof(data));
|
---|
568 | kbd_push_data(sdev->kdev, data);
|
---|
569 | }
|
---|
570 |
|
---|
571 | return EOK;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /** Add new serial console device.
|
---|
575 | *
|
---|
576 | * @param service_id Service ID of the chardev device
|
---|
577 | *
|
---|
578 | */
|
---|
579 | static int serial_add_srldev(service_id_t service_id, serial_dev_t **sdevp)
|
---|
580 | {
|
---|
581 | bool match = false;
|
---|
582 |
|
---|
583 | serial_dev_t *sdev = serial_dev_new();
|
---|
584 | if (sdev == NULL)
|
---|
585 | return -1;
|
---|
586 |
|
---|
587 | sdev->kdev->svc_id = service_id;
|
---|
588 |
|
---|
589 | int rc = loc_service_get_name(service_id, &sdev->kdev->svc_name);
|
---|
590 | if (rc != EOK)
|
---|
591 | goto fail;
|
---|
592 |
|
---|
593 | list_append(&sdev->link, &serial_devs);
|
---|
594 |
|
---|
595 | /*
|
---|
596 | * Is this the device the user wants to use as a serial console?
|
---|
597 | */
|
---|
598 | match = (serial_console != NULL) &&
|
---|
599 | !str_cmp(serial_console, sdev->kdev->svc_name);
|
---|
600 |
|
---|
601 | if (match) {
|
---|
602 | sdev->kdev->ctl_ops = &stty_ctl;
|
---|
603 |
|
---|
604 | /* Initialize controller driver. */
|
---|
605 | if ((*sdev->kdev->ctl_ops->init)(sdev->kdev) != 0) {
|
---|
606 | list_remove(&sdev->link);
|
---|
607 | goto fail;
|
---|
608 | }
|
---|
609 |
|
---|
610 | sdev->sess = loc_service_connect(service_id, INTERFACE_DDF,
|
---|
611 | IPC_FLAG_BLOCKING);
|
---|
612 |
|
---|
613 | fid_t fid = fibril_create(serial_consumer, sdev);
|
---|
614 | fibril_add_ready(fid);
|
---|
615 | }
|
---|
616 |
|
---|
617 | *sdevp = sdev;
|
---|
618 | return EOK;
|
---|
619 |
|
---|
620 | fail:
|
---|
621 | if (sdev->kdev->svc_name != NULL)
|
---|
622 | free(sdev->kdev->svc_name);
|
---|
623 | free(sdev->kdev);
|
---|
624 | free(sdev);
|
---|
625 | return -1;
|
---|
626 | }
|
---|
627 |
|
---|
628 |
|
---|
629 | /** Add legacy drivers/devices. */
|
---|
630 | static void kbd_add_legacy_devs(void)
|
---|
631 | {
|
---|
632 | /*
|
---|
633 | * Need to add these drivers based on config unless we can probe
|
---|
634 | * them automatically.
|
---|
635 | */
|
---|
636 | #if defined(UARCH_arm32) && defined(MACHINE_gta02)
|
---|
637 | kbd_add_dev(&chardev_port, &stty_ctl);
|
---|
638 | #endif
|
---|
639 | #if defined(UARCH_ia64) && defined(MACHINE_ski)
|
---|
640 | kbd_add_dev(&chardev_port, &stty_ctl);
|
---|
641 | #endif
|
---|
642 | #if defined(MACHINE_msim)
|
---|
643 | kbd_add_dev(&msim_port, &stty_ctl);
|
---|
644 | #endif
|
---|
645 | #if defined(UARCH_ppc32)
|
---|
646 | kbd_add_dev(&adb_port, &apple_ctl);
|
---|
647 | #endif
|
---|
648 | #if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
|
---|
649 | kbd_add_dev(&niagara_port, &stty_ctl);
|
---|
650 | #endif
|
---|
651 | /* Silence warning on abs32le about kbd_add_dev() being unused */
|
---|
652 | (void) kbd_add_dev;
|
---|
653 | }
|
---|
654 |
|
---|
655 | /** Add legacy drivers/devices. */
|
---|
656 | static void mouse_add_legacy_devs(void)
|
---|
657 | {
|
---|
658 | /*
|
---|
659 | * Need to add these drivers based on config unless we can probe
|
---|
660 | * them automatically.
|
---|
661 | */
|
---|
662 | #if defined(UARCH_ppc32)
|
---|
663 | mouse_add_dev(&adb_mouse_port, &adb_proto);
|
---|
664 | #endif
|
---|
665 | /* Silence warning on abs32le about mouse_add_dev() being unused */
|
---|
666 | (void) mouse_add_dev;
|
---|
667 | }
|
---|
668 |
|
---|
669 | static int dev_check_new_kbdevs(void)
|
---|
670 | {
|
---|
671 | category_id_t keyboard_cat;
|
---|
672 | service_id_t *svcs;
|
---|
673 | size_t count, i;
|
---|
674 | bool already_known;
|
---|
675 | int rc;
|
---|
676 |
|
---|
677 | rc = loc_category_get_id("keyboard", &keyboard_cat, IPC_FLAG_BLOCKING);
|
---|
678 | if (rc != EOK) {
|
---|
679 | printf("%s: Failed resolving category 'keyboard'.\n", NAME);
|
---|
680 | return ENOENT;
|
---|
681 | }
|
---|
682 |
|
---|
683 | /*
|
---|
684 | * Check for new keyboard devices
|
---|
685 | */
|
---|
686 | rc = loc_category_get_svcs(keyboard_cat, &svcs, &count);
|
---|
687 | if (rc != EOK) {
|
---|
688 | printf("%s: Failed getting list of keyboard devices.\n",
|
---|
689 | NAME);
|
---|
690 | return EIO;
|
---|
691 | }
|
---|
692 |
|
---|
693 | for (i = 0; i < count; i++) {
|
---|
694 | already_known = false;
|
---|
695 |
|
---|
696 | /* Determine whether we already know this device. */
|
---|
697 | list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
|
---|
698 | if (kdev->svc_id == svcs[i]) {
|
---|
699 | already_known = true;
|
---|
700 | break;
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | if (!already_known) {
|
---|
705 | kbd_dev_t *kdev;
|
---|
706 | if (kbd_add_kbdev(svcs[i], &kdev) == EOK) {
|
---|
707 | printf("%s: Connected keyboard device '%s'\n",
|
---|
708 | NAME, kdev->svc_name);
|
---|
709 | }
|
---|
710 | }
|
---|
711 | }
|
---|
712 |
|
---|
713 | free(svcs);
|
---|
714 |
|
---|
715 | /* XXX Handle device removal */
|
---|
716 |
|
---|
717 | return EOK;
|
---|
718 | }
|
---|
719 |
|
---|
720 | static int dev_check_new_mousedevs(void)
|
---|
721 | {
|
---|
722 | category_id_t mouse_cat;
|
---|
723 | service_id_t *svcs;
|
---|
724 | size_t count, i;
|
---|
725 | bool already_known;
|
---|
726 | int rc;
|
---|
727 |
|
---|
728 | rc = loc_category_get_id("mouse", &mouse_cat, IPC_FLAG_BLOCKING);
|
---|
729 | if (rc != EOK) {
|
---|
730 | printf("%s: Failed resolving category 'mouse'.\n", NAME);
|
---|
731 | return ENOENT;
|
---|
732 | }
|
---|
733 |
|
---|
734 | /*
|
---|
735 | * Check for new mouse devices
|
---|
736 | */
|
---|
737 | rc = loc_category_get_svcs(mouse_cat, &svcs, &count);
|
---|
738 | if (rc != EOK) {
|
---|
739 | printf("%s: Failed getting list of mouse devices.\n",
|
---|
740 | NAME);
|
---|
741 | return EIO;
|
---|
742 | }
|
---|
743 |
|
---|
744 | for (i = 0; i < count; i++) {
|
---|
745 | already_known = false;
|
---|
746 |
|
---|
747 | /* Determine whether we already know this device. */
|
---|
748 | list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
|
---|
749 | if (mdev->svc_id == svcs[i]) {
|
---|
750 | already_known = true;
|
---|
751 | break;
|
---|
752 | }
|
---|
753 | }
|
---|
754 |
|
---|
755 | if (!already_known) {
|
---|
756 | mouse_dev_t *mdev;
|
---|
757 | if (mouse_add_mousedev(svcs[i], &mdev) == EOK) {
|
---|
758 | printf("%s: Connected mouse device '%s'\n",
|
---|
759 | NAME, mdev->svc_name);
|
---|
760 | }
|
---|
761 | }
|
---|
762 | }
|
---|
763 |
|
---|
764 | free(svcs);
|
---|
765 |
|
---|
766 | /* XXX Handle device removal */
|
---|
767 |
|
---|
768 | return EOK;
|
---|
769 | }
|
---|
770 |
|
---|
771 | static int dev_check_new_serialdevs(void)
|
---|
772 | {
|
---|
773 | category_id_t serial_cat;
|
---|
774 | service_id_t *svcs;
|
---|
775 | size_t count, i;
|
---|
776 | bool already_known;
|
---|
777 | int rc;
|
---|
778 |
|
---|
779 | rc = loc_category_get_id("serial", &serial_cat, IPC_FLAG_BLOCKING);
|
---|
780 | if (rc != EOK) {
|
---|
781 | printf("%s: Failed resolving category 'serial'.\n", NAME);
|
---|
782 | return ENOENT;
|
---|
783 | }
|
---|
784 |
|
---|
785 | /*
|
---|
786 | * Check for new serial devices
|
---|
787 | */
|
---|
788 | rc = loc_category_get_svcs(serial_cat, &svcs, &count);
|
---|
789 | if (rc != EOK) {
|
---|
790 | printf("%s: Failed getting list of serial devices.\n",
|
---|
791 | NAME);
|
---|
792 | return EIO;
|
---|
793 | }
|
---|
794 |
|
---|
795 | for (i = 0; i < count; i++) {
|
---|
796 | already_known = false;
|
---|
797 |
|
---|
798 | /* Determine whether we already know this device. */
|
---|
799 | list_foreach(serial_devs, link, serial_dev_t, sdev) {
|
---|
800 | if (sdev->kdev->svc_id == svcs[i]) {
|
---|
801 | already_known = true;
|
---|
802 | break;
|
---|
803 | }
|
---|
804 | }
|
---|
805 |
|
---|
806 | if (!already_known) {
|
---|
807 | serial_dev_t *sdev;
|
---|
808 | if (serial_add_srldev(svcs[i], &sdev) == EOK) {
|
---|
809 | printf("%s: Connected serial device '%s'\n",
|
---|
810 | NAME, sdev->kdev->svc_name);
|
---|
811 | }
|
---|
812 | }
|
---|
813 | }
|
---|
814 |
|
---|
815 | free(svcs);
|
---|
816 |
|
---|
817 | /* XXX Handle device removal */
|
---|
818 |
|
---|
819 | return EOK;
|
---|
820 | }
|
---|
821 |
|
---|
822 | static int dev_check_new(void)
|
---|
823 | {
|
---|
824 | int rc;
|
---|
825 |
|
---|
826 | fibril_mutex_lock(&discovery_lock);
|
---|
827 |
|
---|
828 | if (!serial_console) {
|
---|
829 | rc = dev_check_new_kbdevs();
|
---|
830 | if (rc != EOK) {
|
---|
831 | fibril_mutex_unlock(&discovery_lock);
|
---|
832 | return rc;
|
---|
833 | }
|
---|
834 |
|
---|
835 | rc = dev_check_new_mousedevs();
|
---|
836 | if (rc != EOK) {
|
---|
837 | fibril_mutex_unlock(&discovery_lock);
|
---|
838 | return rc;
|
---|
839 | }
|
---|
840 | } else {
|
---|
841 | rc = dev_check_new_serialdevs();
|
---|
842 | if (rc != EOK) {
|
---|
843 | fibril_mutex_unlock(&discovery_lock);
|
---|
844 | return rc;
|
---|
845 | }
|
---|
846 | }
|
---|
847 |
|
---|
848 | fibril_mutex_unlock(&discovery_lock);
|
---|
849 |
|
---|
850 | return EOK;
|
---|
851 | }
|
---|
852 |
|
---|
853 | static void cat_change_cb(void)
|
---|
854 | {
|
---|
855 | dev_check_new();
|
---|
856 | }
|
---|
857 |
|
---|
858 | /** Start listening for new devices. */
|
---|
859 | static int input_start_dev_discovery(void)
|
---|
860 | {
|
---|
861 | int rc = loc_register_cat_change_cb(cat_change_cb);
|
---|
862 | if (rc != EOK) {
|
---|
863 | printf("%s: Failed registering callback for device discovery. "
|
---|
864 | "(%d)\n", NAME, rc);
|
---|
865 | return rc;
|
---|
866 | }
|
---|
867 |
|
---|
868 | return dev_check_new();
|
---|
869 | }
|
---|
870 |
|
---|
871 | static void usage(char *name)
|
---|
872 | {
|
---|
873 | printf("Usage: %s <service_name>\n", name);
|
---|
874 | }
|
---|
875 |
|
---|
876 | int main(int argc, char **argv)
|
---|
877 | {
|
---|
878 | int rc;
|
---|
879 |
|
---|
880 | if (argc < 2) {
|
---|
881 | usage(argv[0]);
|
---|
882 | return 1;
|
---|
883 | }
|
---|
884 |
|
---|
885 | printf("%s: HelenOS input service\n", NAME);
|
---|
886 |
|
---|
887 | list_initialize(&clients);
|
---|
888 | list_initialize(&kbd_devs);
|
---|
889 | list_initialize(&mouse_devs);
|
---|
890 | list_initialize(&serial_devs);
|
---|
891 |
|
---|
892 | serial_console = config_get_value("console");
|
---|
893 |
|
---|
894 | /* Add legacy keyboard devices. */
|
---|
895 | kbd_add_legacy_devs();
|
---|
896 |
|
---|
897 | /* Add legacy mouse devices. */
|
---|
898 | mouse_add_legacy_devs();
|
---|
899 |
|
---|
900 | /* Register driver */
|
---|
901 | async_set_client_data_constructor(client_data_create);
|
---|
902 | async_set_client_data_destructor(client_data_destroy);
|
---|
903 | async_set_fallback_port_handler(client_connection, NULL);
|
---|
904 |
|
---|
905 | rc = loc_server_register(NAME);
|
---|
906 | if (rc != EOK) {
|
---|
907 | printf("%s: Unable to register server\n", NAME);
|
---|
908 | return rc;
|
---|
909 | }
|
---|
910 |
|
---|
911 | service_id_t service_id;
|
---|
912 | rc = loc_service_register(argv[1], &service_id);
|
---|
913 | if (rc != EOK) {
|
---|
914 | printf("%s: Unable to register service %s\n", NAME, argv[1]);
|
---|
915 | return rc;
|
---|
916 | }
|
---|
917 |
|
---|
918 | /* Receive kernel notifications */
|
---|
919 | rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
|
---|
920 | if (rc != EOK)
|
---|
921 | printf("%s: Failed to register kconsole notifications (%s)\n",
|
---|
922 | NAME, str_error(rc));
|
---|
923 |
|
---|
924 | /* Start looking for new input devices */
|
---|
925 | input_start_dev_discovery();
|
---|
926 |
|
---|
927 | printf("%s: Accepting connections\n", NAME);
|
---|
928 | task_retval(0);
|
---|
929 | async_manager();
|
---|
930 |
|
---|
931 | /* Not reached. */
|
---|
932 | return 0;
|
---|
933 | }
|
---|
934 |
|
---|
935 | /**
|
---|
936 | * @}
|
---|
937 | */
|
---|