BSD 4_4 release
[unix-history] / usr / src / sys / hp / dev / hil.c
index d53cc34..4fd4441 100644 (file)
@@ -1,45 +1,80 @@
 /*
  * Copyright (c) 1988 University of Utah.
 /*
  * Copyright (c) 1988 University of Utah.
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1990, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * the Systems Programming Group of the University of Utah Computer
  * Science Department.
  *
  *
  * This code is derived from software contributed to Berkeley by
  * the Systems Programming Group of the University of Utah Computer
  * Science Department.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- * from: Utah $Hdr: hil.c 1.33 89/12/22$
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  *
  *
- *     @(#)hil.c       7.6 (Berkeley) %G%
+ * from: Utah $Hdr: hil.c 1.38 92/01/21$
+ *
+ *     @(#)hil.c       8.1 (Berkeley) 6/10/93
  */
 
  */
 
-#include "sys/param.h"
-#include "sys/conf.h"
-#include "sys/user.h"
-#include "sys/proc.h"
-#include "sys/ioctl.h"
-#include "sys/file.h"
-#include "sys/tty.h"
-#include "sys/systm.h"
-#include "sys/uio.h"
-#include "sys/kernel.h"
-
-#include "hilreg.h"
-#include "hilioctl.h"
-#include "hilvar.h"
-#include "kbdmap.h"
-
-#include "../include/cpu.h"
-
-#include "vm/vm_param.h"
-#include "vm/vm_map.h"
-#include "vm/vm_kern.h"
-#include "vm/vm_page.h"
-#include "vm/vm_pager.h"
-
-struct hilloop hil0;
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/proc.h>
+#include <sys/user.h>
+#include <sys/ioctl.h>
+#include <sys/file.h>
+#include <sys/tty.h>
+#include <sys/systm.h>
+#include <sys/uio.h>
+#include <sys/kernel.h>
+
+#include <hp/dev/hilreg.h>
+#include <hp/dev/hilioctl.h>
+#include <hp/dev/hilvar.h>
+#include <hp/dev/kbdmap.h>
+
+#include <machine/cpu.h>
+
+#include <vm/vm_param.h>
+#include <vm/vm_map.h>
+#include <vm/vm_kern.h>
+#include <vm/vm_page.h>
+#include <vm/vm_pager.h>
+
+#ifdef hp300
+#define NHIL   1       /* XXX */
+#else
+#include "hil.h"
+#endif
+
+struct  hilloop hilloop[NHIL];
 struct _hilbell default_bell = { BELLDUR, BELLFREQ };
 struct _hilbell default_bell = { BELLDUR, BELLFREQ };
+#ifdef hp800
+int    hilspl;
+#endif
 
 #ifdef DEBUG
 int    hildebug = 0;
 
 #ifdef DEBUG
 int    hildebug = 0;
@@ -55,15 +90,21 @@ int         hildebug = 0;
 /* symbolic sleep message strings */
 char hilin[] = "hilin";
 
 /* symbolic sleep message strings */
 char hilin[] = "hilin";
 
-hilinit()
+hilsoftinit(unit, hilbase)
+       int unit;
+       struct hil_dev *hilbase;
 {
 {
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       register struct hilloop *hilp = &hilloop[unit];
        register int i;
 
        register int i;
 
+#ifdef DEBUG
+       if (hildebug & HDB_FOLLOW)
+               printf("hilsoftinit(%d, %x)\n", unit, hilbase);
+#endif
        /*
         * Initialize loop information
         */
        /*
         * Initialize loop information
         */
-       hilp->hl_addr = HILADDR;
+       hilp->hl_addr = hilbase;
        hilp->hl_cmdending = FALSE;
        hilp->hl_actdev = hilp->hl_cmddev = 0;
        hilp->hl_cmddone = FALSE;
        hilp->hl_cmdending = FALSE;
        hilp->hl_actdev = hilp->hl_cmddev = 0;
        hilp->hl_cmddone = FALSE;
@@ -83,25 +124,45 @@ hilinit()
        for (i = 0; i < NHILD; i++)
                hilp->hl_device[i].hd_qmask = 0;
        hilp->hl_device[HILLOOPDEV].hd_flags = (HIL_ALIVE|HIL_PSEUDO);
        for (i = 0; i < NHILD; i++)
                hilp->hl_device[i].hd_qmask = 0;
        hilp->hl_device[HILLOOPDEV].hd_flags = (HIL_ALIVE|HIL_PSEUDO);
+}
+
+hilinit(unit, hilbase)
+       int unit;
+       struct hil_dev *hilbase;
+{
+       register struct hilloop *hilp = &hilloop[unit];
+#ifdef DEBUG
+       if (hildebug & HDB_FOLLOW)
+               printf("hilinit(%d, %x)\n", unit, hilbase);
+#endif
+       /*
+        * Initialize software (if not already done).
+        */
+       if ((hilp->hl_device[HILLOOPDEV].hd_flags & HIL_ALIVE) == 0)
+               hilsoftinit(unit, hilbase);
        /*
        /*
+        * Initialize hardware.
         * Reset the loop hardware, and collect keyboard/id info
         */
        hilreset(hilp);
         * Reset the loop hardware, and collect keyboard/id info
         */
        hilreset(hilp);
-       hilinfo(hilp);
-       kbdenable();
+       hilinfo(unit);
+       kbdenable(unit);
 }
 
 }
 
