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