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