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