pckbc.c revision 1.7 1 /* $NetBSD: pckbc.c,v 1.7 2001/05/15 22:01:07 christos 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 if (found && (t->t_slotdata[slot] != NULL))
297 rnd_attach_source(&t->t_slotdata[slot]->rnd_source, sc->subname[slot],
298 RND_TYPE_TTY, 0);
299 #endif
300 return (found);
301 }
302
303 void
304 pckbc_attach(sc)
305 struct pckbc_softc *sc;
306 {
307 struct pckbc_internal *t;
308 bus_space_tag_t iot;
309 bus_space_handle_t ioh_d, ioh_c;
310 int res;
311 u_char cmdbits = 0;
312
313 t = sc->id;
314 iot = t->t_iot;
315 ioh_d = t->t_ioh_d;
316 ioh_c = t->t_ioh_c;
317
318 /* flush */
319 (void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
320
321 /* set initial cmd byte */
322 if (!pckbc_put8042cmd(t)) {
323 printf("kbc: cmd word write error\n");
324 return;
325 }
326
327 /*
328 * XXX Don't check the keyboard port. There are broken keyboard controllers
329 * which don't pass the test but work normally otherwise.
330 */
331 #if 0
332 /*
333 * check kbd port ok
334 */
335 if (!pckbc_send_cmd(iot, ioh_c, KBC_KBDTEST))
336 return;
337 res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
338
339 /*
340 * Normally, we should get a "0" here.
341 * But there are keyboard controllers behaving differently.
342 */
343 if (res == 0 || res == 0xfa || res == 0x01 || res == 0xab) {
344 #ifdef PCKBCDEBUG
345 if (res != 0)
346 printf("kbc: returned %x on kbd slot test\n", res);
347 #endif
348 if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
349 cmdbits |= KC8_KENABLE;
350 } else {
351 printf("kbc: kbd port test: %x\n", res);
352 return;
353 }
354 #else
355 if (pckbc_attach_slot(sc, PCKBC_KBD_SLOT))
356 cmdbits |= KC8_KENABLE;
357 #endif /* 0 */
358
359 /*
360 * Check aux port ok.
361 * Avoid KBC_AUXTEST because it hangs some older controllers
362 * (eg UMC880?).
363 */
364 if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXECHO)) {
365 printf("kbc: aux echo error 1\n");
366 goto nomouse;
367 }
368 if (!pckbc_wait_output(iot, ioh_c)) {
369 printf("kbc: aux echo error 2\n");
370 goto nomouse;
371 }
372 bus_space_write_1(iot, ioh_d, 0, 0x5a); /* a random value */
373 res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_AUX_SLOT, 1);
374 if (res == 0x5a) {
375 t->t_haveaux = 1;
376 } else {
377 /*
378 * Some old controllers (Gateway 2000 circa 1993)
379 * return 0xfe here. So we have no choice and we try
380 * the auxtest. Hopefully this will not affect the
381 * old controllers where auxtest fails, since they
382 * will be handled in the case above.
383 */
384 #ifdef PCKBCDEBUG
385 printf("kbc: aux echo: %x\n", res);
386 #endif
387 /*
388 * check aux port ok
389 */
390 if (!pckbc_send_cmd(iot, ioh_c, KBC_AUXTEST))
391 return;
392 res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
393
394 if (res == 0 || res == 0xfa || res == 0x01) {
395 #ifdef PCKBCDEBUG
396 if (res != 0)
397 printf("kbc: returned %x on aux slot test\n",
398 res);
399 #endif
400 t->t_haveaux = 1;
401 }
402 }
403 if (t->t_haveaux == 1) {
404 if (pckbc_attach_slot(sc, PCKBC_AUX_SLOT))
405 cmdbits |= KC8_MENABLE;
406 }
407
408 nomouse:
409 /* enable needed interrupts */
410 t->t_cmdbyte |= cmdbits;
411 if (!pckbc_put8042cmd(t))
412 printf("kbc: cmd word write error\n");
413 }
414
415 int
416 pckbcprint(aux, pnp)
417 void *aux;
418 const char *pnp;
419 {
420 struct pckbc_attach_args *pa = aux;
421
422 if (!pnp)
423 printf(" (%s slot)", pckbc_slot_names[pa->pa_slot]);
424 return (QUIET);
425 }
426
427 void
428 pckbc_init_slotdata(q)
429 struct pckbc_slotdata *q;
430 {
431 int i;
432 TAILQ_INIT(&q->cmdqueue);
433 TAILQ_INIT(&q->freequeue);
434
435 for (i = 0; i < NCMD; i++) {
436 TAILQ_INSERT_TAIL(&q->freequeue, &(q->cmds[i]), next);
437 }
438 q->polling = 0;
439 }
440
441 void
442 pckbc_flush(self, slot)
443 pckbc_tag_t self;
444 pckbc_slot_t slot;
445 {
446 struct pckbc_internal *t = self;
447
448 (void) pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
449 slot, t->t_haveaux);
450 }
451
452 int
453 pckbc_poll_data(self, slot)
454 pckbc_tag_t self;
455 pckbc_slot_t slot;
456 {
457 struct pckbc_internal *t = self;
458 struct pckbc_slotdata *q = t->t_slotdata[slot];
459 int c;
460
461 c = pckbc_poll_data1(t->t_iot, t->t_ioh_d, t->t_ioh_c,
462 slot, t->t_haveaux);
463 if (c != -1 && q && CMD_IN_QUEUE(q)) {
464 /* we jumped into a running command - try to
465 deliver the response */
466 if (pckbc_cmdresponse(t, slot, c))
467 return (-1);
468 }
469 return (c);
470 }
471
472 /*
473 * switch scancode translation on / off
474 * return nonzero on success
475 */
476 int
477 pckbc_xt_translation(self, slot, on)
478 pckbc_tag_t self;
479 pckbc_slot_t slot;
480 int on;
481 {
482 struct pckbc_internal *t = self;
483 int ison;
484
485 if (slot != PCKBC_KBD_SLOT) {
486 /* translation only for kbd slot */
487 if (on)
488 return (0);
489 else
490 return (1);
491 }
492
493 ison = t->t_cmdbyte & KC8_TRANS;
494 if ((on && ison) || (!on && !ison))
495 return (1);
496
497 t->t_cmdbyte ^= KC8_TRANS;
498 if (!pckbc_put8042cmd(t))
499 return (0);
500
501 /* read back to be sure */
502 if (!pckbc_get8042cmd(t))
503 return (0);
504
505 ison = t->t_cmdbyte & KC8_TRANS;
506 if ((on && ison) || (!on && !ison))
507 return (1);
508 return (0);
509 }
510
511 static struct pckbc_portcmd {
512 u_char cmd_en, cmd_dis;
513 } pckbc_portcmd[2] = {
514 {
515 KBC_KBDENABLE, KBC_KBDDISABLE,
516 }, {
517 KBC_AUXENABLE, KBC_AUXDISABLE,
518 }
519 };
520
521 void
522 pckbc_slot_enable(self, slot, on)
523 pckbc_tag_t self;
524 pckbc_slot_t slot;
525 int on;
526 {
527 struct pckbc_internal *t = (struct pckbc_internal *)self;
528 struct pckbc_portcmd *cmd;
529
530 cmd = &pckbc_portcmd[slot];
531
532 if (!pckbc_send_cmd(t->t_iot, t->t_ioh_c,
533 on ? cmd->cmd_en : cmd->cmd_dis))
534 printf("pckbc_slot_enable(%d) failed\n", on);
535 }
536
537 void
538 pckbc_set_poll(self, slot, on)
539 pckbc_tag_t self;
540 pckbc_slot_t slot;
541 int on;
542 {
543 struct pckbc_internal *t = (struct pckbc_internal *)self;
544
545 t->t_slotdata[slot]->polling = on;
546
547 if (!on) {
548 int s;
549
550 /*
551 * If disabling polling on a device that's been configured,
552 * make sure there are no bytes left in the FIFO, holding up
553 * the interrupt line. Otherwise we won't get any further
554 * interrupts.
555 */
556 if (t->t_sc) {
557 s = spltty();
558 pckbcintr(t->t_sc);
559 splx(s);
560 }
561 }
562 }
563
564 /*
565 * Pass command to device, poll for ACK and data.
566 * to be called at spltty()
567 */
568 static void
569 pckbc_poll_cmd1(t, slot, cmd)
570 struct pckbc_internal *t;
571 pckbc_slot_t slot;
572 struct pckbc_devcmd *cmd;
573 {
574 bus_space_tag_t iot = t->t_iot;
575 bus_space_handle_t ioh_d = t->t_ioh_d;
576 bus_space_handle_t ioh_c = t->t_ioh_c;
577 int i, c = 0;
578
579 while (cmd->cmdidx < cmd->cmdlen) {
580 if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
581 printf("pckbc_cmd: send error\n");
582 cmd->status = EIO;
583 return;
584 }
585 for (i = 10; i; i--) { /* 1s ??? */
586 c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
587 t->t_haveaux);
588 if (c != -1)
589 break;
590 }
591
592 if (c == KBC_DEVCMD_ACK) {
593 cmd->cmdidx++;
594 continue;
595 }
596 if (c == KBC_DEVCMD_RESEND) {
597 #ifdef PCKBCDEBUG
598 printf("pckbc_cmd: RESEND\n");
599 #endif
600 if (cmd->retries++ < 5)
601 continue;
602 else {
603 #ifdef PCKBCDEBUG
604 printf("pckbc: cmd failed\n");
605 #endif
606 cmd->status = EIO;
607 return;
608 }
609 }
610 if (c == -1) {
611 #ifdef PCKBCDEBUG
612 printf("pckbc_cmd: timeout\n");
613 #endif
614 cmd->status = EIO;
615 return;
616 }
617 #ifdef PCKBCDEBUG
618 printf("pckbc_cmd: lost 0x%x\n", c);
619 #endif
620 }
621
622 while (cmd->responseidx < cmd->responselen) {
623 if (cmd->flags & KBC_CMDFLAG_SLOW)
624 i = 100; /* 10s ??? */
625 else
626 i = 10; /* 1s ??? */
627 while (i--) {
628 c = pckbc_poll_data1(iot, ioh_d, ioh_c, slot,
629 t->t_haveaux);
630 if (c != -1)
631 break;
632 }
633 if (c == -1) {
634 #ifdef PCKBCDEBUG
635 printf("pckbc_cmd: no data\n");
636 #endif
637 cmd->status = ETIMEDOUT;
638 return;
639 } else
640 cmd->response[cmd->responseidx++] = c;
641 }
642 }
643
644 /* for use in autoconfiguration */
645 int
646 pckbc_poll_cmd(self, slot, cmd, len, responselen, respbuf, slow)
647 pckbc_tag_t self;
648 pckbc_slot_t slot;
649 u_char *cmd;
650 int len, responselen;
651 u_char *respbuf;
652 int slow;
653 {
654 struct pckbc_internal *t = self;
655 struct pckbc_devcmd nc;
656
657 if ((len > 4) || (responselen > 4))
658 return (EINVAL);
659
660 bzero(&nc, sizeof(nc));
661 bcopy(cmd, nc.cmd, len);
662 nc.cmdlen = len;
663 nc.responselen = responselen;
664 nc.flags = (slow ? KBC_CMDFLAG_SLOW : 0);
665
666 pckbc_poll_cmd1(t, slot, &nc);
667
668 if (nc.status == 0 && respbuf)
669 bcopy(nc.response, respbuf, responselen);
670
671 return (nc.status);
672 }
673
674 /*
675 * Clean up a command queue, throw away everything.
676 */
677 void
678 pckbc_cleanqueue(q)
679 struct pckbc_slotdata *q;
680 {
681 struct pckbc_devcmd *cmd;
682 #ifdef PCKBCDEBUG
683 int i;
684 #endif
685
686 while ((cmd = TAILQ_FIRST(&q->cmdqueue))) {
687 TAILQ_REMOVE(&q->cmdqueue, cmd, next);
688 #ifdef PCKBCDEBUG
689 printf("pckbc_cleanqueue: removing");
690 for (i = 0; i < cmd->cmdlen; i++)
691 printf(" %02x", cmd->cmd[i]);
692 printf("\n");
693 #endif
694 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
695 }
696 }
697
698 /*
699 * Timeout error handler: clean queues and data port.
700 * XXX could be less invasive.
701 */
702 void
703 pckbc_cleanup(self)
704 void *self;
705 {
706 struct pckbc_internal *t = self;
707 int s;
708
709 printf("pckbc: command timeout\n");
710
711 s = spltty();
712
713 if (t->t_slotdata[PCKBC_KBD_SLOT])
714 pckbc_cleanqueue(t->t_slotdata[PCKBC_KBD_SLOT]);
715 if (t->t_slotdata[PCKBC_AUX_SLOT])
716 pckbc_cleanqueue(t->t_slotdata[PCKBC_AUX_SLOT]);
717
718 while (bus_space_read_1(t->t_iot, t->t_ioh_c, 0) & KBS_DIB) {
719 KBD_DELAY;
720 (void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
721 }
722
723 /* reset KBC? */
724
725 splx(s);
726 }
727
728 /*
729 * Pass command to device during normal operation.
730 * to be called at spltty()
731 */
732 void
733 pckbc_start(t, slot)
734 struct pckbc_internal *t;
735 pckbc_slot_t slot;
736 {
737 struct pckbc_slotdata *q = t->t_slotdata[slot];
738 struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
739
740 if (q->polling) {
741 do {
742 pckbc_poll_cmd1(t, slot, cmd);
743 if (cmd->status)
744 printf("pckbc_start: command error\n");
745
746 TAILQ_REMOVE(&q->cmdqueue, cmd, next);
747 if (cmd->flags & KBC_CMDFLAG_SYNC)
748 wakeup(cmd);
749 else {
750 callout_stop(&t->t_cleanup);
751 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
752 }
753 cmd = TAILQ_FIRST(&q->cmdqueue);
754 } while (cmd);
755 return;
756 }
757
758 if (!pckbc_send_devcmd(t, slot, cmd->cmd[cmd->cmdidx])) {
759 printf("pckbc_start: send error\n");
760 /* XXX what now? */
761 return;
762 }
763 }
764
765 /*
766 * Handle command responses coming in asynchonously,
767 * return nonzero if valid response.
768 * to be called at spltty()
769 */
770 int
771 pckbc_cmdresponse(t, slot, data)
772 struct pckbc_internal *t;
773 pckbc_slot_t slot;
774 u_char data;
775 {
776 struct pckbc_slotdata *q = t->t_slotdata[slot];
777 struct pckbc_devcmd *cmd = TAILQ_FIRST(&q->cmdqueue);
778 #ifdef DIAGNOSTIC
779 if (!cmd)
780 panic("pckbc_cmdresponse: no active command");
781 #endif
782 if (cmd->cmdidx < cmd->cmdlen) {
783 if (data != KBC_DEVCMD_ACK && data != KBC_DEVCMD_RESEND)
784 return (0);
785
786 if (data == KBC_DEVCMD_RESEND) {
787 if (cmd->retries++ < 5) {
788 /* try again last command */
789 goto restart;
790 } else {
791 printf("pckbc: cmd failed\n");
792 cmd->status = EIO;
793 /* dequeue */
794 }
795 } else {
796 if (++cmd->cmdidx < cmd->cmdlen)
797 goto restart;
798 if (cmd->responselen)
799 return (1);
800 /* else dequeue */
801 }
802 } else if (cmd->responseidx < cmd->responselen) {
803 cmd->response[cmd->responseidx++] = data;
804 if (cmd->responseidx < cmd->responselen)
805 return (1);
806 /* else dequeue */
807 } else
808 return (0);
809
810 /* dequeue: */
811 TAILQ_REMOVE(&q->cmdqueue, cmd, next);
812 if (cmd->flags & KBC_CMDFLAG_SYNC)
813 wakeup(cmd);
814 else {
815 callout_stop(&t->t_cleanup);
816 TAILQ_INSERT_TAIL(&q->freequeue, cmd, next);
817 }
818 if (!CMD_IN_QUEUE(q))
819 return (1);
820 restart:
821 pckbc_start(t, slot);
822 return (1);
823 }
824
825 /*
826 * Put command into the device's command queue, return zero or errno.
827 */
828 int
829 pckbc_enqueue_cmd(self, slot, cmd, len, responselen, sync, respbuf)
830 pckbc_tag_t self;
831 pckbc_slot_t slot;
832 u_char *cmd;
833 int len, responselen, sync;
834 u_char *respbuf;
835 {
836 struct pckbc_internal *t = self;
837 struct pckbc_slotdata *q = t->t_slotdata[slot];
838 struct pckbc_devcmd *nc;
839 int s, isactive, res = 0;
840
841 if ((len > 4) || (responselen > 4))
842 return (EINVAL);
843 s = spltty();
844 nc = TAILQ_FIRST(&q->freequeue);
845 if (nc) {
846 TAILQ_REMOVE(&q->freequeue, nc, next);
847 }
848 splx(s);
849 if (!nc)
850 return (ENOMEM);
851
852 bzero(nc, sizeof(*nc));
853 bcopy(cmd, nc->cmd, len);
854 nc->cmdlen = len;
855 nc->responselen = responselen;
856 nc->flags = (sync ? KBC_CMDFLAG_SYNC : 0);
857
858 s = spltty();
859
860 if (q->polling && sync) {
861 /*
862 * XXX We should poll until the queue is empty.
863 * But we don't come here normally, so make
864 * it simple and throw away everything.
865 */
866 pckbc_cleanqueue(q);
867 }
868
869 isactive = CMD_IN_QUEUE(q);
870 TAILQ_INSERT_TAIL(&q->cmdqueue, nc, next);
871 if (!isactive)
872 pckbc_start(t, slot);
873
874 if (q->polling)
875 res = (sync ? nc->status : 0);
876 else if (sync) {
877 if ((res = tsleep(nc, 0, "kbccmd", 1*hz))) {
878 TAILQ_REMOVE(&q->cmdqueue, nc, next);
879 pckbc_cleanup(t);
880 } else
881 res = nc->status;
882 } else
883 callout_reset(&t->t_cleanup, hz, pckbc_cleanup, t);
884
885 if (sync) {
886 if (respbuf)
887 bcopy(nc->response, respbuf, responselen);
888 TAILQ_INSERT_TAIL(&q->freequeue, nc, next);
889 }
890
891 splx(s);
892
893 return (res);
894 }
895
896 void
897 pckbc_set_inputhandler(self, slot, func, arg, name)
898 pckbc_tag_t self;
899 pckbc_slot_t slot;
900 pckbc_inputfcn func;
901 void *arg;
902 char *name;
903 {
904 struct pckbc_internal *t = (struct pckbc_internal *)self;
905 struct pckbc_softc *sc = t->t_sc;
906
907 if (slot >= PCKBC_NSLOTS)
908 panic("pckbc_set_inputhandler: bad slot %d", slot);
909
910 (*sc->intr_establish)(sc, slot);
911
912 sc->inputhandler[slot] = func;
913 sc->inputarg[slot] = arg;
914 sc->subname[slot] = name;
915 }
916
917 int
918 pckbcintr(vsc)
919 void *vsc;
920 {
921 struct pckbc_softc *sc = (struct pckbc_softc *)vsc;
922 struct pckbc_internal *t = sc->id;
923 u_char stat;
924 pckbc_slot_t slot;
925 struct pckbc_slotdata *q;
926 int served = 0, data;
927
928 for(;;) {
929 stat = bus_space_read_1(t->t_iot, t->t_ioh_c, 0);
930 if (!(stat & KBS_DIB))
931 break;
932
933 served = 1;
934
935 slot = (t->t_haveaux && (stat & 0x20)) ?
936 PCKBC_AUX_SLOT : PCKBC_KBD_SLOT;
937 q = t->t_slotdata[slot];
938
939 if (!q) {
940 /* XXX do something for live insertion? */
941 printf("pckbcintr: no dev for slot %d\n", slot);
942 KBD_DELAY;
943 (void) bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
944 continue;
945 }
946
947 if (q->polling)
948 break; /* pckbc_poll_data() will get it */
949
950 KBD_DELAY;
951 data = bus_space_read_1(t->t_iot, t->t_ioh_d, 0);
952
953 #if NRND > 0
954 rnd_add_uint32(&q->rnd_source, (stat<<8)|data);
955 #endif
956 if (CMD_IN_QUEUE(q) && pckbc_cmdresponse(t, slot, data))
957 continue;
958
959 if (sc->inputhandler[slot])
960 (*sc->inputhandler[slot])(sc->inputarg[slot], data);
961 #ifdef PCKBCDEBUG
962 else
963 printf("pckbcintr: slot %d lost %d\n", slot, data);
964 #endif
965 }
966
967 return (served);
968 }
969
970 int
971 pckbc_cnattach(iot, addr, cmd_offset, slot)
972 bus_space_tag_t iot;
973 bus_addr_t addr;
974 bus_size_t cmd_offset;
975 pckbc_slot_t slot;
976 {
977 bus_space_handle_t ioh_d, ioh_c;
978 int res = 0;
979
980 if (bus_space_map(iot, addr + KBDATAP, 1, 0, &ioh_d))
981 return (ENXIO);
982 if (bus_space_map(iot, addr + cmd_offset, 1, 0, &ioh_c)) {
983 bus_space_unmap(iot, ioh_d, 1);
984 return (ENXIO);
985 }
986
987 pckbc_consdata.t_iot = iot;
988 pckbc_consdata.t_ioh_d = ioh_d;
989 pckbc_consdata.t_ioh_c = ioh_c;
990 pckbc_consdata.t_addr = addr;
991 callout_init(&pckbc_consdata.t_cleanup);
992
993 /* flush */
994 (void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0);
995
996 /* selftest? */
997
998 /* init cmd byte, enable ports */
999 pckbc_consdata.t_cmdbyte = KC8_CPU;
1000 if (!pckbc_put8042cmd(&pckbc_consdata)) {
1001 printf("kbc: cmd word write error\n");
1002 res = EIO;
1003 }
1004
1005 if (!res) {
1006 #if (NPCKBD > 0)
1007 res = pckbd_cnattach(&pckbc_consdata, slot);
1008 #else
1009 /*
1010 * XXX This should be replaced with the `notyet' case
1011 * XXX when all of the old PC-style console drivers
1012 * XXX have gone away. When that happens, all of
1013 * XXX the pckbc_machdep_cnattach() should be purged,
1014 * XXX as well.
1015 */
1016 #ifdef notyet
1017 res = ENXIO;
1018 #else
1019 res = pckbc_machdep_cnattach(&pckbc_consdata, slot);
1020 #endif
1021 #endif /* NPCKBD > 0 */
1022 }
1023
1024 if (res) {
1025 bus_space_unmap(iot, pckbc_consdata.t_ioh_d, 1);
1026 bus_space_unmap(iot, pckbc_consdata.t_ioh_c, 1);
1027 } else {
1028 pckbc_consdata.t_slotdata[slot] = &pckbc_cons_slotdata;
1029 pckbc_init_slotdata(&pckbc_cons_slotdata);
1030 pckbc_console = 1;
1031 }
1032
1033 return (res);
1034 }
1035