xref: /original-bsd/sys/hp/dev/hil.c (revision 3705696b)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1990, 1993
4  *	The Regents of the University of California.  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	8.1 (Berkeley) 06/10/93
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 <hp/dev/hilreg.h>
29 #include <hp/dev/hilioctl.h>
30 #include <hp/dev/hilvar.h>
31 #include <hp/dev/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_md.md_flags & MDP_HPUX) {
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_md.md_flags & MDP_HPUX) == 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_md.md_flags & MDP_HPUX)
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 OHILIOCRRT:
402 	case HILIOCRRT:
403 		/* Transfer the real time to the 8042 data buffer */
404 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
405 		/* Read each byte of the real time */
406 		for (i = 0; i < 5; i++) {
407 			send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL,
408 					0, &hold);
409 			data[4-i] = hold;
410 		}
411 		break;
412 
413 	case HILIOCRT:
414 		for (i = 0; i < 4; i++) {
415 			send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i,
416 					NULL, 0, &hold);
417 			data[i] = hold;
418 		}
419 		break;
420 
421 	case HILIOCID:
422 	case OHILIOCID:
423 	case HILIOCSC:
424 	case HILIOCRN:
425 	case HILIOCRS:
426 	case HILIOCED:
427 	  	send_hildev_cmd(hilp, device, (cmd & 0xFF));
428 		bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf);
429 	  	break;
430 
431         case HILIOCAROFF:
432         case HILIOCAR1:
433         case HILIOCAR2:
434 		if (hilp->hl_kbddev) {
435 			hilp->hl_cmddev = hilp->hl_kbddev;
436 			send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF));
437 			hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2);
438 			if (cmd == HILIOCAR1)
439 				hilp->hl_kbdflags |= KBD_AR1;
440 			else if (cmd == HILIOCAR2)
441 				hilp->hl_kbdflags |= KBD_AR2;
442 		}
443 		break;
444 
445 	case HILIOCBEEP:
446 		hilbeep(hilp, (struct _hilbell *)data);
447 		break;
448 
449 	case FIONBIO:
450 		dptr = &hilp->hl_device[device];
451 		if (*(int *)data)
452 			dptr->hd_flags |= HIL_NOBLOCK;
453 		else
454 			dptr->hd_flags &= ~HIL_NOBLOCK;
455 		break;
456 
457 	/*
458 	 * FIOASYNC must be present for FIONBIO above to work!
459 	 * (See fcntl in kern_descrip.c).
460 	 */
461 	case FIOASYNC:
462 		break;
463 
464         case HILIOCALLOCQ:
465 		error = hilqalloc(hilp, (struct hilqinfo *)data);
466 		break;
467 
468         case HILIOCFREEQ:
469 		error = hilqfree(hilp, ((struct hilqinfo *)data)->qid);
470 		break;
471 
472         case HILIOCMAPQ:
473 		error = hilqmap(hilp, *(int *)data, device);
474 		break;
475 
476         case HILIOCUNMAPQ:
477 		error = hilqunmap(hilp, *(int *)data, device);
478 		break;
479 
480 	case HILIOCHPUX:
481 		dptr = &hilp->hl_device[device];
482 		dptr->hd_flags |= HIL_READIN;
483 		dptr->hd_flags &= ~HIL_QUEUEIN;
484 		break;
485 
486         case HILIOCRESET:
487 	        hilreset(hilp);
488 		break;
489 
490 #ifdef DEBUG
491         case HILIOCTEST:
492 		hildebug = *(int *) data;
493 		break;
494 #endif
495 
496         default:
497 		error = EINVAL;
498 		break;
499 
500 	}
501 	hilp->hl_cmddev = 0;
502 	return(error);
503 }
504 
505 #ifdef HPUXCOMPAT
506 /* ARGSUSED */
507 hpuxhilioctl(dev, cmd, data, flag)
508 	dev_t dev;
509 	caddr_t data;
510 {
511 	register struct hilloop *hilp = &hilloop[HILLOOP(dev)];
512 	char device = HILUNIT(dev);
513 	struct hilloopdev *dptr;
514 	register int i;
515 	u_char hold;
516 
517 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
518 	bzero((caddr_t)hilp->hl_cmdbuf, HILBUFSIZE);
519 	hilp->hl_cmddev = device;
520 	switch (cmd) {
521 
522 	case HILSC:
523 	case HILID:
524 	case HILRN:
525 	case HILRS:
526 	case HILED:
527 	case HILP1:
528 	case HILP2:
529 	case HILP3:
530 	case HILP4:
531 	case HILP5:
532 	case HILP6:
533 	case HILP7:
534 	case HILP:
535 	case HILA1:
536 	case HILA2:
537 	case HILA3:
538 	case HILA4:
539 	case HILA5:
540 	case HILA6:
541 	case HILA7:
542 	case HILA:
543 		send_hildev_cmd(hilp, device, (cmd & 0xFF));
544 		bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf);
545 	  	break;
546 
547         case HILDKR:
548         case HILER1:
549         case HILER2:
550 		if (hilp->hl_kbddev) {
551 			hilp->hl_cmddev = hilp->hl_kbddev;
552 			send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF));
553 			hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2);
554 			if (cmd == HILIOCAR1)
555 				hilp->hl_kbdflags |= KBD_AR1;
556 			else if (cmd == HILIOCAR2)
557 				hilp->hl_kbdflags |= KBD_AR2;
558 		}
559 		break;
560 
561 	case EFTSBP:
562 		/* Send four data bytes to the tone gererator. */
563 		send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL);
564 		/* Send the trigger beeper command to the 8042. */
565 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
566 		break;
567 
568 	case EFTRRT:
569 		/* Transfer the real time to the 8042 data buffer */
570 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL);
571 		/* Read each byte of the real time */
572 		for (i = 0; i < 5; i++) {
573 			send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL,
574 					0, &hold);
575 			data[4-i] = hold;
576 		}
577 		break;
578 
579 	case EFTRT:
580 		for (i = 0; i < 4; i++) {
581 			send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i,
582 					NULL, 0, &hold);
583 			data[i] = hold;
584 		}
585 		break;
586 
587         case EFTRLC:
588         case EFTRCC:
589 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, &hold);
590 		*data = hold;
591 		break;
592 
593         case EFTSRPG:
594         case EFTSRD:
595         case EFTSRR:
596 		send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), data, 1, NULL);
597 		break;
598 
599 	case EFTSBI:
600 #ifdef hp800
601 		/* XXX big magic */
602 		hold = 7 - (*(u_char *)data >> 5);
603 		*(int *)data = 0x84069008 | (hold << 8);
604 		send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL);
605 		send_hil_cmd(hilp->hl_addr, 0xC4, NULL, 0, NULL);
606 		break;
607 #else
608 		hilbeep(hilp, (struct _hilbell *)data);
609 #endif
610 		break;
611 
612 	case FIONBIO:
613 		dptr = &hilp->hl_device[device];
614 		if (*(int *)data)
615 			dptr->hd_flags |= HIL_NOBLOCK;
616 		else
617 			dptr->hd_flags &= ~HIL_NOBLOCK;
618 		break;
619 
620 	case FIOASYNC:
621 		break;
622 
623         default:
624 		hilp->hl_cmddev = 0;
625 		return(EINVAL);
626 	}
627 	hilp->hl_cmddev = 0;
628 	return(0);
629 }
630 #endif
631 
632 /* ARGSUSED */
633 hilmap(dev, off, prot)
634 	dev_t dev;
635 	register int off;
636 {
637 }
638 
639 /*ARGSUSED*/
640 hilselect(dev, rw, p)
641 	dev_t dev;
642 	struct proc *p;
643 {
644 	register struct hilloop *hilp = &hilloop[HILLOOP(dev)];
645 	register struct hilloopdev *dptr;
646 	register struct hiliqueue *qp;
647 	register int mask;
648 	int s, device;
649 
650 	if (rw == FWRITE)
651 		return (1);
652 	device = HILUNIT(dev);
653 
654 	/*
655 	 * Read interface.
656 	 * Return 1 if there is something in the queue, 0 ow.
657 	 */
658 	dptr = &hilp->hl_device[device];
659 	if (dptr->hd_flags & HIL_READIN) {
660 		s = splhil();
661 		if (dptr->hd_queue.c_cc) {
662 			splx(s);
663 			return (1);
664 		}
665 		selrecord(p, &dptr->hd_selr);
666 		splx(s);
667 		return (0);
668 	}
669 
670 	/*
671 	 * Make sure device is alive and real (or the loop device).
672 	 * Note that we do not do this for the read interface.
673 	 * This is primarily to be consistant with HP-UX.
674 	 */
675 	if (device && (dptr->hd_flags & (HIL_ALIVE|HIL_PSEUDO)) != HIL_ALIVE)
676 		return (1);
677 
678 	/*
679 	 * Select on loop device is special.
680 	 * Check to see if there are any data for any loop device
681 	 * provided it is associated with a queue belonging to this user.
682 	 */
683 	if (device == 0)
684 		mask = -1;
685 	else
686 		mask = hildevmask(device);
687 	/*
688 	 * Must check everybody with interrupts blocked to prevent races.
689 	 */
690 	s = splhil();
691 	for (qp = hilp->hl_queue; qp < &hilp->hl_queue[NHILQ]; qp++)
692 		if (qp->hq_procp == p && (mask & qp->hq_devmask) &&
693 		    qp->hq_eventqueue->hil_evqueue.head !=
694 		    qp->hq_eventqueue->hil_evqueue.tail) {
695 			splx(s);
696 			return (1);
697 		}
698 
699 	selrecord(p, &dptr->hd_selr);
700 	splx(s);
701 	return (0);
702 }
703 
704 /*ARGSUSED*/
705 hilint(unit)
706 {
707 #ifdef hp300
708 	struct hilloop *hilp = &hilloop[0]; /* XXX how do we know on 300? */
709 #else
710 	struct hilloop *hilp = &hilloop[unit];
711 #endif
712 	register struct hil_dev *hildevice = hilp->hl_addr;
713 	u_char c, stat;
714 
715 	stat = READHILSTAT(hildevice);
716 	c = READHILDATA(hildevice);		/* clears interrupt */
717 	hil_process_int(hilp, stat, c);
718 }
719 
720 #include "ite.h"
721 
722 hil_process_int(hilp, stat, c)
723 	register struct hilloop *hilp;
724 	register u_char stat, c;
725 {
726 #ifdef DEBUG
727 	if (hildebug & HDB_EVENTS)
728 		printf("hilint: %x %x\n", stat, c);
729 #endif
730 
731 	/* the shift enables the compiler to generate a jump table */
732 	switch ((stat>>HIL_SSHIFT) & HIL_SMASK) {
733 
734 #if NITE > 0
735 	case HIL_KEY:
736 	case HIL_SHIFT:
737 	case HIL_CTRL:
738 	case HIL_CTRLSHIFT:
739 		itefilter(stat, c);
740 		return;
741 #endif
742 
743 	case HIL_STATUS:			/* The status info. */
744 		if (c & HIL_ERROR) {
745 		  	hilp->hl_cmddone = TRUE;
746 			if (c == HIL_RECONFIG)
747 				hilconfig(hilp);
748 			break;
749 		}
750 		if (c & HIL_COMMAND) {
751 		  	if (c & HIL_POLLDATA)	/* End of data */
752 				hilevent(hilp);
753 			else			/* End of command */
754 			  	hilp->hl_cmdending = TRUE;
755 			hilp->hl_actdev = 0;
756 		} else {
757 		  	if (c & HIL_POLLDATA) {	/* Start of polled data */
758 			  	if (hilp->hl_actdev != 0)
759 					hilevent(hilp);
760 				hilp->hl_actdev = (c & HIL_DEVMASK);
761 				hilp->hl_pollbp = hilp->hl_pollbuf;
762 			} else {		/* Start of command */
763 				if (hilp->hl_cmddev == (c & HIL_DEVMASK)) {
764 					hilp->hl_cmdbp = hilp->hl_cmdbuf;
765 					hilp->hl_actdev = 0;
766 				}
767 			}
768 		}
769 	        return;
770 
771 	case HIL_DATA:
772 		if (hilp->hl_actdev != 0)	/* Collecting poll data */
773 			*hilp->hl_pollbp++ = c;
774 		else if (hilp->hl_cmddev != 0)  /* Collecting cmd data */
775 			if (hilp->hl_cmdending) {
776 				hilp->hl_cmddone = TRUE;
777 				hilp->hl_cmdending = FALSE;
778 			} else
779 				*hilp->hl_cmdbp++ = c;
780 		return;
781 
782 	case 0:		/* force full jump table */
783 	default:
784 		return;
785 	}
786 }
787 
788 #if defined(DEBUG) && !defined(PANICBUTTON)
789 #define PANICBUTTON
790 #endif
791 
792 /*
793  * Optimized macro to compute:
794  *	eq->head == (eq->tail + 1) % eq->size
795  * i.e. has tail caught up with head.  We do this because 32 bit long
796  * remaidering is expensive (a function call with our compiler).
797  */
798 #define HQFULL(eq)	(((eq)->head?(eq)->head:(eq)->size) == (eq)->tail+1)
799 #define HQVALID(eq) \
800 	((eq)->size == HEVQSIZE && (eq)->tail >= 0 && (eq)->tail < HEVQSIZE)
801 
802 hilevent(hilp)
803 	struct hilloop *hilp;
804 {
805 	register struct hilloopdev *dptr = &hilp->hl_device[hilp->hl_actdev];
806 	register int len, mask, qnum;
807 	register u_char *cp, *pp;
808 	register HILQ *hq;
809 	struct timeval ourtime;
810 	hil_packet *proto;
811 	int s, len0;
812 	long tenths;
813 
814 #ifdef PANICBUTTON
815 	static int first;
816 	extern int panicbutton;
817 
818 	cp = hilp->hl_pollbuf;
819 	if (panicbutton && (*cp & HIL_KBDDATA)) {
820 		if (*++cp == 0x4E)
821 			first = 1;
822 		else if (first && *cp == 0x46 && !panicstr)
823 			panic("are we having fun yet?");
824 		else
825 			first = 0;
826 	}
827 #endif
828 #ifdef DEBUG
829 	if (hildebug & HDB_EVENTS) {
830 		printf("hilevent: dev %d pollbuf: ", hilp->hl_actdev);
831 		printhilpollbuf(hilp);
832 		printf("\n");
833 	}
834 #endif
835 
836 	/*
837 	 * Note that HIL_READIN effectively "shuts off" any queues
838 	 * that may have been in use at the time of an HILIOCHPUX call.
839 	 */
840 	if (dptr->hd_flags & HIL_READIN) {
841 		hpuxhilevent(hilp, dptr);
842 		return;
843 	}
844 
845 	/*
846 	 * If this device isn't on any queue or there are no data
847 	 * in the packet (can this happen?) do nothing.
848 	 */
849 	if (dptr->hd_qmask == 0 ||
850 	    (len0 = hilp->hl_pollbp - hilp->hl_pollbuf) <= 0)
851 		return;
852 
853 	/*
854 	 * Everybody gets the same time stamp
855 	 */
856 	s = splclock();
857 	ourtime = time;
858 	splx(s);
859 	tenths = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000);
860 
861 	proto = NULL;
862 	mask = dptr->hd_qmask;
863 	for (qnum = 0; mask; qnum++) {
864 		if ((mask & hilqmask(qnum)) == 0)
865 			continue;
866 		mask &= ~hilqmask(qnum);
867 		hq = hilp->hl_queue[qnum].hq_eventqueue;
868 
869 		/*
870 		 * Ensure that queue fields that we rely on are valid
871 		 * and that there is space in the queue.  If either
872 		 * test fails, we just skip this queue.
873 		 */
874 		if (!HQVALID(&hq->hil_evqueue) || HQFULL(&hq->hil_evqueue))
875 			continue;
876 
877 		/*
878 		 * Copy data to queue.
879 		 * If this is the first queue we construct the packet
880 		 * with length, timestamp and poll buffer data.
881 		 * For second and sucessive packets we just duplicate
882 		 * the first packet.
883 		 */
884 		pp = (u_char *) &hq->hil_event[hq->hil_evqueue.tail];
885 		if (proto == NULL) {
886 			proto = (hil_packet *)pp;
887 			cp = hilp->hl_pollbuf;
888 			len = len0;
889 			*pp++ = len + 6;
890 			*pp++ = hilp->hl_actdev;
891 			*(long *)pp = tenths;
892 			pp += sizeof(long);
893 			do *pp++ = *cp++; while (--len);
894 		} else
895 			*(hil_packet *)pp = *proto;
896 
897 		if (++hq->hil_evqueue.tail == hq->hil_evqueue.size)
898 			hq->hil_evqueue.tail = 0;
899 	}
900 
901 	/*
902 	 * Wake up anyone selecting on this device or the loop itself
903 	 */
904 	selwakeup(&dptr->hd_selr);
905 	dptr = &hilp->hl_device[HILLOOPDEV];
906 	selwakeup(&dptr->hd_selr);
907 }
908 
909 #undef HQFULL
910 
911 hpuxhilevent(hilp, dptr)
912 	register struct hilloop *hilp;
913 	register struct hilloopdev *dptr;
914 {
915 	register int len;
916 	struct timeval ourtime;
917 	long tstamp;
918 	int s;
919 
920 	/*
921 	 * Everybody gets the same time stamp
922 	 */
923 	s = splclock();
924 	ourtime = time;
925 	splx(s);
926 	tstamp = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000);
927 
928 	/*
929 	 * Each packet that goes into the buffer must be preceded by the
930 	 * number of bytes in the packet, and the timestamp of the packet.
931 	 * This adds 5 bytes to the packet size. Make sure there is enough
932 	 * room in the buffer for it, and if not, toss the packet.
933 	 */
934 	len = hilp->hl_pollbp - hilp->hl_pollbuf;
935 	if (dptr->hd_queue.c_cc <= (HILMAXCLIST - (len+5))) {
936 		putc(len+5, &dptr->hd_queue);
937 		(void) b_to_q((char *)&tstamp, sizeof tstamp, &dptr->hd_queue);
938 		(void) b_to_q((char *)hilp->hl_pollbuf, len, &dptr->hd_queue);
939 	}
940 
941 	/*
942 	 * Wake up any one blocked on a read or select
943 	 */
944 	if (dptr->hd_flags & HIL_ASLEEP) {
945 		dptr->hd_flags &= ~HIL_ASLEEP;
946 		wakeup((caddr_t)dptr);
947 	}
948 	selwakeup(&dptr->hd_selr);
949 }
950 
951 /*
952  * Shared queue manipulation routines
953  */
954 
955 hilqalloc(hilp, qip)
956 	register struct hilloop *hilp;
957 	struct hilqinfo *qip;
958 {
959 	struct proc *p = curproc;		/* XXX */
960 
961 #ifdef DEBUG
962 	if (hildebug & HDB_FOLLOW)
963 		printf("hilqalloc(%d): addr %x\n", p->p_pid, qip->addr);
964 #endif
965 	return(EINVAL);
966 }
967 
968 hilqfree(hilp, qnum)
969 	register int qnum;
970 {
971 	struct proc *p = curproc;		/* XXX */
972 
973 #ifdef DEBUG
974 	if (hildebug & HDB_FOLLOW)
975 		printf("hilqfree(%d): qnum %d\n", p->p_pid, qnum);
976 #endif
977 	return(EINVAL);
978 }
979 
980 hilqmap(hilp, qnum, device)
981 	register struct hilloop *hilp;
982 	register int qnum, device;
983 {
984 	struct proc *p = curproc;		/* XXX */
985 	register struct hilloopdev *dptr = &hilp->hl_device[device];
986 	int s;
987 
988 #ifdef DEBUG
989 	if (hildebug & HDB_FOLLOW)
990 		printf("hilqmap(%d): qnum %d device %x\n",
991 		       p->p_pid, qnum, device);
992 #endif
993 	if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != p)
994 		return(EINVAL);
995 	if ((dptr->hd_flags & HIL_QUEUEIN) == 0)
996 		return(EINVAL);
997 	if (dptr->hd_qmask && p->p_ucred->cr_uid &&
998 	    p->p_ucred->cr_uid != dptr->hd_uid)
999 		return(EPERM);
1000 
1001 	hilp->hl_queue[qnum].hq_devmask |= hildevmask(device);
1002 	if (dptr->hd_qmask == 0)
1003 		dptr->hd_uid = p->p_ucred->cr_uid;
1004 	s = splhil();
1005 	dptr->hd_qmask |= hilqmask(qnum);
1006 	splx(s);
1007 #ifdef DEBUG
1008 	if (hildebug & HDB_MASK)
1009 		printf("hilqmap(%d): devmask %x qmask %x\n",
1010 		       p->p_pid, hilp->hl_queue[qnum].hq_devmask,
1011 		       dptr->hd_qmask);
1012 #endif
1013 	return(0);
1014 }
1015 
1016 hilqunmap(hilp, qnum, device)
1017 	register struct hilloop *hilp;
1018 	register int qnum, device;
1019 {
1020 	struct proc *p = curproc;		/* XXX */
1021 	int s;
1022 
1023 #ifdef DEBUG
1024 	if (hildebug & HDB_FOLLOW)
1025 		printf("hilqunmap(%d): qnum %d device %x\n",
1026 		       p->p_pid, qnum, device);
1027 #endif
1028 
1029 	if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != p)
1030 		return(EINVAL);
1031 
1032 	hilp->hl_queue[qnum].hq_devmask &= ~hildevmask(device);
1033 	s = splhil();
1034 	hilp->hl_device[device].hd_qmask &= ~hilqmask(qnum);
1035 	splx(s);
1036 #ifdef DEBUG
1037 	if (hildebug & HDB_MASK)
1038 		printf("hilqunmap(%d): devmask %x qmask %x\n",
1039 		       p->p_pid, hilp->hl_queue[qnum].hq_devmask,
1040 		       hilp->hl_device[device].hd_qmask);
1041 #endif
1042 	return(0);
1043 }
1044 
1045 /*
1046  * Cooked keyboard functions for ite driver.
1047  * There is only one "cooked" ITE keyboard (the first keyboard found)
1048  * per loop.  There may be other keyboards, but they will always be "raw".
1049  */
1050 
1051 kbdbell(unit)
1052 	int unit;
1053 {
1054 	struct hilloop *hilp = &hilloop[unit];
1055 
1056 	hilbeep(hilp, &default_bell);
1057 }
1058 
1059 kbdenable(unit)
1060 	int unit;
1061 {
1062 	struct hilloop *hilp = &hilloop[unit];
1063 	register struct hil_dev *hildevice = hilp->hl_addr;
1064 	char db;
1065 
1066 	/* Set the autorepeat rate register */
1067 	db = ar_format(KBD_ARR);
1068 	send_hil_cmd(hildevice, HIL_SETARR, &db, 1, NULL);
1069 
1070 	/* Set the autorepeat delay register */
1071 	db = ar_format(KBD_ARD);
1072 	send_hil_cmd(hildevice, HIL_SETARD, &db, 1, NULL);
1073 
1074 	/* Enable interrupts */
1075 	send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
1076 }
1077 
1078 kbddisable(unit)
1079 	int unit;
1080 {
1081 }
1082 
1083 /*
1084  * XXX: read keyboard directly and return code.
1085  * Used by console getchar routine.  Could really screw up anybody
1086  * reading from the keyboard in the normal, interrupt driven fashion.
1087  */
1088 kbdgetc(unit, statp)
1089 	int unit, *statp;
1090 {
1091 	struct hilloop *hilp = &hilloop[unit];
1092 	register struct hil_dev *hildevice = hilp->hl_addr;
1093 	register int c, stat;
1094 	int s;
1095 
1096 	s = splhil();
1097 	while (((stat = READHILSTAT(hildevice)) & HIL_DATA_RDY) == 0)
1098 		;
1099 	c = READHILDATA(hildevice);
1100 	splx(s);
1101 	*statp = stat;
1102 	return(c);
1103 }
1104 
1105 /*
1106  * Recoginize and clear keyboard generated NMIs.
1107  * Returns 1 if it was ours, 0 otherwise.  Note that we cannot use
1108  * send_hil_cmd() to issue the clear NMI command as that would actually
1109  * lower the priority to splimp() and it doesn't wait for the completion
1110  * of the command.  Either of these conditions could result in the
1111  * interrupt reoccuring.  Note that we issue the CNMT command twice.
1112  * This seems to be needed, once is not always enough!?!
1113  */
1114 kbdnmi(unit)
1115 	int unit;
1116 {
1117 #ifdef hp300
1118 	struct hilloop *hilp = &hilloop[0]; /* XXX how do we know on 300? */
1119 #else
1120 	struct hilloop *hilp = &hilloop[unit];
1121 #endif
1122 #ifdef hp300
1123 	if ((*KBDNMISTAT & KBDNMI) == 0)
1124 		return(0);
1125 #endif
1126 	HILWAIT(hilp->hl_addr);
1127 	WRITEHILCMD(hilp->hl_addr, HIL_CNMT);
1128 	HILWAIT(hilp->hl_addr);
1129 	WRITEHILCMD(hilp->hl_addr, HIL_CNMT);
1130 	HILWAIT(hilp->hl_addr);
1131 	return(1);
1132 }
1133 
1134 #define HILSECURITY	0x33
1135 #define HILIDENTIFY	0x03
1136 #define HILSCBIT	0x04
1137 
1138 /*
1139  * Called at boot time to print out info about interesting devices
1140  */
1141 hilinfo(unit)
1142 	int unit;
1143 {
1144   	register struct hilloop *hilp = &hilloop[unit];
1145 	register int id, len;
1146 	register struct kbdmap *km;
1147 
1148 	/*
1149 	 * Keyboard info.
1150 	 */
1151 	if (hilp->hl_kbddev) {
1152 		printf("hil%d: ", hilp->hl_kbddev);
1153 		for (km = kbd_map; km->kbd_code; km++)
1154 			if (km->kbd_code == hilp->hl_kbdlang) {
1155 				printf("%s ", km->kbd_desc);
1156 				break;
1157 			}
1158 		printf("keyboard\n");
1159 	}
1160 	/*
1161 	 * ID module.
1162 	 * Attempt to locate the first ID module and print out its
1163 	 * security code.  Is this a good idea??
1164 	 */
1165 	id = hiliddev(hilp);
1166 	if (id) {
1167 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1168 		hilp->hl_cmddev = id;
1169 		send_hildev_cmd(hilp, id, HILSECURITY);
1170 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
1171 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1172 		hilp->hl_cmddev = 0;
1173 		printf("hil%d: security code", id);
1174 		for (id = 0; id < len; id++)
1175 			printf(" %x", hilp->hl_cmdbuf[id]);
1176 		while (id++ < 16)
1177 			printf(" 0");
1178 		printf("\n");
1179 	}
1180 }
1181 
1182 #define HILAR1	0x3E
1183 #define HILAR2	0x3F
1184 
1185 /*
1186  * Called after the loop has reconfigured.  Here we need to:
1187  *	- determine how many devices are on the loop
1188  *	  (some may have been added or removed)
1189  *	- locate the ITE keyboard (if any) and ensure
1190  *	  that it is in the proper state (raw or cooked)
1191  *	  and is set to use the proper language mapping table
1192  *	- ensure all other keyboards are raw
1193  * Note that our device state is now potentially invalid as
1194  * devices may no longer be where they were.  What we should
1195  * do here is either track where the devices went and move
1196  * state around accordingly or, more simply, just mark all
1197  * devices as HIL_DERROR and don't allow any further use until
1198  * they are closed.  This is a little too brutal for my tastes,
1199  * we prefer to just assume people won't move things around.
1200  */
1201 hilconfig(hilp)
1202 	register struct hilloop *hilp;
1203 {
1204 	u_char db;
1205 	int s;
1206 
1207 	s = splhil();
1208 #ifdef DEBUG
1209 	if (hildebug & HDB_CONFIG) {
1210 		printf("hilconfig: reconfigured: ");
1211 		send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
1212 		printf("LPSTAT %x, ", db);
1213 		send_hil_cmd(hilp->hl_addr, HIL_READLPCTRL, NULL, 0, &db);
1214 		printf("LPCTRL %x, ", db);
1215 		send_hil_cmd(hilp->hl_addr, HIL_READKBDSADR, NULL, 0, &db);
1216 		printf("KBDSADR %x\n", db);
1217 		hilreport(hilp);
1218 	}
1219 #endif
1220 	/*
1221 	 * Determine how many devices are on the loop.
1222 	 * Mark those as alive and real, all others as dead.
1223 	 */
1224 	db = 0;
1225 	send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db);
1226 	hilp->hl_maxdev = db & LPS_DEVMASK;
1227 #ifdef DEBUG
1228 	if (hildebug & HDB_CONFIG)
1229 		printf("hilconfig: %d devices found\n", hilp->hl_maxdev);
1230 #endif
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 #ifdef DEBUG
1312 	if (hildebug & HDB_FOLLOW)
1313 		printf("hilreset(%x)\n", hilp);
1314 #endif
1315 	/*
1316 	 * Initialize the loop: reconfigure, don't report errors,
1317 	 * cook keyboards, and enable autopolling.
1318 	 */
1319 	db = LPC_RECONF | LPC_KBDCOOK | LPC_NOERROR | LPC_AUTOPOLL;
1320 	send_hil_cmd(hildevice, HIL_WRITELPCTRL, &db, 1, NULL);
1321 	/*
1322 	 * Delay one second for reconfiguration and then read the the
1323 	 * data register to clear the interrupt (if the loop reconfigured).
1324 	 */
1325 	DELAY(1000000);
1326 	if (READHILSTAT(hildevice) & HIL_DATA_RDY)
1327 		db = READHILDATA(hildevice);
1328 	/*
1329 	 * The HIL loop may have reconfigured.  If so we proceed on,
1330 	 * if not we loop until a successful reconfiguration is reported
1331 	 * back to us.  The HIL loop will continue to attempt forever.
1332 	 * Probably not very smart.
1333 	 */
1334 	do {
1335 		send_hil_cmd(hildevice, HIL_READLPSTAT, NULL, 0, &db);
1336         } while ((db & (LPS_CONFFAIL|LPS_CONFGOOD)) == 0);
1337 	/*
1338 	 * At this point, the loop should have reconfigured.
1339 	 * The reconfiguration interrupt has already called hilconfig()
1340 	 * so the keyboard has been determined.
1341 	 */
1342 	send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL);
1343 }
1344 
1345 hilbeep(hilp, bp)
1346 	struct hilloop *hilp;
1347 	register struct _hilbell *bp;
1348 {
1349 	u_char buf[2];
1350 
1351 	buf[0] = ~((bp->duration - 10) / 10);
1352 	buf[1] = bp->frequency;
1353 	send_hil_cmd(hilp->hl_addr, HIL_SETTONE, buf, 2, NULL);
1354 }
1355 
1356 /*
1357  * Locate and return the address of the first ID module, 0 if none present.
1358  */
1359 hiliddev(hilp)
1360 	register struct hilloop *hilp;
1361 {
1362 	register int i, len;
1363 
1364 #ifdef DEBUG
1365 	if (hildebug & HDB_IDMODULE)
1366 		printf("hiliddev(%x): max %d, looking for idmodule...",
1367 		       hilp, hilp->hl_maxdev);
1368 #endif
1369 	for (i = 1; i <= hilp->hl_maxdev; i++) {
1370 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1371 		hilp->hl_cmddev = i;
1372 		send_hildev_cmd(hilp, i, HILIDENTIFY);
1373 		/*
1374 		 * XXX: the final condition checks to ensure that the
1375 		 * device ID byte is in the range of the ID module (0x30-0x3F)
1376 		 */
1377 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
1378 		if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT) &&
1379 		    (hilp->hl_cmdbuf[0] & 0xF0) == 0x30) {
1380 			hilp->hl_cmdbp = hilp->hl_cmdbuf;
1381 			hilp->hl_cmddev = i;
1382 			send_hildev_cmd(hilp, i, HILSECURITY);
1383 			break;
1384 		}
1385 	}
1386 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
1387 	hilp->hl_cmddev = 0;
1388 #ifdef DEBUG
1389 	if (hildebug & HDB_IDMODULE)
1390 		if (i <= hilp->hl_maxdev)
1391 			printf("found at %d\n", i);
1392 		else
1393 			printf("not found\n");
1394 #endif
1395 	return(i <= hilp->hl_maxdev ? i : 0);
1396 }
1397 
1398 #ifdef HPUXCOMPAT
1399 /*
1400  * XXX map devno as expected by HP-UX
1401  */
1402 hildevno(dev)
1403 	dev_t dev;
1404 {
1405 	int newdev;
1406 
1407 	newdev = 24 << 24;
1408 #ifdef HILCOMPAT
1409 	/*
1410 	 * XXX compat check
1411 	 * Don't convert old style specfiles already in correct format
1412 	 */
1413 	if (minor(dev) && (dev & 0xF) == 0)
1414 		newdev |= minor(dev);
1415 	else
1416 #endif
1417 	newdev |= (HILLOOP(dev) << 8) | (HILUNIT(dev) << 4);
1418 	return(newdev);
1419 }
1420 #endif
1421 
1422 /*
1423  * Low level routines which actually talk to the 8042 chip.
1424  */
1425 
1426 /*
1427  * Send a command to the 8042 with zero or more bytes of data.
1428  * If rdata is non-null, wait for and return a byte of data.
1429  * We run at splimp() to make the transaction as atomic as
1430  * possible without blocking the clock (is this necessary?)
1431  */
1432 send_hil_cmd(hildevice, cmd, data, dlen, rdata)
1433 	register struct hil_dev *hildevice;
1434 	u_char cmd, *data, dlen;
1435 	u_char *rdata;
1436 {
1437 	u_char status;
1438 	int s = splimp();
1439 
1440 	HILWAIT(hildevice);
1441 	WRITEHILCMD(hildevice, cmd);
1442 	while (dlen--) {
1443 	  	HILWAIT(hildevice);
1444 		WRITEHILDATA(hildevice, *data++);
1445 	}
1446 	if (rdata) {
1447 		do {
1448 			HILDATAWAIT(hildevice);
1449 			status = READHILSTAT(hildevice);
1450 			*rdata = READHILDATA(hildevice);
1451 		} while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
1452 	}
1453 	splx(s);
1454 }
1455 
1456 /*
1457  * Send a command to a device on the loop.
1458  * Since only one command can be active on the loop at any time,
1459  * we must ensure that we are not interrupted during this process.
1460  * Hence we mask interrupts to prevent potential access from most
1461  * interrupt routines and turn off auto-polling to disable the
1462  * internally generated poll commands.
1463  *
1464  * splhigh is extremely conservative but insures atomic operation,
1465  * splimp (clock only interrupts) seems to be good enough in practice.
1466  */
1467 send_hildev_cmd(hilp, device, cmd)
1468 	register struct hilloop *hilp;
1469 	char device, cmd;
1470 {
1471 	register struct hil_dev *hildevice = hilp->hl_addr;
1472 	u_char status, c;
1473 	int s = splimp();
1474 
1475 	polloff(hildevice);
1476 
1477 	/*
1478 	 * Transfer the command and device info to the chip
1479 	 */
1480 	HILWAIT(hildevice);
1481 	WRITEHILCMD(hildevice, HIL_STARTCMD);
1482   	HILWAIT(hildevice);
1483 	WRITEHILDATA(hildevice, 8 + device);
1484   	HILWAIT(hildevice);
1485 	WRITEHILDATA(hildevice, cmd);
1486   	HILWAIT(hildevice);
1487 	WRITEHILDATA(hildevice, HIL_TIMEOUT);
1488 	/*
1489 	 * Trigger the command and wait for completion
1490 	 */
1491 	HILWAIT(hildevice);
1492 	WRITEHILCMD(hildevice, HIL_TRIGGER);
1493 	hilp->hl_cmddone = FALSE;
1494 	do {
1495 		HILDATAWAIT(hildevice);
1496 		status = READHILSTAT(hildevice);
1497 		c = READHILDATA(hildevice);
1498 		hil_process_int(hilp, status, c);
1499 	} while (!hilp->hl_cmddone);
1500 
1501 	pollon(hildevice);
1502 	splx(s);
1503 }
1504 
1505 /*
1506  * Turn auto-polling off and on.
1507  * Also disables and enable auto-repeat.  Why?
1508  */
1509 polloff(hildevice)
1510 	register struct hil_dev *hildevice;
1511 {
1512 	register char db;
1513 
1514 	/*
1515 	 * Turn off auto repeat
1516 	 */
1517 	HILWAIT(hildevice);
1518 	WRITEHILCMD(hildevice, HIL_SETARR);
1519 	HILWAIT(hildevice);
1520 	WRITEHILDATA(hildevice, 0);
1521 	/*
1522 	 * Turn off auto-polling
1523 	 */
1524 	HILWAIT(hildevice);
1525 	WRITEHILCMD(hildevice, HIL_READLPCTRL);
1526 	HILDATAWAIT(hildevice);
1527 	db = READHILDATA(hildevice);
1528 	db &= ~LPC_AUTOPOLL;
1529 	HILWAIT(hildevice);
1530 	WRITEHILCMD(hildevice, HIL_WRITELPCTRL);
1531 	HILWAIT(hildevice);
1532 	WRITEHILDATA(hildevice, db);
1533 	/*
1534 	 * Must wait til polling is really stopped
1535 	 */
1536 	do {
1537 		HILWAIT(hildevice);
1538 		WRITEHILCMD(hildevice, HIL_READBUSY);
1539 		HILDATAWAIT(hildevice);
1540 		db = READHILDATA(hildevice);
1541 	} while (db & BSY_LOOPBUSY);
1542 }
1543 
1544 pollon(hildevice)
1545 	register struct hil_dev *hildevice;
1546 {
1547 	register char db;
1548 
1549 	/*
1550 	 * Turn on auto polling
1551 	 */
1552 	HILWAIT(hildevice);
1553 	WRITEHILCMD(hildevice, HIL_READLPCTRL);
1554 	HILDATAWAIT(hildevice);
1555 	db = READHILDATA(hildevice);
1556 	db |= LPC_AUTOPOLL;
1557 	HILWAIT(hildevice);
1558 	WRITEHILCMD(hildevice, HIL_WRITELPCTRL);
1559 	HILWAIT(hildevice);
1560 	WRITEHILDATA(hildevice, db);
1561 	/*
1562 	 * Turn on auto repeat
1563 	 */
1564 	HILWAIT(hildevice);
1565 	WRITEHILCMD(hildevice, HIL_SETARR);
1566 	HILWAIT(hildevice);
1567 	WRITEHILDATA(hildevice, ar_format(KBD_ARR));
1568 }
1569 
1570 #ifdef DEBUG
1571 printhilpollbuf(hilp)
1572 	register struct hilloop *hilp;
1573 {
1574   	register u_char *cp;
1575 	register int i, len;
1576 
1577 	cp = hilp->hl_pollbuf;
1578 	len = hilp->hl_pollbp - cp;
1579 	for (i = 0; i < len; i++)
1580 		printf("%x ", hilp->hl_pollbuf[i]);
1581 	printf("\n");
1582 }
1583 
1584 printhilcmdbuf(hilp)
1585 	register struct hilloop *hilp;
1586 {
1587   	register u_char *cp;
1588 	register int i, len;
1589 
1590 	cp = hilp->hl_cmdbuf;
1591 	len = hilp->hl_cmdbp - cp;
1592 	for (i = 0; i < len; i++)
1593 		printf("%x ", hilp->hl_cmdbuf[i]);
1594 	printf("\n");
1595 }
1596 
1597 hilreport(hilp)
1598 	register struct hilloop *hilp;
1599 {
1600 	register int i, len;
1601 	int s = splhil();
1602 
1603 	for (i = 1; i <= hilp->hl_maxdev; i++) {
1604 		hilp->hl_cmdbp = hilp->hl_cmdbuf;
1605 		hilp->hl_cmddev = i;
1606 		send_hildev_cmd(hilp, i, HILIDENTIFY);
1607 		printf("hil%d: id: ", i);
1608 		printhilcmdbuf(hilp);
1609 		len = hilp->hl_cmdbp - hilp->hl_cmdbuf;
1610 		if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT)) {
1611 			hilp->hl_cmdbp = hilp->hl_cmdbuf;
1612 			hilp->hl_cmddev = i;
1613 			send_hildev_cmd(hilp, i, HILSECURITY);
1614 			printf("hil%d: sc: ", i);
1615 			printhilcmdbuf(hilp);
1616 		}
1617 	}
1618 	hilp->hl_cmdbp = hilp->hl_cmdbuf;
1619 	hilp->hl_cmddev = 0;
1620 	splx(s);
1621 }
1622 #endif
1623