hil.c revision 1.3.14.2 1 /* $NetBSD: hil.c,v 1.3.14.2 2021/03/22 16:23:44 thorpej Exp $ */
2 /* $OpenBSD: hil.c,v 1.24 2010/11/20 16:45:46 miod Exp $ */
3 /*
4 * Copyright (c) 2003, 2004, Miodrag Vallat.
5 * 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 /*
31 * Copyright (c) 1988 University of Utah.
32 * Copyright (c) 1990, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * This code is derived from software contributed to Berkeley by
36 * the Systems Programming Group of the University of Utah Computer
37 * Science Department.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * from: Utah $Hdr: hil.c 1.38 92/01/21$
64 *
65 * @(#)hil.c 8.2 (Berkeley) 1/12/94
66 */
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/conf.h>
71 #include <sys/device.h>
72 #include <sys/file.h>
73 #include <sys/ioctl.h>
74 #include <sys/kernel.h>
75 #include <sys/proc.h>
76 #include <sys/kthread.h>
77 #include <sys/bus.h>
78 #include <sys/cpu.h>
79
80 #include <machine/autoconf.h>
81
82 #include <dev/hil/hilreg.h>
83 #include <dev/hil/hilvar.h>
84 #include <dev/hil/hildevs.h>
85 #include <dev/hil/hildevs_data.h>
86
87 #include "hilkbd.h"
88
89 static void hilconfig(struct hil_softc *, u_int);
90 static void hilempty(struct hil_softc *);
91 static int hilsubmatch(device_t, cfdata_t, const int *, void *);
92 static void hil_process_int(struct hil_softc *, uint8_t, uint8_t);
93 static int hil_process_poll(struct hil_softc *, uint8_t, uint8_t);
94 static void hil_thread(void *);
95 static int send_device_cmd(struct hil_softc *sc, u_int device, u_int cmd);
96 static void polloff(struct hil_softc *);
97 static void pollon(struct hil_softc *);
98
99 static int hilwait(struct hil_softc *);
100 static int hildatawait(struct hil_softc *);
101
102 #define hil_process_pending(sc) wakeup(&(sc)->sc_pending)
103
104 static __inline int
105 hilwait(struct hil_softc *sc)
106 {
107 int cnt;
108
109 for (cnt = 50000; cnt != 0; cnt--) {
110 DELAY(1);
111 if ((bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
112 HIL_BUSY) == 0)
113 break;
114 }
115
116 return cnt;
117 }
118
119 static __inline int
120 hildatawait(struct hil_softc *sc)
121 {
122 int cnt;
123
124 for (cnt = 50000; cnt != 0; cnt--) {
125 DELAY(1);
126 if ((bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
127 HIL_DATA_RDY) != 0)
128 break;
129 }
130
131 return cnt;
132 }
133
134 /*
135 * Common HIL bus attachment
136 */
137
138 void
139 hil_attach(struct hil_softc *sc, int *hil_is_console)
140 {
141
142 aprint_normal("\n");
143
144 /*
145 * Initialize loop information
146 */
147 sc->sc_cmdending = 0;
148 sc->sc_actdev = sc->sc_cmddev = 0;
149 sc->sc_cmddone = 0;
150 sc->sc_cmdbp = sc->sc_cmdbuf;
151 sc->sc_pollbp = sc->sc_pollbuf;
152 sc->sc_console = hil_is_console;
153 }
154
155 /*
156 * HIL subdevice attachment
157 */
158
159 int
160 hildevprint(void *aux, const char *pnp)
161 {
162 struct hil_attach_args *ha = aux;
163
164 if (pnp != NULL) {
165 aprint_normal("\"%s\" at %s id %x",
166 ha->ha_descr, pnp, ha->ha_id);
167 }
168 aprint_normal(" code %d", ha->ha_code);
169 if (pnp == NULL) {
170 aprint_normal(": %s", ha->ha_descr);
171 }
172
173 return UNCONF;
174 }
175
176 int
177 hilsubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
178 {
179 struct hil_attach_args *ha = aux;
180
181 if (cf->cf_loc[0] != -1 &&
182 cf->cf_loc[0] != ha->ha_code)
183 return 0;
184
185 return config_match(parent, cf, aux);
186 }
187
188 void
189 hil_attach_deferred(device_t self)
190 {
191 struct hil_softc *sc = device_private(self);
192 int tries;
193 uint8_t db;
194
195 sc->sc_status = HIL_STATUS_BUSY;
196
197 /*
198 * Initialize the loop: reconfigure, don't report errors,
199 * put keyboard in cooked mode, and enable autopolling.
200 */
201 db = LPC_RECONF | LPC_KBDCOOK | LPC_NOERROR | LPC_AUTOPOLL;
202 send_hil_cmd(sc, HIL_WRITELPCTRL, &db, 1, NULL);
203
204 /*
205 * Delay one second for reconfiguration and then read the
206 * data to clear the interrupt (if the loop reconfigured).
207 */
208 DELAY(1000000);
209 if (bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT) &
210 HIL_DATA_RDY) {
211 db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
212 DELAY(1);
213 }
214
215 /*
216 * The HIL loop may have reconfigured. If so we proceed on,
217 * if not we loop a few times until a successful reconfiguration
218 * is reported back to us. If the HIL loop is still lost after a
219 * few seconds, give up.
220 */
221 for (tries = 10; tries != 0; tries--) {
222 if (send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db) == 0) {
223 if (db & (LPS_CONFFAIL | LPS_CONFGOOD))
224 break;
225 }
226
227 #ifdef HILDEBUG
228 aprint_debug(self, "%s: loop not ready, retrying...\n");
229 #endif
230
231 DELAY(1000000);
232 }
233
234 if (tries == 0 || (db & LPS_CONFFAIL)) {
235 aprint_normal_dev(self, "no devices\n");
236 sc->sc_pending = 0;
237 if (tries == 0)
238 return;
239 }
240
241 /*
242 * Create asynchronous loop event handler thread.
243 */
244 if (kthread_create(PRI_NONE, 0, NULL, hil_thread, sc, &sc->sc_thread,
245 "%s", device_xname(sc->sc_dev)) != 0) {
246 aprint_error_dev(self, "unable to create event thread\n");
247 return;
248 }
249
250 /*
251 * Enable loop interrupts.
252 */
253 send_hil_cmd(sc, HIL_INTON, NULL, 0, NULL);
254
255 /*
256 * Reconfigure if necessary
257 */
258 sc->sc_status = HIL_STATUS_READY;
259 hil_process_pending(sc);
260 }
261
262 /*
263 * Asynchronous event processing
264 */
265
266 int
267 hil_intr(void *v)
268 {
269 struct hil_softc *sc = v;
270 uint8_t c, stat;
271
272 if (cold)
273 return 0;
274
275 stat = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
276
277 /*
278 * This should never happen if the interrupt comes from the
279 * loop.
280 */
281 if ((stat & HIL_DATA_RDY) == 0)
282 return 0; /* not for us */
283
284 c = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
285 HILP_DATA); /* clears interrupt */
286 DELAY(1);
287
288 hil_process_int(sc, stat, c);
289
290 if (sc->sc_status != HIL_STATUS_BUSY)
291 hil_process_pending(sc);
292
293 return 1;
294 }
295
296 void
297 hil_process_int(struct hil_softc *sc, uint8_t stat, uint8_t c)
298 {
299 device_t child;
300 struct hildev_softc *hdsc;
301
302 switch ((stat >> HIL_SSHIFT) & HIL_SMASK) {
303 case HIL_STATUS:
304 if (c & HIL_ERROR) {
305 sc->sc_cmddone = 1;
306 switch (c) {
307 case HIL_RECONFIG:
308 sc->sc_pending = HIL_PENDING_RECONFIG;
309 break;
310 case HIL_UNPLUGGED:
311 sc->sc_pending = HIL_PENDING_UNPLUGGED;
312 break;
313 }
314 break;
315 }
316 if (c & HIL_COMMAND) {
317 if (c & HIL_POLLDATA) { /* End of data */
318 child = sc->sc_devices[sc->sc_actdev];
319 if (child != NULL) {
320 hdsc = device_private(child);
321 if (hdsc->sc_fn != NULL)
322 (*hdsc->sc_fn)(hdsc,
323 sc->sc_pollbp
324 - sc->sc_pollbuf,
325 sc->sc_pollbuf);
326 }
327 } else { /* End of command */
328 sc->sc_cmdending = 1;
329 }
330 sc->sc_actdev = 0;
331 } else {
332 if (c & HIL_POLLDATA) { /* Start of polled data */
333 sc->sc_actdev = (c & HIL_DEVMASK);
334 sc->sc_pollbp = sc->sc_pollbuf;
335 } else { /* Start of command */
336 if (sc->sc_cmddev == (c & HIL_DEVMASK)) {
337 sc->sc_cmdbp = sc->sc_cmdbuf;
338 sc->sc_actdev = 0;
339 }
340 }
341 }
342 break;
343 case HIL_DATA:
344 if (sc->sc_actdev != 0) /* Collecting poll data */
345 *sc->sc_pollbp++ = c;
346 else {
347 if (sc->sc_cmddev != 0) { /* Collecting cmd data */
348 if (sc->sc_cmdending) {
349 sc->sc_cmddone = 1;
350 sc->sc_cmdending = 0;
351 } else
352 *sc->sc_cmdbp++ = c;
353 }
354 }
355 break;
356 }
357 }
358
359 /*
360 * Same as above, but in polled mode: return data as it gets seen, instead
361 * of buffering it.
362 */
363 int
364 hil_process_poll(struct hil_softc *sc, uint8_t stat, uint8_t c)
365 {
366 uint8_t db;
367
368 switch ((stat >> HIL_SSHIFT) & HIL_SMASK) {
369 case HIL_STATUS:
370 if (c & HIL_ERROR) {
371 sc->sc_cmddone = 1;
372 switch (c) {
373 case HIL_RECONFIG:
374 /*
375 * Remember that a configuration event
376 * occurred; it will be processed upon
377 * leaving polled mode...
378 */
379 sc->sc_pending = HIL_PENDING_RECONFIG;
380 /*
381 * However, the keyboard will come back as
382 * cooked, and we rely on it being in raw
383 * mode. So, put it back in raw mode right
384 * now.
385 */
386 db = 0;
387 send_hil_cmd(sc, HIL_WRITEKBDSADR, &db,
388 1, NULL);
389 break;
390 case HIL_UNPLUGGED:
391 /*
392 * Remember that an unplugged event
393 * occured; it will be processed upon
394 * leaving polled mode...
395 */
396 sc->sc_pending = HIL_PENDING_UNPLUGGED;
397 break;
398 }
399 break;
400 }
401 if (c & HIL_COMMAND) {
402 if (!(c & HIL_POLLDATA)) {
403 /* End of command */
404 sc->sc_cmdending = 1;
405 }
406 sc->sc_actdev = 0;
407 } else {
408 if (c & HIL_POLLDATA) {
409 /* Start of polled data */
410 sc->sc_actdev = (c & HIL_DEVMASK);
411 sc->sc_pollbp = sc->sc_pollbuf;
412 } else {
413 /* Start of command - should not happen */
414 if (sc->sc_cmddev == (c & HIL_DEVMASK)) {
415 sc->sc_cmdbp = sc->sc_cmdbuf;
416 sc->sc_actdev = 0;
417 }
418 }
419 }
420 break;
421 case HIL_DATA:
422 if (sc->sc_actdev != 0) /* Collecting poll data */
423 return 1;
424 else {
425 if (sc->sc_cmddev != 0) { /* Discarding cmd data */
426 if (sc->sc_cmdending) {
427 sc->sc_cmddone = 1;
428 sc->sc_cmdending = 0;
429 }
430 }
431 }
432 break;
433 }
434
435 return 0;
436 }
437
438 void
439 hil_thread(void *arg)
440 {
441 struct hil_softc *sc = arg;
442 int s;
443
444 for (;;) {
445 s = splhil();
446 if (sc->sc_pending == 0) {
447 splx(s);
448 (void)tsleep(&sc->sc_pending, PWAIT, "hil_event", 0);
449 continue;
450 }
451
452 switch (sc->sc_pending) {
453 case HIL_PENDING_RECONFIG:
454 sc->sc_pending = 0;
455 hilconfig(sc, sc->sc_maxdev);
456 break;
457 case HIL_PENDING_UNPLUGGED:
458 sc->sc_pending = 0;
459 hilempty(sc);
460 break;
461 }
462 splx(s);
463 }
464 }
465
466 /*
467 * Called after the loop has reconfigured. Here we need to:
468 * - determine how many devices are on the loop
469 * (some may have been added or removed)
470 * - make sure all keyboards are in raw mode
471 *
472 * Note that our device state is now potentially invalid as
473 * devices may no longer be where they were. What we should
474 * do here is either track where the devices went and move
475 * state around accordingly...
476 *
477 * Note that it is necessary that we operate the loop with the keyboards
478 * in raw mode: they won't cause the loop to generate an NMI if the
479 * ``reset'' key combination is pressed, and we do not handle the hil
480 * NMI interrupt...
481 */
482 void
483 hilconfig(struct hil_softc *sc, u_int knowndevs)
484 {
485 struct hil_attach_args ha;
486 uint8_t db;
487 int id, s;
488
489 s = splhil();
490
491 /*
492 * Determine how many devices are on the loop.
493 */
494 db = 0;
495 send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db);
496 sc->sc_maxdev = db & LPS_DEVMASK;
497 #ifdef HILDEBUG
498 printf("%s: %d device(s)\n", device_xname(sc->sc_dev), sc->sc_maxdev);
499 #endif
500
501 /*
502 * Put all keyboards in raw mode now.
503 */
504 db = 0;
505 send_hil_cmd(sc, HIL_WRITEKBDSADR, &db, 1, NULL);
506
507 /*
508 * If the loop grew, attach new devices.
509 */
510 for (id = knowndevs + 1; id <= sc->sc_maxdev; id++) {
511 int len;
512 const struct hildevice *hd;
513
514 if (send_device_cmd(sc, id, HIL_IDENTIFY) != 0) {
515 aprint_normal_dev(sc->sc_dev,
516 "no answer from device %d\n", id);
517 continue;
518 }
519
520 len = sc->sc_cmdbp - sc->sc_cmdbuf;
521 if (len == 0) {
522 #ifdef HILDEBUG
523 printf("%s: no device at code %d\n",
524 device_xname(sc->sc_dev), id);
525 #endif
526 continue;
527 }
528
529 /* Identify and attach device */
530 for (hd = hildevs; hd->minid >= 0; hd++)
531 if (sc->sc_cmdbuf[0] >= hd->minid &&
532 sc->sc_cmdbuf[0] <= hd->maxid) {
533
534 ha.ha_console = *sc->sc_console;
535 ha.ha_code = id;
536 ha.ha_type = hd->type;
537 ha.ha_descr = hd->descr;
538 ha.ha_infolen = len;
539 memcpy(ha.ha_info, sc->sc_cmdbuf, len);
540
541 sc->sc_devices[id] =
542 config_found(sc->sc_dev, &ha, hildevprint,
543 CFARG_SUBMATCH, hilsubmatch,
544 CFARG_EOL);
545
546 #if NHILKBD > 0
547 /*
548 * If we just attached a keyboard as console,
549 * console choice is not indeterminate anymore.
550 */
551 if (sc->sc_devices[id] != NULL &&
552 ha.ha_type == HIL_DEVICE_KEYBOARD &&
553 ha.ha_console != 0)
554 *sc->sc_console = 1;
555 #endif
556 }
557 }
558
559 /*
560 * Detach remaining devices, if the loop has shrunk.
561 */
562 for (id = sc->sc_maxdev + 1; id < NHILD; id++) {
563 if (sc->sc_devices[id] != NULL)
564 config_detach(sc->sc_devices[id],
565 DETACH_FORCE);
566 sc->sc_devices[id] = NULL;
567 }
568
569 sc->sc_cmdbp = sc->sc_cmdbuf;
570
571 splx(s);
572 }
573
574 /*
575 * Called after the loop has been unplugged. We simply force detach of
576 * all our children.
577 */
578 void
579 hilempty(struct hil_softc *sc)
580 {
581 uint8_t db;
582 int id, s;
583 u_int oldmaxdev;
584
585 s = splhil();
586
587 /*
588 * Wait for the loop to be stable.
589 */
590 for (;;) {
591 if (send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db) == 0) {
592 if (db & (LPS_CONFFAIL | LPS_CONFGOOD))
593 break;
594 } else {
595 db = LPS_CONFFAIL;
596 break;
597 }
598 }
599
600 if (db & LPS_CONFFAIL) {
601 sc->sc_maxdev = 0;
602 } else {
603 db = 0;
604 send_hil_cmd(sc, HIL_READLPSTAT, NULL, 0, &db);
605 oldmaxdev = sc->sc_maxdev;
606 sc->sc_maxdev = db & LPS_DEVMASK;
607
608 if (sc->sc_maxdev != 0) {
609 /*
610 * The loop was not unplugged after all, but its
611 * configuration has changed.
612 */
613 hilconfig(sc, oldmaxdev);
614 return;
615 }
616 }
617
618 /*
619 * Now detach all hil devices.
620 */
621 for (id = sc->sc_maxdev + 1; id < NHILD; id++) {
622 if (sc->sc_devices[id] != NULL)
623 config_detach(sc->sc_devices[id],
624 DETACH_FORCE);
625 sc->sc_devices[id] = NULL;
626 }
627
628 sc->sc_cmdbp = sc->sc_cmdbuf;
629
630 splx(s);
631 }
632
633 /*
634 * Low level routines which actually talk to the 8042 chip.
635 */
636
637 /*
638 * Send a command to the 8042 with zero or more bytes of data.
639 * If rdata is non-null, wait for and return a byte of data.
640 */
641 int
642 send_hil_cmd(struct hil_softc *sc, u_int cmd, uint8_t *data, u_int dlen,
643 uint8_t *rdata)
644 {
645 uint8_t status;
646 int s;
647
648 s = splhil();
649
650 if (hilwait(sc) == 0) {
651 #ifdef HILDEBUG
652 printf("%s: no answer from the loop\n",
653 device_xname(sc->sc_dev));
654 #endif
655 splx(s);
656 return EBUSY;
657 }
658
659 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, cmd);
660 while (dlen--) {
661 hilwait(sc);
662 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, *data++);
663 DELAY(1);
664 }
665 if (rdata) {
666 do {
667 if (hildatawait(sc) == 0) {
668 #ifdef HILDEBUG
669 printf("%s: no answer from the loop\n",
670 device_xname(sc->sc_dev));
671 #endif
672 break;
673 }
674 status = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
675 HILP_STAT);
676 *rdata = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
677 HILP_DATA);
678 DELAY(1);
679 } while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K);
680 }
681 splx(s);
682 return 0;
683 }
684
685 /*
686 * Send a command to a device on the loop.
687 * Since only one command can be active on the loop at any time,
688 * we must ensure that we are not interrupted during this process.
689 * Hence we mask interrupts to prevent potential access from most
690 * interrupt routines and turn off auto-polling to disable the
691 * internally generated poll commands.
692 * Needs to be called at splhil().
693 */
694 int
695 send_device_cmd(struct hil_softc *sc, u_int device, u_int cmd)
696 {
697 uint8_t status, c;
698 int rc = 0;
699
700 polloff(sc);
701
702 sc->sc_cmdbp = sc->sc_cmdbuf;
703 sc->sc_cmddev = device;
704
705 if (hilwait(sc) == 0) {
706 #ifdef HILDEBUG
707 printf("%s: no answer from device %d\n",
708 device_xname(sc->sc_dev), device);
709 #endif
710 rc = EBUSY;
711 goto out;
712 }
713
714 /*
715 * Transfer the command and device info to the chip
716 */
717 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_STARTCMD);
718 hilwait(sc);
719 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 8 + device);
720 hilwait(sc);
721 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, cmd);
722 hilwait(sc);
723 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, HIL_TIMEOUT);
724
725 /*
726 * Trigger the command and wait for completion
727 */
728 hilwait(sc);
729 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_TRIGGER);
730 sc->sc_cmddone = 0;
731 do {
732 if (hildatawait(sc) == 0) {
733 #ifdef HILDEBUG
734 printf("%s: no answer from device %d\n",
735 device_xname(sc->sc_dev), device);
736 #endif
737 rc = EBUSY;
738 break;
739 }
740 status = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
741 c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
742 DELAY(1);
743 hil_process_int(sc, status, c);
744 } while (sc->sc_cmddone == 0);
745 out:
746 sc->sc_cmddev = 0;
747
748 pollon(sc);
749 return rc;
750 }
751
752 int
753 send_hildev_cmd(struct hildev_softc *hdsc, u_int cmd,
754 uint8_t *outbuf, u_int *outlen)
755 {
756 struct hil_softc *sc = device_private(device_parent(hdsc->sc_dev));
757 int s, rc;
758
759 s = splhil();
760
761 if ((rc = send_device_cmd(sc, hdsc->sc_code, cmd)) == 0) {
762 /*
763 * Return the command response in the buffer if necessary
764 */
765 if (outbuf != NULL && outlen != NULL) {
766 *outlen = uimin(*outlen, sc->sc_cmdbp - sc->sc_cmdbuf);
767 memcpy(outbuf, sc->sc_cmdbuf, *outlen);
768 }
769 }
770
771 splx(s);
772 return rc;
773 }
774
775 /*
776 * Turn auto-polling off and on.
777 */
778 void
779 polloff(struct hil_softc *sc)
780 {
781 uint8_t db;
782
783 if (hilwait(sc) == 0)
784 return;
785
786 /*
787 * Turn off auto repeat
788 */
789 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_SETARR);
790 hilwait(sc);
791 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 0);
792
793 /*
794 * Turn off auto-polling
795 */
796 hilwait(sc);
797 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READLPCTRL);
798 hildatawait(sc);
799 db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
800 db &= ~LPC_AUTOPOLL;
801 hilwait(sc);
802 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_WRITELPCTRL);
803 hilwait(sc);
804 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, db);
805
806 /*
807 * Must wait until polling is really stopped
808 */
809 do {
810 hilwait(sc);
811 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READBUSY);
812 hildatawait(sc);
813 db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
814 } while (db & BSY_LOOPBUSY);
815
816 sc->sc_cmddone = 0;
817 sc->sc_cmddev = 0;
818 }
819
820 void
821 pollon(struct hil_softc *sc)
822 {
823 uint8_t db;
824
825 if (hilwait(sc) == 0)
826 return;
827
828 /*
829 * Turn on auto polling
830 */
831 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_READLPCTRL);
832 hildatawait(sc);
833 db = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
834 db |= LPC_AUTOPOLL;
835 hilwait(sc);
836 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_WRITELPCTRL);
837 hilwait(sc);
838 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, db);
839
840 /*
841 * Turn off auto repeat - we emulate this through wscons
842 */
843 hilwait(sc);
844 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_CMD, HIL_SETARR);
845 hilwait(sc);
846 bus_space_write_1(sc->sc_bst, sc->sc_bsh, HILP_DATA, 0);
847 DELAY(1);
848 }
849
850 void
851 hil_set_poll(struct hil_softc *sc, int on)
852 {
853 if (on) {
854 pollon(sc);
855 } else {
856 hil_process_pending(sc);
857 send_hil_cmd(sc, HIL_INTON, NULL, 0, NULL);
858 }
859 }
860
861 int
862 hil_poll_data(struct hildev_softc *hdsc, uint8_t *stat, uint8_t *data)
863 {
864 struct hil_softc *sc = device_private(device_parent(hdsc->sc_dev));
865 uint8_t s, c;
866
867 s = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_STAT);
868 if ((s & HIL_DATA_RDY) == 0)
869 return -1;
870
871 c = bus_space_read_1(sc->sc_bst, sc->sc_bsh, HILP_DATA);
872 DELAY(1);
873
874 if (hil_process_poll(sc, s, c)) {
875 /* Discard any data not for us */
876 if (sc->sc_actdev == hdsc->sc_code) {
877 *stat = s;
878 *data = c;
879 return 0;
880 }
881 }
882
883 return -1;
884 }
885