Remove bogus (void *) casts.
[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 $
a38e57d5
MD
29 */
30/*
31 * Generic keyboard driver.
32 *
33 * Interrupt note: keyboards use clist functions and since usb keyboard
34 * interrupts are not protected by spltty(), we must use a critical section
35 * to protect against corruption.
bcc53404 36 * XXX: this keyboard driver doesn't use clist functions anymore!
22ff886e
AH
37 *
38 * MPSAFE NOTE: all keyboards could easily be put under a different global token.
984263bc
MD
39 */
40
41#include "opt_kbd.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/malloc.h>
47#include <sys/conf.h>
48#include <sys/proc.h>
49#include <sys/tty.h>
25fef0f3 50#include <sys/event.h>
984263bc
MD
51#include <sys/vnode.h>
52#include <sys/uio.h>
a38e57d5
MD
53#include <sys/thread.h>
54#include <sys/thread2.h>
984263bc
MD
55
56#include <machine/console.h>
57
1f2de5d4 58#include "kbdreg.h"
984263bc 59
22ff886e
AH
60#if 0
61#define lwkt_gettoken(x)
62#define lwkt_reltoken(x)
63#endif
64
984263bc
MD
65#define KBD_INDEX(dev) minor(dev)
66
bcc53404
AH
67#define KB_QSIZE 512
68#define KB_BUFSIZE 64
69
8a0a8f43 70struct genkbd_softc {
984263bc
MD
71 int gkb_flags; /* flag/status bits */
72#define KB_ASLEEP (1 << 0)
5b22f1a7 73 struct kqinfo gkb_rkq;
bcc53404
AH
74 char gkb_q[KB_QSIZE]; /* input queue */
75 unsigned int gkb_q_start;
76 unsigned int gkb_q_length;
8a0a8f43
MD
77};
78
79typedef struct genkbd_softc *genkbd_softc_t;
984263bc
MD
80
81static SLIST_HEAD(, keyboard_driver) keyboard_drivers =
82 SLIST_HEAD_INITIALIZER(keyboard_drivers);
83
dc62b251
MD
84SET_DECLARE(kbddriver_set, const keyboard_driver_t);
85
984263bc
MD
86/* local arrays */
87
88/*
89 * We need at least one entry each in order to initialize a keyboard
90 * for the kernel console. The arrays will be increased dynamically
91 * when necessary.
92 */
93
94static int keyboards = 1;
95static keyboard_t *kbd_ini;
96static keyboard_t **keyboard = &kbd_ini;
97static keyboard_switch_t *kbdsw_ini;
98 keyboard_switch_t **kbdsw = &kbdsw_ini;
99
100#define ARRAY_DELTA 4
101
102static int
103kbd_realloc_array(void)
104{
105 keyboard_t **new_kbd;
106 keyboard_switch_t **new_kbdsw;
107 int newsize;
984263bc 108
22ff886e 109 lwkt_gettoken(&tty_token);
984263bc 110 newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
efda3bd0 111 new_kbd = kmalloc(sizeof(*new_kbd) * newsize, M_DEVBUF,
bf22d4c1 112 M_WAITOK | M_ZERO);
efda3bd0 113 new_kbdsw = kmalloc(sizeof(*new_kbdsw) * newsize, M_DEVBUF,
bf22d4c1 114 M_WAITOK | M_ZERO);
984263bc
MD
115 bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
116 bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
a38e57d5 117 crit_enter();
984263bc 118 if (keyboards > 1) {
efda3bd0
MD
119 kfree(keyboard, M_DEVBUF);
120 kfree(kbdsw, M_DEVBUF);
984263bc
MD
121 }
122 keyboard = new_kbd;
123 kbdsw = new_kbdsw;
124 keyboards = newsize;
a38e57d5 125 crit_exit();
984263bc
MD
126
127 if (bootverbose)
e3869ec7 128 kprintf("kbd: new array size %d\n", keyboards);
984263bc 129
22ff886e 130 lwkt_reltoken(&tty_token);
984263bc
MD
131 return 0;
132}
133
134/*
e30db56f
MD
135 * Low-level keyboard driver functions.
136 *
984263bc
MD
137 * Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
138 * driver, call these functions to initialize the keyboard_t structure
139 * and register it to the virtual keyboard driver `kbd'.
e30db56f
MD
140 *
141 * The reinit call is made when a driver has partially detached a keyboard
142 * but does not unregistered it, then wishes to reinitialize it later on.
143 * This is how the USB keyboard driver handles the 'default' keyboard,
144 * because unregistering the keyboard associated with the console will
145 * destroy its console association forever.
984263bc 146 */
984263bc 147void
e30db56f 148kbd_reinit_struct(keyboard_t *kbd, int config, int pref)
984263bc 149{
22ff886e 150 lwkt_gettoken(&tty_token);
e30db56f 151 kbd->kb_flags |= KB_NO_DEVICE; /* device has not been found */
984263bc
MD
152 kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
153 kbd->kb_led = 0; /* unknown */
984263bc
MD
154 kbd->kb_data = NULL;
155 kbd->kb_keymap = NULL;
156 kbd->kb_accentmap = NULL;
157 kbd->kb_fkeytab = NULL;
158 kbd->kb_fkeytab_size = 0;
159 kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
160 kbd->kb_delay2 = KB_DELAY2;
e30db56f
MD
161 kbd->kb_count = 0;
162 kbd->kb_pref = pref;
984263bc 163 bzero(kbd->kb_lastact, sizeof(kbd->kb_lastact));
22ff886e 164 lwkt_reltoken(&tty_token);
984263bc
MD
165}
166
e30db56f
MD
167/* initialize the keyboard_t structure */
168void
169kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
170 int pref, int port, int port_size)
171{
22ff886e 172 lwkt_gettoken(&tty_token);
e30db56f
MD
173 kbd->kb_flags = 0;
174 kbd->kb_name = name;
175 kbd->kb_type = type;
176 kbd->kb_unit = unit;
177 kbd->kb_io_base = port;
178 kbd->kb_io_size = port_size;
179 kbd_reinit_struct(kbd, config, pref);
fc9700a1 180 lockinit(&kbd->kb_lock, name, 0, LK_CANRECURSE);
22ff886e 181 lwkt_reltoken(&tty_token);
e30db56f
MD
182}
183
984263bc
MD
184void
185kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
186 fkeytab_t *fkeymap, int fkeymap_size)
187{
22ff886e 188 lwkt_gettoken(&tty_token);
984263bc
MD
189 kbd->kb_keymap = keymap;
190 kbd->kb_accentmap = accmap;
191 kbd->kb_fkeytab = fkeymap;
192 kbd->kb_fkeytab_size = fkeymap_size;
22ff886e 193 lwkt_reltoken(&tty_token);
984263bc
MD
194}
195
196/* declare a new keyboard driver */
197int
198kbd_add_driver(keyboard_driver_t *driver)
199{
22ff886e
AH
200 lwkt_gettoken(&tty_token);
201 if (SLIST_NEXT(driver, link)) {
202 lwkt_reltoken(&tty_token);
984263bc 203 return EINVAL;
22ff886e 204 }
984263bc 205 SLIST_INSERT_HEAD(&keyboard_drivers, driver, link);
22ff886e 206 lwkt_reltoken(&tty_token);
984263bc
MD
207 return 0;
208}
209
210int
211kbd_delete_driver(keyboard_driver_t *driver)
212{
22ff886e 213 lwkt_gettoken(&tty_token);
984263bc
MD
214 SLIST_REMOVE(&keyboard_drivers, driver, keyboard_driver, link);
215 SLIST_NEXT(driver, link) = NULL;
22ff886e 216 lwkt_reltoken(&tty_token);
984263bc
MD
217 return 0;
218}
219
220/* register a keyboard and associate it with a function table */
221int
222kbd_register(keyboard_t *kbd)
223{
224 const keyboard_driver_t **list;
225 const keyboard_driver_t *p;
bcc53404
AH
226 keyboard_t *mux;
227 keyboard_info_t ki;
984263bc
MD
228 int index;
229
22ff886e 230 lwkt_gettoken(&tty_token);
bcc53404
AH
231 mux = kbd_get_keyboard(kbd_find_keyboard("kbdmux", -1));
232
984263bc
MD
233 for (index = 0; index < keyboards; ++index) {
234 if (keyboard[index] == NULL)
235 break;
236 }
237 if (index >= keyboards) {
22ff886e
AH
238 if (kbd_realloc_array()) {
239 lwkt_reltoken(&tty_token);
984263bc 240 return -1;
22ff886e 241 }
984263bc
MD
242 }
243
244 kbd->kb_index = index;
245 KBD_UNBUSY(kbd);
246 KBD_VALID(kbd);
247 kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
248 kbd->kb_token = NULL;
249 kbd->kb_callback.kc_func = NULL;
250 kbd->kb_callback.kc_arg = NULL;
22ff886e 251 callout_init_mp(&kbd->kb_atkbd_timeout_ch);
984263bc
MD
252
253 SLIST_FOREACH(p, &keyboard_drivers, link) {
254 if (strcmp(p->name, kbd->kb_name) == 0) {
255 keyboard[index] = kbd;
256 kbdsw[index] = p->kbdsw;
bcc53404
AH
257
258 if (mux != NULL) {
259 bzero(&ki, sizeof(ki));
260 strcpy(ki.kb_name, kbd->kb_name);
261 ki.kb_unit = kbd->kb_unit;
262 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
263 }
264
22ff886e 265 lwkt_reltoken(&tty_token);
984263bc
MD
266 return index;
267 }
268 }
dc62b251
MD
269 SET_FOREACH(list, kbddriver_set) {
270 p = *list;
984263bc
MD
271 if (strcmp(p->name, kbd->kb_name) == 0) {
272 keyboard[index] = kbd;
273 kbdsw[index] = p->kbdsw;
bcc53404
AH
274
275 if (mux != NULL) {
276 bzero(&ki, sizeof(ki));
277 strcpy(ki.kb_name, kbd->kb_name);
278 ki.kb_unit = kbd->kb_unit;
279 kbd_ioctl(mux, KBADDKBD, (caddr_t) &ki);
280 }
281
22ff886e 282 lwkt_reltoken(&tty_token);
984263bc
MD
283 return index;
284 }
285 }
286
22ff886e 287 lwkt_reltoken(&tty_token);
984263bc
MD
288 return -1;
289}
290
291int
292kbd_unregister(keyboard_t *kbd)
293{
294 int error;
984263bc 295
b5e22505 296 KBD_LOCK_ASSERT(kbd);
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 583
fef8985e 584static struct dev_ops kbd_ops = {
88abd8b5 585 { "kbd", 0, 0 },
fef8985e
MD
586 .d_open = genkbdopen,
587 .d_close = genkbdclose,
588 .d_read = genkbdread,
589 .d_write = genkbdwrite,
590 .d_ioctl = genkbdioctl,
25fef0f3 591 .d_kqfilter = genkbdkqfilter
984263bc
MD
592};
593
8a0a8f43
MD
594/*
595 * Attach a keyboard.
596 *
597 * NOTE: The usb driver does not detach the default keyboard if it is
598 * unplugged, but calls kbd_attach() when it is plugged back in.
599 */
984263bc
MD
600int
601kbd_attach(keyboard_t *kbd)
602{
b13267a5 603 cdev_t dev;
984263bc 604
22ff886e
AH
605 lwkt_gettoken(&tty_token);
606 if (kbd->kb_index >= keyboards) {
607 lwkt_reltoken(&tty_token);
984263bc 608 return EINVAL;
22ff886e
AH
609 }
610 if (keyboard[kbd->kb_index] != kbd) {
611 lwkt_reltoken(&tty_token);
984263bc 612 return EINVAL;
22ff886e 613 }
984263bc 614
8a0a8f43
MD
615 if (kbd->kb_dev == NULL) {
616 kbd->kb_dev = make_dev(&kbd_ops, kbd->kb_index,
617 UID_ROOT, GID_WHEEL, 0600,
618 "kbd%r", kbd->kb_index);
619 }
620 dev = kbd->kb_dev;
621 if (dev->si_drv1 == NULL) {
622 dev->si_drv1 = kmalloc(sizeof(struct genkbd_softc), M_DEVBUF,
623 M_WAITOK);
624 }
625 bzero(dev->si_drv1, sizeof(struct genkbd_softc));
984263bc 626
e3869ec7 627 kprintf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
22ff886e 628 lwkt_reltoken(&tty_token);
984263bc
MD
629 return 0;
630}
631
632int
633kbd_detach(keyboard_t *kbd)
634{
b13267a5 635 cdev_t dev;
984263bc 636
22ff886e
AH
637 lwkt_gettoken(&tty_token);
638
639 if (kbd->kb_index >= keyboards) {
640 lwkt_reltoken(&tty_token);
984263bc 641 return EINVAL;
22ff886e
AH
642 }
643 if (keyboard[kbd->kb_index] != kbd) {
644 lwkt_reltoken(&tty_token);
984263bc 645 return EINVAL;
22ff886e 646 }
984263bc 647
8a0a8f43 648 if ((dev = kbd->kb_dev) != NULL) {
e4c9c0c8 649 if (dev->si_drv1) {
efda3bd0 650 kfree(dev->si_drv1, M_DEVBUF);
e4c9c0c8
MD
651 dev->si_drv1 = NULL;
652 }
8a0a8f43 653 kbd->kb_dev = NULL;
e4c9c0c8 654 }
cd29885a 655 dev_ops_remove_minor(&kbd_ops, kbd->kb_index);
22ff886e 656 lwkt_reltoken(&tty_token);
984263bc
MD
657 return 0;
658}
659
660/*
661 * Generic keyboard cdev driver functions
662 * Keyboard subdrivers may call these functions to implement common
663 * driver functions.
664 */
665
bcc53404
AH
666static void
667genkbd_putc(genkbd_softc_t sc, char c)
668{
669 unsigned int p;
670
22ff886e
AH
671 lwkt_gettoken(&tty_token);
672
673 if (sc->gkb_q_length == KB_QSIZE) {
674 lwkt_reltoken(&tty_token);
bcc53404 675 return;
22ff886e 676 }
bcc53404
AH
677
678 p = (sc->gkb_q_start + sc->gkb_q_length) % KB_QSIZE;
679 sc->gkb_q[p] = c;
680 sc->gkb_q_length++;
22ff886e
AH
681
682 lwkt_reltoken(&tty_token);
bcc53404
AH
683}
684
685static size_t
686genkbd_getc(genkbd_softc_t sc, char *buf, size_t len)
687{
688
22ff886e
AH
689 lwkt_gettoken(&tty_token);
690
bcc53404 691 /* Determine copy size. */
22ff886e
AH
692 if (sc->gkb_q_length == 0) {
693 lwkt_reltoken(&tty_token);
bcc53404 694 return (0);
22ff886e 695 }
bcc53404
AH
696 if (len >= sc->gkb_q_length)
697 len = sc->gkb_q_length;
698 if (len >= KB_QSIZE - sc->gkb_q_start)
699 len = KB_QSIZE - sc->gkb_q_start;
700
701 /* Copy out data and progress offset. */
702 memcpy(buf, sc->gkb_q + sc->gkb_q_start, len);
703 sc->gkb_q_start = (sc->gkb_q_start + len) % KB_QSIZE;
704 sc->gkb_q_length -= len;
705
22ff886e 706 lwkt_reltoken(&tty_token);
bcc53404
AH
707 return (len);
708}
984263bc
MD
709
710static kbd_callback_func_t genkbd_event;
711
712static int
fef8985e 713genkbdopen(struct dev_open_args *ap)
984263bc 714{
b13267a5 715 cdev_t dev = ap->a_head.a_dev;
984263bc 716 keyboard_t *kbd;
8a0a8f43 717 genkbd_softc_t sc;
984263bc
MD
718 int i;
719
a38e57d5 720 crit_enter();
22ff886e 721 lwkt_gettoken(&tty_token);
984263bc
MD
722 sc = dev->si_drv1;
723 kbd = kbd_get_keyboard(KBD_INDEX(dev));
724 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
22ff886e 725 lwkt_reltoken(&tty_token);
a38e57d5 726 crit_exit();
984263bc
MD
727 return ENXIO;
728 }
729 i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
4e1af74f 730 genkbd_event, sc);
984263bc 731 if (i < 0) {
22ff886e 732 lwkt_reltoken(&tty_token);
a38e57d5 733 crit_exit();
984263bc
MD
734 return EBUSY;
735 }
736 /* assert(i == kbd->kb_index) */
737 /* assert(kbd == kbd_get_keyboard(i)) */
738
739 /*
740 * NOTE: even when we have successfully claimed a keyboard,
741 * the device may still be missing (!KBD_HAS_DEVICE(kbd)).
742 */
743
bcc53404 744 sc->gkb_q_length = 0;
22ff886e 745 lwkt_reltoken(&tty_token);
a38e57d5 746 crit_exit();
984263bc
MD
747
748 return 0;
749}
750
751static int
fef8985e 752genkbdclose(struct dev_close_args *ap)
984263bc 753{
b13267a5 754 cdev_t dev = ap->a_head.a_dev;
984263bc 755 keyboard_t *kbd;
8a0a8f43 756 genkbd_softc_t sc;
984263bc
MD
757
758 /*
759 * NOTE: the device may have already become invalid.
760 * kbd == NULL || !KBD_IS_VALID(kbd)
761 */
a38e57d5 762 crit_enter();
22ff886e 763 lwkt_gettoken(&tty_token);
984263bc
MD
764 sc = dev->si_drv1;
765 kbd = kbd_get_keyboard(KBD_INDEX(dev));
766 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
767 /* XXX: we shall be forgiving and don't report error... */
768 } else {
4e1af74f 769 kbd_release(kbd, sc);
984263bc 770 }
22ff886e 771 lwkt_reltoken(&tty_token);
a38e57d5 772 crit_exit();
984263bc
MD
773 return 0;
774}
775
776static int
fef8985e 777genkbdread(struct dev_read_args *ap)
984263bc 778{
b13267a5 779 cdev_t dev = ap->a_head.a_dev;
fef8985e 780 struct uio *uio = ap->a_uio;
984263bc 781 keyboard_t *kbd;
8a0a8f43 782 genkbd_softc_t sc;
984263bc
MD
783 u_char buffer[KB_BUFSIZE];
784 int len;
785 int error;
984263bc
MD
786
787 /* wait for input */
a38e57d5 788 crit_enter();
22ff886e 789 lwkt_gettoken(&tty_token);
984263bc
MD
790 sc = dev->si_drv1;
791 kbd = kbd_get_keyboard(KBD_INDEX(dev));
792 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
22ff886e 793 lwkt_reltoken(&tty_token);
a38e57d5 794 crit_exit();
984263bc
MD
795 return ENXIO;
796 }
bcc53404
AH
797 while (sc->gkb_q_length == 0) {
798 if (ap->a_ioflag & IO_NDELAY) { /* O_NONBLOCK? */
22ff886e 799 lwkt_reltoken(&tty_token);
a38e57d5 800 crit_exit();
984263bc
MD
801 return EWOULDBLOCK;
802 }
803 sc->gkb_flags |= KB_ASLEEP;
377d4740 804 error = tsleep((caddr_t)sc, PCATCH, "kbdrea", 0);
984263bc
MD
805 kbd = kbd_get_keyboard(KBD_INDEX(dev));
806 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
22ff886e 807 lwkt_reltoken(&tty_token);
a38e57d5 808 crit_exit();
984263bc
MD
809 return ENXIO; /* our keyboard has gone... */
810 }
811 if (error) {
812 sc->gkb_flags &= ~KB_ASLEEP;
22ff886e 813 lwkt_reltoken(&tty_token);
a38e57d5 814 crit_exit();
984263bc
MD
815 return error;
816 }
817 }
22ff886e 818 lwkt_reltoken(&tty_token);
a38e57d5 819 crit_exit();
984263bc
MD
820
821 /* copy as much input as possible */
822 error = 0;
823 while (uio->uio_resid > 0) {
e54488bb 824 len = (int)szmin(uio->uio_resid, sizeof(buffer));
bcc53404
AH
825 len = genkbd_getc(sc, buffer, len);
826 if (len <= 0)
984263bc 827 break;
e54488bb 828 error = uiomove(buffer, (size_t)len, uio);
984263bc
MD
829 if (error)
830 break;
831 }
832
833 return error;
834}
835
836static int
fef8985e 837genkbdwrite(struct dev_write_args *ap)
984263bc 838{
b13267a5 839 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
840 keyboard_t *kbd;
841
22ff886e 842 lwkt_gettoken(&tty_token);
984263bc 843 kbd = kbd_get_keyboard(KBD_INDEX(dev));
22ff886e
AH
844 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
845 lwkt_reltoken(&tty_token);
984263bc 846 return ENXIO;
22ff886e
AH
847 }
848 lwkt_reltoken(&tty_token);
984263bc
MD
849 return ENODEV;
850}
851
852static int
fef8985e 853genkbdioctl(struct dev_ioctl_args *ap)
984263bc 854{
b13267a5 855 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
856 keyboard_t *kbd;
857 int error;
858
22ff886e 859 lwkt_gettoken(&tty_token);
984263bc 860 kbd = kbd_get_keyboard(KBD_INDEX(dev));
22ff886e
AH
861 if ((kbd == NULL) || !KBD_IS_VALID(kbd)) {
862 lwkt_reltoken(&tty_token);
984263bc 863 return ENXIO;
22ff886e 864 }
bcc53404 865 error = kbd_ioctl(kbd, ap->a_cmd, ap->a_data);
984263bc
MD
866 if (error == ENOIOCTL)
867 error = ENODEV;
22ff886e
AH
868
869 lwkt_reltoken(&tty_token);
984263bc
MD
870 return error;
871}
872
25fef0f3 873static struct filterops genkbdfiltops =
4c91dbc9 874 { FILTEROP_ISFD, NULL, genkbdfiltdetach, genkbdfilter };
25fef0f3
SG
875
876static int
877genkbdkqfilter(struct dev_kqfilter_args *ap)
878{
879 cdev_t dev = ap->a_head.a_dev;
880 struct knote *kn = ap->a_kn;
881 genkbd_softc_t sc;
882 struct klist *klist;
883
884 ap->a_result = 0;
885
886 switch (kn->kn_filter) {
887 case EVFILT_READ:
888 kn->kn_fop = &genkbdfiltops;
889 kn->kn_hook = (caddr_t)dev;
890 break;
891 default:
b287d649 892 ap->a_result = EOPNOTSUPP;
25fef0f3
SG
893 return (0);
894 }
895
25fef0f3 896 sc = dev->si_drv1;
5b22f1a7
SG
897 klist = &sc->gkb_rkq.ki_note;
898 knote_insert(klist, kn);
25fef0f3
SG
899
900 return (0);
901}
902
903static void
904genkbdfiltdetach(struct knote *kn)
905{
906 cdev_t dev = (cdev_t)kn->kn_hook;
907 genkbd_softc_t sc;
908 struct klist *klist;
909
25fef0f3 910 sc = dev->si_drv1;
5b22f1a7
SG
911 klist = &sc->gkb_rkq.ki_note;
912 knote_remove(klist, kn);
25fef0f3
SG
913}
914
915static int
916genkbdfilter(struct knote *kn, long hint)
917{
918 cdev_t dev = (cdev_t)kn->kn_hook;
919 keyboard_t *kbd;
920 genkbd_softc_t sc;
921 int ready = 0;
922
923 crit_enter();
22ff886e 924 lwkt_gettoken(&tty_token);
25fef0f3
SG
925 sc = dev->si_drv1;
926 kbd = kbd_get_keyboard(KBD_INDEX(dev));
927 if ((sc == NULL) || (kbd == NULL) || !KBD_IS_VALID(kbd)) {
3bcb6e5e
SZ
928 /* The keyboard has gone */
929 kn->kn_flags |= (EV_EOF | EV_NODATA);
25fef0f3
SG
930 ready = 1;
931 } else {
932 if (sc->gkb_q_length > 0)
933 ready = 1;
934 }
22ff886e 935 lwkt_reltoken(&tty_token);
25fef0f3
SG
936 crit_exit();
937
938 return (ready);
939}
940
984263bc
MD
941static int
942genkbd_event(keyboard_t *kbd, int event, void *arg)
943{
8a0a8f43 944 genkbd_softc_t sc;
984263bc
MD
945 size_t len;
946 u_char *cp;
947 int mode;
948 int c;
949
22ff886e 950 lwkt_gettoken(&tty_token);
984263bc 951 /* assert(KBD_IS_VALID(kbd)) */
8a0a8f43 952 sc = (genkbd_softc_t)arg;
984263bc
MD
953
954 switch (event) {
955 case KBDIO_KEYINPUT:
956 break;
957 case KBDIO_UNLOADING:
958 /* the keyboard is going... */
4e1af74f 959 kbd_release(kbd, sc);
984263bc
MD
960 if (sc->gkb_flags & KB_ASLEEP) {
961 sc->gkb_flags &= ~KB_ASLEEP;
962 wakeup((caddr_t)sc);
963 }
5b22f1a7 964 KNOTE(&sc->gkb_rkq.ki_note, 0);
22ff886e 965 lwkt_reltoken(&tty_token);
984263bc
MD
966 return 0;
967 default:
22ff886e 968 lwkt_reltoken(&tty_token);
984263bc
MD
969 return EINVAL;
970 }
971
972 /* obtain the current key input mode */
bcc53404 973 if (kbd_ioctl(kbd, KDGKBMODE, (caddr_t)&mode))
984263bc
MD
974 mode = K_XLATE;
975
976 /* read all pending input */
bcc53404
AH
977 while (kbd_check_char(kbd)) {
978 c = kbd_read_char(kbd, FALSE);
984263bc
MD
979 if (c == NOKEY)
980 continue;
981 if (c == ERRKEY) /* XXX: ring bell? */
982 continue;
983 if (!KBD_IS_BUSY(kbd))
984 /* the device is not open, discard the input */
985 continue;
986
987 /* store the byte as is for K_RAW and K_CODE modes */
988 if (mode != K_XLATE) {
bcc53404 989 genkbd_putc(sc, KEYCHAR(c));
984263bc
MD
990 continue;
991 }
992
993 /* K_XLATE */
994 if (c & RELKEY) /* key release is ignored */
995 continue;
996
997 /* process special keys; most of them are just ignored... */
998 if (c & SPCLKEY) {
999 switch (KEYCHAR(c)) {
1000 default:
1001 /* ignore them... */
1002 continue;
1003 case BTAB: /* a backtab: ESC [ Z */
bcc53404
AH
1004 genkbd_putc(sc, 0x1b);
1005 genkbd_putc(sc, '[');
1006 genkbd_putc(sc, 'Z');
984263bc
MD
1007 continue;
1008 }
1009 }
1010
1011 /* normal chars, normal chars with the META, function keys */
1012 switch (KEYFLAGS(c)) {
1013 case 0: /* a normal char */
bcc53404 1014 genkbd_putc(sc, KEYCHAR(c));
984263bc
MD
1015 break;
1016 case MKEY: /* the META flag: prepend ESC */
bcc53404
AH
1017 genkbd_putc(sc, 0x1b);
1018 genkbd_putc(sc, KEYCHAR(c));
984263bc
MD
1019 break;
1020 case FKEY | SPCLKEY: /* a function key, return string */
bcc53404 1021 cp = kbd_get_fkeystr(kbd, KEYCHAR(c), &len);
984263bc
MD
1022 if (cp != NULL) {
1023 while (len-- > 0)
bcc53404 1024 genkbd_putc(sc, *cp++);
984263bc
MD
1025 }
1026 break;
1027 }
1028 }
1029
1030 /* wake up sleeping/polling processes */
bcc53404 1031 if (sc->gkb_q_length > 0) {
984263bc
MD
1032 if (sc->gkb_flags & KB_ASLEEP) {
1033 sc->gkb_flags &= ~KB_ASLEEP;
1034 wakeup((caddr_t)sc);
1035 }
5b22f1a7 1036 KNOTE(&sc->gkb_rkq.ki_note, 0);
984263bc
MD
1037 }
1038
22ff886e 1039 lwkt_reltoken(&tty_token);
984263bc
MD
1040 return 0;
1041}
1042
1043#endif /* KBD_INSTALL_CDEV */
1044
1045/*
1046 * Generic low-level keyboard functions
1047 * The low-level functions in the keyboard subdriver may use these
1048 * functions.
1049 */
1050
1051int
1052genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
1053{
1054 keyarg_t *keyp;
1055 fkeyarg_t *fkeyp;
984263bc
MD
1056 int i;
1057
a38e57d5 1058 crit_enter();
22ff886e 1059 lwkt_gettoken(&tty_token);
984263bc
MD
1060 switch (cmd) {
1061
1062 case KDGKBINFO: /* get keyboard information */
1063 ((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
1064 i = imin(strlen(kbd->kb_name) + 1,
1065 sizeof(((keyboard_info_t *)arg)->kb_name));
1066 bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
1067 ((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
1068 ((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
1069 ((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
1070 ((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
1071 break;
1072
1073 case KDGKBTYPE: /* get keyboard type */
1074 *(int *)arg = kbd->kb_type;
1075 break;
1076
1077 case KDGETREPEAT: /* get keyboard repeat rate */
1078 ((int *)arg)[0] = kbd->kb_delay1;
1079 ((int *)arg)[1] = kbd->kb_delay2;
1080 break;
1081
1082 case GIO_KEYMAP: /* get keyboard translation table */
1083 bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
1084 break;
1085 case PIO_KEYMAP: /* set keyboard translation table */
1086#ifndef KBD_DISABLE_KEYMAP_LOAD
1087 bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1088 bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
1089 break;
1090#else
22ff886e 1091 lwkt_reltoken(&tty_token);
a38e57d5 1092 crit_exit();
984263bc
MD
1093 return ENODEV;
1094#endif
1095
1096 case GIO_KEYMAPENT: /* get keyboard translation table entry */
1097 keyp = (keyarg_t *)arg;
1098 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1099 /sizeof(kbd->kb_keymap->key[0])) {
22ff886e 1100 lwkt_reltoken(&tty_token);
a38e57d5 1101 crit_exit();
984263bc
MD
1102 return EINVAL;
1103 }
1104 bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
1105 sizeof(keyp->key));
1106 break;
1107 case PIO_KEYMAPENT: /* set keyboard translation table entry */
1108#ifndef KBD_DISABLE_KEYMAP_LOAD
1109 keyp = (keyarg_t *)arg;
1110 if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
1111 /sizeof(kbd->kb_keymap->key[0])) {
22ff886e 1112 lwkt_reltoken(&tty_token);
a38e57d5 1113 crit_exit();
984263bc
MD
1114 return EINVAL;
1115 }
1116 bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
1117 sizeof(keyp->key));
1118 break;
1119#else
22ff886e 1120 lwkt_reltoken(&tty_token);
a38e57d5 1121 crit_exit();
984263bc
MD
1122 return ENODEV;
1123#endif
1124
1125 case GIO_DEADKEYMAP: /* get accent key translation table */
1126 bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
1127 break;
1128 case PIO_DEADKEYMAP: /* set accent key translation table */
1129#ifndef KBD_DISABLE_KEYMAP_LOAD
1130 bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
1131 break;
1132#else
22ff886e 1133 lwkt_reltoken(&tty_token);
a38e57d5 1134 crit_exit();
984263bc
MD
1135 return ENODEV;
1136#endif
1137
1138 case GETFKEY: /* get functionkey string */
1139 fkeyp = (fkeyarg_t *)arg;
1140 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
22ff886e 1141 lwkt_reltoken(&tty_token);
a38e57d5 1142 crit_exit();
984263bc
MD
1143 return EINVAL;
1144 }
1145 bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
1146 kbd->kb_fkeytab[fkeyp->keynum].len);
1147 fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
1148 break;
1149 case SETFKEY: /* set functionkey string */
1150#ifndef KBD_DISABLE_KEYMAP_LOAD
1151 fkeyp = (fkeyarg_t *)arg;
1152 if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
22ff886e 1153 lwkt_reltoken(&tty_token);
a38e57d5 1154 crit_exit();
984263bc
MD
1155 return EINVAL;
1156 }
1157 kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
1158 bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
1159 kbd->kb_fkeytab[fkeyp->keynum].len);
1160 break;
1161#else
22ff886e 1162 lwkt_reltoken(&tty_token);
a38e57d5 1163 crit_exit();
984263bc
MD
1164 return ENODEV;
1165#endif
1166
1167 default:
22ff886e 1168 lwkt_reltoken(&tty_token);
a38e57d5 1169 crit_exit();
984263bc
MD
1170 return ENOIOCTL;
1171 }
1172
22ff886e 1173 lwkt_reltoken(&tty_token);
a38e57d5 1174 crit_exit();
984263bc
MD
1175 return 0;
1176}
1177
1178/* get a pointer to the string associated with the given function key */
3d8ba483
SW
1179u_char *
1180genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
984263bc 1181{
22ff886e
AH
1182 u_char *ch;
1183
984263bc
MD
1184 if (kbd == NULL)
1185 return NULL;
22ff886e
AH
1186
1187 lwkt_gettoken(&tty_token);
984263bc 1188 fkey -= F_FN;
22ff886e
AH
1189 if (fkey > kbd->kb_fkeytab_size) {
1190 lwkt_reltoken(&tty_token);
984263bc 1191 return NULL;
22ff886e 1192 }
984263bc 1193 *len = kbd->kb_fkeytab[fkey].len;
22ff886e
AH
1194 ch = kbd->kb_fkeytab[fkey].str;
1195
1196 lwkt_reltoken(&tty_token);
1197 return ch;
984263bc
MD
1198}
1199
1200/* diagnostic dump */
3d8ba483
SW
1201static char *
1202get_kbd_type_name(int type)
984263bc
MD
1203{
1204 static struct {
1205 int type;
1206 char *name;
1207 } name_table[] = {
1208 { KB_84, "AT 84" },
1209 { KB_101, "AT 101/102" },
1210 { KB_OTHER, "generic" },
1211 };
1212 int i;
1213
c157ff7a 1214 for (i = 0; i < NELEM(name_table); ++i) {
984263bc
MD
1215 if (type == name_table[i].type)
1216 return name_table[i].name;
1217 }
1218 return "unknown";
1219}
1220
1221void
1222genkbd_diag(keyboard_t *kbd, int level)
1223{
1224 if (level > 0) {
e3869ec7 1225 kprintf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
984263bc
MD
1226 kbd->kb_index, kbd->kb_name, kbd->kb_unit,
1227 get_kbd_type_name(kbd->kb_type), kbd->kb_type,
1228 kbd->kb_config, kbd->kb_flags);
1229 if (kbd->kb_io_base > 0)
e3869ec7 1230 kprintf(", port:0x%x-0x%x", kbd->kb_io_base,
984263bc 1231 kbd->kb_io_base + kbd->kb_io_size - 1);
e3869ec7 1232 kprintf("\n");
984263bc
MD
1233 }
1234}
1235
1236#define set_lockkey_state(k, s, l) \
1237 if (!((s) & l ## DOWN)) { \
1238 int i; \
1239 (s) |= l ## DOWN; \
1240 (s) ^= l ## ED; \
1241 i = (s) & LOCK_MASK; \
bcc53404 1242 kbd_ioctl((k), KDSETLED, (caddr_t)&i); \
984263bc
MD
1243 }
1244
1245static u_int
1246save_accent_key(keyboard_t *kbd, u_int key, int *accents)
1247{
1248 int i;
1249
22ff886e 1250 lwkt_gettoken(&tty_token);
984263bc
MD
1251 /* make an index into the accent map */
1252 i = key - F_ACC + 1;
1253 if ((i > kbd->kb_accentmap->n_accs)
1254 || (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
1255 /* the index is out of range or pointing to an empty entry */
1256 *accents = 0;
22ff886e 1257 lwkt_reltoken(&tty_token);
984263bc
MD
1258 return ERRKEY;
1259 }
1260
1261 /*
1262 * If the same accent key has been hit twice, produce the accent char
1263 * itself.
1264 */
1265 if (i == *accents) {
1266 key = kbd->kb_accentmap->acc[i - 1].accchar;
1267 *accents = 0;
22ff886e 1268 lwkt_reltoken(&tty_token);
984263bc
MD
1269 return key;
1270 }
1271
1272 /* remember the index and wait for the next key */
1273 *accents = i;
22ff886e 1274 lwkt_reltoken(&tty_token);
984263bc
MD
1275 return NOKEY;
1276}
1277
1278static u_int
1279make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
1280{
1281 struct acc_t *acc;
1282 int i;
1283
22ff886e 1284 lwkt_gettoken(&tty_token);
984263bc
MD
1285 acc = &kbd->kb_accentmap->acc[*accents - 1];
1286 *accents = 0;
1287
1288 /*
1289 * If the accent key is followed by the space key,
1290 * produce the accent char itself.
1291 */
22ff886e
AH
1292 if (ch == ' ') {
1293 lwkt_reltoken(&tty_token);
984263bc 1294 return acc->accchar;
22ff886e 1295 }
984263bc
MD
1296
1297 /* scan the accent map */
1298 for (i = 0; i < NUM_ACCENTCHARS; ++i) {
1299 if (acc->map[i][0] == 0) /* end of table */
1300 break;
766a68c8
MD
1301 if (acc->map[i][0] == ch) {
1302 lwkt_reltoken(&tty_token);
984263bc 1303 return acc->map[i][1];
766a68c8 1304 }
984263bc 1305 }
22ff886e 1306 lwkt_reltoken(&tty_token);
984263bc
MD
1307 /* this char cannot be accented... */
1308 return ERRKEY;
1309}
1310
1311int
1312genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
1313 int *accents)
1314{
1315 struct keyent_t *key;
1316 int state = *shiftstate;
1317 int action;
1318 int f;
1319 int i;
1320
22ff886e 1321 lwkt_gettoken(&tty_token);
984263bc
MD
1322 i = keycode;
1323 f = state & (AGRS | ALKED);
1324 if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
1325 i += ALTGR_OFFSET;
1326 key = &kbd->kb_keymap->key[i];
1327 i = ((state & SHIFTS) ? 1 : 0)
1328 | ((state & CTLS) ? 2 : 0)
1329 | ((state & ALTS) ? 4 : 0);
1330 if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
1331 || ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
1332 i ^= 1;
1333
1334 if (up) { /* break: key released */
1335 action = kbd->kb_lastact[keycode];
1336 kbd->kb_lastact[keycode] = NOP;
1337 switch (action) {
1338 case LSHA:
1339 if (state & SHIFTAON) {
1340 set_lockkey_state(kbd, state, ALK);
1341 state &= ~ALKDOWN;
1342 }
1343 action = LSH;
1344 /* FALL THROUGH */
1345 case LSH:
1346 state &= ~SHIFTS1;
1347 break;
1348 case RSHA:
1349 if (state & SHIFTAON) {
1350 set_lockkey_state(kbd, state, ALK);
1351 state &= ~ALKDOWN;
1352 }
1353 action = RSH;
1354 /* FALL THROUGH */
1355 case RSH:
1356 state &= ~SHIFTS2;
1357 break;
1358 case LCTRA:
1359 if (state & SHIFTAON) {
1360 set_lockkey_state(kbd, state, ALK);
1361 state &= ~ALKDOWN;
1362 }
1363 action = LCTR;
1364 /* FALL THROUGH */
1365 case LCTR:
1366 state &= ~CTLS1;
1367 break;
1368 case RCTRA:
1369 if (state & SHIFTAON) {
1370 set_lockkey_state(kbd, state, ALK);
1371 state &= ~ALKDOWN;
1372 }
1373 action = RCTR;
1374 /* FALL THROUGH */
1375 case RCTR:
1376 state &= ~CTLS2;
1377 break;
1378 case LALTA:
1379 if (state & SHIFTAON) {
1380 set_lockkey_state(kbd, state, ALK);
1381 state &= ~ALKDOWN;
1382 }
1383 action = LALT;
1384 /* FALL THROUGH */
1385 case LALT:
1386 state &= ~ALTS1;
1387 break;
1388 case RALTA:
1389 if (state & SHIFTAON) {
1390 set_lockkey_state(kbd, state, ALK);
1391 state &= ~ALKDOWN;
1392 }
1393 action = RALT;
1394 /* FALL THROUGH */
1395 case RALT:
1396 state &= ~ALTS2;
1397 break;
1398 case ASH:
1399 state &= ~AGRS1;
1400 break;
1401 case META:
1402 state &= ~METAS1;
1403 break;
1404 case NLK:
1405 state &= ~NLKDOWN;
1406 break;
1407 case CLK:
984263bc 1408 state &= ~CLKDOWN;
984263bc
MD
1409 break;
1410 case SLK:
1411 state &= ~SLKDOWN;
1412 break;
1413 case ALK:
1414 state &= ~ALKDOWN;
1415 break;
1416 case NOP:
1417 /* release events of regular keys are not reported */
1418 *shiftstate &= ~SHIFTAON;
22ff886e 1419 lwkt_reltoken(&tty_token);
984263bc
MD
1420 return NOKEY;
1421 }
1422 *shiftstate = state & ~SHIFTAON;
22ff886e 1423 lwkt_reltoken(&tty_token);
984263bc
MD
1424 return (SPCLKEY | RELKEY | action);
1425 } else { /* make: key pressed */
1426 action = key->map[i];
1427 state &= ~SHIFTAON;
1428 if (key->spcl & (0x80 >> i)) {
1429 /* special keys */
1430 if (kbd->kb_lastact[keycode] == NOP)
1431 kbd->kb_lastact[keycode] = action;
1432 if (kbd->kb_lastact[keycode] != action)
1433 action = NOP;
1434 switch (action) {
1435 /* LOCKING KEYS */
1436 case NLK:
1437 set_lockkey_state(kbd, state, NLK);
1438 break;
1439 case CLK:
984263bc 1440 set_lockkey_state(kbd, state, CLK);
984263bc
MD
1441 break;
1442 case SLK:
1443 set_lockkey_state(kbd, state, SLK);
1444 break;
1445 case ALK:
1446 set_lockkey_state(kbd, state, ALK);
1447 break;
1448 /* NON-LOCKING KEYS */
1449 case SPSC: case RBT: case SUSP: case STBY:
1450 case DBG: case NEXT: case PREV: case PNC:
1451 case HALT: case PDWN:
1452 *accents = 0;
1453 break;
1454 case BTAB:
1455 *accents = 0;
1456 action |= BKEY;
1457 break;
1458 case LSHA:
1459 state |= SHIFTAON;
1460 action = LSH;
1461 /* FALL THROUGH */
1462 case LSH:
1463 state |= SHIFTS1;
1464 break;
1465 case RSHA:
1466 state |= SHIFTAON;
1467 action = RSH;
1468 /* FALL THROUGH */
1469 case RSH:
1470 state |= SHIFTS2;
1471 break;
1472 case LCTRA:
1473 state |= SHIFTAON;
1474 action = LCTR;
1475 /* FALL THROUGH */
1476 case LCTR:
1477 state |= CTLS1;
1478 break;
1479 case RCTRA:
1480 state |= SHIFTAON;
1481 action = RCTR;
1482 /* FALL THROUGH */
1483 case RCTR:
1484 state |= CTLS2;
1485 break;
1486 case LALTA:
1487 state |= SHIFTAON;
1488 action = LALT;
1489 /* FALL THROUGH */
1490 case LALT:
1491 state |= ALTS1;
1492 break;
1493 case RALTA:
1494 state |= SHIFTAON;
1495 action = RALT;
1496 /* FALL THROUGH */
1497 case RALT:
1498 state |= ALTS2;
1499 break;
1500 case ASH:
1501 state |= AGRS1;
1502 break;
1503 case META:
1504 state |= METAS1;
1505 break;
1506 case NOP:
1507 *shiftstate = state;
22ff886e 1508 lwkt_reltoken(&tty_token);
984263bc
MD
1509 return NOKEY;
1510 default:
1511 /* is this an accent (dead) key? */
1512 *shiftstate = state;
1513 if (action >= F_ACC && action <= L_ACC) {
1514 action = save_accent_key(kbd, action,
1515 accents);
1516 switch (action) {
1517 case NOKEY:
1518 case ERRKEY:
22ff886e 1519 lwkt_reltoken(&tty_token);
984263bc
MD
1520 return action;
1521 default:
22ff886e
AH
1522 if (state & METAS) {
1523 lwkt_reltoken(&tty_token);
984263bc 1524 return (action | MKEY);
22ff886e
AH
1525 } else {
1526 lwkt_reltoken(&tty_token);
984263bc 1527 return action;
22ff886e 1528 }
984263bc
MD
1529 }
1530 /* NOT REACHED */
1531 }
1532 /* other special keys */
1533 if (*accents > 0) {
1534 *accents = 0;
22ff886e 1535 lwkt_reltoken(&tty_token);
984263bc
MD
1536 return ERRKEY;
1537 }
1538 if (action >= F_FN && action <= L_FN)
1539 action |= FKEY;
1540 /* XXX: return fkey string for the FKEY? */
22ff886e 1541 lwkt_reltoken(&tty_token);
984263bc
MD
1542 return (SPCLKEY | action);
1543 }
1544 *shiftstate = state;
22ff886e 1545 lwkt_reltoken(&tty_token);
984263bc
MD
1546 return (SPCLKEY | action);
1547 } else {
1548 /* regular keys */
1549 kbd->kb_lastact[keycode] = NOP;
1550 *shiftstate = state;
1551 if (*accents > 0) {
1552 /* make an accented char */
1553 action = make_accent_char(kbd, action, accents);
22ff886e
AH
1554 if (action == ERRKEY) {
1555 lwkt_reltoken(&tty_token);
984263bc 1556 return action;
22ff886e 1557 }
984263bc
MD
1558 }
1559 if (state & METAS)
1560 action |= MKEY;
22ff886e 1561 lwkt_reltoken(&tty_token);
984263bc
MD
1562 return action;
1563 }
1564 }
1565 /* NOT REACHED */
22ff886e 1566 lwkt_reltoken(&tty_token);
984263bc 1567}