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