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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f14a900 was df1a019, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Switch to kernel console with F12.

  • Property mode set to 100644
File size: 19.2 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>
[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);
255 async_msg_4(exch, INPUT_EVENT_KEY, ev.type, ev.key, ev.mods, ev.c);
256 async_exchange_end(exch);
257 }
258 }
[854eddd6]259}
260
[593e023]261/** Mouse pointer has moved (relative mode). */
[edb3cf2]262void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
[854eddd6]263{
[593e023]264 list_foreach(clients, link, client_t, client) {
265 if (client->active) {
266 async_exch_t *exch = async_exchange_begin(client->sess);
[a35b458]267
[593e023]268 if ((dx) || (dy))
269 async_msg_2(exch, INPUT_EVENT_MOVE, dx, dy);
[a35b458]270
[593e023]271 if (dz) {
272 // TODO: Implement proper wheel support
273 keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
[a35b458]274
[593e023]275 for (unsigned int i = 0; i < 3; i++)
276 async_msg_4(exch, INPUT_EVENT_KEY, KEY_PRESS, code, 0, 0);
[a35b458]277
[593e023]278 async_msg_4(exch, INPUT_EVENT_KEY, KEY_RELEASE, code, 0, 0);
279 }
[a35b458]280
[593e023]281 async_exchange_end(exch);
[edb3cf2]282 }
283 }
[854eddd6]284}
285
[593e023]286/** Mouse pointer has moved (absolute mode). */
[8a99c7e]287void mouse_push_event_abs_move(mouse_dev_t *mdev, unsigned int x, unsigned int y,
288 unsigned int max_x, unsigned int max_y)
289{
[593e023]290 list_foreach(clients, link, client_t, client) {
291 if (client->active) {
292 if ((max_x) && (max_y)) {
293 async_exch_t *exch = async_exchange_begin(client->sess);
294 async_msg_4(exch, INPUT_EVENT_ABS_MOVE, x, y, max_x, max_y);
295 async_exchange_end(exch);
296 }
297 }
[8a99c7e]298 }
299}
300
[854eddd6]301/** Mouse button has been pressed. */
[1875a0c]302void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
[854eddd6]303{
[593e023]304 list_foreach(clients, link, client_t, client) {
305 if (client->active) {
306 async_exch_t *exch = async_exchange_begin(client->sess);
307 async_msg_2(exch, INPUT_EVENT_BUTTON, bnum, press);
308 async_exchange_end(exch);
309 }
310 }
[085bd54]311}
312
[593e023]313/** Arbitrate client actiovation */
[380e23a3]314static void client_arbitration(void)
[593e023]315{
316 /* Mutual exclusion of active clients */
317 list_foreach(clients, link, client_t, client)
[774aa332]318 client->active = ((active) && (client == active_client));
[a35b458]319
[593e023]320 /* Notify clients about the arbitration */
321 list_foreach(clients, link, client_t, client) {
322 async_exch_t *exch = async_exchange_begin(client->sess);
323 async_msg_0(exch, client->active ?
324 INPUT_EVENT_ACTIVE : INPUT_EVENT_DEACTIVE);
325 async_exchange_end(exch);
326 }
327}
328
329/** New client connection */
[984a9ba]330static void client_connection(ipc_call_t *icall, void *arg)
[085bd54]331{
[593e023]332 client_t *client = (client_t *) async_get_client_data();
333 if (client == NULL) {
[984a9ba]334 async_answer_0(icall, ENOMEM);
[593e023]335 return;
336 }
[a35b458]337
[beb83c1]338 async_accept_0(icall);
[a35b458]339
[79ae36dd]340 while (true) {
[5da7199]341 ipc_call_t call;
[984a9ba]342 async_get_call(&call);
[a35b458]343
[fafb8e5]344 if (!ipc_get_imethod(&call)) {
[593e023]345 if (client->sess != NULL) {
346 async_hangup(client->sess);
347 client->sess = NULL;
[47a350f]348 }
[a35b458]349
[984a9ba]350 async_answer_0(&call, EOK);
[085bd54]351 return;
[79ae36dd]352 }
[a35b458]353
[5da7199]354 async_sess_t *sess =
355 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
356 if (sess != NULL) {
[593e023]357 if (client->sess == NULL) {
358 client->sess = sess;
[984a9ba]359 async_answer_0(&call, EOK);
[5da7199]360 } else
[984a9ba]361 async_answer_0(&call, ELIMIT);
[5da7199]362 } else {
[fafb8e5]363 switch (ipc_get_imethod(&call)) {
[593e023]364 case INPUT_ACTIVATE:
365 active_client = client;
[380e23a3]366 client_arbitration();
[984a9ba]367 async_answer_0(&call, EOK);
[085bd54]368 break;
[5da7199]369 default:
[984a9ba]370 async_answer_0(&call, EINVAL);
[085bd54]371 }
372 }
[1875a0c]373 }
[085bd54]374}
375
[01c3bb4]376static void kconsole_event_handler(ipc_call_t *call, void *arg)
[593e023]377{
[fafb8e5]378 if (ipc_get_arg1(call)) {
[593e023]379 /* Kernel console activated */
[380e23a3]380 active = false;
[593e023]381 } else {
382 /* Kernel console deactivated */
[380e23a3]383 active = true;
[593e023]384 }
[a35b458]385
[380e23a3]386 client_arbitration();
[593e023]387}
388
[2f7a564]389static kbd_dev_t *kbd_dev_new(void)
[b1bdc7a4]390{
[1875a0c]391 kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
[9be360ee]392 if (kdev == NULL) {
[1875a0c]393 printf("%s: Error allocating keyboard device. "
394 "Out of memory.\n", NAME);
[2f7a564]395 return NULL;
[9be360ee]396 }
[a35b458]397
[593e023]398 link_initialize(&kdev->link);
[a35b458]399
[2f7a564]400 kdev->mods = KM_NUM_LOCK;
401 kdev->lock_keys = 0;
402 kdev->active_layout = layout_create(layout[0]);
[a35b458]403
[2f7a564]404 return kdev;
405}
406
[1875a0c]407static mouse_dev_t *mouse_dev_new(void)
408{
409 mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
410 if (mdev == NULL) {
[2a72d9f]411 printf("%s: Error allocating mouse device. "
[1875a0c]412 "Out of memory.\n", NAME);
413 return NULL;
414 }
[a35b458]415
[593e023]416 link_initialize(&mdev->link);
[a35b458]417
[1875a0c]418 return mdev;
419}
420
[2a72d9f]421static serial_dev_t *serial_dev_new(void)
422{
423 serial_dev_t *sdev = calloc(1, sizeof(serial_dev_t));
424 if (sdev == NULL) {
425 printf("%s: Error allocating serial device. "
426 "Out of memory.\n", NAME);
427 return NULL;
428 }
[a35b458]429
[2a72d9f]430 sdev->kdev = kbd_dev_new();
431 if (sdev->kdev == NULL) {
432 free(sdev);
433 return NULL;
434 }
435
436 link_initialize(&sdev->link);
[a35b458]437
[2a72d9f]438 return sdev;
439}
440
[2f7a564]441/** Add new legacy keyboard device. */
442static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
443{
[1875a0c]444 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]445 if (kdev == NULL)
446 return;
[a35b458]447
[9be360ee]448 kdev->port_ops = port;
449 kdev->ctl_ops = ctl;
[cce8a83]450 kdev->svc_id = 0;
[a35b458]451
[9be360ee]452 /* Initialize port driver. */
[af897ff0]453 if ((*kdev->port_ops->init)(kdev) != 0)
454 goto fail;
[a35b458]455
[9be360ee]456 /* Initialize controller driver. */
457 if ((*kdev->ctl_ops->init)(kdev) != 0) {
458 /* XXX Uninit port */
459 goto fail;
460 }
[a35b458]461
[593e023]462 list_append(&kdev->link, &kbd_devs);
[9be360ee]463 return;
[a35b458]464
[9be360ee]465fail:
466 free(kdev);
467}
468
[af897ff0]469/** Add new kbdev device.
470 *
[593e023]471 * @param service_id Service ID of the keyboard device
[1875a0c]472 *
[af897ff0]473 */
[cce8a83]474static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
[af897ff0]475{
[1875a0c]476 kbd_dev_t *kdev = kbd_dev_new();
[2f7a564]477 if (kdev == NULL)
[af897ff0]478 return -1;
[a35b458]479
[cce8a83]480 kdev->svc_id = service_id;
[af897ff0]481 kdev->port_ops = NULL;
482 kdev->ctl_ops = &kbdev_ctl;
[a35b458]483
[b7fd2a0]484 errno_t rc = loc_service_get_name(service_id, &kdev->svc_name);
[cce8a83]485 if (rc != EOK) {
486 kdev->svc_name = NULL;
487 goto fail;
488 }
[a35b458]489
[af897ff0]490 /* Initialize controller driver. */
491 if ((*kdev->ctl_ops->init)(kdev) != 0) {
492 goto fail;
493 }
[a35b458]494
[593e023]495 list_append(&kdev->link, &kbd_devs);
[cce8a83]496 *kdevp = kdev;
[d5c1051]497 return 0;
[a35b458]498
[af897ff0]499fail:
[cce8a83]500 if (kdev->svc_name != NULL)
501 free(kdev->svc_name);
[af897ff0]502 free(kdev);
503 return -1;
504}
505
[1875a0c]506/** Add new mousedev device.
507 *
[593e023]508 * @param service_id Service ID of the mouse device
[1875a0c]509 *
510 */
[cce8a83]511static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
[1875a0c]512{
513 mouse_dev_t *mdev = mouse_dev_new();
514 if (mdev == NULL)
515 return -1;
[a35b458]516
[cce8a83]517 mdev->svc_id = service_id;
[1875a0c]518 mdev->port_ops = NULL;
519 mdev->proto_ops = &mousedev_proto;
[a35b458]520
[b7fd2a0]521 errno_t rc = loc_service_get_name(service_id, &mdev->svc_name);
[cce8a83]522 if (rc != EOK) {
523 mdev->svc_name = NULL;
524 goto fail;
525 }
[a35b458]526
[1875a0c]527 /* Initialize controller driver. */
528 if ((*mdev->proto_ops->init)(mdev) != 0) {
529 goto fail;
530 }
[a35b458]531
[593e023]532 list_append(&mdev->link, &mouse_devs);
[cce8a83]533 *mdevp = mdev;
[d5c1051]534 return 0;
[a35b458]535
[1875a0c]536fail:
537 free(mdev);
538 return -1;
539}
540
[b7fd2a0]541static errno_t serial_consumer(void *arg)
[2a72d9f]542{
543 serial_dev_t *sdev = (serial_dev_t *) arg;
544
545 while (true) {
546 uint8_t data;
[74017ce]547 size_t nread;
[2a72d9f]548
[f2d88f3]549 chardev_read(sdev->chardev, &data, sizeof(data), &nread,
550 chardev_f_none);
[74017ce]551 /* XXX Handle error */
[2a72d9f]552 kbd_push_data(sdev->kdev, data);
553 }
554
555 return EOK;
556}
557
558/** Add new serial console device.
559 *
560 * @param service_id Service ID of the chardev device
561 *
562 */
563static int serial_add_srldev(service_id_t service_id, serial_dev_t **sdevp)
564{
[a79b42a]565 bool match = false;
[b7fd2a0]566 errno_t rc;
[a79b42a]567
[2a72d9f]568 serial_dev_t *sdev = serial_dev_new();
569 if (sdev == NULL)
570 return -1;
[a35b458]571
[2a72d9f]572 sdev->kdev->svc_id = service_id;
[a35b458]573
[74017ce]574 rc = loc_service_get_name(service_id, &sdev->kdev->svc_name);
[a79b42a]575 if (rc != EOK)
[2a72d9f]576 goto fail;
577
578 list_append(&sdev->link, &serial_devs);
579
[a79b42a]580 /*
581 * Is this the device the user wants to use as a serial console?
582 */
583 match = (serial_console != NULL) &&
584 !str_cmp(serial_console, sdev->kdev->svc_name);
585
586 if (match) {
587 sdev->kdev->ctl_ops = &stty_ctl;
588
589 /* Initialize controller driver. */
590 if ((*sdev->kdev->ctl_ops->init)(sdev->kdev) != 0) {
591 list_remove(&sdev->link);
592 goto fail;
593 }
594
595 sdev->sess = loc_service_connect(service_id, INTERFACE_DDF,
596 IPC_FLAG_BLOCKING);
597
[74017ce]598 rc = chardev_open(sdev->sess, &sdev->chardev);
599 if (rc != EOK) {
600 async_hangup(sdev->sess);
601 sdev->sess = NULL;
602 list_remove(&sdev->link);
603 goto fail;
604 }
605
[a79b42a]606 fid_t fid = fibril_create(serial_consumer, sdev);
607 fibril_add_ready(fid);
608 }
[a35b458]609
[2a72d9f]610 *sdevp = sdev;
[d5c1051]611 return 0;
[a35b458]612
[2a72d9f]613fail:
614 if (sdev->kdev->svc_name != NULL)
615 free(sdev->kdev->svc_name);
616 free(sdev->kdev);
617 free(sdev);
618 return -1;
619}
620
[9be360ee]621/** Add legacy drivers/devices. */
622static void kbd_add_legacy_devs(void)
623{
624 /*
625 * Need to add these drivers based on config unless we can probe
626 * them automatically.
627 */
628#if defined(UARCH_arm32) && defined(MACHINE_gta02)
629 kbd_add_dev(&chardev_port, &stty_ctl);
630#endif
[6d15572]631#if defined(UARCH_ia64) && defined(MACHINE_ski)
632 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]633#endif
634#if defined(MACHINE_msim)
[676e833]635 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]636#endif
637#if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
[7aa94304]638 kbd_add_dev(&chardev_port, &stty_ctl);
[7348c4b]639#endif
640#if defined(UARCH_arm64) && defined(MACHINE_virt)
641 kbd_add_dev(&chardev_port, &stty_ctl);
[9be360ee]642#endif
[af897ff0]643 /* Silence warning on abs32le about kbd_add_dev() being unused */
644 (void) kbd_add_dev;
[9be360ee]645}
646
[b7fd2a0]647static errno_t dev_check_new_kbdevs(void)
[af897ff0]648{
[12f9f0d0]649 category_id_t keyboard_cat;
[cc574511]650 service_id_t *svcs;
651 size_t count, i;
652 bool already_known;
[b7fd2a0]653 errno_t rc;
[a35b458]654
[cc574511]655 rc = loc_category_get_id("keyboard", &keyboard_cat, IPC_FLAG_BLOCKING);
656 if (rc != EOK) {
657 printf("%s: Failed resolving category 'keyboard'.\n", NAME);
658 return ENOENT;
659 }
[a35b458]660
[12f9f0d0]661 /*
662 * Check for new keyboard devices
663 */
664 rc = loc_category_get_svcs(keyboard_cat, &svcs, &count);
665 if (rc != EOK) {
666 printf("%s: Failed getting list of keyboard devices.\n",
667 NAME);
668 return EIO;
669 }
670
671 for (i = 0; i < count; i++) {
672 already_known = false;
[a35b458]673
[12f9f0d0]674 /* Determine whether we already know this device. */
[593e023]675 list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
[12f9f0d0]676 if (kdev->svc_id == svcs[i]) {
677 already_known = true;
678 break;
679 }
680 }
[a35b458]681
[12f9f0d0]682 if (!already_known) {
683 kbd_dev_t *kdev;
[d5c1051]684 if (kbd_add_kbdev(svcs[i], &kdev) == 0) {
[12f9f0d0]685 printf("%s: Connected keyboard device '%s'\n",
686 NAME, kdev->svc_name);
687 }
688 }
689 }
[a35b458]690
[99ac5cf]691 free(svcs);
[a35b458]692
[12f9f0d0]693 /* XXX Handle device removal */
[a35b458]694
[12f9f0d0]695 return EOK;
696}
697
[b7fd2a0]698static errno_t dev_check_new_mousedevs(void)
[12f9f0d0]699{
700 category_id_t mouse_cat;
701 service_id_t *svcs;
702 size_t count, i;
703 bool already_known;
[b7fd2a0]704 errno_t rc;
[a35b458]705
[cc574511]706 rc = loc_category_get_id("mouse", &mouse_cat, IPC_FLAG_BLOCKING);
707 if (rc != EOK) {
708 printf("%s: Failed resolving category 'mouse'.\n", NAME);
709 return ENOENT;
710 }
[a35b458]711
[12f9f0d0]712 /*
713 * Check for new mouse devices
714 */
715 rc = loc_category_get_svcs(mouse_cat, &svcs, &count);
716 if (rc != EOK) {
717 printf("%s: Failed getting list of mouse devices.\n",
718 NAME);
719 return EIO;
720 }
[a35b458]721
[12f9f0d0]722 for (i = 0; i < count; i++) {
723 already_known = false;
[a35b458]724
[12f9f0d0]725 /* Determine whether we already know this device. */
[593e023]726 list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
[12f9f0d0]727 if (mdev->svc_id == svcs[i]) {
728 already_known = true;
729 break;
[cc574511]730 }
[854eddd6]731 }
[a35b458]732
[12f9f0d0]733 if (!already_known) {
734 mouse_dev_t *mdev;
[d5c1051]735 if (mouse_add_mousedev(svcs[i], &mdev) == 0) {
[12f9f0d0]736 printf("%s: Connected mouse device '%s'\n",
737 NAME, mdev->svc_name);
[cc574511]738 }
[af897ff0]739 }
740 }
[a35b458]741
[99ac5cf]742 free(svcs);
[a35b458]743
[12f9f0d0]744 /* XXX Handle device removal */
[a35b458]745
[af897ff0]746 return EOK;
747}
748
[b7fd2a0]749static errno_t dev_check_new_serialdevs(void)
[2a72d9f]750{
751 category_id_t serial_cat;
752 service_id_t *svcs;
753 size_t count, i;
754 bool already_known;
[b7fd2a0]755 errno_t rc;
[a35b458]756
[2a72d9f]757 rc = loc_category_get_id("serial", &serial_cat, IPC_FLAG_BLOCKING);
758 if (rc != EOK) {
759 printf("%s: Failed resolving category 'serial'.\n", NAME);
760 return ENOENT;
761 }
[a35b458]762
[2a72d9f]763 /*
764 * Check for new serial devices
765 */
766 rc = loc_category_get_svcs(serial_cat, &svcs, &count);
767 if (rc != EOK) {
768 printf("%s: Failed getting list of serial devices.\n",
769 NAME);
770 return EIO;
771 }
772
773 for (i = 0; i < count; i++) {
774 already_known = false;
[a35b458]775
[2a72d9f]776 /* Determine whether we already know this device. */
777 list_foreach(serial_devs, link, serial_dev_t, sdev) {
778 if (sdev->kdev->svc_id == svcs[i]) {
779 already_known = true;
780 break;
781 }
782 }
[a35b458]783
[2a72d9f]784 if (!already_known) {
785 serial_dev_t *sdev;
[d5c1051]786 if (serial_add_srldev(svcs[i], &sdev) == 0) {
[2a72d9f]787 printf("%s: Connected serial device '%s'\n",
788 NAME, sdev->kdev->svc_name);
789 }
790 }
791 }
[a35b458]792
[2a72d9f]793 free(svcs);
[a35b458]794
[2a72d9f]795 /* XXX Handle device removal */
[a35b458]796
[2a72d9f]797 return EOK;
798}
799
[b7fd2a0]800static errno_t dev_check_new(void)
[af897ff0]801{
[b7fd2a0]802 errno_t rc;
[a35b458]803
[10a5479d]804 fibril_mutex_lock(&discovery_lock);
[a35b458]805
[73d8600]806 if (!serial_console) {
807 rc = dev_check_new_kbdevs();
808 if (rc != EOK) {
809 fibril_mutex_unlock(&discovery_lock);
810 return rc;
811 }
[a35b458]812
[73d8600]813 rc = dev_check_new_mousedevs();
814 if (rc != EOK) {
815 fibril_mutex_unlock(&discovery_lock);
816 return rc;
817 }
818 } else {
819 rc = dev_check_new_serialdevs();
820 if (rc != EOK) {
821 fibril_mutex_unlock(&discovery_lock);
822 return rc;
823 }
[2a72d9f]824 }
[a35b458]825
[10a5479d]826 fibril_mutex_unlock(&discovery_lock);
[a35b458]827
[12f9f0d0]828 return EOK;
829}
830
[e89a06a]831static void cat_change_cb(void *arg)
[12f9f0d0]832{
833 dev_check_new();
834}
835
836/** Start listening for new devices. */
[b7fd2a0]837static errno_t input_start_dev_discovery(void)
[12f9f0d0]838{
[e89a06a]839 errno_t rc = loc_register_cat_change_cb(cat_change_cb, NULL);
[12f9f0d0]840 if (rc != EOK) {
[dd8ab1c]841 printf("%s: Failed registering callback for device discovery: "
842 "%s\n", NAME, str_error(rc));
[12f9f0d0]843 return rc;
844 }
[a35b458]845
[12f9f0d0]846 return dev_check_new();
[af897ff0]847}
848
[b6a088f]849static void usage(char *name)
850{
851 printf("Usage: %s <service_name>\n", name);
852}
853
[51d6f80]854int main(int argc, char **argv)
855{
[b7fd2a0]856 errno_t rc;
[a79b42a]857
[b6a088f]858 if (argc < 2) {
859 usage(argv[0]);
860 return 1;
861 }
[a35b458]862
[5f88293]863 printf("%s: HelenOS input service\n", NAME);
[a35b458]864
[593e023]865 list_initialize(&clients);
[9be360ee]866 list_initialize(&kbd_devs);
[854eddd6]867 list_initialize(&mouse_devs);
[2a72d9f]868 list_initialize(&serial_devs);
[a35b458]869
[03e0beaf]870 serial_console = config_get_value("console");
[a35b458]871
[854eddd6]872 /* Add legacy keyboard devices. */
[9be360ee]873 kbd_add_legacy_devs();
[a35b458]874
[47a350f]875 /* Register driver */
[593e023]876 async_set_client_data_constructor(client_data_create);
877 async_set_client_data_destructor(client_data_destroy);
[b688fd8]878 async_set_fallback_port_handler(client_connection, NULL);
[a35b458]879
[a79b42a]880 rc = loc_server_register(NAME);
[3123d2a]881 if (rc != EOK) {
882 printf("%s: Unable to register server\n", NAME);
883 return rc;
[47a350f]884 }
[a35b458]885
[15f3c3f]886 service_id_t service_id;
[b6a088f]887 rc = loc_service_register(argv[1], &service_id);
[3123d2a]888 if (rc != EOK) {
[b6a088f]889 printf("%s: Unable to register service %s\n", NAME, argv[1]);
[3123d2a]890 return rc;
[47a350f]891 }
[a35b458]892
[593e023]893 /* Receive kernel notifications */
[8820544]894 rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
[593e023]895 if (rc != EOK)
896 printf("%s: Failed to register kconsole notifications (%s)\n",
897 NAME, str_error(rc));
[a35b458]898
[854eddd6]899 /* Start looking for new input devices */
900 input_start_dev_discovery();
[a35b458]901
[1875a0c]902 printf("%s: Accepting connections\n", NAME);
[b6a088f]903 task_retval(0);
[085bd54]904 async_manager();
[a35b458]905
[f89979b]906 /* Not reached. */
[153a209]907 return 0;
[51d6f80]908}
[ce5bcb4]909
910/**
911 * @}
[5f88293]912 */
Note: See TracBrowser for help on using the repository browser.