source: mainline/uspace/srv/hid/input/input.c@ fa942bc

topic/simplify-dev-export
Last change on this file since fa942bc was 4c6fd56, checked in by Jiri Svoboda <jiri@…>, 22 months ago

loc_server_register() should be callable more than once (API only)

Now loc_server_register() returns a pointer to a loc_srv_t object,
that is then passed to loc_service_register() and
loc_service_add_to_cat().

Added loc_server_unregister() that unregisters the server
and frees the loc_srv_t object.

Updated all callers. The implementation, however, is a stub.
It is not actually possible to call loc_server_register() more
than once, yet.

  • Property mode set to 100644
File size: 19.8 KB
RevLine 
[51d6f80]1/*
[4c6fd56]2 * Copyright (c) 2023 Jiri Svoboda
[df4ed85]3 * Copyright (c) 2006 Josef Cejka
[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>
[74017ce]58
59#include "input.h"
[b6a088f]60#include "kbd.h"
61#include "kbd_port.h"
62#include "kbd_ctl.h"
[74017ce]63#include "layout.h"
[b6a088f]64#include "mouse.h"
65#include "mouse_proto.h"
[2a72d9f]66#include "serial.h"
[51d6f80]67
[453c5ce]68#define NUM_LAYOUTS 5
[1875a0c]69
70static layout_ops_t *layout[NUM_LAYOUTS] = {
71 &us_qwerty_ops,
72 &us_dvorak_ops,
[c928bb7]73 &cz_ops,
[453c5ce]74 &ar_ops,
75 &fr_azerty_ops
[1875a0c]76};
[854387b]77
[593e023]78typedef struct {
79 /** Link into the list of clients */
80 link_t link;
[a35b458]81
[593e023]82 /** Indicate whether the client is active */
83 bool active;
[a35b458]84
[593e023]85 /** Client callback session */
86 async_sess_t *sess;
87} client_t;
[9be360ee]88
[593e023]89/** List of clients */
90static list_t clients;
91static client_t *active_client = NULL;
[d1eece6]92
[380e23a3]93/** Kernel override */
94static bool active = true;
95
[a79b42a]96/** Serial console specified by the user */
97static char *serial_console;
98
[9be360ee]99/** List of keyboard devices */
[b72efe8]100static list_t kbd_devs;
[b1bdc7a4]101
[854eddd6]102/** List of mouse devices */
[1875a0c]103static list_t mouse_devs;
[854eddd6]104
[2a72d9f]105/** List of serial devices */
106static list_t serial_devs;
107
[10a5479d]108static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
109
[593e023]110static void *client_data_create(void)
111{
112 client_t *client = (client_t *) calloc(1, sizeof(client_t));
113 if (client == NULL)
114 return NULL;
[a35b458]115
[593e023]116 link_initialize(&client->link);
117 client->active = false;
118 client->sess = NULL;
[a35b458]119
[593e023]120 list_append(&client->link, &clients);
[a35b458]121
[593e023]122 return client;
123}
124
125static void client_data_destroy(void *data)
126{
127 client_t *client = (client_t *) data;
[a35b458]128
[593e023]129 list_remove(&client->link);
130 free(client);
131}
132
[1875a0c]133void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
134{
135 (*kdev->ctl_ops->parse)(data);
136}
[0175246]137
[1875a0c]138void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
[f89979b]139{
[1875a0c]140 (*mdev->proto_ops->parse)(data);
[f89979b]141}
142
[1875a0c]143void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
[085bd54]144{
[79ae36dd]145 kbd_event_t ev;
[1875a0c]146 unsigned int mod_mask;
[a35b458]147
[d1eece6]148 switch (key) {
[ce3efa0]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;
[d1eece6]169 }
[a35b458]170
[d1eece6]171 if (mod_mask != 0) {
[215abc1]172 if (type == KEY_PRESS)
[2f7a564]173 kdev->mods = kdev->mods | mod_mask;
[d1eece6]174 else
[2f7a564]175 kdev->mods = kdev->mods & ~mod_mask;
[d1eece6]176 }
[a35b458]177
[d1eece6]178 switch (key) {
[ce3efa0]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;
[d1eece6]190 }
[a35b458]191
[12b6796]192 if (mod_mask != 0) {
[215abc1]193 if (type == KEY_PRESS) {
[12b6796]194 /*
195 * Only change lock state on transition from released
196 * to pressed. This prevents autorepeat from messing
197 * up the lock state.
198 */
[2f7a564]199 kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
200 kdev->lock_keys = kdev->lock_keys | mod_mask;
[a35b458]201
[c145bc2]202 /* Update keyboard lock indicator lights. */
[2f7a564]203 (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
[12b6796]204 } else {
[2f7a564]205 kdev->lock_keys = kdev->lock_keys & ~mod_mask;
[12b6796]206 }
207 }
[a35b458]208
[ce3efa0]209 // TODO: More elegant layout switching
[a35b458]210
[453c5ce]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 }
[c928bb7]236 }
[a35b458]237
[df1a019]238 if (type == KEY_PRESS) {
239 switch (key) {
240 case KC_F12:
241 console_kcon();
242 break;
243 }
244 }
245
[f89979b]246 ev.type = type;
247 ev.key = key;
[2f7a564]248 ev.mods = kdev->mods;
[a35b458]249
[2f7a564]250 ev.c = layout_parse_ev(kdev->active_layout, &ev);
[a35b458]251
[593e023]252 list_foreach(clients, link, client_t, client) {
253 if (client->active) {
254 async_exch_t *exch = async_exchange_begin(client->sess);
[60ebe63]255 async_msg_5(exch, INPUT_EVENT_KEY, kdev->svc_id,
256 ev.type, ev.key, ev.mods, ev.c);
[593e023]257 async_exchange_end(exch);
258 }
259 }
[854eddd6]260}
261
[593e023]262/** Mouse pointer has moved (relative mode). */
[edb3cf2]263void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
[854eddd6]264{
[593e023]265 list_foreach(clients, link, client_t, client) {
266 if (client->active) {
267 async_exch_t *exch = async_exchange_begin(client->sess);
[a35b458]268
[593e023]269 if ((dx) || (dy))
[60ebe63]270 async_msg_3(exch, INPUT_EVENT_MOVE,
271 mdev->svc_id, dx, dy);
[a35b458]272
[593e023]273 if (dz) {
274 // TODO: Implement proper wheel support
275 keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
[a35b458]276
[593e023]277 for (unsigned int i = 0; i < 3; i++)
[60ebe63]278 async_msg_5(exch, INPUT_EVENT_KEY,
279 0 /* XXX kbd_id */,
280 KEY_PRESS, code, 0, 0);
[a35b458]281
[60ebe63]282 async_msg_5(exch, INPUT_EVENT_KEY, KEY_RELEASE,
283 0 /* XXX kbd_id */, code, 0, 0);
[593e023]284 }
[a35b458]285
[593e023]286 async_exchange_end(exch);
[edb3cf2]287 }
288 }
[854eddd6]289}
290
[593e023]291/** Mouse pointer has moved (absolute mode). */
[8a99c7e]292void 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{
[593e023]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);
[60ebe63]299 async_msg_5(exch, INPUT_EVENT_ABS_MOVE,
300 mdev->svc_id, x, y, max_x, max_y);
[593e023]301 async_exchange_end(exch);
302 }
303 }
[8a99c7e]304 }
305}
306
[854eddd6]307/** Mouse button has been pressed. */
[1875a0c]308void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
[854eddd6]309{
[593e023]310 list_foreach(clients, link, client_t, client) {
311 if (client->active) {
312 async_exch_t *exch = async_exchange_begin(client->sess);
[60ebe63]313 async_msg_3(exch, INPUT_EVENT_BUTTON, mdev->svc_id,
314 bnum, press);
[593e023]315 async_exchange_end(exch);
316 }
317 }
[085bd54]318}
319
[8edec53]320/** Mouse button has been double-clicked. */
321void 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);
[60ebe63]326 async_msg_2(exch, INPUT_EVENT_DCLICK, mdev->svc_id,
327 bnum);
[8edec53]328 async_exchange_end(exch);
329 }
330 }
331}
332
333/** Arbitrate client activation */
[380e23a3]334static void client_arbitration(void)
[593e023]335{
336 /* Mutual exclusion of active clients */
337 list_foreach(clients, link, client_t, client)
[774aa332]338 client->active = ((active) && (client == active_client));
[a35b458]339
[593e023]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 */
[984a9ba]350static void client_connection(ipc_call_t *icall, void *arg)
[085bd54]351{
[593e023]352 client_t *client = (client_t *) async_get_client_data();
353 if (client == NULL) {
[984a9ba]354 async_answer_0(icall, ENOMEM);
[593e023]355 return;
356 }
[a35b458]357
[beb83c1]358 async_accept_0(icall);
[a35b458]359
[79ae36dd]360 while (true) {
[5da7199]361 ipc_call_t call;
[984a9ba]362 async_get_call(&call);
[a35b458]363
[fafb8e5]364 if (!ipc_get_imethod(&call)) {
[593e023]365 if (client->sess != NULL) {
366 async_hangup(client->sess);
367 client->sess = NULL;
[47a350f]368 }
[a35b458]369
[984a9ba]370 async_answer_0(&call, EOK);
[085bd54]371 return;
[79ae36dd]372 }
[a35b458]373
[5da7199]374 async_sess_t *sess =
375 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
376 if (sess != NULL) {
[593e023]377 if (client->sess == NULL) {
378 client->sess = sess;
[984a9ba]379 async_answer_0(&call, EOK);
[5da7199]380 } else
[984a9ba]381 async_answer_0(&call, ELIMIT);
[5da7199]382 } else {
[fafb8e5]383 switch (ipc_get_imethod(&call)) {
[593e023]384 case INPUT_ACTIVATE:
385 active_client = client;
[380e23a3]386 client_arbitration();
[984a9ba]387 async_answer_0(&call, EOK);
[085bd54]388 break;
[5da7199]389 default:
[984a9ba]390 async_answer_0(&call, EINVAL);
[085bd54]391 }
392 }
[1875a0c]393 }
[085bd54]394}
395
[01c3bb4]396static void kconsole_event_handler(ipc_call_t *call, void *arg)
[593e023]397{
[fafb8e5]398 if (ipc_get_arg1(call)) {
[593e023]399 /* Kernel console activated */
[380e23a3]400 active = false;
[593e023]401 } else {
402 /* Kernel console deactivated */
[380e23a3]403 active = true;
[593e023]404 }
[a35b458]405
[380e23a3]406 client_arbitration();
[593e023]407}
408
[2f7a564]409static kbd_dev_t *kbd_dev_new(void)
[b1bdc7a4]410{
[1875a0c]411 kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
[9be360ee]412 if (kdev == NULL) {
[1875a0c]413 printf("%s: Error allocating keyboard device. "
414 "Out of memory.\n", NAME);
[2f7a564]415 return NULL;
[9be360ee]416 }
[a35b458]417
[593e023]418 link_initialize(&kdev->link);
[a35b458]419
[2f7a564]420 kdev->mods = KM_NUM_LOCK;
421 kdev->lock_keys = 0;
422 kdev->active_layout = layout_create(layout[0]);
[a35b458]423
[2f7a564]424 return kdev;
425}
426
[1875a0c]427static mouse_dev_t *mouse_dev_new(void)
428{
429 mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
430 if (mdev == NULL) {
[2a72d9f]431 printf("%s: Error allocating mouse device. "
[1875a0c]432 "Out of memory.\n", NAME);
433 return NULL;
434 }
[a35b458]435
[593e023]436 link_initialize(&mdev->link);
[a35b458]437
[1875a0c]438 return mdev;
439}
440
[2a72d9f]441static 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 }
[a35b458]449
[2a72d9f]450 sdev->kdev = kbd_dev_new();
451 if (sdev->kdev == NULL) {
452 free(sdev);
453 return NULL;
454 }
455
456 link_initialize(&sdev->link);
[a35b458]457
[2a72d9f]458 return sdev;
459}
460
[2f7a564]461/** Add new legacy keyboard device. */
462static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
463{
[1875a0c]464 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]465 if (kdev == NULL)
466 return;
[a35b458]467
[9be360ee]468 kdev->port_ops = port;
469 kdev->ctl_ops = ctl;
[cce8a83]470 kdev->svc_id = 0;
[a35b458]471
[9be360ee]472 /* Initialize port driver. */
[af897ff0]473 if ((*kdev->port_ops->init)(kdev) != 0)
474 goto fail;
[a35b458]475
[9be360ee]476 /* Initialize controller driver. */
477 if ((*kdev->ctl_ops->init)(kdev) != 0) {
478 /* XXX Uninit port */
479 goto fail;
480 }
[a35b458]481
[593e023]482 list_append(&kdev->link, &kbd_devs);
[9be360ee]483 return;
[a35b458]484
[9be360ee]485fail:
486 free(kdev);
487}
488
[af897ff0]489/** Add new kbdev device.
490 *
[593e023]491 * @param service_id Service ID of the keyboard device
[1875a0c]492 *
[af897ff0]493 */
[cce8a83]494static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
[af897ff0]495{
[1875a0c]496 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]497 if (kdev == NULL)
[af897ff0]498 return -1;
[a35b458]499
[cce8a83]500 kdev->svc_id = service_id;
[af897ff0]501 kdev->port_ops = NULL;
502 kdev->ctl_ops = &kbdev_ctl;
[a35b458]503
[b7fd2a0]504 errno_t rc = loc_service_get_name(service_id, &kdev->svc_name);
[cce8a83]505 if (rc != EOK) {
506 kdev->svc_name = NULL;
507 goto fail;
508 }
[a35b458]509
[af897ff0]510 /* Initialize controller driver. */
511 if ((*kdev->ctl_ops->init)(kdev) != 0) {
512 goto fail;
513 }
[a35b458]514
[593e023]515 list_append(&kdev->link, &kbd_devs);
[cce8a83]516 *kdevp = kdev;
[d5c1051]517 return 0;
[a35b458]518
[af897ff0]519fail:
[cce8a83]520 if (kdev->svc_name != NULL)
521 free(kdev->svc_name);
[af897ff0]522 free(kdev);
523 return -1;
524}
525
[1875a0c]526/** Add new mousedev device.
527 *
[593e023]528 * @param service_id Service ID of the mouse device
[1875a0c]529 *
530 */
[cce8a83]531static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
[1875a0c]532{
533 mouse_dev_t *mdev = mouse_dev_new();
534 if (mdev == NULL)
535 return -1;
[a35b458]536
[cce8a83]537 mdev->svc_id = service_id;
[1875a0c]538 mdev->port_ops = NULL;
539 mdev->proto_ops = &mousedev_proto;
[a35b458]540
[b7fd2a0]541 errno_t rc = loc_service_get_name(service_id, &mdev->svc_name);
[cce8a83]542 if (rc != EOK) {
543 mdev->svc_name = NULL;
544 goto fail;
545 }
[a35b458]546
[1875a0c]547 /* Initialize controller driver. */
548 if ((*mdev->proto_ops->init)(mdev) != 0) {
549 goto fail;
550 }
[a35b458]551
[593e023]552 list_append(&mdev->link, &mouse_devs);
[cce8a83]553 *mdevp = mdev;
[d5c1051]554 return 0;
[a35b458]555
[1875a0c]556fail:
557 free(mdev);
558 return -1;
559}
560
[b7fd2a0]561static errno_t serial_consumer(void *arg)
[2a72d9f]562{
563 serial_dev_t *sdev = (serial_dev_t *) arg;
564
565 while (true) {
566 uint8_t data;
[74017ce]567 size_t nread;
[2a72d9f]568
[f2d88f3]569 chardev_read(sdev->chardev, &data, sizeof(data), &nread,
570 chardev_f_none);
[74017ce]571 /* XXX Handle error */
[2a72d9f]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 */
583static int serial_add_srldev(service_id_t service_id, serial_dev_t **sdevp)
584{
[a79b42a]585 bool match = false;
[b7fd2a0]586 errno_t rc;
[a79b42a]587
[2a72d9f]588 serial_dev_t *sdev = serial_dev_new();
589 if (sdev == NULL)
590 return -1;
[a35b458]591
[2a72d9f]592 sdev->kdev->svc_id = service_id;
[a35b458]593
[74017ce]594 rc = loc_service_get_name(service_id, &sdev->kdev->svc_name);
[a79b42a]595 if (rc != EOK)
[2a72d9f]596 goto fail;
597
598 list_append(&sdev->link, &serial_devs);
599
[a79b42a]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
[74017ce]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
[a79b42a]626 fid_t fid = fibril_create(serial_consumer, sdev);
627 fibril_add_ready(fid);
628 }
[a35b458]629
[2a72d9f]630 *sdevp = sdev;
[d5c1051]631 return 0;
[a35b458]632
[2a72d9f]633fail:
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
[9be360ee]641/** Add legacy drivers/devices. */
642static 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
[6d15572]651#if defined(UARCH_ia64) && defined(MACHINE_ski)
652 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]653#endif
654#if defined(MACHINE_msim)
[676e833]655 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]656#endif
657#if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
[7aa94304]658 kbd_add_dev(&chardev_port, &stty_ctl);
[7348c4b]659#endif
660#if defined(UARCH_arm64) && defined(MACHINE_virt)
661 kbd_add_dev(&chardev_port, &stty_ctl);
[06f10ac]662#endif
663#if defined(UARCH_arm64) && defined(MACHINE_hikey960)
664 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]665#endif
[af897ff0]666 /* Silence warning on abs32le about kbd_add_dev() being unused */
667 (void) kbd_add_dev;
[9be360ee]668}
669
[b7fd2a0]670static errno_t dev_check_new_kbdevs(void)
[af897ff0]671{
[12f9f0d0]672 category_id_t keyboard_cat;
[cc574511]673 service_id_t *svcs;
674 size_t count, i;
675 bool already_known;
[b7fd2a0]676 errno_t rc;
[a35b458]677
[cc574511]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 }
[a35b458]683
[12f9f0d0]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;
[a35b458]696
[12f9f0d0]697 /* Determine whether we already know this device. */
[593e023]698 list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
[12f9f0d0]699 if (kdev->svc_id == svcs[i]) {
700 already_known = true;
701 break;
702 }
703 }
[a35b458]704
[12f9f0d0]705 if (!already_known) {
706 kbd_dev_t *kdev;
[d5c1051]707 if (kbd_add_kbdev(svcs[i], &kdev) == 0) {
[12f9f0d0]708 printf("%s: Connected keyboard device '%s'\n",
709 NAME, kdev->svc_name);
710 }
711 }
712 }
[a35b458]713
[99ac5cf]714 free(svcs);
[a35b458]715
[12f9f0d0]716 /* XXX Handle device removal */
[a35b458]717
[12f9f0d0]718 return EOK;
719}
720
[b7fd2a0]721static errno_t dev_check_new_mousedevs(void)
[12f9f0d0]722{
723 category_id_t mouse_cat;
724 service_id_t *svcs;
725 size_t count, i;
726 bool already_known;
[b7fd2a0]727 errno_t rc;
[a35b458]728
[cc574511]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 }
[a35b458]734
[12f9f0d0]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 }
[a35b458]744
[12f9f0d0]745 for (i = 0; i < count; i++) {
746 already_known = false;
[a35b458]747
[12f9f0d0]748 /* Determine whether we already know this device. */
[593e023]749 list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
[12f9f0d0]750 if (mdev->svc_id == svcs[i]) {
751 already_known = true;
752 break;
[cc574511]753 }
[854eddd6]754 }
[a35b458]755
[12f9f0d0]756 if (!already_known) {
757 mouse_dev_t *mdev;
[d5c1051]758 if (mouse_add_mousedev(svcs[i], &mdev) == 0) {
[12f9f0d0]759 printf("%s: Connected mouse device '%s'\n",
760 NAME, mdev->svc_name);
[cc574511]761 }
[af897ff0]762 }
763 }
[a35b458]764
[99ac5cf]765 free(svcs);
[a35b458]766
[12f9f0d0]767 /* XXX Handle device removal */
[a35b458]768
[af897ff0]769 return EOK;
770}
771
[b7fd2a0]772static errno_t dev_check_new_serialdevs(void)
[2a72d9f]773{
774 category_id_t serial_cat;
775 service_id_t *svcs;
776 size_t count, i;
777 bool already_known;
[b7fd2a0]778 errno_t rc;
[a35b458]779
[2a72d9f]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 }
[a35b458]785
[2a72d9f]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;
[a35b458]798
[2a72d9f]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 }
[a35b458]806
[2a72d9f]807 if (!already_known) {
808 serial_dev_t *sdev;
[d5c1051]809 if (serial_add_srldev(svcs[i], &sdev) == 0) {
[2a72d9f]810 printf("%s: Connected serial device '%s'\n",
811 NAME, sdev->kdev->svc_name);
812 }
813 }
814 }
[a35b458]815
[2a72d9f]816 free(svcs);
[a35b458]817
[2a72d9f]818 /* XXX Handle device removal */
[a35b458]819
[2a72d9f]820 return EOK;
821}
822
[b7fd2a0]823static errno_t dev_check_new(void)
[af897ff0]824{
[b7fd2a0]825 errno_t rc;
[a35b458]826
[10a5479d]827 fibril_mutex_lock(&discovery_lock);
[a35b458]828
[73d8600]829 if (!serial_console) {
830 rc = dev_check_new_kbdevs();
831 if (rc != EOK) {
832 fibril_mutex_unlock(&discovery_lock);
833 return rc;
834 }
[a35b458]835
[73d8600]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 }
[2a72d9f]847 }
[a35b458]848
[10a5479d]849 fibril_mutex_unlock(&discovery_lock);
[a35b458]850
[12f9f0d0]851 return EOK;
852}
853
[e89a06a]854static void cat_change_cb(void *arg)
[12f9f0d0]855{
856 dev_check_new();
857}
858
859/** Start listening for new devices. */
[b7fd2a0]860static errno_t input_start_dev_discovery(void)
[12f9f0d0]861{
[e89a06a]862 errno_t rc = loc_register_cat_change_cb(cat_change_cb, NULL);
[12f9f0d0]863 if (rc != EOK) {
[dd8ab1c]864 printf("%s: Failed registering callback for device discovery: "
865 "%s\n", NAME, str_error(rc));
[12f9f0d0]866 return rc;
867 }
[a35b458]868
[12f9f0d0]869 return dev_check_new();
[af897ff0]870}
871
[b6a088f]872static void usage(char *name)
873{
874 printf("Usage: %s <service_name>\n", name);
875}
876
[51d6f80]877int main(int argc, char **argv)
878{
[b7fd2a0]879 errno_t rc;
[4c6fd56]880 loc_srv_t *srv;
[a79b42a]881
[b6a088f]882 if (argc < 2) {
883 usage(argv[0]);
884 return 1;
885 }
[a35b458]886
[5f88293]887 printf("%s: HelenOS input service\n", NAME);
[a35b458]888
[593e023]889 list_initialize(&clients);
[9be360ee]890 list_initialize(&kbd_devs);
[854eddd6]891 list_initialize(&mouse_devs);
[2a72d9f]892 list_initialize(&serial_devs);
[a35b458]893
[03e0beaf]894 serial_console = config_get_value("console");
[a35b458]895
[854eddd6]896 /* Add legacy keyboard devices. */
[9be360ee]897 kbd_add_legacy_devs();
[a35b458]898
[47a350f]899 /* Register driver */
[593e023]900 async_set_client_data_constructor(client_data_create);
901 async_set_client_data_destructor(client_data_destroy);
[b688fd8]902 async_set_fallback_port_handler(client_connection, NULL);
[a35b458]903
[4c6fd56]904 rc = loc_server_register(NAME, &srv);
[3123d2a]905 if (rc != EOK) {
906 printf("%s: Unable to register server\n", NAME);
907 return rc;
[47a350f]908 }
[a35b458]909
[15f3c3f]910 service_id_t service_id;
[4c6fd56]911 rc = loc_service_register(srv, argv[1], &service_id);
[3123d2a]912 if (rc != EOK) {
[b6a088f]913 printf("%s: Unable to register service %s\n", NAME, argv[1]);
[3123d2a]914 return rc;
[47a350f]915 }
[a35b458]916
[593e023]917 /* Receive kernel notifications */
[8820544]918 rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
[593e023]919 if (rc != EOK)
920 printf("%s: Failed to register kconsole notifications (%s)\n",
921 NAME, str_error(rc));
[a35b458]922
[854eddd6]923 /* Start looking for new input devices */
924 input_start_dev_discovery();
[a35b458]925
[1875a0c]926 printf("%s: Accepting connections\n", NAME);
[b6a088f]927 task_retval(0);
[085bd54]928 async_manager();
[a35b458]929
[f89979b]930 /* Not reached. */
[153a209]931 return 0;
[51d6f80]932}
[ce5bcb4]933
934/**
935 * @}
[5f88293]936 */
Note: See TracBrowser for help on using the repository browser.