-hilopen(dev, flags)
+/* ARGSUSED */
+hilopen(dev, flags, mode, p)
        dev_t dev;
        dev_t dev;
+       int flags, mode;
+       struct proc *p;
 {
 {
-       struct proc *p = u.u_procp;             /* XXX */
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       register struct hilloop *hilp = &hilloop[HILLOOP(dev)];
        register struct hilloopdev *dptr;
        u_char device = HILUNIT(dev);
 
 #ifdef DEBUG
        if (hildebug & HDB_FOLLOW)
        register struct hilloopdev *dptr;
        u_char device = HILUNIT(dev);
 
 #ifdef DEBUG
        if (hildebug & HDB_FOLLOW)
-               printf("hilopen(%d): device %x\n", p->p_pid, device);
+               printf("hilopen(%d): loop %x device %x\n",
+                      p->p_pid, HILLOOP(dev), device);
 #endif
        
        if ((hilp->hl_device[HILLOOPDEV].hd_flags & HIL_ALIVE) == 0)
 #endif
        
        if ((hilp->hl_device[HILLOOPDEV].hd_flags & HIL_ALIVE) == 0)
@@ -125,7 +186,7 @@ hilopen(dev, flags)
         * 3.   BSD processes default to shared queue interface.
         *      Multiple processes can open the device.
         */
         * 3.   BSD processes default to shared queue interface.
         *      Multiple processes can open the device.
         */
-       if (p->p_flag & SHPUX) {
+       if (p->p_md.md_flags & MDP_HPUX) {
                if (dptr->hd_flags & (HIL_READIN|HIL_QUEUEIN))
                        return(EBUSY);
                dptr->hd_flags |= HIL_READIN;
                if (dptr->hd_flags & (HIL_READIN|HIL_QUEUEIN))
                        return(EBUSY);
                dptr->hd_flags |= HIL_READIN;
@@ -134,7 +195,7 @@ hilopen(dev, flags)
                        return(EBUSY);
                dptr->hd_flags |= HIL_QUEUEIN;
        }
                        return(EBUSY);
                dptr->hd_flags |= HIL_QUEUEIN;
        }
-       if (flags & FNDELAY)
+       if (flags & FNONBLOCK)
                dptr->hd_flags |= HIL_NOBLOCK;
        /*
         * It is safe to flush the read buffer as we are guarenteed
                dptr->hd_flags |= HIL_NOBLOCK;
        /*
         * It is safe to flush the read buffer as we are guarenteed
@@ -161,11 +222,11 @@ hilopen(dev, flags)
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
-hilclose(dev, flags)
+hilclose(dev, flags, mode, p)
        dev_t dev;
        dev_t dev;
+       struct proc *p;
 {
 {
-       struct proc *p = u.u_procp;             /* XXX */
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       register struct hilloop *hilp = &hilloop[HILLOOP(dev)];
        register struct hilloopdev *dptr;
        register int i;
        u_char device = HILUNIT(dev);
        register struct hilloopdev *dptr;
        register int i;
        u_char device = HILUNIT(dev);
@@ -180,7 +241,7 @@ hilclose(dev, flags)
        if (device && (dptr->hd_flags & HIL_PSEUDO))
                return (0);
 
        if (device && (dptr->hd_flags & HIL_PSEUDO))
                return (0);
 
-       if ((p->p_flag & SHPUX) == 0) {
+       if (p && (p->p_md.md_flags & MDP_HPUX) == 0) {
                /*
                 * If this is the loop device,
                 * free up all queues belonging to this process.
                /*
                 * If this is the loop device,
                 * free up all queues belonging to this process.
@@ -188,7 +249,7 @@ hilclose(dev, flags)
                if (device == 0) {
                        for (i = 0; i < NHILQ; i++)
                                if (hilp->hl_queue[i].hq_procp == p)
                if (device == 0) {
                        for (i = 0; i < NHILQ; i++)
                                if (hilp->hl_queue[i].hq_procp == p)
-                                       (void) hilqfree(i);
+                                       (void) hilqfree(hilp, i);
                } else {
                        mask = ~hildevmask(device);
                        (void) splhil();
                } else {
                        mask = ~hildevmask(device);
                        (void) splhil();
@@ -232,7 +293,7 @@ hilclose(dev, flags)
                        printf("hilclose: keyboard %d cooked\n",
                               hilp->hl_kbddev);
 #endif
                        printf("hilclose: keyboard %d cooked\n",
                               hilp->hl_kbddev);
 #endif
-               kbdenable();
+               kbdenable(HILLOOP(dev));
        }
        (void) spl0();
        return (0);
        }
        (void) spl0();
        return (0);
@@ -245,7 +306,7 @@ hilread(dev, uio)
        dev_t dev;
        register struct uio *uio;
 {
        dev_t dev;
        register struct uio *uio;
 {
-       struct hilloop *hilp = &hil0;           /* XXX */
+       struct hilloop *hilp = &hilloop[HILLOOP(dev)];
        register struct hilloopdev *dptr;
        register int cc;
        u_char device = HILUNIT(dev);
        register struct hilloopdev *dptr;
        register int cc;
        u_char device = HILUNIT(dev);
@@ -284,7 +345,7 @@ hilread(dev, uio)
        error = 0;
        while (uio->uio_resid > 0 && error == 0) {
                cc = hilq_to_b(&dptr->hd_queue, buf,
        error = 0;
        while (uio->uio_resid > 0 && error == 0) {
                cc = hilq_to_b(&dptr->hd_queue, buf,
-                              MIN(uio->uio_resid, HILBUFSIZE));
+                              min(uio->uio_resid, HILBUFSIZE));
                if (cc <= 0)
                        break;
                error = uiomove(buf, cc, uio);
                if (cc <= 0)
                        break;
                error = uiomove(buf, cc, uio);
@@ -292,12 +353,12 @@ hilread(dev, uio)
        return(error);
 }
 
        return(error);
 }
 
-hilioctl(dev, cmd, data, flag)
+hilioctl(dev, cmd, data, flag, p)
        dev_t dev;
        caddr_t data;
        dev_t dev;
        caddr_t data;
+       struct proc *p;
 {
 {
-       struct proc *p = u.u_procp;             /* XXX */
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       register struct hilloop *hilp = &hilloop[HILLOOP(dev)];
        char device = HILUNIT(dev);
        struct hilloopdev *dptr;
        register int i;
        char device = HILUNIT(dev);
        struct hilloopdev *dptr;
        register int i;
@@ -323,6 +384,7 @@ hilioctl(dev, cmd, data, flag)
                switch (cmd) {
                case HILIOCSC:
                case HILIOCID:
                switch (cmd) {
                case HILIOCSC:
                case HILIOCID:
+               case OHILIOCID:
                case HILIOCRN:
                case HILIOCRS:
                case HILIOCED:
                case HILIOCRN:
                case HILIOCRS:
                case HILIOCED:
@@ -345,7 +407,7 @@ hilioctl(dev, cmd, data, flag)
        }
 
 #ifdef HPUXCOMPAT
        }
 
 #ifdef HPUXCOMPAT
-       if (p->p_flag & SHPUX)
+       if (p->p_md.md_flags & MDP_HPUX)
                return(hpuxhilioctl(dev, cmd, data, flag));
 #endif
 
                return(hpuxhilioctl(dev, cmd, data, flag));
 #endif
 
@@ -362,6 +424,7 @@ hilioctl(dev, cmd, data, flag)
                send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
                break;
 
                send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
                break;
 
+       case OHILIOCRRT:
        case HILIOCRRT:
                /* Transfer the real time to the 8042 data buffer */
                send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
        case HILIOCRRT:
                /* Transfer the real time to the 8042 data buffer */
                send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
@@ -382,6 +445,7 @@ hilioctl(dev, cmd, data, flag)
                break;
 
        case HILIOCID:
                break;
 
        case HILIOCID:
+       case OHILIOCID:
        case HILIOCSC:
        case HILIOCRN:
        case HILIOCRS:
        case HILIOCSC:
        case HILIOCRN:
        case HILIOCRS:
@@ -424,19 +488,19 @@ hilioctl(dev, cmd, data, flag)
                break;
 
         case HILIOCALLOCQ:
                break;
 
         case HILIOCALLOCQ:
-               error = hilqalloc((struct hilqinfo *)data);
+               error = hilqalloc(hilp, (struct hilqinfo *)data);
                break;
 
         case HILIOCFREEQ:
                break;
 
         case HILIOCFREEQ:
-               error = hilqfree(((struct hilqinfo *)data)->qid);
+               error = hilqfree(hilp, ((struct hilqinfo *)data)->qid);
                break;
 
         case HILIOCMAPQ:
                break;
 
         case HILIOCMAPQ:
-               error = hilqmap(*(int *)data, device);
+               error = hilqmap(hilp, *(int *)data, device);
                break;
 
         case HILIOCUNMAPQ:
                break;
 
         case HILIOCUNMAPQ:
-               error = hilqunmap(*(int *)data, device);
+               error = hilqunmap(hilp, *(int *)data, device);
                break;
 
        case HILIOCHPUX:
                break;
 
        case HILIOCHPUX:
@@ -470,7 +534,7 @@ hpuxhilioctl(dev, cmd, data, flag)
        dev_t dev;
        caddr_t data;
 {
        dev_t dev;
        caddr_t data;
 {
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       register struct hilloop *hilp = &hilloop[HILLOOP(dev)];
        char device = HILUNIT(dev);
        struct hilloopdev *dptr;
        register int i;
        char device = HILUNIT(dev);
        struct hilloopdev *dptr;
        register int i;
@@ -559,7 +623,16 @@ hpuxhilioctl(dev, cmd, data, flag)
                break;
                
        case EFTSBI:
                break;
                
        case EFTSBI:
+#ifdef hp800
+               /* XXX big magic */
+               hold = 7 - (*(u_char *)data >> 5);
+               *(int *)data = 0x84069008 | (hold << 8);
+               send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL);
+               send_hil_cmd(hilp->hl_addr, 0xC4, NULL, 0, NULL);
+               break;
+#else
                hilbeep(hilp, (struct _hilbell *)data);
                hilbeep(hilp, (struct _hilbell *)data);
+#endif
                break;
 
        case FIONBIO:
                break;
 
        case FIONBIO:
@@ -582,47 +655,19 @@ hpuxhilioctl(dev, cmd, data, flag)
 }
 #endif
 
 }
 #endif
 
-/*
- * XXX: the mmap inteface for HIL devices should be rethought.
- * We used it only briefly in conjuntion with shared queues
- * (instead of HILIOCMAPQ ioctl).  Perhaps mmap()ing a device
- * should give a single queue per process.
- */
 /* ARGSUSED */
 hilmap(dev, off, prot)
        dev_t dev;
        register int off;
 {
 /* ARGSUSED */
 hilmap(dev, off, prot)
        dev_t dev;
        register int off;
 {
-#ifdef MMAP
-       struct proc *p = u.u_procp;             /* XXX */
-       register struct hilloop *hilp = &hil0;  /* XXX */
-       register struct hiliqueue *qp;
-       register int qnum;
-
-       /*
-        * Only allow mmap() on loop device
-        */
-       if (HILUNIT(dev) != 0 || off >= NHILQ*sizeof(HILQ))
-               return(-1);
-       /*
-        * Determine which queue we want based on the offset.
-        * Queue must belong to calling process.
-        */
-       qp = &hilp->hl_queue[off / sizeof(HILQ)];
-       if (qp->hq_procp != p)
-               return(-1);
-
-       off %= sizeof(HILQ);
-       return(kvtop((u_int)qp->hq_eventqueue + off) >> PGSHIFT);
-#endif
 }
 
 /*ARGSUSED*/
 }
 
 /*ARGSUSED*/
-hilselect(dev, rw)
+hilselect(dev, rw, p)
        dev_t dev;
        dev_t dev;
+       struct proc *p;
 {
 {
-       struct proc *p = u.u_procp;             /* XXX */
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       register struct hilloop *hilp = &hilloop[HILLOOP(dev)];
        register struct hilloopdev *dptr;
        register struct hiliqueue *qp;
        register int mask;
        register struct hilloopdev *dptr;
        register struct hiliqueue *qp;
        register int mask;
@@ -643,11 +688,7 @@ hilselect(dev, rw)
                        splx(s);
                        return (1);
                }
                        splx(s);
                        return (1);
                }
-               if (dptr->hd_selr &&
-                   dptr->hd_selr->p_wchan == (caddr_t)&selwait)
-                       dptr->hd_flags |= HIL_SELCOLL;
-               else
-                       dptr->hd_selr = p;
+               selrecord(p, &dptr->hd_selr);
                splx(s);
                return (0);
        }
                splx(s);
                return (0);
        }
@@ -681,32 +722,33 @@ hilselect(dev, rw)
                        return (1);
                }
 
                        return (1);
                }
 
-       if (dptr->hd_selr && dptr->hd_selr->p_wchan == (caddr_t)&selwait)
-               dptr->hd_flags |= HIL_SELCOLL;
-       else
-               dptr->hd_selr = p;
+       selrecord(p, &dptr->hd_selr);
        splx(s);
        return (0);
 }
 
        splx(s);
        return (0);
 }
 
