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