source: mainline/uspace/srv/hid/input/input.c@ 7b1ae09

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7b1ae09 was 7348c4b, checked in by Jakub Jermář <jakub@…>, 7 years ago

arm64: Add PL011 uspace driver

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