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