-hilint()
+/*ARGSUSED*/
+hilint(unit)
 {
 {
-       struct hilloop *hilp = &hil0;           /* XXX */
+#ifdef hp300
+       struct hilloop *hilp = &hilloop[0]; /* XXX how do we know on 300? */
+#else
+       struct hilloop *hilp = &hilloop[unit];
+#endif
        register struct hil_dev *hildevice = hilp->hl_addr;
        u_char c, stat;
 
        register struct hil_dev *hildevice = hilp->hl_addr;
        u_char c, stat;
 
-       stat = hildevice->hil_stat;
-       c = hildevice->hil_data;                /* clears interrupt */
-       hil_process_int(stat, c);
+       stat = READHILSTAT(hildevice);
+       c = READHILDATA(hildevice);             /* clears interrupt */
+       hil_process_int(hilp, stat, c);
 }
 
 #include "ite.h"
 
 }
 
 #include "ite.h"
 
-hil_process_int(stat, c)
+hil_process_int(hilp, stat, c)
+       register struct hilloop *hilp;
        register u_char stat, c;
 {
        register u_char stat, c;
 {
-       register struct hilloop *hilp;
-
 #ifdef DEBUG
        if (hildebug & HDB_EVENTS)
                printf("hilint: %x %x\n", stat, c);
 #ifdef DEBUG
        if (hildebug & HDB_EVENTS)
                printf("hilint: %x %x\n", stat, c);
@@ -725,7 +767,6 @@ hil_process_int(stat, c)
 #endif
                
        case HIL_STATUS:                        /* The status info. */
 #endif
                
        case HIL_STATUS:                        /* The status info. */
-               hilp = &hil0;                   /* XXX */
                if (c & HIL_ERROR) {
                        hilp->hl_cmddone = TRUE;
                        if (c == HIL_RECONFIG)
                if (c & HIL_ERROR) {
                        hilp->hl_cmddone = TRUE;
                        if (c == HIL_RECONFIG)
@@ -754,7 +795,6 @@ hil_process_int(stat, c)
                return;
 
        case HIL_DATA:
                return;
 
        case HIL_DATA:
-               hilp = &hil0;                   /* XXX */
                if (hilp->hl_actdev != 0)       /* Collecting poll data */
                        *hilp->hl_pollbp++ = c;
                else if (hilp->hl_cmddev != 0)  /* Collecting cmd data */
                if (hilp->hl_actdev != 0)       /* Collecting poll data */
                        *hilp->hl_pollbp++ = c;
                else if (hilp->hl_cmddev != 0)  /* Collecting cmd data */
@@ -887,17 +927,9 @@ hilevent(hilp)
        /*
         * Wake up anyone selecting on this device or the loop itself
         */
        /*
         * Wake up anyone selecting on this device or the loop itself
         */
-       if (dptr->hd_selr) {
-               selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
-               dptr->hd_selr = NULL;
-               dptr->hd_flags &= ~HIL_SELCOLL;
-       }
+       selwakeup(&dptr->hd_selr);
        dptr = &hilp->hl_device[HILLOOPDEV];
        dptr = &hilp->hl_device[HILLOOPDEV];
-       if (dptr->hd_selr) {
-               selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
-               dptr->hd_selr = NULL;
-               dptr->hd_flags &= ~HIL_SELCOLL;
-       }
+       selwakeup(&dptr->hd_selr);
 }
 
 #undef HQFULL
 }
 
 #undef HQFULL
@@ -939,21 +971,18 @@ hpuxhilevent(hilp, dptr)
                dptr->hd_flags &= ~HIL_ASLEEP;
                wakeup((caddr_t)dptr);
        }
                dptr->hd_flags &= ~HIL_ASLEEP;
                wakeup((caddr_t)dptr);
        }
