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