source: mainline/uspace/srv/hid/input/generic/input.c@ c0699467

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c0699467 was c0699467, checked in by Martin Decky <martin@…>, 14 years ago

do not provide general access to kernel headers from uspace, only allow specific headers to be accessed or shared
externalize headers which serve as kernel/uspace API/ABI into a special tree

  • Property mode set to 100644
File size: 13.5 KB
Line 
1/*
2 * Copyright (c) 2006 Josef Cejka
3 * Copyright (c) 2011 Jiri Svoboda
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
30/**
31 * @addtogroup inputgen generic
32 * @brief HelenOS input server.
33 * @ingroup input
34 * @{
35 */
36/** @file
37 */
38
39#include <adt/list.h>
40#include <ipc/services.h>
41#include <ipc/input.h>
42#include <sysinfo.h>
43#include <stdio.h>
44#include <unistd.h>
45#include <stdlib.h>
46#include <stdio.h>
47#include <ns.h>
48#include <ns_obsolete.h>
49#include <async.h>
50#include <async_obsolete.h>
51#include <errno.h>
52#include <adt/fifo.h>
53#include <io/console.h>
54#include <io/keycode.h>
55#include <devmap.h>
56#include <input.h>
57#include <kbd.h>
58#include <kbd_port.h>
59#include <kbd_ctl.h>
60#include <mouse_proto.h>
61#include <layout.h>
62#include <mouse.h>
63
64// FIXME: remove this header
65#include <abi/ipc/methods.h>
66
67/* In microseconds */
68#define DISCOVERY_POLL_INTERVAL (10 * 1000 * 1000)
69
70#define NUM_LAYOUTS 3
71
72static layout_ops_t *layout[NUM_LAYOUTS] = {
73 &us_qwerty_ops,
74 &us_dvorak_ops,
75 &cz_ops
76};
77
78static void kbd_devs_yield(void);
79static void kbd_devs_reclaim(void);
80
81int client_phone = -1;
82
83/** List of keyboard devices */
84static list_t kbd_devs;
85
86/** List of mouse devices */
87static list_t mouse_devs;
88
89bool irc_service = false;
90int irc_phone = -1;
91
92void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
93{
94 (*kdev->ctl_ops->parse)(data);
95}
96
97void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
98{
99 (*mdev->proto_ops->parse)(data);
100}
101
102void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
103{
104 kbd_event_t ev;
105 unsigned int mod_mask;
106
107 switch (key) {
108 case KC_LCTRL: mod_mask = KM_LCTRL; break;
109 case KC_RCTRL: mod_mask = KM_RCTRL; break;
110 case KC_LSHIFT: mod_mask = KM_LSHIFT; break;
111 case KC_RSHIFT: mod_mask = KM_RSHIFT; break;
112 case KC_LALT: mod_mask = KM_LALT; break;
113 case KC_RALT: mod_mask = KM_RALT; break;
114 default: mod_mask = 0; break;
115 }
116
117 if (mod_mask != 0) {
118 if (type == KEY_PRESS)
119 kdev->mods = kdev->mods | mod_mask;
120 else
121 kdev->mods = kdev->mods & ~mod_mask;
122 }
123
124 switch (key) {
125 case KC_CAPS_LOCK: mod_mask = KM_CAPS_LOCK; break;
126 case KC_NUM_LOCK: mod_mask = KM_NUM_LOCK; break;
127 case KC_SCROLL_LOCK: mod_mask = KM_SCROLL_LOCK; break;
128 default: mod_mask = 0; break;
129 }
130
131 if (mod_mask != 0) {
132 if (type == KEY_PRESS) {
133 /*
134 * Only change lock state on transition from released
135 * to pressed. This prevents autorepeat from messing
136 * up the lock state.
137 */
138 kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
139 kdev->lock_keys = kdev->lock_keys | mod_mask;
140
141 /* Update keyboard lock indicator lights. */
142 (*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
143 } else {
144 kdev->lock_keys = kdev->lock_keys & ~mod_mask;
145 }
146 }
147
148 if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
149 key == KC_F1) {
150 layout_destroy(kdev->active_layout);
151 kdev->active_layout = layout_create(layout[0]);
152 return;
153 }
154
155 if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
156 key == KC_F2) {
157 layout_destroy(kdev->active_layout);
158 kdev->active_layout = layout_create(layout[1]);
159 return;
160 }
161
162 if (type == KEY_PRESS && (kdev->mods & KM_LCTRL) &&
163 key == KC_F3) {
164 layout_destroy(kdev->active_layout);
165 kdev->active_layout = layout_create(layout[2]);
166 return;
167 }
168
169 ev.type = type;
170 ev.key = key;
171 ev.mods = kdev->mods;
172
173 ev.c = layout_parse_ev(kdev->active_layout, &ev);
174 async_obsolete_msg_4(client_phone, INPUT_EVENT_KEY, ev.type, ev.key,
175 ev.mods, ev.c);
176}
177
178/** Mouse pointer has moved. */
179void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy)
180{
181 async_obsolete_msg_2(client_phone, INPUT_EVENT_MOVE, dx, dy);
182}
183
184/** Mouse button has been pressed. */
185void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
186{
187 async_obsolete_msg_2(client_phone, INPUT_EVENT_BUTTON, bnum, press);
188}
189
190static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
191{
192 ipc_callid_t callid;
193 ipc_call_t call;
194 int retval;
195
196 async_answer_0(iid, EOK);
197
198 while (true) {
199 callid = async_get_call(&call);
200
201 if (!IPC_GET_IMETHOD(call)) {
202 if (client_phone != -1) {
203 async_obsolete_hangup(client_phone);
204 client_phone = -1;
205 }
206
207 async_answer_0(callid, EOK);
208 return;
209 }
210
211 switch (IPC_GET_IMETHOD(call)) {
212 case IPC_M_CONNECT_TO_ME:
213 if (client_phone != -1) {
214 retval = ELIMIT;
215 break;
216 }
217 client_phone = IPC_GET_ARG5(call);
218 retval = 0;
219 break;
220 case INPUT_YIELD:
221 kbd_devs_yield();
222 retval = 0;
223 break;
224 case INPUT_RECLAIM:
225 kbd_devs_reclaim();
226 retval = 0;
227 break;
228 default:
229 retval = EINVAL;
230 }
231
232 async_answer_0(callid, retval);
233 }
234}
235
236static kbd_dev_t *kbd_dev_new(void)
237{
238 kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
239 if (kdev == NULL) {
240 printf("%s: Error allocating keyboard device. "
241 "Out of memory.\n", NAME);
242 return NULL;
243 }
244
245 link_initialize(&kdev->kbd_devs);
246
247 kdev->mods = KM_NUM_LOCK;
248 kdev->lock_keys = 0;
249 kdev->active_layout = layout_create(layout[0]);
250
251 return kdev;
252}
253
254static mouse_dev_t *mouse_dev_new(void)
255{
256 mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
257 if (mdev == NULL) {
258 printf("%s: Error allocating keyboard device. "
259 "Out of memory.\n", NAME);
260 return NULL;
261 }
262
263 link_initialize(&mdev->mouse_devs);
264
265 return mdev;
266}
267
268/** Add new legacy keyboard device. */
269static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
270{
271 kbd_dev_t *kdev = kbd_dev_new();
272 if (kdev == NULL)
273 return;
274
275 kdev->port_ops = port;
276 kdev->ctl_ops = ctl;
277 kdev->dev_path = NULL;
278
279 /* Initialize port driver. */
280 if ((*kdev->port_ops->init)(kdev) != 0)
281 goto fail;
282
283 /* Initialize controller driver. */
284 if ((*kdev->ctl_ops->init)(kdev) != 0) {
285 /* XXX Uninit port */
286 goto fail;
287 }
288
289 list_append(&kdev->kbd_devs, &kbd_devs);
290 return;
291
292fail:
293 free(kdev);
294}
295
296/** Add new legacy mouse device. */
297static void mouse_add_dev(mouse_port_ops_t *port, mouse_proto_ops_t *proto)
298{
299 mouse_dev_t *mdev = mouse_dev_new();
300 if (mdev == NULL)
301 return;
302
303 mdev->port_ops = port;
304 mdev->proto_ops = proto;
305 mdev->dev_path = NULL;
306
307 /* Initialize port driver. */
308 if ((*mdev->port_ops->init)(mdev) != 0)
309 goto fail;
310
311 /* Initialize protocol driver. */
312 if ((*mdev->proto_ops->init)(mdev) != 0) {
313 /* XXX Uninit port */
314 goto fail;
315 }
316
317 list_append(&mdev->mouse_devs, &mouse_devs);
318 return;
319
320fail:
321 free(mdev);
322}
323
324/** Add new kbdev device.
325 *
326 * @param dev_path Filesystem path to the device (/dev/class/...)
327 *
328 */
329static int kbd_add_kbdev(const char *dev_path)
330{
331 kbd_dev_t *kdev = kbd_dev_new();
332 if (kdev == NULL)
333 return -1;
334
335 kdev->dev_path = dev_path;
336 kdev->port_ops = NULL;
337 kdev->ctl_ops = &kbdev_ctl;
338
339 /* Initialize controller driver. */
340 if ((*kdev->ctl_ops->init)(kdev) != 0) {
341 goto fail;
342 }
343
344 list_append(&kdev->kbd_devs, &kbd_devs);
345 return EOK;
346
347fail:
348 free(kdev);
349 return -1;
350}
351
352/** Add new mousedev device.
353 *
354 * @param dev_path Filesystem path to the device (/dev/class/...)
355 *
356 */
357static int mouse_add_mousedev(const char *dev_path)
358{
359 mouse_dev_t *mdev = mouse_dev_new();
360 if (mdev == NULL)
361 return -1;
362
363 mdev->dev_path = dev_path;
364 mdev->port_ops = NULL;
365 mdev->proto_ops = &mousedev_proto;
366
367 /* Initialize controller driver. */
368 if ((*mdev->proto_ops->init)(mdev) != 0) {
369 goto fail;
370 }
371
372 list_append(&mdev->mouse_devs, &mouse_devs);
373 return EOK;
374
375fail:
376 free(mdev);
377 return -1;
378}
379
380/** Add legacy drivers/devices. */
381static void kbd_add_legacy_devs(void)
382{
383 /*
384 * Need to add these drivers based on config unless we can probe
385 * them automatically.
386 */
387#if defined(UARCH_amd64)
388 kbd_add_dev(&chardev_port, &pc_ctl);
389#endif
390#if defined(UARCH_arm32) && defined(MACHINE_gta02)
391 kbd_add_dev(&chardev_port, &stty_ctl);
392#endif
393#if defined(UARCH_arm32) && defined(MACHINE_testarm) && defined(CONFIG_FB)
394 kbd_add_dev(&gxemul_port, &gxe_fb_ctl);
395#endif
396#if defined(UARCH_arm32) && defined(MACHINE_testarm) && !defined(CONFIG_FB)
397 kbd_add_dev(&gxemul_port, &stty_ctl);
398#endif
399#if defined(UARCH_arm32) && defined(MACHINE_integratorcp)
400 kbd_add_dev(&pl050_port, &pc_ctl);
401#endif
402#if defined(UARCH_ia32)
403 kbd_add_dev(&chardev_port, &pc_ctl);
404#endif
405#if defined(MACHINE_i460GX)
406 kbd_add_dev(&chardev_port, &pc_ctl);
407#endif
408#if defined(MACHINE_ski)
409 kbd_add_dev(&ski_port, &stty_ctl);
410#endif
411#if defined(MACHINE_msim)
412 kbd_add_dev(&msim_port, &stty_ctl);
413#endif
414#if (defined(MACHINE_lgxemul) || defined(MACHINE_bgxemul)) && defined(CONFIG_FB)
415 kbd_add_dev(&gxemul_port, &gxe_fb_ctl);
416#endif
417#if defined(MACHINE_lgxemul) || defined(MACHINE_bgxemul) && !defined(CONFIG_FB)
418 kbd_add_dev(&gxemul_port, &stty_ctl);
419#endif
420#if defined(UARCH_ppc32)
421 kbd_add_dev(&adb_port, &apple_ctl);
422#endif
423#if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
424 kbd_add_dev(&niagara_port, &stty_ctl);
425#endif
426#if defined(UARCH_sparc64) && defined(MACHINE_generic)
427 kbd_add_dev(&ns16550_port, &sun_ctl);
428#endif
429 /* Silence warning on abs32le about kbd_add_dev() being unused */
430 (void) kbd_add_dev;
431}
432
433/** Add legacy drivers/devices. */
434static void mouse_add_legacy_devs(void)
435{
436 /*
437 * Need to add these drivers based on config unless we can probe
438 * them automatically.
439 */
440#if defined(UARCH_amd64)
441 mouse_add_dev(&chardev_mouse_port, &ps2_proto);
442#endif
443#if defined(UARCH_ia32)
444 mouse_add_dev(&chardev_mouse_port, &ps2_proto);
445#endif
446#if defined(MACHINE_i460GX)
447 mouse_add_dev(&chardev_mouse_port, &ps2_proto);
448#endif
449#if defined(UARCH_ppc32)
450 mouse_add_dev(&adb_mouse_port, &adb_proto);
451#endif
452 /* Silence warning on abs32le about mouse_add_dev() being unused */
453 (void) mouse_add_dev;
454}
455
456static void kbd_devs_yield(void)
457{
458 /* For each keyboard device */
459 list_foreach(kbd_devs, kdev_link) {
460 kbd_dev_t *kdev = list_get_instance(kdev_link, kbd_dev_t,
461 kbd_devs);
462
463 /* Yield port */
464 if (kdev->port_ops != NULL)
465 (*kdev->port_ops->yield)();
466 }
467}
468
469static void kbd_devs_reclaim(void)
470{
471 /* For each keyboard device */
472 list_foreach(kbd_devs, kdev_link) {
473 kbd_dev_t *kdev = list_get_instance(kdev_link, kbd_dev_t,
474 kbd_devs);
475
476 /* Reclaim port */
477 if (kdev->port_ops != NULL)
478 (*kdev->port_ops->reclaim)();
479 }
480}
481
482/** Periodically check for new input devices.
483 *
484 * Looks under /dev/class/keyboard and /dev/class/mouse.
485 *
486 * @param arg Ignored
487 *
488 */
489static int dev_discovery_fibril(void *arg)
490{
491 char *dev_path;
492 size_t kbd_id = 1;
493 size_t mouse_id = 1;
494 int rc;
495
496 while (true) {
497 async_usleep(DISCOVERY_POLL_INTERVAL);
498
499 /*
500 * Check for new keyboard device
501 */
502 rc = asprintf(&dev_path, "/dev/class/keyboard\\%zu", kbd_id);
503 if (rc < 0)
504 continue;
505
506 if (kbd_add_kbdev(dev_path) == EOK) {
507 printf("%s: Connected keyboard device '%s'\n",
508 NAME, dev_path);
509
510 /* XXX Handle device removal */
511 ++kbd_id;
512 }
513
514 free(dev_path);
515
516 /*
517 * Check for new mouse device
518 */
519 rc = asprintf(&dev_path, "/dev/class/mouse\\%zu", mouse_id);
520 if (rc < 0)
521 continue;
522
523 if (mouse_add_mousedev(dev_path) == EOK) {
524 printf("%s: Connected mouse device '%s'\n",
525 NAME, dev_path);
526
527 /* XXX Handle device removal */
528 ++mouse_id;
529 }
530
531 free(dev_path);
532 }
533
534 return EOK;
535}
536
537/** Start a fibril for discovering new devices. */
538static void input_start_dev_discovery(void)
539{
540 fid_t fid = fibril_create(dev_discovery_fibril, NULL);
541 if (!fid) {
542 printf("%s: Failed to create device discovery fibril.\n",
543 NAME);
544 return;
545 }
546
547 fibril_add_ready(fid);
548}
549
550int main(int argc, char **argv)
551{
552 printf("%s: HelenOS input service\n", NAME);
553
554 sysarg_t obio;
555
556 list_initialize(&kbd_devs);
557 list_initialize(&mouse_devs);
558
559 if ((sysinfo_get_value("kbd.cir.obio", &obio) == EOK) && (obio))
560 irc_service = true;
561
562 if (irc_service) {
563 while (irc_phone < 0)
564 irc_phone = service_obsolete_connect_blocking(SERVICE_IRC, 0, 0);
565 }
566
567 /* Add legacy keyboard devices. */
568 kbd_add_legacy_devs();
569
570 /* Add legacy mouse devices. */
571 mouse_add_legacy_devs();
572
573 /* Register driver */
574 int rc = devmap_driver_register(NAME, client_connection);
575 if (rc < 0) {
576 printf("%s: Unable to register driver (%d)\n", NAME, rc);
577 return -1;
578 }
579
580 char kbd[DEVMAP_NAME_MAXLEN + 1];
581 snprintf(kbd, DEVMAP_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
582
583 devmap_handle_t devmap_handle;
584 if (devmap_device_register(kbd, &devmap_handle) != EOK) {
585 printf("%s: Unable to register device %s\n", NAME, kbd);
586 return -1;
587 }
588
589 /* Start looking for new input devices */
590 input_start_dev_discovery();
591
592 printf("%s: Accepting connections\n", NAME);
593 async_manager();
594
595 /* Not reached. */
596 return 0;
597}
598
599/**
600 * @}
601 */
Note: See TracBrowser for help on using the repository browser.