-       if (dptr->hd_selr) {
-               selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
-               dptr->hd_selr = NULL;
-               dptr->hd_flags &= ~HIL_SELCOLL;
-       }
+       selwakeup(&dptr->hd_selr);
 }
 
 /*
  * Shared queue manipulation routines
  */
 
 }
 
 /*
  * Shared queue manipulation routines
  */
 
-hilqalloc(qip)
+hilqalloc(hilp, qip)
+       register struct hilloop *hilp;
        struct hilqinfo *qip;
 {
        struct hilqinfo *qip;
 {
-       struct proc *p = u.u_procp;             /* XXX */
+       struct proc *p = curproc;               /* XXX */
 
 #ifdef DEBUG
        if (hildebug & HDB_FOLLOW)
 
 #ifdef DEBUG
        if (hildebug & HDB_FOLLOW)
@@ -962,10 +991,10 @@ hilqalloc(qip)
        return(EINVAL);
 }
 
        return(EINVAL);
 }
 
-hilqfree(qnum)
+hilqfree(hilp, qnum)
        register int qnum;
 {
        register int qnum;
 {
-       struct proc *p = u.u_procp;             /* XXX */
+       struct proc *p = curproc;               /* XXX */
 
 #ifdef DEBUG
        if (hildebug & HDB_FOLLOW)
 
 #ifdef DEBUG
        if (hildebug & HDB_FOLLOW)
@@ -974,11 +1003,11 @@ hilqfree(qnum)
        return(EINVAL);
 }
 
        return(EINVAL);
 }
 
