xref: /csrg-svn/sys/hp/dev/hil.c (revision 42360)
141480Smckusick /*
241480Smckusick  * Copyright (c) 1988 University of Utah.
341480Smckusick  * Copyright (c) 1990 The Regents of the University of California.
441480Smckusick  * All rights reserved.
541480Smckusick  *
641480Smckusick  * This code is derived from software contributed to Berkeley by
741480Smckusick  * the Systems Programming Group of the University of Utah Computer
841480Smckusick  * Science Department.
941480Smckusick  *
1041480Smckusick  * %sccs.include.redist.c%
1141480Smckusick  *
1241480Smckusick  * from: Utah $Hdr: hil.c 1.33 89/12/22$
1341480Smckusick  *
14*42360Smckusick  *	@(#)hil.c	7.2 (Berkeley) 05/25/90
1541480Smckusick  */
1641480Smckusick 
1741480Smckusick #include "param.h"
1841480Smckusick #include "conf.h"
1941480Smckusick #include "user.h"
2041480Smckusick #include "proc.h"
2141480Smckusick #include "ioctl.h"
2241480Smckusick #include "file.h"
2341480Smckusick #include "tty.h"
2441480Smckusick #include "systm.h"
2541480Smckusick #include "uio.h"
2641480Smckusick #include "kernel.h"
2741480Smckusick #include "mapmem.h"
2841480Smckusick 
2941480Smckusick #include "hilreg.h"
3041480Smckusick #include "hilioctl.h"
3141480Smckusick #include "hilvar.h"
3241480Smckusick #include "kbdmap.h"
3341480Smckusick 
3441480Smckusick #include "machine/cpu.h"
3541480Smckusick 
3641480Smckusick struct	hilloop	hil0;
3741480Smckusick struct	_hilbell default_bell = { BELLDUR, BELLFREQ };
3841480Smckusick 
3941480Smckusick #ifdef MAPMEM
4041480Smckusick int	hilqfork(), hilqvfork(), hilqexit();
4141480Smckusick struct	mapmemops hilqops = { hilqfork, hilqvfork, hilqexit, hilqexit };
4241480Smckusick #endif
4341480Smckusick 
4441480Smckusick #ifdef DEBUG
4541480Smckusick int 	hildebug = 0;
4641480Smckusick #define HDB_FOLLOW	0x01
4741480Smckusick #define HDB_MMAP	0x02
4841480Smckusick #define HDB_MASK	0x04
4941480Smckusick #define HDB_CONFIG	0x08
5041480Smckusick #define HDB_KEYBOARD	0x10
5141480Smckusick #define HDB_IDMODULE	0x20
5241480Smckusick #define HDB_EVENTS	0x80
5341480Smckusick #endif
5441480Smckusick 
55*42360Smckusick /* symbolic sleep message strings */
56*42360Smckusick char hilin[] = "hilin";
57*42360Smckusick 
5841480Smckusick hilinit()
5941480Smckusick {
6041480Smckusick   	register struct hilloop *hilp = &hil0;	/* XXX */
6141480Smckusick 	register int i;
6241480Smckusick 
6341480Smckusick 	/*
6441480Smckusick 	 * Initialize loop information
6541480Smckusick 	 */
6641480Smckusick 	hilp->hl_addr = HILADDR;
6741480Smckusick 	hilp->hl_cmdending = FALSE;
6841480Smckusick 	hilp->hl_actdev = hilp->hl_cmddev = 0;
6941480Smckusick 	hilp->hl_cmddone = FALSE;
7041480Smckusick 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
7141480Smckusick 	hilp->hl_pollbp = hilp->hl_pollbuf;
7241480Smckusick 	hilp->hl_kbddev = 0;
7341480Smckusick 	hilp->hl_kbdlang = KBD_DEFAULT;
7441480Smckusick 	hilp->hl_kbdflags = 0;
7541480Smckusick 	/*
7641480Smckusick 	 * Clear all queues and device associations with queues
7741480Smckusick 	 */
7841480Smckusick 	for (i = 0; i < NHILQ; i++) {
7941480Smckusick 		hilp->hl_queue[i].hq_eventqueue = NULL;
8041480Smckusick 		hilp->hl_queue[i].hq_procp = NULL;
8141480Smckusick 		hilp->hl_queue[i].hq_devmask = 0;
8241480Smckusick 	}
8341480Smckusick 	for (i = 0; i < NHILD; i++)
8441480Smckusick 		hilp->hl_device[i].hd_qmask = 0;
8541480Smckusick 	hilp->hl_device[HILLOOPDEV].hd_flags = (HIL_ALIVE|HIL_PSEUDO);
8641480Smckusick 	/*
8741480Smckusick 	 * Reset the loop hardware, and collect keyboard/id info
8841480Smckusick 	 */
8941480Smckusick 	hilreset(hilp);
9041480Smckusick 	hilinfo(hilp);
9141480Smckusick 	kbdenable();
9241480Smckusick }
9341480Smckusick 
9441480Smckusick hilopen(dev, flags)
9541480Smckusick 	dev_t dev;
9641480Smckusick {
9741480Smckusick   	register struct hilloop *hilp = &hil0;	/* XXX */
9841480Smckusick 	register struct hilloopdev *dptr;
9941480Smckusick 	u_char device = HILUNIT(dev);
10041480Smckusick 
10141480Smckusick #ifdef DEBUG
10241480Smckusick 	if (hildebug & HDB_FOLLOW)
10341480Smckusick 		printf("hilopen(%d): device %x\n", u.u_procp->p_pid, device);
10441480Smckusick #endif
10541480Smckusick 
10641480Smckusick 	if ((hilp->hl_device[HILLOOPDEV].hd_flags & HIL_ALIVE) == 0)
10741480Smckusick 		return(ENXIO);
10841480Smckusick 
10941480Smckusick 	dptr = &hilp->hl_device[device];
11041480Smckusick 	if ((dptr->hd_flags & HIL_ALIVE) == 0)
11141480Smckusick 		return(ENODEV);
11241480Smckusick 
11341480Smckusick 	/*
11441480Smckusick 	 * Pseudo-devices cannot be read, nothing more to do.
11541480Smckusick 	 */
11641480Smckusick 	if (dptr->hd_flags & HIL_PSEUDO)
11741480Smckusick 		return(0);
11841480Smckusick 
11941480Smckusick 	/*
12041480Smckusick 	 * Open semantics:
12141480Smckusick 	 * 1.	Open devices have only one of HIL_READIN/HIL_QUEUEIN.
12241480Smckusick 	 * 2.	HPUX processes always get read syscall interface and
12341480Smckusick 	 *	must have exclusive use of the device.
12441480Smckusick 	 * 3.	BSD processes default to shared queue interface.
12541480Smckusick 	 *	Multiple processes can open the device.
12641480Smckusick 	 */
12741480Smckusick 	if (u.u_procp->p_flag & SHPUX) {
12841480Smckusick 		if (dptr->hd_flags & (HIL_READIN|HIL_QUEUEIN))
12941480Smckusick 			return(EBUSY);
13041480Smckusick 		dptr->hd_flags |= HIL_READIN;
13141480Smckusick 	} else {
13241480Smckusick 		if (dptr->hd_flags & HIL_READIN)
13341480Smckusick 			return(EBUSY);
13441480Smckusick 		dptr->hd_flags |= HIL_QUEUEIN;
13541480Smckusick 	}
13641480Smckusick 	if (flags & FNDELAY)
13741480Smckusick 		dptr->hd_flags |= HIL_NOBLOCK;
13841480Smckusick 	/*
13941480Smckusick 	 * It is safe to flush the read buffer as we are guarenteed
14041480Smckusick 	 * that no one else is using it.
14141480Smckusick 	 */
14241480Smckusick 	ndflush(&dptr->hd_queue, dptr->hd_queue.c_cc);
14341480Smckusick 
14441480Smckusick 	send_hil_cmd(hilp->hl_addr, HIL_INTON, NULL, 0, NULL);
14541480Smckusick 	/*
14641480Smckusick 	 * Opened the keyboard, put in raw mode.
14741480Smckusick 	 */
14841480Smckusick 	(void) splhil();
14941480Smckusick 	if (device == hilp->hl_kbddev) {
15041480Smckusick 		u_char mask = 0;
15141480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &mask, 1, NULL);
15241480Smckusick 		hilp->hl_kbdflags |= KBD_RAW;
15341480Smckusick #ifdef DEBUG
15441480Smckusick 		if (hildebug & HDB_KEYBOARD)
15541480Smckusick 			printf("hilopen: keyboard %d raw\n", hilp->hl_kbddev);
15641480Smckusick #endif
15741480Smckusick 	}
15841480Smckusick 	(void) spl0();
15941480Smckusick 	return (0);
16041480Smckusick }
16141480Smckusick 
16241480Smckusick /* ARGSUSED */
16341480Smckusick hilclose(dev, flags)
16441480Smckusick 	dev_t dev;
16541480Smckusick {
16641480Smckusick   	register struct hilloop *hilp = &hil0;	/* XXX */
16741480Smckusick 	register struct hilloopdev *dptr;
16841480Smckusick 	register int i;
16941480Smckusick 	u_char device = HILUNIT(dev);
17041480Smckusick 	char mask, lpctrl;
17141480Smckusick 
17241480Smckusick #ifdef DEBUG
17341480Smckusick 	if (hildebug & HDB_FOLLOW)
17441480Smckusick 		printf("hilclose(%d): device %x\n", u.u_procp->p_pid, device);
17541480Smckusick #endif
17641480Smckusick 
17741480Smckusick 	dptr = &hilp->hl_device[device];
17841480Smckusick 	if (device && (dptr->hd_flags & HIL_PSEUDO))
17941480Smckusick 		return (0);
18041480Smckusick 
18141480Smckusick 	if ((u.u_procp->p_flag & SHPUX) == 0) {
18241480Smckusick 		/*
18341480Smckusick 		 * If this is the loop device,
18441480Smckusick 		 * free up all queues belonging to this process.
18541480Smckusick 		 */
18641480Smckusick 		if (device == 0) {
18741480Smckusick 			for (i = 0; i < NHILQ; i++)
18841480Smckusick 				if (hilp->hl_queue[i].hq_procp == u.u_procp)
18941480Smckusick 					(void) hilqfree(i);
19041480Smckusick 		} else {
19141480Smckusick 			mask = ~hildevmask(device);
19241480Smckusick 			(void) splhil();
19341480Smckusick 			for (i = 0; i < NHILQ; i++)
19441480Smckusick 				if (hilp->hl_queue[i].hq_procp == u.u_procp) {
19541480Smckusick 					dptr->hd_qmask &= ~hilqmask(i);
19641480Smckusick 					hilp->hl_queue[i].hq_devmask &= mask;
19741480Smckusick 				}
19841480Smckusick 			(void) spl0();
19941480Smckusick 		}
20041480Smckusick 	}
20141480Smckusick 	/*
20241480Smckusick 	 * Always flush the read buffer
20341480Smckusick 	 */
20441480Smckusick 	dptr->hd_flags &= ~(HIL_QUEUEIN|HIL_READIN|HIL_NOBLOCK);
20541480Smckusick 	ndflush(&dptr->hd_queue, dptr->hd_queue.c_cc);
20641480Smckusick 	/*
20741480Smckusick 	 * Set keyboard back to cooked mode when closed.
20841480Smckusick 	 */
20941480Smckusick 	(void) splhil();
21041480Smckusick 	if (device && device == hilp->hl_kbddev) {
21141480Smckusick 		mask = 1 << (hilp->hl_kbddev - 1);
21241480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &mask, 1, NULL);
21341480Smckusick 		hilp->hl_kbdflags &= ~(KBD_RAW|KBD_AR1|KBD_AR2);
21441480Smckusick 		/*
21541480Smckusick 		 * XXX: We have had trouble with keyboards remaining raw
21641480Smckusick 		 * after close due to the LPC_KBDCOOK bit getting cleared
21741480Smckusick 		 * somewhere along the line.  Hence we check and reset
21841480Smckusick 		 * LPCTRL if necessary.
21941480Smckusick 		 */
22041480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_READLPCTRL, NULL, 0, &lpctrl);
22141480Smckusick 		if ((lpctrl & LPC_KBDCOOK) == 0) {
22241480Smckusick 			printf("hilclose: bad LPCTRL %x, reset to %x\n",
22341480Smckusick 			       lpctrl, lpctrl|LPC_KBDCOOK);
22441480Smckusick 			lpctrl |= LPC_KBDCOOK;
22541480Smckusick 			send_hil_cmd(hilp->hl_addr, HIL_WRITELPCTRL,
22641480Smckusick 					&lpctrl, 1, NULL);
22741480Smckusick 		}
22841480Smckusick #ifdef DEBUG
22941480Smckusick 		if (hildebug & HDB_KEYBOARD)
23041480Smckusick 			printf("hilclose: keyboard %d cooked\n",
23141480Smckusick 			       hilp->hl_kbddev);
23241480Smckusick #endif
23341480Smckusick 		kbdenable();
23441480Smckusick 	}
23541480Smckusick 	(void) spl0();
23641480Smckusick 	return (0);
23741480Smckusick }
23841480Smckusick 
23941480Smckusick /*
24041480Smckusick  * Read interface to HIL device.
24141480Smckusick  */
24241480Smckusick hilread(dev, uio)
24341480Smckusick 	dev_t dev;
24441480Smckusick 	register struct uio *uio;
24541480Smckusick {
24641480Smckusick 	struct hilloop *hilp = &hil0;		/* XXX */
24741480Smckusick 	register struct hilloopdev *dptr;
24841480Smckusick 	register int cc;
24941480Smckusick 	u_char device = HILUNIT(dev);
25041480Smckusick 	char buf[HILBUFSIZE];
25141480Smckusick 	int error;
25241480Smckusick 
25341480Smckusick #if 0
25441480Smckusick 	/*
25541480Smckusick 	 * XXX: Don't do this since HP-UX doesn't.
25641480Smckusick 	 *
25741480Smckusick 	 * Check device number.
25841480Smckusick 	 * This check is necessary since loop can reconfigure.
25941480Smckusick 	 */
26041480Smckusick 	if (device > hilp->hl_maxdev)
26141480Smckusick 		return(ENODEV);
26241480Smckusick #endif
26341480Smckusick 
26441480Smckusick 	dptr = &hilp->hl_device[device];
26541480Smckusick 	if ((dptr->hd_flags & HIL_READIN) == 0)
26641480Smckusick 		return(ENODEV);
26741480Smckusick 
26841480Smckusick 	(void) splhil();
26941480Smckusick 	while (dptr->hd_queue.c_cc == 0) {
27041480Smckusick 		if (dptr->hd_flags & HIL_NOBLOCK) {
27141480Smckusick 			spl0();
27241480Smckusick 			return(EWOULDBLOCK);
27341480Smckusick 		}
27441480Smckusick 		dptr->hd_flags |= HIL_ASLEEP;
275*42360Smckusick 		if (error = tsleep((caddr_t)dptr, TTIPRI | PCATCH, hilin, 0)) {
276*42360Smckusick 			(void) spl0();
277*42360Smckusick 			return (error);
278*42360Smckusick 		}
27941480Smckusick 	}
28041480Smckusick 	(void) spl0();
28141480Smckusick 
28241480Smckusick 	error = 0;
28341480Smckusick 	while (uio->uio_resid > 0 && error == 0) {
28441480Smckusick 		cc = hilq_to_b(&dptr->hd_queue, buf,
28541480Smckusick 			       MIN(uio->uio_resid, HILBUFSIZE));
28641480Smckusick 		if (cc <= 0)
28741480Smckusick 			break;
28841480Smckusick 		error = uiomove(buf, cc, uio);
28941480Smckusick 	}
29041480Smckusick 	return(error);
29141480Smckusick }
29241480Smckusick 
29341480Smckusick hilioctl(dev, cmd, data, flag)
29441480Smckusick 	dev_t dev;
29541480Smckusick 	caddr_t data;
29641480Smckusick {
29741480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
29841480Smckusick 	char device = HILUNIT(dev);
29941480Smckusick 	struct hilloopdev *dptr;
30041480Smckusick 	register int i;
30141480Smckusick 	u_char hold;
30241480Smckusick 	int error;
30341480Smckusick 
30441480Smckusick #ifdef DEBUG
30541480Smckusick 	if (hildebug & HDB_FOLLOW)
30641480Smckusick 		printf("hilioctl(%d): dev %x cmd %x\n",
30741480Smckusick 		       u.u_procp->p_pid, device, cmd);
30841480Smckusick #endif
30941480Smckusick 
31041480Smckusick 	dptr = &hilp->hl_device[device];
31141480Smckusick 	if ((dptr->hd_flags & HIL_ALIVE) == 0)
31241480Smckusick 		return (ENODEV);
31341480Smckusick 
31441480Smckusick 	/*
31541480Smckusick 	 * Don't allow hardware ioctls on virtual devices.
31641480Smckusick 	 * Note that though these are the BSD names, they have the same
31741480Smckusick 	 * values as the HP-UX equivalents so we catch them as well.
31841480Smckusick 	 */
31941480Smckusick 	if (dptr->hd_flags & HIL_PSEUDO) {
32041480Smckusick 		switch (cmd) {
32141480Smckusick 		case HILIOCSC:
32241480Smckusick 		case HILIOCID:
32341480Smckusick 		case HILIOCRN:
32441480Smckusick 		case HILIOCRS:
32541480Smckusick 		case HILIOCED:
32641480Smckusick 			return(ENODEV);
32741480Smckusick 
32841480Smckusick 		/*
32941480Smckusick 		 * XXX: should also return ENODEV but HP-UX compat
33041480Smckusick 		 * breaks if we do.  They work ok right now because
33141480Smckusick 		 * we only recognize one keyboard on the loop.  This
33241480Smckusick 		 * will have to change if we remove that restriction.
33341480Smckusick 		 */
33441480Smckusick 		case HILIOCAROFF:
33541480Smckusick 		case HILIOCAR1:
33641480Smckusick 		case HILIOCAR2:
33741480Smckusick 			break;
33841480Smckusick 
33941480Smckusick 		default:
34041480Smckusick 			break;
34141480Smckusick 		}
34241480Smckusick 	}
34341480Smckusick 
34441480Smckusick #ifdef HPUXCOMPAT
34541480Smckusick 	if (u.u_procp->p_flag & SHPUX)
34641480Smckusick 		return(hpuxhilioctl(dev, cmd, data, flag));
34741480Smckusick #endif
34841480Smckusick 
34941480Smckusick 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
35041480Smckusick 	bzero((caddr_t)hilp->hl_cmdbuf, HILBUFSIZE);
35141480Smckusick 	hilp->hl_cmddev = device;
35241480Smckusick 	error = 0;
35341480Smckusick 	switch (cmd) {
35441480Smckusick 
35541480Smckusick 	case HILIOCSBP:
35641480Smckusick 		/* Send four data bytes to the tone gererator. */
35741480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL);
35841480Smckusick 		/* Send the trigger beeper command to the 8042. */
35941480Smckusick 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
36041480Smckusick 		break;
36141480Smckusick 
36241480Smckusick 	case HILIOCRRT:
36341480Smckusick 		/* Transfer the real time to the 8042 data buffer */
36441480Smckusick 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
36541480Smckusick 		/* Read each byte of the real time */
36641480Smckusick 		for (i = 0; i < 5; i++) {
36741480Smckusick 			send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL,
36841480Smckusick 					0, &hold);
36941480Smckusick 			data[4-i] = hold;
37041480Smckusick 		}
37141480Smckusick 		break;
37241480Smckusick 
37341480Smckusick 	case HILIOCRT:
37441480Smckusick 		for (i = 0; i < 4; i++) {
37541480Smckusick 			send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i,
37641480Smckusick 					NULL, 0, &hold);
37741480Smckusick 			data[i] = hold;
37841480Smckusick 		}
37941480Smckusick 		break;
38041480Smckusick 
38141480Smckusick 	case HILIOCID:
38241480Smckusick 	case HILIOCSC:
38341480Smckusick 	case HILIOCRN:
38441480Smckusick 	case HILIOCRS:
38541480Smckusick 	case HILIOCED:
38641480Smckusick 	  	send_hildev_cmd(hilp, device, (cmd & 0xFF));
38741480Smckusick 		bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf);
38841480Smckusick 	  	break;
38941480Smckusick 
39041480Smckusick         case HILIOCAROFF:
39141480Smckusick         case HILIOCAR1:
39241480Smckusick         case HILIOCAR2:
39341480Smckusick 		if (hilp->hl_kbddev) {
39441480Smckusick 			hilp->hl_cmddev = hilp->hl_kbddev;
39541480Smckusick 			send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF));
39641480Smckusick 			hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2);
39741480Smckusick 			if (cmd == HILIOCAR1)
39841480Smckusick 				hilp->hl_kbdflags |= KBD_AR1;
39941480Smckusick 			else if (cmd == HILIOCAR2)
40041480Smckusick 				hilp->hl_kbdflags |= KBD_AR2;
40141480Smckusick 		}
40241480Smckusick 		break;
40341480Smckusick 
40441480Smckusick 	case HILIOCBEEP:
40541480Smckusick 		hilbeep(hilp, (struct _hilbell *)data);
40641480Smckusick 		break;
40741480Smckusick 
40841480Smckusick 	case FIONBIO:
40941480Smckusick 		dptr = &hilp->hl_device[device];
41041480Smckusick 		if (*(int *)data)
41141480Smckusick 			dptr->hd_flags |= HIL_NOBLOCK;
41241480Smckusick 		else
41341480Smckusick 			dptr->hd_flags &= ~HIL_NOBLOCK;
41441480Smckusick 		break;
41541480Smckusick 
41641480Smckusick 	/*
41741480Smckusick 	 * FIOASYNC must be present for FIONBIO above to work!
41841480Smckusick 	 * (See fcntl in kern_descrip.c).
41941480Smckusick 	 */
42041480Smckusick 	case FIOASYNC:
42141480Smckusick 		break;
42241480Smckusick 
42341480Smckusick         case HILIOCALLOCQ:
42441480Smckusick 		error = hilqalloc((struct hilqinfo *)data);
42541480Smckusick 		break;
42641480Smckusick 
42741480Smckusick         case HILIOCFREEQ:
42841480Smckusick 		error = hilqfree(((struct hilqinfo *)data)->qid);
42941480Smckusick 		break;
43041480Smckusick 
43141480Smckusick         case HILIOCMAPQ:
43241480Smckusick 		error = hilqmap(*(int *)data, device);
43341480Smckusick 		break;
43441480Smckusick 
43541480Smckusick         case HILIOCUNMAPQ:
43641480Smckusick 		error = hilqunmap(*(int *)data, device);
43741480Smckusick 		break;
43841480Smckusick 
43941480Smckusick 	case HILIOCHPUX:
44041480Smckusick 		dptr = &hilp->hl_device[device];
44141480Smckusick 		dptr->hd_flags |= HIL_READIN;
44241480Smckusick 		dptr->hd_flags &= ~HIL_QUEUEIN;
44341480Smckusick 		break;
44441480Smckusick 
44541480Smckusick         case HILIOCRESET:
44641480Smckusick 	        hilreset(hilp);
44741480Smckusick 		break;
44841480Smckusick 
44941480Smckusick #ifdef DEBUG
45041480Smckusick         case HILIOCTEST:
45141480Smckusick 		hildebug = *(int *) data;
45241480Smckusick 		break;
45341480Smckusick #endif
45441480Smckusick 
45541480Smckusick         default:
45641480Smckusick 		error = EINVAL;
45741480Smckusick 		break;
45841480Smckusick 
45941480Smckusick 	}
46041480Smckusick 	hilp->hl_cmddev = 0;
46141480Smckusick 	return(error);
46241480Smckusick }
46341480Smckusick 
46441480Smckusick #ifdef HPUXCOMPAT
46541480Smckusick /* ARGSUSED */
46641480Smckusick hpuxhilioctl(dev, cmd, data, flag)
46741480Smckusick 	dev_t dev;
46841480Smckusick 	caddr_t data;
46941480Smckusick {
47041480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
47141480Smckusick 	char device = HILUNIT(dev);
47241480Smckusick 	struct hilloopdev *dptr;
47341480Smckusick 	register int i;
47441480Smckusick 	u_char hold;
47541480Smckusick 
47641480Smckusick 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
47741480Smckusick 	bzero((caddr_t)hilp->hl_cmdbuf, HILBUFSIZE);
47841480Smckusick 	hilp->hl_cmddev = device;
47941480Smckusick 	switch (cmd) {
48041480Smckusick 
48141480Smckusick 	case HILSC:
48241480Smckusick 	case HILID:
48341480Smckusick 	case HILRN:
48441480Smckusick 	case HILRS:
48541480Smckusick 	case HILED:
48641480Smckusick 	case HILP1:
48741480Smckusick 	case HILP2:
48841480Smckusick 	case HILP3:
48941480Smckusick 	case HILP4:
49041480Smckusick 	case HILP5:
49141480Smckusick 	case HILP6:
49241480Smckusick 	case HILP7:
49341480Smckusick 	case HILP:
49441480Smckusick 	case HILA1:
49541480Smckusick 	case HILA2:
49641480Smckusick 	case HILA3:
49741480Smckusick 	case HILA4:
49841480Smckusick 	case HILA5:
49941480Smckusick 	case HILA6:
50041480Smckusick 	case HILA7:
50141480Smckusick 	case HILA:
50241480Smckusick 		send_hildev_cmd(hilp, device, (cmd & 0xFF));
50341480Smckusick 		bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf);
50441480Smckusick 	  	break;
50541480Smckusick 
50641480Smckusick         case HILDKR:
50741480Smckusick         case HILER1:
50841480Smckusick         case HILER2:
50941480Smckusick 		if (hilp->hl_kbddev) {
51041480Smckusick 			hilp->hl_cmddev = hilp->hl_kbddev;
51141480Smckusick 			send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF));
51241480Smckusick 			hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2);
51341480Smckusick 			if (cmd == HILIOCAR1)
51441480Smckusick 				hilp->hl_kbdflags |= KBD_AR1;
51541480Smckusick 			else if (cmd == HILIOCAR2)
51641480Smckusick 				hilp->hl_kbdflags |= KBD_AR2;
51741480Smckusick 		}
51841480Smckusick 		break;
51941480Smckusick 
52041480Smckusick 	case EFTSBP:
52141480Smckusick 		/* Send four data bytes to the tone gererator. */
52241480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL);
52341480Smckusick 		/* Send the trigger beeper command to the 8042. */
52441480Smckusick 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
52541480Smckusick 		break;
52641480Smckusick 
52741480Smckusick 	case EFTRRT:
52841480Smckusick 		/* Transfer the real time to the 8042 data buffer */
52941480Smckusick 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
53041480Smckusick 		/* Read each byte of the real time */
53141480Smckusick 		for (i = 0; i < 5; i++) {
53241480Smckusick 			send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL,
53341480Smckusick 					0, &hold);
53441480Smckusick 			data[4-i] = hold;
53541480Smckusick 		}
53641480Smckusick 		break;
53741480Smckusick 
53841480Smckusick 	case EFTRT:
53941480Smckusick 		for (i = 0; i < 4; i++) {
54041480Smckusick 			send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i,
54141480Smckusick 					NULL, 0, &hold);
54241480Smckusick 			data[i] = hold;
54341480Smckusick 		}
54441480Smckusick 		break;
54541480Smckusick 
54641480Smckusick         case EFTRLC:
54741480Smckusick         case EFTRCC:
54841480Smckusick 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, &hold);
54941480Smckusick 		*data = hold;
55041480Smckusick 		break;
55141480Smckusick 
55241480Smckusick         case EFTSRPG:
55341480Smckusick         case EFTSRD:
55441480Smckusick         case EFTSRR:
55541480Smckusick 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), data, 1, NULL);
55641480Smckusick 		break;
55741480Smckusick 
55841480Smckusick 	case EFTSBI:
55941480Smckusick 		hilbeep(hilp, (struct _hilbell *)data);
56041480Smckusick 		break;
56141480Smckusick 
56241480Smckusick 	case FIONBIO:
56341480Smckusick 		dptr = &hilp->hl_device[device];
56441480Smckusick 		if (*(int *)data)
56541480Smckusick 			dptr->hd_flags |= HIL_NOBLOCK;
56641480Smckusick 		else
56741480Smckusick 			dptr->hd_flags &= ~HIL_NOBLOCK;
56841480Smckusick 		break;
56941480Smckusick 
57041480Smckusick 	case FIOASYNC:
57141480Smckusick 		break;
57241480Smckusick 
57341480Smckusick         default:
57441480Smckusick 		hilp->hl_cmddev = 0;
57541480Smckusick 		return(EINVAL);
57641480Smckusick 	}
57741480Smckusick 	hilp->hl_cmddev = 0;
57841480Smckusick 	return(0);
57941480Smckusick }
58041480Smckusick #endif
58141480Smckusick 
58241480Smckusick /*
58341480Smckusick  * XXX: the mmap inteface for HIL devices should be rethought.
58441480Smckusick  * We used it only briefly in conjuntion with shared queues
58541480Smckusick  * (instead of HILIOCMAPQ ioctl).  Perhaps mmap()ing a device
58641480Smckusick  * should give a single queue per process.
58741480Smckusick  */
58841480Smckusick /* ARGSUSED */
58941480Smckusick hilmap(dev, off, prot)
59041480Smckusick 	dev_t dev;
59141480Smckusick 	register int off;
59241480Smckusick {
59341480Smckusick #ifdef MMAP
59441480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
59541480Smckusick 	register struct hiliqueue *qp;
59641480Smckusick 	register int qnum;
59741480Smckusick 
59841480Smckusick 	/*
59941480Smckusick 	 * Only allow mmap() on loop device
60041480Smckusick 	 */
60141480Smckusick 	if (HILUNIT(dev) != 0 || off >= NHILQ*sizeof(HILQ))
60241480Smckusick 		return(-1);
60341480Smckusick 	/*
60441480Smckusick 	 * Determine which queue we want based on the offset.
60541480Smckusick 	 * Queue must belong to calling process.
60641480Smckusick 	 */
60741480Smckusick 	qp = &hilp->hl_queue[off / sizeof(HILQ)];
60841480Smckusick 	if (qp->hq_procp != u.u_procp)
60941480Smckusick 		return(-1);
61041480Smckusick 
61141480Smckusick 	off %= sizeof(HILQ);
61241480Smckusick 	return(kvtop((u_int)qp->hq_eventqueue + off) >> PGSHIFT);
61341480Smckusick #endif
61441480Smckusick }
61541480Smckusick 
61641480Smckusick /*ARGSUSED*/
61741480Smckusick hilselect(dev, rw)
61841480Smckusick 	dev_t dev;
61941480Smckusick {
62041480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
62141480Smckusick 	register struct hilloopdev *dptr;
62241480Smckusick 	register struct hiliqueue *qp;
62341480Smckusick 	register int mask;
62441480Smckusick 	int s, device;
62541480Smckusick 
62641480Smckusick 	if (rw == FWRITE)
62741480Smckusick 		return (1);
62841480Smckusick 	device = HILUNIT(dev);
62941480Smckusick 
63041480Smckusick 	/*
63141480Smckusick 	 * Read interface.
63241480Smckusick 	 * Return 1 if there is something in the queue, 0 ow.
63341480Smckusick 	 */
63441480Smckusick 	dptr = &hilp->hl_device[device];
63541480Smckusick 	if (dptr->hd_flags & HIL_READIN) {
63641480Smckusick 		s = splhil();
63741480Smckusick 		if (dptr->hd_queue.c_cc) {
63841480Smckusick 			splx(s);
63941480Smckusick 			return (1);
64041480Smckusick 		}
64141480Smckusick 		if (dptr->hd_selr &&
64241480Smckusick 		    dptr->hd_selr->p_wchan == (caddr_t)&selwait)
64341480Smckusick 			dptr->hd_flags |= HIL_SELCOLL;
64441480Smckusick 		else
64541480Smckusick 			dptr->hd_selr = u.u_procp;
64641480Smckusick 		splx(s);
64741480Smckusick 		return (0);
64841480Smckusick 	}
64941480Smckusick 
65041480Smckusick 	/*
65141480Smckusick 	 * Make sure device is alive and real (or the loop device).
65241480Smckusick 	 * Note that we do not do this for the read interface.
65341480Smckusick 	 * This is primarily to be consistant with HP-UX.
65441480Smckusick 	 */
65541480Smckusick 	if (device && (dptr->hd_flags & (HIL_ALIVE|HIL_PSEUDO)) != HIL_ALIVE)
65641480Smckusick 		return (1);
65741480Smckusick 
65841480Smckusick 	/*
65941480Smckusick 	 * Select on loop device is special.
66041480Smckusick 	 * Check to see if there are any data for any loop device
66141480Smckusick 	 * provided it is associated with a queue belonging to this user.
66241480Smckusick 	 */
66341480Smckusick 	if (device == 0)
66441480Smckusick 		mask = -1;
66541480Smckusick 	else
66641480Smckusick 		mask = hildevmask(device);
66741480Smckusick 	/*
66841480Smckusick 	 * Must check everybody with interrupts blocked to prevent races.
66941480Smckusick 	 */
67041480Smckusick 	s = splhil();
67141480Smckusick 	for (qp = hilp->hl_queue; qp < &hilp->hl_queue[NHILQ]; qp++)
67241480Smckusick 		if (qp->hq_procp == u.u_procp && (mask & qp->hq_devmask) &&
67341480Smckusick 		    qp->hq_eventqueue->hil_evqueue.head !=
67441480Smckusick 		    qp->hq_eventqueue->hil_evqueue.tail) {
67541480Smckusick 			splx(s);
67641480Smckusick 			return (1);
67741480Smckusick 		}
67841480Smckusick 
67941480Smckusick 	if (dptr->hd_selr && dptr->hd_selr->p_wchan == (caddr_t)&selwait)
68041480Smckusick 		dptr->hd_flags |= HIL_SELCOLL;
68141480Smckusick 	else
68241480Smckusick 		dptr->hd_selr = u.u_procp;
68341480Smckusick 	splx(s);
68441480Smckusick 	return (0);
68541480Smckusick }
68641480Smckusick 
68741480Smckusick hilint()
68841480Smckusick {
68941480Smckusick 	struct hilloop *hilp = &hil0;		/* XXX */
69041480Smckusick 	register struct hil_dev *hildevice = hilp->hl_addr;
69141480Smckusick 	u_char c, stat;
69241480Smckusick 
69341480Smckusick 	stat = hildevice->hil_stat;
69441480Smckusick 	c = hildevice->hil_data;		/* clears interrupt */
69541480Smckusick 	hil_process_int(stat, c);
69641480Smckusick }
69741480Smckusick 
69841480Smckusick #include "ite.h"
69941480Smckusick 
70041480Smckusick hil_process_int(stat, c)
70141480Smckusick 	register u_char stat, c;
70241480Smckusick {
70341480Smckusick   	register struct hilloop *hilp;
70441480Smckusick 
70541480Smckusick #ifdef DEBUG
70641480Smckusick 	if (hildebug & HDB_EVENTS)
70741480Smckusick 		printf("hilint: %x %x\n", stat, c);
70841480Smckusick #endif
70941480Smckusick 
71041480Smckusick 	/* the shift enables the compiler to generate a jump table */
71141480Smckusick 	switch ((stat>>HIL_SSHIFT) & HIL_SMASK) {
71241480Smckusick 
71341480Smckusick #if NITE > 0
71441480Smckusick 	case HIL_KEY:
71541480Smckusick 	case HIL_SHIFT:
71641480Smckusick 	case HIL_CTRL:
71741480Smckusick 	case HIL_CTRLSHIFT:
71841480Smckusick 		itefilter(stat, c);
71941480Smckusick 		return;
72041480Smckusick #endif
72141480Smckusick 
72241480Smckusick 	case HIL_STATUS:			/* The status info. */
72341480Smckusick 		hilp = &hil0;			/* XXX */
72441480Smckusick 		if (c & HIL_ERROR) {
72541480Smckusick 		  	hilp->hl_cmddone = TRUE;
72641480Smckusick 			if (c == HIL_RECONFIG)
72741480Smckusick 				hilconfig(hilp);
72841480Smckusick 			break;
72941480Smckusick 		}
73041480Smckusick 		if (c & HIL_COMMAND) {
73141480Smckusick 		  	if (c & HIL_POLLDATA)	/* End of data */
73241480Smckusick 				hilevent(hilp);
73341480Smckusick 			else			/* End of command */
73441480Smckusick 			  	hilp->hl_cmdending = TRUE;
73541480Smckusick 			hilp->hl_actdev = 0;
73641480Smckusick 		} else {
73741480Smckusick 		  	if (c & HIL_POLLDATA) {	/* Start of polled data */
73841480Smckusick 			  	if (hilp->hl_actdev != 0)
73941480Smckusick 					hilevent(hilp);
74041480Smckusick 				hilp->hl_actdev = (c & HIL_DEVMASK);
74141480Smckusick 				hilp->hl_pollbp = hilp->hl_pollbuf;
74241480Smckusick 			} else {		/* Start of command */
74341480Smckusick 				if (hilp->hl_cmddev == (c & HIL_DEVMASK)) {
74441480Smckusick 					hilp->hl_cmdbp = hilp->hl_cmdbuf;
74541480Smckusick 					hilp->hl_actdev = 0;
74641480Smckusick 				}
74741480Smckusick 			}
74841480Smckusick 		}
74941480Smckusick 	        return;
75041480Smckusick 
75141480Smckusick 	case HIL_DATA:
75241480Smckusick 		hilp = &hil0;			/* XXX */
75341480Smckusick 		if (hilp->hl_actdev != 0)	/* Collecting poll data */
75441480Smckusick 			*hilp->hl_pollbp++ = c;
75541480Smckusick 		else if (hilp->hl_cmddev != 0)  /* Collecting cmd data */
75641480Smckusick 			if (hilp->hl_cmdending) {
75741480Smckusick 				hilp->hl_cmddone = TRUE;
75841480Smckusick 				hilp->hl_cmdending = FALSE;
75941480Smckusick 			} else
76041480Smckusick 				*hilp->hl_cmdbp++ = c;
76141480Smckusick 		return;
76241480Smckusick 
76341480Smckusick 	case 0:		/* force full jump table */
76441480Smckusick 	default:
76541480Smckusick 		return;
76641480Smckusick 	}
76741480Smckusick }
76841480Smckusick 
76941480Smckusick #if defined(DEBUG) && !defined(PANICBUTTON)
77041480Smckusick #define PANICBUTTON
77141480Smckusick #endif
77241480Smckusick 
77341480Smckusick /*
77441480Smckusick  * Optimized macro to compute:
77541480Smckusick  *	eq->head == (eq->tail + 1) % eq->size
77641480Smckusick  * i.e. has tail caught up with head.  We do this because 32 bit long
77741480Smckusick  * remaidering is expensive (a function call with our compiler).
77841480Smckusick  */
77941480Smckusick #define HQFULL(eq)	(((eq)->head?(eq)->head:(eq)->size) == (eq)->tail+1)
78041480Smckusick #define HQVALID(eq) \
78141480Smckusick 	((eq)->size == HEVQSIZE && (eq)->tail >= 0 && (eq)->tail < HEVQSIZE)
78241480Smckusick 
78341480Smckusick hilevent(hilp)
78441480Smckusick 	struct hilloop *hilp;
78541480Smckusick {
78641480Smckusick 	register struct hilloopdev *dptr = &hilp->hl_device[hilp->hl_actdev];
78741480Smckusick 	register int len, mask, qnum;
78841480Smckusick 	register u_char *cp, *pp;
78941480Smckusick 	register HILQ *hq;
79041480Smckusick 	struct timeval ourtime;
79141480Smckusick 	hil_packet *proto;
79241480Smckusick 	int s, len0;
79341480Smckusick 	long tenths;
79441480Smckusick 
79541480Smckusick #ifdef PANICBUTTON
79641480Smckusick 	static int first;
79741480Smckusick 	extern int panicbutton;
79841480Smckusick 
79941480Smckusick 	cp = hilp->hl_pollbuf;
80041480Smckusick 	if (panicbutton && (*cp & HIL_KBDDATA)) {
80141480Smckusick 		if (*++cp == 0x4E)
80241480Smckusick 			first = 1;
80341480Smckusick 		else if (first && *cp == 0x46 && !panicstr)
80441480Smckusick 			panic("are we having fun yet?");
80541480Smckusick 		else
80641480Smckusick 			first = 0;
80741480Smckusick 	}
80841480Smckusick #endif
80941480Smckusick #ifdef DEBUG
81041480Smckusick 	if (hildebug & HDB_EVENTS) {
81141480Smckusick 		printf("hilevent: dev %d pollbuf: ", hilp->hl_actdev);
81241480Smckusick 		printhilpollbuf(hilp);
81341480Smckusick 		printf("\n");
81441480Smckusick 	}
81541480Smckusick #endif
81641480Smckusick 
81741480Smckusick 	/*
81841480Smckusick 	 * Note that HIL_READIN effectively "shuts off" any queues
81941480Smckusick 	 * that may have been in use at the time of an HILIOCHPUX call.
82041480Smckusick 	 */
82141480Smckusick 	if (dptr->hd_flags & HIL_READIN) {
82241480Smckusick 		hpuxhilevent(hilp, dptr);
82341480Smckusick 		return;
82441480Smckusick 	}
82541480Smckusick 
82641480Smckusick 	/*
82741480Smckusick 	 * If this device isn't on any queue or there are no data
82841480Smckusick 	 * in the packet (can this happen?) do nothing.
82941480Smckusick 	 */
83041480Smckusick 	if (dptr->hd_qmask == 0 ||
83141480Smckusick 	    (len0 = hilp->hl_pollbp - hilp->hl_pollbuf) <= 0)
83241480Smckusick 		return;
83341480Smckusick 
83441480Smckusick 	/*
83541480Smckusick 	 * Everybody gets the same time stamp
83641480Smckusick 	 */
83741480Smckusick 	s = splclock();
83841480Smckusick 	ourtime = time;
83941480Smckusick 	splx(s);
84041480Smckusick 	tenths = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000);
84141480Smckusick 
84241480Smckusick 	proto = NULL;
84341480Smckusick 	mask = dptr->hd_qmask;
84441480Smckusick 	for (qnum = 0; mask; qnum++) {
84541480Smckusick 		if ((mask & hilqmask(qnum)) == 0)
84641480Smckusick 			continue;
84741480Smckusick 		mask &= ~hilqmask(qnum);
84841480Smckusick 		hq = hilp->hl_queue[qnum].hq_eventqueue;
84941480Smckusick 
85041480Smckusick 		/*
85141480Smckusick 		 * Ensure that queue fields that we rely on are valid
85241480Smckusick 		 * and that there is space in the queue.  If either
85341480Smckusick 		 * test fails, we just skip this queue.
85441480Smckusick 		 */
85541480Smckusick 		if (!HQVALID(&hq->hil_evqueue) || HQFULL(&hq->hil_evqueue))
85641480Smckusick 			continue;
85741480Smckusick 
85841480Smckusick 		/*
85941480Smckusick 		 * Copy data to queue.
86041480Smckusick 		 * If this is the first queue we construct the packet
86141480Smckusick 		 * with length, timestamp and poll buffer data.
86241480Smckusick 		 * For second and sucessive packets we just duplicate
86341480Smckusick 		 * the first packet.
86441480Smckusick 		 */
86541480Smckusick 		pp = (u_char *) &hq->hil_event[hq->hil_evqueue.tail];
86641480Smckusick 		if (proto == NULL) {
86741480Smckusick 			proto = (hil_packet *)pp;
86841480Smckusick 			cp = hilp->hl_pollbuf;
86941480Smckusick 			len = len0;
87041480Smckusick 			*pp++ = len + 6;
87141480Smckusick 			*pp++ = hilp->hl_actdev;
87241480Smckusick 			*(long *)pp = tenths;
87341480Smckusick 			pp += sizeof(long);
87441480Smckusick 			do *pp++ = *cp++; while (--len);
87541480Smckusick 		} else
87641480Smckusick 			*(hil_packet *)pp = *proto;
87741480Smckusick 
87841480Smckusick 		if (++hq->hil_evqueue.tail == hq->hil_evqueue.size)
87941480Smckusick 			hq->hil_evqueue.tail = 0;
88041480Smckusick 	}
88141480Smckusick 
88241480Smckusick 	/*
88341480Smckusick 	 * Wake up anyone selecting on this device or the loop itself
88441480Smckusick 	 */
88541480Smckusick 	if (dptr->hd_selr) {
88641480Smckusick 		selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
88741480Smckusick 		dptr->hd_selr = NULL;
88841480Smckusick 		dptr->hd_flags &= ~HIL_SELCOLL;
88941480Smckusick 	}
89041480Smckusick 	dptr = &hilp->hl_device[HILLOOPDEV];
89141480Smckusick 	if (dptr->hd_selr) {
89241480Smckusick 		selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
89341480Smckusick 		dptr->hd_selr = NULL;
89441480Smckusick 		dptr->hd_flags &= ~HIL_SELCOLL;
89541480Smckusick 	}
89641480Smckusick }
89741480Smckusick 
89841480Smckusick #undef HQFULL
89941480Smckusick 
90041480Smckusick hpuxhilevent(hilp, dptr)
90141480Smckusick 	register struct hilloop *hilp;
90241480Smckusick 	register struct hilloopdev *dptr;
90341480Smckusick {
90441480Smckusick 	register int len;
90541480Smckusick 	struct timeval ourtime;
90641480Smckusick 	long tstamp;
90741480Smckusick 	int s;
90841480Smckusick 
90941480Smckusick 	/*
91041480Smckusick 	 * Everybody gets the same time stamp
91141480Smckusick 	 */
91241480Smckusick 	s = splclock();
91341480Smckusick 	ourtime = time;
91441480Smckusick 	splx(s);
91541480Smckusick 	tstamp = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000);
91641480Smckusick 
91741480Smckusick 	/*
91841480Smckusick 	 * Each packet that goes into the buffer must be preceded by the
91941480Smckusick 	 * number of bytes in the packet, and the timestamp of the packet.
92041480Smckusick 	 * This adds 5 bytes to the packet size. Make sure there is enough
92141480Smckusick 	 * room in the buffer for it, and if not, toss the packet.
92241480Smckusick 	 */
92341480Smckusick 	len = hilp->hl_pollbp - hilp->hl_pollbuf;
92441480Smckusick 	if (dptr->hd_queue.c_cc <= (HILMAXCLIST - (len+5))) {
92541480Smckusick 		putc(len+5, &dptr->hd_queue);
92641480Smckusick 		(void) b_to_q((char *)&tstamp, sizeof tstamp, &dptr->hd_queue);
92741480Smckusick 		(void) b_to_q((char *)hilp->hl_pollbuf, len, &dptr->hd_queue);
92841480Smckusick 	}
92941480Smckusick 
93041480Smckusick 	/*
93141480Smckusick 	 * Wake up any one blocked on a read or select
93241480Smckusick 	 */
93341480Smckusick 	if (dptr->hd_flags & HIL_ASLEEP) {
93441480Smckusick 		dptr->hd_flags &= ~HIL_ASLEEP;
93541480Smckusick 		wakeup((caddr_t)dptr);
93641480Smckusick 	}
93741480Smckusick 	if (dptr->hd_selr) {
93841480Smckusick 		selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
93941480Smckusick 		dptr->hd_selr = NULL;
94041480Smckusick 		dptr->hd_flags &= ~HIL_SELCOLL;
94141480Smckusick 	}
94241480Smckusick }
94341480Smckusick 
94441480Smckusick /*
94541480Smckusick  * Shared queue manipulation routines
94641480Smckusick  */
94741480Smckusick 
94841480Smckusick hilqalloc(qip)
94941480Smckusick 	struct hilqinfo *qip;
95041480Smckusick {
95141480Smckusick #ifdef MAPMEM
95241480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
95341480Smckusick 	register HILQ *hq;
95441480Smckusick 	register int qnum;
95541480Smckusick 	struct mapmem *mp;
95641480Smckusick 	int hilqmapin();
95741480Smckusick 
95841480Smckusick #ifdef DEBUG
95941480Smckusick 	if (hildebug & HDB_FOLLOW)
96041480Smckusick 		printf("hilqalloc(%d): addr %x\n",
96141480Smckusick 		       u.u_procp->p_pid, qip->addr);
96241480Smckusick #endif
96341480Smckusick 	/*
96441480Smckusick 	 * Find a free queue
96541480Smckusick 	 */
96641480Smckusick 	for (qnum = 0; qnum < NHILQ; qnum++)
96741480Smckusick 		if (hilp->hl_queue[qnum].hq_procp == NULL)
96841480Smckusick 			break;
96941480Smckusick 	if (qnum == NHILQ)
97041480Smckusick 		return(EMFILE);
97141480Smckusick 
97241480Smckusick 	/*
97341480Smckusick 	 * Allocate and clear memory for the queue
97441480Smckusick 	 */
97541480Smckusick 	if (hilp->hl_queue[qnum].hq_eventqueue)
97641480Smckusick 		panic("hilqalloc");
97741480Smckusick 	hq = (HILQ *) cialloc(sizeof(HILQ));
97841480Smckusick 	if (hq == NULL)
97941480Smckusick 		return(ENOMEM);
98041480Smckusick 	bzero((caddr_t)hq, sizeof(HILQ));
98141480Smckusick 	hilp->hl_queue[qnum].hq_eventqueue = hq;
98241480Smckusick 	hq->hil_evqueue.size = HEVQSIZE;
98341480Smckusick 
98441480Smckusick 	/*
98541480Smckusick 	 * Map queue into user address space as instructed
98641480Smckusick 	 */
98741480Smckusick 	mp = mmalloc(qnum, &qip->addr, sizeof(HILQ), MM_RW|MM_CI, &hilqops);
98841480Smckusick 	if (mp == MMNIL) {
98941480Smckusick 		cifree((caddr_t)hq, sizeof(HILQ));
99041480Smckusick 		hilp->hl_queue[qnum].hq_eventqueue = NULL;
99141480Smckusick 		return(u.u_error);
99241480Smckusick 	}
99341480Smckusick 	qip->qid = qnum;
99441480Smckusick 	if (!mmmapin(mp, hilqmapin)) {
99541480Smckusick 		mmfree(mp);
99641480Smckusick 		cifree((caddr_t)hq, sizeof(HILQ));
99741480Smckusick 		hilp->hl_queue[qnum].hq_eventqueue = NULL;
99841480Smckusick 		return(u.u_error);
99941480Smckusick 	}
100041480Smckusick 	hilp->hl_queue[qnum].hq_procp = u.u_procp;
100141480Smckusick 	hilp->hl_queue[qnum].hq_devmask = 0;
100241480Smckusick 	return(0);
100341480Smckusick #else
100441480Smckusick 	return(EINVAL);
100541480Smckusick #endif
100641480Smckusick }
100741480Smckusick 
100841480Smckusick hilqfree(qnum)
100941480Smckusick 	register int qnum;
101041480Smckusick {
101141480Smckusick #ifdef MAPMEM
101241480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
101341480Smckusick 	register struct mapmem *mp;
101441480Smckusick 
101541480Smckusick #ifdef DEBUG
101641480Smckusick 	if (hildebug & HDB_FOLLOW)
101741480Smckusick 		printf("hilqfree(%d): qnum %d\n",
101841480Smckusick 		       u.u_procp->p_pid, qnum);
101941480Smckusick #endif
102041480Smckusick 	if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != u.u_procp)
102141480Smckusick 		return(EINVAL);
102241480Smckusick 	for (mp = u.u_mmap; mp; mp = mp->mm_next)
102341480Smckusick 		if (qnum == mp->mm_id && mp->mm_ops == &hilqops) {
102441480Smckusick 			hilqexit(mp);
102541480Smckusick 			return(0);
102641480Smckusick 		}
102741480Smckusick 	panic("hilqfree");
102841480Smckusick 	/* NOTREACHED */
102941480Smckusick #else
103041480Smckusick 	return(EINVAL);
103141480Smckusick #endif
103241480Smckusick }
103341480Smckusick 
103441480Smckusick hilqmap(qnum, device)
103541480Smckusick 	register int qnum, device;
103641480Smckusick {
103741480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
103841480Smckusick 	register struct hilloopdev *dptr = &hilp->hl_device[device];
103941480Smckusick 	int s;
104041480Smckusick 
104141480Smckusick #ifdef DEBUG
104241480Smckusick 	if (hildebug & HDB_FOLLOW)
104341480Smckusick 		printf("hilqmap(%d): qnum %d device %x\n",
104441480Smckusick 		       u.u_procp->p_pid, qnum, device);
104541480Smckusick #endif
104641480Smckusick 	if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != u.u_procp)
104741480Smckusick 		return(EINVAL);
104841480Smckusick 	if ((dptr->hd_flags & HIL_QUEUEIN) == 0)
104941480Smckusick 		return(EINVAL);
105041480Smckusick 	if (dptr->hd_qmask && u.u_uid && u.u_uid != dptr->hd_uid)
105141480Smckusick 		return(EPERM);
105241480Smckusick 
105341480Smckusick 	hilp->hl_queue[qnum].hq_devmask |= hildevmask(device);
105441480Smckusick 	if (dptr->hd_qmask == 0)
105541480Smckusick 		dptr->hd_uid = u.u_uid;
105641480Smckusick 	s = splhil();
105741480Smckusick 	dptr->hd_qmask |= hilqmask(qnum);
105841480Smckusick 	splx(s);
105941480Smckusick #ifdef DEBUG
106041480Smckusick 	if (hildebug & HDB_MASK)
106141480Smckusick 		printf("hilqmap(%d): devmask %x qmask %x\n",
106241480Smckusick 		       u.u_procp->p_pid, hilp->hl_queue[qnum].hq_devmask,
106341480Smckusick 		       dptr->hd_qmask);
106441480Smckusick #endif
106541480Smckusick 	return(0);
106641480Smckusick }
106741480Smckusick 
106841480Smckusick hilqunmap(qnum, device)
106941480Smckusick 	register int qnum, device;
107041480Smckusick {
107141480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
107241480Smckusick 	int s;
107341480Smckusick 
107441480Smckusick #ifdef DEBUG
107541480Smckusick 	if (hildebug & HDB_FOLLOW)
107641480Smckusick 		printf("hilqunmap(%d): qnum %d device %x\n",
107741480Smckusick 		       u.u_procp->p_pid, qnum, device);
107841480Smckusick #endif
107941480Smckusick 
108041480Smckusick 	if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != u.u_procp)
108141480Smckusick 		return(EINVAL);
108241480Smckusick 
108341480Smckusick 	hilp->hl_queue[qnum].hq_devmask &= ~hildevmask(device);
108441480Smckusick 	s = splhil();
108541480Smckusick 	hilp->hl_device[device].hd_qmask &= ~hilqmask(qnum);
108641480Smckusick 	splx(s);
108741480Smckusick #ifdef DEBUG
108841480Smckusick 	if (hildebug & HDB_MASK)
108941480Smckusick 		printf("hilqunmap(%d): devmask %x qmask %x\n",
109041480Smckusick 		       u.u_procp->p_pid, hilp->hl_queue[qnum].hq_devmask,
109141480Smckusick 		       hilp->hl_device[device].hd_qmask);
109241480Smckusick #endif
109341480Smckusick 	return(0);
109441480Smckusick }
109541480Smckusick 
109641480Smckusick #ifdef MAPMEM
109741480Smckusick hilqmapin(mp, off)
109841480Smckusick 	struct mapmem *mp;
109941480Smckusick {
110041480Smckusick 	struct hilloop *hilp = &hil0;		/* XXX */
110141480Smckusick 	register HILQ *hq = hilp->hl_queue[mp->mm_id].hq_eventqueue;
110241480Smckusick 
110341480Smckusick 	if (hq == NULL || off >= sizeof(HILQ))
110441480Smckusick 		return(-1);
110541480Smckusick 	return(kvtop((u_int)hq + off) >> PGSHIFT);
110641480Smckusick }
110741480Smckusick 
110841480Smckusick /*
110941480Smckusick  * Fork hook.
111041480Smckusick  * Unmap queue from child's address space
111141480Smckusick  */
111241480Smckusick hilqfork(mp, ischild)
111341480Smckusick 	struct mapmem *mp;
111441480Smckusick {
111541480Smckusick #ifdef DEBUG
111641480Smckusick 	if (hildebug & HDB_MMAP)
111741480Smckusick 		printf("hilqfork(%d): %s qnum %d\n", u.u_procp->p_pid,
111841480Smckusick 		       ischild ? "child" : "parent", mp->mm_id);
111941480Smckusick #endif
112041480Smckusick 	if (ischild) {
112141480Smckusick 		mmmapout(mp);
112241480Smckusick 		mmfree(mp);
112341480Smckusick 	}
112441480Smckusick }
112541480Smckusick 
112641480Smckusick /*
112741480Smckusick  * Vfork hook.
112841480Smckusick  * Associate queue with child when VM resources are passed.
112941480Smckusick  */
113041480Smckusick hilqvfork(mp, fup, tup)
113141480Smckusick 	struct mapmem *mp;
113241480Smckusick 	struct user *fup, *tup;
113341480Smckusick {
113441480Smckusick 	struct hilloop *hilp = &hil0;		/* XXX */
113541480Smckusick 	register struct hiliqueue *qp = &hilp->hl_queue[mp->mm_id];
113641480Smckusick 
113741480Smckusick #ifdef DEBUG
113841480Smckusick 	if (hildebug & HDB_MMAP)
113941480Smckusick 		printf("hilqvfork(%d): from %x to %x qnum %d, qprocp %x\n",
114041480Smckusick 		       u.u_procp->p_pid, fup->u_procp, tup->u_procp,
114141480Smckusick 		       mp->mm_id, qp->hq_procp);
114241480Smckusick #endif
114341480Smckusick 	if (qp->hq_procp == fup->u_procp)
114441480Smckusick 		qp->hq_procp = tup->u_procp;
114541480Smckusick }
114641480Smckusick 
114741480Smckusick /*
114841480Smckusick  * Exit hook.
114941480Smckusick  * Unmap all devices and free all queues.
115041480Smckusick  */
115141480Smckusick hilqexit(mp)
115241480Smckusick 	struct mapmem *mp;
115341480Smckusick {
115441480Smckusick 	register struct hilloop *hilp = &hil0;	/* XXX */
115541480Smckusick 	register int mask, i;
115641480Smckusick 	int s;
115741480Smckusick 
115841480Smckusick #ifdef DEBUG
115941480Smckusick 	if (hildebug & HDB_MMAP)
116041480Smckusick 		printf("hilqexit(%d): qnum %d\n", u.u_procp->p_pid, mp->mm_id);
116141480Smckusick #endif
116241480Smckusick 	/*
116341480Smckusick 	 * Atomically take all devices off the queue
116441480Smckusick 	 */
116541480Smckusick 	mask = ~hilqmask(mp->mm_id);
116641480Smckusick 	s = splhil();
116741480Smckusick 	for (i = 0; i < NHILD; i++)
116841480Smckusick 		hilp->hl_device[i].hd_qmask &= mask;
116941480Smckusick 	splx(s);
117041480Smckusick 	/*
117141480Smckusick 	 * Now unmap from user address space and free queue
117241480Smckusick 	 */
117341480Smckusick 	i = mp->mm_id;
117441480Smckusick 	cifree((caddr_t)hilp->hl_queue[i].hq_eventqueue, sizeof(HILQ));
117541480Smckusick 	hilp->hl_queue[i].hq_eventqueue = NULL;
117641480Smckusick 	hilp->hl_queue[i].hq_procp = NULL;
117741480Smckusick 	mmfree(mp);
117841480Smckusick }
117941480Smckusick #endif
118041480Smckusick 
118141480Smckusick #include "clist.h"
118241480Smckusick 
118341480Smckusick /*
118441480Smckusick  * This is just a copy of the virgin q_to_b routine with minor
118541480Smckusick  * optimizations for HIL use.  It is used for two reasons:
118641480Smckusick  * 1. If we have PAGE mode defined, the normal q_to_b processes
118741480Smckusick  *    chars one at a time and breaks on newlines.
118841480Smckusick  * 2. We don't have to raise the priority to spltty() for most
118941480Smckusick  *    of the clist manipulations.
119041480Smckusick  */
119141480Smckusick hilq_to_b(q, cp, cc)
119241480Smckusick 	register struct clist *q;
119341480Smckusick 	register char *cp;
119441480Smckusick {
119541480Smckusick 	register struct cblock *bp;
119641480Smckusick 	register int nc;
119741480Smckusick 	char *acp;
119841480Smckusick 	int s;
119941480Smckusick 	extern char cwaiting;
120041480Smckusick 
120141480Smckusick 	if (cc <= 0)
120241480Smckusick 		return (0);
120341480Smckusick 	s = splhil();
120441480Smckusick 	if (q->c_cc <= 0) {
120541480Smckusick 		q->c_cc = 0;
120641480Smckusick 		q->c_cf = q->c_cl = NULL;
120741480Smckusick 		splx(s);
120841480Smckusick 		return (0);
120941480Smckusick 	}
121041480Smckusick 	acp = cp;
121141480Smckusick 
121241480Smckusick 	while (cc) {
121341480Smckusick 		nc = sizeof (struct cblock) - ((int)q->c_cf & CROUND);
121441480Smckusick 		nc = MIN(nc, cc);
121541480Smckusick 		nc = MIN(nc, q->c_cc);
121641480Smckusick 		(void) bcopy(q->c_cf, cp, (unsigned)nc);
121741480Smckusick 		q->c_cf += nc;
121841480Smckusick 		q->c_cc -= nc;
121941480Smckusick 		cc -= nc;
122041480Smckusick 		cp += nc;
122141480Smckusick 		if (q->c_cc <= 0) {
122241480Smckusick 			bp = (struct cblock *)(q->c_cf - 1);
122341480Smckusick 			bp = (struct cblock *)((int)bp & ~CROUND);
122441480Smckusick 			q->c_cf = q->c_cl = NULL;
122541480Smckusick 			spltty();
122641480Smckusick 			bp->c_next = cfreelist;
122741480Smckusick 			cfreelist = bp;
122841480Smckusick 			cfreecount += CBSIZE;
122941480Smckusick 			if (cwaiting) {
123041480Smckusick 				wakeup(&cwaiting);
123141480Smckusick 				cwaiting = 0;
123241480Smckusick 			}
123341480Smckusick 			break;
123441480Smckusick 		}
123541480Smckusick 		if (((int)q->c_cf & CROUND) == 0) {
123641480Smckusick 			bp = (struct cblock *)(q->c_cf);
123741480Smckusick 			bp--;
123841480Smckusick 			q->c_cf = bp->c_next->c_info;
123941480Smckusick 			spltty();
124041480Smckusick 			bp->c_next = cfreelist;
124141480Smckusick 			cfreelist = bp;
124241480Smckusick 			cfreecount += CBSIZE;
124341480Smckusick 			if (cwaiting) {
124441480Smckusick 				wakeup(&cwaiting);
124541480Smckusick 				cwaiting = 0;
124641480Smckusick 			}
124741480Smckusick 			splhil();
124841480Smckusick 		}
124941480Smckusick 	}
125041480Smckusick 	splx(s);
125141480Smckusick 	return (cp-acp);
125241480Smckusick }
125341480Smckusick 
125441480Smckusick /*
125541480Smckusick  * Cooked keyboard functions for ite driver.
125641480Smckusick  * There is only one "cooked" ITE keyboard (the first keyboard found)
125741480Smckusick  * per loop.  There may be other keyboards, but they will always be "raw".
125841480Smckusick  */
125941480Smckusick 
126041480Smckusick kbdbell()
126141480Smckusick {
126241480Smckusick 	struct hilloop *hilp = &hil0;		/* XXX */
126341480Smckusick 
126441480Smckusick 	hilbeep(hilp, &default_bell);
126541480Smckusick }
126641480Smckusick 
126741480Smckusick kbdenable()
126841480Smckusick {
126941480Smckusick 	struct hilloop *hilp = &hil0;	/* XXX */
127041480Smckusick 	register struct hil_dev *hildevice = hilp->hl_addr;
127141480Smckusick 	char db;
127241480Smckusick 
127341480Smckusick 	/* Set the autorepeat rate register */
127441480Smckusick 	db = ar_format(KBD_ARR);
127541480Smckusick 	send_hil_cmd(hildevice, HIL_SETARR, &db, 1, NULL);
127641480Smckusick 
127741480Smckusick 	/* Set the autorepeat delay register */
127841480Smckusick 	db = ar_format(KBD_ARD);
127941480Smckusick 	send_hil_cmd(hildevice, HIL_SETARD, &db, 1, NULL);
128041480Smckusick 
128141480Smckusick 	/* Enable interrupts */
128241480Smckusick 	send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
128341480Smckusick }
128441480Smckusick 
128541480Smckusick kbddisable()
128641480Smckusick {
128741480Smckusick }
128841480Smckusick 
128941480Smckusick /*
129041480Smckusick  * XXX: read keyboard directly and return code.
129141480Smckusick  * Used by console getchar routine.  Could really screw up anybody
129241480Smckusick  * reading from the keyboard in the normal, interrupt driven fashion.
129341480Smckusick  */
129441480Smckusick kbdgetc(statp)
129541480Smckusick 	int *statp;
129641480Smckusick {
129741480Smckusick 	struct hilloop *hilp = &hil0;		/* XXX */
129841480Smckusick 	register struct hil_dev *hildevice = hilp->hl_addr;
129941480Smckusick 	register int c, stat;
130041480Smckusick 	int s;
130141480Smckusick 
130241480Smckusick 	s = splhil();
130341480Smckusick 	while (((stat = hildevice->hil_stat) & HIL_DATA_RDY) == 0)
130441480Smckusick 		;
130541480Smckusick 	c = hildevice->hil_data;
130641480Smckusick 	splx(s);
130741480Smckusick 	*statp = stat;
130841480Smckusick 	return(c);
130941480Smckusick }
131041480Smckusick 
131141480Smckusick /*
131241480Smckusick  * Recoginize and clear keyboard generated NMIs.
131341480Smckusick  * Returns 1 if it was ours, 0 otherwise.  Note that we cannot use
131441480Smckusick  * send_hil_cmd() to issue the clear NMI command as that would actually
131541480Smckusick  * lower the priority to splimp() and it doesn't wait for the completion
131641480Smckusick  * of the command.  Either of these conditions could result in the
131741480Smckusick  * interrupt reoccuring.  Note that we issue the CNMT command twice.
131841480Smckusick  * This seems to be needed, once is not always enough!?!
131941480Smckusick  */
132041480Smckusick kbdnmi()
132141480Smckusick {
132241480Smckusick 	register struct hilloop *hilp = &hil0;		/* XXX */
132341480Smckusick 
132441480Smckusick 	if ((*KBDNMISTAT & KBDNMI) == 0)
132541480Smckusick 		return(0);
132641480Smckusick 	HILWAIT(hilp->hl_addr);
132741480Smckusick 	hilp->hl_addr->hil_cmd = HIL_CNMT;
132841480Smckusick 	HILWAIT(hilp->hl_addr);
132941480Smckusick 	hilp->hl_addr->hil_cmd = HIL_CNMT;
133041480Smckusick 	HILWAIT(hilp->hl_addr);
133141480Smckusick 	return(1);
133241480Smckusick }
133341480Smckusick 
133441480Smckusick #define HILSECURITY	0x33
133541480Smckusick #define HILIDENTIFY	0x03
133641480Smckusick #define HILSCBIT	0x04
133741480Smckusick 
133841480Smckusick /*
133941480Smckusick  * Called at boot time to print out info about interesting devices
134041480Smckusick  */
134141480Smckusick hilinfo(hilp)
134241480Smckusick 	register struct hilloop *hilp;
134341480Smckusick {
134441480Smckusick 	register int id, len;
134541480Smckusick 	register struct kbdmap *km;
134641480Smckusick 
134741480Smckusick 	/*
134841480Smckusick 	 * Keyboard info.
134941480Smckusick 	 */
135041480Smckusick 	if (hilp->hl_kbddev) {
135141480Smckusick 		printf("hil%d: ", hilp->hl_kbddev);
135241480Smckusick 		for (km = kbd_map; km->kbd_code; km++)
135341480Smckusick 			if (km->kbd_code == hilp->hl_kbdlang) {
135441480Smckusick 				printf("%s ", km->kbd_desc);
135541480Smckusick 				break;
135641480Smckusick 			}
135741480Smckusick 		printf("keyboard\n");
135841480Smckusick 	}
135941480Smckusick 	/*
136041480Smckusick 	 * ID module.
136141480Smckusick 	 * Attempt to locate the first ID module and print out its
136241480Smckusick 	 * security code.  Is this a good idea??
136341480Smckusick 	 */
136441480Smckusick 	id = hiliddev(hilp);
136541480Smckusick 	if (id) {
136641480Smckusick 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
136741480Smckusick 		hilp->hl_cmddev = id;
136841480Smckusick 		send_hildev_cmd(hilp, id, HILSECURITY);
136941480Smckusick 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
137041480Smckusick 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
137141480Smckusick 		hilp->hl_cmddev = 0;
137241480Smckusick 		printf("hil%d: security code", id);
137341480Smckusick 		for (id = 0; id < len; id++)
137441480Smckusick 			printf(" %x", hilp->hl_cmdbuf[id]);
137541480Smckusick 		while (id++ < 16)
137641480Smckusick 			printf(" 0");
137741480Smckusick 		printf("\n");
137841480Smckusick 	}
137941480Smckusick }
138041480Smckusick 
138141480Smckusick #define HILAR1	0x3E
138241480Smckusick #define HILAR2	0x3F
138341480Smckusick 
138441480Smckusick /*
138541480Smckusick  * Called after the loop has reconfigured.  Here we need to:
138641480Smckusick  *	- determine how many devices are on the loop
138741480Smckusick  *	  (some may have been added or removed)
138841480Smckusick  *	- locate the ITE keyboard (if any) and ensure
138941480Smckusick  *	  that it is in the proper state (raw or cooked)
139041480Smckusick  *	  and is set to use the proper language mapping table
139141480Smckusick  *	- ensure all other keyboards are raw
139241480Smckusick  * Note that our device state is now potentially invalid as
139341480Smckusick  * devices may no longer be where they were.  What we should
139441480Smckusick  * do here is either track where the devices went and move
139541480Smckusick  * state around accordingly or, more simply, just mark all
139641480Smckusick  * devices as HIL_DERROR and don't allow any further use until
139741480Smckusick  * they are closed.  This is a little too brutal for my tastes,
139841480Smckusick  * we prefer to just assume people won't move things around.
139941480Smckusick  */
140041480Smckusick hilconfig(hilp)
140141480Smckusick 	register struct hilloop *hilp;
140241480Smckusick {
140341480Smckusick 	u_char db;
140441480Smckusick 	int s;
140541480Smckusick 
140641480Smckusick 	s = splhil();
140741480Smckusick #ifdef DEBUG
140841480Smckusick 	if (hildebug & HDB_CONFIG) {
140941480Smckusick 		printf("hilconfig: reconfigured: ");
141041480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
141141480Smckusick 		printf("LPSTAT %x, ", db);
141241480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_READLPCTRL, NULL, 0, &db);
141341480Smckusick 		printf("LPCTRL %x, ", db);
141441480Smckusick 		send_hil_cmd(hilp->hl_addr, HIL_READKBDSADR, NULL, 0, &db);
141541480Smckusick 		printf("KBDSADR %x\n", db);
141641480Smckusick 		hilreport(hilp);
141741480Smckusick 	}
141841480Smckusick #endif
141941480Smckusick 	/*
142041480Smckusick 	 * Determine how many devices are on the loop.
142141480Smckusick 	 * Mark those as alive and real, all others as dead.
142241480Smckusick 	 */
142341480Smckusick 	db = 0;
142441480Smckusick 	send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
142541480Smckusick 	hilp->hl_maxdev = db & LPS_DEVMASK;
142641480Smckusick 	for (db = 1; db < NHILD; db++) {
142741480Smckusick 		if (db <= hilp->hl_maxdev)
142841480Smckusick 			hilp->hl_device[db].hd_flags |= HIL_ALIVE;
142941480Smckusick 		else
143041480Smckusick 			hilp->hl_device[db].hd_flags &= ~HIL_ALIVE;
143141480Smckusick 		hilp->hl_device[db].hd_flags &= ~HIL_PSEUDO;
143241480Smckusick 	}
143341480Smckusick #ifdef DEBUG
143441480Smckusick 	if (hildebug & (HDB_CONFIG|HDB_KEYBOARD))
143541480Smckusick 		printf("hilconfig: max device %d\n", hilp->hl_maxdev);
143641480Smckusick #endif
143741480Smckusick 	if (hilp->hl_maxdev == 0) {
143841480Smckusick 		hilp->hl_kbddev = 0;
143941480Smckusick 		splx(s);
144041480Smckusick 		return;
144141480Smckusick 	}
144241480Smckusick 	/*
144341480Smckusick 	 * Find out where the keyboards are and record the ITE keyboard
144441480Smckusick 	 * (first one found).  If no keyboards found, we are all done.
144541480Smckusick 	 */
144641480Smckusick 	db = 0;
144741480Smckusick 	send_hil_cmd(hilp->hl_addr, HIL_READKBDSADR, NULL, 0, &db);
144841480Smckusick #ifdef DEBUG
144941480Smckusick 	if (hildebug & HDB_KEYBOARD)
145041480Smckusick 		printf("hilconfig: keyboard: KBDSADR %x, old %d, new %d\n",
145141480Smckusick 		       db, hilp->hl_kbddev, ffs((int)db));
145241480Smckusick #endif
145341480Smckusick 	hilp->hl_kbddev = ffs((int)db);
145441480Smckusick 	if (hilp->hl_kbddev == 0) {
145541480Smckusick 		splx(s);
145641480Smckusick 		return;
145741480Smckusick 	}
145841480Smckusick 	/*
145941480Smckusick 	 * Determine if the keyboard should be cooked or raw and configure it.
146041480Smckusick 	 */
146141480Smckusick 	db = (hilp->hl_kbdflags & KBD_RAW) ? 0 : 1 << (hilp->hl_kbddev - 1);
146241480Smckusick 	send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &db, 1, NULL);
146341480Smckusick 	/*
146441480Smckusick 	 * Re-enable autorepeat in raw mode, cooked mode AR is not affected.
146541480Smckusick 	 */
146641480Smckusick 	if (hilp->hl_kbdflags & (KBD_AR1|KBD_AR2)) {
146741480Smckusick 		db = (hilp->hl_kbdflags & KBD_AR1) ? HILAR1 : HILAR2;
146841480Smckusick 		hilp->hl_cmddev = hilp->hl_kbddev;
146941480Smckusick 		send_hildev_cmd(hilp, hilp->hl_kbddev, db);
147041480Smckusick 		hilp->hl_cmddev = 0;
147141480Smckusick 	}
147241480Smckusick 	/*
147341480Smckusick 	 * Determine the keyboard language configuration, but don't
147441480Smckusick 	 * override a user-specified setting.
147541480Smckusick 	 */
147641480Smckusick 	db = 0;
147741480Smckusick 	send_hil_cmd(hilp->hl_addr, HIL_READKBDLANG, NULL, 0, &db);
147841480Smckusick #ifdef DEBUG
147941480Smckusick 	if (hildebug & HDB_KEYBOARD)
148041480Smckusick 		printf("hilconfig: language: old %x new %x\n",
148141480Smckusick 		       hilp->hl_kbdlang, db);
148241480Smckusick #endif
148341480Smckusick 	if (hilp->hl_kbdlang != KBD_SPECIAL) {
148441480Smckusick 		struct kbdmap *km;
148541480Smckusick 
148641480Smckusick 		for (km = kbd_map; km->kbd_code; km++)
148741480Smckusick 			if (km->kbd_code == db) {
148841480Smckusick 				hilp->hl_kbdlang = db;
148941480Smckusick 				/* XXX */
149041480Smckusick 				kbd_keymap = km->kbd_keymap;
149141480Smckusick 				kbd_shiftmap = km->kbd_shiftmap;
149241480Smckusick 				kbd_ctrlmap = km->kbd_ctrlmap;
149341480Smckusick 				kbd_ctrlshiftmap = km->kbd_ctrlshiftmap;
149441480Smckusick 				kbd_stringmap = km->kbd_stringmap;
149541480Smckusick 			}
149641480Smckusick 	}
149741480Smckusick 	splx(s);
149841480Smckusick }
149941480Smckusick 
150041480Smckusick hilreset(hilp)
150141480Smckusick 	struct hilloop *hilp;
150241480Smckusick {
150341480Smckusick 	register struct hil_dev *hildevice = hilp->hl_addr;
150441480Smckusick 	u_char db;
150541480Smckusick 
150641480Smckusick 	/*
150741480Smckusick 	 * Initialize the loop: reconfigure, don't report errors,
150841480Smckusick 	 * cook keyboards, and enable autopolling.
150941480Smckusick 	 */
151041480Smckusick 	db = LPC_RECONF | LPC_KBDCOOK | LPC_NOERROR | LPC_AUTOPOLL;
151141480Smckusick 	send_hil_cmd(hildevice, HIL_WRITELPCTRL, &db, 1, NULL);
151241480Smckusick 	/*
151341480Smckusick 	 * Delay one second for reconfiguration and then read the the
151441480Smckusick 	 * data register to clear the interrupt (if the loop reconfigured).
151541480Smckusick 	 */
151641480Smckusick 	DELAY(1000000);
151741480Smckusick 	if (hildevice->hil_stat & HIL_DATA_RDY)
151841480Smckusick 		db = hildevice->hil_data;
151941480Smckusick 	/*
152041480Smckusick 	 * The HIL loop may have reconfigured.  If so we proceed on,
152141480Smckusick 	 * if not we loop until a successful reconfiguration is reported
152241480Smckusick 	 * back to us.  The HIL loop will continue to attempt forever.
152341480Smckusick 	 * Probably not very smart.
152441480Smckusick 	 */
152541480Smckusick 	do {
152641480Smckusick 		send_hil_cmd(hildevice, HIL_READLPSTAT, NULL, 0, &db);
152741480Smckusick         } while ((db & (LPS_CONFFAIL|LPS_CONFGOOD)) == 0);
152841480Smckusick 	/*
152941480Smckusick 	 * At this point, the loop should have reconfigured.
153041480Smckusick 	 * The reconfiguration interrupt has already called hilconfig()
153141480Smckusick 	 * so the keyboard has been determined.  All that is left is
153241480Smckusick 	 *
153341480Smckusick 	 */
153441480Smckusick #if 0
153541480Smckusick 	hilconfig(hilp);
153641480Smckusick #endif
153741480Smckusick 	send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
153841480Smckusick }
153941480Smckusick 
154041480Smckusick hilbeep(hilp, bp)
154141480Smckusick 	struct hilloop *hilp;
154241480Smckusick 	register struct _hilbell *bp;
154341480Smckusick {
154441480Smckusick 	u_char buf[2];
154541480Smckusick 
154641480Smckusick 	buf[0] = ~((bp->duration - 10) / 10);
154741480Smckusick 	buf[1] = bp->frequency;
154841480Smckusick 	send_hil_cmd(hilp->hl_addr, HIL_SETTONE, buf, 2, NULL);
154941480Smckusick }
155041480Smckusick 
155141480Smckusick /*
155241480Smckusick  * Locate and return the address of the first ID module, 0 if none present.
155341480Smckusick  */
155441480Smckusick hiliddev(hilp)
155541480Smckusick 	register struct hilloop *hilp;
155641480Smckusick {
155741480Smckusick 	register int i, len;
155841480Smckusick 
155941480Smckusick #ifdef DEBUG
156041480Smckusick 	if (hildebug & HDB_IDMODULE)
156141480Smckusick 		printf("hiliddev(%x): looking for idmodule...", hilp);
156241480Smckusick #endif
156341480Smckusick 	for (i = 1; i <= hilp->hl_maxdev; i++) {
156441480Smckusick 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
156541480Smckusick 		hilp->hl_cmddev = i;
156641480Smckusick 		send_hildev_cmd(hilp, i, HILIDENTIFY);
156741480Smckusick 		/*
156841480Smckusick 		 * XXX: the final condition checks to ensure that the
156941480Smckusick 		 * device ID byte is in the range of the ID module (0x30-0x3F)
157041480Smckusick 		 */
157141480Smckusick 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
157241480Smckusick 		if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT) &&
157341480Smckusick 		    (hilp->hl_cmdbuf[0] & 0xF0) == 0x30) {
157441480Smckusick 			hilp->hl_cmdbp = hilp->hl_cmdbuf;
157541480Smckusick 			hilp->hl_cmddev = i;
157641480Smckusick 			send_hildev_cmd(hilp, i, HILSECURITY);
157741480Smckusick 			break;
157841480Smckusick 		}
157941480Smckusick 	}
158041480Smckusick 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
158141480Smckusick 	hilp->hl_cmddev = 0;
158241480Smckusick #ifdef DEBUG
158341480Smckusick 	if (hildebug & HDB_IDMODULE)
158441480Smckusick 		if (i <= hilp->hl_maxdev)
158541480Smckusick 			printf("found at %d\n", i);
158641480Smckusick 		else
158741480Smckusick 			printf("not found\n");
158841480Smckusick #endif
158941480Smckusick 	return(i <= hilp->hl_maxdev ? i : 0);
159041480Smckusick }
159141480Smckusick 
159241480Smckusick /*
159341480Smckusick  * Low level routines which actually talk to the 8042 chip.
159441480Smckusick  */
159541480Smckusick 
159641480Smckusick /*
159741480Smckusick  * Send a command to the 8042 with zero or more bytes of data.
159841480Smckusick  * If rdata is non-null, wait for and return a byte of data.
159941480Smckusick  * We run at splimp() to make the transaction as atomic as
160041480Smckusick  * possible without blocking the clock (is this necessary?)
160141480Smckusick  */
160241480Smckusick send_hil_cmd(hildevice, cmd, data, dlen, rdata)
160341480Smckusick 	register struct hil_dev *hildevice;
160441480Smckusick 	u_char cmd, *data, dlen;
160541480Smckusick 	u_char *rdata;
160641480Smckusick {
160741480Smckusick 	u_char status;
160841480Smckusick 	int s = splimp();
160941480Smckusick 
161041480Smckusick 	HILWAIT(hildevice);
161141480Smckusick 	hildevice->hil_cmd = cmd;
161241480Smckusick 	while (dlen--) {
161341480Smckusick 	  	HILWAIT(hildevice);
161441480Smckusick 		hildevice->hil_data = *data++;
161541480Smckusick 	}
161641480Smckusick 	if (rdata) {
161741480Smckusick 		do {
161841480Smckusick 			HILDATAWAIT(hildevice);
161941480Smckusick 			status = hildevice->hil_stat;
162041480Smckusick 			*rdata = hildevice->hil_data;
162141480Smckusick 		} while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
162241480Smckusick 	}
162341480Smckusick 	splx(s);
162441480Smckusick }
162541480Smckusick 
162641480Smckusick /*
162741480Smckusick  * Send a command to a device on the loop.
162841480Smckusick  * Since only one command can be active on the loop at any time,
162941480Smckusick  * we must ensure that we are not interrupted during this process.
163041480Smckusick  * Hence we mask interrupts to prevent potential access from most
163141480Smckusick  * interrupt routines and turn off auto-polling to disable the
163241480Smckusick  * internally generated poll commands.
163341480Smckusick  *
163441480Smckusick  * splhigh is extremely conservative but insures atomic operation,
163541480Smckusick  * splimp (clock only interrupts) seems to be good enough in practice.
163641480Smckusick  */
163741480Smckusick send_hildev_cmd(hilp, device, cmd)
163841480Smckusick 	register struct hilloop *hilp;
163941480Smckusick 	char device, cmd;
164041480Smckusick {
164141480Smckusick 	register struct hil_dev *hildevice = hilp->hl_addr;
164241480Smckusick 	u_char status, c;
164341480Smckusick 	int s = splimp();
164441480Smckusick 
164541480Smckusick 	polloff(hildevice);
164641480Smckusick 
164741480Smckusick 	/*
164841480Smckusick 	 * Transfer the command and device info to the chip
164941480Smckusick 	 */
165041480Smckusick 	HILWAIT(hildevice);
165141480Smckusick 	hildevice->hil_cmd = HIL_STARTCMD;
165241480Smckusick   	HILWAIT(hildevice);
165341480Smckusick 	hildevice->hil_data = 8 + device;
165441480Smckusick   	HILWAIT(hildevice);
165541480Smckusick 	hildevice->hil_data = cmd;
165641480Smckusick   	HILWAIT(hildevice);
165741480Smckusick 	hildevice->hil_data = HIL_TIMEOUT;
165841480Smckusick 	/*
165941480Smckusick 	 * Trigger the command and wait for completion
166041480Smckusick 	 */
166141480Smckusick 	HILWAIT(hildevice);
166241480Smckusick 	hildevice->hil_cmd = HIL_TRIGGER;
166341480Smckusick 	hilp->hl_cmddone = FALSE;
166441480Smckusick 	do {
166541480Smckusick 		HILDATAWAIT(hildevice);
166641480Smckusick 		status = hildevice->hil_stat;
166741480Smckusick 		c = hildevice->hil_data;
166841480Smckusick 		hil_process_int(status, c);
166941480Smckusick 	} while (!hilp->hl_cmddone);
167041480Smckusick 
167141480Smckusick 	pollon(hildevice);
167241480Smckusick 	splx(s);
167341480Smckusick }
167441480Smckusick 
167541480Smckusick /*
167641480Smckusick  * Turn auto-polling off and on.
167741480Smckusick  * Also disables and enable auto-repeat.  Why?
167841480Smckusick  */
167941480Smckusick polloff(hildevice)
168041480Smckusick 	register struct hil_dev *hildevice;
168141480Smckusick {
168241480Smckusick 	register char db;
168341480Smckusick 
168441480Smckusick 	/*
168541480Smckusick 	 * Turn off auto repeat
168641480Smckusick 	 */
168741480Smckusick 	HILWAIT(hildevice);
168841480Smckusick 	hildevice->hil_cmd = HIL_SETARR;
168941480Smckusick 	HILWAIT(hildevice);
169041480Smckusick 	hildevice->hil_data = 0;
169141480Smckusick 	/*
169241480Smckusick 	 * Turn off auto-polling
169341480Smckusick 	 */
169441480Smckusick 	HILWAIT(hildevice);
169541480Smckusick 	hildevice->hil_cmd = HIL_READLPCTRL;
169641480Smckusick 	HILDATAWAIT(hildevice);
169741480Smckusick 	db = hildevice->hil_data;
169841480Smckusick 	db &= ~LPC_AUTOPOLL;
169941480Smckusick 	HILWAIT(hildevice);
170041480Smckusick 	hildevice->hil_cmd = HIL_WRITELPCTRL;
170141480Smckusick 	HILWAIT(hildevice);
170241480Smckusick 	hildevice->hil_data = db;
170341480Smckusick 	/*
170441480Smckusick 	 * Must wait til polling is really stopped
170541480Smckusick 	 */
170641480Smckusick 	do {
170741480Smckusick 		HILWAIT(hildevice);
170841480Smckusick 		hildevice->hil_cmd = HIL_READBUSY;
170941480Smckusick 		HILDATAWAIT(hildevice);
171041480Smckusick 		db = hildevice->hil_data;
171141480Smckusick 	} while (db & BSY_LOOPBUSY);
171241480Smckusick }
171341480Smckusick 
171441480Smckusick pollon(hildevice)
171541480Smckusick 	register struct hil_dev *hildevice;
171641480Smckusick {
171741480Smckusick 	register char db;
171841480Smckusick 
171941480Smckusick 	/*
172041480Smckusick 	 * Turn on auto polling
172141480Smckusick 	 */
172241480Smckusick 	HILWAIT(hildevice);
172341480Smckusick 	hildevice->hil_cmd = HIL_READLPCTRL;
172441480Smckusick 	HILDATAWAIT(hildevice);
172541480Smckusick 	db = hildevice->hil_data;
172641480Smckusick 	db |= LPC_AUTOPOLL;
172741480Smckusick 	HILWAIT(hildevice);
172841480Smckusick 	hildevice->hil_cmd = HIL_WRITELPCTRL;
172941480Smckusick 	HILWAIT(hildevice);
173041480Smckusick 	hildevice->hil_data = db;
173141480Smckusick 	/*
173241480Smckusick 	 * Turn on auto repeat
173341480Smckusick 	 */
173441480Smckusick 	HILWAIT(hildevice);
173541480Smckusick 	hildevice->hil_cmd = HIL_SETARR;
173641480Smckusick 	HILWAIT(hildevice);
173741480Smckusick 	hildevice->hil_data = ar_format(KBD_ARR);
173841480Smckusick }
173941480Smckusick 
174041480Smckusick #ifdef DEBUG
174141480Smckusick printhilpollbuf(hilp)
174241480Smckusick 	register struct hilloop *hilp;
174341480Smckusick {
174441480Smckusick   	register u_char *cp;
174541480Smckusick 	register int i, len;
174641480Smckusick 
174741480Smckusick 	cp = hilp->hl_pollbuf;
174841480Smckusick 	len = hilp->hl_pollbp - cp;
174941480Smckusick 	for (i = 0; i < len; i++)
175041480Smckusick 		printf("%x ", hilp->hl_pollbuf[i]);
175141480Smckusick 	printf("\n");
175241480Smckusick }
175341480Smckusick 
175441480Smckusick printhilcmdbuf(hilp)
175541480Smckusick 	register struct hilloop *hilp;
175641480Smckusick {
175741480Smckusick   	register u_char *cp;
175841480Smckusick 	register int i, len;
175941480Smckusick 
176041480Smckusick 	cp = hilp->hl_cmdbuf;
176141480Smckusick 	len = hilp->hl_cmdbp - cp;
176241480Smckusick 	for (i = 0; i < len; i++)
176341480Smckusick 		printf("%x ", hilp->hl_cmdbuf[i]);
176441480Smckusick 	printf("\n");
176541480Smckusick }
176641480Smckusick 
176741480Smckusick hilreport(hilp)
176841480Smckusick 	register struct hilloop *hilp;
176941480Smckusick {
177041480Smckusick 	register int i, len;
177141480Smckusick 	int s = splhil();
177241480Smckusick 
177341480Smckusick 	for (i = 1; i <= hilp->hl_maxdev; i++) {
177441480Smckusick 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
177541480Smckusick 		hilp->hl_cmddev = i;
177641480Smckusick 		send_hildev_cmd(hilp, i, HILIDENTIFY);
177741480Smckusick 		printf("hil%d: id: ", i);
177841480Smckusick 		printhilcmdbuf(hilp);
177941480Smckusick 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
178041480Smckusick 		if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT)) {
178141480Smckusick 			hilp->hl_cmdbp = hilp->hl_cmdbuf;
178241480Smckusick 			hilp->hl_cmddev = i;
178341480Smckusick 			send_hildev_cmd(hilp, i, HILSECURITY);
178441480Smckusick 			printf("hil%d: sc: ", i);
178541480Smckusick 			printhilcmdbuf(hilp);
178641480Smckusick 		}
178741480Smckusick 	}
178841480Smckusick 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
178941480Smckusick 	hilp->hl_cmddev = 0;
179041480Smckusick 	splx(s);
179141480Smckusick }
179241480Smckusick #endif
1793