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