kernel - Fix multiple tty_token issues related to vt switching and X
[dragonfly.git] / sys / dev / misc / kbd / kbd.c
CommitLineData
984263bc 1/*-
22ff886e
AH
2 * (MPSAFE)
3 *
984263bc
MD
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/dev/kbd/kbd.c,v 1.17.2.2 2001/07/30 16:46:43 yokota Exp $
028066b1 29 * $DragonFly: src/sys/dev/misc/kbd/kbd.c,v 1.23 2007/05/08 02:31:39 dillon Exp $
a38e57d5
MD
30 */
31/*
32 * Generic keyboard driver.
33 *
34 * Interrupt note: keyboards use clist functions and since usb keyboard
35 * interrupts are not protected by spltty(), we must use a critical section
36 * to protect against corruption.
bcc53404 37 * XXX: this keyboard driver doesn't use clist functions anymore!
22ff886e
AH
38 *
39 * MPSAFE NOTE: all keyboards could easily be put under a different global token.
984263bc
MD
40 */
41
42#include "opt_kbd.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/malloc.h>
48#include <sys/conf.h>
49#include <sys/proc.h>
50#include <sys/tty.h>
25fef0f3 51#include <sys/event.h>
984263bc
MD
52#include <sys/vnode.h>
53#include <sys/uio.h>
a38e57d5
MD
54#include <sys/thread.h>
55#include <sys/thread2.h>
984263bc
MD
56
57#include <machine/console.h>
58
1f2de5d4 59#include "kbdreg.h"
984263bc 60
22ff886e
AH
61#if 0
62#define lwkt_gettoken(x)
63#define lwkt_reltoken(x)
64#endif
65
984263bc
MD
66#define KBD_INDEX(dev) minor(dev)
67
bcc53404
AH
68#define KB_QSIZE 512
69#define KB_BUFSIZE 64
70
8a0a8f43 71struct genkbd_softc {
984263bc
MD
72 int gkb_flags; /* flag/status bits */
73#define KB_ASLEEP (1 << 0)
5b22f1a7 74 struct kqinfo gkb_rkq;
bcc53404
AH
75 char gkb_q[KB_QSIZE]; /* input queue */
76 unsigned int gkb_q_start;
77 unsigned int gkb_q_length;
8a0a8f43
MD
78};
79
80typedef struct genkbd_softc *genkbd_softc_t;
984263bc
MD
81
82static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
83 SLIST_HEAD_INITIALIZER(keyboard_drivers);
84
dc62b251
MD
85SET_DECLARE(kbddriver_set, const keyboard_driver_t);
86
984263bc
MD
87/* local arrays */
88
89/*
90 * We need at least one entry each in order to initialize a keyboard
91 * for the kernel console. The arrays will be increased dynamically
92 * when necessary.
93 */
94
95static int keyboards = 1;
96static keyboard_t *kbd_ini;
97static keyboard_t **keyboard = &kbd_ini;
98static keyboard_switch_t *kbdsw_ini;
99 keyboard_switch_t **kbdsw = &kbdsw_ini;
100
101#define ARRAY_DELTA 4
102
103static int
104kbd_realloc_array(void)
105{
106 keyboard_t **new_kbd;
107 keyboard_switch_t **new_kbdsw;
108 int newsize;
984263bc 109
22ff886e 110 lwkt_gettoken(&tty_token);
984263bc 111 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
efda3bd0 112 new_kbd = kmalloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
bf22d4c1 113 M_WAITOK | M_ZERO);
efda3bd0 114 new_kbdsw = kmalloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
bf22d4c1 115 M_WAITOK | M_ZERO);
984263bc
MD
116 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
117 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
a38e57d5 118 crit_enter();
984263bc 119 if (keyboards > 1) {
efda3bd0
MD
120 kfree(keyboard, M_DEVBUF);
121 kfree(kbdsw, M_DEVBUF);
984263bc
MD
122 }
123 keyboard = new_kbd;
124 kbdsw = new_kbdsw;
125 keyboards = newsize;
a38e57d5 126 crit_exit();
984263bc
MD
127
128 if (bootverbose)
e3869ec7 129 kprintf("kbd: new array size %d\n", keyboards);
984263bc 130
22ff886e 131 lwkt_reltoken(&tty_token);
984263bc
MD
132 return 0;
133}
134
135/*
e30db56f
MD
136 * Low-level keyboard driver functions.
137 *
984263bc
MD
138 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
139 * driver, call these functions to initialize the keyboard_t structure
140 * and register it to the virtual keyboard driver `kbd'.
e30db56f
MD
141 *
142 * The reinit call is made when a driver has partially detached a keyboard
143 * but does not unregistered it, then wishes to reinitialize it later on.
144 * This is how the USB keyboard driver handles the 'default' keyboard,
145 * because unregistering the keyboard associated with the console will
146 * destroy its console association forever.
984263bc 147 */
984263bc 148void
e30db56f 149kbd_reinit_struct(keyboard_t *kbd, int config, int pref)
984263bc 150{
22ff886e 151 lwkt_gettoken(&tty_token);
e30db56f 152 kbd->kb_flags |= KB_NO_DEVICE; /* device has not been found */
984263bc
MD
153 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
154 kbd->kb_led = 0; /* unknown */
984263bc
MD
155 kbd->kb_data = NULL;
156 kbd->kb_keymap = NULL;
157 kbd->kb_accentmap = NULL;
158 kbd->kb_fkeytab = NULL;
159 kbd->kb_fkeytab_size = 0;
160 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
161 kbd->kb_delay2 = KB_DELAY2;
e30db56f
MD
162 kbd->kb_count = 0;
163 kbd->kb_pref = pref;
984263bc 164 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
22ff886e 165 lwkt_reltoken(&tty_token);
984263bc
MD
166}
167
e30db56f
MD
168/* initialize the keyboard_t structure */
169void
170kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
171 int pref, int port, int port_size)
172{
22ff886e 173 lwkt_gettoken(&tty_token);
e30db56f
MD
174 kbd->kb_flags = 0;
175 kbd->kb_name = name;
176 kbd->kb_type = type;
177 kbd->kb_unit = unit;
178 kbd->kb_io_base = port;
179 kbd->kb_io_size = port_size;
180 kbd_reinit_struct(kbd, config, pref);
fc9700a1 181 lockinit(&kbd->kb_lock, name, 0, LK_CANRECURSE);
22ff886e 182 lwkt_reltoken(&tty_token);
e30db56f
MD
183}
184
984263bc
MD
185void
186kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
187 fkeytab_t *fkeymap, int fkeymap_size)
188{
22ff886e 189 lwkt_gettoken(&tty_token);
984263bc
MD
190 kbd->kb_keymap = keymap;
191 kbd->kb_accentmap = accmap;
192 kbd->kb_fkeytab = fkeymap;
193 kbd->kb_fkeytab_size = fkeymap_size;
22ff886e 194 lwkt_reltoken(&tty_token);
984263bc
MD
195}
196
197/* declare a new keyboard driver */
198int
199kbd_add_driver(keyboard_driver_t *driver)
200{
22ff886e
AH
201 lwkt_gettoken(&tty_token);
202 if (SLIST_NEXT(driver, link)) {
203 lwkt_reltoken(&tty_token);
984263bc 204 return EINVAL;
22ff886e 205 }
984263bc 206 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
22ff886e 207 lwkt_reltoken(&tty_token);
984263bc
MD
208 return 0;
209}
210
211int
212kbd_delete_driver(keyboard_driver_t *driver)
213{
22ff886e 214 lwkt_gettoken(&tty_token);
984263bc
MD
215 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
216 SLIST_NEXT(driver, link) = NULL;
22ff886e 217 lwkt_reltoken(&tty_token);
984263bc
MD
218 return 0;
219}
220
221/* register a keyboard and associate it with a function table */
222int
223kbd_register(keyboard_t *kbd)
224{
225 const keyboard_driver_t **list;
226 const keyboard_driver_t *p;
bcc53404
AH
227 keyboard_t *mux;
228 keyboard_info_t ki;
984263bc
MD
229 int index;
230
22ff886e 231 lwkt_gettoken(&tty_token);
bcc53404
AH
232 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
233
984263bc
MD
234 for (index = 0; index < keyboards; ++index) {
235 if (keyboard[index] == NULL)
236 break;
237 }
238 if (index >= keyboards) {
22ff886e
AH
239 if (kbd_realloc_array()) {
240 lwkt_reltoken(&tty_token);
984263bc 241 return -1;
22ff886e 242 }
984263bc
MD
243 }
244
245 kbd->kb_index = index;
246 KBD_UNBUSY(kbd);
247 KBD_VALID(kbd);
248 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
249 kbd->kb_token = NULL;
250 kbd->kb_callback.kc_func = NULL;
251 kbd->kb_callback.kc_arg = NULL;
22ff886e 252 callout_init_mp(&kbd->kb_atkbd_timeout_ch);
984263bc
MD
253
254 SLIST_FOREACH(p, &keyboard_drivers, link) {
255 if (strcmp(p->name, kbd->kb_name) == 0) {
256 keyboard[index] = kbd;
257 kbdsw[index] = p->kbdsw;
bcc53404
AH
258
259 if (mux != NULL) {
260 bzero(&ki, sizeof(ki));
261 strcpy(ki.kb_name, kbd->kb_name);
262 ki.kb_unit = kbd->kb_unit;
263 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
264 }
265
22ff886e 266 lwkt_reltoken(&tty_token);
984263bc
MD
267 return index;
268 }
269 }
dc62b251
MD
270 SET_FOREACH(list, kbddriver_set) {
271 p = *list;
984263bc
MD
272 if (strcmp(p->name, kbd->kb_name) == 0) {
273 keyboard[index] = kbd;
274 kbdsw[index] = p->kbdsw;
bcc53404
AH
275
276 if (mux != NULL) {
277 bzero(&ki, sizeof(ki));
278 strcpy(ki.kb_name, kbd->kb_name);
279 ki.kb_unit = kbd->kb_unit;
280 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
281 }
282
22ff886e 283 lwkt_reltoken(&tty_token);
984263bc
MD
284 return index;
285 }
286 }
287
22ff886e 288 lwkt_reltoken(&tty_token);
984263bc
MD
289 return -1;
290}
291
292int
293kbd_unregister(keyboard_t *kbd)
294{
295 int error;
984263bc 296
22ff886e
AH
297 lwkt_gettoken(&tty_token);
298 if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards)) {
299 lwkt_reltoken(&tty_token);
984263bc 300 return ENOENT;
22ff886e
AH
301 }
302 if (keyboard[kbd->kb_index] != kbd) {
303 lwkt_reltoken(&tty_token);
984263bc 304 return ENOENT;
22ff886e 305 }
984263bc 306
a38e57d5 307 crit_enter();
c5e92a81 308 callout_stop(&kbd->kb_atkbd_timeout_ch);
984263bc
MD
309 if (KBD_IS_BUSY(kbd)) {
310 error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
311 kbd->kb_callback.kc_arg);
312 if (error) {
a38e57d5 313 crit_exit();
22ff886e 314 lwkt_reltoken(&tty_token);
984263bc
MD
315 return error;
316 }
317 if (KBD_IS_BUSY(kbd)) {
a38e57d5 318 crit_exit();
22ff886e 319 lwkt_reltoken(&tty_token);
984263bc
MD
320 return EBUSY;
321 }
322 }
fc9700a1 323 KBD_CONFIG_LOST(kbd);
984263bc
MD
324 KBD_INVALID(kbd);
325 keyboard[kbd->kb_index] = NULL;
326 kbdsw[kbd->kb_index] = NULL;
327
fc9700a1
MD
328 KBD_ALWAYS_UNLOCK(kbd);
329 lockuninit(&kbd->kb_lock);
330
a38e57d5 331 crit_exit();
22ff886e 332 lwkt_reltoken(&tty_token);
984263bc
MD
333 return 0;
334}
335
336/* find a funciton table by the driver name */
3d8ba483
SW
337keyboard_switch_t *
338kbd_get_switch(char *driver)
984263bc
MD
339{
340 const keyboard_driver_t **list;
341 const keyboard_driver_t *p;
342
22ff886e
AH
343 lwkt_gettoken(&tty_token);
344
984263bc 345 SLIST_FOREACH(p, &keyboard_drivers, link) {
22ff886e
AH
346 if (strcmp(p->name, driver) == 0) {
347 lwkt_reltoken(&tty_token);
984263bc 348 return p->kbdsw;
22ff886e 349 }
984263bc 350 }
dc62b251
MD
351 SET_FOREACH(list, kbddriver_set) {
352 p = *list;
22ff886e
AH
353 if (strcmp(p->name, driver) == 0) {
354 lwkt_reltoken(&tty_token);
984263bc 355 return p->kbdsw;
22ff886e 356 }
984263bc
MD
357 }
358
22ff886e 359 lwkt_reltoken(&tty_token);
984263bc
MD
360 return NULL;
361}
362
363/*
364 * Keyboard client functions
365 * Keyboard clients, such as the console driver `syscons' and the keyboard
366 * cdev driver, use these functions to claim and release a keyboard for
367 * exclusive use.
368 */
bcc53404
AH
369/*
370 * find the keyboard specified by a driver name and a unit number
371 * starting at given index
372 */
984263bc 373int
bcc53404 374kbd_find_keyboard2(char *driver, int unit, int index, int legacy)
984263bc
MD
375{
376 int i;
e30db56f
MD
377 int pref;
378 int pref_index;
379
380 pref = 0;
381 pref_index = -1;
984263bc 382
22ff886e
AH
383 lwkt_gettoken(&tty_token);
384 if ((index < 0) || (index >= keyboards)) {
385 lwkt_reltoken(&tty_token);
bcc53404 386 return (-1);
22ff886e 387 }
bcc53404
AH
388
389 for (i = index; i < keyboards; ++i) {
984263bc
MD
390 if (keyboard[i] == NULL)
391 continue;
392 if (!KBD_IS_VALID(keyboard[i]))
393 continue;
394 if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
395 continue;
396 if ((unit != -1) && (keyboard[i]->kb_unit != unit))
397 continue;
bcc53404
AH
398 /*
399 * If we are in legacy mode, we do the old preference magic and
400 * don't return on the first found unit.
401 */
402 if (legacy) {
403 if (pref <= keyboard[i]->kb_pref) {
404 pref = keyboard[i]->kb_pref;
405 pref_index = i;
406 }
407 } else {
22ff886e 408 lwkt_reltoken(&tty_token);
bcc53404 409 return i;
e30db56f 410 }
984263bc 411 }
bcc53404
AH
412
413 if (!legacy)
414 KKASSERT(pref_index == -1);
415
22ff886e 416 lwkt_reltoken(&tty_token);
e30db56f 417 return (pref_index);
984263bc
MD
418}
419
bcc53404
AH
420/* find the keyboard specified by a driver name and a unit number */
421int
422kbd_find_keyboard(char *driver, int unit)
423{
424 return (kbd_find_keyboard2(driver, unit, 0, 1));
425}
426
984263bc
MD
427/* allocate a keyboard */
428int
429kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
430 void *arg)
431{
432 int index;
984263bc
MD
433
434 if (func == NULL)
435 return -1;
436
a38e57d5 437 crit_enter();
22ff886e
AH
438 lwkt_gettoken(&tty_token);
439
984263bc
MD
440 index = kbd_find_keyboard(driver, unit);
441 if (index >= 0) {
442 if (KBD_IS_BUSY(keyboard[index])) {
a38e57d5 443 crit_exit();
766a68c8 444 lwkt_reltoken(&tty_token);
984263bc
MD
445 return -1;
446 }
447 keyboard[index]->kb_token = id;
448 KBD_BUSY(keyboard[index]);
449 keyboard[index]->kb_callback.kc_func = func;
450 keyboard[index]->kb_callback.kc_arg = arg;
bcc53404 451 kbd_clear_state(keyboard[index]);
984263bc 452 }
22ff886e
AH
453
454 lwkt_reltoken(&tty_token);
a38e57d5 455 crit_exit();
984263bc
MD
456 return index;
457}
458
459int
460kbd_release(keyboard_t *kbd, void *id)
461{
462 int error;
984263bc 463
a38e57d5 464 crit_enter();
22ff886e
AH
465 lwkt_gettoken(&tty_token);
466
984263bc
MD
467 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
468 error = EINVAL;
469 } else if (kbd->kb_token != id) {
470 error = EPERM;
471 } else {
472 kbd->kb_token = NULL;
473 KBD_UNBUSY(kbd);
474 kbd->kb_callback.kc_func = NULL;
475 kbd->kb_callback.kc_arg = NULL;
bcc53404 476 kbd_clear_state(kbd);
984263bc
MD
477 error = 0;
478 }
22ff886e
AH
479
480 lwkt_reltoken(&tty_token);
a38e57d5 481 crit_exit();
984263bc
MD
482 return error;
483}
484
485int
486kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
487 void *arg)
488{
489 int error;
984263bc 490
a38e57d5 491 crit_enter();
22ff886e
AH
492 lwkt_gettoken(&tty_token);
493
984263bc
MD
494 if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
495 error = EINVAL;
496 } else if (kbd->kb_token != id) {
497 error = EPERM;
498 } else if (func == NULL) {
499 error = EINVAL;
500 } else {
501 kbd->kb_callback.kc_func = func;
502 kbd->kb_callback.kc_arg = arg;
503 error = 0;
504 }
22ff886e
AH
505
506 lwkt_reltoken(&tty_token);
a38e57d5 507 crit_exit();
984263bc
MD
508 return error;
509}
510
511/* get a keyboard structure */
3d8ba483
SW
512keyboard_t *
513kbd_get_keyboard(int index)
984263bc 514{
22ff886e
AH
515 keyboard_t *kbd;
516
517 lwkt_gettoken(&tty_token);
518 if ((index < 0) || (index >= keyboards)) {
519 lwkt_reltoken(&tty_token);
984263bc 520 return NULL;
22ff886e
AH
521 }
522 if (keyboard[index] == NULL) {
523 lwkt_reltoken(&tty_token);
984263bc 524 return NULL;
22ff886e
AH
525 }
526 if (!KBD_IS_VALID(keyboard[index])) {
527 lwkt_reltoken(&tty_token);
984263bc 528 return NULL;
22ff886e
AH
529 }
530 kbd = keyboard[index];
531 lwkt_reltoken(&tty_token);
532
533 return kbd;
984263bc
MD
534}
535
536/*
537 * The back door for the console driver; configure keyboards
538 * This function is for the kernel console to initialize keyboards
539 * at very early stage.
540 */
541
542int
543kbd_configure(int flags)
544{
545 const keyboard_driver_t **list;
546 const keyboard_driver_t *p;
547
22ff886e
AH
548 lwkt_gettoken(&tty_token);
549
984263bc
MD
550 SLIST_FOREACH(p, &keyboard_drivers, link) {
551 if (p->configure != NULL)
552 (*p->configure)(flags);
553 }
dc62b251
MD
554 SET_FOREACH(list, kbddriver_set) {
555 p = *list;
984263bc
MD
556 if (p->configure != NULL)
557 (*p->configure)(flags);
558 }
559
22ff886e 560 lwkt_reltoken(&tty_token);
984263bc
MD
561 return 0;
562}
563
564#ifdef KBD_INSTALL_CDEV
565
566/*
567 * Virtual keyboard cdev driver functions
568 * The virtual keyboard driver dispatches driver functions to
569 * appropriate subdrivers.
570 */
571
572#define KBD_UNIT(dev) minor(dev)
573
574static d_open_t genkbdopen;
575static d_close_t genkbdclose;
576static d_read_t genkbdread;
577static d_write_t genkbdwrite;
578static d_ioctl_t genkbdioctl;
25fef0f3
SG
579static d_kqfilter_t genkbdkqfilter;
580
581static void genkbdfiltdetach(struct knote *);
582static int genkbdfilter(struct knote *, long);
984263bc
MD
583
584#define CDEV_MAJOR 112
585
fef8985e 586static struct dev_ops kbd_ops = {
d4b8aec4 587 { "kbd", CDEV_MAJOR, 0 },
fef8985e
MD
588 .d_open = genkbdopen,
589 .d_close = genkbdclose,
590 .d_read = genkbdread,
591 .d_write = genkbdwrite,
592 .d_ioctl = genkbdioctl,
25fef0f3 593 .d_kqfilter = genkbdkqfilter
984263bc
MD
594};
595
8a0a8f43
MD
596/*
597 * Attach a keyboard.
598 *
599 * NOTE: The usb driver does not detach the default keyboard if it is
600 * unplugged, but calls kbd_attach() when it is plugged back in.
601 */
984263bc
MD
602int
603kbd_attach(keyboard_t *kbd)
604{
b13267a5 605 cdev_t dev;
984263bc 606
22ff886e
AH
607 lwkt_gettoken(&tty_token);
608 if (kbd->kb_index >= keyboards) {
609 lwkt_reltoken(&tty_token);
984263bc 610 return EINVAL;
22ff886e
AH
611 }
612 if (keyboard[kbd->kb_index] != kbd) {
613 lwkt_reltoken(&tty_token);
984263bc 614 return EINVAL;
22ff886e 615 }
984263bc 616
8a0a8f43
MD
617 if (kbd->kb_dev == NULL) {
618 kbd->kb_dev = make_dev(&kbd_ops, kbd->kb_index,
619 UID_ROOT, GID_WHEEL, 0600,
620 "kbd%r", kbd->kb_index);
621 }
622 dev = kbd->kb_dev;
623 if (dev->si_drv1 == NULL) {
624 dev->si_drv1 = kmalloc(sizeof(struct genkbd_softc), M_DEVBUF,
625 M_WAITOK);
626 }
627 bzero(dev->si_drv1, sizeof(struct genkbd_softc));
984263bc 628
e3869ec7 629 kprintf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
22ff886e 630 lwkt_reltoken(&tty_token);
984263bc
MD
631 return 0;
632}
633
634int
635kbd_detach(keyboard_t *kbd)
636{
b13267a5 637 cdev_t dev;
984263bc 638
22ff886e
AH
639 lwkt_gettoken(&tty_token);
640
641 if (kbd->kb_index >= keyboards) {
642 lwkt_reltoken(&tty_token);
984263bc 643 return EINVAL;
22ff886e
AH
644 }
645 if (keyboard[kbd->kb_index] != kbd) {
646 lwkt_reltoken(&tty_token);
984263bc 647 return EINVAL;
22ff886e 648 }
984263bc 649
8a0a8f43 650 if ((dev = kbd->kb_dev) != NULL) {
e4c9c0c8 651 if (dev->si_drv1) {
efda3bd0 652 kfree(dev->si_drv1, M_DEVBUF);
e4c9c0c8
MD
653 dev->si_drv1 = NULL;
654 }
8a0a8f43 655 kbd->kb_dev = NULL;
e4c9c0c8 656 }
cd29885a 657 dev_ops_remove_minor(&kbd_ops, kbd->kb_index);
22ff886e 658 lwkt_reltoken(&tty_token);
984263bc
MD
659 return 0;
660}
661
662/*
663 * Generic keyboard cdev driver functions
664 * Keyboard subdrivers may call these functions to implement common
665 * driver functions.
666 */
667
bcc53404
AH
668static void
669genkbd_putc(genkbd_softc_t sc, char c)
670{
671 unsigned int p;
672
22ff886e
AH
673 lwkt_gettoken(&tty_token);
674
675 if (sc->gkb_q_length == KB_QSIZE) {
676 lwkt_reltoken(&tty_token);
bcc53404 677 return;
22ff886e 678 }
bcc53404
AH
679
680 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
681 sc->gkb_q[p] = c;
682 sc->gkb_q_length++;
22ff886e
AH
683
684 lwkt_reltoken(&tty_token);
bcc53404
AH
685}
686
687static size_t
688genkbd_getc(genkbd_softc_t sc, char *buf, size_t len)
689{
690
22ff886e
AH
691 lwkt_gettoken(&tty_token);
692
bcc53404 693 /* Determine copy size. */
22ff886e
AH
694 if (sc->gkb_q_length == 0) {
695 lwkt_reltoken(&tty_token);
bcc53404 696 return (0);
22ff886e 697 }
bcc53404
AH
698 if (len >= sc->gkb_q_length)
699 len = sc->gkb_q_length;
700 if (len >= KB_QSIZE - sc->gkb_q_start)
701 len = KB_QSIZE - sc->gkb_q_start;
702
703 /* Copy out data and progress offset. */
704 memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
705 sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
706 sc->gkb_q_length -= len;
707
22ff886e 708 lwkt_reltoken(&tty_token);
bcc53404
AH
709 return (len);
710}
984263bc
MD
711
712static kbd_callback_func_t genkbd_event;
713
714static int
fef8985e 715genkbdopen(struct dev_open_args *ap)
984263bc 716{
b13267a5 717 cdev_t dev = ap->a_head.a_dev;
984263bc 718 keyboard_t *kbd;
8a0a8f43 719 genkbd_softc_t sc;
984263bc
MD
720 int i;
721
a38e57d5 722 crit_enter();
22ff886e 723 lwkt_gettoken(&tty_token);
984263bc
MD
724 sc = dev->si_drv1;
725 kbd = kbd_get_keyboard(KBD_INDEX(dev));
726 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
22ff886e 727 lwkt_reltoken(&tty_token);
a38e57d5 728 crit_exit();
984263bc
MD
729 return ENXIO;
730 }
731 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
732 genkbd_event, (void *)sc);
733 if (i < 0) {
22ff886e 734 lwkt_reltoken(&tty_token);
a38e57d5 735 crit_exit();
984263bc
MD
736 return EBUSY;
737 }
738 /* assert(i == kbd->kb_index) */
739 /* assert(kbd == kbd_get_keyboard(i)) */
740
741 /*
742 * NOTE: even when we have successfully claimed a keyboard,
743 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
744 */
745
bcc53404 746 sc->gkb_q_length = 0;
22ff886e 747 lwkt_reltoken(&tty_token);
a38e57d5 748 crit_exit();
984263bc
MD
749
750 return 0;
751}
752
753static int
fef8985e 754genkbdclose(struct dev_close_args *ap)
984263bc 755{
b13267a5 756 cdev_t dev = ap->a_head.a_dev;
984263bc 757 keyboard_t *kbd;
8a0a8f43 758 genkbd_softc_t sc;
984263bc
MD
759
760 /*
761 * NOTE: the device may have already become invalid.
762 * kbd == NULL || !KBD_IS_VALID(kbd)
763 */
a38e57d5 764 crit_enter();
22ff886e 765 lwkt_gettoken(&tty_token);
984263bc
MD
766 sc = dev->si_drv1;
767 kbd = kbd_get_keyboard(KBD_INDEX(dev));
768 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
769 /* XXX: we shall be forgiving and don't report error... */
770 } else {
771 kbd_release(kbd, (void *)sc);
984263bc 772 }
22ff886e 773 lwkt_reltoken(&tty_token);
a38e57d5 774 crit_exit();
984263bc
MD
775 return 0;
776}
777
778static int
fef8985e 779genkbdread(struct dev_read_args *ap)
984263bc 780{
b13267a5 781 cdev_t dev = ap->a_head.a_dev;
fef8985e 782 struct uio *uio = ap->a_uio;
984263bc 783 keyboard_t *kbd;
8a0a8f43 784 genkbd_softc_t sc;
984263bc
MD
785 u_char buffer[KB_BUFSIZE];
786 int len;
787 int error;
984263bc
MD
788
789 /* wait for input */
a38e57d5 790 crit_enter();
22ff886e 791 lwkt_gettoken(&tty_token);
984263bc
MD
792 sc = dev->si_drv1;
793 kbd = kbd_get_keyboard(KBD_INDEX(dev));
794 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
22ff886e 795 lwkt_reltoken(&tty_token);
a38e57d5 796 crit_exit();
984263bc
MD
797 return ENXIO;
798 }
bcc53404
AH
799 while (sc->gkb_q_length == 0) {
800 if (ap->a_ioflag & IO_NDELAY) { /* O_NONBLOCK? */
22ff886e 801 lwkt_reltoken(&tty_token);
a38e57d5 802 crit_exit();
984263bc
MD
803 return EWOULDBLOCK;
804 }
805 sc->gkb_flags |= KB_ASLEEP;
377d4740 806 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
984263bc
MD
807 kbd = kbd_get_keyboard(KBD_INDEX(dev));
808 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
22ff886e 809 lwkt_reltoken(&tty_token);
a38e57d5 810 crit_exit();
984263bc
MD
811 return ENXIO; /* our keyboard has gone... */
812 }
813 if (error) {
814 sc->gkb_flags &= ~KB_ASLEEP;
22ff886e 815 lwkt_reltoken(&tty_token);
a38e57d5 816 crit_exit();
984263bc
MD
817 return error;
818 }
819 }
22ff886e 820 lwkt_reltoken(&tty_token);
a38e57d5 821 crit_exit();
984263bc
MD
822
823 /* copy as much input as possible */
824 error = 0;
825 while (uio->uio_resid > 0) {
e54488bb 826 len = (int)szmin(uio->uio_resid, sizeof(buffer));
bcc53404
AH
827 len = genkbd_getc(sc, buffer, len);
828 if (len <= 0)
984263bc 829 break;
e54488bb 830 error = uiomove(buffer, (size_t)len, uio);
984263bc
MD
831 if (error)
832 break;
833 }
834
835 return error;
836}
837
838static int
fef8985e 839genkbdwrite(struct dev_write_args *ap)
984263bc 840{
b13267a5 841 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
842 keyboard_t *kbd;
843
22ff886e 844 lwkt_gettoken(&tty_token);
984263bc 845 kbd = kbd_get_keyboard(KBD_INDEX(dev));
22ff886e
AH
846 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
847 lwkt_reltoken(&tty_token);
984263bc 848 return ENXIO;
22ff886e
AH
849 }
850 lwkt_reltoken(&tty_token);
984263bc
MD
851 return ENODEV;
852}
853
854static int
fef8985e 855genkbdioctl(struct dev_ioctl_args *ap)
984263bc 856{
b13267a5 857 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
858 keyboard_t *kbd;
859 int error;
860
22ff886e 861 lwkt_gettoken(&tty_token);
984263bc 862 kbd = kbd_get_keyboard(KBD_INDEX(dev));
22ff886e
AH
863 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
864 lwkt_reltoken(&tty_token);
984263bc 865 return ENXIO;
22ff886e 866 }
bcc53404 867 error = kbd_ioctl(kbd, ap->a_cmd, ap->a_data);
984263bc
MD
868 if (error == ENOIOCTL)
869 error = ENODEV;
22ff886e
AH
870
871 lwkt_reltoken(&tty_token);
984263bc
MD
872 return error;
873}
874
25fef0f3 875static struct filterops genkbdfiltops =
4c91dbc9 876 { FILTEROP_ISFD, NULL, genkbdfiltdetach, genkbdfilter };
25fef0f3
SG
877
878static int
879genkbdkqfilter(struct dev_kqfilter_args *ap)
880{
881 cdev_t dev = ap->a_head.a_dev;
882 struct knote *kn = ap->a_kn;
883 genkbd_softc_t sc;
884 struct klist *klist;
885
886 ap->a_result = 0;
887
888 switch (kn->kn_filter) {
889 case EVFILT_READ:
890 kn->kn_fop = &genkbdfiltops;
891 kn->kn_hook = (caddr_t)dev;
892 break;
893 default:
b287d649 894 ap->a_result = EOPNOTSUPP;
25fef0f3
SG
895 return (0);
896 }
897
25fef0f3 898 sc = dev->si_drv1;
5b22f1a7
SG
899 klist = &sc->gkb_rkq.ki_note;
900 knote_insert(klist, kn);
25fef0f3
SG
901
902 return (0);
903}
904
905static void
906genkbdfiltdetach(struct knote *kn)
907{
908 cdev_t dev = (cdev_t)kn->kn_hook;
909 genkbd_softc_t sc;
910 struct klist *klist;
911
25fef0f3 912 sc = dev->si_drv1;
5b22f1a7
SG
913 klist = &sc->gkb_rkq.ki_note;
914 knote_remove(klist, kn);
25fef0f3
SG
915}
916
917static int
918genkbdfilter(struct knote *kn, long hint)
919{
920 cdev_t dev = (cdev_t)kn->kn_hook;
921 keyboard_t *kbd;
922 genkbd_softc_t sc;
923 int ready = 0;
924
925 crit_enter();
22ff886e 926 lwkt_gettoken(&tty_token);
25fef0f3
SG
927 sc = dev->si_drv1;
928 kbd = kbd_get_keyboard(KBD_INDEX(dev));
929 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
930 kn->kn_flags |= EV_EOF; /* the keyboard has gone */
931 ready = 1;
932 } else {
933 if (sc->gkb_q_length > 0)
934 ready = 1;
935 }
22ff886e 936 lwkt_reltoken(&tty_token);
25fef0f3
SG
937 crit_exit();
938
939 return (ready);
940}
941
984263bc
MD
942static int
943genkbd_event(keyboard_t *kbd, int event, void *arg)
944{
8a0a8f43 945 genkbd_softc_t sc;
984263bc
MD
946 size_t len;
947 u_char *cp;
948 int mode;
949 int c;
950
22ff886e 951 lwkt_gettoken(&tty_token);
984263bc 952 /* assert(KBD_IS_VALID(kbd)) */
8a0a8f43 953 sc = (genkbd_softc_t)arg;
984263bc
MD
954
955 switch (event) {
956 case KBDIO_KEYINPUT:
957 break;
958 case KBDIO_UNLOADING:
959 /* the keyboard is going... */
960 kbd_release(kbd, (void *)sc);
961 if (sc->gkb_flags & KB_ASLEEP) {
962 sc->gkb_flags &= ~KB_ASLEEP;
963 wakeup((caddr_t)sc);
964 }
5b22f1a7 965 KNOTE(&sc->gkb_rkq.ki_note, 0);
22ff886e 966 lwkt_reltoken(&tty_token);
984263bc
MD
967 return 0;
968 default:
22ff886e 969 lwkt_reltoken(&tty_token);
984263bc
MD
970 return EINVAL;
971 }
972
973 /* obtain the current key input mode */
bcc53404 974 if (kbd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
984263bc
MD
975 mode = K_XLATE;
976
977 /* read all pending input */
bcc53404
AH
978 while (kbd_check_char(kbd)) {
979 c = kbd_read_char(kbd, FALSE);
984263bc
MD
980 if (c == NOKEY)
981 continue;
982 if (c == ERRKEY) /* XXX: ring bell? */
983 continue;
984 if (!KBD_IS_BUSY(kbd))
985 /* the device is not open, discard the input */
986 continue;
987
988 /* store the byte as is for K_RAW and K_CODE modes */
989 if (mode != K_XLATE) {
bcc53404 990 genkbd_putc(sc, KEYCHAR(c));
984263bc
MD
991 continue;
992 }
993
994 /* K_XLATE */
995 if (c & RELKEY) /* key release is ignored */
996 continue;
997
998 /* process special keys; most of them are just ignored... */
999 if (c & SPCLKEY) {
1000 switch (KEYCHAR(c)) {
1001 default:
1002 /* ignore them... */
1003 continue;
1004 case BTAB: /* a backtab: ESC [ Z */
bcc53404
AH
1005 genkbd_putc(sc, 0x1b);
1006 genkbd_putc(sc, '[');
1007 genkbd_putc(sc, 'Z');
984263bc
MD
1008 continue;
1009 }
1010 }
1011
1012 /* normal chars, normal chars with the META, function keys */
1013 switch (KEYFLAGS(c)) {
1014 case 0: /* a normal char */
bcc53404 1015 genkbd_putc(sc, KEYCHAR(c));
984263bc
MD
1016 break;
1017 case MKEY: /* the META flag: prepend ESC */
bcc53404
AH
1018 genkbd_putc(sc, 0x1b);
1019 genkbd_putc(sc, KEYCHAR(c));
984263bc
MD
1020 break;
1021 case FKEY | SPCLKEY: /* a function key, return string */
bcc53404 1022 cp = kbd_get_fkeystr(kbd, KEYCHAR(c), &len);
984263bc
MD
1023 if (cp != NULL) {
1024 while (len-- > 0)
bcc53404 1025 genkbd_putc(sc, *cp++);
984263bc
MD
1026 }
1027 break;
1028 }
1029 }
1030
1031 /* wake up sleeping/polling processes */
bcc53404 1032 if (sc->gkb_q_length > 0) {
984263bc
MD
1033 if (sc->gkb_flags & KB_ASLEEP) {
1034 sc->gkb_flags &= ~KB_ASLEEP;
1035 wakeup((caddr_t)sc);
1036 }
5b22f1a7 1037 KNOTE(&sc->gkb_rkq.ki_note, 0);
984263bc
MD
1038 }
1039
22ff886e 1040 lwkt_reltoken(&tty_token);
984263bc
MD
1041 return 0;
1042}
1043
1044#endif /* KBD_INSTALL_CDEV */
1045
1046/*
1047 * Generic low-level keyboard functions
1048 * The low-level functions in the keyboard subdriver may use these
1049 * functions.
1050 */
1051
1052int
1053genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1054{
1055 keyarg_t *keyp;
1056 fkeyarg_t *fkeyp;
984263bc
MD
1057 int i;
1058
a38e57d5 1059 crit_enter();
22ff886e 1060 lwkt_gettoken(&tty_token);
984263bc
MD
1061 switch (cmd) {
1062
1063 case KDGKBINFO: /* get keyboard information */
1064 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
1065 i = imin(strlen(kbd->kb_name) + 1,
1066 sizeof(((keyboard_info_t *)arg)->kb_name));
1067 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
1068 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
1069 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
1070 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
1071 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
1072 break;
1073
1074 case KDGKBTYPE: /* get keyboard type */
1075 *(int *)arg = kbd->kb_type;
1076 break;
1077
1078 case KDGETREPEAT: /* get keyboard repeat rate */
1079 ((int *)arg)[0] = kbd->kb_delay1;
1080 ((int *)arg)[1] = kbd->kb_delay2;
1081 break;
1082
1083 case GIO_KEYMAP: /* get keyboard translation table */
1084 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
1085 break;
1086 case PIO_KEYMAP: /* set keyboard translation table */
1087#ifndef KBD_DISABLE_KEYMAP_LOAD
1088 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1089 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
1090 break;
1091#else
22ff886e 1092 lwkt_reltoken(&tty_token);
a38e57d5 1093 crit_exit();
984263bc
MD
1094 return ENODEV;
1095#endif
1096
1097 case GIO_KEYMAPENT: /* get keyboard translation table entry */
1098 keyp = (keyarg_t *)arg;
1099 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1100 /sizeof(kbd->kb_keymap->key[0])) {
22ff886e 1101 lwkt_reltoken(&tty_token);
a38e57d5 1102 crit_exit();
984263bc
MD
1103 return EINVAL;
1104 }
1105 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
1106 sizeof(keyp->key));
1107 break;
1108 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1109#ifndef KBD_DISABLE_KEYMAP_LOAD
1110 keyp = (keyarg_t *)arg;
1111 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1112 /sizeof(kbd->kb_keymap->key[0])) {
22ff886e 1113 lwkt_reltoken(&tty_token);
a38e57d5 1114 crit_exit();
984263bc
MD
1115 return EINVAL;
1116 }
1117 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
1118 sizeof(keyp->key));
1119 break;
1120#else
22ff886e 1121 lwkt_reltoken(&tty_token);
a38e57d5 1122 crit_exit();
984263bc
MD
1123 return ENODEV;
1124#endif
1125
1126 case GIO_DEADKEYMAP: /* get accent key translation table */
1127 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
1128 break;
1129 case PIO_DEADKEYMAP: /* set accent key translation table */
1130#ifndef KBD_DISABLE_KEYMAP_LOAD
1131 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1132 break;
1133#else
22ff886e 1134 lwkt_reltoken(&tty_token);
a38e57d5 1135 crit_exit();
984263bc
MD
1136 return ENODEV;
1137#endif
1138
1139 case GETFKEY: /* get functionkey string */
1140 fkeyp = (fkeyarg_t *)arg;
1141 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
22ff886e 1142 lwkt_reltoken(&tty_token);
a38e57d5 1143 crit_exit();
984263bc
MD
1144 return EINVAL;
1145 }
1146 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
1147 kbd->kb_fkeytab[fkeyp->keynum].len);
1148 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
1149 break;
1150 case SETFKEY: /* set functionkey string */
1151#ifndef KBD_DISABLE_KEYMAP_LOAD
1152 fkeyp = (fkeyarg_t *)arg;
1153 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
22ff886e 1154 lwkt_reltoken(&tty_token);
a38e57d5 1155 crit_exit();
984263bc
MD
1156 return EINVAL;
1157 }
1158 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
1159 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
1160 kbd->kb_fkeytab[fkeyp->keynum].len);
1161 break;
1162#else
22ff886e 1163 lwkt_reltoken(&tty_token);
a38e57d5 1164 crit_exit();
984263bc
MD
1165 return ENODEV;
1166#endif
1167
1168 default:
22ff886e 1169 lwkt_reltoken(&tty_token);
a38e57d5 1170 crit_exit();
984263bc
MD
1171 return ENOIOCTL;
1172 }
1173
22ff886e 1174 lwkt_reltoken(&tty_token);
a38e57d5 1175 crit_exit();
984263bc
MD
1176 return 0;
1177}
1178
1179/* get a pointer to the string associated with the given function key */
3d8ba483
SW
1180u_char *
1181genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
984263bc 1182{
22ff886e
AH
1183 u_char *ch;
1184
984263bc
MD
1185 if (kbd == NULL)
1186 return NULL;
22ff886e
AH
1187
1188 lwkt_gettoken(&tty_token);
984263bc 1189 fkey -= F_FN;
22ff886e
AH
1190 if (fkey > kbd->kb_fkeytab_size) {
1191 lwkt_reltoken(&tty_token);
984263bc 1192 return NULL;
22ff886e 1193 }
984263bc 1194 *len = kbd->kb_fkeytab[fkey].len;
22ff886e
AH
1195 ch = kbd->kb_fkeytab[fkey].str;
1196
1197 lwkt_reltoken(&tty_token);
1198 return ch;
984263bc
MD
1199}
1200
1201/* diagnostic dump */
3d8ba483
SW
1202static char *
1203get_kbd_type_name(int type)
984263bc
MD
1204{
1205 static struct {
1206 int type;
1207 char *name;
1208 } name_table[] = {
1209 { KB_84, "AT 84" },
1210 { KB_101, "AT 101/102" },
1211 { KB_OTHER, "generic" },
1212 };
1213 int i;
1214
1215 for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
1216 if (type == name_table[i].type)
1217 return name_table[i].name;
1218 }
1219 return "unknown";
1220}
1221
1222void
1223genkbd_diag(keyboard_t *kbd, int level)
1224{
1225 if (level > 0) {
e3869ec7 1226 kprintf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
984263bc
MD
1227 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1228 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1229 kbd->kb_config, kbd->kb_flags);
1230 if (kbd->kb_io_base > 0)
e3869ec7 1231 kprintf(", port:0x%x-0x%x", kbd->kb_io_base,
984263bc 1232 kbd->kb_io_base + kbd->kb_io_size - 1);
e3869ec7 1233 kprintf("\n");
984263bc
MD
1234 }
1235}
1236
1237#define set_lockkey_state(k, s, l) \
1238 if (!((s) & l ## DOWN)) { \
1239 int i; \
1240 (s) |= l ## DOWN; \
1241 (s) ^= l ## ED; \
1242 i = (s) & LOCK_MASK; \
bcc53404 1243 kbd_ioctl((k), KDSETLED, (caddr_t)&i); \
984263bc
MD
1244 }
1245
1246static u_int
1247save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1248{
1249 int i;
1250
22ff886e 1251 lwkt_gettoken(&tty_token);
984263bc
MD
1252 /* make an index into the accent map */
1253 i = key - F_ACC + 1;
1254 if ((i > kbd->kb_accentmap->n_accs)
1255 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1256 /* the index is out of range or pointing to an empty entry */
1257 *accents = 0;
22ff886e 1258 lwkt_reltoken(&tty_token);
984263bc
MD
1259 return ERRKEY;
1260 }
1261
1262 /*
1263 * If the same accent key has been hit twice, produce the accent char
1264 * itself.
1265 */
1266 if (i == *accents) {
1267 key = kbd->kb_accentmap->acc[i - 1].accchar;
1268 *accents = 0;
22ff886e 1269 lwkt_reltoken(&tty_token);
984263bc
MD
1270 return key;
1271 }
1272
1273 /* remember the index and wait for the next key */
1274 *accents = i;
22ff886e 1275 lwkt_reltoken(&tty_token);
984263bc
MD
1276 return NOKEY;
1277}
1278
1279static u_int
1280make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1281{
1282 struct acc_t *acc;
1283 int i;
1284
22ff886e 1285 lwkt_gettoken(&tty_token);
984263bc
MD
1286 acc = &kbd->kb_accentmap->acc[*accents - 1];
1287 *accents = 0;
1288
1289 /*
1290 * If the accent key is followed by the space key,
1291 * produce the accent char itself.
1292 */
22ff886e
AH
1293 if (ch == ' ') {
1294 lwkt_reltoken(&tty_token);
984263bc 1295 return acc->accchar;
22ff886e 1296 }
984263bc
MD
1297
1298 /* scan the accent map */
1299 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1300 if (acc->map[i][0] == 0) /* end of table */
1301 break;
766a68c8
MD
1302 if (acc->map[i][0] == ch) {
1303 lwkt_reltoken(&tty_token);
984263bc 1304 return acc->map[i][1];
766a68c8 1305 }
984263bc 1306 }
22ff886e 1307 lwkt_reltoken(&tty_token);
984263bc
MD
1308 /* this char cannot be accented... */
1309 return ERRKEY;
1310}
1311
1312int
1313genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1314 int *accents)
1315{
1316 struct keyent_t *key;
1317 int state = *shiftstate;
1318 int action;
1319 int f;
1320 int i;
1321
22ff886e 1322 lwkt_gettoken(&tty_token);
984263bc
MD
1323 i = keycode;
1324 f = state & (AGRS | ALKED);
1325 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1326 i += ALTGR_OFFSET;
1327 key = &kbd->kb_keymap->key[i];
1328 i = ((state & SHIFTS) ? 1 : 0)
1329 | ((state & CTLS) ? 2 : 0)
1330 | ((state & ALTS) ? 4 : 0);
1331 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1332 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1333 i ^= 1;
1334
1335 if (up) { /* break: key released */
1336 action = kbd->kb_lastact[keycode];
1337 kbd->kb_lastact[keycode] = NOP;
1338 switch (action) {
1339 case LSHA:
1340 if (state & SHIFTAON) {
1341 set_lockkey_state(kbd, state, ALK);
1342 state &= ~ALKDOWN;
1343 }
1344 action = LSH;
1345 /* FALL THROUGH */
1346 case LSH:
1347 state &= ~SHIFTS1;
1348 break;
1349 case RSHA:
1350 if (state & SHIFTAON) {
1351 set_lockkey_state(kbd, state, ALK);
1352 state &= ~ALKDOWN;
1353 }
1354 action = RSH;
1355 /* FALL THROUGH */
1356 case RSH:
1357 state &= ~SHIFTS2;
1358 break;
1359 case LCTRA:
1360 if (state & SHIFTAON) {
1361 set_lockkey_state(kbd, state, ALK);
1362 state &= ~ALKDOWN;
1363 }
1364 action = LCTR;
1365 /* FALL THROUGH */
1366 case LCTR:
1367 state &= ~CTLS1;
1368 break;
1369 case RCTRA:
1370 if (state & SHIFTAON) {
1371 set_lockkey_state(kbd, state, ALK);
1372 state &= ~ALKDOWN;
1373 }
1374 action = RCTR;
1375 /* FALL THROUGH */
1376 case RCTR:
1377 state &= ~CTLS2;
1378 break;
1379 case LALTA:
1380 if (state & SHIFTAON) {
1381 set_lockkey_state(kbd, state, ALK);
1382 state &= ~ALKDOWN;
1383 }
1384 action = LALT;
1385 /* FALL THROUGH */
1386 case LALT:
1387 state &= ~ALTS1;
1388 break;
1389 case RALTA:
1390 if (state & SHIFTAON) {
1391 set_lockkey_state(kbd, state, ALK);
1392 state &= ~ALKDOWN;
1393 }
1394 action = RALT;
1395 /* FALL THROUGH */
1396 case RALT:
1397 state &= ~ALTS2;
1398 break;
1399 case ASH:
1400 state &= ~AGRS1;
1401 break;
1402 case META:
1403 state &= ~METAS1;
1404 break;
1405 case NLK:
1406 state &= ~NLKDOWN;
1407 break;
1408 case CLK:
984263bc 1409 state &= ~CLKDOWN;
984263bc
MD
1410 break;
1411 case SLK:
1412 state &= ~SLKDOWN;
1413 break;
1414 case ALK:
1415 state &= ~ALKDOWN;
1416 break;
1417 case NOP:
1418 /* release events of regular keys are not reported */
1419 *shiftstate &= ~SHIFTAON;
22ff886e 1420 lwkt_reltoken(&tty_token);
984263bc
MD
1421 return NOKEY;
1422 }
1423 *shiftstate = state & ~SHIFTAON;
22ff886e 1424 lwkt_reltoken(&tty_token);
984263bc
MD
1425 return (SPCLKEY | RELKEY | action);
1426 } else { /* make: key pressed */
1427 action = key->map[i];
1428 state &= ~SHIFTAON;
1429 if (key->spcl & (0x80 >> i)) {
1430 /* special keys */
1431 if (kbd->kb_lastact[keycode] == NOP)
1432 kbd->kb_lastact[keycode] = action;
1433 if (kbd->kb_lastact[keycode] != action)
1434 action = NOP;
1435 switch (action) {
1436 /* LOCKING KEYS */
1437 case NLK:
1438 set_lockkey_state(kbd, state, NLK);
1439 break;
1440 case CLK:
984263bc 1441 set_lockkey_state(kbd, state, CLK);
984263bc
MD
1442 break;
1443 case SLK:
1444 set_lockkey_state(kbd, state, SLK);
1445 break;
1446 case ALK:
1447 set_lockkey_state(kbd, state, ALK);
1448 break;
1449 /* NON-LOCKING KEYS */
1450 case SPSC: case RBT: case SUSP: case STBY:
1451 case DBG: case NEXT: case PREV: case PNC:
1452 case HALT: case PDWN:
1453 *accents = 0;
1454 break;
1455 case BTAB:
1456 *accents = 0;
1457 action |= BKEY;
1458 break;
1459 case LSHA:
1460 state |= SHIFTAON;
1461 action = LSH;
1462 /* FALL THROUGH */
1463 case LSH:
1464 state |= SHIFTS1;
1465 break;
1466 case RSHA:
1467 state |= SHIFTAON;
1468 action = RSH;
1469 /* FALL THROUGH */
1470 case RSH:
1471 state |= SHIFTS2;
1472 break;
1473 case LCTRA:
1474 state |= SHIFTAON;
1475 action = LCTR;
1476 /* FALL THROUGH */
1477 case LCTR:
1478 state |= CTLS1;
1479 break;
1480 case RCTRA:
1481 state |= SHIFTAON;
1482 action = RCTR;
1483 /* FALL THROUGH */
1484 case RCTR:
1485 state |= CTLS2;
1486 break;
1487 case LALTA:
1488 state |= SHIFTAON;
1489 action = LALT;
1490 /* FALL THROUGH */
1491 case LALT:
1492 state |= ALTS1;
1493 break;
1494 case RALTA:
1495 state |= SHIFTAON;
1496 action = RALT;
1497 /* FALL THROUGH */
1498 case RALT:
1499 state |= ALTS2;
1500 break;
1501 case ASH:
1502 state |= AGRS1;
1503 break;
1504 case META:
1505 state |= METAS1;
1506 break;
1507 case NOP:
1508 *shiftstate = state;
22ff886e 1509 lwkt_reltoken(&tty_token);
984263bc
MD
1510 return NOKEY;
1511 default:
1512 /* is this an accent (dead) key? */
1513 *shiftstate = state;
1514 if (action >= F_ACC && action <= L_ACC) {
1515 action = save_accent_key(kbd, action,
1516 accents);
1517 switch (action) {
1518 case NOKEY:
1519 case ERRKEY:
22ff886e 1520 lwkt_reltoken(&tty_token);
984263bc
MD
1521 return action;
1522 default:
22ff886e
AH
1523 if (state & METAS) {
1524 lwkt_reltoken(&tty_token);
984263bc 1525 return (action | MKEY);
22ff886e
AH
1526 } else {
1527 lwkt_reltoken(&tty_token);
984263bc 1528 return action;
22ff886e 1529 }
984263bc
MD
1530 }
1531 /* NOT REACHED */
1532 }
1533 /* other special keys */
1534 if (*accents > 0) {
1535 *accents = 0;
22ff886e 1536 lwkt_reltoken(&tty_token);
984263bc
MD
1537 return ERRKEY;
1538 }
1539 if (action >= F_FN && action <= L_FN)
1540 action |= FKEY;
1541 /* XXX: return fkey string for the FKEY? */
22ff886e 1542 lwkt_reltoken(&tty_token);
984263bc
MD
1543 return (SPCLKEY | action);
1544 }
1545 *shiftstate = state;
22ff886e 1546 lwkt_reltoken(&tty_token);
984263bc
MD
1547 return (SPCLKEY | action);
1548 } else {
1549 /* regular keys */
1550 kbd->kb_lastact[keycode] = NOP;
1551 *shiftstate = state;
1552 if (*accents > 0) {
1553 /* make an accented char */
1554 action = make_accent_char(kbd, action, accents);
22ff886e
AH
1555 if (action == ERRKEY) {
1556 lwkt_reltoken(&tty_token);
984263bc 1557 return action;
22ff886e 1558 }
984263bc
MD
1559 }
1560 if (state & METAS)
1561 action |= MKEY;
22ff886e 1562 lwkt_reltoken(&tty_token);
984263bc
MD
1563 return action;
1564 }
1565 }
1566 /* NOT REACHED */
22ff886e 1567 lwkt_reltoken(&tty_token);
984263bc 1568}