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