Home | History | Annotate | Line # | Download | only in tc
dtkbd.c revision 1.1.2.4
      1 /*	$NetBSD: dtkbd.c,v 1.1.2.4 2002/03/15 16:48:32 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: dtkbd.c,v 1.1.2.4 2002/03/15 16:48:32 ad Exp $");
     41 
     42 #include "locators.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/device.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/callout.h>
     49 
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wscons/wskbdvar.h>
     52 #include <dev/wscons/wsksymdef.h>
     53 #include <dev/wscons/wsksymvar.h>
     54 #include <dev/dec/wskbdmap_lk201.h>
     55 
     56 #include <machine/bus.h>
     57 
     58 #include <pmax/tc/dtreg.h>
     59 #include <pmax/tc/dtvar.h>
     60 
     61 struct dtkbd_softc {
     62 	struct device	sc_dv;
     63 	struct device	*sc_wskbddev;
     64 	int		sc_enabled;
     65 };
     66 
     67 int	dtkbd_match(struct device *, struct cfdata *, void *);
     68 void	dtkbd_attach(struct device *, struct device *, void *);
     69 int	dtkbd_enable(void *, int);
     70 int	dtkbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
     71 void	dtkbd_cngetc(void *, u_int *, int *);
     72 void	dtkbd_cnpollc(void *, int);
     73 int	dtkbd_process_msg(struct dt_msg *, u_int *, int *);
     74 void	dtkbd_handler(void *);
     75 void	dtkbd_set_leds(void *, int);
     76 void	dtkbd_cnattach(void);
     77 
     78 const struct wskbd_accessops dtkbd_accessops = {
     79 	dtkbd_enable,
     80 	dtkbd_set_leds,
     81 	dtkbd_ioctl,
     82 };
     83 
     84 const struct wskbd_consops dtkbd_consops = {
     85 	dtkbd_cngetc,
     86 	dtkbd_cnpollc,
     87 };
     88 
     89 struct cfattach dtkbd_ca = {
     90 	sizeof(struct dtkbd_softc), dtkbd_match, dtkbd_attach,
     91 };
     92 
     93 const struct wskbd_mapdata dtkbd_keymapdata = {
     94 	lkkbd_keydesctab,
     95 #ifdef DTKBD_LAYOUT
     96 	DTKBD_LAYOUT,
     97 #else
     98 	KB_US | KB_LK401,
     99 #endif
    100 };
    101 
    102 int	dtkbd_isconsole;
    103 int	dtkbd_map[10];
    104 int	dtkbd_maplen;
    105 
    106 int
    107 dtkbd_match(struct device *parent, struct cfdata *cf, void *aux)
    108 {
    109 	struct dt_attach_args *dta;
    110 	struct dt_ident ident;
    111 
    112 	dta = aux;
    113 
    114 	/*
    115 	 * Allow hard-wiring of addresses.
    116 	 */
    117 	if (cf->cf_loc[DTCF_ADDR] == dta->dta_addr)
    118 		return (2);
    119 
    120 	/*
    121 	 * The keyboard and mouse addresses are often swapped.  So, we
    122 	 * accept the standard address for either, and ask the device to
    123 	 * identify itself.
    124 	 */
    125 	if (cf->cf_loc[DTCF_ADDR] == DTCF_ADDR_DEFAULT &&
    126 	    (dta->dta_addr == DT_ADDR_KBD || dta->dta_addr == DT_ADDR_MOUSE)) {
    127 		if (dt_identify(dta->dta_addr, &ident))
    128 			return (0);
    129 		return (strncmp(ident.vendor, "DEC     ", 8) == 0 &&
    130 		    strncmp(ident.module, "LK501-", 6) == 0);
    131 	}
    132 
    133 	return (0);
    134 }
    135 
    136 void
    137 dtkbd_attach(struct device *parent, struct device *self, void *aux)
    138 {
    139 	struct dt_softc *dt;
    140 	struct dtkbd_softc *sc;
    141 	struct dt_attach_args *dta;
    142 	struct wskbddev_attach_args a;
    143 
    144 	dt = (struct dt_softc *)parent;
    145 	sc = (struct dtkbd_softc *)self;
    146 	dta = aux;
    147 
    148 	printf("\n");
    149 
    150 	if (dt_establish_handler(dt, dta->dta_addr, self, dtkbd_handler)) {
    151 		printf("%s: unable to establish handler\n", self->dv_xname);
    152 		return;
    153 	}
    154 
    155 	sc->sc_enabled = 1;
    156 	dt_kbd_addr = dta->dta_addr;
    157 
    158 	a.console = dtkbd_isconsole;
    159 	a.keymap = &dtkbd_keymapdata;
    160 	a.accessops = &dtkbd_accessops;
    161 	a.accesscookie = sc;
    162 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    163 }
    164 
    165 void
    166 dtkbd_cnattach(void)
    167 {
    168 
    169 	dtkbd_isconsole = 1;
    170 	dt_cninit();
    171 
    172 	wskbd_cnattach(&dtkbd_consops, &dtkbd_map, &dtkbd_keymapdata);
    173 }
    174 
    175 int
    176 dtkbd_enable(void *v, int on)
    177 {
    178 	struct dtkbd_softc *sc;
    179 
    180 	sc = v;
    181 	sc->sc_enabled = on;
    182 
    183 	return (0);
    184 }
    185 
    186 void
    187 dtkbd_cngetc(void *v, u_int *type, int *data)
    188 {
    189 	struct dt_msg msg;
    190 	static u_int types[20];
    191 	static int cnt, i, vals[20];
    192 
    193 	if (i >= cnt) {
    194 		for (;;) {
    195 			if (dt_msg_get(&msg, 0) == DT_GET_DONE)
    196 				if (msg.src == DT_ADDR_KBD &&
    197 				    !msg.code.val.P)
    198 					break;
    199 			DELAY(1000);
    200 		}
    201 
    202 		cnt = dtkbd_process_msg(&msg, types, vals);
    203 		i = 0;
    204 	}
    205 
    206 	*type = types[i++];
    207 	*data = vals[i++];
    208 }
    209 
    210 void
    211 dtkbd_cnpollc(void *v, int on)
    212 {
    213 
    214 	/* XXX */
    215 }
    216 
    217 int
    218 dtkbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    219 {
    220 	struct dtkbd_softc *sc;
    221 
    222 	sc = (struct dtkbd_softc *)v;
    223 
    224 	switch (cmd) {
    225 	case WSKBDIO_GTYPE:
    226 		*(int *)data = WSKBD_TYPE_LK201;
    227 		return 0;
    228 	default:
    229 		/* XXX */
    230 		return (-1);
    231 	}
    232 }
    233 
    234 void
    235 dtkbd_set_leds(void *v, int state)
    236 {
    237 
    238 	/* XXX */
    239 }
    240 
    241 void
    242 dtkbd_handler(void *cookie)
    243 {
    244 	struct dtkbd_softc *sc;
    245 	struct dt_softc *dt;
    246 	struct dt_device *dtdv;
    247 	struct dt_msg *msg;
    248 	u_int types[20];
    249 	int i, cnt, vals[20];
    250 
    251 	dtdv = cookie;
    252 	sc = (struct dtkbd_softc *)dtdv->dtdv_dv;
    253 	dt = (struct dt_softc *)sc->sc_dv.dv_parent;
    254 
    255 	while ((msg = dt_msg_dequeue(dtdv)) != NULL) {
    256 		if (sc->sc_enabled) {
    257 			if (!msg->code.val.P) {
    258 				cnt = dtkbd_process_msg(msg, types, vals);
    259 				for (i = 0; i < cnt; i++)
    260 					wskbd_input(sc->sc_wskbddev, types[i],
    261 					    vals[i]);
    262 			}
    263 		}
    264 
    265 		dt_msg_release(dt, msg);
    266 	}
    267 }
    268 
    269 int
    270 dtkbd_process_msg(struct dt_msg *msg, u_int *types, int *vals)
    271 {
    272 	u_int len, c, count;
    273 	int i, j;
    274 
    275 	len = msg->code.val.len;
    276 
    277 	if ((msg->body[0] < DT_KBD_KEY_MIN && msg->body[0] != DT_KBD_EMPTY) ||
    278 	    len > 10) {
    279 		printf("dtkbd0: error: %x %x %x\n", len, msg->body[0],
    280 		    msg->body[1]);
    281 
    282 		/*
    283 		 * Fake an "all ups" to avoid the stuck key syndrome.
    284 		 */
    285 		msg->body[0] = DT_KBD_EMPTY;
    286 		len = 1;
    287 	}
    288 
    289 	if (msg->body[0] == DT_KBD_EMPTY) {
    290 		types[0] = WSCONS_EVENT_ALL_KEYS_UP;
    291 		vals[0] = 0;
    292 		dtkbd_maplen = 0;
    293 		return (1);
    294 	}
    295 
    296 	count = 0;
    297 
    298 	for (i = 0; i < len; i++) {
    299 		c = msg->body[i];
    300 
    301 		for (j = 0; j < dtkbd_maplen; j++)
    302 			if (dtkbd_map[j] == c)
    303 				break;
    304 
    305 		if (j == dtkbd_maplen) {
    306 			types[count] = WSCONS_EVENT_KEY_DOWN;
    307 			vals[count] = c - MIN_LK201_KEY;
    308 			count++;
    309 		}
    310 	}
    311 
    312 	for (j = 0; j < dtkbd_maplen; j++) {
    313 		c = dtkbd_map[j];
    314 
    315 		for (i = 0; i < len; i++)
    316 			if (msg->body[i] == c)
    317 				break;
    318 
    319 		if (i == len) {
    320 			types[count] = WSCONS_EVENT_KEY_UP;
    321 			vals[count] = c - MIN_LK201_KEY;
    322 			count++;
    323 		}
    324 	}
    325 
    326 	memcpy(dtkbd_map, msg->body, len);
    327 	dtkbd_maplen = len;
    328 
    329 	return (count);
    330 }
    331