source: mainline/uspace/srv/hid/input/input.c@ 9fe1635

Last change on this file since 9fe1635 was 9fe1635, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Adding support for changing the layout through IPC

The service hid/input is now available for other
programs to access. The service accepts a new request
type for changing the keyboard layout (INPUT_CHANGE_LAYOUT)

  • Property mode set to 100644
File size: 19.9 KB
RevLine 
[51d6f80]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
[9be360ee]3 * Copyright (c) 2011 Jiri Svoboda
[51d6f80]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
[231a60a]30/**
[5f88293]31 * @addtogroup inputgen generic
32 * @brief HelenOS input server.
33 * @ingroup input
[ce5bcb4]34 * @{
[215abc1]35 */
[ce5bcb4]36/** @file
37 */
38
[74017ce]39#include <adt/fifo.h>
[9be360ee]40#include <adt/list.h>
[fa09449]41#include <async.h>
[74017ce]42#include <config.h>
[51d6f80]43#include <errno.h>
[74017ce]44#include <fibril.h>
45#include <fibril_synch.h>
46#include <io/chardev.h>
[215abc1]47#include <io/console.h>
48#include <io/keycode.h>
[74017ce]49#include <ipc/services.h>
50#include <ipc/input.h>
[15f3c3f]51#include <loc.h>
[74017ce]52#include <ns.h>
53#include <stdbool.h>
54#include <stdio.h>
55#include <stdlib.h>
[1d6dd2a]56#include <str.h>
[593e023]57#include <str_error.h>
[e89dc0b]58#include <dlfcn.h>
[74017ce]59
60#include "input.h"
[b6a088f]61#include "kbd.h"
62#include "kbd_port.h"
63#include "kbd_ctl.h"
[74017ce]64#include "layout.h"
[b6a088f]65#include "mouse.h"
66#include "mouse_proto.h"
[2a72d9f]67#include "serial.h"
[51d6f80]68
[593e023]69typedef struct {
70 /** Link into the list of clients */
71 link_t link;
[a35b458]72
[593e023]73 /** Indicate whether the client is active */
74 bool active;
[a35b458]75
[593e023]76 /** Client callback session */
77 async_sess_t *sess;
78} client_t;
[9be360ee]79
[593e023]80/** List of clients */
81static list_t clients;
82static client_t *active_client = NULL;
[d1eece6]83
[380e23a3]84/** Kernel override */
85static bool active = true;
86
[a79b42a]87/** Serial console specified by the user */
88static char *serial_console;
89
[9be360ee]90/** List of keyboard devices */
[b72efe8]91static list_t kbd_devs;
[b1bdc7a4]92
[854eddd6]93/** List of mouse devices */
[1875a0c]94static list_t mouse_devs;
[854eddd6]95
[2a72d9f]96/** List of serial devices */
97static list_t serial_devs;
98
[10a5479d]99static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
100
[e89dc0b]101static 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
[9fe1635]112static errno_t layout_load(const char *layout_name)
[e89dc0b]113{
[9fe1635]114#ifdef CONFIG_RTLD
115 const char *prefix = "layouts/";
[e89dc0b]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);
[9fe1635]121 char *path = malloc(sizeof(char) * (length + 1));
122
[e89dc0b]123 if (path == NULL) {
124 printf("%s: Error allocating layout path. Out of memory.\n", NAME);
[9fe1635]125 return ENOMEM;
[e89dc0b]126 }
127
[9fe1635]128 path[0] = '\0';
[e89dc0b]129 str_append(path, length, prefix);
130 str_append(path, length, layout_name);
131 str_append(path, length, suffix);
[9fe1635]132 printf("%s\n", path);
[e89dc0b]133
134 void *handle = dlopen(path, 0);
135 if (handle == NULL) {
136 printf("%s: Keyboard layout could not be found.\n", NAME);
[9fe1635]137 return ENOENT;
[e89dc0b]138 }
139
[9fe1635]140 layout_ops_t (*get_layout)(void) = dlsym(handle, "get_layout");
[e89dc0b]141
142 if (get_layout == NULL) {
143 printf("%s: Keyboard layout constructor could not be found.\n", NAME);
[9fe1635]144 return ENOENT;
[e89dc0b]145 }
146
147 layout_active = get_layout();
148 layout_change(&layout_active);
[9fe1635]149#endif
150
151 return EOK;
[e89dc0b]152}
153
[593e023]154static void *client_data_create(void)
155{
156 client_t *client = (client_t *) calloc(1, sizeof(client_t));
157 if (client == NULL)
158 return NULL;
[a35b458]159
[593e023]160 link_initialize(&client->link);
161 client->active = false;
162 client->sess = NULL;
[a35b458]163
[593e023]164 list_append(&client->link, &clients);
[a35b458]165
[593e023]166 return client;
167}
168
169static void client_data_destroy(void *data)
170{
171 client_t *client = (client_t *) data;
[a35b458]172
[593e023]173 list_remove(&client->link);
174 free(client);
175}
176
[1875a0c]177void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
178{
179 (*kdev->ctl_ops->parse)(data);
180}
[0175246]181
[1875a0c]182void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
[f89979b]183{
[1875a0c]184 (*mdev->proto_ops->parse)(data);
[f89979b]185}
186
[1875a0c]187void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
[085bd54]188{
[79ae36dd]189 kbd_event_t ev;
[1875a0c]190 unsigned int mod_mask;
[a35b458]191
[d1eece6]192 switch (key) {
[ce3efa0]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;
[d1eece6]213 }
[a35b458]214
[d1eece6]215 if (mod_mask != 0) {
[215abc1]216 if (type == KEY_PRESS)
[2f7a564]217 kdev->mods = kdev->mods | mod_mask;
[d1eece6]218 else
[2f7a564]219 kdev->mods = kdev->mods & ~mod_mask;
[d1eece6]220 }
[a35b458]221
[d1eece6]222 switch (key) {
[ce3efa0]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;
[d1eece6]234 }
[a35b458]235
[12b6796]236 if (mod_mask != 0) {
[215abc1]237 if (type == KEY_PRESS) {
[12b6796]238 /*
239 * Only change lock state on transition from released
240 * to pressed. This prevents autorepeat from messing
241 * up the lock state.
242 */
[2f7a564]243 kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
244 kdev->lock_keys = kdev->lock_keys | mod_mask;
[a35b458]245
[c145bc2]246 /* Update keyboard lock indicator lights. */
[2f7a564]247 (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
[12b6796]248 } else {
[2f7a564]249 kdev->lock_keys = kdev->lock_keys & ~mod_mask;
[12b6796]250 }
251 }
[a35b458]252
[cb9313e]253 if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL) && (key == KC_F1)) {
[e89dc0b]254 layout_active = layout_default;
255 layout_change(&layout_default);
[c928bb7]256 return;
257 }
[a35b458]258
[f89979b]259 ev.type = type;
260 ev.key = key;
[2f7a564]261 ev.mods = kdev->mods;
[a35b458]262
[2f7a564]263 ev.c = layout_parse_ev(kdev->active_layout, &ev);
[a35b458]264
[593e023]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 }
[854eddd6]272}
273
[593e023]274/** Mouse pointer has moved (relative mode). */
[edb3cf2]275void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
[854eddd6]276{
[593e023]277 list_foreach(clients, link, client_t, client) {
278 if (client->active) {
279 async_exch_t *exch = async_exchange_begin(client->sess);
[a35b458]280
[593e023]281 if ((dx) || (dy))
282 async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
[a35b458]283
[593e023]284 if (dz) {
285 // TODO: Implement proper wheel support
286 keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
[a35b458]287
[593e023]288 for (unsigned int i = 0; i < 3; i++)
289 async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
[a35b458]290
[593e023]291 async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
292 }
[a35b458]293
[593e023]294 async_exchange_end(exch);
[edb3cf2]295 }
296 }
[854eddd6]297}
298
[593e023]299/** Mouse pointer has moved (absolute mode). */
[8a99c7e]300void 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{
[593e023]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 }
[8a99c7e]311 }
312}
313
[854eddd6]314/** Mouse button has been pressed. */
[1875a0c]315void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
[854eddd6]316{
[593e023]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 }
[085bd54]324}
325
[593e023]326/** Arbitrate client actiovation */
[380e23a3]327static void client_arbitration(void)
[593e023]328{
329 /* Mutual exclusion of active clients */
330 list_foreach(clients, link, client_t, client)
[774aa332]331 client->active = ((active) && (client == active_client));
[a35b458]332
[593e023]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 */
[984a9ba]343static void client_connection(ipc_call_t *icall, void *arg)
[085bd54]344{
[593e023]345 client_t *client = (client_t *) async_get_client_data();
346 if (client == NULL) {
[984a9ba]347 async_answer_0(icall, ENOMEM);
[593e023]348 return;
349 }
[a35b458]350
[beb83c1]351 async_accept_0(icall);
[a35b458]352
[79ae36dd]353 while (true) {
[5da7199]354 ipc_call_t call;
[984a9ba]355 async_get_call(&call);
[a35b458]356
[fafb8e5]357 if (!ipc_get_imethod(&call)) {
[593e023]358 if (client->sess != NULL) {
359 async_hangup(client->sess);
360 client->sess = NULL;
[47a350f]361 }
[a35b458]362
[984a9ba]363 async_answer_0(&call, EOK);
[085bd54]364 return;
[79ae36dd]365 }
[a35b458]366
[5da7199]367 async_sess_t *sess =
368 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
369 if (sess != NULL) {
[593e023]370 if (client->sess == NULL) {
371 client->sess = sess;
[984a9ba]372 async_answer_0(&call, EOK);
[5da7199]373 } else
[984a9ba]374 async_answer_0(&call, ELIMIT);
[5da7199]375 } else {
[fafb8e5]376 switch (ipc_get_imethod(&call)) {
[593e023]377 case INPUT_ACTIVATE:
378 active_client = client;
[380e23a3]379 client_arbitration();
[984a9ba]380 async_answer_0(&call, EOK);
[085bd54]381 break;
[e89dc0b]382 case INPUT_CHANGE_LAYOUT: {
[9fe1635]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);
[e89dc0b]391 free(layout_name);
[9fe1635]392 async_answer_0(&call, retval);
[e89dc0b]393 break;
394 }
[5da7199]395 default:
[984a9ba]396 async_answer_0(&call, EINVAL);
[085bd54]397 }
398 }
[1875a0c]399 }
[085bd54]400}
401
[01c3bb4]402static void kconsole_event_handler(ipc_call_t *call, void *arg)
[593e023]403{
[fafb8e5]404 if (ipc_get_arg1(call)) {
[593e023]405 /* Kernel console activated */
[380e23a3]406 active = false;
[593e023]407 } else {
408 /* Kernel console deactivated */
[380e23a3]409 active = true;
[593e023]410 }
[a35b458]411
[380e23a3]412 client_arbitration();
[593e023]413}
414
[2f7a564]415static kbd_dev_t *kbd_dev_new(void)
[b1bdc7a4]416{
[1875a0c]417 kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
[9be360ee]418 if (kdev == NULL) {
[1875a0c]419 printf("%s: Error allocating keyboard device. "
420 "Out of memory.\n", NAME);
[2f7a564]421 return NULL;
[9be360ee]422 }
[a35b458]423
[593e023]424 link_initialize(&kdev->link);
[a35b458]425
[2f7a564]426 kdev->mods = KM_NUM_LOCK;
427 kdev->lock_keys = 0;
[e89dc0b]428 kdev->active_layout = layout_create(&layout_active);
[a35b458]429
[2f7a564]430 return kdev;
431}
432
[1875a0c]433static mouse_dev_t *mouse_dev_new(void)
434{
435 mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
436 if (mdev == NULL) {
[2a72d9f]437 printf("%s: Error allocating mouse device. "
[1875a0c]438 "Out of memory.\n", NAME);
439 return NULL;
440 }
[a35b458]441
[593e023]442 link_initialize(&mdev->link);
[a35b458]443
[1875a0c]444 return mdev;
445}
446
[2a72d9f]447static 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 }
[a35b458]455
[2a72d9f]456 sdev->kdev = kbd_dev_new();
457 if (sdev->kdev == NULL) {
458 free(sdev);
459 return NULL;
460 }
461
462 link_initialize(&sdev->link);
[a35b458]463
[2a72d9f]464 return sdev;
465}
466
[2f7a564]467/** Add new legacy keyboard device. */
468static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
469{
[1875a0c]470 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]471 if (kdev == NULL)
472 return;
[a35b458]473
[9be360ee]474 kdev->port_ops = port;
475 kdev->ctl_ops = ctl;
[cce8a83]476 kdev->svc_id = 0;
[a35b458]477
[9be360ee]478 /* Initialize port driver. */
[af897ff0]479 if ((*kdev->port_ops->init)(kdev) != 0)
480 goto fail;
[a35b458]481
[9be360ee]482 /* Initialize controller driver. */
483 if ((*kdev->ctl_ops->init)(kdev) != 0) {
484 /* XXX Uninit port */
485 goto fail;
486 }
[a35b458]487
[593e023]488 list_append(&kdev->link, &kbd_devs);
[9be360ee]489 return;
[a35b458]490
[9be360ee]491fail:
492 free(kdev);
493}
494
[af897ff0]495/** Add new kbdev device.
496 *
[593e023]497 * @param service_id Service ID of the keyboard device
[1875a0c]498 *
[af897ff0]499 */
[cce8a83]500static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
[af897ff0]501{
[1875a0c]502 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]503 if (kdev == NULL)
[af897ff0]504 return -1;
[a35b458]505
[cce8a83]506 kdev->svc_id = service_id;
[af897ff0]507 kdev->port_ops = NULL;
508 kdev->ctl_ops = &kbdev_ctl;
[a35b458]509
[b7fd2a0]510 errno_t rc = loc_service_get_name(service_id, &kdev->svc_name);
[cce8a83]511 if (rc != EOK) {
512 kdev->svc_name = NULL;
513 goto fail;
514 }
[a35b458]515
[af897ff0]516 /* Initialize controller driver. */
517 if ((*kdev->ctl_ops->init)(kdev) != 0) {
518 goto fail;
519 }
[a35b458]520
[593e023]521 list_append(&kdev->link, &kbd_devs);
[cce8a83]522 *kdevp = kdev;
[d5c1051]523 return 0;
[a35b458]524
[af897ff0]525fail:
[cce8a83]526 if (kdev->svc_name != NULL)
527 free(kdev->svc_name);
[af897ff0]528 free(kdev);
529 return -1;
530}
531
[1875a0c]532/** Add new mousedev device.
533 *
[593e023]534 * @param service_id Service ID of the mouse device
[1875a0c]535 *
536 */
[cce8a83]537static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
[1875a0c]538{
539 mouse_dev_t *mdev = mouse_dev_new();
540 if (mdev == NULL)
541 return -1;
[a35b458]542
[cce8a83]543 mdev->svc_id = service_id;
[1875a0c]544 mdev->port_ops = NULL;
545 mdev->proto_ops = &mousedev_proto;
[a35b458]546
[b7fd2a0]547 errno_t rc = loc_service_get_name(service_id, &mdev->svc_name);
[cce8a83]548 if (rc != EOK) {
549 mdev->svc_name = NULL;
550 goto fail;
551 }
[a35b458]552
[1875a0c]553 /* Initialize controller driver. */
554 if ((*mdev->proto_ops->init)(mdev) != 0) {
555 goto fail;
556 }
[a35b458]557
[593e023]558 list_append(&mdev->link, &mouse_devs);
[cce8a83]559 *mdevp = mdev;
[d5c1051]560 return 0;
[a35b458]561
[1875a0c]562fail:
563 free(mdev);
564 return -1;
565}
566
[b7fd2a0]567static errno_t serial_consumer(void *arg)
[2a72d9f]568{
569 serial_dev_t *sdev = (serial_dev_t *) arg;
570
571 while (true) {
572 uint8_t data;
[74017ce]573 size_t nread;
[2a72d9f]574
[f2d88f3]575 chardev_read(sdev->chardev, &data, sizeof(data), &nread,
576 chardev_f_none);
[74017ce]577 /* XXX Handle error */
[2a72d9f]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 */
589static int serial_add_srldev(service_id_t service_id, serial_dev_t **sdevp)
590{
[a79b42a]591 bool match = false;
[b7fd2a0]592 errno_t rc;
[a79b42a]593
[2a72d9f]594 serial_dev_t *sdev = serial_dev_new();
595 if (sdev == NULL)
596 return -1;
[a35b458]597
[2a72d9f]598 sdev->kdev->svc_id = service_id;
[a35b458]599
[74017ce]600 rc = loc_service_get_name(service_id, &sdev->kdev->svc_name);
[a79b42a]601 if (rc != EOK)
[2a72d9f]602 goto fail;
603
604 list_append(&sdev->link, &serial_devs);
605
[a79b42a]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
[74017ce]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
[a79b42a]632 fid_t fid = fibril_create(serial_consumer, sdev);
633 fibril_add_ready(fid);
634 }
[a35b458]635
[2a72d9f]636 *sdevp = sdev;
[d5c1051]637 return 0;
[a35b458]638
[2a72d9f]639fail:
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
[9be360ee]647/** Add legacy drivers/devices. */
648static 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
[6d15572]657#if defined(UARCH_ia64) && defined(MACHINE_ski)
658 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]659#endif
660#if defined(MACHINE_msim)
[676e833]661 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]662#endif
663#if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
[7aa94304]664 kbd_add_dev(&chardev_port, &stty_ctl);
[7348c4b]665#endif
666#if defined(UARCH_arm64) && defined(MACHINE_virt)
667 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]668#endif
[af897ff0]669 /* Silence warning on abs32le about kbd_add_dev() being unused */
670 (void) kbd_add_dev;
[9be360ee]671}
672
[b7fd2a0]673static errno_t dev_check_new_kbdevs(void)
[af897ff0]674{
[12f9f0d0]675 category_id_t keyboard_cat;
[cc574511]676 service_id_t *svcs;
677 size_t count, i;
678 bool already_known;
[b7fd2a0]679 errno_t rc;
[a35b458]680
[cc574511]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 }
[a35b458]686
[12f9f0d0]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;
[a35b458]699
[12f9f0d0]700 /* Determine whether we already know this device. */
[593e023]701 list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
[12f9f0d0]702 if (kdev->svc_id == svcs[i]) {
703 already_known = true;
704 break;
705 }
706 }
[a35b458]707
[12f9f0d0]708 if (!already_known) {
709 kbd_dev_t *kdev;
[d5c1051]710 if (kbd_add_kbdev(svcs[i], &kdev) == 0) {
[12f9f0d0]711 printf("%s: Connected keyboard device '%s'\n",
712 NAME, kdev->svc_name);
713 }
714 }
715 }
[a35b458]716
[99ac5cf]717 free(svcs);
[a35b458]718
[12f9f0d0]719 /* XXX Handle device removal */
[a35b458]720
[12f9f0d0]721 return EOK;
722}
723
[b7fd2a0]724static errno_t dev_check_new_mousedevs(void)
[12f9f0d0]725{
726 category_id_t mouse_cat;
727 service_id_t *svcs;
728 size_t count, i;
729 bool already_known;
[b7fd2a0]730 errno_t rc;
[a35b458]731
[cc574511]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 }
[a35b458]737
[12f9f0d0]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 }
[a35b458]747
[12f9f0d0]748 for (i = 0; i < count; i++) {
749 already_known = false;
[a35b458]750
[12f9f0d0]751 /* Determine whether we already know this device. */
[593e023]752 list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
[12f9f0d0]753 if (mdev->svc_id == svcs[i]) {
754 already_known = true;
755 break;
[cc574511]756 }
[854eddd6]757 }
[a35b458]758
[12f9f0d0]759 if (!already_known) {
760 mouse_dev_t *mdev;
[d5c1051]761 if (mouse_add_mousedev(svcs[i], &mdev) == 0) {
[12f9f0d0]762 printf("%s: Connected mouse device '%s'\n",
763 NAME, mdev->svc_name);
[cc574511]764 }
[af897ff0]765 }
766 }
[a35b458]767
[99ac5cf]768 free(svcs);
[a35b458]769
[12f9f0d0]770 /* XXX Handle device removal */
[a35b458]771
[af897ff0]772 return EOK;
773}
774
[b7fd2a0]775static errno_t dev_check_new_serialdevs(void)
[2a72d9f]776{
777 category_id_t serial_cat;
778 service_id_t *svcs;
779 size_t count, i;
780 bool already_known;
[b7fd2a0]781 errno_t rc;
[a35b458]782
[2a72d9f]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 }
[a35b458]788
[2a72d9f]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;
[a35b458]801
[2a72d9f]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 }
[a35b458]809
[2a72d9f]810 if (!already_known) {
811 serial_dev_t *sdev;
[d5c1051]812 if (serial_add_srldev(svcs[i], &sdev) == 0) {
[2a72d9f]813 printf("%s: Connected serial device '%s'\n",
814 NAME, sdev->kdev->svc_name);
815 }
816 }
817 }
[a35b458]818
[2a72d9f]819 free(svcs);
[a35b458]820
[2a72d9f]821 /* XXX Handle device removal */
[a35b458]822
[2a72d9f]823 return EOK;
824}
825
[b7fd2a0]826static errno_t dev_check_new(void)
[af897ff0]827{
[b7fd2a0]828 errno_t rc;
[a35b458]829
[10a5479d]830 fibril_mutex_lock(&discovery_lock);
[a35b458]831
[73d8600]832 if (!serial_console) {
833 rc = dev_check_new_kbdevs();
834 if (rc != EOK) {
835 fibril_mutex_unlock(&discovery_lock);
836 return rc;
837 }
[a35b458]838
[73d8600]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 }
[2a72d9f]850 }
[a35b458]851
[10a5479d]852 fibril_mutex_unlock(&discovery_lock);
[a35b458]853
[12f9f0d0]854 return EOK;
855}
856
[e89a06a]857static void cat_change_cb(void *arg)
[12f9f0d0]858{
859 dev_check_new();
860}
861
862/** Start listening for new devices. */
[b7fd2a0]863static errno_t input_start_dev_discovery(void)
[12f9f0d0]864{
[e89a06a]865 errno_t rc = loc_register_cat_change_cb(cat_change_cb, NULL);
[12f9f0d0]866 if (rc != EOK) {
[dd8ab1c]867 printf("%s: Failed registering callback for device discovery: "
868 "%s\n", NAME, str_error(rc));
[12f9f0d0]869 return rc;
870 }
[a35b458]871
[12f9f0d0]872 return dev_check_new();
[af897ff0]873}
874
[b6a088f]875static void usage(char *name)
876{
877 printf("Usage: %s <service_name>\n", name);
878}
879
[51d6f80]880int main(int argc, char **argv)
881{
[b7fd2a0]882 errno_t rc;
[a79b42a]883
[b6a088f]884 if (argc < 2) {
885 usage(argv[0]);
886 return 1;
887 }
[a35b458]888
[5f88293]889 printf("%s: HelenOS input service\n", NAME);
[a35b458]890
[593e023]891 list_initialize(&clients);
[9be360ee]892 list_initialize(&kbd_devs);
[854eddd6]893 list_initialize(&mouse_devs);
[2a72d9f]894 list_initialize(&serial_devs);
[a35b458]895
[03e0beaf]896 serial_console = config_get_value("console");
[a35b458]897
[854eddd6]898 /* Add legacy keyboard devices. */
[9be360ee]899 kbd_add_legacy_devs();
[a35b458]900
[47a350f]901 /* Register driver */
[593e023]902 async_set_client_data_constructor(client_data_create);
903 async_set_client_data_destructor(client_data_destroy);
[b688fd8]904 async_set_fallback_port_handler(client_connection, NULL);
[a35b458]905
[a79b42a]906 rc = loc_server_register(NAME);
[3123d2a]907 if (rc != EOK) {
908 printf("%s: Unable to register server\n", NAME);
909 return rc;
[47a350f]910 }
[a35b458]911
[15f3c3f]912 service_id_t service_id;
[b6a088f]913 rc = loc_service_register(argv[1], &service_id);
[3123d2a]914 if (rc != EOK) {
[b6a088f]915 printf("%s: Unable to register service %s\n", NAME, argv[1]);
[3123d2a]916 return rc;
[47a350f]917 }
[a35b458]918
[593e023]919 /* Receive kernel notifications */
[8820544]920 rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
[593e023]921 if (rc != EOK)
922 printf("%s: Failed to register kconsole notifications (%s)\n",
923 NAME, str_error(rc));
[a35b458]924
[854eddd6]925 /* Start looking for new input devices */
926 input_start_dev_discovery();
[a35b458]927
[1875a0c]928 printf("%s: Accepting connections\n", NAME);
[b6a088f]929 task_retval(0);
[085bd54]930 async_manager();
[a35b458]931
[f89979b]932 /* Not reached. */
[153a209]933 return 0;
[51d6f80]934}
[ce5bcb4]935
936/**
937 * @}
[5f88293]938 */
Note: See TracBrowser for help on using the repository browser.