Home | History | Annotate | Line # | Download | only in tc
dtkbd.c revision 1.1.2.3
      1 /*	$NetBSD: dtkbd.c,v 1.1.2.3 2002/03/15 15:46:49 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.3 2002/03/15 15:46:49 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 
    157 	a.console = dtkbd_isconsole;
    158 	a.keymap = &dtkbd_keymapdata;
    159 	a.accessops = &dtkbd_accessops;
    160 	a.accesscookie = sc;
    161 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    162 }
    163 
    164 void
    165 dtkbd_cnattach(void)
    166 {
    167 
    168 	dtkbd_isconsole = 1;
    169 	dt_cninit();
    170 
    171 	wskbd_cnattach(&dtkbd_consops, &dtkbd_map, &dtkbd_keymapdata);
    172 }
    173 
    174 int
    175 dtkbd_enable(void *v, int on)
    176 {
    177 	struct dtkbd_softc *sc;
    178 
    179 	sc = v;
    180 	sc->sc_enabled = on;
    181 
    182 	return (0);
    183 }
    184 
    185 void
    186 dtkbd_cngetc(void *v, u_int *type, int *data)
    187 {
    188 	struct dt_msg msg;
    189 	static u_int types[20];
    190 	static int cnt, i, vals[20];
    191 
    192 	if (i >= cnt) {
    193 		for (;;) {
    194 			if (dt_msg_get(&msg, 0) == DT_GET_DONE)
    195 				if (msg.src == DT_ADDR_KBD)
    196 					break;
    197 			DELAY(1000);
    198 		}
    199 
    200 		cnt = dtkbd_process_msg(&msg, types, vals);
    201 		i = 0;
    202 	}
    203 
    204 	*type = types[i++];
    205 	*data = vals[i++];
    206 }
    207 
    208 void
    209 dtkbd_cnpollc(void *v, int on)
    210 {
    211 
    212 	/* XXX */
    213 }
    214 
    215 int
    216 dtkbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    217 {
    218 	struct dtkbd_softc *sc;
    219 
    220 	sc = (struct dtkbd_softc *)v;
    221 
    222 	switch (cmd) {
    223 	case WSKBDIO_GTYPE:
    224 		*(int *)data = WSKBD_TYPE_LK201;
    225 		return 0;
    226 	default:
    227 		/* XXX */
    228 		return (-1);
    229 	}
    230 }
    231 
    232 void
    233 dtkbd_set_leds(void *v, int state)
    234 {
    235 
    236 	/* XXX */
    237 }
    238 
    239 void
    240 dtkbd_handler(void *cookie)
    241 {
    242 	struct dtkbd_softc *sc;
    243 	struct dt_softc *dt;
    244 	struct dt_device *dtdv;
    245 	struct dt_msg *msg;
    246 	u_int types[20];
    247 	int i, cnt, vals[20];
    248 
    249 	dtdv = cookie;
    250 	sc = (struct dtkbd_softc *)dtdv->dtdv_dv;
    251 	dt = (struct dt_softc *)sc->sc_dv.dv_parent;
    252 
    253 	while ((msg = dt_msg_dequeue(dtdv)) != NULL) {
    254 		if (sc->sc_enabled) {
    255 			cnt = dtkbd_process_msg(msg, types, vals);
    256 			for (i = 0; i < cnt; i++)
    257 				wskbd_input(sc->sc_wskbddev, types[i],
    258 				    vals[i]);
    259 		}
    260 
    261 		dt_msg_release(dt, msg);
    262 	}
    263 }
    264 
    265 int
    266 dtkbd_process_msg(struct dt_msg *msg, u_int *types, int *vals)
    267 {
    268 	u_int len, c, count;
    269 	int i, j;
    270 
    271 	len = msg->code.val.len;
    272 
    273 	if ((msg->body[0] < DT_KBD_KEY_MIN && msg->body[0] != DT_KBD_EMPTY) ||
    274 	    len > 10) {
    275 		printf("dtkbd0: error: %x %x %x\n", len, msg->body[0],
    276 		    msg->body[1]);
    277 
    278 		/*
    279 		 * Fake an "all ups" to avoid the stuck key syndrome.
    280 		 */
    281 		msg->body[0] = DT_KBD_EMPTY;
    282 		len = 1;
    283 	}
    284 
    285 	if (msg->body[0] == DT_KBD_EMPTY) {
    286 		types[0] = WSCONS_EVENT_ALL_KEYS_UP;
    287 		vals[0] = 0;
    288 		dtkbd_maplen = 0;
    289 		return (1);
    290 	}
    291 
    292 	count = 0;
    293 
    294 	for (i = 0; i < len; i++) {
    295 		c = msg->body[i];
    296 
    297 		for (j = 0; j < dtkbd_maplen; j++)
    298 			if (dtkbd_map[j] == c)
    299 				break;
    300 
    301 		if (j == dtkbd_maplen) {
    302 			types[count] = WSCONS_EVENT_KEY_DOWN;
    303 			vals[count] = c - MIN_LK201_KEY;
    304 			count++;
    305 		}
    306 	}
    307 
    308 	for (j = 0; j < dtkbd_maplen; j++) {
    309 		c = dtkbd_map[j];
    310 
    311 		for (i = 0; i < len; i++)
    312 			if (msg->body[i] == c)
    313 				break;
    314 
    315 		if (i == len) {
    316 			types[count] = WSCONS_EVENT_KEY_UP;
    317 			vals[count] = c - MIN_LK201_KEY;
    318 			count++;
    319 		}
    320 	}
    321 
    322 	memcpy(dtkbd_map, msg->body, len);
    323 	dtkbd_maplen = len;
    324 
    325 	return (count);
    326 }
    327