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