source: mainline/uspace/srv/hid/input/input.c@ 6d15572

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

Move receiving side of ski console support to a separate driver, ski-con. Add ski platform driver.

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