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