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