Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.35
      1  1.35        is /*	$NetBSD: kbd.c,v 1.35 2001/02/02 21:52:11 is Exp $	*/
      2  1.11       cgd 
      3   1.1        mw /*
      4   1.1        mw  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
      5   1.1        mw  * All rights reserved.
      6   1.1        mw  *
      7   1.1        mw  * Redistribution and use in source and binary forms, with or without
      8   1.1        mw  * modification, are permitted provided that the following conditions
      9   1.1        mw  * are met:
     10   1.1        mw  * 1. Redistributions of source code must retain the above copyright
     11   1.1        mw  *    notice, this list of conditions and the following disclaimer.
     12   1.1        mw  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        mw  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        mw  *    documentation and/or other materials provided with the distribution.
     15   1.1        mw  * 3. All advertising materials mentioning features or use of this software
     16   1.1        mw  *    must display the following acknowledgement:
     17   1.1        mw  *	This product includes software developed by the University of
     18   1.1        mw  *	California, Berkeley and its contributors.
     19   1.1        mw  * 4. Neither the name of the University nor the names of its contributors
     20   1.1        mw  *    may be used to endorse or promote products derived from this software
     21   1.1        mw  *    without specific prior written permission.
     22   1.1        mw  *
     23   1.1        mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1        mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1        mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1        mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1        mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1        mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1        mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1        mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1        mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1        mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1        mw  * SUCH DAMAGE.
     34   1.1        mw  *
     35   1.4        mw  *	kbd.c
     36   1.1        mw  */
     37   1.7    chopps #include <sys/param.h>
     38   1.7    chopps #include <sys/systm.h>
     39   1.9    chopps #include <sys/device.h>
     40   1.7    chopps #include <sys/ioctl.h>
     41   1.7    chopps #include <sys/tty.h>
     42   1.7    chopps #include <sys/proc.h>
     43   1.7    chopps #include <sys/file.h>
     44   1.7    chopps #include <sys/kernel.h>
     45   1.7    chopps #include <sys/syslog.h>
     46  1.18     veego #include <sys/signalvar.h>
     47   1.8    chopps #include <dev/cons.h>
     48   1.7    chopps #include <machine/cpu.h>
     49   1.9    chopps #include <amiga/amiga/device.h>
     50   1.7    chopps #include <amiga/amiga/custom.h>
     51  1.19        is #ifdef DRACO
     52  1.34        is #include <m68k/asm_single.h>
     53  1.19        is #include <amiga/amiga/drcustom.h>
     54  1.19        is #endif
     55   1.7    chopps #include <amiga/amiga/cia.h>
     56   1.9    chopps #include <amiga/dev/itevar.h>
     57   1.9    chopps #include <amiga/dev/kbdreg.h>
     58  1.18     veego #include <amiga/dev/kbdmap.h>
     59   1.7    chopps #include <amiga/dev/event_var.h>
     60   1.7    chopps #include <amiga/dev/vuid_event.h>
     61  1.35        is 
     62   1.9    chopps #include "kbd.h"
     63  1.35        is #include "ite.h"
     64  1.35        is 
     65  1.35        is /* WSKBD */
     66  1.35        is 
     67  1.35        is /*
     68  1.35        is  * If NWSKBD>0 we try to attach an wskbd device to us. What follows
     69  1.35        is  * is definitions of callback functions and structures that are passed
     70  1.35        is  * to wscons when initializing.
     71  1.35        is  */
     72  1.35        is 
     73  1.35        is #include "wskbd.h"
     74  1.35        is 
     75  1.35        is #if NWSKBD>0
     76  1.35        is #include <dev/wscons/wsconsio.h>
     77  1.35        is #include <dev/wscons/wskbdvar.h>
     78  1.35        is #include <dev/wscons/wsksymdef.h>
     79  1.35        is #include <dev/wscons/wsksymvar.h>
     80  1.35        is #include <amiga/dev/wskbdmap_amiga.h>
     81  1.35        is 
     82  1.35        is /* accessops */
     83  1.35        is int     kbd_enable    __P((void *, int));
     84  1.35        is void    kbd_set_leds  __P((void *, int));
     85  1.35        is int     kbd_ioctl     __P((void *, u_long, caddr_t, int, struct proc *));
     86  1.35        is 
     87  1.35        is /* console ops */
     88  1.35        is void    kbd_getc      __P((void *, u_int *, int *));
     89  1.35        is void    kbd_pollc     __P((void *, int));
     90  1.35        is void    kbd_bell      __P((void *, u_int, u_int, u_int));
     91  1.35        is 
     92  1.35        is static struct wskbd_accessops kbd_accessops = {
     93  1.35        is 	kbd_enable,
     94  1.35        is 	kbd_set_leds,
     95  1.35        is 	kbd_ioctl
     96  1.35        is };
     97  1.35        is 
     98  1.35        is static struct wskbd_consops kbd_consops = {
     99  1.35        is 	kbd_getc,
    100  1.35        is 	kbd_pollc,
    101  1.35        is 	kbd_bell
    102  1.35        is };
    103  1.35        is 
    104  1.35        is /*
    105  1.35        is  * Pointer to keymap. It is defined in wskbdmap_amiga.c.
    106  1.35        is  */
    107  1.35        is static struct wskbd_mapdata kbd_mapdata = {
    108  1.35        is 	amigakbd_keydesctab,
    109  1.35        is 	KB_US
    110  1.35        is };
    111  1.35        is 
    112  1.35        is #endif /* WSKBD */
    113  1.35        is 
    114   1.5        mw 
    115  1.18     veego #include <sys/conf.h>
    116  1.18     veego #include <machine/conf.h>
    117  1.18     veego 
    118   1.5        mw struct kbd_softc {
    119   1.9    chopps 	int k_event_mode;	/* if true, collect events, else pass to ite */
    120   1.9    chopps 	struct evvar k_events;	/* event queue state */
    121  1.19        is #ifdef DRACO
    122  1.19        is 	u_char k_rlprfx;	/* MF-II rel. prefix has been seen */
    123  1.31        is 	u_char k_mf2;
    124  1.19        is #endif
    125  1.35        is 
    126  1.35        is #if NWSKBD>0
    127  1.35        is 	struct device *k_wskbddev; /* pointer to wskbd for sending strokes */
    128  1.35        is 	int k_pollingmode;         /* polling mode on? whatever it isss... */
    129  1.35        is #endif
    130   1.9    chopps };
    131   1.9    chopps struct kbd_softc kbd_softc;
    132   1.9    chopps 
    133  1.28     veego int kbdmatch __P((struct device *, struct cfdata *, void *));
    134  1.17    mhitch void kbdattach __P((struct device *, struct device *, void *));
    135  1.18     veego void kbdintr __P((int));
    136  1.19        is void kbdstuffchar __P((u_char));
    137   1.9    chopps 
    138  1.31        is int drkbdgetc __P((void));
    139  1.31        is int drkbdrputc __P((int));
    140  1.31        is int drkbdputc __P((int));
    141  1.31        is int drkbdputc2 __P((int, int));
    142  1.31        is int drkbdwaitfor __P((int));
    143  1.31        is 
    144  1.16   thorpej struct cfattach kbd_ca = {
    145  1.16   thorpej 	sizeof(struct device), kbdmatch, kbdattach
    146  1.16   thorpej };
    147   1.9    chopps 
    148   1.9    chopps /*ARGSUSED*/
    149   1.9    chopps int
    150  1.28     veego kbdmatch(pdp, cfp, auxp)
    151   1.9    chopps 	struct device *pdp;
    152  1.28     veego 	struct cfdata *cfp;
    153  1.28     veego 	void *auxp;
    154   1.9    chopps {
    155  1.16   thorpej 
    156   1.9    chopps 	if (matchname((char *)auxp, "kbd"))
    157   1.9    chopps 		return(1);
    158   1.9    chopps 	return(0);
    159   1.9    chopps }
    160   1.9    chopps 
    161   1.9    chopps /*ARGSUSED*/
    162   1.9    chopps void
    163   1.9    chopps kbdattach(pdp, dp, auxp)
    164   1.9    chopps 	struct device *pdp, *dp;
    165   1.9    chopps 	void *auxp;
    166   1.9    chopps {
    167  1.19        is #ifdef DRACO
    168  1.31        is 	kbdenable();
    169  1.31        is 	if (kbd_softc.k_mf2)
    170  1.31        is 		printf(": QuickLogic type MF-II\n");
    171  1.31        is 	else
    172  1.31        is 		printf(": CIA A type Amiga\n");
    173  1.19        is #else
    174  1.26  christos 	printf(": CIA A type Amiga\n");
    175  1.19        is #endif
    176  1.19        is 
    177  1.35        is #if NWSKBD>0
    178  1.35        is 	if (dp != NULL) {
    179  1.35        is 		/*
    180  1.35        is 		 * Try to attach the wskbd.
    181  1.35        is 		 */
    182  1.35        is 		struct wskbddev_attach_args waa;
    183  1.35        is 
    184  1.35        is 		/* Maybe should be done before this?... */
    185  1.35        is 		wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata);
    186  1.35        is 
    187  1.35        is 		waa.console = 1;
    188  1.35        is 		waa.keymap = &kbd_mapdata;
    189  1.35        is 		waa.accessops = &kbd_accessops;
    190  1.35        is 		waa.accesscookie = NULL;
    191  1.35        is 		kbd_softc.k_wskbddev = config_found(dp, &waa, wskbddevprint);
    192  1.35        is 
    193  1.35        is 		kbd_softc.k_pollingmode = 0;
    194  1.35        is 	}
    195  1.35        is 	kbdenable();
    196  1.35        is #endif /* WSKBD */
    197   1.9    chopps }
    198   1.5        mw 
    199   1.5        mw /* definitions for amiga keyboard encoding. */
    200   1.5        mw #define KEY_CODE(c)  ((c) & 0x7f)
    201   1.5        mw #define KEY_UP(c)    ((c) & 0x80)
    202   1.5        mw 
    203  1.31        is #define DATLO single_inst_bclr_b(draco_ioct->io_control, DRCNTRL_KBDDATOUT)
    204  1.31        is #define DATHI single_inst_bset_b(draco_ioct->io_control, DRCNTRL_KBDDATOUT)
    205  1.31        is 
    206  1.31        is #define CLKLO single_inst_bclr_b(draco_ioct->io_control, DRCNTRL_KBDCLKOUT)
    207  1.31        is #define CLKHI single_inst_bset_b(draco_ioct->io_control, DRCNTRL_KBDCLKOUT)
    208  1.31        is 
    209   1.1        mw void
    210  1.18     veego kbdenable()
    211   1.1        mw {
    212  1.30        is 	static int kbd_inited = 0;
    213  1.30        is 
    214   1.9    chopps 	int s;
    215  1.22        is 
    216  1.21        is #ifdef DRACO
    217  1.31        is 	int id;
    218  1.21        is #endif
    219   1.9    chopps 	/*
    220   1.9    chopps 	 * collides with external ints from SCSI, watch out for this when
    221   1.9    chopps 	 * enabling/disabling interrupts there !!
    222   1.9    chopps 	 */
    223  1.30        is 	s = splhigh();	/* don't lower; might be called from early ddb */
    224  1.30        is 	if (kbd_inited) {
    225  1.30        is 		splx(s);
    226  1.30        is 		return;
    227  1.30        is 	}
    228  1.30        is 	kbd_inited = 1;
    229  1.19        is #ifdef DRACO
    230  1.31        is 	if (is_draco()) {
    231  1.31        is 
    232  1.31        is 		CLKLO;
    233  1.31        is 		delay(5000);
    234  1.31        is 		draco_ioct->io_kbdrst = 0;
    235  1.31        is 
    236  1.31        is 		if (drkbdputc(0xf2))
    237  1.31        is 			goto LnoMFII;
    238  1.31        is 
    239  1.31        is 		id = drkbdgetc() << 8;
    240  1.31        is 		id |= drkbdgetc();
    241  1.31        is 
    242  1.31        is 		if (id != 0xab83)
    243  1.31        is 			goto LnoMFII;
    244  1.19        is 
    245  1.31        is 		if (drkbdputc2(0xf0, 3))	/* mode 3 */
    246  1.31        is 			goto LnoMFII;
    247  1.31        is 
    248  1.31        is 		if (drkbdputc(0xf8))		/* make/break, no typematic */
    249  1.31        is 			goto LnoMFII;
    250  1.31        is 
    251  1.31        is 		if (drkbdputc(0xf4))		/* enable */
    252  1.31        is 			goto LnoMFII;
    253  1.31        is 		kbd_softc.k_mf2 = 1;
    254  1.33        is 		single_inst_bclr_b(draco_ioct->io_control, DRCNTRL_KBDINTENA);
    255  1.19        is 
    256  1.31        is 		ciaa.icr = CIA_ICR_SP;  /* CIA SP interrupt disable */
    257  1.31        is 		ciaa.cra &= ~(1<<6);	/* serial line == input */
    258  1.31        is 		splx(s);
    259  1.31        is 		return;
    260  1.19        is 
    261  1.31        is 	LnoMFII:
    262  1.31        is 		kbd_softc.k_mf2 = 0;
    263  1.33        is 		single_inst_bset_b(*draco_intena, DRIRQ_INT2);
    264  1.31        is 		ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;
    265  1.31        is 					/* SP interrupt enable */
    266  1.31        is 		ciaa.cra &= ~(1<<6);	/* serial line == input */
    267  1.31        is 		splx(s);
    268  1.31        is 		return;
    269  1.31        is 
    270  1.31        is 	} else {
    271  1.31        is #endif
    272   1.9    chopps 	custom.intena = INTF_SETCLR | INTF_PORTS;
    273   1.9    chopps 	ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;  /* SP interrupt enable */
    274   1.9    chopps 	ciaa.cra &= ~(1<<6);		/* serial line == input */
    275  1.31        is #ifdef DRACO
    276  1.31        is 	}
    277  1.19        is #endif
    278   1.9    chopps 	kbd_softc.k_event_mode = 0;
    279   1.9    chopps 	kbd_softc.k_events.ev_io = 0;
    280   1.9    chopps 	splx(s);
    281   1.1        mw }
    282   1.1        mw 
    283  1.32        is #ifdef DRACO
    284  1.31        is /*
    285  1.31        is  * call this with kbd interupt blocked
    286  1.31        is  */
    287  1.31        is 
    288  1.31        is int
    289  1.31        is drkbdgetc()
    290  1.31        is {
    291  1.31        is 	u_int8_t in;
    292  1.31        is 
    293  1.31        is 	while ((draco_ioct->io_status & DRSTAT_KBDRECV) == 0);
    294  1.31        is 	in = draco_ioct->io_kbddata;
    295  1.31        is 	draco_ioct->io_kbdrst = 0;
    296  1.31        is 
    297  1.31        is 	return in;
    298  1.31        is }
    299  1.31        is 
    300  1.31        is #define WAIT0 if (drkbdwaitfor(0)) goto Ltimeout
    301  1.31        is #define WAIT1 if (drkbdwaitfor(DRSTAT_KBDCLKIN)) goto Ltimeout
    302  1.31        is 
    303  1.31        is int
    304  1.31        is drkbdwaitfor(bit)
    305  1.31        is 	int bit;
    306  1.31        is {
    307  1.31        is 	int i;
    308  1.31        is 
    309  1.31        is 
    310  1.31        is 
    311  1.31        is 	i = 60000;	/* about 50 ms max */
    312  1.31        is 
    313  1.31        is 	do {
    314  1.31        is 		if ((draco_ioct->io_status & DRSTAT_KBDCLKIN) == bit)
    315  1.31        is 			return 0;
    316  1.31        is 
    317  1.31        is 	} while (--i >= 0);
    318  1.31        is 
    319  1.31        is 	return 1;
    320  1.31        is }
    321  1.31        is 
    322  1.31        is /*
    323  1.31        is  * Output a raw byte to the keyboard (+ parity and stop bit).
    324  1.31        is  * return 0 on success, 1 on timeout.
    325  1.31        is  */
    326  1.31        is int
    327  1.31        is drkbdrputc(c)
    328  1.31        is 	u_int8_t c;
    329  1.31        is {
    330  1.31        is 	u_int8_t parity;
    331  1.31        is 	int bitcnt;
    332  1.31        is 
    333  1.31        is 	DATLO; CLKHI; WAIT1;
    334  1.31        is 	parity = 0;
    335  1.31        is 
    336  1.31        is 	for (bitcnt=7; bitcnt >= 0; bitcnt--) {
    337  1.31        is 		WAIT0;
    338  1.31        is 		if (c & 1) {
    339  1.31        is 			DATHI;
    340  1.31        is 		} else {
    341  1.31        is 			++parity;
    342  1.31        is 			DATLO;
    343  1.31        is 		}
    344  1.31        is 		c >>= 1;
    345  1.31        is 		WAIT1;
    346  1.31        is 	}
    347  1.31        is 	WAIT0;
    348  1.31        is 	/* parity bit */
    349  1.31        is 	if (parity & 1) {
    350  1.31        is 		DATLO;
    351  1.31        is 	} else {
    352  1.31        is 		DATHI;
    353  1.31        is 	}
    354  1.31        is 	WAIT1;
    355  1.31        is 	/* stop bit */
    356  1.31        is 	WAIT0; DATHI; WAIT1;
    357  1.31        is 
    358  1.31        is 	WAIT0; /* XXX should check the ack bit here... */
    359  1.31        is 	WAIT1;
    360  1.31        is 	draco_ioct->io_kbdrst = 0;
    361  1.31        is 	return 0;
    362  1.31        is 
    363  1.31        is Ltimeout:
    364  1.31        is 	DATHI;
    365  1.31        is 	draco_ioct->io_kbdrst = 0;
    366  1.31        is 	return 1;
    367  1.31        is }
    368  1.31        is 
    369  1.31        is /*
    370  1.31        is  * Output one cooked byte to the keyboard, with wait for ACK or RESEND,
    371  1.31        is  * and retry if necessary. 0 == success, 1 == timeout
    372  1.31        is  */
    373  1.31        is int
    374  1.31        is drkbdputc(c)
    375  1.31        is 	u_int8_t c;
    376  1.31        is {
    377  1.31        is 	int rc;
    378  1.31        is 
    379  1.31        is 	do {
    380  1.31        is 		if (drkbdrputc(c))
    381  1.31        is 			return(-1);
    382  1.31        is 
    383  1.31        is 		rc = drkbdgetc();
    384  1.31        is 	} while (rc == 0xfe);
    385  1.31        is 	return (!(rc == 0xfa));
    386  1.31        is }
    387  1.31        is 
    388  1.31        is /*
    389  1.31        is  * same for twobyte sequence
    390  1.31        is  */
    391  1.31        is 
    392  1.31        is int
    393  1.31        is drkbdputc2(c1, c2)
    394  1.31        is 	u_int8_t c1, c2;
    395  1.31        is {
    396  1.31        is 	int rc;
    397  1.31        is 
    398  1.31        is 	do {
    399  1.31        is 		do {
    400  1.31        is 			if (drkbdrputc(c1))
    401  1.31        is 				return(-1);
    402  1.31        is 
    403  1.31        is 			rc = drkbdgetc();
    404  1.31        is 		} while (rc == 0xfe);
    405  1.31        is 		if (rc != 0xfa)
    406  1.31        is 			return (-1);
    407  1.31        is 
    408  1.31        is 		if (drkbdrputc(c2))
    409  1.31        is 			return(-1);
    410  1.31        is 
    411  1.31        is 		rc = drkbdgetc();
    412  1.31        is 	} while (rc == 0xfe);
    413  1.31        is 	return (!(rc == 0xfa));
    414  1.31        is }
    415  1.32        is #endif
    416   1.1        mw 
    417   1.1        mw int
    418  1.18     veego kbdopen(dev, flags, mode, p)
    419  1.18     veego 	dev_t dev;
    420  1.18     veego 	int flags, mode;
    421  1.18     veego 	struct proc *p;
    422   1.5        mw {
    423   1.5        mw 
    424  1.30        is 	kbdenable();
    425  1.18     veego 	if (kbd_softc.k_events.ev_io)
    426  1.18     veego 		return EBUSY;
    427   1.5        mw 
    428  1.18     veego 	kbd_softc.k_events.ev_io = p;
    429  1.18     veego 	ev_init(&kbd_softc.k_events);
    430  1.18     veego 	return (0);
    431   1.5        mw }
    432   1.5        mw 
    433   1.5        mw int
    434  1.18     veego kbdclose(dev, flags, mode, p)
    435  1.18     veego 	dev_t dev;
    436  1.18     veego 	int flags, mode;
    437  1.18     veego 	struct proc *p;
    438   1.5        mw {
    439  1.18     veego 
    440  1.18     veego 	/* Turn off event mode, dump the queue */
    441  1.18     veego 	kbd_softc.k_event_mode = 0;
    442  1.18     veego 	ev_fini(&kbd_softc.k_events);
    443  1.18     veego 	kbd_softc.k_events.ev_io = NULL;
    444  1.18     veego 	return (0);
    445   1.5        mw }
    446   1.5        mw 
    447   1.5        mw int
    448  1.18     veego kbdread(dev, uio, flags)
    449  1.18     veego 	dev_t dev;
    450  1.18     veego 	struct uio *uio;
    451  1.18     veego 	int flags;
    452   1.5        mw {
    453  1.18     veego 	return ev_read (&kbd_softc.k_events, uio, flags);
    454   1.5        mw }
    455   1.5        mw 
    456   1.5        mw int
    457  1.18     veego kbdioctl(dev, cmd, data, flag, p)
    458  1.18     veego 	dev_t dev;
    459  1.18     veego 	u_long cmd;
    460  1.18     veego 	register caddr_t data;
    461  1.18     veego 	int flag;
    462  1.18     veego 	struct proc *p;
    463   1.5        mw {
    464  1.18     veego 	register struct kbd_softc *k = &kbd_softc;
    465  1.18     veego 
    466  1.18     veego 	switch (cmd) {
    467  1.18     veego 		case KIOCTRANS:
    468  1.18     veego 			if (*(int *)data == TR_UNTRANS_EVENT)
    469  1.18     veego 				return 0;
    470  1.18     veego 			break;
    471  1.18     veego 
    472  1.18     veego 		case KIOCGTRANS:
    473  1.18     veego 			/* Get translation mode */
    474  1.18     veego 			*(int *)data = TR_UNTRANS_EVENT;
    475  1.18     veego 			return 0;
    476  1.18     veego 
    477  1.18     veego 		case KIOCSDIRECT:
    478  1.18     veego 			k->k_event_mode = *(int *)data;
    479  1.18     veego 			return 0;
    480  1.18     veego 
    481  1.18     veego 		case FIONBIO:	/* we will remove this someday (soon???) */
    482  1.18     veego 			return 0;
    483  1.18     veego 
    484  1.18     veego 		case FIOASYNC:
    485  1.18     veego 			k->k_events.ev_async = *(int *)data != 0;
    486  1.18     veego 			return 0;
    487  1.18     veego 
    488  1.18     veego 		case TIOCSPGRP:
    489  1.18     veego 			if (*(int *)data != k->k_events.ev_io->p_pgid)
    490  1.18     veego 				return EPERM;
    491  1.18     veego 			return 0;
    492  1.18     veego 
    493  1.18     veego 		default:
    494  1.18     veego 			return ENOTTY;
    495  1.18     veego 	}
    496   1.5        mw 
    497  1.18     veego 	/* We identified the ioctl, but we do not handle it. */
    498  1.18     veego 	return EOPNOTSUPP;	/* misuse, but what the heck */
    499   1.5        mw }
    500   1.5        mw 
    501   1.5        mw int
    502  1.25    mhitch kbdpoll(dev, events, p)
    503  1.18     veego 	dev_t dev;
    504  1.25    mhitch 	int events;
    505  1.18     veego 	struct proc *p;
    506   1.5        mw {
    507  1.25    mhitch 	return ev_poll (&kbd_softc.k_events, events, p);
    508   1.5        mw }
    509   1.5        mw 
    510   1.5        mw 
    511  1.18     veego void
    512  1.18     veego kbdintr(mask)
    513  1.18     veego 	int mask;
    514   1.1        mw {
    515  1.18     veego 	u_char c;
    516  1.13    chopps #ifdef KBDRESET
    517  1.18     veego 	static int reset_warn;
    518  1.13    chopps #endif
    519   1.1        mw 
    520  1.19        is 	/*
    521  1.19        is 	 * now only invoked from generic CIA interrupt handler if there *is*
    522  1.18     veego 	 * a keyboard interrupt pending
    523  1.18     veego 	 */
    524   1.1        mw 
    525  1.18     veego 	c = ~ciaa.sdr;	/* keyboard data is inverted */
    526  1.18     veego 	/* ack */
    527  1.18     veego 	ciaa.cra |= (1 << 6);	/* serial line output */
    528  1.13    chopps #ifdef KBDRESET
    529  1.18     veego 	if (reset_warn && c == 0xf0) {
    530  1.13    chopps #ifdef DEBUG
    531  1.26  christos 		printf ("kbdintr: !!!! Reset Warning !!!!\n");
    532  1.13    chopps #endif
    533  1.18     veego 		bootsync();
    534  1.18     veego 		reset_warn = 0;
    535  1.18     veego 		DELAY(30000000);
    536  1.18     veego 	}
    537  1.13    chopps #endif
    538  1.18     veego 	/* wait 200 microseconds (for bloody Cherry keyboards..) */
    539  1.18     veego 	DELAY(2000);			/* fudge delay a bit for some keyboards */
    540  1.18     veego 	ciaa.cra &= ~(1 << 6);
    541  1.18     veego 
    542  1.18     veego 	/* process the character */
    543  1.18     veego 	c = (c >> 1) | (c << 7);	/* rotate right once */
    544   1.1        mw 
    545  1.13    chopps #ifdef KBDRESET
    546  1.18     veego 	if (c == 0x78) {
    547  1.13    chopps #ifdef DEBUG
    548  1.26  christos 		printf ("kbdintr: Reset Warning started\n");
    549  1.13    chopps #endif
    550  1.18     veego 		++reset_warn;
    551  1.18     veego 		return;
    552  1.18     veego 	}
    553  1.13    chopps #endif
    554  1.19        is 	kbdstuffchar(c);
    555  1.19        is }
    556  1.19        is 
    557  1.19        is #ifdef DRACO
    558  1.19        is /* maps MF-II keycodes to Amiga keycodes */
    559  1.19        is 
    560  1.27        is const u_char drkbdtab[] = {
    561  1.19        is 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x50,
    562  1.19        is 	0x45, 0xff, 0xff, 0xff, 0xff, 0x42, 0x00, 0x51,
    563  1.19        is 
    564  1.19        is 	0xff, 0x64, 0x60, 0x30, 0x63, 0x10, 0x01, 0x52,
    565  1.19        is 	0xff, 0x66, 0x31, 0x21, 0x20, 0x11, 0x02, 0x53,
    566  1.19        is 
    567  1.19        is 	0xff, 0x33, 0x32, 0x22, 0x12, 0x04, 0x03, 0x54,
    568  1.19        is 	0xff, 0x40, 0x34, 0x23, 0x14, 0x13, 0x05, 0x55,
    569  1.19        is 
    570  1.19        is 	0xff, 0x36, 0x35, 0x25, 0x24, 0x15, 0x06, 0x56,
    571  1.19        is 	0xff, 0x67, 0x37, 0x26, 0x16, 0x07, 0x08, 0x57,
    572  1.19        is 	/* --- */
    573  1.19        is 	0xff, 0x38, 0x27, 0x17, 0x18, 0x0a, 0x09, 0x58,
    574  1.19        is 	0xff, 0x39, 0x3a, 0x28, 0x29, 0x19, 0x0b, 0x59,
    575  1.19        is 
    576  1.19        is 	0xff, 0xff, 0x2a, 0x2b, 0x1a, 0x0c, 0x4b, 0xff,
    577  1.19        is 	0x65, 0x61, 0x44, 0x1b, 0xff, 0xff, 0x6f, 0xff,
    578  1.19        is 
    579  1.19        is 	0x4d, 0x4f, 0xff, 0x4c, 0x0d, 0xff, 0x41, 0x46,
    580  1.19        is 	0xff, 0x1d, 0x4e, 0x2d, 0x3d, 0x4a, 0x5f, 0x62,
    581  1.19        is 
    582  1.19        is 	0x0f, 0x3c, 0x1e, 0x2e, 0x2f, 0x3e, 0x5a, 0x5b,
    583  1.19        is 	0xff, 0x43, 0x1f, 0xff, 0x5e, 0x3f, 0x5c, 0xff,
    584  1.19        is 	/* --- */
    585  1.19        is 	0xff, 0xff, 0xff, 0xff, 0x5d
    586  1.19        is };
    587  1.19        is #endif
    588  1.19        is 
    589  1.19        is 
    590  1.19        is int
    591  1.19        is kbdgetcn ()
    592  1.19        is {
    593  1.19        is 	int s;
    594  1.19        is 	u_char ints, mask, c, in;
    595  1.19        is 
    596  1.19        is #ifdef DRACO
    597  1.31        is 	if (is_draco() && kbd_softc.k_mf2) {
    598  1.31        is 		do {
    599  1.31        is 			c = 0;
    600  1.31        is 			s = spltty ();
    601  1.19        is 			while ((draco_ioct->io_status & DRSTAT_KBDRECV) == 0);
    602  1.19        is 			in = draco_ioct->io_kbddata;
    603  1.19        is 			draco_ioct->io_kbdrst = 0;
    604  1.31        is 			if (in == 0xF0) { /* release prefix */
    605  1.31        is 				c = 0x80;
    606  1.33        is 				while ((draco_ioct->io_status &
    607  1.33        is 				    DRSTAT_KBDRECV) == 0);
    608  1.31        is 				in = draco_ioct->io_kbddata;
    609  1.31        is 				draco_ioct->io_kbdrst = 0;
    610  1.31        is 			}
    611  1.31        is 			splx(s);
    612  1.19        is #ifdef DRACORAWKEYDEBUG
    613  1.31        is 			printf("<%02x>", in);
    614  1.19        is #endif
    615  1.31        is 			c |= in>=sizeof(drkbdtab) ? 0xff : drkbdtab[in];
    616  1.31        is 		} while (c == 0xff);
    617  1.31        is 		return (c);
    618  1.19        is 	}
    619  1.19        is #endif
    620  1.19        is 	s = spltty();
    621  1.19        is 	for (ints = 0; ! ((mask = ciaa.icr) & CIA_ICR_SP);
    622  1.19        is 	    ints |= mask) ;
    623  1.19        is 
    624  1.19        is 	in = ciaa.sdr;
    625  1.19        is 	c = ~in;
    626  1.19        is 
    627  1.19        is 	/* ack */
    628  1.19        is 	ciaa.cra |= (1 << 6);	/* serial line output */
    629  1.19        is 	ciaa.sdr = 0xff;	/* ack */
    630  1.19        is 	/* wait 200 microseconds */
    631  1.19        is 	DELAY(2000);	/* XXXX only works as long as DELAY doesn't
    632  1.19        is 			 * use a timer and waits.. */
    633  1.19        is 	ciaa.cra &= ~(1 << 6);
    634  1.19        is 	ciaa.sdr = in;
    635  1.19        is 
    636  1.19        is 	splx (s);
    637  1.19        is 	c = (c >> 1) | (c << 7);
    638  1.19        is 
    639  1.19        is 	/* take care that no CIA-interrupts are lost */
    640  1.19        is 	if (ints)
    641  1.19        is 		dispatch_cia_ints (0, ints);
    642  1.19        is 
    643  1.19        is 	return c;
    644  1.19        is }
    645  1.19        is 
    646  1.19        is void
    647  1.19        is kbdstuffchar(c)
    648  1.19        is 	u_char c;
    649  1.19        is {
    650  1.19        is 	struct firm_event *fe;
    651  1.19        is 	struct kbd_softc *k = &kbd_softc;
    652  1.19        is 	int put;
    653  1.19        is 
    654  1.35        is #if NWSKBD>0
    655  1.35        is 	/*
    656  1.35        is 	 * If we have attached a wskbd and not in polling mode and
    657  1.35        is 	 * nobody has opened us directly, then send the keystroke
    658  1.35        is 	 * to the wskbd.
    659  1.35        is 	 */
    660  1.35        is 
    661  1.35        is 	if (kbd_softc.k_pollingmode == 0
    662  1.35        is 	    && kbd_softc.k_wskbddev != NULL
    663  1.35        is 	    && k->k_event_mode == 0) {
    664  1.35        is 		wskbd_input(kbd_softc.k_wskbddev,
    665  1.35        is 			    KEY_UP(c) ?
    666  1.35        is 			    WSCONS_EVENT_KEY_UP :
    667  1.35        is 			    WSCONS_EVENT_KEY_DOWN,
    668  1.35        is 			    KEY_CODE(c));
    669  1.35        is 		return;
    670  1.35        is 	}
    671  1.35        is 
    672  1.35        is #endif /* NWSKBD */
    673  1.35        is 
    674  1.19        is 	/*
    675  1.19        is 	 * If not in event mode, deliver straight to ite to process
    676  1.19        is 	 * key stroke
    677  1.19        is 	 */
    678  1.19        is 
    679  1.18     veego 	if (! k->k_event_mode) {
    680  1.35        is #if NITE>0
    681  1.18     veego 		ite_filter (c, ITEFILT_TTY);
    682  1.35        is #endif
    683  1.18     veego 		return;
    684  1.18     veego 	}
    685  1.18     veego 
    686  1.19        is 	/*
    687  1.19        is 	 * Keyboard is generating events. Turn this keystroke into an
    688  1.19        is 	 * event and put it in the queue. If the queue is full, the
    689  1.18     veego 	 * keystroke is lost (sorry!).
    690  1.18     veego 	 */
    691  1.19        is 
    692  1.18     veego 	put = k->k_events.ev_put;
    693  1.18     veego 	fe = &k->k_events.ev_q[put];
    694  1.18     veego 	put = (put + 1) % EV_QSIZE;
    695  1.18     veego 	if (put == k->k_events.ev_get) {
    696  1.19        is 		log(LOG_WARNING, "keyboard event queue overflow\n");
    697  1.19        is 			/* ??? */
    698  1.18     veego 		return;
    699  1.18     veego 	}
    700  1.18     veego 	fe->id = KEY_CODE(c);
    701  1.18     veego 	fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    702  1.18     veego 	fe->time = time;
    703  1.18     veego 	k->k_events.ev_put = put;
    704  1.18     veego 	EV_WAKEUP(&k->k_events);
    705   1.1        mw }
    706   1.1        mw 
    707   1.1        mw 
    708  1.19        is #ifdef DRACO
    709  1.19        is void
    710  1.19        is drkbdintr()
    711   1.1        mw {
    712  1.19        is 	u_char in;
    713  1.19        is 	struct kbd_softc *k = &kbd_softc;
    714  1.18     veego 
    715  1.19        is 	in = draco_ioct->io_kbddata;
    716  1.19        is 	draco_ioct->io_kbdrst = 0;
    717   1.1        mw 
    718  1.19        is 	if (in == 0xF0)
    719  1.19        is 		k->k_rlprfx = 0x80;
    720  1.19        is 	else {
    721  1.19        is 		kbdstuffchar(in>=sizeof(drkbdtab) ? 0xff :
    722  1.19        is 		    drkbdtab[in] | k->k_rlprfx);
    723  1.19        is 		k->k_rlprfx = 0;
    724  1.19        is 	}
    725  1.19        is }
    726   1.1        mw 
    727  1.19        is #endif
    728  1.35        is 
    729  1.35        is 
    730  1.35        is #if NWSKBD>0
    731  1.35        is /*
    732  1.35        is  * These are the callback functions that are passed to wscons.
    733  1.35        is  * They really don't do anything worth noting, just call the
    734  1.35        is  * other functions above.
    735  1.35        is  */
    736  1.35        is 
    737  1.35        is int
    738  1.35        is kbd_enable(c, on)
    739  1.35        is 	void *c;
    740  1.35        is 	int   on;
    741  1.35        is {
    742  1.35        is 	/* Wonder what this is supposed to do... */
    743  1.35        is 	return (0);
    744  1.35        is }
    745  1.35        is 
    746  1.35        is void
    747  1.35        is kbd_set_leds(c, leds)
    748  1.35        is 	void *c;
    749  1.35        is 	int   leds;
    750  1.35        is {
    751  1.35        is }
    752  1.35        is 
    753  1.35        is int
    754  1.35        is kbd_ioctl(c, cmd, data, flag, p)
    755  1.35        is 	void        *c;
    756  1.35        is 	u_long       cmd;
    757  1.35        is 	caddr_t      data;
    758  1.35        is 	int          flag;
    759  1.35        is 	struct proc *p;
    760  1.35        is {
    761  1.35        is 	switch (cmd)
    762  1.35        is 	{
    763  1.35        is 	case WSKBDIO_COMPLEXBELL:
    764  1.35        is 		return 0;
    765  1.35        is 	case WSKBDIO_SETLEDS:
    766  1.35        is 		return 0;
    767  1.35        is 	case WSKBDIO_GETLEDS:
    768  1.35        is 		*(int*)data = 0;
    769  1.35        is 		return 0;
    770  1.35        is 	case WSKBDIO_GTYPE:
    771  1.35        is 		/* XXX well is it, dont think so */
    772  1.35        is 		*(u_int*)data = WSKBD_TYPE_PC_AT;
    773  1.35        is 		return 0;
    774  1.35        is 	}
    775  1.35        is 
    776  1.35        is 	/* We are supposed to return -1 to wscons if we didnt understand */
    777  1.35        is 	return (-1);
    778  1.35        is }
    779  1.35        is 
    780  1.35        is void
    781  1.35        is kbd_getc(c, type, data)
    782  1.35        is 	void  *c;
    783  1.35        is 	u_int *type;
    784  1.35        is 	int   *data;
    785  1.35        is {
    786  1.35        is 	int key;
    787  1.35        is 
    788  1.35        is 	key = kbdgetcn();
    789  1.35        is 
    790  1.35        is 	*data = KEY_CODE(key);
    791  1.35        is 	*type = KEY_UP(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    792  1.35        is }
    793  1.35        is 
    794  1.35        is void
    795  1.35        is kbd_pollc(c, on)
    796  1.35        is 	void *c;
    797  1.35        is 	int   on;
    798  1.35        is {
    799  1.35        is 	kbd_softc.k_pollingmode = on;
    800  1.35        is }
    801  1.35        is 
    802  1.35        is void
    803  1.35        is kbd_bell(c, x, y, z)
    804  1.35        is 	void  *c;
    805  1.35        is 	u_int  x;
    806  1.35        is 	u_int  y;
    807  1.35        is 	u_int  z;
    808  1.35        is {
    809  1.35        is }
    810  1.35        is #endif /* WSKBD */
    811