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