-hilqmap(qnum, device)
+hilqmap(hilp, qnum, device)
+       register struct hilloop *hilp;
        register int qnum, device;
 {
        register int qnum, device;
 {
-       struct proc *p = u.u_procp;             /* XXX */
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       struct proc *p = curproc;               /* XXX */
        register struct hilloopdev *dptr = &hilp->hl_device[device];
        int s;
 
        register struct hilloopdev *dptr = &hilp->hl_device[device];
        int s;
 
@@ -991,12 +1020,13 @@ hilqmap(qnum, device)
                return(EINVAL);
        if ((dptr->hd_flags & HIL_QUEUEIN) == 0)
                return(EINVAL);
                return(EINVAL);
        if ((dptr->hd_flags & HIL_QUEUEIN) == 0)
                return(EINVAL);
-       if (dptr->hd_qmask && u.u_uid && u.u_uid != dptr->hd_uid)
+       if (dptr->hd_qmask && p->p_ucred->cr_uid &&
+           p->p_ucred->cr_uid != dptr->hd_uid)
                return(EPERM);
 
        hilp->hl_queue[qnum].hq_devmask |= hildevmask(device);
        if (dptr->hd_qmask == 0)
                return(EPERM);
 
        hilp->hl_queue[qnum].hq_devmask |= hildevmask(device);
        if (dptr->hd_qmask == 0)
-               dptr->hd_uid = u.u_uid;
+               dptr->hd_uid = p->p_ucred->cr_uid;
        s = splhil();
        dptr->hd_qmask |= hilqmask(qnum);
        splx(s);
        s = splhil();
        dptr->hd_qmask |= hilqmask(qnum);
        splx(s);
@@ -1009,11 +1039,11 @@ hilqmap(qnum, device)
        return(0);
 }
 
        return(0);
 }
 
-hilqunmap(qnum, device)
+hilqunmap(hilp, qnum, device)
+       register struct hilloop *hilp;
        register int qnum, device;
 {
        register int qnum, device;
 {
-       struct proc *p = u.u_procp;             /* XXX */
-       register struct hilloop *hilp = &hil0;  /* XXX */
+       struct proc *p = curproc;               /* XXX */
        int s;
 
 #ifdef DEBUG
        int s;
 
 #ifdef DEBUG
@@ -1038,95 +1068,24 @@ hilqunmap(qnum, device)
        return(0);
 }
 
        return(0);
 }
 
