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