Home | History | Annotate | Line # | Download | only in hil
hil.c revision 1.1
      1 /*	$NetBSD: hil.c,v 1.1 2011/02/06 18:26:54 tsutsui Exp $	*/
      2 /*	$OpenBSD: hil.c,v 1.24 2010/11/20 16:45:46 miod Exp $	*/
      3 /*
      4  * Copyright (c) 2003, 2004, Miodrag Vallat.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 /*
     31  * Copyright (c) 1988 University of Utah.
     32  * Copyright (c) 1990, 1993
     33  *	The Regents of the University of California.  All rights reserved.
     34  *
     35  * This code is derived from software contributed to Berkeley by
     36  * the Systems Programming Group of the University of Utah Computer
     37  * Science Department.
     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  * from: Utah $Hdr: hil.c 1.38 92/01/21$
     64  *
     65  *	@(#)hil.c	8.2 (Berkeley) 1/12/94
     66  */
     67 
     68 #include <sys/param.h>
     69 #include <sys/systm.h>
     70 #include <sys/conf.h>
     71 #include <sys/device.h>
     72 #include <sys/file.h>
     73 #include <sys/ioctl.h>
     74 #include <sys/kernel.h>
     75 #include <sys/proc.h>
     76 #include <sys/kthread.h>
     77 #include <sys/bus.h>
     78 #include <sys/cpu.h>
     79 
     80 #include <machine/autoconf.h>
     81 
     82 #include <dev/hil/hilreg.h>
     83 #include <dev/hil/hilvar.h>
     84 #include <dev/hil/hildevs.h>
     85 #include <dev/hil/hildevs_data.h>
     86 
     87 #include "hilkbd.h"
     88 
     89 void	hilconfig(struct hil_softc *, u_int);
     90 void	hilempty(struct hil_softc *);
     91 int	hilsubmatch(device_t, cfdata_t, const int *, void *);
     92 void	hil_process_int(struct hil_softc *, u_int8_t, u_int8_t);
     93 int	hil_process_poll(struct hil_softc *, u_int8_t, u_int8_t);
     94 void	hil_thread(void *);
     95 int	send_device_cmd(struct hil_softc *sc, u_int device, u_int cmd);
     96 void	polloff(struct hil_softc *);
     97 void	pollon(struct hil_softc *);
     98 
     99 static int hilwait(struct hil_softc *);
    100 static int hildatawait(struct hil_softc *);
    101 
    102 #define	hil_process_pending(sc)	wakeup(&(sc)->sc_pending)
    103 
    104 static __inline int
    105 hilwait(struct hil_softc *sc)
    106 {
    107 	int cnt;
    108 
    109 	for (cnt = 50000; cnt != 0; cnt--) {
    110 		DELAY(1);
    111 		if ((bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
    112 		    HIL_BUSY) == 0)
    113 			break;
    114 	}
    115 
    116 	return (cnt);
    117 }
    118 
    119 static __inline int
    120 hildatawait(struct hil_softc *sc)
    121 {
    122 	int cnt;
    123 
    124 	for (cnt = 50000; cnt != 0; cnt--) {
    125 		DELAY(1);
    126 		if ((bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
    127 		    HIL_DATA_RDY) != 0)
    128 			break;
    129 	}
    130 
    131 	return (cnt);
    132 }
    133 
    134 /*
    135  * Common HIL bus attachment
    136  */
    137 
    138 void
    139 hil_attach(struct hil_softc *sc, int *hil_is_console)
    140 {
    141 	printf("\n");
    142 
    143 	/*
    144 	 * Initialize loop information
    145 	 */
    146 	sc->sc_cmdending = 0;
    147 	sc->sc_actdev = sc->sc_cmddev = 0;
    148 	sc->sc_cmddone = 0;
    149 	sc->sc_cmdbp = sc->sc_cmdbuf;
    150 	sc->sc_pollbp = sc->sc_pollbuf;
    151 	sc->sc_console = hil_is_console;
    152 }
    153 
    154 /*
    155  * HIL subdevice attachment
    156  */
    157 
    158 int
    159 hildevprint(void *aux, const char *pnp)
    160 {
    161 	struct hil_attach_args *ha = aux;
    162 
    163 	if (pnp != NULL) {
    164 		printf("\"%s\" at %s id %x",
    165 		    ha->ha_descr, pnp, ha->ha_id);
    166 	}
    167 	printf(" code %d", ha->ha_code);
    168 	if (pnp == NULL) {
    169 		printf(": %s", ha->ha_descr);
    170 	}
    171 
    172 	return (UNCONF);
    173 }
    174 
    175 int
    176 hilsubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    177 {
    178 	struct hil_attach_args *ha = aux;
    179 
    180 	if (cf->cf_loc[0] != -1 &&
    181 	    cf->cf_loc[0] != ha->ha_code)
    182 		return (0);
    183 
    184 	return config_match(parent, cf, aux);
    185 }
    186 
    187 void
    188 hil_attach_deferred(device_t self)
    189 {
    190 	struct hil_softc *sc = device_private(self);
    191 	int tries;
    192 	u_int8_t db;
    193 
    194 	sc->sc_status = HIL_STATUS_BUSY;
    195 
    196 	/*
    197 	 * Initialize the loop: reconfigure, don't report errors,
    198 	 * put keyboard in cooked mode, and enable autopolling.
    199 	 */
    200 	db = LPC_RECONF | LPC_KBDCOOK | LPC_NOERROR | LPC_AUTOPOLL;
    201 	send_hil_cmd(sc, HIL_WRITELPCTRL, &db, 1, NULL);
    202 
    203 	/*
    204 	 * Delay one second for reconfiguration and then read the
    205 	 * data to clear the interrupt (if the loop reconfigured).
    206 	 */
    207 	DELAY(1000000);
    208 	if (bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
    209 	    HIL_DATA_RDY) {
    210 		db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
    211 		DELAY(1);
    212 	}
    213 
    214 	/*
    215 	 * The HIL loop may have reconfigured.  If so we proceed on,
    216 	 * if not we loop a few times until a successful reconfiguration
    217 	 * is reported back to us. If the HIL loop is still lost after a
    218 	 * few seconds, give up.
    219 	 */
    220 	for (tries = 10; tries != 0; tries--) {
    221 		if (send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db) == 0) {
    222 			if (db & (LPS_CONFFAIL | LPS_CONFGOOD))
    223 				break;
    224 		}
    225 
    226 #ifdef HILDEBUG
    227 		printf("%s: loop not ready, retrying...\n",
    228 		    device_xname(sc->sc_dev));
    229 #endif
    230 
    231 		DELAY(1000000);
    232         }
    233 
    234 	if (tries == 0 || (db & LPS_CONFFAIL)) {
    235 		printf("%s: no devices\n", device_xname(sc->sc_dev));
    236 		sc->sc_pending = 0;
    237 		if (tries == 0)
    238 			return;
    239 	}
    240 
    241 	/*
    242 	 * Create asynchronous loop event handler thread.
    243 	 */
    244 	if (kthread_create(PRI_NONE, 0, NULL, hil_thread, sc, &sc->sc_thread,
    245 	    "%s", device_xname(sc->sc_dev)) != 0) {
    246 		printf("%s: unable to create event thread\n",
    247 		    device_xname(sc->sc_dev));
    248 		return;
    249 	}
    250 
    251 	/*
    252 	 * Enable loop interrupts.
    253 	 */
    254 	send_hil_cmd(sc, HIL_INTON, NULL, 0, NULL);
    255 
    256 	/*
    257 	 * Reconfigure if necessary
    258 	 */
    259 	sc->sc_status = HIL_STATUS_READY;
    260 	hil_process_pending(sc);
    261 }
    262 
    263 /*
    264  * Asynchronous event processing
    265  */
    266 
    267 int
    268 hil_intr(void *v)
    269 {
    270 	struct hil_softc *sc = v;
    271 	u_int8_t c, stat;
    272 
    273 	if (cold)
    274 		return (0);
    275 
    276 	stat = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
    277 
    278 	/*
    279 	 * This should never happen if the interrupt comes from the
    280 	 * loop.
    281 	 */
    282 	if ((stat & HIL_DATA_RDY) == 0)
    283 		return (0);	/* not for us */
    284 
    285 	c = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    286 	    HILP_DATA);	/* clears interrupt */
    287 	DELAY(1);
    288 
    289 	hil_process_int(sc, stat, c);
    290 
    291 	if (sc->sc_status != HIL_STATUS_BUSY)
    292 		hil_process_pending(sc);
    293 
    294 	return (1);
    295 }
    296 
    297 void
    298 hil_process_int(struct hil_softc *sc, u_int8_t stat, u_int8_t c)
    299 {
    300 	device_t child;
    301 	struct hildev_softc *hdsc;
    302 
    303 	switch ((stat >> HIL_SSHIFT) & HIL_SMASK) {
    304 	case HIL_STATUS:
    305 		if (c & HIL_ERROR) {
    306 		  	sc->sc_cmddone = 1;
    307 			switch (c) {
    308 			case HIL_RECONFIG:
    309 				sc->sc_pending = HIL_PENDING_RECONFIG;
    310 				break;
    311 			case HIL_UNPLUGGED:
    312 				sc->sc_pending = HIL_PENDING_UNPLUGGED;
    313 				break;
    314 			}
    315 			break;
    316 		}
    317 		if (c & HIL_COMMAND) {
    318 		  	if (c & HIL_POLLDATA) {	/* End of data */
    319 				child = sc->sc_devices[sc->sc_actdev];
    320 				if (child != NULL) {
    321 					hdsc = device_private(child);
    322 					if (hdsc->sc_fn != NULL)
    323 						(*hdsc->sc_fn)(hdsc,
    324 						    sc->sc_pollbp
    325 						    - sc->sc_pollbuf,
    326 						    sc->sc_pollbuf);
    327 				}
    328 			} else {		/* End of command */
    329 			  	sc->sc_cmdending = 1;
    330 			}
    331 			sc->sc_actdev = 0;
    332 		} else {
    333 		  	if (c & HIL_POLLDATA) {	/* Start of polled data */
    334 				sc->sc_actdev = (c & HIL_DEVMASK);
    335 				sc->sc_pollbp = sc->sc_pollbuf;
    336 			} else {		/* Start of command */
    337 				if (sc->sc_cmddev == (c & HIL_DEVMASK)) {
    338 					sc->sc_cmdbp = sc->sc_cmdbuf;
    339 					sc->sc_actdev = 0;
    340 				}
    341 			}
    342 		}
    343 	        break;
    344 	case HIL_DATA:
    345 		if (sc->sc_actdev != 0)	/* Collecting poll data */
    346 			*sc->sc_pollbp++ = c;
    347 		else {
    348 			if (sc->sc_cmddev != 0) {  /* Collecting cmd data */
    349 				if (sc->sc_cmdending) {
    350 					sc->sc_cmddone = 1;
    351 					sc->sc_cmdending = 0;
    352 				} else
    353 					*sc->sc_cmdbp++ = c;
    354 		        }
    355 		}
    356 		break;
    357 	}
    358 }
    359 
    360 /*
    361  * Same as above, but in polled mode: return data as it gets seen, instead
    362  * of buffering it.
    363  */
    364 int
    365 hil_process_poll(struct hil_softc *sc, u_int8_t stat, u_int8_t c)
    366 {
    367 	u_int8_t db;
    368 
    369 	switch ((stat >> HIL_SSHIFT) & HIL_SMASK) {
    370 	case HIL_STATUS:
    371 		if (c & HIL_ERROR) {
    372 		  	sc->sc_cmddone = 1;
    373 			switch (c) {
    374 			case HIL_RECONFIG:
    375 				/*
    376 				 * Remember that a configuration event
    377 				 * occurred; it will be processed upon
    378 				 * leaving polled mode...
    379 				 */
    380 				sc->sc_pending = HIL_PENDING_RECONFIG;
    381 				/*
    382 				 * However, the keyboard will come back as
    383 				 * cooked, and we rely on it being in raw
    384 				 * mode. So, put it back in raw mode right
    385 				 * now.
    386 				 */
    387 				db = 0;
    388 				send_hil_cmd(sc, HIL_WRITEKBDSADR, &db,
    389 				    1, NULL);
    390 				break;
    391 			case HIL_UNPLUGGED:
    392 				/*
    393 				 * Remember that an unplugged event
    394 				 * occured; it will be processed upon
    395 				 * leaving polled mode...
    396 				 */
    397 				sc->sc_pending = HIL_PENDING_UNPLUGGED;
    398 				break;
    399 			}
    400 			break;
    401 		}
    402 		if (c & HIL_COMMAND) {
    403 		  	if (!(c & HIL_POLLDATA)) {
    404 				/* End of command */
    405 			  	sc->sc_cmdending = 1;
    406 			}
    407 			sc->sc_actdev = 0;
    408 		} else {
    409 		  	if (c & HIL_POLLDATA) {
    410 				/* Start of polled data */
    411 				sc->sc_actdev = (c & HIL_DEVMASK);
    412 				sc->sc_pollbp = sc->sc_pollbuf;
    413 			} else {
    414 				/* Start of command - should not happen */
    415 				if (sc->sc_cmddev == (c & HIL_DEVMASK)) {
    416 					sc->sc_cmdbp = sc->sc_cmdbuf;
    417 					sc->sc_actdev = 0;
    418 				}
    419 			}
    420 		}
    421 	        break;
    422 	case HIL_DATA:
    423 		if (sc->sc_actdev != 0)	/* Collecting poll data */
    424 			return 1;
    425 		else {
    426 			if (sc->sc_cmddev != 0) {  /* Discarding cmd data */
    427 				if (sc->sc_cmdending) {
    428 					sc->sc_cmddone = 1;
    429 					sc->sc_cmdending = 0;
    430 				}
    431 		        }
    432 		}
    433 		break;
    434 	}
    435 
    436 	return 0;
    437 }
    438 
    439 void
    440 hil_thread(void *arg)
    441 {
    442 	struct hil_softc *sc = arg;
    443 	int s;
    444 
    445 	for (;;) {
    446 		s = splhil();
    447 		if (sc->sc_pending == 0) {
    448 			splx(s);
    449 			(void)tsleep(&sc->sc_pending, PWAIT, "hil_event", 0);
    450 			continue;
    451 		}
    452 
    453 		switch (sc->sc_pending) {
    454 		case HIL_PENDING_RECONFIG:
    455 			sc->sc_pending = 0;
    456 			hilconfig(sc, sc->sc_maxdev);
    457 			break;
    458 		case HIL_PENDING_UNPLUGGED:
    459 			sc->sc_pending = 0;
    460 			hilempty(sc);
    461 			break;
    462 		}
    463 		splx(s);
    464 	}
    465 }
    466 
    467 /*
    468  * Called after the loop has reconfigured.  Here we need to:
    469  *	- determine how many devices are on the loop
    470  *	  (some may have been added or removed)
    471  *	- make sure all keyboards are in raw mode
    472  *
    473  * Note that our device state is now potentially invalid as
    474  * devices may no longer be where they were.  What we should
    475  * do here is either track where the devices went and move
    476  * state around accordingly...
    477  *
    478  * Note that it is necessary that we operate the loop with the keyboards
    479  * in raw mode: they won't cause the loop to generate an NMI if the
    480  * ``reset'' key combination is pressed, and we do not handle the hil
    481  * NMI interrupt...
    482  */
    483 void
    484 hilconfig(struct hil_softc *sc, u_int knowndevs)
    485 {
    486 	struct hil_attach_args ha;
    487 	u_int8_t db;
    488 	int id, s;
    489 
    490 	s = splhil();
    491 
    492 	/*
    493 	 * Determine how many devices are on the loop.
    494 	 */
    495 	db = 0;
    496 	send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db);
    497 	sc->sc_maxdev = db & LPS_DEVMASK;
    498 #ifdef HILDEBUG
    499 	printf("%s: %d device(s)\n", device_xname(sc->sc_dev), sc->sc_maxdev);
    500 #endif
    501 
    502 	/*
    503 	 * Put all keyboards in raw mode now.
    504 	 */
    505 	db = 0;
    506 	send_hil_cmd(sc, HIL_WRITEKBDSADR, &db, 1, NULL);
    507 
    508 	/*
    509 	 * If the loop grew, attach new devices.
    510 	 */
    511 	for (id = knowndevs + 1; id <= sc->sc_maxdev; id++) {
    512 		int len;
    513 		const struct hildevice *hd;
    514 
    515 		if (send_device_cmd(sc, id, HIL_IDENTIFY) != 0) {
    516 			printf("%s: no answer from device %d\n",
    517 			    device_xname(sc->sc_dev), id);
    518 			continue;
    519 		}
    520 
    521 		len = sc->sc_cmdbp - sc->sc_cmdbuf;
    522 		if (len == 0) {
    523 #ifdef HILDEBUG
    524 			printf("%s: no device at code %d\n",
    525 			    device_xname(sc->sc_dev), id);
    526 #endif
    527 			continue;
    528 		}
    529 
    530 		/* Identify and attach device */
    531 		for (hd = hildevs; hd->minid >= 0; hd++)
    532 			if (sc->sc_cmdbuf[0] >= hd->minid &&
    533 			    sc->sc_cmdbuf[0] <= hd->maxid) {
    534 
    535 			ha.ha_console = *sc->sc_console;
    536 			ha.ha_code = id;
    537 			ha.ha_type = hd->type;
    538 			ha.ha_descr = hd->descr;
    539 			ha.ha_infolen = len;
    540 			memcpy(ha.ha_info, sc->sc_cmdbuf, len);
    541 
    542 			sc->sc_devices[id] =
    543 			    config_found_sm_loc(sc->sc_dev, "hil", NULL,
    544 			    &ha, hildevprint, hilsubmatch);
    545 
    546 #if NHILKBD > 0
    547 			/*
    548 			 * If we just attached a keyboard as console,
    549 			 * console choice is not indeterminate anymore.
    550 			 */
    551 			if (sc->sc_devices[id] != NULL &&
    552 			    ha.ha_type == HIL_DEVICE_KEYBOARD &&
    553 			    ha.ha_console != 0)
    554 				*sc->sc_console = 1;
    555 #endif
    556 		}
    557 	}
    558 
    559 	/*
    560 	 * Detach remaining devices, if the loop has shrunk.
    561 	 */
    562 	for (id = sc->sc_maxdev + 1; id < NHILD; id++) {
    563 		if (sc->sc_devices[id] != NULL)
    564 			config_detach(sc->sc_devices[id],
    565 			    DETACH_FORCE);
    566 		sc->sc_devices[id] = NULL;
    567 	}
    568 
    569 	sc->sc_cmdbp = sc->sc_cmdbuf;
    570 
    571 	splx(s);
    572 }
    573 
    574 /*
    575  * Called after the loop has been unplugged. We simply force detach of
    576  * all our children.
    577  */
    578 void
    579 hilempty(struct hil_softc *sc)
    580 {
    581 	u_int8_t db;
    582 	int id, s;
    583 	u_int oldmaxdev;
    584 
    585 	s = splhil();
    586 
    587 	/*
    588 	 * Wait for the loop to be stable.
    589 	 */
    590 	for (;;) {
    591 		if (send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db) == 0) {
    592 			if (db & (LPS_CONFFAIL | LPS_CONFGOOD))
    593 				break;
    594 		} else {
    595 			db = LPS_CONFFAIL;
    596 			break;
    597 		}
    598 	}
    599 
    600 	if (db & LPS_CONFFAIL) {
    601 		sc->sc_maxdev = 0;
    602 	} else {
    603 		db = 0;
    604 		send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db);
    605 		oldmaxdev = sc->sc_maxdev;
    606 		sc->sc_maxdev = db & LPS_DEVMASK;
    607 
    608 		if (sc->sc_maxdev != 0) {
    609 			/*
    610 			 * The loop was not unplugged after all, but its
    611 			 * configuration has changed.
    612 			 */
    613 			hilconfig(sc, oldmaxdev);
    614 			return;
    615 		}
    616 	}
    617 
    618 	/*
    619 	 * Now detach all hil devices.
    620 	 */
    621 	for (id = sc->sc_maxdev + 1; id < NHILD; id++) {
    622 		if (sc->sc_devices[id] != NULL)
    623 			config_detach(sc->sc_devices[id],
    624 			    DETACH_FORCE);
    625 		sc->sc_devices[id] = NULL;
    626 	}
    627 
    628 	sc->sc_cmdbp = sc->sc_cmdbuf;
    629 
    630 	splx(s);
    631 }
    632 
    633 /*
    634  * Low level routines which actually talk to the 8042 chip.
    635  */
    636 
    637 /*
    638  * Send a command to the 8042 with zero or more bytes of data.
    639  * If rdata is non-null, wait for and return a byte of data.
    640  */
    641 int
    642 send_hil_cmd(struct hil_softc *sc, u_int cmd, u_int8_t *data, u_int dlen,
    643     u_int8_t *rdata)
    644 {
    645 	u_int8_t status;
    646 	int s;
    647 
    648 	s = splhil();
    649 
    650 	if (hilwait(sc) == 0) {
    651 #ifdef HILDEBUG
    652 		printf("%s: no answer from the loop\n",
    653 		    device_xname(sc->sc_dev));
    654 #endif
    655 		splx(s);
    656 		return (EBUSY);
    657 	}
    658 
    659 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, cmd);
    660 	while (dlen--) {
    661 	  	hilwait(sc);
    662 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, *data++);
    663 		DELAY(1);
    664 	}
    665 	if (rdata) {
    666 		do {
    667 			if (hildatawait(sc) == 0) {
    668 #ifdef HILDEBUG
    669 				printf("%s: no answer from the loop\n",
    670 				    device_xname(sc->sc_dev));
    671 #endif
    672 				break;
    673 			}
    674 			status = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    675 			    HILP_STAT);
    676 			*rdata = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    677 			    HILP_DATA);
    678 			DELAY(1);
    679 		} while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
    680 	}
    681 	splx(s);
    682 	return (0);
    683 }
    684 
    685 /*
    686  * Send a command to a device on the loop.
    687  * Since only one command can be active on the loop at any time,
    688  * we must ensure that we are not interrupted during this process.
    689  * Hence we mask interrupts to prevent potential access from most
    690  * interrupt routines and turn off auto-polling to disable the
    691  * internally generated poll commands.
    692  * Needs to be called at splhil().
    693  */
    694 int
    695 send_device_cmd(struct hil_softc *sc, u_int device, u_int cmd)
    696 {
    697 	u_int8_t status, c;
    698 	int rc = 0;
    699 
    700 	polloff(sc);
    701 
    702 	sc->sc_cmdbp = sc->sc_cmdbuf;
    703 	sc->sc_cmddev = device;
    704 
    705 	if (hilwait(sc) == 0) {
    706 #ifdef HILDEBUG
    707 		printf("%s: no answer from device %d\n",
    708 		    device_xname(sc->sc_dev), device);
    709 #endif
    710 		rc = EBUSY;
    711 		goto out;
    712 	}
    713 
    714 	/*
    715 	 * Transfer the command and device info to the chip
    716 	 */
    717 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_STARTCMD);
    718   	hilwait(sc);
    719 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 8 + device);
    720   	hilwait(sc);
    721 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, cmd);
    722   	hilwait(sc);
    723 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, HIL_TIMEOUT);
    724 
    725 	/*
    726 	 * Trigger the command and wait for completion
    727 	 */
    728 	hilwait(sc);
    729 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_TRIGGER);
    730 	sc->sc_cmddone = 0;
    731 	do {
    732 		if (hildatawait(sc) == 0) {
    733 #ifdef HILDEBUG
    734 			printf("%s: no answer from device %d\n",
    735 			    device_xname(sc->sc_dev), device);
    736 #endif
    737 			rc = EBUSY;
    738 			break;
    739 		}
    740 		status = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
    741 		c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
    742 		DELAY(1);
    743 		hil_process_int(sc, status, c);
    744 	} while (sc->sc_cmddone == 0);
    745 out:
    746 	sc->sc_cmddev = 0;
    747 
    748 	pollon(sc);
    749 	return (rc);
    750 }
    751 
    752 int
    753 send_hildev_cmd(struct hildev_softc *hdsc, u_int cmd,
    754     u_int8_t *outbuf, u_int *outlen)
    755 {
    756 	struct hil_softc *sc = device_private(device_parent(hdsc->sc_dev));
    757 	int s, rc;
    758 
    759 	s = splhil();
    760 
    761 	if ((rc = send_device_cmd(sc, hdsc->sc_code, cmd)) == 0) {
    762 		/*
    763 		 * Return the command response in the buffer if necessary
    764 	 	*/
    765 		if (outbuf != NULL && outlen != NULL) {
    766 			*outlen = min(*outlen, sc->sc_cmdbp - sc->sc_cmdbuf);
    767 			memcpy(outbuf, sc->sc_cmdbuf, *outlen);
    768 		}
    769 	}
    770 
    771 	splx(s);
    772 	return (rc);
    773 }
    774 
    775 /*
    776  * Turn auto-polling off and on.
    777  */
    778 void
    779 polloff(struct hil_softc *sc)
    780 {
    781 	u_int8_t db;
    782 
    783 	if (hilwait(sc) == 0)
    784 		return;
    785 
    786 	/*
    787 	 * Turn off auto repeat
    788 	 */
    789 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_SETARR);
    790 	hilwait(sc);
    791 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 0);
    792 
    793 	/*
    794 	 * Turn off auto-polling
    795 	 */
    796 	hilwait(sc);
    797 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READLPCTRL);
    798 	hildatawait(sc);
    799 	db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
    800 	db &= ~LPC_AUTOPOLL;
    801 	hilwait(sc);
    802 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_WRITELPCTRL);
    803 	hilwait(sc);
    804 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, db);
    805 
    806 	/*
    807 	 * Must wait until polling is really stopped
    808 	 */
    809 	do {
    810 		hilwait(sc);
    811 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READBUSY);
    812 		hildatawait(sc);
    813 		db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
    814 	} while (db & BSY_LOOPBUSY);
    815 
    816 	sc->sc_cmddone = 0;
    817 	sc->sc_cmddev = 0;
    818 }
    819 
    820 void
    821 pollon(struct hil_softc *sc)
    822 {
    823 	u_int8_t db;
    824 
    825 	if (hilwait(sc) == 0)
    826 		return;
    827 
    828 	/*
    829 	 * Turn on auto polling
    830 	 */
    831 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READLPCTRL);
    832 	hildatawait(sc);
    833 	db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
    834 	db |= LPC_AUTOPOLL;
    835 	hilwait(sc);
    836 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_WRITELPCTRL);
    837 	hilwait(sc);
    838 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, db);
    839 
    840 	/*
    841 	 * Turn off auto repeat - we emulate this through wscons
    842 	 */
    843 	hilwait(sc);
    844 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_SETARR);
    845 	hilwait(sc);
    846 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 0);
    847 	DELAY(1);
    848 }
    849 
    850 void
    851 hil_set_poll(struct hil_softc *sc, int on)
    852 {
    853 	if (on) {
    854 		pollon(sc);
    855 	} else {
    856 		hil_process_pending(sc);
    857 		send_hil_cmd(sc, HIL_INTON, NULL, 0, NULL);
    858 	}
    859 }
    860 
    861 int
    862 hil_poll_data(struct hildev_softc *hdsc, u_int8_t *stat, u_int8_t *data)
    863 {
    864 	struct hil_softc *sc = device_private(device_parent(hdsc->sc_dev));
    865 	u_int8_t s, c;
    866 
    867 	s = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
    868 	if ((s & HIL_DATA_RDY) == 0)
    869 		return -1;
    870 
    871 	c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
    872 	DELAY(1);
    873 
    874 	if (hil_process_poll(sc, s, c)) {
    875 		/* Discard any data not for us */
    876 		if (sc->sc_actdev == hdsc->sc_code) {
    877 			*stat = s;
    878 			*data = c;
    879 			return 0;
    880 		}
    881 	}
    882 
    883 	return -1;
    884 }
    885