Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.51
      1 /*	$NetBSD: kbd.c,v 1.51 2022/06/25 14:39:19 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.51 2022/06/25 14:39:19 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		k_event_mode;	/* if 1, collect events,	*/
     82 					/*   else pass to ite		*/
     83 	struct evvar	k_events;	/* event queue state		*/
     84 	uint8_t		k_soft_cs;	/* control-reg. copy		*/
     85 	uint8_t		k_package[20];	/* XXX package being build	*/
     86 	uint8_t		k_pkg_size;	/* Size of the package		*/
     87 	uint8_t		k_pkg_idx;	/* Running pkg assembly index	*/
     88 	uint8_t		k_pkg_type;	/* Type of package		*/
     89 	const uint8_t	*k_sendp;	/* Output pointer		*/
     90 	int		k_send_cnt;	/* Chars left for output	*/
     91 #if NWSKBD > 0
     92 	device_t	k_wskbddev;   /* pointer to wskbd for sending strokes */
     93 	int		k_pollingmode;	/* polling mode on? whatever it is... */
     94 #endif
     95 	void		*k_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;
    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 	int timeout;
    213 	const uint8_t kbd_rst[]  = { 0x80, 0x01 };
    214 	const uint8_t kbd_icmd[] = { 0x12, 0x15 };
    215 
    216 	/*
    217 	 * Disable keyboard interrupts from MFP
    218 	 */
    219 	MFP->mf_ierb &= ~IB_AINT;
    220 
    221 	/*
    222 	 * Reset ACIA and initialize to:
    223 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
    224 	 */
    225 	KBD->ac_cs = A_RESET;
    226 	delay(100);	/* XXX: enough? */
    227 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
    228 
    229 	/*
    230 	 * Clear error conditions
    231 	 */
    232 	while ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0)
    233 		timeout = KBD->ac_da;
    234 
    235 	/*
    236 	 * Now send the reset string, and read+ignore its response
    237 	 */
    238 	if (kbd_write_poll(kbd_rst, 2) == 0)
    239 		printf("kbd: error cannot reset keyboard\n");
    240 	for (timeout = 1000; timeout > 0; timeout--) {
    241 		if ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0) {
    242 			timeout = KBD->ac_da;
    243 			timeout = 100;
    244 		}
    245 		delay(100);
    246 	}
    247 	/*
    248 	 * Send init command: disable mice & joysticks
    249 	 */
    250 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
    251 
    252 	printf("\n");
    253 
    254 	kbd_softc.k_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 		kbd_softc.k_wskbddev = config_found(self, &waa, wskbddevprint,
    271 		    CFARGS_NONE);
    272 
    273 		kbd_softc.k_pollingmode = 0;
    274 
    275 		kbdenable();
    276 	}
    277 #endif /* WSKBD */
    278 }
    279 
    280 void
    281 kbdenable(void)
    282 {
    283 	int s;
    284 
    285 	s = spltty();
    286 
    287 	/*
    288 	 * Clear error conditions...
    289 	 */
    290 	while ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0)
    291 		(void)KBD->ac_da;
    292 	/*
    293 	 * Enable interrupts from MFP
    294 	 */
    295 	MFP->mf_iprb  = (uint8_t)~IB_AINT;
    296 	MFP->mf_ierb |= IB_AINT;
    297 	MFP->mf_imrb |= IB_AINT;
    298 
    299 	kbd_softc.k_event_mode   = 0;
    300 	kbd_softc.k_events.ev_io = 0;
    301 	kbd_softc.k_pkg_size     = 0;
    302 	splx(s);
    303 }
    304 
    305 static int
    306 kbdopen(dev_t dev, int flags, int mode, struct lwp *l)
    307 {
    308 
    309 	if (kbd_softc.k_events.ev_io)
    310 		return EBUSY;
    311 
    312 	kbd_softc.k_events.ev_io = l->l_proc;
    313 	ev_init(&kbd_softc.k_events);
    314 	return 0;
    315 }
    316 
    317 static int
    318 kbdclose(dev_t dev, int flags, int mode, struct lwp *l)
    319 {
    320 
    321 	/* Turn off event mode, dump the queue */
    322 	kbd_softc.k_event_mode = 0;
    323 	ev_fini(&kbd_softc.k_events);
    324 	kbd_softc.k_events.ev_io = NULL;
    325 	return 0;
    326 }
    327 
    328 static int
    329 kbdread(dev_t dev, struct uio *uio, int flags)
    330 {
    331 
    332 	return ev_read(&kbd_softc.k_events, uio, flags);
    333 }
    334 
    335 static int
    336 kbdioctl(dev_t dev, u_long cmd, register void *data, int flag, struct lwp *l)
    337 {
    338 	struct kbd_softc *k = &kbd_softc;
    339 	struct kbdbell	*kb;
    340 
    341 	switch (cmd) {
    342 	case KIOCTRANS:
    343 		if (*(int *)data == TR_UNTRANS_EVENT)
    344 			return 0;
    345 		break;
    346 
    347 	case KIOCGTRANS:
    348 		/*
    349 		 * Get translation mode
    350 		 */
    351 		*(int *)data = TR_UNTRANS_EVENT;
    352 		return 0;
    353 
    354 	case KIOCSDIRECT:
    355 		k->k_event_mode = *(int *)data;
    356 		return 0;
    357 
    358 	case KIOCRINGBELL:
    359 		kb = (struct kbdbell *)data;
    360 		if (kb)
    361 			kbd_bell_sparms(kb->volume, kb->pitch,
    362 			    kb->duration);
    363 		kbdbell();
    364 		return 0;
    365 
    366 	case FIONBIO:	/* we will remove this someday (soon???) */
    367 		return 0;
    368 
    369 	case FIOASYNC:
    370 		k->k_events.ev_async = *(int *)data != 0;
    371 		return 0;
    372 
    373 	case FIOSETOWN:
    374 		if (-*(int *)data != k->k_events.ev_io->p_pgid &&
    375 		    *(int *)data != k->k_events.ev_io->p_pid)
    376 			return EPERM;
    377 		return 0;
    378 
    379 	case TIOCSPGRP:
    380 		if (*(int *)data != k->k_events.ev_io->p_pgid)
    381 			return EPERM;
    382 		return 0;
    383 
    384 	default:
    385 		return ENOTTY;
    386 	}
    387 
    388 	/*
    389 	 * We identified the ioctl, but we do not handle it.
    390 	 */
    391 	return EOPNOTSUPP;		/* misuse, but what the heck */
    392 }
    393 
    394 static int
    395 kbdpoll(dev_t dev, int events, struct lwp *l)
    396 {
    397 
    398 	return ev_poll(&kbd_softc.k_events, events, l);
    399 }
    400 
    401 static int
    402 kbdkqfilter(dev_t dev, struct knote *kn)
    403 {
    404 
    405 	return ev_kqfilter(&kbd_softc.k_events, kn);
    406 }
    407 
    408 /*
    409  * Keyboard interrupt handler called straight from MFP at spl6.
    410  */
    411 void
    412 kbdintr(int sr)
    413 	/* sr: sr at time of interrupt	*/
    414 {
    415 	uint8_t code;
    416 	bool got_char = false;
    417 
    418 	/*
    419 	 * There may be multiple keys available. Read them all.
    420 	 */
    421 	while ((KBD->ac_cs & (A_RXRDY | A_OE | A_PE)) != 0) {
    422 		got_char = true;
    423 		code = KBD->ac_da;
    424 		if ((KBD->ac_cs & (A_OE | A_PE)) != 0) {
    425 			/* Silently ignore errors */
    426 			continue;
    427 		}
    428 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = code;
    429 	}
    430 
    431 	/*
    432 	 * If characters are waiting for transmit, send them.
    433 	 */
    434 	if ((kbd_softc.k_soft_cs & A_TXINT) != 0 &&
    435 	    (KBD->ac_cs & A_TXRDY) != 0) {
    436 		if (kbd_softc.k_sendp != NULL)
    437 			KBD->ac_da = *kbd_softc.k_sendp++;
    438 		if (--kbd_softc.k_send_cnt <= 0) {
    439 			/*
    440 			 * The total package has been transmitted,
    441 			 * wakeup anyone waiting for it.
    442 			 */
    443 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
    444 			kbd_softc.k_sendp    = NULL;
    445 			kbd_softc.k_send_cnt = 0;
    446 			wakeup((void *)&kbd_softc.k_send_cnt);
    447 		}
    448 	}
    449 
    450 	/*
    451 	 * Activate software-level to handle possible input.
    452 	 */
    453 	if (got_char)
    454 		softint_schedule(kbd_softc.k_sicookie);
    455 }
    456 
    457 /*
    458  * Keyboard soft interrupt handler
    459  */
    460 static void
    461 kbdsoft(void *junk1)
    462 {
    463 	int s;
    464 	uint8_t code;
    465 	struct kbd_softc *k = &kbd_softc;
    466 	struct firm_event *fe;
    467 	int put, get, n;
    468 
    469 	get = kbd_rbget;
    470 
    471 	for (;;) {
    472 		n = kbd_rbput;
    473 		if (get == n) /* We're done	*/
    474 			break;
    475 		n -= get;
    476 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
    477 			get += n - KBD_RING_SIZE;
    478 			n    = KBD_RING_SIZE;
    479 		}
    480 		while (--n >= 0) {
    481 			code = kbd_ring[get++ & KBD_RING_MASK];
    482 
    483 			/*
    484 			 * If collecting a package, stuff it in and
    485 			 * continue.
    486 			 */
    487 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
    488 			    k->k_package[k->k_pkg_idx++] = code;
    489 				if (k->k_pkg_idx == k->k_pkg_size) {
    490 					/*
    491 					 * Package is complete.
    492 					 */
    493 					switch (k->k_pkg_type) {
    494 #if NMOUSE > 0
    495 					case KBD_AMS_PKG:
    496 					case KBD_RMS_PKG:
    497 					case KBD_JOY1_PKG:
    498 						mouse_soft(
    499 						    (REL_MOUSE *)k->k_package,
    500 						    k->k_pkg_size,
    501 						    k->k_pkg_type);
    502 #endif /* NMOUSE */
    503 					}
    504 					k->k_pkg_size = 0;
    505 				}
    506 				continue;
    507 			}
    508 			/*
    509 			 * If this is a package header, init pkg. handling.
    510 			 */
    511 			if (!KBD_IS_KEY(code)) {
    512 				kbd_pkg_start(k, code);
    513 				continue;
    514 			}
    515 #if NWSKBD > 0
    516 			/*
    517 			 * If we have attached a wskbd and not in polling mode
    518 			 * and nobody has opened us directly, then send the
    519 			 * keystroke to the wskbd.
    520 			 */
    521 
    522 			if (kbd_softc.k_pollingmode == 0 &&
    523 			    kbd_softc.k_wskbddev != NULL &&
    524 			    k->k_event_mode == 0) {
    525 				wskbd_input(kbd_softc.k_wskbddev,
    526 				    KBD_RELEASED(code) ?
    527 				    WSCONS_EVENT_KEY_UP :
    528 				    WSCONS_EVENT_KEY_DOWN,
    529 				    KBD_SCANCODE(code));
    530 				continue;
    531 			}
    532 #endif /* NWSKBD */
    533 #if NITE > 0
    534 			if (kbd_do_modifier(code) && !k->k_event_mode)
    535 				continue;
    536 #endif
    537 
    538 			/*
    539 			 * if not in event mode, deliver straight to ite to
    540 			 * process key stroke
    541 			 */
    542 			if (!k->k_event_mode) {
    543 				/* Gets to spltty() by itself	*/
    544 #if NITE > 0
    545 				ite_filter(code, ITEFILT_TTY);
    546 #endif
    547 				continue;
    548 			}
    549 
    550 			/*
    551 			 * Keyboard is generating events.  Turn this keystroke
    552 			 * into an event and put it in the queue.  If the queue
    553 			 * is full, the keystroke is lost (sorry!).
    554 			 */
    555 			s = spltty();
    556 			put = k->k_events.ev_put;
    557 			fe  = &k->k_events.ev_q[put];
    558 			put = (put + 1) % EV_QSIZE;
    559 			if (put == k->k_events.ev_get) {
    560 				log(LOG_WARNING,
    561 				    "keyboard event queue overflow\n");
    562 				splx(s);
    563 				continue;
    564 			}
    565 			fe->id    = KBD_SCANCODE(code);
    566 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
    567 			firm_gettime(fe);
    568 			k->k_events.ev_put = put;
    569 			EV_WAKEUP(&k->k_events);
    570 			splx(s);
    571 		}
    572 		kbd_rbget = get;
    573 	}
    574 }
    575 
    576 static	uint8_t sound[] = {
    577 	0xA8, 0x01, 0xA9, 0x01, 0xAA, 0x01, 0x00,
    578 	0xF8, 0x10, 0x10, 0x10, 0x00, 0x20, 0x03
    579 };
    580 
    581 void
    582 kbdbell(void)
    583 {
    584 	register int	i, sps;
    585 
    586 	sps = splhigh();
    587 	for (i = 0; i < sizeof(sound); i++) {
    588 		YM2149->sd_selr = i;
    589 		YM2149->sd_wdat = sound[i];
    590 	}
    591 	splx(sps);
    592 }
    593 
    594 
    595 /*
    596  * Set the parameters of the 'default' beep.
    597  */
    598 
    599 #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
    600 #define KBDBELLDURATION	128	/* 256 / 2MHz */
    601 
    602 void
    603 kbd_bell_gparms(u_int *volume, u_int *pitch, u_int *duration)
    604 {
    605 	u_int	tmp;
    606 
    607 	tmp = sound[11] | (sound[12] << 8);
    608 	*duration = (tmp * KBDBELLDURATION) / 1000;
    609 
    610 	tmp = sound[0] | (sound[1] << 8);
    611 	*pitch = KBDBELLCLOCK / tmp;
    612 
    613 	*volume = 0;
    614 }
    615 
    616 void
    617 kbd_bell_sparms(u_int volume, u_int pitch, u_int duration)
    618 {
    619 	u_int	f, t;
    620 
    621 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
    622 	if (f > 20000)
    623 		f = 20000;		/* maximum pitch */
    624 
    625 	f = KBDBELLCLOCK / f;
    626 
    627 	t = (duration * 1000) / KBDBELLDURATION;
    628 
    629 	sound[ 0] = f & 0xff;
    630 	sound[ 1] = (f >> 8) & 0xf;
    631 	f -= 1;
    632 	sound[ 2] = f & 0xff;
    633 	sound[ 3] = (f >> 8) & 0xf;
    634 	f += 2;
    635 	sound[ 4] = f & 0xff;
    636 	sound[ 5] = (f >> 8) & 0xf;
    637 
    638 	sound[11] = t & 0xff;
    639 	sound[12] = (t >> 8) & 0xff;
    640 
    641 	sound[13] = 0x03;
    642 }
    643 
    644 int
    645 kbdgetcn(void)
    646 {
    647 	uint8_t code;
    648 	int s = spltty();
    649 	int ints_active;
    650 
    651 	ints_active = 0;
    652 	if ((MFP->mf_imrb & IB_AINT) != 0) {
    653 		ints_active   = 1;
    654 		MFP->mf_imrb &= ~IB_AINT;
    655 	}
    656 	for (;;) {
    657 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
    658 			continue;	/* Wait for key	*/
    659 		if ((KBD->ac_cs & (A_OE | A_PE)) != 0) {
    660 			code = KBD->ac_da;	/* Silently ignore errors */
    661 			continue;
    662 		}
    663 		code = KBD->ac_da;
    664 #if NITE>0
    665 		if (!kbd_do_modifier(code))
    666 #endif
    667 			break;
    668 	}
    669 
    670 	if (ints_active) {
    671 		MFP->mf_iprb  = (uint8_t)~IB_AINT;
    672 		MFP->mf_imrb |=  IB_AINT;
    673 	}
    674 
    675 	splx(s);
    676 	return code;
    677 }
    678 
    679 /*
    680  * Write a command to the keyboard in 'polled' mode.
    681  */
    682 static int
    683 kbd_write_poll(const uint8_t *cmd, int len)
    684 {
    685 	int timeout;
    686 
    687 	while (len-- > 0) {
    688 		KBD->ac_da = *cmd++;
    689 		for (timeout = 100; (KBD->ac_cs & A_TXRDY) == 0; timeout--)
    690 			delay(10);
    691 		if ((KBD->ac_cs & A_TXRDY) == 0)
    692 			return 0;
    693 	}
    694 	return 1;
    695 }
    696 
    697 /*
    698  * Write a command to the keyboard. Return when command is send.
    699  */
    700 void
    701 kbd_write(const uint8_t *cmd, int len)
    702 {
    703 	struct kbd_softc	*k = &kbd_softc;
    704 	int			s;
    705 
    706 	/*
    707 	 * Get to splhigh, 'real' interrupts arrive at spl6!
    708 	 */
    709 	s = splhigh();
    710 
    711 	/*
    712 	 * Make sure any privious write has ended...
    713 	 */
    714 	while (k->k_sendp != NULL)
    715 		tsleep((void *)&k->k_sendp, TTOPRI, "kbd_write1", 0);
    716 
    717 	/*
    718 	 * If the KBD-acia is not currently busy, send the first
    719 	 * character now.
    720 	 */
    721 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
    722 	if (KBD->ac_cs & A_TXRDY) {
    723 		KBD->ac_da = *cmd++;
    724 		len--;
    725 	}
    726 
    727 	/*
    728 	 * If we're not yet done, wait until all characters are send.
    729 	 */
    730 	if (len > 0) {
    731 		k->k_sendp    = cmd;
    732 		k->k_send_cnt = len;
    733 		tsleep((void *)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
    734 	}
    735 	splx(s);
    736 
    737 	/*
    738 	 * Wakeup all procs waiting for us.
    739 	 */
    740 	wakeup((void *)&k->k_sendp);
    741 }
    742 
    743 /*
    744  * Setup softc-fields to assemble a keyboard package.
    745  */
    746 static void
    747 kbd_pkg_start(struct kbd_softc *kp, uint8_t msg_start)
    748 {
    749 
    750 	kp->k_pkg_idx    = 1;
    751 	kp->k_package[0] = msg_start;
    752 	switch (msg_start) {
    753 	case 0xf6:
    754 		kp->k_pkg_type = KBD_MEM_PKG;
    755 		kp->k_pkg_size = 8;
    756 		break;
    757 	case 0xf7:
    758 		kp->k_pkg_type = KBD_AMS_PKG;
    759 		kp->k_pkg_size = 6;
    760 		break;
    761 	case 0xf8:
    762 	case 0xf9:
    763 	case 0xfa:
    764 	case 0xfb:
    765 		kp->k_pkg_type = KBD_RMS_PKG;
    766 		kp->k_pkg_size = 3;
    767 		break;
    768 	case 0xfc:
    769 		kp->k_pkg_type = KBD_CLK_PKG;
    770 		kp->k_pkg_size = 7;
    771 		break;
    772 	case 0xfe:
    773 		kp->k_pkg_type = KBD_JOY0_PKG;
    774 		kp->k_pkg_size = 2;
    775 		break;
    776 	case 0xff:
    777 		kp->k_pkg_type = KBD_JOY1_PKG;
    778 		kp->k_pkg_size = 2;
    779 		break;
    780 	default:
    781 		printf("kbd: Unknown packet 0x%x\n", msg_start);
    782 		break;
    783 	}
    784 }
    785 
    786 #if NITE > 0
    787 /*
    788  * Modifier processing
    789  */
    790 static int
    791 kbd_do_modifier(uint8_t code)
    792 {
    793 	uint8_t up, mask;
    794 
    795 	up   = KBD_RELEASED(code);
    796 	mask = 0;
    797 
    798 	switch (KBD_SCANCODE(code)) {
    799 	case KBD_LEFT_SHIFT:
    800 		mask = KBD_MOD_LSHIFT;
    801 		break;
    802 	case KBD_RIGHT_SHIFT:
    803 		mask = KBD_MOD_RSHIFT;
    804 		break;
    805 	case KBD_CTRL:
    806 		mask = KBD_MOD_CTRL;
    807 		break;
    808 	case KBD_ALT:
    809 		mask = KBD_MOD_ALT;
    810 		break;
    811 	case KBD_CAPS_LOCK:
    812 		/* CAPSLOCK is a toggle */
    813 		if (!up)
    814 			kbd_modifier ^= KBD_MOD_CAPS;
    815 		return 1;
    816 	}
    817 	if (mask) {
    818 		if (up)
    819 			kbd_modifier &= ~mask;
    820 		else
    821 			kbd_modifier |= mask;
    822 		return 1;
    823 	}
    824 	return 0;
    825 }
    826 #endif
    827 
    828 #if NWSKBD > 0
    829 /*
    830  * These are the callback functions that are passed to wscons.
    831  * They really don't do anything worth noting, just call the
    832  * other functions above.
    833  */
    834 
    835 static int
    836 kbd_enable(void *c, int on)
    837 {
    838 
    839 	/* Wonder what this is supposed to do... */
    840 	return 0;
    841 }
    842 
    843 static void
    844 kbd_set_leds(void *c, int leds)
    845 {
    846 
    847 	/* we can not set the leds */
    848 }
    849 
    850 static int
    851 kbd_ioctl(void *c, u_long cmd, void *data, int flag, struct lwp *p)
    852 {
    853 	struct wskbd_bell_data *kd;
    854 
    855 	switch (cmd) {
    856 	case WSKBDIO_COMPLEXBELL:
    857 		kd = (struct wskbd_bell_data *)data;
    858 		kbd_bell(0, kd->pitch, kd->period, kd->volume);
    859 		return 0;
    860 	case WSKBDIO_SETLEDS:
    861 		return 0;
    862 	case WSKBDIO_GETLEDS:
    863 		*(int *)data = 0;
    864 		return 0;
    865 	case WSKBDIO_GTYPE:
    866 		*(u_int *)data = WSKBD_TYPE_ATARI;
    867 		return 0;
    868 	}
    869 
    870 	/*
    871 	 * We are supposed to return EPASSTHROUGH to wscons if we didn't
    872 	 * understand.
    873 	 */
    874 	return EPASSTHROUGH;
    875 }
    876 
    877 static void
    878 kbd_getc(void *c, u_int *type, int *data)
    879 {
    880 	int key;
    881 	key = kbdgetcn();
    882 
    883 	*data = KBD_SCANCODE(key);
    884 	*type = KBD_RELEASED(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    885 }
    886 
    887 static void
    888 kbd_pollc(void *c, int on)
    889 {
    890 
    891 	kbd_softc.k_pollingmode = on;
    892 }
    893 
    894 static void
    895 kbd_bell(void *v, u_int pitch, u_int duration, u_int volume)
    896 {
    897 
    898 	kbd_bell_sparms(volume, pitch, duration);
    899 	kbdbell();
    900 }
    901 #endif /* NWSKBD */
    902