-#include "sys/clist.h"
-
-/*
- * This is just a copy of the virgin q_to_b routine with minor
- * optimizations for HIL use.  It is used for two reasons:
- * 1. If we have PAGE mode defined, the normal q_to_b processes
- *    chars one at a time and breaks on newlines.
- * 2. We don't have to raise the priority to spltty() for most
- *    of the clist manipulations.
- */
-hilq_to_b(q, cp, cc)
-       register struct clist *q;
-       register char *cp;
-{
-       register struct cblock *bp;
-       register int nc;
-       char *acp;
-       int s;
-       extern char cwaiting;
-
-       if (cc <= 0)
-               return (0);
-       s = splhil();
-       if (q->c_cc <= 0) {
-               q->c_cc = 0;
-               q->c_cf = q->c_cl = NULL;
-               splx(s);
-               return (0);
-       }
-       acp = cp;
-
-       while (cc) {
-               nc = sizeof (struct cblock) - ((int)q->c_cf & CROUND);
-               nc = MIN(nc, cc);
-               nc = MIN(nc, q->c_cc);
-               (void) bcopy(q->c_cf, cp, (unsigned)nc);
-               q->c_cf += nc;
-               q->c_cc -= nc;
-               cc -= nc;
-               cp += nc;
-               if (q->c_cc <= 0) {
-                       bp = (struct cblock *)(q->c_cf - 1);
-                       bp = (struct cblock *)((int)bp & ~CROUND);
-                       q->c_cf = q->c_cl = NULL;
-                       spltty();
-                       bp->c_next = cfreelist;
-                       cfreelist = bp;
-                       cfreecount += CBSIZE;
-                       if (cwaiting) {
-                               wakeup(&cwaiting);
-                               cwaiting = 0;
-                       }
-                       break;
-               }
-               if (((int)q->c_cf & CROUND) == 0) {
-                       bp = (struct cblock *)(q->c_cf);
-                       bp--;
-                       q->c_cf = bp->c_next->c_info;
-                       spltty();
-                       bp->c_next = cfreelist;
-                       cfreelist = bp;
-                       cfreecount += CBSIZE;
-                       if (cwaiting) {
-                               wakeup(&cwaiting);
-                               cwaiting = 0;
-                       }
-                       splhil();
-               }
-       }
-       splx(s);
-       return (cp-acp);
-}
-
 /*
  * Cooked keyboard functions for ite driver.
  * There is only one "cooked" ITE keyboard (the first keyboard found)
  * per loop.  There may be other keyboards, but they will always be "raw".
  */
 
 /*
  * Cooked keyboard functions for ite driver.
  * There is only one "cooked" ITE keyboard (the first keyboard found)
  * per loop.  There may be other keyboards, but they will always be "raw".
  */
 
-kbdbell()
+kbdbell(unit)
+       int unit;
 {
 {
-       struct hilloop *hilp = &hil0;           /* XXX */
+       struct hilloop *hilp = &hilloop[unit];
 
        hilbeep(hilp, &default_bell);
 }
 
 
        hilbeep(hilp, &default_bell);
 }
 
-kbdenable()
+kbdenable(unit)
+       int unit;
 {
 {
-       struct hilloop *hilp = &hil0;   /* XXX */
+       struct hilloop *hilp = &hilloop[unit];
        register struct hil_dev *hildevice = hilp->hl_addr;
        char db;
 
        register struct hil_dev *hildevice = hilp->hl_addr;
        char db;
 
@@ -1142,7 +1101,8 @@ kbdenable()
        send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
 }
 
        send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
 }
 
-kbddisable()
+kbddisable(unit)
+       int unit;
 {
 }
 
 {
 }
 
@@ -1151,18 +1111,18 @@ kbddisable()
  * Used by console getchar routine.  Could really screw up anybody
  * reading from the keyboard in the normal, interrupt driven fashion.
  */
  * Used by console getchar routine.  Could really screw up anybody
  * reading from the keyboard in the normal, interrupt driven fashion.
  */
