Home | History | Annotate | Line # | Download | only in tc
dtkbd.c revision 1.9
      1  1.9   tsutsui /*	$NetBSD: dtkbd.c,v 1.9 2011/06/04 01:37:36 tsutsui 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  *
     19  1.2        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2        ad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2        ad  */
     31  1.2        ad 
     32  1.2        ad #include <sys/cdefs.h>
     33  1.9   tsutsui __KERNEL_RCSID(0, "$NetBSD: dtkbd.c,v 1.9 2011/06/04 01:37:36 tsutsui Exp $");
     34  1.2        ad 
     35  1.2        ad #include "locators.h"
     36  1.2        ad 
     37  1.2        ad #include <sys/param.h>
     38  1.2        ad #include <sys/systm.h>
     39  1.2        ad #include <sys/device.h>
     40  1.2        ad #include <sys/ioctl.h>
     41  1.2        ad #include <sys/callout.h>
     42  1.2        ad 
     43  1.2        ad #include <dev/wscons/wsconsio.h>
     44  1.2        ad #include <dev/wscons/wskbdvar.h>
     45  1.2        ad #include <dev/wscons/wsksymdef.h>
     46  1.2        ad #include <dev/wscons/wsksymvar.h>
     47  1.2        ad #include <dev/dec/wskbdmap_lk201.h>
     48  1.2        ad #include <dev/tc/tcvar.h>
     49  1.2        ad 
     50  1.2        ad #include <machine/bus.h>
     51  1.2        ad 
     52  1.2        ad #include <pmax/tc/dtreg.h>
     53  1.2        ad #include <pmax/tc/dtvar.h>
     54  1.2        ad 
     55  1.2        ad #include <pmax/pmax/cons.h>
     56  1.2        ad 
     57  1.2        ad struct dtkbd_softc {
     58  1.9   tsutsui 	device_t	sc_dev;
     59  1.9   tsutsui 	device_t	sc_wskbddev;
     60  1.2        ad 	int		sc_enabled;
     61  1.2        ad };
     62  1.2        ad 
     63  1.9   tsutsui int	dtkbd_match(device_t, cfdata_t, void *);
     64  1.9   tsutsui void	dtkbd_attach(device_t, device_t, void *);
     65  1.2        ad int	dtkbd_enable(void *, int);
     66  1.7  christos int	dtkbd_ioctl(void *, u_long, void *, int, struct lwp *);
     67  1.2        ad void	dtkbd_cngetc(void *, u_int *, int *);
     68  1.2        ad void	dtkbd_cnpollc(void *, int);
     69  1.2        ad int	dtkbd_process_msg(struct dt_msg *, u_int *, int *);
     70  1.3        ad void	dtkbd_handler(void *, struct dt_msg *);
     71  1.2        ad void	dtkbd_set_leds(void *, int);
     72  1.2        ad 
     73  1.2        ad const struct wskbd_accessops dtkbd_accessops = {
     74  1.2        ad 	dtkbd_enable,
     75  1.2        ad 	dtkbd_set_leds,
     76  1.2        ad 	dtkbd_ioctl,
     77  1.2        ad };
     78  1.2        ad 
     79  1.2        ad const struct wskbd_consops dtkbd_consops = {
     80  1.2        ad 	dtkbd_cngetc,
     81  1.2        ad 	dtkbd_cnpollc,
     82  1.2        ad };
     83  1.2        ad 
     84  1.9   tsutsui CFATTACH_DECL_NEW(dtkbd, sizeof(struct dtkbd_softc),
     85  1.2        ad     dtkbd_match, dtkbd_attach, NULL, NULL);
     86  1.2        ad 
     87  1.2        ad const struct wskbd_mapdata dtkbd_keymapdata = {
     88  1.2        ad 	lkkbd_keydesctab,
     89  1.2        ad #ifdef DTKBD_LAYOUT
     90  1.2        ad 	DTKBD_LAYOUT,
     91  1.2        ad #else
     92  1.2        ad 	KB_US | KB_LK401,
     93  1.2        ad #endif
     94  1.2        ad };
     95  1.2        ad 
     96  1.2        ad int	dtkbd_isconsole;
     97  1.4    mhitch uint8_t	dtkbd_map[10];
     98  1.2        ad int	dtkbd_maplen;
     99  1.2        ad 
    100  1.2        ad int
    101  1.9   tsutsui dtkbd_match(device_t parent, cfdata_t cf, void *aux)
    102  1.2        ad {
    103  1.2        ad 	struct dt_attach_args *dta;
    104  1.2        ad 
    105  1.2        ad 	dta = aux;
    106  1.3        ad 	return (dta->dta_addr == DT_ADDR_KBD);
    107  1.2        ad }
    108  1.2        ad 
    109  1.2        ad void
    110  1.9   tsutsui dtkbd_attach(device_t parent, device_t self, void *aux)
    111  1.2        ad {
    112  1.2        ad 	struct dt_softc *dt;
    113  1.2        ad 	struct dtkbd_softc *sc;
    114  1.2        ad 	struct wskbddev_attach_args a;
    115  1.2        ad 
    116  1.9   tsutsui 	dt = device_private(parent);
    117  1.9   tsutsui 	sc = device_private(self);
    118  1.9   tsutsui 	sc->sc_dev = self;
    119  1.2        ad 
    120  1.2        ad 	printf("\n");
    121  1.2        ad 
    122  1.9   tsutsui 	if (dt_establish_handler(dt, &dt_kbd_dv, sc, dtkbd_handler)) {
    123  1.9   tsutsui 		printf("%s: unable to establish handler\n", device_xname(self));
    124  1.2        ad 		return;
    125  1.2        ad 	}
    126  1.2        ad 
    127  1.2        ad 	sc->sc_enabled = 1;
    128  1.2        ad 
    129  1.2        ad 	a.console = dtkbd_isconsole;
    130  1.2        ad 	a.keymap = &dtkbd_keymapdata;
    131  1.2        ad 	a.accessops = &dtkbd_accessops;
    132  1.2        ad 	a.accesscookie = sc;
    133  1.2        ad 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    134  1.2        ad }
    135  1.2        ad 
    136  1.2        ad void
    137  1.2        ad dtkbd_cnattach(void)
    138  1.2        ad {
    139  1.2        ad 
    140  1.2        ad 	dtkbd_isconsole = 1;
    141  1.2        ad 	dt_cninit();
    142  1.2        ad 
    143  1.2        ad 	wskbd_cnattach(&dtkbd_consops, &dtkbd_map, &dtkbd_keymapdata);
    144  1.2        ad }
    145  1.2        ad 
    146  1.2        ad int
    147  1.2        ad dtkbd_enable(void *v, int on)
    148  1.2        ad {
    149  1.2        ad 	struct dtkbd_softc *sc;
    150  1.2        ad 
    151  1.2        ad 	sc = v;
    152  1.2        ad 	sc->sc_enabled = on;
    153  1.2        ad 
    154  1.2        ad 	return (0);
    155  1.2        ad }
    156  1.2        ad 
    157  1.2        ad void
    158  1.2        ad dtkbd_cngetc(void *v, u_int *type, int *data)
    159  1.2        ad {
    160  1.2        ad 	struct dt_msg msg;
    161  1.2        ad 	static u_int types[20];
    162  1.2        ad 	static int cnt, i, vals[20];
    163  1.2        ad 
    164  1.3        ad 	while (i >= cnt) {
    165  1.2        ad 		for (;;) {
    166  1.2        ad 			if (dt_msg_get(&msg, 0) == DT_GET_DONE)
    167  1.3        ad 				if (msg.src == dt_kbd_addr &&
    168  1.3        ad 				    !DT_CTL_P(msg.ctl))
    169  1.2        ad 					break;
    170  1.2        ad 			DELAY(1000);
    171  1.2        ad 		}
    172  1.2        ad 
    173  1.2        ad 		cnt = dtkbd_process_msg(&msg, types, vals);
    174  1.2        ad 		i = 0;
    175  1.2        ad 	}
    176  1.2        ad 
    177  1.2        ad 	*type = types[i++];
    178  1.2        ad 	*data = vals[i++];
    179  1.2        ad }
    180  1.2        ad 
    181  1.2        ad void
    182  1.2        ad dtkbd_cnpollc(void *v, int on)
    183  1.2        ad {
    184  1.2        ad 
    185  1.2        ad 	/* XXX */
    186  1.2        ad }
    187  1.2        ad 
    188  1.2        ad int
    189  1.7  christos dtkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    190  1.2        ad {
    191  1.2        ad 	struct dtkbd_softc *sc;
    192  1.2        ad 
    193  1.9   tsutsui 	sc = v;
    194  1.2        ad 
    195  1.2        ad 	switch (cmd) {
    196  1.2        ad 	case WSKBDIO_GTYPE:
    197  1.2        ad 		*(int *)data = WSKBD_TYPE_LK201;
    198  1.2        ad 		return 0;
    199  1.2        ad 	default:
    200  1.2        ad 		/* XXX */
    201  1.5    mhitch 		return (EPASSTHROUGH);
    202  1.2        ad 	}
    203  1.2        ad }
    204  1.2        ad 
    205  1.2        ad void
    206  1.2        ad dtkbd_set_leds(void *v, int state)
    207  1.2        ad {
    208  1.2        ad 
    209  1.2        ad 	/* XXX */
    210  1.2        ad }
    211  1.2        ad 
    212  1.2        ad void
    213  1.3        ad dtkbd_handler(void *cookie, struct dt_msg *msg)
    214  1.2        ad {
    215  1.2        ad 	struct dtkbd_softc *sc;
    216  1.2        ad 	u_int types[20];
    217  1.2        ad 	int i, cnt, vals[20];
    218  1.2        ad 
    219  1.3        ad 	sc = cookie;
    220  1.3        ad 
    221  1.3        ad 	if (!sc->sc_enabled)
    222  1.3        ad 		return;
    223  1.2        ad 
    224  1.3        ad 	cnt = dtkbd_process_msg(msg, types, vals);
    225  1.3        ad 	for (i = 0; i < cnt; i++)
    226  1.3        ad 		wskbd_input(sc->sc_wskbddev, types[i], vals[i]);
    227  1.2        ad }
    228  1.2        ad 
    229  1.2        ad int
    230  1.2        ad dtkbd_process_msg(struct dt_msg *msg, u_int *types, int *vals)
    231  1.2        ad {
    232  1.2        ad 	u_int len, c, count;
    233  1.2        ad 	int i, j;
    234  1.2        ad 
    235  1.3        ad 	len = DT_CTL_LEN(msg->ctl);
    236  1.2        ad 
    237  1.2        ad 	if ((msg->body[0] < DT_KBD_KEY_MIN && msg->body[0] != DT_KBD_EMPTY) ||
    238  1.2        ad 	    len > 10) {
    239  1.2        ad 		printf("dtkbd0: error: %x %x %x\n", len, msg->body[0],
    240  1.2        ad 		    msg->body[1]);
    241  1.2        ad 
    242  1.2        ad 		/*
    243  1.2        ad 		 * Fake an "all ups" to avoid the stuck key syndrome.
    244  1.2        ad 		 */
    245  1.2        ad 		msg->body[0] = DT_KBD_EMPTY;
    246  1.2        ad 		len = 1;
    247  1.2        ad 	}
    248  1.2        ad 
    249  1.2        ad 	if (msg->body[0] == DT_KBD_EMPTY) {
    250  1.2        ad 		types[0] = WSCONS_EVENT_ALL_KEYS_UP;
    251  1.2        ad 		vals[0] = 0;
    252  1.2        ad 		dtkbd_maplen = 0;
    253  1.2        ad 		return (1);
    254  1.2        ad 	}
    255  1.2        ad 
    256  1.2        ad 	count = 0;
    257  1.2        ad 
    258  1.2        ad 	for (i = 0; i < len; i++) {
    259  1.2        ad 		c = msg->body[i];
    260  1.2        ad 
    261  1.2        ad 		for (j = 0; j < dtkbd_maplen; j++)
    262  1.2        ad 			if (dtkbd_map[j] == c)
    263  1.2        ad 				break;
    264  1.2        ad 
    265  1.2        ad 		if (j == dtkbd_maplen) {
    266  1.2        ad 			types[count] = WSCONS_EVENT_KEY_DOWN;
    267  1.2        ad 			vals[count] = c - MIN_LK201_KEY;
    268  1.2        ad 			count++;
    269  1.2        ad 		}
    270  1.2        ad 	}
    271  1.2        ad 
    272  1.2        ad 	for (j = 0; j < dtkbd_maplen; j++) {
    273  1.2        ad 		c = dtkbd_map[j];
    274  1.2        ad 
    275  1.2        ad 		for (i = 0; i < len; i++)
    276  1.2        ad 			if (msg->body[i] == c)
    277  1.2        ad 				break;
    278  1.2        ad 
    279  1.2        ad 		if (i == len) {
    280  1.2        ad 			types[count] = WSCONS_EVENT_KEY_UP;
    281  1.2        ad 			vals[count] = c - MIN_LK201_KEY;
    282  1.2        ad 			count++;
    283  1.2        ad 		}
    284  1.2        ad 	}
    285  1.2        ad 
    286  1.2        ad 	memcpy(dtkbd_map, msg->body, len);
    287  1.2        ad 	dtkbd_maplen = len;
    288  1.2        ad 
    289  1.2        ad 	return (count);
    290  1.2        ad }
    291