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