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