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