-kbdgetc(statp)
-       int *statp;
+kbdgetc(unit, statp)
+       int unit, *statp;
 {
 {
-       struct hilloop *hilp = &hil0;           /* XXX */
+       struct hilloop *hilp = &hilloop[unit];
        register struct hil_dev *hildevice = hilp->hl_addr;
        register int c, stat;
        int s;
 
        s = splhil();
        register struct hil_dev *hildevice = hilp->hl_addr;
        register int c, stat;
        int s;
 
        s = splhil();
-       while (((stat = hildevice->hil_stat) & HIL_DATA_RDY) == 0)
+       while (((stat = READHILSTAT(hildevice)) & HIL_DATA_RDY) == 0)
                ;
                ;
-       c = hildevice->hil_data;
+       c = READHILDATA(hildevice);
        splx(s);
        *statp = stat;
        return(c);
        splx(s);
        *statp = stat;
        return(c);
@@ -1177,16 +1137,22 @@ kbdgetc(statp)
  * interrupt reoccuring.  Note that we issue the CNMT command twice.
  * This seems to be needed, once is not always enough!?!
  */
  * interrupt reoccuring.  Note that we issue the CNMT command twice.
  * This seems to be needed, once is not always enough!?!
  */
-kbdnmi()
+kbdnmi(unit)
+       int unit;
 {
 {
-       register struct hilloop *hilp = &hil0;          /* XXX */
-
+#ifdef hp300
+       struct hilloop *hilp = &hilloop[0]; /* XXX how do we know on 300? */
+#else
+       struct hilloop *hilp = &hilloop[unit];
+#endif
+#ifdef hp300
        if ((*KBDNMISTAT & KBDNMI) == 0)
                return(0);
        if ((*KBDNMISTAT & KBDNMI) == 0)
                return(0);
+#endif
        HILWAIT(hilp->hl_addr);
        HILWAIT(hilp->hl_addr);
-       hilp->hl_addr->hil_cmd = HIL_CNMT;
+       WRITEHILCMD(hilp->hl_addr, HIL_CNMT);
        HILWAIT(hilp->hl_addr);
        HILWAIT(hilp->hl_addr);
-       hilp->hl_addr->hil_cmd = HIL_CNMT;
+       WRITEHILCMD(hilp->hl_addr, HIL_CNMT);
        HILWAIT(hilp->hl_addr);
        return(1);
 }
        HILWAIT(hilp->hl_addr);
        return(1);
 }
@@ -1198,9 +1164,10 @@ kbdnmi()
 /*
  * Called at boot time to print out info about interesting devices
  */
 /*
  * Called at boot time to print out info about interesting devices
  */
-hilinfo(hilp)
-       register struct hilloop *hilp;
+hilinfo(unit)
+       int unit;
 {
 {
+       register struct hilloop *hilp = &hilloop[unit];
        register int id, len;
        register struct kbdmap *km;
 
        register int id, len;
        register struct kbdmap *km;
 
@@ -1283,6 +1250,10 @@ hilconfig(hilp)
        db = 0;
        send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
        hilp->hl_maxdev = db & LPS_DEVMASK;
        db = 0;
        send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
        hilp->hl_maxdev = db & LPS_DEVMASK;
+#ifdef DEBUG
+       if (hildebug & HDB_CONFIG)
+               printf("hilconfig: %d devices found\n", hilp->hl_maxdev);
+#endif
        for (db = 1; db < NHILD; db++) {
                if (db <= hilp->hl_maxdev)
                        hilp->hl_device[db].hd_flags |= HIL_ALIVE;
        for (db = 1; db < NHILD; db++) {
                if (db <= hilp->hl_maxdev)
                        hilp->hl_device[db].hd_flags |= HIL_ALIVE;
@@ -1363,6 +1334,10 @@ hilreset(hilp)
        register struct hil_dev *hildevice = hilp->hl_addr;
        u_char db;
 
        register struct hil_dev *hildevice = hilp->hl_addr;
        u_char db;
 
+#ifdef DEBUG
+       if (hildebug & HDB_FOLLOW)
+               printf("hilreset(%x)\n", hilp);
+#endif
        /*
         * Initialize the loop: reconfigure, don't report errors,
         * cook keyboards, and enable autopolling.
        /*
         * Initialize the loop: reconfigure, don't report errors,
         * cook keyboards, and enable autopolling.
@@ -1374,8 +1349,8 @@ hilreset(hilp)
         * data register to clear the interrupt (if the loop reconfigured).
         */
        DELAY(1000000);
         * data register to clear the interrupt (if the loop reconfigured).
         */
        DELAY(1000000);
-       if (hildevice->hil_stat & HIL_DATA_RDY)
-               db = hildevice->hil_data;
+       if (READHILSTAT(hildevice) & HIL_DATA_RDY)
+               db = READHILDATA(hildevice);
        /*
         * The HIL loop may have reconfigured.  If so we proceed on,
         * if not we loop until a successful reconfiguration is reported
        /*
         * The HIL loop may have reconfigured.  If so we proceed on,
         * if not we loop until a successful reconfiguration is reported
@@ -1414,7 +1389,8 @@ hiliddev(hilp)
 
 #ifdef DEBUG
        if (hildebug & HDB_IDMODULE)
 
 #ifdef DEBUG
        if (hildebug & HDB_IDMODULE)
-               printf("hiliddev(%x): looking for idmodule...", hilp);
+               printf("hiliddev(%x): max %d, looking for idmodule...",
+                      hilp, hilp->hl_maxdev);
 #endif
        for (i = 1; i <= hilp->hl_maxdev; i++) {
                hilp->hl_cmdbp = hilp->hl_cmdbuf;
 #endif
        for (i = 1; i <= hilp->hl_maxdev; i++) {
                hilp->hl_cmdbp = hilp->hl_cmdbuf;
@@ -1445,6 +1421,30 @@ hiliddev(hilp)
        return(i <= hilp->hl_maxdev ? i : 0);
 }
 
        return(i <= hilp->hl_maxdev ? i : 0);
 }
 
+#ifdef HPUXCOMPAT
+/*
+ * XXX map devno as expected by HP-UX
+ */
+hildevno(dev)
+       dev_t dev;
+{
+       int newdev;
+
+       newdev = 24 << 24;
+#ifdef HILCOMPAT
+       /*
+        * XXX compat check
+        * Don't convert old style specfiles already in correct format
+        */
+       if (minor(dev) && (dev & 0xF) == 0)
+               newdev |= minor(dev);
+       else
+#endif
+       newdev |= (HILLOOP(dev) << 8) | (HILUNIT(dev) << 4);
+       return(newdev);
+}
+#endif
+
 /*
  * Low level routines which actually talk to the 8042 chip.
  */
 /*
  * Low level routines which actually talk to the 8042 chip.
  */
@@ -1464,16 +1464,16 @@ send_hil_cmd(hildevice, cmd, data, dlen, rdata)
        int s = splimp();
 
        HILWAIT(hildevice);
        int s = splimp();
 
        HILWAIT(hildevice);
-       hildevice->hil_cmd = cmd;
+       WRITEHILCMD(hildevice, cmd);
        while (dlen--) {
                HILWAIT(hildevice);
        while (dlen--) {
                HILWAIT(hildevice);
-               hildevice->hil_data = *data++;
+               WRITEHILDATA(hildevice, *data++);
        }
        if (rdata) {
                do {
                        HILDATAWAIT(hildevice);
        }
        if (rdata) {
                do {
                        HILDATAWAIT(hildevice);
-                       status = hildevice->hil_stat;
-                       *rdata = hildevice->hil_data;
+                       status = READHILSTAT(hildevice);
+                       *rdata = READHILDATA(hildevice);
                } while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
        }
        splx(s);
                } while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
        }
        splx(s);
@@ -1504,24 +1504,24 @@ send_hildev_cmd(hilp, device, cmd)
         * Transfer the command and device info to the chip
         */
        HILWAIT(hildevice);
         * Transfer the command and device info to the chip
         */
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_STARTCMD;
+       WRITEHILCMD(hildevice, HIL_STARTCMD);
        HILWAIT(hildevice);
        HILWAIT(hildevice);
-       hildevice->hil_data = 8 + device;
+       WRITEHILDATA(hildevice, 8 + device);
        HILWAIT(hildevice);
        HILWAIT(hildevice);
-       hildevice->hil_data = cmd;
+       WRITEHILDATA(hildevice, cmd);
        HILWAIT(hildevice);
        HILWAIT(hildevice);
-       hildevice->hil_data = HIL_TIMEOUT;
+       WRITEHILDATA(hildevice, HIL_TIMEOUT);
        /*
         * Trigger the command and wait for completion
         */
        HILWAIT(hildevice);
        /*
         * Trigger the command and wait for completion
         */
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_TRIGGER;
+       WRITEHILCMD(hildevice, HIL_TRIGGER);
        hilp->hl_cmddone = FALSE;
        do {
                HILDATAWAIT(hildevice);
        hilp->hl_cmddone = FALSE;
        do {
                HILDATAWAIT(hildevice);
-               status = hildevice->hil_stat;
-               c = hildevice->hil_data;
-               hil_process_int(status, c);
+               status = READHILSTAT(hildevice);
+               c = READHILDATA(hildevice);
+               hil_process_int(hilp, status, c);
        } while (!hilp->hl_cmddone);
 
        pollon(hildevice);
        } while (!hilp->hl_cmddone);
 
        pollon(hildevice);
@@ -1541,29 +1541,29 @@ polloff(hildevice)
         * Turn off auto repeat
         */
        HILWAIT(hildevice);
         * Turn off auto repeat
         */
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_SETARR;
+       WRITEHILCMD(hildevice, HIL_SETARR);
        HILWAIT(hildevice);
        HILWAIT(hildevice);
-       hildevice->hil_data = 0;
+       WRITEHILDATA(hildevice, 0);
        /*
         * Turn off auto-polling
         */
        HILWAIT(hildevice);
        /*
         * Turn off auto-polling
         */
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_READLPCTRL;
+       WRITEHILCMD(hildevice, HIL_READLPCTRL);
        HILDATAWAIT(hildevice);
        HILDATAWAIT(hildevice);
-       db = hildevice->hil_data;
+       db = READHILDATA(hildevice);
        db &= ~LPC_AUTOPOLL;
        HILWAIT(hildevice);
        db &= ~LPC_AUTOPOLL;
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_WRITELPCTRL;
+       WRITEHILCMD(hildevice, HIL_WRITELPCTRL);
        HILWAIT(hildevice);
        HILWAIT(hildevice);
-       hildevice->hil_data = db;
+       WRITEHILDATA(hildevice, db);
        /*
         * Must wait til polling is really stopped
         */
        do {    
                HILWAIT(hildevice);
        /*
         * Must wait til polling is really stopped
         */
        do {    
                HILWAIT(hildevice);
-               hildevice->hil_cmd = HIL_READBUSY;
+               WRITEHILCMD(hildevice, HIL_READBUSY);
                HILDATAWAIT(hildevice);
                HILDATAWAIT(hildevice);
-               db = hildevice->hil_data;
+               db = READHILDATA(hildevice);
        } while (db & BSY_LOOPBUSY);
 }
 
        } while (db & BSY_LOOPBUSY);
 }
 
@@ -1576,21 +1576,21 @@ pollon(hildevice)
         * Turn on auto polling
         */
        HILWAIT(hildevice);
         * Turn on auto polling
         */
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_READLPCTRL;
+       WRITEHILCMD(hildevice, HIL_READLPCTRL);
        HILDATAWAIT(hildevice);
        HILDATAWAIT(hildevice);
-       db = hildevice->hil_data;
+       db = READHILDATA(hildevice);
        db |= LPC_AUTOPOLL;
        HILWAIT(hildevice);
        db |= LPC_AUTOPOLL;
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_WRITELPCTRL;
+       WRITEHILCMD(hildevice, HIL_WRITELPCTRL);
        HILWAIT(hildevice);
        HILWAIT(hildevice);
-       hildevice->hil_data = db;
+       WRITEHILDATA(hildevice, db);
        /*
         * Turn on auto repeat
         */
        HILWAIT(hildevice);
        /*
         * Turn on auto repeat
         */
        HILWAIT(hildevice);
-       hildevice->hil_cmd = HIL_SETARR;
+       WRITEHILCMD(hildevice, HIL_SETARR);
        HILWAIT(hildevice);
        HILWAIT(hildevice);
-       hildevice->hil_data = ar_format(KBD_ARR);
+       WRITEHILDATA(hildevice, ar_format(KBD_ARR));
 }
 
 #ifdef DEBUG
 }
 
 #ifdef DEBUG