Home | History | Annotate | Line # | Download | only in tc
dt.c revision 1.8
      1 /*	$NetBSD: dt.c,v 1.8 2007/10/08 16:18:02 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2003 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 /*-
     40  * Copyright (c) 1992, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Ralph Campbell and Rick Macklem.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)dtop.c	8.2 (Berkeley) 11/30/93
     71  */
     72 
     73 /*
     74  * Mach Operating System
     75  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
     76  * All Rights Reserved.
     77  *
     78  * Permission to use, copy, modify and distribute this software and its
     79  * documentation is hereby granted, provided that both the copyright
     80  * notice and this permission notice appear in all copies of the
     81  * software, derivative works or modified versions, and any portions
     82  * thereof, and that both notices appear in supporting documentation.
     83  *
     84  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     85  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     86  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     87  *
     88  * Carnegie Mellon requests users of this software to return to
     89  *
     90  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     91  *  School of Computer Science
     92  *  Carnegie Mellon University
     93  *  Pittsburgh PA 15213-3890
     94  *
     95  * any improvements or extensions that they make and grant Carnegie the
     96  * rights to redistribute these changes.
     97  */
     98 /*
     99  * 	Author: Alessandro Forin, Carnegie Mellon University
    100  *
    101  *	Hardware-level operations for the Desktop serial line
    102  *	bus (i2c aka ACCESS).
    103  */
    104 /************************************************************
    105 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
    106 and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
    107 
    108                         All Rights Reserved
    109 
    110 Permission to use, copy, modify, and distribute this software and its
    111 documentation for any purpose and without fee is hereby granted,
    112 provided that the above copyright notice appear in all copies and that
    113 both that copyright notice and this permission notice appear in
    114 supporting documentation, and that the names of Digital or MIT not be
    115 used in advertising or publicity pertaining to distribution of the
    116 software without specific, written prior permission.
    117 
    118 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
    119 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
    120 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
    121 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
    122 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
    123 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
    124 SOFTWARE.
    125 
    126 ********************************************************/
    127 
    128 /*
    129  * ACCESS.bus device support for the Personal DECstation.  This code handles
    130  * only the keyboard and mouse, and will likely not work if other ACCESS.bus
    131  * devices are physically attached to the system.
    132  *
    133  * Since we do not know how to drive the hardware (the only reference being
    134  * Mach), we can't identify which devices are connected to the system by
    135  * sending idenfication requests.  With only a mouse and keyboard attached
    136  * to the system, we do know which two slave addresses will be in use.
    137  * However, we don't know which is the mouse, and which is the keyboard.
    138  * So, we resort to inspecting device reports and making an educated guess
    139  * as to which is which.
    140  */
    141 
    142 #include <sys/cdefs.h>
    143 __KERNEL_RCSID(0, "$NetBSD: dt.c,v 1.8 2007/10/08 16:18:02 ad Exp $");
    144 
    145 #include <sys/param.h>
    146 #include <sys/systm.h>
    147 #include <sys/tty.h>
    148 #include <sys/proc.h>
    149 #include <sys/conf.h>
    150 #include <sys/file.h>
    151 #include <sys/kernel.h>
    152 #include <sys/device.h>
    153 #include <sys/malloc.h>
    154 #include <sys/intr.h>
    155 
    156 #include <dev/dec/lk201.h>
    157 
    158 #include <dev/tc/tcvar.h>
    159 #include <dev/tc/ioasicreg.h>
    160 #include <dev/tc/ioasicvar.h>
    161 
    162 #include <pmax/pmax/maxine.h>
    163 
    164 #include <pmax/tc/dtreg.h>
    165 #include <pmax/tc/dtvar.h>
    166 
    167 #define	DT_BUF_CNT		16
    168 #define	DT_ESC_CHAR		0xf8
    169 #define	DT_XMT_OK		0xfb
    170 #define	DT_MAX_POLL		0x70000		/* about half a sec */
    171 
    172 #define	DT_GET_BYTE(data)	(((*(data)) >> 8) & 0xff)
    173 #define	DT_PUT_BYTE(data,c)	{ *(data) = (c) << 8; wbflush(); }
    174 
    175 #define	DT_RX_AVAIL(poll)	((*(poll) & 1) != 0)
    176 #define	DT_TX_AVAIL(poll)	((*(poll) & 2) != 0)
    177 
    178 int	dt_match(struct device *, struct cfdata *, void *);
    179 void	dt_attach(struct device *, struct device *, void *);
    180 int	dt_intr(void *);
    181 int	dt_null_handler(struct device *, struct dt_msg *, int);
    182 int	dt_print(void *, const char *);
    183 void	dt_strvis(uint8_t *, char *, int);
    184 void	dt_dispatch(void *);
    185 
    186 int	dt_kbd_addr = DT_ADDR_KBD;
    187 struct	dt_device dt_kbd_dv;
    188 int	dt_ms_addr = DT_ADDR_MOUSE;
    189 struct	dt_device dt_ms_dv;
    190 struct	dt_state dt_state;
    191 
    192 CFATTACH_DECL(dt, sizeof(struct dt_softc),
    193     dt_match, dt_attach, NULL, NULL);
    194 
    195 int
    196 dt_match(struct device *parent, struct cfdata *match, void *aux)
    197 {
    198 	struct ioasicdev_attach_args *d;
    199 
    200 	d = aux;
    201 
    202 	if (strcmp(d->iada_modname, "dtop") != 0)
    203 		return (0);
    204 
    205 	if (badaddr((void *)(d->iada_addr), 2))
    206 		return (0);
    207 
    208 	return (1);
    209 }
    210 
    211 void
    212 dt_attach(struct device *parent, struct device *self, void *aux)
    213 {
    214 	struct ioasicdev_attach_args *d;
    215 	struct dt_attach_args dta;
    216 	struct dt_softc *sc;
    217 	struct dt_msg *msg;
    218 	int i;
    219 
    220 	d = aux;
    221 	sc = (struct dt_softc*)self;
    222 
    223 	dt_cninit();
    224 
    225 	msg = malloc(sizeof(*msg) * DT_BUF_CNT, M_DEVBUF, M_NOWAIT);
    226 	if (msg == NULL) {
    227 		printf("%s: memory exhausted\n", sc->sc_dv.dv_xname);
    228 		return;
    229 	}
    230 
    231 	sc->sc_sih = softint_establish(SOFTINT_SERIAL, dt_dispatch, sc);
    232 	if (sc->sc_sih == NULL) {
    233 		printf("%s: memory exhausted\n", sc->sc_dv.dv_xname);
    234 		free(msg, M_DEVBUF);
    235 	}
    236 
    237 	SIMPLEQ_INIT(&sc->sc_queue);
    238 	SLIST_INIT(&sc->sc_free);
    239 	for (i = 0; i < DT_BUF_CNT; i++, msg++)
    240 		SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);
    241 
    242 	ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_TTY, dt_intr, sc);
    243 	printf("\n");
    244 
    245 	dta.dta_addr = DT_ADDR_KBD;
    246 	config_found(self, &dta, dt_print);
    247 	dta.dta_addr = DT_ADDR_MOUSE;
    248 	config_found(self, &dta, dt_print);
    249 }
    250 
    251 void
    252 dt_cninit(void)
    253 {
    254 
    255 	dt_state.ds_poll = (volatile u_int *)
    256 	    MIPS_PHYS_TO_KSEG1(XINE_REG_INTR);
    257 	dt_state.ds_data = (volatile u_int *)
    258 	    MIPS_PHYS_TO_KSEG1(XINE_PHYS_TC_3_START + 0x280000);
    259 }
    260 
    261 int
    262 dt_print(void *aux, const char *pnp)
    263 {
    264 
    265 	return (QUIET);
    266 }
    267 
    268 int
    269 dt_establish_handler(struct dt_softc *sc, struct dt_device *dtdv,
    270     struct device *dv, void (*hdlr)(void *, struct dt_msg *))
    271 {
    272 
    273 	dtdv->dtdv_dv = dv;
    274 	dtdv->dtdv_handler = hdlr;
    275 	return (0);
    276 }
    277 
    278 int
    279 dt_intr(void *cookie)
    280 {
    281 	struct dt_softc *sc;
    282 	struct dt_msg *msg, *pend;
    283 
    284 	sc = cookie;
    285 
    286 	switch (dt_msg_get(&sc->sc_msg, 1)) {
    287 	case DT_GET_ERROR:
    288 		/*
    289 		 * Ugh! The most common occurrence of a data overrun is upon
    290 		 * a key press and the result is a software generated "stuck
    291 		 * key".  All I can think to do is fake an "all keys up"
    292 		 * whenever a data overrun occurs.
    293 		 */
    294 		sc->sc_msg.src = dt_kbd_addr;
    295 		sc->sc_msg.ctl = DT_CTL(1, 0, 0);
    296 		sc->sc_msg.body[0] = DT_KBD_EMPTY;
    297 #ifdef DIAGNOSTIC
    298 		printf("%s: data overrun or stray interrupt\n",
    299 		    sc->sc_dv.dv_xname);
    300 #endif
    301 		break;
    302 
    303 	case DT_GET_DONE:
    304 		break;
    305 
    306 	case DT_GET_NOTYET:
    307 		return (1);
    308 	}
    309 
    310 	if ((msg = SLIST_FIRST(&sc->sc_free)) == NULL) {
    311 		printf("%s: input overflow\n", sc->sc_dv.dv_xname);
    312 		return (1);
    313 	}
    314 	SLIST_REMOVE_HEAD(&sc->sc_free, chain.slist);
    315 	memcpy(msg, &sc->sc_msg, sizeof(*msg));
    316 
    317 	pend = SIMPLEQ_FIRST(&sc->sc_queue);
    318 	SIMPLEQ_INSERT_TAIL(&sc->sc_queue, msg, chain.simpleq);
    319 	if (pend == NULL)
    320 		softint_schedule(sc->sc_sih);
    321 
    322 	return (1);
    323 }
    324 
    325 void
    326 dt_dispatch(void *cookie)
    327 {
    328 	struct dt_softc *sc;
    329 	struct dt_msg *msg;
    330 	int s;
    331 	struct dt_device *dtdv;
    332 
    333 	sc = cookie;
    334 	msg = NULL;
    335 
    336 	for (;;) {
    337 		s = spltty();
    338 		if (msg != NULL)
    339 			SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);
    340 		msg = SIMPLEQ_FIRST(&sc->sc_queue);
    341 		if (msg != NULL)
    342 			SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, chain.simpleq);
    343 		splx(s);
    344 		if (msg == NULL)
    345 			break;
    346 
    347 		if (msg->src != DT_ADDR_MOUSE && msg->src != DT_ADDR_KBD) {
    348 			printf("%s: message from unknown dev 0x%x\n",
    349 			    sc->sc_dv.dv_xname, sc->sc_msg.src);
    350 			dt_msg_dump(msg);
    351 			continue;
    352 		}
    353 		if (DT_CTL_P(msg->ctl) != 0) {
    354 			printf("%s: received control message\n",
    355 			    sc->sc_dv.dv_xname);
    356 			dt_msg_dump(msg);
    357 			continue;
    358 		}
    359 
    360 		/*
    361 		 * 1. Mouse should have no more than eight buttons, so first
    362 		 *    8 bits of body will be zero.
    363 		 * 2. Mouse should always send full locator report.
    364 		 *    Note:  my mouse does not send 'z' data, so the size
    365 		 *    did not match the size of struct dt_locator_msg - mhitch
    366 		 * 3. Keyboard should never report all-up (0x00) in
    367 		 *    a packet with size > 1.
    368 		 */
    369 		if (DT_CTL_LEN(msg->ctl) >= 6 &&
    370 		    msg->body[0] == 0 && msg->src != dt_ms_addr) {
    371 			dt_kbd_addr = dt_ms_addr;
    372 			dt_ms_addr = msg->src;
    373 		} else if (DT_CTL_LEN(msg->ctl) < 6 && msg->body[0] != 0 &&
    374 		    msg->src != dt_kbd_addr) {
    375 			dt_ms_addr = dt_kbd_addr;
    376 			dt_kbd_addr = msg->src;
    377 		}
    378 
    379 		if (msg->src == dt_kbd_addr)
    380 			dtdv = &dt_kbd_dv;
    381 		else
    382 			dtdv = &dt_ms_dv;
    383 
    384 		if (dtdv->dtdv_handler != NULL)
    385 			(*dtdv->dtdv_handler)(dtdv->dtdv_dv, msg);
    386 	}
    387 }
    388 
    389 int
    390 dt_msg_get(struct dt_msg *msg, int intr)
    391 {
    392 	volatile u_int *poll, *data;
    393 	uint8_t c;
    394 	int max_polls;
    395 
    396 	poll = dt_state.ds_poll;
    397 	data = dt_state.ds_data;
    398 
    399 	/*
    400 	 * The interface does not hand us the first byte, which is our
    401 	 * address and cannot ever be anything else but 0x50.
    402 	 */
    403 	if (dt_state.ds_state == 0) {
    404 		dt_state.ds_escaped = 0;
    405 		dt_state.ds_ptr = 0;
    406 	}
    407 
    408 	for (;;) {
    409 		max_polls = DT_MAX_POLL;
    410 
    411 		while (!DT_RX_AVAIL(poll)) {
    412 			if (intr)
    413 				return (DT_GET_NOTYET);
    414 			if (max_polls-- <= 0)
    415 				break;
    416 			DELAY(1);
    417 		}
    418 
    419 		if (max_polls <= 0) {
    420 			if (dt_state.ds_state != 0) {
    421 				dt_state.ds_bad_pkts++;
    422 				dt_state.ds_state = 0;
    423 			}
    424 			return (DT_GET_ERROR);
    425 		}
    426 
    427 		c = DT_GET_BYTE(data);
    428 
    429 		if (dt_state.ds_escaped) {
    430 			switch (c) {
    431 			case 0xe8:
    432 			case 0xe9:
    433 			case 0xea:
    434 			case 0xeb:
    435 				c += 0x10;
    436 				break;
    437 			}
    438 			if (c == 'O') {
    439 				dt_state.ds_bad_pkts++;
    440 				dt_state.ds_state = 0;
    441 				return (DT_GET_ERROR);
    442 			}
    443 			dt_state.ds_escaped = 0;
    444 		} else if (c == DT_ESC_CHAR) {
    445 			dt_state.ds_escaped = 1;
    446 			continue;
    447 		}
    448 
    449 		if (dt_state.ds_state == 0) {
    450 			msg->src = c;
    451 			dt_state.ds_state = 1;
    452 		} else if (dt_state.ds_state == 1) {
    453 			msg->ctl = c;
    454 			dt_state.ds_state = 2;
    455 			dt_state.ds_len = DT_CTL_LEN(msg->ctl) + 1;
    456 			if (dt_state.ds_len > sizeof(msg->body))
    457 				printf("dt_msg_get: msg truncated: %d\n",
    458 				    dt_state.ds_len);
    459 		} else /* if (dt_state.ds_state == 2) */ {
    460 			if (dt_state.ds_ptr < sizeof(msg->body))
    461 				msg->body[dt_state.ds_ptr++] = c;
    462 			if (dt_state.ds_ptr >= dt_state.ds_len)
    463 				break;
    464 		}
    465 	}
    466 
    467 	msg->dst = DT_ADDR_HOST;
    468 	dt_state.ds_state = 0;
    469 	return (DT_GET_DONE);
    470 }
    471 
    472 void
    473 dt_msg_dump(struct dt_msg *msg)
    474 {
    475 	int i, l;
    476 
    477 	l = DT_CTL_LEN(msg->ctl);
    478 
    479 	printf("hdr: dst=%02x src=%02x p=%02x sub=%02x len=%02x\n",
    480 	   msg->dst, msg->src, DT_CTL_P(msg->ctl), DT_CTL_SUBADDR(msg->ctl),
    481 	   l);
    482 
    483 	printf("body: ");
    484 	for (i = 0; i < l && i < 20; i++)
    485 		printf("%02x ", msg->body[i]);
    486 	if (i < l) {
    487 		printf("\n");
    488 		for (; i < l; i++)
    489 			printf("%02x ", msg->body[i]);
    490 	}
    491 	printf("\n");
    492 }
    493