xref: /csrg-svn/sys/hp/dev/hil.c (revision 50253)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * %sccs.include.redist.c%
11  *
12  * from: Utah $Hdr: hil.c 1.33 89/12/22$
13  *
14  *	@(#)hil.c	7.8.1.1 (Berkeley) 06/28/91
15  */
16 
17 #include "sys/param.h"
18 #include "sys/conf.h"
19 #include "sys/proc.h"
20 #include "sys/user.h"
21 #include "sys/ioctl.h"
22 #include "sys/file.h"
23 #include "sys/tty.h"
24 #include "sys/systm.h"
25 #include "sys/uio.h"
26 #include "sys/kernel.h"
27 
28 #include "hilreg.h"
29 #include "hilioctl.h"
30 #include "hilvar.h"
31 #include "kbdmap.h"
32 
33 #include "machine/cpu.h"
34 
35 #include "vm/vm_param.h"
36 #include "vm/vm_map.h"
37 #include "vm/vm_kern.h"
38 #include "vm/vm_page.h"
39 #include "vm/vm_pager.h"
40 
41 struct	hilloop	hil0;
42 struct	_hilbell default_bell = { BELLDUR, BELLFREQ };
43 
44 #ifdef DEBUG
45 int 	hildebug = 0;
46 #define HDB_FOLLOW	0x01
47 #define HDB_MMAP	0x02
48 #define HDB_MASK	0x04
49 #define HDB_CONFIG	0x08
50 #define HDB_KEYBOARD	0x10
51 #define HDB_IDMODULE	0x20
52 #define HDB_EVENTS	0x80
53 #endif
54 
55 /* symbolic sleep message strings */
56 char hilin[] = "hilin";
57 
58 hilinit()
59 {
60   	register struct hilloop *hilp = &hil0;	/* XXX */
61 	register int i;
62 
63 	/*
64 	 * Initialize loop information
65 	 */
66 	hilp->hl_addr = HILADDR;
67 	hilp->hl_cmdending = FALSE;
68 	hilp->hl_actdev = hilp->hl_cmddev = 0;
69 	hilp->hl_cmddone = FALSE;
70 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
71 	hilp->hl_pollbp = hilp->hl_pollbuf;
72 	hilp->hl_kbddev = 0;
73 	hilp->hl_kbdlang = KBD_DEFAULT;
74 	hilp->hl_kbdflags = 0;
75 	/*
76 	 * Clear all queues and device associations with queues
77 	 */
78 	for (i = 0; i < NHILQ; i++) {
79 		hilp->hl_queue[i].hq_eventqueue = NULL;
80 		hilp->hl_queue[i].hq_procp = NULL;
81 		hilp->hl_queue[i].hq_devmask = 0;
82 	}
83 	for (i = 0; i < NHILD; i++)
84 		hilp->hl_device[i].hd_qmask = 0;
85 	hilp->hl_device[HILLOOPDEV].hd_flags = (HIL_ALIVE|HIL_PSEUDO);
86 	/*
87 	 * Reset the loop hardware, and collect keyboard/id info
88 	 */
89 	hilreset(hilp);
90 	hilinfo(hilp);
91 	kbdenable();
92 }
93 
94 /* ARGSUSED */
95 hilopen(dev, flags, mode, p)
96 	dev_t dev;
97 	int flags, mode;
98 	struct proc *p;
99 {
100   	register struct hilloop *hilp = &hil0;	/* XXX */
101 	register struct hilloopdev *dptr;
102 	u_char device = HILUNIT(dev);
103 
104 #ifdef DEBUG
105 	if (hildebug & HDB_FOLLOW)
106 		printf("hilopen(%d): device %x\n", p->p_pid, device);
107 #endif
108 
109 	if ((hilp->hl_device[HILLOOPDEV].hd_flags & HIL_ALIVE) == 0)
110 		return(ENXIO);
111 
112 	dptr = &hilp->hl_device[device];
113 	if ((dptr->hd_flags & HIL_ALIVE) == 0)
114 		return(ENODEV);
115 
116 	/*
117 	 * Pseudo-devices cannot be read, nothing more to do.
118 	 */
119 	if (dptr->hd_flags & HIL_PSEUDO)
120 		return(0);
121 
122 	/*
123 	 * Open semantics:
124 	 * 1.	Open devices have only one of HIL_READIN/HIL_QUEUEIN.
125 	 * 2.	HPUX processes always get read syscall interface and
126 	 *	must have exclusive use of the device.
127 	 * 3.	BSD processes default to shared queue interface.
128 	 *	Multiple processes can open the device.
129 	 */
130 	if (p->p_flag & SHPUX) {
131 		if (dptr->hd_flags & (HIL_READIN|HIL_QUEUEIN))
132 			return(EBUSY);
133 		dptr->hd_flags |= HIL_READIN;
134 	} else {
135 		if (dptr->hd_flags & HIL_READIN)
136 			return(EBUSY);
137 		dptr->hd_flags |= HIL_QUEUEIN;
138 	}
139 	if (flags & FNONBLOCK)
140 		dptr->hd_flags |= HIL_NOBLOCK;
141 	/*
142 	 * It is safe to flush the read buffer as we are guarenteed
143 	 * that no one else is using it.
144 	 */
145 	ndflush(&dptr->hd_queue, dptr->hd_queue.c_cc);
146 
147 	send_hil_cmd(hilp->hl_addr, HIL_INTON, NULL, 0, NULL);
148 	/*
149 	 * Opened the keyboard, put in raw mode.
150 	 */
151 	(void) splhil();
152 	if (device == hilp->hl_kbddev) {
153 		u_char mask = 0;
154 		send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &mask, 1, NULL);
155 		hilp->hl_kbdflags |= KBD_RAW;
156 #ifdef DEBUG
157 		if (hildebug & HDB_KEYBOARD)
158 			printf("hilopen: keyboard %d raw\n", hilp->hl_kbddev);
159 #endif
160 	}
161 	(void) spl0();
162 	return (0);
163 }
164 
165 /* ARGSUSED */
166 hilclose(dev, flags)
167 	dev_t dev;
168 {
169 	struct proc *p = curproc;		/* XXX */
170   	register struct hilloop *hilp = &hil0;	/* XXX */
171 	register struct hilloopdev *dptr;
172 	register int i;
173 	u_char device = HILUNIT(dev);
174 	char mask, lpctrl;
175 
176 #ifdef DEBUG
177 	if (hildebug & HDB_FOLLOW)
178 		printf("hilclose(%d): device %x\n", p->p_pid, device);
179 #endif
180 
181 	dptr = &hilp->hl_device[device];
182 	if (device && (dptr->hd_flags & HIL_PSEUDO))
183 		return (0);
184 
185 	if ((p->p_flag & SHPUX) == 0) {
186 		/*
187 		 * If this is the loop device,
188 		 * free up all queues belonging to this process.
189 		 */
190 		if (device == 0) {
191 			for (i = 0; i < NHILQ; i++)
192 				if (hilp->hl_queue[i].hq_procp == p)
193 					(void) hilqfree(i);
194 		} else {
195 			mask = ~hildevmask(device);
196 			(void) splhil();
197 			for (i = 0; i < NHILQ; i++)
198 				if (hilp->hl_queue[i].hq_procp == p) {
199 					dptr->hd_qmask &= ~hilqmask(i);
200 					hilp->hl_queue[i].hq_devmask &= mask;
201 				}
202 			(void) spl0();
203 		}
204 	}
205 	/*
206 	 * Always flush the read buffer
207 	 */
208 	dptr->hd_flags &= ~(HIL_QUEUEIN|HIL_READIN|HIL_NOBLOCK);
209 	ndflush(&dptr->hd_queue, dptr->hd_queue.c_cc);
210 	/*
211 	 * Set keyboard back to cooked mode when closed.
212 	 */
213 	(void) splhil();
214 	if (device && device == hilp->hl_kbddev) {
215 		mask = 1 << (hilp->hl_kbddev - 1);
216 		send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &mask, 1, NULL);
217 		hilp->hl_kbdflags &= ~(KBD_RAW|KBD_AR1|KBD_AR2);
218 		/*
219 		 * XXX: We have had trouble with keyboards remaining raw
220 		 * after close due to the LPC_KBDCOOK bit getting cleared
221 		 * somewhere along the line.  Hence we check and reset
222 		 * LPCTRL if necessary.
223 		 */
224 		send_hil_cmd(hilp->hl_addr, HIL_READLPCTRL, NULL, 0, &lpctrl);
225 		if ((lpctrl & LPC_KBDCOOK) == 0) {
226 			printf("hilclose: bad LPCTRL %x, reset to %x\n",
227 			       lpctrl, lpctrl|LPC_KBDCOOK);
228 			lpctrl |= LPC_KBDCOOK;
229 			send_hil_cmd(hilp->hl_addr, HIL_WRITELPCTRL,
230 					&lpctrl, 1, NULL);
231 		}
232 #ifdef DEBUG
233 		if (hildebug & HDB_KEYBOARD)
234 			printf("hilclose: keyboard %d cooked\n",
235 			       hilp->hl_kbddev);
236 #endif
237 		kbdenable();
238 	}
239 	(void) spl0();
240 	return (0);
241 }
242 
243 /*
244  * Read interface to HIL device.
245  */
246 hilread(dev, uio)
247 	dev_t dev;
248 	register struct uio *uio;
249 {
250 	struct hilloop *hilp = &hil0;		/* XXX */
251 	register struct hilloopdev *dptr;
252 	register int cc;
253 	u_char device = HILUNIT(dev);
254 	char buf[HILBUFSIZE];
255 	int error;
256 
257 #if 0
258 	/*
259 	 * XXX: Don't do this since HP-UX doesn't.
260 	 *
261 	 * Check device number.
262 	 * This check is necessary since loop can reconfigure.
263 	 */
264 	if (device > hilp->hl_maxdev)
265 		return(ENODEV);
266 #endif
267 
268 	dptr = &hilp->hl_device[device];
269 	if ((dptr->hd_flags & HIL_READIN) == 0)
270 		return(ENODEV);
271 
272 	(void) splhil();
273 	while (dptr->hd_queue.c_cc == 0) {
274 		if (dptr->hd_flags & HIL_NOBLOCK) {
275 			spl0();
276 			return(EWOULDBLOCK);
277 		}
278 		dptr->hd_flags |= HIL_ASLEEP;
279 		if (error = tsleep((caddr_t)dptr, TTIPRI | PCATCH, hilin, 0)) {
280 			(void) spl0();
281 			return (error);
282 		}
283 	}
284 	(void) spl0();
285 
286 	error = 0;
287 	while (uio->uio_resid > 0 && error == 0) {
288 		cc = hilq_to_b(&dptr->hd_queue, buf,
289 			       MIN(uio->uio_resid, HILBUFSIZE));
290 		if (cc <= 0)
291 			break;
292 		error = uiomove(buf, cc, uio);
293 	}
294 	return(error);
295 }
296 
297 hilioctl(dev, cmd, data, flag, p)
298 	dev_t dev;
299 	caddr_t data;
300 	struct proc *p;
301 {
302 	register struct hilloop *hilp = &hil0;	/* XXX */
303 	char device = HILUNIT(dev);
304 	struct hilloopdev *dptr;
305 	register int i;
306 	u_char hold;
307 	int error;
308 
309 #ifdef DEBUG
310 	if (hildebug & HDB_FOLLOW)
311 		printf("hilioctl(%d): dev %x cmd %x\n",
312 		       p->p_pid, device, cmd);
313 #endif
314 
315 	dptr = &hilp->hl_device[device];
316 	if ((dptr->hd_flags & HIL_ALIVE) == 0)
317 		return (ENODEV);
318 
319 	/*
320 	 * Don't allow hardware ioctls on virtual devices.
321 	 * Note that though these are the BSD names, they have the same
322 	 * values as the HP-UX equivalents so we catch them as well.
323 	 */
324 	if (dptr->hd_flags & HIL_PSEUDO) {
325 		switch (cmd) {
326 		case HILIOCSC:
327 		case HILIOCID:
328 		case HILIOCRN:
329 		case HILIOCRS:
330 		case HILIOCED:
331 			return(ENODEV);
332 
333 		/*
334 		 * XXX: should also return ENODEV but HP-UX compat
335 		 * breaks if we do.  They work ok right now because
336 		 * we only recognize one keyboard on the loop.  This
337 		 * will have to change if we remove that restriction.
338 		 */
339 		case HILIOCAROFF:
340 		case HILIOCAR1:
341 		case HILIOCAR2:
342 			break;
343 
344 		default:
345 			break;
346 		}
347 	}
348 
349 #ifdef HPUXCOMPAT
350 	if (p->p_flag & SHPUX)
351 		return(hpuxhilioctl(dev, cmd, data, flag));
352 #endif
353 
354 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
355 	bzero((caddr_t)hilp->hl_cmdbuf, HILBUFSIZE);
356 	hilp->hl_cmddev = device;
357 	error = 0;
358 	switch (cmd) {
359 
360 	case HILIOCSBP:
361 		/* Send four data bytes to the tone gererator. */
362 		send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL);
363 		/* Send the trigger beeper command to the 8042. */
364 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
365 		break;
366 
367 	case HILIOCRRT:
368 		/* Transfer the real time to the 8042 data buffer */
369 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
370 		/* Read each byte of the real time */
371 		for (i = 0; i < 5; i++) {
372 			send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL,
373 					0, &hold);
374 			data[4-i] = hold;
375 		}
376 		break;
377 
378 	case HILIOCRT:
379 		for (i = 0; i < 4; i++) {
380 			send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i,
381 					NULL, 0, &hold);
382 			data[i] = hold;
383 		}
384 		break;
385 
386 	case HILIOCID:
387 	case HILIOCSC:
388 	case HILIOCRN:
389 	case HILIOCRS:
390 	case HILIOCED:
391 	  	send_hildev_cmd(hilp, device, (cmd & 0xFF));
392 		bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf);
393 	  	break;
394 
395         case HILIOCAROFF:
396         case HILIOCAR1:
397         case HILIOCAR2:
398 		if (hilp->hl_kbddev) {
399 			hilp->hl_cmddev = hilp->hl_kbddev;
400 			send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF));
401 			hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2);
402 			if (cmd == HILIOCAR1)
403 				hilp->hl_kbdflags |= KBD_AR1;
404 			else if (cmd == HILIOCAR2)
405 				hilp->hl_kbdflags |= KBD_AR2;
406 		}
407 		break;
408 
409 	case HILIOCBEEP:
410 		hilbeep(hilp, (struct _hilbell *)data);
411 		break;
412 
413 	case FIONBIO:
414 		dptr = &hilp->hl_device[device];
415 		if (*(int *)data)
416 			dptr->hd_flags |= HIL_NOBLOCK;
417 		else
418 			dptr->hd_flags &= ~HIL_NOBLOCK;
419 		break;
420 
421 	/*
422 	 * FIOASYNC must be present for FIONBIO above to work!
423 	 * (See fcntl in kern_descrip.c).
424 	 */
425 	case FIOASYNC:
426 		break;
427 
428         case HILIOCALLOCQ:
429 		error = hilqalloc((struct hilqinfo *)data);
430 		break;
431 
432         case HILIOCFREEQ:
433 		error = hilqfree(((struct hilqinfo *)data)->qid);
434 		break;
435 
436         case HILIOCMAPQ:
437 		error = hilqmap(*(int *)data, device);
438 		break;
439 
440         case HILIOCUNMAPQ:
441 		error = hilqunmap(*(int *)data, device);
442 		break;
443 
444 	case HILIOCHPUX:
445 		dptr = &hilp->hl_device[device];
446 		dptr->hd_flags |= HIL_READIN;
447 		dptr->hd_flags &= ~HIL_QUEUEIN;
448 		break;
449 
450         case HILIOCRESET:
451 	        hilreset(hilp);
452 		break;
453 
454 #ifdef DEBUG
455         case HILIOCTEST:
456 		hildebug = *(int *) data;
457 		break;
458 #endif
459 
460         default:
461 		error = EINVAL;
462 		break;
463 
464 	}
465 	hilp->hl_cmddev = 0;
466 	return(error);
467 }
468 
469 #ifdef HPUXCOMPAT
470 /* ARGSUSED */
471 hpuxhilioctl(dev, cmd, data, flag)
472 	dev_t dev;
473 	caddr_t data;
474 {
475 	register struct hilloop *hilp = &hil0;	/* XXX */
476 	char device = HILUNIT(dev);
477 	struct hilloopdev *dptr;
478 	register int i;
479 	u_char hold;
480 
481 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
482 	bzero((caddr_t)hilp->hl_cmdbuf, HILBUFSIZE);
483 	hilp->hl_cmddev = device;
484 	switch (cmd) {
485 
486 	case HILSC:
487 	case HILID:
488 	case HILRN:
489 	case HILRS:
490 	case HILED:
491 	case HILP1:
492 	case HILP2:
493 	case HILP3:
494 	case HILP4:
495 	case HILP5:
496 	case HILP6:
497 	case HILP7:
498 	case HILP:
499 	case HILA1:
500 	case HILA2:
501 	case HILA3:
502 	case HILA4:
503 	case HILA5:
504 	case HILA6:
505 	case HILA7:
506 	case HILA:
507 		send_hildev_cmd(hilp, device, (cmd & 0xFF));
508 		bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf);
509 	  	break;
510 
511         case HILDKR:
512         case HILER1:
513         case HILER2:
514 		if (hilp->hl_kbddev) {
515 			hilp->hl_cmddev = hilp->hl_kbddev;
516 			send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF));
517 			hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2);
518 			if (cmd == HILIOCAR1)
519 				hilp->hl_kbdflags |= KBD_AR1;
520 			else if (cmd == HILIOCAR2)
521 				hilp->hl_kbdflags |= KBD_AR2;
522 		}
523 		break;
524 
525 	case EFTSBP:
526 		/* Send four data bytes to the tone gererator. */
527 		send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL);
528 		/* Send the trigger beeper command to the 8042. */
529 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
530 		break;
531 
532 	case EFTRRT:
533 		/* Transfer the real time to the 8042 data buffer */
534 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
535 		/* Read each byte of the real time */
536 		for (i = 0; i < 5; i++) {
537 			send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL,
538 					0, &hold);
539 			data[4-i] = hold;
540 		}
541 		break;
542 
543 	case EFTRT:
544 		for (i = 0; i < 4; i++) {
545 			send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i,
546 					NULL, 0, &hold);
547 			data[i] = hold;
548 		}
549 		break;
550 
551         case EFTRLC:
552         case EFTRCC:
553 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, &hold);
554 		*data = hold;
555 		break;
556 
557         case EFTSRPG:
558         case EFTSRD:
559         case EFTSRR:
560 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), data, 1, NULL);
561 		break;
562 
563 	case EFTSBI:
564 		hilbeep(hilp, (struct _hilbell *)data);
565 		break;
566 
567 	case FIONBIO:
568 		dptr = &hilp->hl_device[device];
569 		if (*(int *)data)
570 			dptr->hd_flags |= HIL_NOBLOCK;
571 		else
572 			dptr->hd_flags &= ~HIL_NOBLOCK;
573 		break;
574 
575 	case FIOASYNC:
576 		break;
577 
578         default:
579 		hilp->hl_cmddev = 0;
580 		return(EINVAL);
581 	}
582 	hilp->hl_cmddev = 0;
583 	return(0);
584 }
585 #endif
586 
587 /*
588  * XXX: the mmap interface for HIL devices should be rethought.
589  * We used it only briefly in conjuntion with shared queues
590  * (instead of HILIOCMAPQ ioctl).  Perhaps mmap()ing a device
591  * should give a single queue per process.
592  */
593 /* ARGSUSED */
594 hilmap(dev, off, prot)
595 	dev_t dev;
596 	register int off;
597 {
598 #ifdef MMAP
599 	struct proc *p = curproc;		/* XXX */
600 	register struct hilloop *hilp = &hil0;	/* XXX */
601 	register struct hiliqueue *qp;
602 	register int qnum;
603 
604 	/*
605 	 * Only allow mmap() on loop device
606 	 */
607 	if (HILUNIT(dev) != 0 || off >= NHILQ*sizeof(HILQ))
608 		return(-1);
609 	/*
610 	 * Determine which queue we want based on the offset.
611 	 * Queue must belong to calling process.
612 	 */
613 	qp = &hilp->hl_queue[off / sizeof(HILQ)];
614 	if (qp->hq_procp != p)
615 		return(-1);
616 
617 	off %= sizeof(HILQ);
618 	return(kvtop((u_int)qp->hq_eventqueue + off) >> PGSHIFT);
619 #endif
620 }
621 
622 /*ARGSUSED*/
623 hilselect(dev, rw, p)
624 	dev_t dev;
625 	struct proc *p;
626 {
627 	register struct hilloop *hilp = &hil0;	/* XXX */
628 	register struct hilloopdev *dptr;
629 	register struct hiliqueue *qp;
630 	register int mask;
631 	int s, device;
632 
633 	if (rw == FWRITE)
634 		return (1);
635 	device = HILUNIT(dev);
636 
637 	/*
638 	 * Read interface.
639 	 * Return 1 if there is something in the queue, 0 ow.
640 	 */
641 	dptr = &hilp->hl_device[device];
642 	if (dptr->hd_flags & HIL_READIN) {
643 		s = splhil();
644 		if (dptr->hd_queue.c_cc) {
645 			splx(s);
646 			return (1);
647 		}
648 		if (dptr->hd_selr &&
649 		    dptr->hd_selr->p_wchan == (caddr_t)&selwait)
650 			dptr->hd_flags |= HIL_SELCOLL;
651 		else
652 			dptr->hd_selr = p;
653 		splx(s);
654 		return (0);
655 	}
656 
657 	/*
658 	 * Make sure device is alive and real (or the loop device).
659 	 * Note that we do not do this for the read interface.
660 	 * This is primarily to be consistant with HP-UX.
661 	 */
662 	if (device && (dptr->hd_flags & (HIL_ALIVE|HIL_PSEUDO)) != HIL_ALIVE)
663 		return (1);
664 
665 	/*
666 	 * Select on loop device is special.
667 	 * Check to see if there are any data for any loop device
668 	 * provided it is associated with a queue belonging to this user.
669 	 */
670 	if (device == 0)
671 		mask = -1;
672 	else
673 		mask = hildevmask(device);
674 	/*
675 	 * Must check everybody with interrupts blocked to prevent races.
676 	 */
677 	s = splhil();
678 	for (qp = hilp->hl_queue; qp < &hilp->hl_queue[NHILQ]; qp++)
679 		if (qp->hq_procp == p && (mask & qp->hq_devmask) &&
680 		    qp->hq_eventqueue->hil_evqueue.head !=
681 		    qp->hq_eventqueue->hil_evqueue.tail) {
682 			splx(s);
683 			return (1);
684 		}
685 
686 	if (dptr->hd_selr && dptr->hd_selr->p_wchan == (caddr_t)&selwait)
687 		dptr->hd_flags |= HIL_SELCOLL;
688 	else
689 		dptr->hd_selr = p;
690 	splx(s);
691 	return (0);
692 }
693 
694 hilint()
695 {
696 	struct hilloop *hilp = &hil0;		/* XXX */
697 	register struct hil_dev *hildevice = hilp->hl_addr;
698 	u_char c, stat;
699 
700 	stat = hildevice->hil_stat;
701 	c = hildevice->hil_data;		/* clears interrupt */
702 	hil_process_int(stat, c);
703 }
704 
705 #include "ite.h"
706 
707 hil_process_int(stat, c)
708 	register u_char stat, c;
709 {
710   	register struct hilloop *hilp;
711 
712 #ifdef DEBUG
713 	if (hildebug & HDB_EVENTS)
714 		printf("hilint: %x %x\n", stat, c);
715 #endif
716 
717 	/* the shift enables the compiler to generate a jump table */
718 	switch ((stat>>HIL_SSHIFT) & HIL_SMASK) {
719 
720 #if NITE > 0
721 	case HIL_KEY:
722 	case HIL_SHIFT:
723 	case HIL_CTRL:
724 	case HIL_CTRLSHIFT:
725 		itefilter(stat, c);
726 		return;
727 #endif
728 
729 	case HIL_STATUS:			/* The status info. */
730 		hilp = &hil0;			/* XXX */
731 		if (c & HIL_ERROR) {
732 		  	hilp->hl_cmddone = TRUE;
733 			if (c == HIL_RECONFIG)
734 				hilconfig(hilp);
735 			break;
736 		}
737 		if (c & HIL_COMMAND) {
738 		  	if (c & HIL_POLLDATA)	/* End of data */
739 				hilevent(hilp);
740 			else			/* End of command */
741 			  	hilp->hl_cmdending = TRUE;
742 			hilp->hl_actdev = 0;
743 		} else {
744 		  	if (c & HIL_POLLDATA) {	/* Start of polled data */
745 			  	if (hilp->hl_actdev != 0)
746 					hilevent(hilp);
747 				hilp->hl_actdev = (c & HIL_DEVMASK);
748 				hilp->hl_pollbp = hilp->hl_pollbuf;
749 			} else {		/* Start of command */
750 				if (hilp->hl_cmddev == (c & HIL_DEVMASK)) {
751 					hilp->hl_cmdbp = hilp->hl_cmdbuf;
752 					hilp->hl_actdev = 0;
753 				}
754 			}
755 		}
756 	        return;
757 
758 	case HIL_DATA:
759 		hilp = &hil0;			/* XXX */
760 		if (hilp->hl_actdev != 0)	/* Collecting poll data */
761 			*hilp->hl_pollbp++ = c;
762 		else if (hilp->hl_cmddev != 0)  /* Collecting cmd data */
763 			if (hilp->hl_cmdending) {
764 				hilp->hl_cmddone = TRUE;
765 				hilp->hl_cmdending = FALSE;
766 			} else
767 				*hilp->hl_cmdbp++ = c;
768 		return;
769 
770 	case 0:		/* force full jump table */
771 	default:
772 		return;
773 	}
774 }
775 
776 #if defined(DEBUG) && !defined(PANICBUTTON)
777 #define PANICBUTTON
778 #endif
779 
780 /*
781  * Optimized macro to compute:
782  *	eq->head == (eq->tail + 1) % eq->size
783  * i.e. has tail caught up with head.  We do this because 32 bit long
784  * remaidering is expensive (a function call with our compiler).
785  */
786 #define HQFULL(eq)	(((eq)->head?(eq)->head:(eq)->size) == (eq)->tail+1)
787 #define HQVALID(eq) \
788 	((eq)->size == HEVQSIZE && (eq)->tail >= 0 && (eq)->tail < HEVQSIZE)
789 
790 hilevent(hilp)
791 	struct hilloop *hilp;
792 {
793 	register struct hilloopdev *dptr = &hilp->hl_device[hilp->hl_actdev];
794 	register int len, mask, qnum;
795 	register u_char *cp, *pp;
796 	register HILQ *hq;
797 	struct timeval ourtime;
798 	hil_packet *proto;
799 	int s, len0;
800 	long tenths;
801 
802 #ifdef PANICBUTTON
803 	static int first;
804 	extern int panicbutton;
805 
806 	cp = hilp->hl_pollbuf;
807 	if (panicbutton && (*cp & HIL_KBDDATA)) {
808 		if (*++cp == 0x4E)
809 			first = 1;
810 		else if (first && *cp == 0x46 && !panicstr)
811 			panic("are we having fun yet?");
812 		else
813 			first = 0;
814 	}
815 #endif
816 #ifdef DEBUG
817 	if (hildebug & HDB_EVENTS) {
818 		printf("hilevent: dev %d pollbuf: ", hilp->hl_actdev);
819 		printhilpollbuf(hilp);
820 		printf("\n");
821 	}
822 #endif
823 
824 	/*
825 	 * Note that HIL_READIN effectively "shuts off" any queues
826 	 * that may have been in use at the time of an HILIOCHPUX call.
827 	 */
828 	if (dptr->hd_flags & HIL_READIN) {
829 		hpuxhilevent(hilp, dptr);
830 		return;
831 	}
832 
833 	/*
834 	 * If this device isn't on any queue or there are no data
835 	 * in the packet (can this happen?) do nothing.
836 	 */
837 	if (dptr->hd_qmask == 0 ||
838 	    (len0 = hilp->hl_pollbp - hilp->hl_pollbuf) <= 0)
839 		return;
840 
841 	/*
842 	 * Everybody gets the same time stamp
843 	 */
844 	s = splclock();
845 	ourtime = time;
846 	splx(s);
847 	tenths = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000);
848 
849 	proto = NULL;
850 	mask = dptr->hd_qmask;
851 	for (qnum = 0; mask; qnum++) {
852 		if ((mask & hilqmask(qnum)) == 0)
853 			continue;
854 		mask &= ~hilqmask(qnum);
855 		hq = hilp->hl_queue[qnum].hq_eventqueue;
856 
857 		/*
858 		 * Ensure that queue fields that we rely on are valid
859 		 * and that there is space in the queue.  If either
860 		 * test fails, we just skip this queue.
861 		 */
862 		if (!HQVALID(&hq->hil_evqueue) || HQFULL(&hq->hil_evqueue))
863 			continue;
864 
865 		/*
866 		 * Copy data to queue.
867 		 * If this is the first queue we construct the packet
868 		 * with length, timestamp and poll buffer data.
869 		 * For second and sucessive packets we just duplicate
870 		 * the first packet.
871 		 */
872 		pp = (u_char *) &hq->hil_event[hq->hil_evqueue.tail];
873 		if (proto == NULL) {
874 			proto = (hil_packet *)pp;
875 			cp = hilp->hl_pollbuf;
876 			len = len0;
877 			*pp++ = len + 6;
878 			*pp++ = hilp->hl_actdev;
879 			*(long *)pp = tenths;
880 			pp += sizeof(long);
881 			do *pp++ = *cp++; while (--len);
882 		} else
883 			*(hil_packet *)pp = *proto;
884 
885 		if (++hq->hil_evqueue.tail == hq->hil_evqueue.size)
886 			hq->hil_evqueue.tail = 0;
887 	}
888 
889 	/*
890 	 * Wake up anyone selecting on this device or the loop itself
891 	 */
892 	if (dptr->hd_selr) {
893 		selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
894 		dptr->hd_selr = NULL;
895 		dptr->hd_flags &= ~HIL_SELCOLL;
896 	}
897 	dptr = &hilp->hl_device[HILLOOPDEV];
898 	if (dptr->hd_selr) {
899 		selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
900 		dptr->hd_selr = NULL;
901 		dptr->hd_flags &= ~HIL_SELCOLL;
902 	}
903 }
904 
905 #undef HQFULL
906 
907 hpuxhilevent(hilp, dptr)
908 	register struct hilloop *hilp;
909 	register struct hilloopdev *dptr;
910 {
911 	register int len;
912 	struct timeval ourtime;
913 	long tstamp;
914 	int s;
915 
916 	/*
917 	 * Everybody gets the same time stamp
918 	 */
919 	s = splclock();
920 	ourtime = time;
921 	splx(s);
922 	tstamp = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000);
923 
924 	/*
925 	 * Each packet that goes into the buffer must be preceded by the
926 	 * number of bytes in the packet, and the timestamp of the packet.
927 	 * This adds 5 bytes to the packet size. Make sure there is enough
928 	 * room in the buffer for it, and if not, toss the packet.
929 	 */
930 	len = hilp->hl_pollbp - hilp->hl_pollbuf;
931 	if (dptr->hd_queue.c_cc <= (HILMAXCLIST - (len+5))) {
932 		putc(len+5, &dptr->hd_queue);
933 		(void) b_to_q((char *)&tstamp, sizeof tstamp, &dptr->hd_queue);
934 		(void) b_to_q((char *)hilp->hl_pollbuf, len, &dptr->hd_queue);
935 	}
936 
937 	/*
938 	 * Wake up any one blocked on a read or select
939 	 */
940 	if (dptr->hd_flags & HIL_ASLEEP) {
941 		dptr->hd_flags &= ~HIL_ASLEEP;
942 		wakeup((caddr_t)dptr);
943 	}
944 	if (dptr->hd_selr) {
945 		selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL);
946 		dptr->hd_selr = NULL;
947 		dptr->hd_flags &= ~HIL_SELCOLL;
948 	}
949 }
950 
951 /*
952  * Shared queue manipulation routines
953  */
954 
955 hilqalloc(qip)
956 	struct hilqinfo *qip;
957 {
958 	struct proc *p = curproc;		/* XXX */
959 
960 #ifdef DEBUG
961 	if (hildebug & HDB_FOLLOW)
962 		printf("hilqalloc(%d): addr %x\n", p->p_pid, qip->addr);
963 #endif
964 	return(EINVAL);
965 }
966 
967 hilqfree(qnum)
968 	register int qnum;
969 {
970 	struct proc *p = curproc;		/* XXX */
971 
972 #ifdef DEBUG
973 	if (hildebug & HDB_FOLLOW)
974 		printf("hilqfree(%d): qnum %d\n", p->p_pid, qnum);
975 #endif
976 	return(EINVAL);
977 }
978 
979 hilqmap(qnum, device)
980 	register int qnum, device;
981 {
982 	struct proc *p = curproc;		/* XXX */
983 	register struct hilloop *hilp = &hil0;	/* XXX */
984 	register struct hilloopdev *dptr = &hilp->hl_device[device];
985 	int s;
986 
987 #ifdef DEBUG
988 	if (hildebug & HDB_FOLLOW)
989 		printf("hilqmap(%d): qnum %d device %x\n",
990 		       p->p_pid, qnum, device);
991 #endif
992 	if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != p)
993 		return(EINVAL);
994 	if ((dptr->hd_flags & HIL_QUEUEIN) == 0)
995 		return(EINVAL);
996 	if (dptr->hd_qmask && p->p_ucred->cr_uid &&
997 	    p->p_ucred->cr_uid != dptr->hd_uid)
998 		return(EPERM);
999 
1000 	hilp->hl_queue[qnum].hq_devmask |= hildevmask(device);
1001 	if (dptr->hd_qmask == 0)
1002 		dptr->hd_uid = p->p_ucred->cr_uid;
1003 	s = splhil();
1004 	dptr->hd_qmask |= hilqmask(qnum);
1005 	splx(s);
1006 #ifdef DEBUG
1007 	if (hildebug & HDB_MASK)
1008 		printf("hilqmap(%d): devmask %x qmask %x\n",
1009 		       p->p_pid, hilp->hl_queue[qnum].hq_devmask,
1010 		       dptr->hd_qmask);
1011 #endif
1012 	return(0);
1013 }
1014 
1015 hilqunmap(qnum, device)
1016 	register int qnum, device;
1017 {
1018 	struct proc *p = curproc;		/* XXX */
1019 	register struct hilloop *hilp = &hil0;	/* XXX */
1020 	int s;
1021 
1022 #ifdef DEBUG
1023 	if (hildebug & HDB_FOLLOW)
1024 		printf("hilqunmap(%d): qnum %d device %x\n",
1025 		       p->p_pid, qnum, device);
1026 #endif
1027 
1028 	if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != p)
1029 		return(EINVAL);
1030 
1031 	hilp->hl_queue[qnum].hq_devmask &= ~hildevmask(device);
1032 	s = splhil();
1033 	hilp->hl_device[device].hd_qmask &= ~hilqmask(qnum);
1034 	splx(s);
1035 #ifdef DEBUG
1036 	if (hildebug & HDB_MASK)
1037 		printf("hilqunmap(%d): devmask %x qmask %x\n",
1038 		       p->p_pid, hilp->hl_queue[qnum].hq_devmask,
1039 		       hilp->hl_device[device].hd_qmask);
1040 #endif
1041 	return(0);
1042 }
1043 
1044 #include "sys/clist.h"
1045 
1046 /*
1047  * This is just a copy of the virgin q_to_b routine with minor
1048  * optimizations for HIL use.  It is used because we don't have
1049  * to raise the priority to spltty() for most of the clist manipulations.
1050  */
1051 hilq_to_b(q, cp, cc)
1052 	register struct clist *q;
1053 	register char *cp;
1054 {
1055 
1056 	BODY_DELETED
1057 }
1058 
1059 /*
1060  * Cooked keyboard functions for ite driver.
1061  * There is only one "cooked" ITE keyboard (the first keyboard found)
1062  * per loop.  There may be other keyboards, but they will always be "raw".
1063  */
1064 
1065 kbdbell()
1066 {
1067 	struct hilloop *hilp = &hil0;		/* XXX */
1068 
1069 	hilbeep(hilp, &default_bell);
1070 }
1071 
1072 kbdenable()
1073 {
1074 	struct hilloop *hilp = &hil0;	/* XXX */
1075 	register struct hil_dev *hildevice = hilp->hl_addr;
1076 	char db;
1077 
1078 	/* Set the autorepeat rate register */
1079 	db = ar_format(KBD_ARR);
1080 	send_hil_cmd(hildevice, HIL_SETARR, &db, 1, NULL);
1081 
1082 	/* Set the autorepeat delay register */
1083 	db = ar_format(KBD_ARD);
1084 	send_hil_cmd(hildevice, HIL_SETARD, &db, 1, NULL);
1085 
1086 	/* Enable interrupts */
1087 	send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
1088 }
1089 
1090 kbddisable()
1091 {
1092 }
1093 
1094 /*
1095  * XXX: read keyboard directly and return code.
1096  * Used by console getchar routine.  Could really screw up anybody
1097  * reading from the keyboard in the normal, interrupt driven fashion.
1098  */
1099 kbdgetc(statp)
1100 	int *statp;
1101 {
1102 	struct hilloop *hilp = &hil0;		/* XXX */
1103 	register struct hil_dev *hildevice = hilp->hl_addr;
1104 	register int c, stat;
1105 	int s;
1106 
1107 	s = splhil();
1108 	while (((stat = hildevice->hil_stat) & HIL_DATA_RDY) == 0)
1109 		;
1110 	c = hildevice->hil_data;
1111 	splx(s);
1112 	*statp = stat;
1113 	return(c);
1114 }
1115 
1116 /*
1117  * Recoginize and clear keyboard generated NMIs.
1118  * Returns 1 if it was ours, 0 otherwise.  Note that we cannot use
1119  * send_hil_cmd() to issue the clear NMI command as that would actually
1120  * lower the priority to splimp() and it doesn't wait for the completion
1121  * of the command.  Either of these conditions could result in the
1122  * interrupt reoccuring.  Note that we issue the CNMT command twice.
1123  * This seems to be needed, once is not always enough!?!
1124  */
1125 kbdnmi()
1126 {
1127 	register struct hilloop *hilp = &hil0;		/* XXX */
1128 
1129 	if ((*KBDNMISTAT & KBDNMI) == 0)
1130 		return(0);
1131 	HILWAIT(hilp->hl_addr);
1132 	hilp->hl_addr->hil_cmd = HIL_CNMT;
1133 	HILWAIT(hilp->hl_addr);
1134 	hilp->hl_addr->hil_cmd = HIL_CNMT;
1135 	HILWAIT(hilp->hl_addr);
1136 	return(1);
1137 }
1138 
1139 #define HILSECURITY	0x33
1140 #define HILIDENTIFY	0x03
1141 #define HILSCBIT	0x04
1142 
1143 /*
1144  * Called at boot time to print out info about interesting devices
1145  */
1146 hilinfo(hilp)
1147 	register struct hilloop *hilp;
1148 {
1149 	register int id, len;
1150 	register struct kbdmap *km;
1151 
1152 	/*
1153 	 * Keyboard info.
1154 	 */
1155 	if (hilp->hl_kbddev) {
1156 		printf("hil%d: ", hilp->hl_kbddev);
1157 		for (km = kbd_map; km->kbd_code; km++)
1158 			if (km->kbd_code == hilp->hl_kbdlang) {
1159 				printf("%s ", km->kbd_desc);
1160 				break;
1161 			}
1162 		printf("keyboard\n");
1163 	}
1164 	/*
1165 	 * ID module.
1166 	 * Attempt to locate the first ID module and print out its
1167 	 * security code.  Is this a good idea??
1168 	 */
1169 	id = hiliddev(hilp);
1170 	if (id) {
1171 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1172 		hilp->hl_cmddev = id;
1173 		send_hildev_cmd(hilp, id, HILSECURITY);
1174 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
1175 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1176 		hilp->hl_cmddev = 0;
1177 		printf("hil%d: security code", id);
1178 		for (id = 0; id < len; id++)
1179 			printf(" %x", hilp->hl_cmdbuf[id]);
1180 		while (id++ < 16)
1181 			printf(" 0");
1182 		printf("\n");
1183 	}
1184 }
1185 
1186 #define HILAR1	0x3E
1187 #define HILAR2	0x3F
1188 
1189 /*
1190  * Called after the loop has reconfigured.  Here we need to:
1191  *	- determine how many devices are on the loop
1192  *	  (some may have been added or removed)
1193  *	- locate the ITE keyboard (if any) and ensure
1194  *	  that it is in the proper state (raw or cooked)
1195  *	  and is set to use the proper language mapping table
1196  *	- ensure all other keyboards are raw
1197  * Note that our device state is now potentially invalid as
1198  * devices may no longer be where they were.  What we should
1199  * do here is either track where the devices went and move
1200  * state around accordingly or, more simply, just mark all
1201  * devices as HIL_DERROR and don't allow any further use until
1202  * they are closed.  This is a little too brutal for my tastes,
1203  * we prefer to just assume people won't move things around.
1204  */
1205 hilconfig(hilp)
1206 	register struct hilloop *hilp;
1207 {
1208 	u_char db;
1209 	int s;
1210 
1211 	s = splhil();
1212 #ifdef DEBUG
1213 	if (hildebug & HDB_CONFIG) {
1214 		printf("hilconfig: reconfigured: ");
1215 		send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
1216 		printf("LPSTAT %x, ", db);
1217 		send_hil_cmd(hilp->hl_addr, HIL_READLPCTRL, NULL, 0, &db);
1218 		printf("LPCTRL %x, ", db);
1219 		send_hil_cmd(hilp->hl_addr, HIL_READKBDSADR, NULL, 0, &db);
1220 		printf("KBDSADR %x\n", db);
1221 		hilreport(hilp);
1222 	}
1223 #endif
1224 	/*
1225 	 * Determine how many devices are on the loop.
1226 	 * Mark those as alive and real, all others as dead.
1227 	 */
1228 	db = 0;
1229 	send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
1230 	hilp->hl_maxdev = db & LPS_DEVMASK;
1231 	for (db = 1; db < NHILD; db++) {
1232 		if (db <= hilp->hl_maxdev)
1233 			hilp->hl_device[db].hd_flags |= HIL_ALIVE;
1234 		else
1235 			hilp->hl_device[db].hd_flags &= ~HIL_ALIVE;
1236 		hilp->hl_device[db].hd_flags &= ~HIL_PSEUDO;
1237 	}
1238 #ifdef DEBUG
1239 	if (hildebug & (HDB_CONFIG|HDB_KEYBOARD))
1240 		printf("hilconfig: max device %d\n", hilp->hl_maxdev);
1241 #endif
1242 	if (hilp->hl_maxdev == 0) {
1243 		hilp->hl_kbddev = 0;
1244 		splx(s);
1245 		return;
1246 	}
1247 	/*
1248 	 * Find out where the keyboards are and record the ITE keyboard
1249 	 * (first one found).  If no keyboards found, we are all done.
1250 	 */
1251 	db = 0;
1252 	send_hil_cmd(hilp->hl_addr, HIL_READKBDSADR, NULL, 0, &db);
1253 #ifdef DEBUG
1254 	if (hildebug & HDB_KEYBOARD)
1255 		printf("hilconfig: keyboard: KBDSADR %x, old %d, new %d\n",
1256 		       db, hilp->hl_kbddev, ffs((int)db));
1257 #endif
1258 	hilp->hl_kbddev = ffs((int)db);
1259 	if (hilp->hl_kbddev == 0) {
1260 		splx(s);
1261 		return;
1262 	}
1263 	/*
1264 	 * Determine if the keyboard should be cooked or raw and configure it.
1265 	 */
1266 	db = (hilp->hl_kbdflags & KBD_RAW) ? 0 : 1 << (hilp->hl_kbddev - 1);
1267 	send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &db, 1, NULL);
1268 	/*
1269 	 * Re-enable autorepeat in raw mode, cooked mode AR is not affected.
1270 	 */
1271 	if (hilp->hl_kbdflags & (KBD_AR1|KBD_AR2)) {
1272 		db = (hilp->hl_kbdflags & KBD_AR1) ? HILAR1 : HILAR2;
1273 		hilp->hl_cmddev = hilp->hl_kbddev;
1274 		send_hildev_cmd(hilp, hilp->hl_kbddev, db);
1275 		hilp->hl_cmddev = 0;
1276 	}
1277 	/*
1278 	 * Determine the keyboard language configuration, but don't
1279 	 * override a user-specified setting.
1280 	 */
1281 	db = 0;
1282 	send_hil_cmd(hilp->hl_addr, HIL_READKBDLANG, NULL, 0, &db);
1283 #ifdef DEBUG
1284 	if (hildebug & HDB_KEYBOARD)
1285 		printf("hilconfig: language: old %x new %x\n",
1286 		       hilp->hl_kbdlang, db);
1287 #endif
1288 	if (hilp->hl_kbdlang != KBD_SPECIAL) {
1289 		struct kbdmap *km;
1290 
1291 		for (km = kbd_map; km->kbd_code; km++)
1292 			if (km->kbd_code == db) {
1293 				hilp->hl_kbdlang = db;
1294 				/* XXX */
1295 				kbd_keymap = km->kbd_keymap;
1296 				kbd_shiftmap = km->kbd_shiftmap;
1297 				kbd_ctrlmap = km->kbd_ctrlmap;
1298 				kbd_ctrlshiftmap = km->kbd_ctrlshiftmap;
1299 				kbd_stringmap = km->kbd_stringmap;
1300 			}
1301 	}
1302 	splx(s);
1303 }
1304 
1305 hilreset(hilp)
1306 	struct hilloop *hilp;
1307 {
1308 	register struct hil_dev *hildevice = hilp->hl_addr;
1309 	u_char db;
1310 
1311 	/*
1312 	 * Initialize the loop: reconfigure, don't report errors,
1313 	 * cook keyboards, and enable autopolling.
1314 	 */
1315 	db = LPC_RECONF | LPC_KBDCOOK | LPC_NOERROR | LPC_AUTOPOLL;
1316 	send_hil_cmd(hildevice, HIL_WRITELPCTRL, &db, 1, NULL);
1317 	/*
1318 	 * Delay one second for reconfiguration and then read the the
1319 	 * data register to clear the interrupt (if the loop reconfigured).
1320 	 */
1321 	DELAY(1000000);
1322 	if (hildevice->hil_stat & HIL_DATA_RDY)
1323 		db = hildevice->hil_data;
1324 	/*
1325 	 * The HIL loop may have reconfigured.  If so we proceed on,
1326 	 * if not we loop until a successful reconfiguration is reported
1327 	 * back to us.  The HIL loop will continue to attempt forever.
1328 	 * Probably not very smart.
1329 	 */
1330 	do {
1331 		send_hil_cmd(hildevice, HIL_READLPSTAT, NULL, 0, &db);
1332         } while ((db & (LPS_CONFFAIL|LPS_CONFGOOD)) == 0);
1333 	/*
1334 	 * At this point, the loop should have reconfigured.
1335 	 * The reconfiguration interrupt has already called hilconfig()
1336 	 * so the keyboard has been determined.
1337 	 */
1338 	send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
1339 }
1340 
1341 hilbeep(hilp, bp)
1342 	struct hilloop *hilp;
1343 	register struct _hilbell *bp;
1344 {
1345 	u_char buf[2];
1346 
1347 	buf[0] = ~((bp->duration - 10) / 10);
1348 	buf[1] = bp->frequency;
1349 	send_hil_cmd(hilp->hl_addr, HIL_SETTONE, buf, 2, NULL);
1350 }
1351 
1352 /*
1353  * Locate and return the address of the first ID module, 0 if none present.
1354  */
1355 hiliddev(hilp)
1356 	register struct hilloop *hilp;
1357 {
1358 	register int i, len;
1359 
1360 #ifdef DEBUG
1361 	if (hildebug & HDB_IDMODULE)
1362 		printf("hiliddev(%x): looking for idmodule...", hilp);
1363 #endif
1364 	for (i = 1; i <= hilp->hl_maxdev; i++) {
1365 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1366 		hilp->hl_cmddev = i;
1367 		send_hildev_cmd(hilp, i, HILIDENTIFY);
1368 		/*
1369 		 * XXX: the final condition checks to ensure that the
1370 		 * device ID byte is in the range of the ID module (0x30-0x3F)
1371 		 */
1372 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
1373 		if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT) &&
1374 		    (hilp->hl_cmdbuf[0] & 0xF0) == 0x30) {
1375 			hilp->hl_cmdbp = hilp->hl_cmdbuf;
1376 			hilp->hl_cmddev = i;
1377 			send_hildev_cmd(hilp, i, HILSECURITY);
1378 			break;
1379 		}
1380 	}
1381 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
1382 	hilp->hl_cmddev = 0;
1383 #ifdef DEBUG
1384 	if (hildebug & HDB_IDMODULE)
1385 		if (i <= hilp->hl_maxdev)
1386 			printf("found at %d\n", i);
1387 		else
1388 			printf("not found\n");
1389 #endif
1390 	return(i <= hilp->hl_maxdev ? i : 0);
1391 }
1392 
1393 /*
1394  * Low level routines which actually talk to the 8042 chip.
1395  */
1396 
1397 /*
1398  * Send a command to the 8042 with zero or more bytes of data.
1399  * If rdata is non-null, wait for and return a byte of data.
1400  * We run at splimp() to make the transaction as atomic as
1401  * possible without blocking the clock (is this necessary?)
1402  */
1403 send_hil_cmd(hildevice, cmd, data, dlen, rdata)
1404 	register struct hil_dev *hildevice;
1405 	u_char cmd, *data, dlen;
1406 	u_char *rdata;
1407 {
1408 	u_char status;
1409 	int s = splimp();
1410 
1411 	HILWAIT(hildevice);
1412 	hildevice->hil_cmd = cmd;
1413 	while (dlen--) {
1414 	  	HILWAIT(hildevice);
1415 		hildevice->hil_data = *data++;
1416 	}
1417 	if (rdata) {
1418 		do {
1419 			HILDATAWAIT(hildevice);
1420 			status = hildevice->hil_stat;
1421 			*rdata = hildevice->hil_data;
1422 		} while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
1423 	}
1424 	splx(s);
1425 }
1426 
1427 /*
1428  * Send a command to a device on the loop.
1429  * Since only one command can be active on the loop at any time,
1430  * we must ensure that we are not interrupted during this process.
1431  * Hence we mask interrupts to prevent potential access from most
1432  * interrupt routines and turn off auto-polling to disable the
1433  * internally generated poll commands.
1434  *
1435  * splhigh is extremely conservative but insures atomic operation,
1436  * splimp (clock only interrupts) seems to be good enough in practice.
1437  */
1438 send_hildev_cmd(hilp, device, cmd)
1439 	register struct hilloop *hilp;
1440 	char device, cmd;
1441 {
1442 	register struct hil_dev *hildevice = hilp->hl_addr;
1443 	u_char status, c;
1444 	int s = splimp();
1445 
1446 	polloff(hildevice);
1447 
1448 	/*
1449 	 * Transfer the command and device info to the chip
1450 	 */
1451 	HILWAIT(hildevice);
1452 	hildevice->hil_cmd = HIL_STARTCMD;
1453   	HILWAIT(hildevice);
1454 	hildevice->hil_data = 8 + device;
1455   	HILWAIT(hildevice);
1456 	hildevice->hil_data = cmd;
1457   	HILWAIT(hildevice);
1458 	hildevice->hil_data = HIL_TIMEOUT;
1459 	/*
1460 	 * Trigger the command and wait for completion
1461 	 */
1462 	HILWAIT(hildevice);
1463 	hildevice->hil_cmd = HIL_TRIGGER;
1464 	hilp->hl_cmddone = FALSE;
1465 	do {
1466 		HILDATAWAIT(hildevice);
1467 		status = hildevice->hil_stat;
1468 		c = hildevice->hil_data;
1469 		hil_process_int(status, c);
1470 	} while (!hilp->hl_cmddone);
1471 
1472 	pollon(hildevice);
1473 	splx(s);
1474 }
1475 
1476 /*
1477  * Turn auto-polling off and on.
1478  * Also disables and enable auto-repeat.  Why?
1479  */
1480 polloff(hildevice)
1481 	register struct hil_dev *hildevice;
1482 {
1483 	register char db;
1484 
1485 	/*
1486 	 * Turn off auto repeat
1487 	 */
1488 	HILWAIT(hildevice);
1489 	hildevice->hil_cmd = HIL_SETARR;
1490 	HILWAIT(hildevice);
1491 	hildevice->hil_data = 0;
1492 	/*
1493 	 * Turn off auto-polling
1494 	 */
1495 	HILWAIT(hildevice);
1496 	hildevice->hil_cmd = HIL_READLPCTRL;
1497 	HILDATAWAIT(hildevice);
1498 	db = hildevice->hil_data;
1499 	db &= ~LPC_AUTOPOLL;
1500 	HILWAIT(hildevice);
1501 	hildevice->hil_cmd = HIL_WRITELPCTRL;
1502 	HILWAIT(hildevice);
1503 	hildevice->hil_data = db;
1504 	/*
1505 	 * Must wait til polling is really stopped
1506 	 */
1507 	do {
1508 		HILWAIT(hildevice);
1509 		hildevice->hil_cmd = HIL_READBUSY;
1510 		HILDATAWAIT(hildevice);
1511 		db = hildevice->hil_data;
1512 	} while (db & BSY_LOOPBUSY);
1513 }
1514 
1515 pollon(hildevice)
1516 	register struct hil_dev *hildevice;
1517 {
1518 	register char db;
1519 
1520 	/*
1521 	 * Turn on auto polling
1522 	 */
1523 	HILWAIT(hildevice);
1524 	hildevice->hil_cmd = HIL_READLPCTRL;
1525 	HILDATAWAIT(hildevice);
1526 	db = hildevice->hil_data;
1527 	db |= LPC_AUTOPOLL;
1528 	HILWAIT(hildevice);
1529 	hildevice->hil_cmd = HIL_WRITELPCTRL;
1530 	HILWAIT(hildevice);
1531 	hildevice->hil_data = db;
1532 	/*
1533 	 * Turn on auto repeat
1534 	 */
1535 	HILWAIT(hildevice);
1536 	hildevice->hil_cmd = HIL_SETARR;
1537 	HILWAIT(hildevice);
1538 	hildevice->hil_data = ar_format(KBD_ARR);
1539 }
1540 
1541 #ifdef DEBUG
1542 printhilpollbuf(hilp)
1543 	register struct hilloop *hilp;
1544 {
1545   	register u_char *cp;
1546 	register int i, len;
1547 
1548 	cp = hilp->hl_pollbuf;
1549 	len = hilp->hl_pollbp - cp;
1550 	for (i = 0; i < len; i++)
1551 		printf("%x ", hilp->hl_pollbuf[i]);
1552 	printf("\n");
1553 }
1554 
1555 printhilcmdbuf(hilp)
1556 	register struct hilloop *hilp;
1557 {
1558   	register u_char *cp;
1559 	register int i, len;
1560 
1561 	cp = hilp->hl_cmdbuf;
1562 	len = hilp->hl_cmdbp - cp;
1563 	for (i = 0; i < len; i++)
1564 		printf("%x ", hilp->hl_cmdbuf[i]);
1565 	printf("\n");
1566 }
1567 
1568 hilreport(hilp)
1569 	register struct hilloop *hilp;
1570 {
1571 	register int i, len;
1572 	int s = splhil();
1573 
1574 	for (i = 1; i <= hilp->hl_maxdev; i++) {
1575 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1576 		hilp->hl_cmddev = i;
1577 		send_hildev_cmd(hilp, i, HILIDENTIFY);
1578 		printf("hil%d: id: ", i);
1579 		printhilcmdbuf(hilp);
1580 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
1581 		if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT)) {
1582 			hilp->hl_cmdbp = hilp->hl_cmdbuf;
1583 			hilp->hl_cmddev = i;
1584 			send_hildev_cmd(hilp, i, HILSECURITY);
1585 			printf("hil%d: sc: ", i);
1586 			printhilcmdbuf(hilp);
1587 		}
1588 	}
1589 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
1590 	hilp->hl_cmddev = 0;
1591 	splx(s);
1592 }
1593 #endif
1594