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