Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.50
      1 /*	$NetBSD: kbd.c,v 1.50 2022/06/25 14:27:43 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Leo Weppelman
      5  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     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  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.50 2022/06/25 14:27:43 tsutsui Exp $");
     35 
     36 #include "mouse.h"
     37 #include "ite.h"
     38 #include "wskbd.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/tty.h>
     45 #include <sys/proc.h>
     46 #include <sys/conf.h>
     47 #include <sys/file.h>
     48 #include <sys/kernel.h>
     49 #include <sys/signalvar.h>
     50 #include <sys/syslog.h>
     51 #include <dev/cons.h>
     52 #include <machine/cpu.h>
     53 #include <machine/iomap.h>
     54 #include <machine/mfp.h>
     55 #include <machine/acia.h>
     56 #include <atari/dev/itevar.h>
     57 #include <atari/dev/event_var.h>
     58 #include <atari/dev/vuid_event.h>
     59 #include <atari/dev/ym2149reg.h>
     60 #include <atari/dev/kbdreg.h>
     61 #include <atari/dev/kbdvar.h>
     62 #include <atari/dev/kbdmap.h>
     63 #include <atari/dev/msvar.h>
     64 
     65 #if NWSKBD>0
     66 #include <dev/wscons/wsconsio.h>
     67 #include <dev/wscons/wskbdvar.h>
     68 #include <dev/wscons/wsksymdef.h>
     69 #include <dev/wscons/wsksymvar.h>
     70 #include <atari/dev/wskbdmap_atari.h>
     71 #endif
     72 
     73 /* WSKBD */
     74 /*
     75  * If NWSKBD>0 we try to attach an wskbd device to us. What follows
     76  * is definitions of callback functions and structures that are passed
     77  * to wscons when initializing.
     78  */
     79 
     80 /*
     81  * Now with wscons this driver exhibits some weird behaviour.
     82  * It may act both as a driver of its own and the md part of the
     83  * wskbd driver. Therefore it can be accessed through /dev/kbd
     84  * and /dev/wskbd0 both.
     85  *
     86  * The data from they keyboard may end up in at least four different
     87  * places:
     88  * - If this driver has been opened (/dev/kbd) and the
     89  *   direct mode (TIOCDIRECT) has been set, data goes to
     90  *   the process who opened the device. Data will transmit itself
     91  *   as described by the firm_event structure.
     92  * - If wskbd support is compiled in and a wskbd driver has been
     93  *   attached then the data is sent to it. Wskbd in turn may
     94  *   - Send the data in the wscons_event form to a process that
     95  *     has opened /dev/wskbd0
     96  *   - Feed the data to a virtual terminal.
     97  * - If an ite is present the data may be fed to it.
     98  */
     99 
    100 uint8_t			kbd_modifier;	/* Modifier mask		*/
    101 
    102 static uint8_t		kbd_ring[KBD_RING_SIZE];
    103 static volatile u_int	kbd_rbput = 0;	/* 'put' index			*/
    104 static u_int		kbd_rbget = 0;	/* 'get' index			*/
    105 
    106 static struct kbd_softc kbd_softc;
    107 
    108 /* {b,c}devsw[] function prototypes */
    109 static dev_type_open(kbdopen);
    110 static dev_type_close(kbdclose);
    111 static dev_type_read(kbdread);
    112 static dev_type_ioctl(kbdioctl);
    113 static dev_type_poll(kbdpoll);
    114 static dev_type_kqfilter(kbdkqfilter);
    115 
    116 /* Interrupt handler */
    117 void	kbdintr(int);
    118 
    119 static void kbdsoft(void *);
    120 static void kbdattach(device_t, device_t, void *);
    121 static int  kbdmatch(device_t, cfdata_t, void *);
    122 #if NITE > 0
    123 static int  kbd_do_modifier(uint8_t);
    124 #endif
    125 static int  kbd_write_poll(const uint8_t *, int);
    126 static void kbd_pkg_start(struct kbd_softc *, uint8_t);
    127 
    128 CFATTACH_DECL_NEW(kbd, 0,
    129     kbdmatch, kbdattach, NULL, NULL);
    130 
    131 const struct cdevsw kbd_cdevsw = {
    132 	.d_open = kbdopen,
    133 	.d_close = kbdclose,
    134 	.d_read = kbdread,
    135 	.d_write = nowrite,
    136 	.d_ioctl = kbdioctl,
    137 	.d_stop = nostop,
    138 	.d_tty = notty,
    139 	.d_poll = kbdpoll,
    140 	.d_mmap = nommap,
    141 	.d_kqfilter = kbdkqfilter,
    142 	.d_discard = nodiscard,
    143 	.d_flag = 0
    144 };
    145 
    146 #if NWSKBD>0
    147 /* accessops */
    148 static int	kbd_enable(void *, int);
    149 static void	kbd_set_leds(void *, int);
    150 static int	kbd_ioctl(void *, u_long, void *, int, struct lwp *);
    151 
    152 /* console ops */
    153 static void	kbd_getc(void *, u_int *, int *);
    154 static void	kbd_pollc(void *, int);
    155 static void	kbd_bell(void *, u_int, u_int, u_int);
    156 
    157 static struct wskbd_accessops kbd_accessops = {
    158 	kbd_enable,
    159 	kbd_set_leds,
    160 	kbd_ioctl
    161 };
    162 
    163 static struct wskbd_consops kbd_consops = {
    164 	kbd_getc,
    165 	kbd_pollc,
    166 	kbd_bell
    167 };
    168 
    169 /* Pointer to keymaps. */
    170 static struct wskbd_mapdata kbd_mapdata = {
    171 	atarikbd_keydesctab,
    172 	KB_US
    173 };
    174 #endif /* WSKBD */
    175 
    176 /*ARGSUSED*/
    177 static int
    178 kbdmatch(device_t parent, cfdata_t cf, void *aux)
    179 {
    180 
    181 	if (!strcmp((char *)aux, "kbd"))
    182 		return 1;
    183 	return 0;
    184 }
    185 
    186 /*ARGSUSED*/
    187 static void
    188 kbdattach(device_t parent, device_t self, void *aux)
    189 {
    190 	int timeout;
    191 	const uint8_t kbd_rst[]  = { 0x80, 0x01 };
    192 	const uint8_t kbd_icmd[] = { 0x12, 0x15 };
    193 
    194 	/*
    195 	 * Disable keyboard interrupts from MFP
    196 	 */
    197 	MFP->mf_ierb &= ~IB_AINT;
    198 
    199 	/*
    200 	 * Reset ACIA and initialize to:
    201 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
    202 	 */
    203 	KBD->ac_cs = A_RESET;
    204 	delay(100);	/* XXX: enough? */
    205 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
    206 
    207 	/*
    208 	 * Clear error conditions
    209 	 */
    210 	while ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0)
    211 		timeout = KBD->ac_da;
    212 
    213 	/*
    214 	 * Now send the reset string, and read+ignore its response
    215 	 */
    216 	if (kbd_write_poll(kbd_rst, 2) == 0)
    217 		printf("kbd: error cannot reset keyboard\n");
    218 	for (timeout = 1000; timeout > 0; timeout--) {
    219 		if ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0) {
    220 			timeout = KBD->ac_da;
    221 			timeout = 100;
    222 		}
    223 		delay(100);
    224 	}
    225 	/*
    226 	 * Send init command: disable mice & joysticks
    227 	 */
    228 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
    229 
    230 	printf("\n");
    231 
    232 	kbd_softc.k_sicookie = softint_establish(SOFTINT_SERIAL, kbdsoft, NULL);
    233 
    234 #if NWSKBD > 0
    235 	if (self != NULL) {
    236 		/*
    237 		 * Try to attach the wskbd.
    238 		 */
    239 		struct wskbddev_attach_args waa;
    240 
    241 		/* Maybe should be done before this?... */
    242 		wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata);
    243 
    244 		waa.console = 1;
    245 		waa.keymap = &kbd_mapdata;
    246 		waa.accessops = &kbd_accessops;
    247 		waa.accesscookie = NULL;
    248 		kbd_softc.k_wskbddev = config_found(self, &waa, wskbddevprint,
    249 		    CFARGS_NONE);
    250 
    251 		kbd_softc.k_pollingmode = 0;
    252 
    253 		kbdenable();
    254 	}
    255 #endif /* WSKBD */
    256 }
    257 
    258 void
    259 kbdenable(void)
    260 {
    261 	int s;
    262 
    263 	s = spltty();
    264 
    265 	/*
    266 	 * Clear error conditions...
    267 	 */
    268 	while ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0)
    269 		(void)KBD->ac_da;
    270 	/*
    271 	 * Enable interrupts from MFP
    272 	 */
    273 	MFP->mf_iprb  = (uint8_t)~IB_AINT;
    274 	MFP->mf_ierb |= IB_AINT;
    275 	MFP->mf_imrb |= IB_AINT;
    276 
    277 	kbd_softc.k_event_mode   = 0;
    278 	kbd_softc.k_events.ev_io = 0;
    279 	kbd_softc.k_pkg_size     = 0;
    280 	splx(s);
    281 }
    282 
    283 static int
    284 kbdopen(dev_t dev, int flags, int mode, struct lwp *l)
    285 {
    286 
    287 	if (kbd_softc.k_events.ev_io)
    288 		return EBUSY;
    289 
    290 	kbd_softc.k_events.ev_io = l->l_proc;
    291 	ev_init(&kbd_softc.k_events);
    292 	return 0;
    293 }
    294 
    295 static int
    296 kbdclose(dev_t dev, int flags, int mode, struct lwp *l)
    297 {
    298 
    299 	/* Turn off event mode, dump the queue */
    300 	kbd_softc.k_event_mode = 0;
    301 	ev_fini(&kbd_softc.k_events);
    302 	kbd_softc.k_events.ev_io = NULL;
    303 	return 0;
    304 }
    305 
    306 static int
    307 kbdread(dev_t dev, struct uio *uio, int flags)
    308 {
    309 
    310 	return ev_read(&kbd_softc.k_events, uio, flags);
    311 }
    312 
    313 static int
    314 kbdioctl(dev_t dev, u_long cmd, register void *data, int flag, struct lwp *l)
    315 {
    316 	struct kbd_softc *k = &kbd_softc;
    317 	struct kbdbell	*kb;
    318 
    319 	switch (cmd) {
    320 	case KIOCTRANS:
    321 		if (*(int *)data == TR_UNTRANS_EVENT)
    322 			return 0;
    323 		break;
    324 
    325 	case KIOCGTRANS:
    326 		/*
    327 		 * Get translation mode
    328 		 */
    329 		*(int *)data = TR_UNTRANS_EVENT;
    330 		return 0;
    331 
    332 	case KIOCSDIRECT:
    333 		k->k_event_mode = *(int *)data;
    334 		return 0;
    335 
    336 	case KIOCRINGBELL:
    337 		kb = (struct kbdbell *)data;
    338 		if (kb)
    339 			kbd_bell_sparms(kb->volume, kb->pitch,
    340 			    kb->duration);
    341 		kbdbell();
    342 		return 0;
    343 
    344 	case FIONBIO:	/* we will remove this someday (soon???) */
    345 		return 0;
    346 
    347 	case FIOASYNC:
    348 		k->k_events.ev_async = *(int *)data != 0;
    349 		return 0;
    350 
    351 	case FIOSETOWN:
    352 		if (-*(int *)data != k->k_events.ev_io->p_pgid &&
    353 		    *(int *)data != k->k_events.ev_io->p_pid)
    354 			return EPERM;
    355 		return 0;
    356 
    357 	case TIOCSPGRP:
    358 		if (*(int *)data != k->k_events.ev_io->p_pgid)
    359 			return EPERM;
    360 		return 0;
    361 
    362 	default:
    363 		return ENOTTY;
    364 	}
    365 
    366 	/*
    367 	 * We identified the ioctl, but we do not handle it.
    368 	 */
    369 	return EOPNOTSUPP;		/* misuse, but what the heck */
    370 }
    371 
    372 static int
    373 kbdpoll(dev_t dev, int events, struct lwp *l)
    374 {
    375 
    376 	return ev_poll(&kbd_softc.k_events, events, l);
    377 }
    378 
    379 static int
    380 kbdkqfilter(dev_t dev, struct knote *kn)
    381 {
    382 
    383 	return ev_kqfilter(&kbd_softc.k_events, kn);
    384 }
    385 
    386 /*
    387  * Keyboard interrupt handler called straight from MFP at spl6.
    388  */
    389 void
    390 kbdintr(int sr)
    391 	/* sr: sr at time of interrupt	*/
    392 {
    393 	uint8_t code;
    394 	bool got_char = false;
    395 
    396 	/*
    397 	 * There may be multiple keys available. Read them all.
    398 	 */
    399 	while ((KBD->ac_cs & (A_RXRDY | A_OE | A_PE)) != 0) {
    400 		got_char = true;
    401 		code = KBD->ac_da;
    402 		if ((KBD->ac_cs & (A_OE | A_PE)) != 0) {
    403 			/* Silently ignore errors */
    404 			continue;
    405 		}
    406 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = code;
    407 	}
    408 
    409 	/*
    410 	 * If characters are waiting for transmit, send them.
    411 	 */
    412 	if ((kbd_softc.k_soft_cs & A_TXINT) != 0 &&
    413 	    (KBD->ac_cs & A_TXRDY) != 0) {
    414 		if (kbd_softc.k_sendp != NULL)
    415 			KBD->ac_da = *kbd_softc.k_sendp++;
    416 		if (--kbd_softc.k_send_cnt <= 0) {
    417 			/*
    418 			 * The total package has been transmitted,
    419 			 * wakeup anyone waiting for it.
    420 			 */
    421 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
    422 			kbd_softc.k_sendp    = NULL;
    423 			kbd_softc.k_send_cnt = 0;
    424 			wakeup((void *)&kbd_softc.k_send_cnt);
    425 		}
    426 	}
    427 
    428 	/*
    429 	 * Activate software-level to handle possible input.
    430 	 */
    431 	if (got_char)
    432 		softint_schedule(kbd_softc.k_sicookie);
    433 }
    434 
    435 /*
    436  * Keyboard soft interrupt handler
    437  */
    438 static void
    439 kbdsoft(void *junk1)
    440 {
    441 	int s;
    442 	uint8_t code;
    443 	struct kbd_softc *k = &kbd_softc;
    444 	struct firm_event *fe;
    445 	int put, get, n;
    446 
    447 	get = kbd_rbget;
    448 
    449 	for (;;) {
    450 		n = kbd_rbput;
    451 		if (get == n) /* We're done	*/
    452 			break;
    453 		n -= get;
    454 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
    455 			get += n - KBD_RING_SIZE;
    456 			n    = KBD_RING_SIZE;
    457 		}
    458 		while (--n >= 0) {
    459 			code = kbd_ring[get++ & KBD_RING_MASK];
    460 
    461 			/*
    462 			 * If collecting a package, stuff it in and
    463 			 * continue.
    464 			 */
    465 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
    466 			    k->k_package[k->k_pkg_idx++] = code;
    467 				if (k->k_pkg_idx == k->k_pkg_size) {
    468 					/*
    469 					 * Package is complete.
    470 					 */
    471 					switch (k->k_pkg_type) {
    472 #if NMOUSE > 0
    473 					case KBD_AMS_PKG:
    474 					case KBD_RMS_PKG:
    475 					case KBD_JOY1_PKG:
    476 						mouse_soft(
    477 						    (REL_MOUSE *)k->k_package,
    478 						    k->k_pkg_size,
    479 						    k->k_pkg_type);
    480 #endif /* NMOUSE */
    481 					}
    482 					k->k_pkg_size = 0;
    483 				}
    484 				continue;
    485 			}
    486 			/*
    487 			 * If this is a package header, init pkg. handling.
    488 			 */
    489 			if (!KBD_IS_KEY(code)) {
    490 				kbd_pkg_start(k, code);
    491 				continue;
    492 			}
    493 #if NWSKBD > 0
    494 			/*
    495 			 * If we have attached a wskbd and not in polling mode
    496 			 * and nobody has opened us directly, then send the
    497 			 * keystroke to the wskbd.
    498 			 */
    499 
    500 			if (kbd_softc.k_pollingmode == 0 &&
    501 			    kbd_softc.k_wskbddev != NULL &&
    502 			    k->k_event_mode == 0) {
    503 				wskbd_input(kbd_softc.k_wskbddev,
    504 				    KBD_RELEASED(code) ?
    505 				    WSCONS_EVENT_KEY_UP :
    506 				    WSCONS_EVENT_KEY_DOWN,
    507 				    KBD_SCANCODE(code));
    508 				continue;
    509 			}
    510 #endif /* NWSKBD */
    511 #if NITE > 0
    512 			if (kbd_do_modifier(code) && !k->k_event_mode)
    513 				continue;
    514 #endif
    515 
    516 			/*
    517 			 * if not in event mode, deliver straight to ite to
    518 			 * process key stroke
    519 			 */
    520 			if (!k->k_event_mode) {
    521 				/* Gets to spltty() by itself	*/
    522 #if NITE > 0
    523 				ite_filter(code, ITEFILT_TTY);
    524 #endif
    525 				continue;
    526 			}
    527 
    528 			/*
    529 			 * Keyboard is generating events.  Turn this keystroke
    530 			 * into an event and put it in the queue.  If the queue
    531 			 * is full, the keystroke is lost (sorry!).
    532 			 */
    533 			s = spltty();
    534 			put = k->k_events.ev_put;
    535 			fe  = &k->k_events.ev_q[put];
    536 			put = (put + 1) % EV_QSIZE;
    537 			if (put == k->k_events.ev_get) {
    538 				log(LOG_WARNING,
    539 				    "keyboard event queue overflow\n");
    540 				splx(s);
    541 				continue;
    542 			}
    543 			fe->id    = KBD_SCANCODE(code);
    544 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
    545 			firm_gettime(fe);
    546 			k->k_events.ev_put = put;
    547 			EV_WAKEUP(&k->k_events);
    548 			splx(s);
    549 		}
    550 		kbd_rbget = get;
    551 	}
    552 }
    553 
    554 static	uint8_t sound[] = {
    555 	0xA8, 0x01, 0xA9, 0x01, 0xAA, 0x01, 0x00,
    556 	0xF8, 0x10, 0x10, 0x10, 0x00, 0x20, 0x03
    557 };
    558 
    559 void
    560 kbdbell(void)
    561 {
    562 	register int	i, sps;
    563 
    564 	sps = splhigh();
    565 	for (i = 0; i < sizeof(sound); i++) {
    566 		YM2149->sd_selr = i;
    567 		YM2149->sd_wdat = sound[i];
    568 	}
    569 	splx(sps);
    570 }
    571 
    572 
    573 /*
    574  * Set the parameters of the 'default' beep.
    575  */
    576 
    577 #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
    578 #define KBDBELLDURATION	128	/* 256 / 2MHz */
    579 
    580 void
    581 kbd_bell_gparms(u_int *volume, u_int *pitch, u_int *duration)
    582 {
    583 	u_int	tmp;
    584 
    585 	tmp = sound[11] | (sound[12] << 8);
    586 	*duration = (tmp * KBDBELLDURATION) / 1000;
    587 
    588 	tmp = sound[0] | (sound[1] << 8);
    589 	*pitch = KBDBELLCLOCK / tmp;
    590 
    591 	*volume = 0;
    592 }
    593 
    594 void
    595 kbd_bell_sparms(u_int volume, u_int pitch, u_int duration)
    596 {
    597 	u_int	f, t;
    598 
    599 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
    600 	if (f > 20000)
    601 		f = 20000;		/* maximum pitch */
    602 
    603 	f = KBDBELLCLOCK / f;
    604 
    605 	t = (duration * 1000) / KBDBELLDURATION;
    606 
    607 	sound[ 0] = f & 0xff;
    608 	sound[ 1] = (f >> 8) & 0xf;
    609 	f -= 1;
    610 	sound[ 2] = f & 0xff;
    611 	sound[ 3] = (f >> 8) & 0xf;
    612 	f += 2;
    613 	sound[ 4] = f & 0xff;
    614 	sound[ 5] = (f >> 8) & 0xf;
    615 
    616 	sound[11] = t & 0xff;
    617 	sound[12] = (t >> 8) & 0xff;
    618 
    619 	sound[13] = 0x03;
    620 }
    621 
    622 int
    623 kbdgetcn(void)
    624 {
    625 	uint8_t code;
    626 	int s = spltty();
    627 	int ints_active;
    628 
    629 	ints_active = 0;
    630 	if ((MFP->mf_imrb & IB_AINT) != 0) {
    631 		ints_active   = 1;
    632 		MFP->mf_imrb &= ~IB_AINT;
    633 	}
    634 	for (;;) {
    635 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
    636 			continue;	/* Wait for key	*/
    637 		if ((KBD->ac_cs & (A_OE | A_PE)) != 0) {
    638 			code = KBD->ac_da;	/* Silently ignore errors */
    639 			continue;
    640 		}
    641 		code = KBD->ac_da;
    642 #if NITE>0
    643 		if (!kbd_do_modifier(code))
    644 #endif
    645 			break;
    646 	}
    647 
    648 	if (ints_active) {
    649 		MFP->mf_iprb  = (uint8_t)~IB_AINT;
    650 		MFP->mf_imrb |=  IB_AINT;
    651 	}
    652 
    653 	splx(s);
    654 	return code;
    655 }
    656 
    657 /*
    658  * Write a command to the keyboard in 'polled' mode.
    659  */
    660 static int
    661 kbd_write_poll(const uint8_t *cmd, int len)
    662 {
    663 	int timeout;
    664 
    665 	while (len-- > 0) {
    666 		KBD->ac_da = *cmd++;
    667 		for (timeout = 100; (KBD->ac_cs & A_TXRDY) == 0; timeout--)
    668 			delay(10);
    669 		if ((KBD->ac_cs & A_TXRDY) == 0)
    670 			return 0;
    671 	}
    672 	return 1;
    673 }
    674 
    675 /*
    676  * Write a command to the keyboard. Return when command is send.
    677  */
    678 void
    679 kbd_write(const uint8_t *cmd, int len)
    680 {
    681 	struct kbd_softc	*k = &kbd_softc;
    682 	int			s;
    683 
    684 	/*
    685 	 * Get to splhigh, 'real' interrupts arrive at spl6!
    686 	 */
    687 	s = splhigh();
    688 
    689 	/*
    690 	 * Make sure any privious write has ended...
    691 	 */
    692 	while (k->k_sendp != NULL)
    693 		tsleep((void *)&k->k_sendp, TTOPRI, "kbd_write1", 0);
    694 
    695 	/*
    696 	 * If the KBD-acia is not currently busy, send the first
    697 	 * character now.
    698 	 */
    699 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
    700 	if (KBD->ac_cs & A_TXRDY) {
    701 		KBD->ac_da = *cmd++;
    702 		len--;
    703 	}
    704 
    705 	/*
    706 	 * If we're not yet done, wait until all characters are send.
    707 	 */
    708 	if (len > 0) {
    709 		k->k_sendp    = cmd;
    710 		k->k_send_cnt = len;
    711 		tsleep((void *)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
    712 	}
    713 	splx(s);
    714 
    715 	/*
    716 	 * Wakeup all procs waiting for us.
    717 	 */
    718 	wakeup((void *)&k->k_sendp);
    719 }
    720 
    721 /*
    722  * Setup softc-fields to assemble a keyboard package.
    723  */
    724 static void
    725 kbd_pkg_start(struct kbd_softc *kp, uint8_t msg_start)
    726 {
    727 
    728 	kp->k_pkg_idx    = 1;
    729 	kp->k_package[0] = msg_start;
    730 	switch (msg_start) {
    731 	case 0xf6:
    732 		kp->k_pkg_type = KBD_MEM_PKG;
    733 		kp->k_pkg_size = 8;
    734 		break;
    735 	case 0xf7:
    736 		kp->k_pkg_type = KBD_AMS_PKG;
    737 		kp->k_pkg_size = 6;
    738 		break;
    739 	case 0xf8:
    740 	case 0xf9:
    741 	case 0xfa:
    742 	case 0xfb:
    743 		kp->k_pkg_type = KBD_RMS_PKG;
    744 		kp->k_pkg_size = 3;
    745 		break;
    746 	case 0xfc:
    747 		kp->k_pkg_type = KBD_CLK_PKG;
    748 		kp->k_pkg_size = 7;
    749 		break;
    750 	case 0xfe:
    751 		kp->k_pkg_type = KBD_JOY0_PKG;
    752 		kp->k_pkg_size = 2;
    753 		break;
    754 	case 0xff:
    755 		kp->k_pkg_type = KBD_JOY1_PKG;
    756 		kp->k_pkg_size = 2;
    757 		break;
    758 	default:
    759 		printf("kbd: Unknown packet 0x%x\n", msg_start);
    760 		break;
    761 	}
    762 }
    763 
    764 #if NITE > 0
    765 /*
    766  * Modifier processing
    767  */
    768 static int
    769 kbd_do_modifier(uint8_t code)
    770 {
    771 	uint8_t up, mask;
    772 
    773 	up   = KBD_RELEASED(code);
    774 	mask = 0;
    775 
    776 	switch (KBD_SCANCODE(code)) {
    777 	case KBD_LEFT_SHIFT:
    778 		mask = KBD_MOD_LSHIFT;
    779 		break;
    780 	case KBD_RIGHT_SHIFT:
    781 		mask = KBD_MOD_RSHIFT;
    782 		break;
    783 	case KBD_CTRL:
    784 		mask = KBD_MOD_CTRL;
    785 		break;
    786 	case KBD_ALT:
    787 		mask = KBD_MOD_ALT;
    788 		break;
    789 	case KBD_CAPS_LOCK:
    790 		/* CAPSLOCK is a toggle */
    791 		if (!up)
    792 			kbd_modifier ^= KBD_MOD_CAPS;
    793 		return 1;
    794 	}
    795 	if (mask) {
    796 		if (up)
    797 			kbd_modifier &= ~mask;
    798 		else
    799 			kbd_modifier |= mask;
    800 		return 1;
    801 	}
    802 	return 0;
    803 }
    804 #endif
    805 
    806 #if NWSKBD > 0
    807 /*
    808  * These are the callback functions that are passed to wscons.
    809  * They really don't do anything worth noting, just call the
    810  * other functions above.
    811  */
    812 
    813 static int
    814 kbd_enable(void *c, int on)
    815 {
    816 
    817 	/* Wonder what this is supposed to do... */
    818 	return 0;
    819 }
    820 
    821 static void
    822 kbd_set_leds(void *c, int leds)
    823 {
    824 
    825 	/* we can not set the leds */
    826 }
    827 
    828 static int
    829 kbd_ioctl(void *c, u_long cmd, void *data, int flag, struct lwp *p)
    830 {
    831 	struct wskbd_bell_data *kd;
    832 
    833 	switch (cmd) {
    834 	case WSKBDIO_COMPLEXBELL:
    835 		kd = (struct wskbd_bell_data *)data;
    836 		kbd_bell(0, kd->pitch, kd->period, kd->volume);
    837 		return 0;
    838 	case WSKBDIO_SETLEDS:
    839 		return 0;
    840 	case WSKBDIO_GETLEDS:
    841 		*(int *)data = 0;
    842 		return 0;
    843 	case WSKBDIO_GTYPE:
    844 		*(u_int *)data = WSKBD_TYPE_ATARI;
    845 		return 0;
    846 	}
    847 
    848 	/*
    849 	 * We are supposed to return EPASSTHROUGH to wscons if we didn't
    850 	 * understand.
    851 	 */
    852 	return EPASSTHROUGH;
    853 }
    854 
    855 static void
    856 kbd_getc(void *c, u_int *type, int *data)
    857 {
    858 	int key;
    859 	key = kbdgetcn();
    860 
    861 	*data = KBD_SCANCODE(key);
    862 	*type = KBD_RELEASED(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    863 }
    864 
    865 static void
    866 kbd_pollc(void *c, int on)
    867 {
    868 
    869 	kbd_softc.k_pollingmode = on;
    870 }
    871 
    872 static void
    873 kbd_bell(void *v, u_int pitch, u_int duration, u_int volume)
    874 {
    875 
    876 	kbd_bell_sparms(volume, pitch, duration);
    877 	kbdbell();
    878 }
    879 #endif /* NWSKBD */
    880