VFS messaging/interfacing work stage 8a/99: Sync other filesystems to stage 7
[dragonfly.git] / sys / dev / misc / kbd / kbd.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/dev/kbd/kbd.c,v 1.17.2.2 2001/07/30 16:46:43 yokota Exp $
697fa140 27 * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.12 2004/09/19 02:15:44 dillon Exp $
984263bc
MD
28 */
29
30#include "opt_kbd.h"
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/malloc.h>
36#include <sys/conf.h>
37#include <sys/proc.h>
38#include <sys/tty.h>
39#include <sys/poll.h>
40#include <sys/vnode.h>
41#include <sys/uio.h>
42
43#include <machine/console.h>
44
1f2de5d4 45#include "kbdreg.h"
984263bc
MD
46
47#define KBD_INDEX(dev) minor(dev)
48
49typedef struct genkbd_softc {
50 int gkb_flags; /* flag/status bits */
51#define KB_ASLEEP (1 << 0)
52 struct clist gkb_q; /* input queue */
53 struct selinfo gkb_rsel;
54} genkbd_softc_t;
55
56static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
57 SLIST_HEAD_INITIALIZER(keyboard_drivers);
58
dc62b251
MD
59SET_DECLARE(kbddriver_set, const keyboard_driver_t);
60
984263bc
MD
61/* local arrays */
62
63/*
64 * We need at least one entry each in order to initialize a keyboard
65 * for the kernel console. The arrays will be increased dynamically
66 * when necessary.
67 */
68
69static int keyboards = 1;
70static keyboard_t *kbd_ini;
71static keyboard_t **keyboard = &kbd_ini;
72static keyboard_switch_t *kbdsw_ini;
73 keyboard_switch_t **kbdsw = &kbdsw_ini;
74
75#define ARRAY_DELTA 4
76
77static int
78kbd_realloc_array(void)
79{
80 keyboard_t **new_kbd;
81 keyboard_switch_t **new_kbdsw;
82 int newsize;
83 int s;
84
85 s = spltty();
86 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
bf22d4c1
MD
87 new_kbd = malloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
88 M_WAITOK | M_ZERO);
89 new_kbdsw = malloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
90 M_WAITOK | M_ZERO);
984263bc
MD
91 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
92 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
93 if (keyboards > 1) {
94 free(keyboard, M_DEVBUF);
95 free(kbdsw, M_DEVBUF);
96 }
97 keyboard = new_kbd;
98 kbdsw = new_kbdsw;
99 keyboards = newsize;
100 splx(s);
101
102 if (bootverbose)
103 printf("kbd: new array size %d\n", keyboards);
104
105 return 0;
106}
107
108/*
e30db56f
MD
109 * Low-level keyboard driver functions.
110 *
984263bc
MD
111 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
112 * driver, call these functions to initialize the keyboard_t structure
113 * and register it to the virtual keyboard driver `kbd'.
e30db56f
MD
114 *
115 * The reinit call is made when a driver has partially detached a keyboard
116 * but does not unregistered it, then wishes to reinitialize it later on.
117 * This is how the USB keyboard driver handles the 'default' keyboard,
118 * because unregistering the keyboard associated with the console will
119 * destroy its console association forever.
984263bc 120 */
984263bc 121void
e30db56f 122kbd_reinit_struct(keyboard_t *kbd, int config, int pref)
984263bc 123{
e30db56f 124 kbd->kb_flags |= KB_NO_DEVICE; /* device has not been found */
984263bc
MD
125 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
126 kbd->kb_led = 0; /* unknown */
984263bc
MD
127 kbd->kb_data = NULL;
128 kbd->kb_keymap = NULL;
129 kbd->kb_accentmap = NULL;
130 kbd->kb_fkeytab = NULL;
131 kbd->kb_fkeytab_size = 0;
132 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
133 kbd->kb_delay2 = KB_DELAY2;
e30db56f
MD
134 kbd->kb_count = 0;
135 kbd->kb_pref = pref;
984263bc
MD
136 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
137}
138
e30db56f
MD
139/* initialize the keyboard_t structure */
140void
141kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
142 int pref, int port, int port_size)
143{
144 kbd->kb_flags = 0;
145 kbd->kb_name = name;
146 kbd->kb_type = type;
147 kbd->kb_unit = unit;
148 kbd->kb_io_base = port;
149 kbd->kb_io_size = port_size;
150 kbd_reinit_struct(kbd, config, pref);
151}
152
984263bc
MD
153void
154kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
155 fkeytab_t *fkeymap, int fkeymap_size)
156{
157 kbd->kb_keymap = keymap;
158 kbd->kb_accentmap = accmap;
159 kbd->kb_fkeytab = fkeymap;
160 kbd->kb_fkeytab_size = fkeymap_size;
161}
162
163/* declare a new keyboard driver */
164int
165kbd_add_driver(keyboard_driver_t *driver)
166{
167 if (SLIST_NEXT(driver, link))
168 return EINVAL;
169 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
170 return 0;
171}
172
173int
174kbd_delete_driver(keyboard_driver_t *driver)
175{
176 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
177 SLIST_NEXT(driver, link) = NULL;
178 return 0;
179}
180
181/* register a keyboard and associate it with a function table */
182int
183kbd_register(keyboard_t *kbd)
184{
185 const keyboard_driver_t **list;
186 const keyboard_driver_t *p;
187 int index;
188
189 for (index = 0; index < keyboards; ++index) {
190 if (keyboard[index] == NULL)
191 break;
192 }
193 if (index >= keyboards) {
194 if (kbd_realloc_array())
195 return -1;
196 }
197
198 kbd->kb_index = index;
199 KBD_UNBUSY(kbd);
200 KBD_VALID(kbd);
201 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
202 kbd->kb_token = NULL;
203 kbd->kb_callback.kc_func = NULL;
204 kbd->kb_callback.kc_arg = NULL;
205
206 SLIST_FOREACH(p, &keyboard_drivers, link) {
207 if (strcmp(p->name, kbd->kb_name) == 0) {
208 keyboard[index] = kbd;
209 kbdsw[index] = p->kbdsw;
210 return index;
211 }
212 }
dc62b251
MD
213 SET_FOREACH(list, kbddriver_set) {
214 p = *list;
984263bc
MD
215 if (strcmp(p->name, kbd->kb_name) == 0) {
216 keyboard[index] = kbd;
217 kbdsw[index] = p->kbdsw;
218 return index;
219 }
220 }
221
222 return -1;
223}
224
225int
226kbd_unregister(keyboard_t *kbd)
227{
228 int error;
229 int s;
230
231 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
232 return ENOENT;
233 if (keyboard[kbd->kb_index] != kbd)
234 return ENOENT;
235
236 s = spltty();
237 if (KBD_IS_BUSY(kbd)) {
238 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
239 kbd->kb_callback.kc_arg);
240 if (error) {
241 splx(s);
242 return error;
243 }
244 if (KBD_IS_BUSY(kbd)) {
245 splx(s);
246 return EBUSY;
247 }
248 }
249 KBD_INVALID(kbd);
250 keyboard[kbd->kb_index] = NULL;
251 kbdsw[kbd->kb_index] = NULL;
252
253 splx(s);
254 return 0;
255}
256
257/* find a funciton table by the driver name */
258keyboard_switch_t
259*kbd_get_switch(char *driver)
260{
261 const keyboard_driver_t **list;
262 const keyboard_driver_t *p;
263
264 SLIST_FOREACH(p, &keyboard_drivers, link) {
265 if (strcmp(p->name, driver) == 0)
266 return p->kbdsw;
267 }
dc62b251
MD
268 SET_FOREACH(list, kbddriver_set) {
269 p = *list;
984263bc
MD
270 if (strcmp(p->name, driver) == 0)
271 return p->kbdsw;
272 }
273
274 return NULL;
275}
276
277/*
278 * Keyboard client functions
279 * Keyboard clients, such as the console driver `syscons' and the keyboard
280 * cdev driver, use these functions to claim and release a keyboard for
281 * exclusive use.
282 */
283
284/* find the keyboard specified by a driver name and a unit number */
285int
286kbd_find_keyboard(char *driver, int unit)
287{
288 int i;
e30db56f
MD
289 int pref;
290 int pref_index;
291
292 pref = 0;
293 pref_index = -1;
984263bc
MD
294
295 for (i = 0; i < keyboards; ++i) {
296 if (keyboard[i] == NULL)
297 continue;
298 if (!KBD_IS_VALID(keyboard[i]))
299 continue;
300 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
301 continue;
302 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
303 continue;
e30db56f
MD
304 if (pref <= keyboard[i]->kb_pref) {
305 pref = keyboard[i]->kb_pref;
306 pref_index = i;
307 }
984263bc 308 }
e30db56f 309 return (pref_index);
984263bc
MD
310}
311
312/* allocate a keyboard */
313int
314kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
315 void *arg)
316{
317 int index;
318 int s;
319
320 if (func == NULL)
321 return -1;
322
323 s = spltty();
324 index = kbd_find_keyboard(driver, unit);
325 if (index >= 0) {
326 if (KBD_IS_BUSY(keyboard[index])) {
327 splx(s);
328 return -1;
329 }
697fa140 330 callout_init(&keyboard[index]->kb_atkbd_timeout_ch);
984263bc
MD
331 keyboard[index]->kb_token = id;
332 KBD_BUSY(keyboard[index]);
333 keyboard[index]->kb_callback.kc_func = func;
334 keyboard[index]->kb_callback.kc_arg = arg;
335 (*kbdsw[index]->clear_state)(keyboard[index]);
336 }
337 splx(s);
338 return index;
339}
340
341int
342kbd_release(keyboard_t *kbd, void *id)
343{
344 int error;
345 int s;
346
347 s = spltty();
348 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
349 error = EINVAL;
350 } else if (kbd->kb_token != id) {
351 error = EPERM;
352 } else {
697fa140 353 callout_stop(&kbd->kb_atkbd_timeout_ch);
984263bc
MD
354 kbd->kb_token = NULL;
355 KBD_UNBUSY(kbd);
356 kbd->kb_callback.kc_func = NULL;
357 kbd->kb_callback.kc_arg = NULL;
358 (*kbdsw[kbd->kb_index]->clear_state)(kbd);
359 error = 0;
360 }
361 splx(s);
362 return error;
363}
364
365int
366kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
367 void *arg)
368{
369 int error;
370 int s;
371
372 s = spltty();
373 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
374 error = EINVAL;
375 } else if (kbd->kb_token != id) {
376 error = EPERM;
377 } else if (func == NULL) {
378 error = EINVAL;
379 } else {
380 kbd->kb_callback.kc_func = func;
381 kbd->kb_callback.kc_arg = arg;
382 error = 0;
383 }
384 splx(s);
385 return error;
386}
387
388/* get a keyboard structure */
389keyboard_t
390*kbd_get_keyboard(int index)
391{
392 if ((index < 0) || (index >= keyboards))
393 return NULL;
394 if (keyboard[index] == NULL)
395 return NULL;
396 if (!KBD_IS_VALID(keyboard[index]))
397 return NULL;
398 return keyboard[index];
399}
400
401/*
402 * The back door for the console driver; configure keyboards
403 * This function is for the kernel console to initialize keyboards
404 * at very early stage.
405 */
406
407int
408kbd_configure(int flags)
409{
410 const keyboard_driver_t **list;
411 const keyboard_driver_t *p;
412
413 SLIST_FOREACH(p, &keyboard_drivers, link) {
414 if (p->configure != NULL)
415 (*p->configure)(flags);
416 }
dc62b251
MD
417 SET_FOREACH(list, kbddriver_set) {
418 p = *list;
984263bc
MD
419 if (p->configure != NULL)
420 (*p->configure)(flags);
421 }
422
423 return 0;
424}
425
426#ifdef KBD_INSTALL_CDEV
427
428/*
429 * Virtual keyboard cdev driver functions
430 * The virtual keyboard driver dispatches driver functions to
431 * appropriate subdrivers.
432 */
433
434#define KBD_UNIT(dev) minor(dev)
435
436static d_open_t genkbdopen;
437static d_close_t genkbdclose;
438static d_read_t genkbdread;
439static d_write_t genkbdwrite;
440static d_ioctl_t genkbdioctl;
441static d_poll_t genkbdpoll;
442
443#define CDEV_MAJOR 112
444
445static struct cdevsw kbd_cdevsw = {
fabb8ceb
MD
446 /* name */ "kbd",
447 /* maj */ CDEV_MAJOR,
448 /* flags */ 0,
449 /* port */ NULL,
455fcd7e 450 /* clone */ NULL,
fabb8ceb 451
984263bc
MD
452 /* open */ genkbdopen,
453 /* close */ genkbdclose,
454 /* read */ genkbdread,
455 /* write */ genkbdwrite,
456 /* ioctl */ genkbdioctl,
457 /* poll */ genkbdpoll,
458 /* mmap */ nommap,
459 /* strategy */ nostrategy,
984263bc 460 /* dump */ nodump,
fabb8ceb 461 /* psize */ nopsize
984263bc
MD
462};
463
464int
465kbd_attach(keyboard_t *kbd)
466{
467 dev_t dev;
468
469 if (kbd->kb_index >= keyboards)
470 return EINVAL;
471 if (keyboard[kbd->kb_index] != kbd)
472 return EINVAL;
473
e4c9c0c8 474 cdevsw_add(&kbd_cdevsw, -1, kbd->kb_index);
984263bc
MD
475 dev = make_dev(&kbd_cdevsw, kbd->kb_index, UID_ROOT, GID_WHEEL, 0600,
476 "kbd%r", kbd->kb_index);
477 if (dev->si_drv1 == NULL)
478 dev->si_drv1 = malloc(sizeof(genkbd_softc_t), M_DEVBUF,
479 M_WAITOK);
480 bzero(dev->si_drv1, sizeof(genkbd_softc_t));
481
482 printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
483 return 0;
484}
485
486int
487kbd_detach(keyboard_t *kbd)
488{
489 dev_t dev;
490
491 if (kbd->kb_index >= keyboards)
492 return EINVAL;
493 if (keyboard[kbd->kb_index] != kbd)
494 return EINVAL;
495
e4c9c0c8
MD
496 /*
497 * Deal with refs properly. The KBD driver really ought to have
498 * recorded the dev_t separately.
499 */
500 if ((dev = make_adhoc_dev(&kbd_cdevsw, kbd->kb_index)) != NODEV) {
501 if (dev->si_drv1) {
502 free(dev->si_drv1, M_DEVBUF);
503 dev->si_drv1 = NULL;
504 }
505 }
506 cdevsw_remove(&kbd_cdevsw, -1, kbd->kb_index);
984263bc
MD
507 return 0;
508}
509
510/*
511 * Generic keyboard cdev driver functions
512 * Keyboard subdrivers may call these functions to implement common
513 * driver functions.
514 */
515
516#define KB_QSIZE 512
517#define KB_BUFSIZE 64
518
519static kbd_callback_func_t genkbd_event;
520
521static int
41c20dac 522genkbdopen(dev_t dev, int mode, int flag, d_thread_t *td)
984263bc
MD
523{
524 keyboard_t *kbd;
525 genkbd_softc_t *sc;
526 int s;
527 int i;
528
529 s = spltty();
530 sc = dev->si_drv1;
531 kbd = kbd_get_keyboard(KBD_INDEX(dev));
532 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
533 splx(s);
534 return ENXIO;
535 }
536 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
537 genkbd_event, (void *)sc);
538 if (i < 0) {
539 splx(s);
540 return EBUSY;
541 }
542 /* assert(i == kbd->kb_index) */
543 /* assert(kbd == kbd_get_keyboard(i)) */
544
545 /*
546 * NOTE: even when we have successfully claimed a keyboard,
547 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
548 */
549
550#if 0
551 bzero(&sc->gkb_q, sizeof(sc->gkb_q));
552#endif
553 clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
554 sc->gkb_rsel.si_flags = 0;
555 sc->gkb_rsel.si_pid = 0;
556 splx(s);
557
558 return 0;
559}
560
561static int
41c20dac 562genkbdclose(dev_t dev, int mode, int flag, d_thread_t *td)
984263bc
MD
563{
564 keyboard_t *kbd;
565 genkbd_softc_t *sc;
566 int s;
567
568 /*
569 * NOTE: the device may have already become invalid.
570 * kbd == NULL || !KBD_IS_VALID(kbd)
571 */
572 s = spltty();
573 sc = dev->si_drv1;
574 kbd = kbd_get_keyboard(KBD_INDEX(dev));
575 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
576 /* XXX: we shall be forgiving and don't report error... */
577 } else {
578 kbd_release(kbd, (void *)sc);
579#if 0
580 clist_free_cblocks(&sc->gkb_q);
581#endif
582 }
583 splx(s);
584 return 0;
585}
586
587static int
588genkbdread(dev_t dev, struct uio *uio, int flag)
589{
590 keyboard_t *kbd;
591 genkbd_softc_t *sc;
592 u_char buffer[KB_BUFSIZE];
593 int len;
594 int error;
595 int s;
596
597 /* wait for input */
598 s = spltty();
599 sc = dev->si_drv1;
600 kbd = kbd_get_keyboard(KBD_INDEX(dev));
601 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
602 splx(s);
603 return ENXIO;
604 }
605 while (sc->gkb_q.c_cc == 0) {
606 if (flag & IO_NDELAY) {
607 splx(s);
608 return EWOULDBLOCK;
609 }
610 sc->gkb_flags |= KB_ASLEEP;
377d4740 611 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
984263bc
MD
612 kbd = kbd_get_keyboard(KBD_INDEX(dev));
613 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
614 splx(s);
615 return ENXIO; /* our keyboard has gone... */
616 }
617 if (error) {
618 sc->gkb_flags &= ~KB_ASLEEP;
619 splx(s);
620 return error;
621 }
622 }
623 splx(s);
624
625 /* copy as much input as possible */
626 error = 0;
627 while (uio->uio_resid > 0) {
628 len = imin(uio->uio_resid, sizeof(buffer));
629 len = q_to_b(&sc->gkb_q, buffer, len);
630 if (len <= 0)
631 break;
632 error = uiomove(buffer, len, uio);
633 if (error)
634 break;
635 }
636
637 return error;
638}
639
640static int
641genkbdwrite(dev_t dev, struct uio *uio, int flag)
642{
643 keyboard_t *kbd;
644
645 kbd = kbd_get_keyboard(KBD_INDEX(dev));
646 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
647 return ENXIO;
648 return ENODEV;
649}
650
651static int
41c20dac 652genkbdioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, d_thread_t *td)
984263bc
MD
653{
654 keyboard_t *kbd;
655 int error;
656
657 kbd = kbd_get_keyboard(KBD_INDEX(dev));
658 if ((kbd == NULL) || !KBD_IS_VALID(kbd))
659 return ENXIO;
660 error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
661 if (error == ENOIOCTL)
662 error = ENODEV;
663 return error;
664}
665
666static int
41c20dac 667genkbdpoll(dev_t dev, int events, d_thread_t *td)
984263bc
MD
668{
669 keyboard_t *kbd;
670 genkbd_softc_t *sc;
671 int revents;
672 int s;
673
674 revents = 0;
675 s = spltty();
676 sc = dev->si_drv1;
677 kbd = kbd_get_keyboard(KBD_INDEX(dev));
678 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
679 revents = POLLHUP; /* the keyboard has gone */
680 } else if (events & (POLLIN | POLLRDNORM)) {
681 if (sc->gkb_q.c_cc > 0)
682 revents = events & (POLLIN | POLLRDNORM);
683 else
41c20dac 684 selrecord(td, &sc->gkb_rsel);
984263bc
MD
685 }
686 splx(s);
687 return revents;
688}
689
690static int
691genkbd_event(keyboard_t *kbd, int event, void *arg)
692{
693 genkbd_softc_t *sc;
694 size_t len;
695 u_char *cp;
696 int mode;
697 int c;
698
699 /* assert(KBD_IS_VALID(kbd)) */
700 sc = (genkbd_softc_t *)arg;
701
702 switch (event) {
703 case KBDIO_KEYINPUT:
704 break;
705 case KBDIO_UNLOADING:
706 /* the keyboard is going... */
707 kbd_release(kbd, (void *)sc);
708 if (sc->gkb_flags & KB_ASLEEP) {
709 sc->gkb_flags &= ~KB_ASLEEP;
710 wakeup((caddr_t)sc);
711 }
712 selwakeup(&sc->gkb_rsel);
713 return 0;
714 default:
715 return EINVAL;
716 }
717
718 /* obtain the current key input mode */
719 if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
720 mode = K_XLATE;
721
722 /* read all pending input */
723 while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
724 c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
725 if (c == NOKEY)
726 continue;
727 if (c == ERRKEY) /* XXX: ring bell? */
728 continue;
729 if (!KBD_IS_BUSY(kbd))
730 /* the device is not open, discard the input */
731 continue;
732
733 /* store the byte as is for K_RAW and K_CODE modes */
734 if (mode != K_XLATE) {
735 putc(KEYCHAR(c), &sc->gkb_q);
736 continue;
737 }
738
739 /* K_XLATE */
740 if (c & RELKEY) /* key release is ignored */
741 continue;
742
743 /* process special keys; most of them are just ignored... */
744 if (c & SPCLKEY) {
745 switch (KEYCHAR(c)) {
746 default:
747 /* ignore them... */
748 continue;
749 case BTAB: /* a backtab: ESC [ Z */
750 putc(0x1b, &sc->gkb_q);
751 putc('[', &sc->gkb_q);
752 putc('Z', &sc->gkb_q);
753 continue;
754 }
755 }
756
757 /* normal chars, normal chars with the META, function keys */
758 switch (KEYFLAGS(c)) {
759 case 0: /* a normal char */
760 putc(KEYCHAR(c), &sc->gkb_q);
761 break;
762 case MKEY: /* the META flag: prepend ESC */
763 putc(0x1b, &sc->gkb_q);
764 putc(KEYCHAR(c), &sc->gkb_q);
765 break;
766 case FKEY | SPCLKEY: /* a function key, return string */
767 cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
768 KEYCHAR(c), &len);
769 if (cp != NULL) {
770 while (len-- > 0)
771 putc(*cp++, &sc->gkb_q);
772 }
773 break;
774 }
775 }
776
777 /* wake up sleeping/polling processes */
778 if (sc->gkb_q.c_cc > 0) {
779 if (sc->gkb_flags & KB_ASLEEP) {
780 sc->gkb_flags &= ~KB_ASLEEP;
781 wakeup((caddr_t)sc);
782 }
783 selwakeup(&sc->gkb_rsel);
784 }
785
786 return 0;
787}
788
789#endif /* KBD_INSTALL_CDEV */
790
791/*
792 * Generic low-level keyboard functions
793 * The low-level functions in the keyboard subdriver may use these
794 * functions.
795 */
796
797int
798genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
799{
800 keyarg_t *keyp;
801 fkeyarg_t *fkeyp;
802 int s;
803 int i;
804
805 s = spltty();
806 switch (cmd) {
807
808 case KDGKBINFO: /* get keyboard information */
809 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
810 i = imin(strlen(kbd->kb_name) + 1,
811 sizeof(((keyboard_info_t *)arg)->kb_name));
812 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
813 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
814 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
815 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
816 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
817 break;
818
819 case KDGKBTYPE: /* get keyboard type */
820 *(int *)arg = kbd->kb_type;
821 break;
822
823 case KDGETREPEAT: /* get keyboard repeat rate */
824 ((int *)arg)[0] = kbd->kb_delay1;
825 ((int *)arg)[1] = kbd->kb_delay2;
826 break;
827
828 case GIO_KEYMAP: /* get keyboard translation table */
829 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
830 break;
831 case PIO_KEYMAP: /* set keyboard translation table */
832#ifndef KBD_DISABLE_KEYMAP_LOAD
833 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
834 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
835 break;
836#else
837 splx(s);
838 return ENODEV;
839#endif
840
841 case GIO_KEYMAPENT: /* get keyboard translation table entry */
842 keyp = (keyarg_t *)arg;
843 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
844 /sizeof(kbd->kb_keymap->key[0])) {
845 splx(s);
846 return EINVAL;
847 }
848 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
849 sizeof(keyp->key));
850 break;
851 case PIO_KEYMAPENT: /* set keyboard translation table entry */
852#ifndef KBD_DISABLE_KEYMAP_LOAD
853 keyp = (keyarg_t *)arg;
854 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
855 /sizeof(kbd->kb_keymap->key[0])) {
856 splx(s);
857 return EINVAL;
858 }
859 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
860 sizeof(keyp->key));
861 break;
862#else
863 splx(s);
864 return ENODEV;
865#endif
866
867 case GIO_DEADKEYMAP: /* get accent key translation table */
868 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
869 break;
870 case PIO_DEADKEYMAP: /* set accent key translation table */
871#ifndef KBD_DISABLE_KEYMAP_LOAD
872 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
873 break;
874#else
875 splx(s);
876 return ENODEV;
877#endif
878
879 case GETFKEY: /* get functionkey string */
880 fkeyp = (fkeyarg_t *)arg;
881 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
882 splx(s);
883 return EINVAL;
884 }
885 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
886 kbd->kb_fkeytab[fkeyp->keynum].len);
887 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
888 break;
889 case SETFKEY: /* set functionkey string */
890#ifndef KBD_DISABLE_KEYMAP_LOAD
891 fkeyp = (fkeyarg_t *)arg;
892 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
893 splx(s);
894 return EINVAL;
895 }
896 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
897 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
898 kbd->kb_fkeytab[fkeyp->keynum].len);
899 break;
900#else
901 splx(s);
902 return ENODEV;
903#endif
904
905 default:
906 splx(s);
907 return ENOIOCTL;
908 }
909
910 splx(s);
911 return 0;
912}
913
914/* get a pointer to the string associated with the given function key */
915u_char
916*genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
917{
918 if (kbd == NULL)
919 return NULL;
920 fkey -= F_FN;
921 if (fkey > kbd->kb_fkeytab_size)
922 return NULL;
923 *len = kbd->kb_fkeytab[fkey].len;
924 return kbd->kb_fkeytab[fkey].str;
925}
926
927/* diagnostic dump */
928static char
929*get_kbd_type_name(int type)
930{
931 static struct {
932 int type;
933 char *name;
934 } name_table[] = {
935 { KB_84, "AT 84" },
936 { KB_101, "AT 101/102" },
937 { KB_OTHER, "generic" },
938 };
939 int i;
940
941 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
942 if (type == name_table[i].type)
943 return name_table[i].name;
944 }
945 return "unknown";
946}
947
948void
949genkbd_diag(keyboard_t *kbd, int level)
950{
951 if (level > 0) {
952 printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
953 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
954 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
955 kbd->kb_config, kbd->kb_flags);
956 if (kbd->kb_io_base > 0)
957 printf(", port:0x%x-0x%x", kbd->kb_io_base,
958 kbd->kb_io_base + kbd->kb_io_size - 1);
959 printf("\n");
960 }
961}
962
963#define set_lockkey_state(k, s, l) \
964 if (!((s) & l ## DOWN)) { \
965 int i; \
966 (s) |= l ## DOWN; \
967 (s) ^= l ## ED; \
968 i = (s) & LOCK_MASK; \
969 (*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
970 }
971
972static u_int
973save_accent_key(keyboard_t *kbd, u_int key, int *accents)
974{
975 int i;
976
977 /* make an index into the accent map */
978 i = key - F_ACC + 1;
979 if ((i > kbd->kb_accentmap->n_accs)
980 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
981 /* the index is out of range or pointing to an empty entry */
982 *accents = 0;
983 return ERRKEY;
984 }
985
986 /*
987 * If the same accent key has been hit twice, produce the accent char
988 * itself.
989 */
990 if (i == *accents) {
991 key = kbd->kb_accentmap->acc[i - 1].accchar;
992 *accents = 0;
993 return key;
994 }
995
996 /* remember the index and wait for the next key */
997 *accents = i;
998 return NOKEY;
999}
1000
1001static u_int
1002make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1003{
1004 struct acc_t *acc;
1005 int i;
1006
1007 acc = &kbd->kb_accentmap->acc[*accents - 1];
1008 *accents = 0;
1009
1010 /*
1011 * If the accent key is followed by the space key,
1012 * produce the accent char itself.
1013 */
1014 if (ch == ' ')
1015 return acc->accchar;
1016
1017 /* scan the accent map */
1018 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1019 if (acc->map[i][0] == 0) /* end of table */
1020 break;
1021 if (acc->map[i][0] == ch)
1022 return acc->map[i][1];
1023 }
1024 /* this char cannot be accented... */
1025 return ERRKEY;
1026}
1027
1028int
1029genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1030 int *accents)
1031{
1032 struct keyent_t *key;
1033 int state = *shiftstate;
1034 int action;
1035 int f;
1036 int i;
1037
1038 i = keycode;
1039 f = state & (AGRS | ALKED);
1040 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1041 i += ALTGR_OFFSET;
1042 key = &kbd->kb_keymap->key[i];
1043 i = ((state & SHIFTS) ? 1 : 0)
1044 | ((state & CTLS) ? 2 : 0)
1045 | ((state & ALTS) ? 4 : 0);
1046 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1047 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1048 i ^= 1;
1049
1050 if (up) { /* break: key released */
1051 action = kbd->kb_lastact[keycode];
1052 kbd->kb_lastact[keycode] = NOP;
1053 switch (action) {
1054 case LSHA:
1055 if (state & SHIFTAON) {
1056 set_lockkey_state(kbd, state, ALK);
1057 state &= ~ALKDOWN;
1058 }
1059 action = LSH;
1060 /* FALL THROUGH */
1061 case LSH:
1062 state &= ~SHIFTS1;
1063 break;
1064 case RSHA:
1065 if (state & SHIFTAON) {
1066 set_lockkey_state(kbd, state, ALK);
1067 state &= ~ALKDOWN;
1068 }
1069 action = RSH;
1070 /* FALL THROUGH */
1071 case RSH:
1072 state &= ~SHIFTS2;
1073 break;
1074 case LCTRA:
1075 if (state & SHIFTAON) {
1076 set_lockkey_state(kbd, state, ALK);
1077 state &= ~ALKDOWN;
1078 }
1079 action = LCTR;
1080 /* FALL THROUGH */
1081 case LCTR:
1082 state &= ~CTLS1;
1083 break;
1084 case RCTRA:
1085 if (state & SHIFTAON) {
1086 set_lockkey_state(kbd, state, ALK);
1087 state &= ~ALKDOWN;
1088 }
1089 action = RCTR;
1090 /* FALL THROUGH */
1091 case RCTR:
1092 state &= ~CTLS2;
1093 break;
1094 case LALTA:
1095 if (state & SHIFTAON) {
1096 set_lockkey_state(kbd, state, ALK);
1097 state &= ~ALKDOWN;
1098 }
1099 action = LALT;
1100 /* FALL THROUGH */
1101 case LALT:
1102 state &= ~ALTS1;
1103 break;
1104 case RALTA:
1105 if (state & SHIFTAON) {
1106 set_lockkey_state(kbd, state, ALK);
1107 state &= ~ALKDOWN;
1108 }
1109 action = RALT;
1110 /* FALL THROUGH */
1111 case RALT:
1112 state &= ~ALTS2;
1113 break;
1114 case ASH:
1115 state &= ~AGRS1;
1116 break;
1117 case META:
1118 state &= ~METAS1;
1119 break;
1120 case NLK:
1121 state &= ~NLKDOWN;
1122 break;
1123 case CLK:
1124#ifndef PC98
1125 state &= ~CLKDOWN;
1126#else
1127 state &= ~CLKED;
1128 i = state & LOCK_MASK;
1129 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1130 (caddr_t)&i);
1131#endif
1132 break;
1133 case SLK:
1134 state &= ~SLKDOWN;
1135 break;
1136 case ALK:
1137 state &= ~ALKDOWN;
1138 break;
1139 case NOP:
1140 /* release events of regular keys are not reported */
1141 *shiftstate &= ~SHIFTAON;
1142 return NOKEY;
1143 }
1144 *shiftstate = state & ~SHIFTAON;
1145 return (SPCLKEY | RELKEY | action);
1146 } else { /* make: key pressed */
1147 action = key->map[i];
1148 state &= ~SHIFTAON;
1149 if (key->spcl & (0x80 >> i)) {
1150 /* special keys */
1151 if (kbd->kb_lastact[keycode] == NOP)
1152 kbd->kb_lastact[keycode] = action;
1153 if (kbd->kb_lastact[keycode] != action)
1154 action = NOP;
1155 switch (action) {
1156 /* LOCKING KEYS */
1157 case NLK:
1158 set_lockkey_state(kbd, state, NLK);
1159 break;
1160 case CLK:
1161#ifndef PC98
1162 set_lockkey_state(kbd, state, CLK);
1163#else
1164 state |= CLKED;
1165 i = state & LOCK_MASK;
1166 (*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
1167 (caddr_t)&i);
1168#endif
1169 break;
1170 case SLK:
1171 set_lockkey_state(kbd, state, SLK);
1172 break;
1173 case ALK:
1174 set_lockkey_state(kbd, state, ALK);
1175 break;
1176 /* NON-LOCKING KEYS */
1177 case SPSC: case RBT: case SUSP: case STBY:
1178 case DBG: case NEXT: case PREV: case PNC:
1179 case HALT: case PDWN:
1180 *accents = 0;
1181 break;
1182 case BTAB:
1183 *accents = 0;
1184 action |= BKEY;
1185 break;
1186 case LSHA:
1187 state |= SHIFTAON;
1188 action = LSH;
1189 /* FALL THROUGH */
1190 case LSH:
1191 state |= SHIFTS1;
1192 break;
1193 case RSHA:
1194 state |= SHIFTAON;
1195 action = RSH;
1196 /* FALL THROUGH */
1197 case RSH:
1198 state |= SHIFTS2;
1199 break;
1200 case LCTRA:
1201 state |= SHIFTAON;
1202 action = LCTR;
1203 /* FALL THROUGH */
1204 case LCTR:
1205 state |= CTLS1;
1206 break;
1207 case RCTRA:
1208 state |= SHIFTAON;
1209 action = RCTR;
1210 /* FALL THROUGH */
1211 case RCTR:
1212 state |= CTLS2;
1213 break;
1214 case LALTA:
1215 state |= SHIFTAON;
1216 action = LALT;
1217 /* FALL THROUGH */
1218 case LALT:
1219 state |= ALTS1;
1220 break;
1221 case RALTA:
1222 state |= SHIFTAON;
1223 action = RALT;
1224 /* FALL THROUGH */
1225 case RALT:
1226 state |= ALTS2;
1227 break;
1228 case ASH:
1229 state |= AGRS1;
1230 break;
1231 case META:
1232 state |= METAS1;
1233 break;
1234 case NOP:
1235 *shiftstate = state;
1236 return NOKEY;
1237 default:
1238 /* is this an accent (dead) key? */
1239 *shiftstate = state;
1240 if (action >= F_ACC && action <= L_ACC) {
1241 action = save_accent_key(kbd, action,
1242 accents);
1243 switch (action) {
1244 case NOKEY:
1245 case ERRKEY:
1246 return action;
1247 default:
1248 if (state & METAS)
1249 return (action | MKEY);
1250 else
1251 return action;
1252 }
1253 /* NOT REACHED */
1254 }
1255 /* other special keys */
1256 if (*accents > 0) {
1257 *accents = 0;
1258 return ERRKEY;
1259 }
1260 if (action >= F_FN && action <= L_FN)
1261 action |= FKEY;
1262 /* XXX: return fkey string for the FKEY? */
1263 return (SPCLKEY | action);
1264 }
1265 *shiftstate = state;
1266 return (SPCLKEY | action);
1267 } else {
1268 /* regular keys */
1269 kbd->kb_lastact[keycode] = NOP;
1270 *shiftstate = state;
1271 if (*accents > 0) {
1272 /* make an accented char */
1273 action = make_accent_char(kbd, action, accents);
1274 if (action == ERRKEY)
1275 return action;
1276 }
1277 if (state & METAS)
1278 action |= MKEY;
1279 return action;
1280 }
1281 }
1282 /* NOT REACHED */
1283}