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