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