Home | History | Annotate | Line # | Download | only in dev
nextkbd.c revision 1.2
      1 /* $NetBSD: nextkbd.c,v 1.2 1999/03/24 23:15:52 dbj Exp $ */
      2 /*
      3  * Copyright (c) 1998 Matt DeBergalis
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Matt DeBergalis
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 #include <sys/proc.h>
     38 #include <sys/device.h>
     39 #include <sys/malloc.h>
     40 #include <sys/errno.h>
     41 #include <sys/queue.h>
     42 #include <sys/lock.h>
     43 
     44 #include <machine/autoconf.h>
     45 #include <machine/cpu.h>
     46 #include <machine/intr.h>
     47 #include <machine/bus.h>
     48 
     49 #include <next68k/dev/nextkbdvar.h>
     50 #include <next68k/dev/wskbdmap_next.h>
     51 
     52 #include <dev/wscons/wsconsio.h>
     53 #include <dev/wscons/wskbdvar.h>
     54 #include <dev/wscons/wsksymdef.h>
     55 #include <dev/wscons/wsksymvar.h>
     56 
     57 #include <next68k/next68k/isr.h>
     58 
     59 struct nextkbd_internal {
     60 	int num_ints; /* interrupt total */
     61 	int polling;
     62 	int isconsole;
     63 
     64 	bus_space_tag_t iot;
     65 	bus_space_handle_t ioh;
     66 	struct nextkbd_softc *t_sc; /* back pointer */
     67 	u_int32_t mods;
     68 };
     69 
     70 struct mon_regs {
     71 	u_int32_t mon_csr;
     72 	u_int32_t mon_1;
     73 	u_int32_t mon_data;
     74 };
     75 
     76 int nextkbd_match __P((struct device *, struct cfdata *, void *));
     77 void nextkbd_attach __P((struct device *, struct device *, void *));
     78 
     79 int nextkbc_cnattach __P((bus_space_tag_t));
     80 
     81 struct cfattach nextkbd_ca = {
     82 	sizeof(struct nextkbd_softc), nextkbd_match, nextkbd_attach
     83 };
     84 
     85 int	nextkbd_enable __P((void *, int));
     86 void	nextkbd_set_leds __P((void *, int));
     87 int	nextkbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     88 
     89 const struct wskbd_accessops nextkbd_accessops = {
     90 	nextkbd_enable,
     91 	nextkbd_set_leds,
     92 	nextkbd_ioctl,
     93 };
     94 
     95 void	nextkbd_cngetc __P((void *, u_int *, int *));
     96 void	nextkbd_cnpollc __P((void *, int));
     97 
     98 const struct wskbd_consops nextkbd_consops = {
     99 	nextkbd_cngetc,
    100 	nextkbd_cnpollc,
    101 };
    102 
    103 const struct wskbd_mapdata nextkbd_keymapdata = {
    104 	nextkbd_keydesctab,
    105 	KB_US,
    106 };
    107 
    108 static int nextkbd_poll_data __P((bus_space_tag_t, bus_space_handle_t));
    109 static int nextkbd_decode __P((struct nextkbd_internal *, int, u_int *, int *));
    110 
    111 static struct nextkbd_internal nextkbd_consdata;
    112 static int nextkbd_is_console __P((bus_space_tag_t bst));
    113 
    114 int nextkbdhard __P((void *));
    115 
    116 static int
    117 nextkbd_is_console(bst)
    118 	bus_space_tag_t bst;
    119 {
    120 	return (nextkbd_consdata.isconsole
    121 			&& (bst == nextkbd_consdata.iot));
    122 }
    123 
    124 int
    125 nextkbd_match(parent, match, aux)
    126 	struct device *parent;
    127 	struct cfdata *match;
    128 	void *aux;
    129 {
    130 	return 1;
    131 }
    132 
    133 void
    134 nextkbd_attach(parent, self, aux)
    135 	struct device *parent, *self;
    136 	void *aux;
    137 {
    138 	struct nextkbd_softc *sc = (struct nextkbd_softc *)self;
    139 	int isconsole;
    140 	struct wskbddev_attach_args a;
    141 
    142 	printf("\n");
    143 
    144 	isconsole = nextkbd_is_console(NEXT68K_INTIO_BUS_SPACE); /* XXX */
    145 
    146 	if (isconsole) {
    147 		sc->id = &nextkbd_consdata;
    148 	} else {
    149 		sc->id = malloc(sizeof(struct nextkbd_internal),
    150 				M_DEVBUF, M_WAITOK);
    151 
    152 		memset(sc->id, 0, sizeof(struct nextkbd_internal));
    153 		sc->id->iot = NEXT68K_INTIO_BUS_SPACE;
    154 		if (bus_space_map(sc->id->iot, NEXT_P_MON,
    155 				sizeof(struct mon_regs),
    156 				0, &sc->id->ioh)) {
    157 			printf("%s: can't map mon status control register\n",
    158 					sc->sc_dev.dv_xname);
    159 			return;
    160 		}
    161 	}
    162 
    163 	sc->id->t_sc = sc; /* set back pointer */
    164 
    165 	isrlink_autovec(nextkbdhard, sc, NEXT_I_IPL(NEXT_I_KYBD_MOUSE), 0);
    166 
    167 	INTR_ENABLE(NEXT_I_KYBD_MOUSE);
    168 
    169 	a.console = isconsole;
    170 	a.keymap = &nextkbd_keymapdata;
    171 	a.accessops = &nextkbd_accessops;
    172 	a.accesscookie = sc;
    173 
    174 	/*
    175 	 * Attach the wskbd, saving a handle to it.
    176 	 * XXX XXX XXX
    177 	 */
    178 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    179 }
    180 
    181 int
    182 nextkbd_enable(v, on)
    183 	void *v;
    184 	int on;
    185 {
    186 	/* XXX not sure if this should do anything */
    187 #if 0
    188 	printf("nextkbd_enable %d\n", on);
    189 #endif
    190 	return 0;
    191 }
    192 
    193 /* XXX not yet implemented */
    194 void
    195 nextkbd_set_leds(v, leds)
    196 	void *v;
    197 	int leds;
    198 {
    199 	return;
    200 }
    201 
    202 int
    203 nextkbd_ioctl(v, cmd, data, flag, p)
    204 	void *v;
    205 	u_long cmd;
    206 	caddr_t data;
    207 	int flag;
    208 	struct proc *p;
    209 {
    210 	/* XXX struct nextkbd_softc *nc = v; */
    211 
    212 	switch (cmd) {
    213 	case WSKBDIO_GTYPE:
    214 		/* XXX */
    215 		*(int *)data = WSKBD_TYPE_NEXT;
    216 		return (0);
    217 	case WSKBDIO_SETLEDS:
    218 		return (0);
    219 	case WSKBDIO_GETLEDS:
    220 		*(int *)data = 0;
    221 		return (0);
    222 	case WSKBDIO_COMPLEXBELL:
    223 		return (0);
    224 	}
    225 	return -1;
    226 }
    227 
    228 int
    229 nextkbdhard(arg)
    230 	void *arg;
    231 {
    232 	register struct nextkbd_softc *sc = arg;
    233 	struct mon_regs stat;
    234 	unsigned char device;
    235 	int type, key;
    236 
    237 	if (!INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) return 0;
    238 
    239 #define CSR_INT 0x00800000
    240 #define CSR_DATA 0x00400000
    241 
    242 #define KD_KEYMASK			0x007f
    243 #define KD_DIRECTION		0x0080 /* pressed or released */
    244 #define KD_CNTL					0x0100
    245 #define KD_LSHIFT				0x0200
    246 #define KD_RSHIFT				0x0400
    247 #define KD_LCOMM				0x0800
    248 #define KD_RCOMM				0x1000
    249 #define KD_LALT					0x2000
    250 #define KD_RALT					0x4000
    251 #define KD_VALID				0x8000 /* only set for scancode keys ? */
    252 #define KD_MODS					0x4f00
    253 
    254 	bus_space_read_region_4(sc->id->iot, sc->id->ioh, 0, &stat, 3);
    255 	if (stat.mon_csr & CSR_INT) {
    256                 if (stat.mon_csr & CSR_DATA) {
    257                         stat.mon_csr &= ~CSR_INT;
    258 			sc->id->num_ints++;
    259 			bus_space_write_4(sc->id->iot, sc->id->ioh, 0, stat.mon_csr);
    260 			device = stat.mon_data >> 28;
    261 			if (device != 1) return(1); /* XXX: mouse */
    262 			if (nextkbd_decode(sc->id, stat.mon_data & 0xffff, &type, &key)) {
    263 				wskbd_input(sc->sc_wskbddev, type, key);
    264 			}
    265 		} else {
    266                         printf("nextkbd: int but no data\n");
    267                         return(1);
    268                 }
    269 	}
    270 
    271 	return(1);
    272 }
    273 
    274 int
    275 nextkbd_cnattach(bst)
    276 	bus_space_tag_t bst;
    277 {
    278 	bus_space_handle_t bsh;
    279 
    280 	if (bus_space_map(bst, NEXT_P_MON, sizeof(struct mon_regs),
    281 			0, &bsh))
    282 		return (ENXIO);
    283 
    284 	memset(&nextkbd_consdata, 0, sizeof(nextkbd_consdata));
    285 
    286 	nextkbd_consdata.iot = bst;
    287 	nextkbd_consdata.ioh = bsh;
    288 	nextkbd_consdata.isconsole = 1;
    289 
    290 	wskbd_cnattach(&nextkbd_consops, &nextkbd_consdata,
    291 			&nextkbd_keymapdata);
    292 
    293 	return (0);
    294 }
    295 
    296 /* ARGSUSED */
    297 void
    298 nextkbd_cngetc(v, type, data)
    299 	void *v;
    300 	u_int *type;
    301 	int *data;
    302 {
    303 	struct nextkbd_internal *t = v;
    304 	int val;
    305 
    306 	/* printf("cngetc: data at %08x (%08x)\n", t, v); */
    307 	for (;;) {
    308 		val = nextkbd_poll_data(t->iot, t->ioh);
    309 		/* printf("%08x\n", val); */
    310 		if ((val != -1) && nextkbd_decode(t, val, type, data))
    311 			return;
    312 	}
    313 }
    314 
    315 void
    316 nextkbd_cnpollc(v, on)
    317 	void *v;
    318 	int on;
    319 {
    320 	struct nextkbd_internal *t = v;
    321 
    322 	printf("cnpollc %d\n", on);
    323 	t->polling = on;
    324 	if (on) {
    325 		INTR_DISABLE(NEXT_I_KYBD_MOUSE);
    326 	} else {
    327 		INTR_ENABLE(NEXT_I_KYBD_MOUSE);
    328 	}
    329 
    330 }
    331 
    332 static int
    333 nextkbd_poll_data(iot, ioh)
    334 	bus_space_tag_t iot;
    335 	bus_space_handle_t ioh;
    336 {
    337 	int i;
    338 	struct mon_regs stat;
    339 
    340 	/* printf("cnstart\n"); */
    341 	for (i=100000; i; i--) {
    342 		bus_space_read_region_4(iot, ioh, 0, &stat, 3);
    343                 stat.mon_csr &= ~CSR_INT;
    344                 if ( (stat.mon_csr & CSR_DATA) ) {
    345 			if ( (stat.mon_data >> 28) == 1) {
    346                           /* printf("cnkey %08x %08x\n", stat.mon_csr, stat.mon_data); */
    347 				bus_space_write_4(iot, ioh, 0, stat.mon_csr);
    348 				return (stat.mon_data & 0xffff);
    349 			}
    350 		}
    351 	}
    352 	/* printf("cnend %08x %08x\n", stat.mon_csr, stat.mon_data); */
    353 	return (-1);
    354 }
    355 
    356 static int
    357 nextkbd_decode(id, datain, type, dataout)
    358 	struct nextkbd_internal *id;
    359 	int datain;
    360 	u_int *type;
    361 	int *dataout;
    362 {
    363 	/* printf("datain %08x mods %08x\n", datain, id->mods); */
    364 
    365 	if ((datain ^ id->mods) & KD_LSHIFT) {
    366 		id->mods ^= KD_LSHIFT;
    367 		*dataout = 90;
    368 		if (datain & KD_LSHIFT)
    369 			*type = WSCONS_EVENT_KEY_DOWN;
    370 		else
    371 			*type = WSCONS_EVENT_KEY_UP;
    372 	} else if ((datain ^ id->mods) & KD_RSHIFT) {
    373 		id->mods ^= KD_RSHIFT;
    374 		*dataout = 91;
    375 		if (datain & KD_RSHIFT)
    376 			*type = WSCONS_EVENT_KEY_DOWN;
    377 		else
    378 			*type = WSCONS_EVENT_KEY_UP;
    379 	} else if ((datain ^ id->mods) & KD_LALT) {
    380 		id->mods ^= KD_LALT;
    381 		*dataout = 92;
    382 		if (datain & KD_LALT)
    383 			*type = WSCONS_EVENT_KEY_DOWN;
    384 		else
    385 			*type = WSCONS_EVENT_KEY_UP;
    386 	} else if ((datain ^ id->mods) & KD_RALT) {
    387 		id->mods ^= KD_RALT;
    388 		*dataout = 93;
    389 		if (datain & KD_RALT)
    390 			*type = WSCONS_EVENT_KEY_DOWN;
    391 		else
    392 			*type = WSCONS_EVENT_KEY_UP;
    393 	} else if ((datain ^ id->mods) & KD_CNTL) {
    394 		id->mods ^= KD_CNTL;
    395 		*dataout = 94;
    396 		if (datain & KD_CNTL)
    397 			*type = WSCONS_EVENT_KEY_DOWN;
    398 		else
    399 			*type = WSCONS_EVENT_KEY_UP;
    400 	} else if ((datain ^ id->mods) & KD_LCOMM) {
    401 		id->mods ^= KD_LCOMM;
    402 		*dataout = 95;
    403 		if (datain & KD_LCOMM)
    404 			*type = WSCONS_EVENT_KEY_DOWN;
    405 		else
    406 			*type = WSCONS_EVENT_KEY_UP;
    407 	} else if ((datain ^ id->mods) & KD_RCOMM) {
    408 		id->mods ^= KD_RCOMM;
    409 		*dataout = 96;
    410 		if (datain & KD_RCOMM)
    411 			*type = WSCONS_EVENT_KEY_DOWN;
    412 		else
    413 			*type = WSCONS_EVENT_KEY_UP;
    414 	} else if (datain & KD_KEYMASK) {
    415 		if (datain & KD_DIRECTION)
    416 			*type = WSCONS_EVENT_KEY_UP;
    417 		else
    418 			*type = WSCONS_EVENT_KEY_DOWN;
    419 
    420 		*dataout = (datain & KD_KEYMASK);
    421 	} else {
    422 		*dataout = 0;
    423 	}
    424 
    425 	return 1;
    426 }
    427