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