xref: /netbsd-src/sys/dev/ic/pckbc.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /* $NetBSD: pckbc.c,v 1.3 2000/06/05 22:20:54 sommerfeld Exp $ */
2 
3 /*
4  * Copyright (c) 1998
5  *	Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project
18  *	by Matthias Drochner.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/callout.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 #include <sys/errno.h>
42 #include <sys/queue.h>
43 #include <sys/lock.h>
44 
45 #include <machine/bus.h>
46 
47 #include <dev/ic/i8042reg.h>
48 #include <dev/ic/pckbcvar.h>
49 
50 #include "rnd.h"
51 #include "locators.h"
52 
53 #ifdef __HAVE_NWSCONS /* XXX: this port uses sys/dev/pckbc */
54 #include "pckbd.h"
55 #else /* ie: only md drivers attach to pckbc */
56 #define NPCKBD 0
57 #endif
58 #if (NPCKBD > 0)
59 #include <dev/pckbc/pckbdvar.h>
60 #endif
61 #if NRND > 0
62 #include <sys/rnd.h>
63 #endif
64 
65 /* descriptor for one device command */
66 struct pckbc_devcmd {
67 	TAILQ_ENTRY(pckbc_devcmd) next;
68 	int flags;
69 #define KBC_CMDFLAG_SYNC 1 /* give descriptor back to caller */
70 #define KBC_CMDFLAG_SLOW 2
71 	u_char cmd[4];
72 	int cmdlen, cmdidx, retries;
73 	u_char response[4];
74 	int status, responselen, responseidx;
75 };
76 
77 /* data per slave device */
78 struct pckbc_slotdata {
79 	int polling; /* don't read data port in interrupt handler */
80 	TAILQ_HEAD(, pckbc_devcmd) cmdqueue; /* active commands */
81 	TAILQ_HEAD(, pckbc_devcmd) freequeue; /* free commands */
82 #define NCMD 5
83 	struct pckbc_devcmd cmds[NCMD];
84 #if NRND > 0
85 	rndsource_element_t	rnd_source;
86 #endif
87 };
88 
89 #define CMD_IN_QUEUE(q) (TAILQ_FIRST(&(q)->cmdqueue) != NULL)
90 
91 void pckbc_init_slotdata __P((struct pckbc_slotdata *));
92 int pckbc_attach_slot __P((struct pckbc_softc *, pckbc_slot_t));
93 int pckbc_submatch __P((struct device *, struct cfdata *, void *));
94 int pckbcprint __P((void *, const char *));
95 
96 struct pckbc_internal pckbc_consdata;
97 int pckbc_console_attached;
98 
99 static int pckbc_console;
100 static struct pckbc_slotdata pckbc_cons_slotdata;
101 
102 static int pckbc_wait_output __P((bus_space_tag_t, bus_space_handle_t));
103 
104 static int pckbc_get8042cmd __P((struct pckbc_internal *));
105 static int pckbc_put8042cmd __P((struct pckbc_internal *));
106 static int pckbc_send_devcmd __P((struct pckbc_internal *, pckbc_slot_t,
107 				  u_char));
108 static void pckbc_poll_cmd1 __P((struct pckbc_internal *, pckbc_slot_t,
109 				 struct pckbc_devcmd *));
110 
111 void pckbc_cleanqueue __P((struct pckbc_slotdata *));
112 void pckbc_cleanup __P((void *));
113 int pckbc_cmdresponse __P((struct pckbc_internal *, pckbc_slot_t, u_char));
114 void pckbc_start __P((struct pckbc_internal *, pckbc_slot_t));
115 
116 const char *pckbc_slot_names[] = { "kbd", "aux" };
117 
118 #define KBC_DEVCMD_ACK 0xfa
119 #define KBC_DEVCMD_RESEND 0xfe
120 
121 #define	KBD_DELAY	DELAY(8)
122 
123 static inline int
124 pckbc_wait_output(iot, ioh_c)
125 	bus_space_tag_t iot;
126 	bus_space_handle_t ioh_c;
127 {
128 	u_int i;
129 
130 	for (i = 100000; i; i--)
131 		if (!(bus_space_read_1(iot, ioh_c, 0) & KBS_IBF)) {
132 			KBD_DELAY;
133 			return (1);
134 		}
135 	return (0);
136 }
137 
138 int
139 pckbc_send_cmd(iot, ioh_c, val)
140 	bus_space_tag_t iot;
141 	bus_space_handle_t ioh_c;
142 	u_char val;
143 {
144 	if (!pckbc_wait_output(iot, ioh_c))
145 		return (0);
146 	bus_space_write_1(iot, ioh_c, 0, val);
147 	return (1);
148 }
149 
150 int
151 pckbc_poll_data1(iot, ioh_d, ioh_c, slot, checkaux)
152 	bus_space_tag_t iot;
153 	bus_space_handle_t ioh_d, ioh_c;
154 	pckbc_slot_t slot;
155 	int checkaux;
156 {
157 	int i;
158 	u_char stat;
159 
160 	/* if 1 port read takes 1us (?), this polls for 100ms */
161 	for (i = 100000; i; i--) {
162 		stat = bus_space_read_1(iot, ioh_c, 0);
163 		if (stat & KBS_DIB) {
164 			register u_char c;
165 
166 			KBD_DELAY;
167 			c = bus_space_read_1(iot, ioh_d, 0);
168 			if (checkaux && (stat & 0x20)) { /* aux data */
169 				if (slot != PCKBC_AUX_SLOT) {
170 #ifdef PCKBCDEBUG
171 					printf("lost aux 0x%x\n", c);
172 #endif
173 					continue;
174 				}
175 			} else {
176 				if (slot == PCKBC_AUX_SLOT) {
177 #ifdef PCKBCDEBUG
178 					printf("lost kbd 0x%x\n", c);
179 #endif
180 					continue;
181 				}
182 			}
183 			return (c);
184 		}
185 	}
186 	return (-1);
187 }
188 
189 /*
190  * Get the current command byte.
191  */
192 static int
193 pckbc_get8042cmd(t)
194 	struct pckbc_internal *t;
195 {
196 	bus_space_tag_t iot = t->t_iot;
197 	bus_space_handle_t ioh_d = t->t_ioh_d;
198 	bus_space_handle_t ioh_c = t->t_ioh_c;
199 	int data;
200 
201 	if (!pckbc_send_cmd(iot, ioh_c, K_RDCMDBYTE))
202 		return (0);
203 	data = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT,
204 				t->t_haveaux);
205 	if (data == -1)
206 		return (0);
207 	t->t_cmdbyte = data;
208 	return (1);
209 }
210 
211 /*
212  * Pass command byte to keyboard controller (8042).
213  */
214 static int
215 pckbc_put8042cmd(t)
216 	struct pckbc_internal *t;
217 {
218 	bus_space_tag_t iot = t->t_iot;
219 	bus_space_handle_t ioh_d = t->t_ioh_d;
220 	bus_space_handle_t ioh_c = t->t_ioh_c;
221 
222 	if (!pckbc_send_cmd(iot, ioh_c, K_LDCMDBYTE))
223 		return (0);
224 	if (!pckbc_wait_output(iot, ioh_c))
225 		return (0);
226 	bus_space_write_1(iot, ioh_d, 0, t->t_cmdbyte);
227 	return (1);
228 }
229 
230 static int
231 pckbc_send_devcmd(t, slot, val)
232 	struct pckbc_internal *t;
233 	pckbc_slot_t slot;
234 	u_char val;
235 {
236 	bus_space_tag_t iot = t->t_iot;
237 	bus_space_handle_t ioh_d = t->t_ioh_d;
238 	bus_space_handle_t ioh_c = t->t_ioh_c;
239 
240 	if (slot == PCKBC_AUX_SLOT) {
241 		if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXWRITE))
242 			return (0);
243 	}
244 	if (!pckbc_wait_output(iot, ioh_c))
245 		return (0);
246 	bus_space_write_1(iot, ioh_d, 0, val);
247 	return (1);
248 }
249 
250 int
251 pckbc_is_console(iot, addr)
252 	bus_space_tag_t iot;
253 	bus_addr_t addr;
254 {
255 	if (pckbc_console && !pckbc_console_attached &&
256 	    pckbc_consdata.t_iot == iot &&
257 	    pckbc_consdata.t_addr == addr)
258 		return (1);
259 	return (0);
260 }
261 
262 int
263 pckbc_submatch(parent, cf, aux)
264 	struct device *parent;
265 	struct cfdata *cf;
266 	void *aux;
267 {
268 	struct pckbc_attach_args *pa = aux;
269 
270 	if (cf->cf_loc[PCKBCCF_SLOT] != PCKBCCF_SLOT_DEFAULT &&
271 	    cf->cf_loc[PCKBCCF_SLOT] != pa->pa_slot)
272 		return (0);
273 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
274 }
275 
276 int
277 pckbc_attach_slot(sc, slot)
278 	struct pckbc_softc *sc;
279 	pckbc_slot_t slot;
280 {
281 	struct pckbc_internal *t = sc->id;
282 	struct pckbc_attach_args pa;
283 	int found;
284 
285 	pa.pa_tag = t;
286 	pa.pa_slot = slot;
287 	found = (config_found_sm((struct device *)sc, &pa,
288 				 pckbcprint, pckbc_submatch) != NULL);
289 
290 	if (found && !t->t_slotdata[slot]) {
291 		t->t_slotdata[slot] = malloc(sizeof(struct pckbc_slotdata),
292 					     M_DEVBUF, M_NOWAIT);
293 		pckbc_init_slotdata(t->t_slotdata[slot]);
294 	}
295 #if NRND > 0
296 	rnd_attach_source(&t->t_slotdata[slot]->rnd_source, sc->subname[slot],
297 	    RND_TYPE_TTY, 0);
298 #endif
299 	return (found);
300 }
301 
302 void
303 pckbc_attach(sc)
304 	struct pckbc_softc *sc;
305 {
306 	struct pckbc_internal *t;
307 	bus_space_tag_t iot;
308 	bus_space_handle_t ioh_d, ioh_c;
309 	int res;
310 	u_char cmdbits = 0;
311 
312 	t = sc->id;
313 	iot = t->t_iot;
314 	ioh_d = t->t_ioh_d;
315 	ioh_c = t->t_ioh_c;
316 
317 	/* flush */
318 	(void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
319 
320 	/* set initial cmd byte */
321 	if (!pckbc_put8042cmd(t)) {
322 		printf("kbc: cmd word write error\n");
323 		return;
324 	}
325 
326 /*
327  * XXX Don't check the keyboard port. There are broken keyboard controllers
328  * which don't pass the test but work normally otherwise.
329  */
330 #if 0
331 	/*
332 	 * check kbd port ok
333 	 */
334 	if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST))
335 		return;
336 	res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
337 
338 	/*
339 	 * Normally, we should get a "0" here.
340 	 * But there are keyboard controllers behaving differently.
341 	 */
342 	if (res == 0 || res == 0xfa || res == 0x01 || res == 0xab) {
343 #ifdef PCKBCDEBUG
344 		if (res != 0)
345 			printf("kbc: returned %x on kbd slot test\n", res);
346 #endif
347 		if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
348 			cmdbits |= KC8_KENABLE;
349 	} else {
350 		printf("kbc: kbd port test: %x\n", res);
351 		return;
352 	}
353 #else
354 	if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
355 		cmdbits |= KC8_KENABLE;
356 #endif /* 0 */
357 
358 	/*
359 	 * check aux port ok
360 	 */
361 	if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXTEST))
362 		return;
363 	res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
364 
365 	if (res == 0 || res == 0xfa || res == 0x01) {
366 #ifdef PCKBCDEBUG
367 		if (res != 0)
368 			printf("kbc: returned %x on aux slot test\n", res);
369 #endif
370 		t->t_haveaux = 1;
371 		if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT))
372 			cmdbits |= KC8_MENABLE;
373 	}
374 #ifdef PCKBCDEBUG
375 	  else
376 		printf("kbc: aux port test: %x\n", res);
377 #endif
378 
379 	/* enable needed interrupts */
380 	t->t_cmdbyte |= cmdbits;
381 	if (!pckbc_put8042cmd(t))
382 		printf("kbc: cmd word write error\n");
383 }
384 
385 int
386 pckbcprint(aux, pnp)
387 	void *aux;
388 	const char *pnp;
389 {
390 	struct pckbc_attach_args *pa = aux;
391 
392 	if (!pnp)
393 		printf(" (%s slot)", pckbc_slot_names[pa->pa_slot]);
394 	return (QUIET);
395 }
396 
397 void
398 pckbc_init_slotdata(q)
399 	struct pckbc_slotdata *q;
400 {
401 	int i;
402 	TAILQ_INIT(&q->cmdqueue);
403 	TAILQ_INIT(&q->freequeue);
404 
405 	for (i=0; i<NCMD; i++) {
406 		TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
407 	}
408 	q->polling = 0;
409 }
410 
411 void
412 pckbc_flush(self, slot)
413 	pckbc_tag_t self;
414 	pckbc_slot_t slot;
415 {
416 	struct pckbc_internal *t = self;
417 
418 	(void) pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
419 				slot, t->t_haveaux);
420 }
421 
422 int
423 pckbc_poll_data(self, slot)
424 	pckbc_tag_t self;
425 	pckbc_slot_t slot;
426 {
427 	struct pckbc_internal *t = self;
428 	struct pckbc_slotdata *q = t->t_slotdata[slot];
429 	int c;
430 
431 	c = pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
432 			     slot, t->t_haveaux);
433 	if (c != -1 && q && CMD_IN_QUEUE(q)) {
434 		/* we jumped into a running command - try to
435 		 deliver the response */
436 		if (pckbc_cmdresponse(t, slot, c))
437 			return (-1);
438 	}
439 	return (c);
440 }
441 
442 /*
443  * switch scancode translation on / off
444  * return nonzero on success
445  */
446 int
447 pckbc_xt_translation(self, slot, on)
448 	pckbc_tag_t self;
449 	pckbc_slot_t slot;
450 	int on;
451 {
452 	struct pckbc_internal *t = self;
453 	int ison;
454 
455 	if (slot != PCKBC_KBD_SLOT) {
456 		/* translation only for kbd slot */
457 		if (on)
458 			return (0);
459 		else
460 			return (1);
461 	}
462 
463 	ison = t->t_cmdbyte & KC8_TRANS;
464 	if ((on && ison) || (!on && !ison))
465 		return (1);
466 
467 	t->t_cmdbyte ^= KC8_TRANS;
468 	if (!pckbc_put8042cmd(t))
469 		return (0);
470 
471 	/* read back to be sure */
472 	if (!pckbc_get8042cmd(t))
473 		return (0);
474 
475 	ison = t->t_cmdbyte & KC8_TRANS;
476 	if ((on && ison) || (!on && !ison))
477 		return (1);
478 	return (0);
479 }
480 
481 static struct pckbc_portcmd {
482 	u_char cmd_en, cmd_dis;
483 } pckbc_portcmd[2] = {
484 	{
485 		KBC_KBDENABLE, KBC_KBDDISABLE,
486 	}, {
487 		KBC_AUXENABLE, KBC_AUXDISABLE,
488 	}
489 };
490 
491 void
492 pckbc_slot_enable(self, slot, on)
493 	pckbc_tag_t self;
494 	pckbc_slot_t slot;
495 	int on;
496 {
497 	struct pckbc_internal *t = (struct pckbc_internal *)self;
498 	struct pckbc_portcmd *cmd;
499 
500 	cmd = &pckbc_portcmd[slot];
501 
502 	if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c,
503 			    on ? cmd->cmd_en : cmd->cmd_dis))
504 		printf("pckbc_slot_enable(%d) failed\n", on);
505 }
506 
507 void
508 pckbc_set_poll(self, slot, on)
509 	pckbc_tag_t self;
510 	pckbc_slot_t slot;
511 	int on;
512 {
513 	struct pckbc_internal *t = (struct pckbc_internal *)self;
514 
515 	t->t_slotdata[slot]->polling = on;
516 
517 	if (!on) {
518                 int s;
519 
520                 /*
521                  * If disabling polling on a device that's been configured,
522                  * make sure there are no bytes left in the FIFO, holding up
523                  * the interrupt line.  Otherwise we won't get any further
524                  * interrupts.
525                  */
526 		if (t->t_sc) {
527 			s = spltty();
528 			pckbcintr(t->t_sc);
529 			splx(s);
530 		}
531 	}
532 }
533 
534 /*
535  * Pass command to device, poll for ACK and data.
536  * to be called at spltty()
537  */
538 static void
539 pckbc_poll_cmd1(t, slot, cmd)
540 	struct pckbc_internal *t;
541 	pckbc_slot_t slot;
542 	struct pckbc_devcmd *cmd;
543 {
544 	bus_space_tag_t iot = t->t_iot;
545 	bus_space_handle_t ioh_d = t->t_ioh_d;
546 	bus_space_handle_t ioh_c = t->t_ioh_c;
547 	int i, c = 0;
548 
549 	while (cmd->cmdidx < cmd->cmdlen) {
550 		if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
551 			printf("pckbc_cmd: send error\n");
552 			cmd->status = EIO;
553 			return;
554 		}
555 		for (i = 10; i; i--) { /* 1s ??? */
556 			c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
557 					     t->t_haveaux);
558 			if (c != -1)
559 				break;
560 		}
561 
562 		if (c == KBC_DEVCMD_ACK) {
563 			cmd->cmdidx++;
564 			continue;
565 		}
566 		if (c == KBC_DEVCMD_RESEND) {
567 #ifdef PCKBCDEBUG
568 			printf("pckbc_cmd: RESEND\n");
569 #endif
570 			if (cmd->retries++ < 5)
571 				continue;
572 			else {
573 #ifdef PCKBCDEBUG
574 				printf("pckbc: cmd failed\n");
575 #endif
576 				cmd->status = EIO;
577 				return;
578 			}
579 		}
580 		if (c == -1) {
581 #ifdef PCKBCDEBUG
582 			printf("pckbc_cmd: timeout\n");
583 #endif
584 			cmd->status = EIO;
585 			return;
586 		}
587 #ifdef PCKBCDEBUG
588 		printf("pckbc_cmd: lost 0x%x\n", c);
589 #endif
590 	}
591 
592 	while (cmd->responseidx < cmd->responselen) {
593 		if (cmd->flags & KBC_CMDFLAG_SLOW)
594 			i = 100; /* 10s ??? */
595 		else
596 			i = 10; /* 1s ??? */
597 		while (i--) {
598 			c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
599 					     t->t_haveaux);
600 			if (c != -1)
601 				break;
602 		}
603 		if (c == -1) {
604 #ifdef PCKBCDEBUG
605 			printf("pckbc_cmd: no data\n");
606 #endif
607 			cmd->status = ETIMEDOUT;
608 			return;
609 		} else
610 			cmd->response[cmd->responseidx++] = c;
611 	}
612 }
613 
614 /* for use in autoconfiguration */
615 int
616 pckbc_poll_cmd(self, slot, cmd, len, responselen, respbuf, slow)
617 	pckbc_tag_t self;
618 	pckbc_slot_t slot;
619 	u_char *cmd;
620 	int len, responselen;
621 	u_char *respbuf;
622 	int slow;
623 {
624 	struct pckbc_internal *t = self;
625 	struct pckbc_devcmd nc;
626 
627 	if ((len > 4) || (responselen > 4))
628 		return (EINVAL);
629 
630 	bzero(&nc, sizeof(nc));
631 	bcopy(cmd, nc.cmd, len);
632 	nc.cmdlen = len;
633 	nc.responselen = responselen;
634 	nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
635 
636 	pckbc_poll_cmd1(t, slot, &nc);
637 
638 	if (nc.status == 0 && respbuf)
639 		bcopy(nc.response, respbuf, responselen);
640 
641 	return (nc.status);
642 }
643 
644 /*
645  * Clean up a command queue, throw away everything.
646  */
647 void
648 pckbc_cleanqueue(q)
649 	struct pckbc_slotdata *q;
650 {
651 	struct pckbc_devcmd *cmd;
652 #ifdef PCKBCDEBUG
653 	int i;
654 #endif
655 
656 	while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
657 		TAILQ_REMOVE(&q->cmdqueue, cmd, next);
658 #ifdef PCKBCDEBUG
659 		printf("pckbc_cleanqueue: removing");
660 		for (i = 0; i < cmd->cmdlen; i++)
661 			printf(" %02x", cmd->cmd[i]);
662 		printf("\n");
663 #endif
664 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
665 	}
666 }
667 
668 /*
669  * Timeout error handler: clean queues and data port.
670  * XXX could be less invasive.
671  */
672 void
673 pckbc_cleanup(self)
674 	void *self;
675 {
676 	struct pckbc_internal *t = self;
677 	int s;
678 
679 	printf("pckbc: command timeout\n");
680 
681 	s = spltty();
682 
683 	if (t->t_slotdata[PCKBC_KBD_SLOT])
684 		pckbc_cleanqueue(t->t_slotdata[PCKBC_KBD_SLOT]);
685 	if (t->t_slotdata[PCKBC_AUX_SLOT])
686 		pckbc_cleanqueue(t->t_slotdata[PCKBC_AUX_SLOT]);
687 
688 	while (bus_space_read_1(t->t_iot, t->t_ioh_c, 0) & KBS_DIB) {
689 		KBD_DELAY;
690 		(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
691 	}
692 
693 	/* reset KBC? */
694 
695 	splx(s);
696 }
697 
698 /*
699  * Pass command to device during normal operation.
700  * to be called at spltty()
701  */
702 void
703 pckbc_start(t, slot)
704 	struct pckbc_internal *t;
705 	pckbc_slot_t slot;
706 {
707 	struct pckbc_slotdata *q = t->t_slotdata[slot];
708 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
709 
710 	if (q->polling) {
711 		do {
712 			pckbc_poll_cmd1(t, slot, cmd);
713 			if (cmd->status)
714 				printf("pckbc_start: command error\n");
715 
716 			TAILQ_REMOVE(&q->cmdqueue, cmd, next);
717 			if (cmd->flags & KBC_CMDFLAG_SYNC)
718 				wakeup(cmd);
719 			else {
720 				callout_stop(&t->t_cleanup);
721 				TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
722 			}
723 			cmd = TAILQ_FIRST(&q->cmdqueue);
724 		} while (cmd);
725 		return;
726 	}
727 
728 	if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
729 		printf("pckbc_start: send error\n");
730 		/* XXX what now? */
731 		return;
732 	}
733 }
734 
735 /*
736  * Handle command responses coming in asynchonously,
737  * return nonzero if valid response.
738  * to be called at spltty()
739  */
740 int
741 pckbc_cmdresponse(t, slot, data)
742 	struct pckbc_internal *t;
743 	pckbc_slot_t slot;
744 	u_char data;
745 {
746 	struct pckbc_slotdata *q = t->t_slotdata[slot];
747 	struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
748 #ifdef DIAGNOSTIC
749 	if (!cmd)
750 		panic("pckbc_cmdresponse: no active command");
751 #endif
752 	if (cmd->cmdidx < cmd->cmdlen) {
753 		if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
754 			return (0);
755 
756 		if (data == KBC_DEVCMD_RESEND) {
757 			if (cmd->retries++ < 5) {
758 				/* try again last command */
759 				goto restart;
760 			} else {
761 				printf("pckbc: cmd failed\n");
762 				cmd->status = EIO;
763 				/* dequeue */
764 			}
765 		} else {
766 			if (++cmd->cmdidx < cmd->cmdlen)
767 				goto restart;
768 			if (cmd->responselen)
769 				return (1);
770 			/* else dequeue */
771 		}
772 	} else if (cmd->responseidx < cmd->responselen) {
773 		cmd->response[cmd->responseidx++] = data;
774 		if (cmd->responseidx < cmd->responselen)
775 			return (1);
776 		/* else dequeue */
777 	} else
778 		return (0);
779 
780 	/* dequeue: */
781 	TAILQ_REMOVE(&q->cmdqueue, cmd, next);
782 	if (cmd->flags & KBC_CMDFLAG_SYNC)
783 		wakeup(cmd);
784 	else {
785 		callout_stop(&t->t_cleanup);
786 		TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
787 	}
788 	if (!CMD_IN_QUEUE(q))
789 		return (1);
790 restart:
791 	pckbc_start(t, slot);
792 	return (1);
793 }
794 
795 /*
796  * Put command into the device's command queue, return zero or errno.
797  */
798 int
799 pckbc_enqueue_cmd(self, slot, cmd, len, responselen, sync, respbuf)
800 	pckbc_tag_t self;
801 	pckbc_slot_t slot;
802 	u_char *cmd;
803 	int len, responselen, sync;
804 	u_char *respbuf;
805 {
806 	struct pckbc_internal *t = self;
807 	struct pckbc_slotdata *q = t->t_slotdata[slot];
808 	struct pckbc_devcmd *nc;
809 	int s, isactive, res = 0;
810 
811 	if ((len > 4) || (responselen > 4))
812 		return (EINVAL);
813 	s = spltty();
814 	nc = TAILQ_FIRST(&q->freequeue);
815 	if (nc) {
816 		TAILQ_REMOVE(&q->freequeue, nc, next);
817 	}
818 	splx(s);
819 	if (!nc)
820 		return (ENOMEM);
821 
822 	bzero(nc, sizeof(*nc));
823 	bcopy(cmd, nc->cmd, len);
824 	nc->cmdlen = len;
825 	nc->responselen = responselen;
826 	nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
827 
828 	s = spltty();
829 
830 	if (q->polling && sync) {
831 		/*
832 		 * XXX We should poll until the queue is empty.
833 		 * But we don't come here normally, so make
834 		 * it simple and throw away everything.
835 		 */
836 		pckbc_cleanqueue(q);
837 	}
838 
839 	isactive = CMD_IN_QUEUE(q);
840 	TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
841 	if (!isactive)
842 		pckbc_start(t, slot);
843 
844 	if (q->polling)
845 		res = (sync ? nc->status : 0);
846 	else if (sync) {
847 		if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
848 			TAILQ_REMOVE(&q->cmdqueue, nc, next);
849 			pckbc_cleanup(t);
850 		} else
851 			res = nc->status;
852 	} else
853 		callout_reset(&t->t_cleanup, hz, pckbc_cleanup, t);
854 
855 	if (sync) {
856 		if (respbuf)
857 			bcopy(nc->response, respbuf, responselen);
858 		TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
859 	}
860 
861 	splx(s);
862 
863 	return (res);
864 }
865 
866 void
867 pckbc_set_inputhandler(self, slot, func, arg, name)
868 	pckbc_tag_t self;
869 	pckbc_slot_t slot;
870 	pckbc_inputfcn func;
871 	void *arg;
872 	char *name;
873 {
874 	struct pckbc_internal *t = (struct pckbc_internal *)self;
875 	struct pckbc_softc *sc = t->t_sc;
876 
877 	if (slot >= PCKBC_NSLOTS)
878 		panic("pckbc_set_inputhandler: bad slot %d", slot);
879 
880 	(*sc->intr_establish)(sc, slot);
881 
882 	sc->inputhandler[slot] = func;
883 	sc->inputarg[slot] = arg;
884 	sc->subname[slot] = name;
885 }
886 
887 int
888 pckbcintr(vsc)
889 	void *vsc;
890 {
891 	struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
892 	struct pckbc_internal *t = sc->id;
893 	u_char stat;
894 	pckbc_slot_t slot;
895 	struct pckbc_slotdata *q;
896 	int served = 0, data;
897 
898 	for(;;) {
899 		stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
900 		if (!(stat & KBS_DIB))
901 			break;
902 
903 		served = 1;
904 
905 		slot = (t->t_haveaux && (stat & 0x20)) ?
906 		    PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
907 		q = t->t_slotdata[slot];
908 
909 		if (!q) {
910 			/* XXX do something for live insertion? */
911 			printf("pckbcintr: no dev for slot %d\n", slot);
912 			KBD_DELAY;
913 			(void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
914 			continue;
915 		}
916 
917 		if (q->polling)
918 			break; /* pckbc_poll_data() will get it */
919 
920 		KBD_DELAY;
921 		data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
922 
923 #if NRND > 0
924 		rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
925 #endif
926 		if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data))
927 			continue;
928 
929 		if (sc->inputhandler[slot])
930 			(*sc->inputhandler[slot])(sc->inputarg[slot], data);
931 #ifdef PCKBCDEBUG
932 		else
933 			printf("pckbcintr: slot %d lost %d\n", slot, data);
934 #endif
935 	}
936 
937 	return (served);
938 }
939 
940 int
941 pckbc_cnattach(iot, addr, slot)
942 	bus_space_tag_t iot;
943 	bus_addr_t addr;
944 	pckbc_slot_t slot;
945 {
946 	bus_space_handle_t ioh_d, ioh_c;
947 	int res = 0;
948 
949 	if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d))
950                 return (ENXIO);
951 	if (bus_space_map(iot, addr + KBCMDP, 1, 0, &ioh_c)) {
952 		bus_space_unmap(iot, ioh_d, 1);
953                 return (ENXIO);
954 	}
955 
956 	pckbc_consdata.t_iot = iot;
957 	pckbc_consdata.t_ioh_d = ioh_d;
958 	pckbc_consdata.t_ioh_c = ioh_c;
959 	pckbc_consdata.t_addr = addr;
960 	callout_init(&pckbc_consdata.t_cleanup);
961 
962 	/* flush */
963 	(void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
964 
965 	/* selftest? */
966 
967 	/* init cmd byte, enable ports */
968 	pckbc_consdata.t_cmdbyte = KC8_CPU;
969 	if (!pckbc_put8042cmd(&pckbc_consdata)) {
970 		printf("kbc: cmd word write error\n");
971 		res = EIO;
972 	}
973 
974 	if (!res) {
975 #if (NPCKBD > 0)
976 		res = pckbd_cnattach(&pckbc_consdata, slot);
977 #else
978 		/*
979 		 * XXX This should be replaced with the `notyet' case
980 		 * XXX when all of the old PC-style console drivers
981 		 * XXX have gone away.  When that happens, all of
982 		 * XXX the pckbc_machdep_cnattach() should be purged,
983 		 * XXX as well.
984 		 */
985 #ifdef notyet
986 		res = ENXIO;
987 #else
988 		res = pckbc_machdep_cnattach(&pckbc_consdata, slot);
989 #endif
990 #endif /* NPCKBD > 0 */
991 	}
992 
993 	if (res) {
994 		bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1);
995 		bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1);
996 	} else {
997 		pckbc_consdata.t_slotdata[slot] = &pckbc_cons_slotdata;
998 		pckbc_init_slotdata(&pckbc_cons_slotdata);
999 		pckbc_console = 1;
1000 	}
1001 
1002 	return (res);
1003 }
1004