kbd.c revision 1.1 1 /* $NetBSD: kbd.c,v 1.1 1996/01/24 01:15:35 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
45 */
46
47 /*
48 * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
49 * [yet?]). Translates incoming bytes to ASCII or to `firm_events' and
50 * passes them up to the appropriate reader.
51 */
52
53 /*
54 * Zilog Z8530 Dual UART driver (keyboard interface)
55 *
56 * This is the "slave" driver that will be attached to
57 * the "zsc" driver for a Sun keyboard.
58 */
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/proc.h>
63 #include <sys/device.h>
64 #include <sys/conf.h>
65 #include <sys/file.h>
66 #include <sys/ioctl.h>
67 /* #include <sys/tty.h> */
68 #include <sys/time.h>
69 #include <sys/kernel.h>
70 #include <sys/syslog.h>
71
72 #include <dev/ic/z8530reg.h>
73 #include <machine/z8530var.h>
74 #include <machine/vuid_event.h>
75 #include <machine/kbd.h>
76 #include <machine/kbio.h>
77
78 #include "event_var.h"
79 #include "kbd_xlate.h"
80
81 /*
82 * Ideas:
83 * /dev/kbd is not a tty (plain device)
84 */
85
86 /*
87 * How many input characters we can buffer.
88 * The port-specific var.h may override this.
89 * Note: must be a power of two!
90 */
91 #define KBD_RX_RING_SIZE 256
92 #define KBD_RX_RING_MASK (KBD_RX_RING_SIZE-1)
93 /*
94 * Output buffer. Only need a few chars.
95 */
96 #define KBD_TX_RING_SIZE 16
97 #define KBD_TX_RING_MASK (KBD_TX_RING_SIZE-1)
98 /*
99 * Keyboard serial line speed is fixed at 1200 bps.
100 */
101 #define KBD_BPS 1200
102 #define KBD_RESET_TIMO 1000 /* mS. */
103
104 /*
105 * XXX - Historical comment - no longer quite right...
106 * Keyboard driver state. The ascii and kbd links go up and down and
107 * we just sit in the middle doing translation. Note that it is possible
108 * to get just one of the two links, in which case /dev/kbd is unavailable.
109 * The downlink supplies us with `internal' open and close routines which
110 * will enable dataflow across the downlink. We promise to call open when
111 * we are willing to take keystrokes, and to call close when we are not.
112 * If /dev/kbd is not the console tty input source, we do this whenever
113 * /dev/kbd is in use; otherwise we just leave it open forever.
114 */
115 struct kbd_softc {
116 struct device k_dev; /* required first: base device */
117 struct zs_chanstate *k_cs;
118
119 /* Flags to communicate with kbd_softint() */
120 volatile int k_intr_flags;
121 #define INTR_RX_OVERRUN 1
122 #define INTR_TX_EMPTY 2
123 #define INTR_ST_CHECK 4
124
125 /* Transmit state */
126 volatile int k_txflags;
127 #define K_TXBUSY 1
128 #define K_TXWANT 2
129
130 /*
131 * State of upper interface.
132 */
133 int k_isopen; /* set if open has been done */
134 int k_evmode; /* set if we should produce events */
135 struct evvar k_events; /* event queue state */
136
137 /*
138 * ACSI translation state
139 */
140 int k_repeat_start; /* initial delay */
141 int k_repeat_step; /* inter-char delay */
142 int k_repeatsym; /* repeating symbol */
143 int k_repeating; /* we've called timeout() */
144 struct kbd_state k_state; /* ASCII translation state */
145
146 /*
147 * Magic sequence stuff (L1-A)
148 */
149 char k_isconsole;
150 char k_magic1_down;
151 u_char k_magic1; /* L1 */
152 u_char k_magic2; /* A */
153
154 /*
155 * The transmit ring buffer.
156 */
157 volatile u_int k_tbget; /* transmit buffer `get' index */
158 volatile u_int k_tbput; /* transmit buffer `put' index */
159 u_char k_tbuf[KBD_TX_RING_SIZE]; /* data */
160
161 /*
162 * The receive ring buffer.
163 */
164 u_int k_rbget; /* ring buffer `get' index */
165 volatile u_int k_rbput; /* ring buffer `put' index */
166 u_short k_rbuf[KBD_RX_RING_SIZE]; /* rr1, data pairs */
167
168 };
169
170 /* Prototypes */
171 void kbd_ascii(struct tty *);
172 void kbd_serial(struct tty *, void (*)(), void (*)());
173 int kbd_iopen(int unit);
174 void kbd_was_reset(struct kbd_softc *);
175 void kbd_new_layout(struct kbd_softc *);
176 void kbd_rint(int);
177 int kbd_docmd(struct kbd_softc *k, int cmd);
178 void kbd_output(struct kbd_softc *k, int c);
179 void kbd_start_tx(struct kbd_softc *k);
180 void kbd_repeat(void *arg);
181
182 extern void kd_input(int ascii);
183
184 cdev_decl(kbd); /* open, close, read, write, ioctl, stop, ... */
185
186 struct zsops zsops_kbd;
187
188 /****************************************************************
189 * Definition of the driver for autoconfig.
190 ****************************************************************/
191
192 static int kbd_match(struct device *, void *, void *);
193 static void kbd_attach(struct device *, struct device *, void *);
194
195 struct cfdriver kbdcd = {
196 NULL, "kbd", kbd_match, kbd_attach,
197 DV_DULL, sizeof(struct kbd_softc), NULL,
198 };
199
200
201 /*
202 * kbd_match: how is this zs channel configured?
203 */
204 int
205 kbd_match(parent, match, aux)
206 struct device *parent;
207 void *match, *aux;
208 {
209 struct cfdata *cf = match;
210 struct zsc_attach_args *args = aux;
211
212 /* Exact match required for keyboard. */
213 if (cf->cf_loc[0] == args->channel)
214 return 2;
215
216 return 0;
217 }
218
219 void
220 kbd_attach(parent, self, aux)
221 struct device *parent, *self;
222 void *aux;
223
224 {
225 struct zsc_softc *zsc = (void *) parent;
226 struct kbd_softc *k = (void *) self;
227 struct zsc_attach_args *args = aux;
228 struct zs_chanstate *cs;
229 struct cfdata *cf;
230 int channel, kbd_unit;
231 int reset, s, tconst;
232
233 cf = k->k_dev.dv_cfdata;
234 kbd_unit = cf->cf_unit;
235 channel = args->channel;
236 cs = &zsc->zsc_cs[channel];
237 cs->cs_private = k;
238 cs->cs_ops = &zsops_kbd;
239 k->k_cs = cs;
240
241 if (args->hwflags & ZS_HWFLAG_CONSOLE) {
242 k->k_isconsole = 1;
243 printf(" (console)");
244 }
245 printf("\n");
246
247 /* Initialize the speed, etc. */
248 tconst = BPS_TO_TCONST(cs->cs_pclk_div16, KBD_BPS);
249 s = splzs();
250 if (k->k_isconsole == 0) {
251 /* Not the console; may need reset. */
252 reset = (channel == 0) ?
253 ZSWR9_A_RESET : ZSWR9_B_RESET;
254 ZS_WRITE(cs, 9, reset);
255 }
256 /* These are OK as set by zscc: WR3, WR4, WR5 */
257 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
258 cs->cs_preg[12] = tconst;
259 cs->cs_preg[13] = tconst >> 8;
260 zs_loadchannelregs(cs);
261 splx(s);
262
263 /* Do this before any calls to kbd_rint(). */
264 kbd_xlate_init(&k->k_state);
265
266 /* XXX - Do this in open? */
267 k->k_repeat_start = hz/2;
268 k->k_repeat_step = hz/20;
269
270 /* Magic sequence. */
271 k->k_magic1 = KBD_L1;
272 k->k_magic2 = KBD_A;
273
274 /* Now attach the (kd) pseudo-driver. */
275 kd_init(kbd_unit);
276 }
277
278
279 /****************************************************************
280 * Entry points for /dev/kbd
281 * (open,close,read,write,...)
282 ****************************************************************/
283
284 /*
285 * Open:
286 * Check exclusion, open actual device (_iopen),
287 * setup event channel, clear ASCII repeat stuff.
288 */
289 int
290 kbdopen(dev, flags, mode, p)
291 dev_t dev;
292 int flags, mode;
293 struct proc *p;
294 {
295 struct kbd_softc *k;
296 int error, s, unit;
297
298 unit = minor(dev);
299 if (unit >= kbdcd.cd_ndevs)
300 return (ENXIO);
301 k = kbdcd.cd_devs[unit];
302 if (k == NULL)
303 return (ENXIO);
304
305 /* Exclusive open required for /dev/kbd */
306 if (k->k_events.ev_io)
307 return (EBUSY);
308 k->k_events.ev_io = p;
309
310 if ((error = kbd_iopen(unit)) != 0) {
311 k->k_events.ev_io = NULL;
312 return (error);
313 }
314 ev_init(&k->k_events);
315 k->k_evmode = 1; /* XXX: OK? */
316
317 if (k->k_repeating) {
318 k->k_repeating = 0;
319 untimeout(kbd_repeat, k);
320 }
321
322 return (0);
323 }
324
325 /*
326 * Close:
327 * Turn off event mode, dump the queue, and close the keyboard
328 * unless it is supplying console input.
329 */
330 int
331 kbdclose(dev, flags, mode, p)
332 dev_t dev;
333 int flags, mode;
334 struct proc *p;
335 {
336 struct kbd_softc *k;
337
338 k = kbdcd.cd_devs[minor(dev)];
339 k->k_evmode = 0;
340 ev_fini(&k->k_events);
341 k->k_events.ev_io = NULL;
342 return (0);
343 }
344
345 int
346 kbdread(dev, uio, flags)
347 dev_t dev;
348 struct uio *uio;
349 int flags;
350 {
351 struct kbd_softc *k;
352
353 k = kbdcd.cd_devs[minor(dev)];
354 return (ev_read(&k->k_events, uio, flags));
355 }
356
357 /* this routine should not exist, but is convenient to write here for now */
358 int
359 kbdwrite(dev, uio, flags)
360 dev_t dev;
361 struct uio *uio;
362 int flags;
363 {
364
365 return (EOPNOTSUPP);
366 }
367
368 int
369 kbdselect(dev, rw, p)
370 dev_t dev;
371 int rw;
372 struct proc *p;
373 {
374 struct kbd_softc *k;
375
376 k = kbdcd.cd_devs[minor(dev)];
377 return (ev_select(&k->k_events, rw, p));
378 }
379
380 static int kbd_oldkeymap __P((struct kbd_state *ks,
381 u_long cmd, struct okiockey *okio));
382
383 static int kbd_iockeymap __P((struct kbd_state *ks,
384 u_long cmd, struct kiockeymap *kio));
385
386 int
387 kbdioctl(dev, cmd, data, flag, p)
388 dev_t dev;
389 u_long cmd;
390 register caddr_t data;
391 int flag;
392 struct proc *p;
393 {
394 struct kbd_softc *k;
395 struct kbd_state *ks;
396 int *ip;
397 int error = 0;
398
399 k = kbdcd.cd_devs[minor(dev)];
400 ks = &k->k_state;
401
402 switch (cmd) {
403
404 case KIOCTRANS: /* Set translation mode */
405 ip = (int *)data;
406 /* We only support "raw" mode on /dev/kbd */
407 if (*ip != TR_UNTRANS_EVENT)
408 error = EINVAL;
409 break;
410
411 case KIOCGTRANS: /* Get translation mode */
412 ip = (int *)data;
413 /* We only support "raw" mode on /dev/kbd */
414 *ip = TR_UNTRANS_EVENT;
415 break;
416
417 #ifdef KIOCGETKEY
418 case KIOCGETKEY: /* Get keymap entry (old format) */
419 error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
420 break;
421 #endif KIOCGETKEY */
422
423 case KIOCSKEY: /* Set keymap entry */
424 /* Don't let just anyone hose the keyboard. */
425 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
426 return (error);
427 /* fallthrough */
428 case KIOCGKEY: /* Get keymap entry */
429 error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
430 break;
431
432 case KIOCCMD: /* Send a command to the keyboard */
433 /*
434 * ``unimplemented commands are ignored'' (blech)
435 * so cannot check return value from kbd_docmd
436 */
437 error = kbd_drain_tx(k);
438 if (error == 0) {
439 (void) kbd_docmd(k, *(int *)data);
440 }
441 break;
442
443 case KIOCTYPE: /* Get keyboard type */
444 ip = (int *)data;
445 *ip = ks->kbd_id;
446 break;
447
448 case KIOCSDIRECT: /* where to send input */
449 ip = (int *)data;
450 k->k_evmode = *ip;
451 break;
452
453 case KIOCLAYOUT: /* Get keyboard layout */
454 *data = ks->kbd_layout;
455 break;
456
457 case KIOCSLED:
458 error = kbd_set_leds(k, data);
459 break;
460
461 case KIOCGLED:
462 *(char *)data = ks->kbd_leds;
463 break;
464
465 case FIONBIO: /* we will remove this someday (soon???) */
466 break;
467
468 case FIOASYNC:
469 k->k_events.ev_async = *(int *)data != 0;
470 break;
471
472 case TIOCSPGRP:
473 ip = (int *)data;
474 if (*ip != k->k_events.ev_io->p_pgid)
475 error = EPERM;
476 break;
477
478 }
479
480 return (error);
481 }
482
483 /****************************************************************
484 * ioctl helpers
485 ****************************************************************/
486
487 /*
488 * Get/Set keymap entry
489 */
490 int
491 kbd_iockeymap(ks, cmd, kio)
492 struct kbd_state *ks;
493 u_long cmd;
494 struct kiockeymap *kio;
495 {
496 struct keymap *km;
497 u_int station;
498
499 switch (kio->kio_tablemask) {
500 case KIOC_NOMASK:
501 km = ks->kbd_k.k_normal;
502 break;
503 case KIOC_SHIFTMASK:
504 km = ks->kbd_k.k_shifted;
505 break;
506 case KIOC_CTRLMASK:
507 km = ks->kbd_k.k_control;
508 break;
509 case KIOC_UPMASK:
510 km = ks->kbd_k.k_release;
511 break;
512 default:
513 /* Silently ignore unsupported masks */
514 return (0);
515 }
516
517 /* Range-check the table position. */
518 station = kio->kio_station;
519 if (station >= KEYMAP_SIZE)
520 return (EINVAL);
521
522 switch (cmd) {
523
524 case KIOCGKEY: /* Get keymap entry */
525 kio->kio_entry = km->keymap[station];
526 break;
527
528 case KIOCSKEY: /* Set keymap entry */
529 km->keymap[station] = kio->kio_entry;
530 break;
531
532 default:
533 return(ENOTTY);
534 }
535 return (0);
536 }
537
538 #ifdef KIOCGETKEY
539 /*
540 * Get/Set keymap entry,
541 * old format (compatibility)
542 */
543 int
544 kbd_oldkeymap(ks, cmd, kio)
545 struct kbd_state *ks;
546 u_long cmd;
547 struct okiockey *kio;
548 {
549 int error = 0;
550
551 switch (cmd) {
552
553 case KIOCGETKEY:
554 if (kio->kio_station == 118) {
555 /*
556 * This is X11 asking if a type 3 keyboard is
557 * really a type 3 keyboard. Say yes, it is,
558 * by reporting key station 118 as a "hole".
559 * Note old (SunOS 3.5) definition of HOLE!
560 */
561 kio->kio_entry = 0xA2;
562 break;
563 }
564 /* fall through */
565
566 default:
567 error = ENOTTY;
568 break;
569 }
570
571 return (error);
572 }
573 #endif /* KIOCGETKEY */
574
575 /****************************************************************
576 * middle layers:
577 * - keysym to ASCII sequence
578 * - raw key codes to keysym
579 ****************************************************************/
580
581
582 /*
583 * Initialization done by either kdcninit or kbd_iopen
584 */
585 void
586 kbd_xlate_init(ks)
587 struct kbd_state *ks;
588 {
589 struct keyboard *ktbls;
590 int id;
591
592 id = ks->kbd_id;
593 if (id < KBD_MIN_TYPE)
594 id = KBD_MIN_TYPE;
595 if (id > kbd_max_type)
596 id = kbd_max_type;
597 ktbls = keyboards[id];
598
599 ks->kbd_k = *ktbls; /* struct assignment */
600 ks->kbd_modbits = 0;
601 }
602
603 /*
604 * Turn keyboard up/down codes into a KEYSYM.
605 * Note that the "kd" driver uses this too!
606 */
607 int
608 kbd_code_to_keysym(ks, c)
609 register struct kbd_state *ks;
610 register int c;
611 {
612 struct keymap *km;
613 int keysym;
614
615 /*
616 * Get keymap pointer. One of these:
617 * release, control, shifted, normal, ...
618 */
619 if (KEY_UP(c))
620 km = ks->kbd_k.k_release;
621 else {
622 if (ks->kbd_modbits & KBMOD_CTRL_MASK)
623 km = ks->kbd_k.k_control;
624 else {
625 if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
626 km = ks->kbd_k.k_shifted;
627 else
628 km = ks->kbd_k.k_normal;
629 }
630 }
631 if (km == NULL) {
632 /*
633 * Do not know how to translate yet.
634 * We will find out when a RESET comes along.
635 */
636 keysym = KEYSYM_NOP;
637 } else
638 keysym = km->keymap[KEY_CODE(c)];
639
640 return (keysym);
641 }
642
643 void
644 kbd_input_string(k, str)
645 struct kbd_softc *k;
646 char *str;
647 {
648 while (*str) {
649 kd_input(*str);
650 str++;
651 }
652 }
653
654 void
655 kbd_input_funckey(k, keysym)
656 struct kbd_softc *k;
657 register int keysym;
658 {
659 register int n;
660 char str[12];
661
662 /*
663 * Format the F-key sequence and send as a string.
664 * XXX: Ugly compatibility mappings.
665 */
666 n = 0xC0 + (keysym & 0x3F);
667 sprintf(str, "\033[%dz", n);
668 kbd_input_string(k, str);
669 }
670
671 /*
672 * This is called by kbd_input_raw() or by kb_repeat()
673 * to deliver ASCII input. Called at splsoftclock()
674 * XXX: Raise to spltty before calling kd_input() ?
675 */
676 void
677 kbd_input_keysym(k, keysym)
678 struct kbd_softc *k;
679 register int keysym;
680 {
681 struct kbd_state *ks = &k->k_state;
682 register int class, data;
683
684 class = KEYSYM_CLASS(keysym);
685 switch (class) {
686
687 case KEYSYM_ASCII:
688 data = KEYSYM_DATA(keysym);
689 if (ks->kbd_modbits & KBMOD_META_MASK)
690 data |= 0x80;
691 kd_input(data);
692 break;
693
694 case KEYSYM_STRING:
695 data = keysym & 0xF;
696 kbd_input_string(k, kbd_stringtab[data]);
697 break;
698
699 case KEYSYM_FUNC:
700 kbd_input_funckey(k, keysym);
701 break;
702
703 case KEYSYM_CLRMOD:
704 data = 1 << (keysym & 0x1F);
705 ks->kbd_modbits &= ~data;
706 break;
707
708 case KEYSYM_SETMOD:
709 data = 1 << (keysym & 0x1F);
710 ks->kbd_modbits |= data;
711 break;
712
713 case KEYSYM_INVMOD:
714 data = 1 << (keysym & 0x1F);
715 ks->kbd_modbits ^= data;
716 break;
717
718 case KEYSYM_ALL_UP:
719 ks->kbd_modbits &= ~0xFFFF;
720 break;
721
722 case KEYSYM_SPECIAL:
723 if (keysym == KEYSYM_NOP)
724 break;
725 /* fall through */
726 default:
727 log(LOG_WARNING, "%s: unexpected keysym 0x%x\n",
728 k->k_dev.dv_xname, keysym);
729 break;
730 }
731 }
732
733 /*
734 * This is the autorepeat timeout function.
735 * (called at splsoftclock)
736 */
737 void
738 kbd_repeat(void *arg)
739 {
740 struct kbd_softc *k = (struct kbd_softc *)arg;
741
742 if (k->k_repeating && k->k_repeatsym >= 0) {
743 kbd_input_keysym(k, k->k_repeatsym);
744 timeout(kbd_repeat, k, k->k_repeat_step);
745 }
746 }
747
748 /*
749 * Called by our kbd_softint() routine on input,
750 * which passes the raw hardware scan codes.
751 * Note: this is called at splsoftclock()
752 */
753 void
754 kbd_input_raw(k, c)
755 struct kbd_softc *k;
756 register int c;
757 {
758 struct kbd_state *ks = &k->k_state;
759 struct firm_event *fe;
760 int put, keysym;
761
762 /* XXX - Input errors already handled. */
763
764 /* Are we expecting special input? */
765 if (ks->kbd_expect) {
766 if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
767 /* We read a KBD_RESET last time. */
768 ks->kbd_id = c;
769 kbd_was_reset(k);
770 }
771 if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
772 /* We read a KBD_LAYOUT last time. */
773 ks->kbd_layout = c;
774 kbd_new_layout(k);
775 }
776 ks->kbd_expect = 0;
777 return;
778 }
779
780 /* Is this one of the "special" input codes? */
781 if (KBD_SPECIAL(c)) {
782 switch (c) {
783 case KBD_RESET:
784 ks->kbd_expect |= KBD_EXPECT_IDCODE;
785 /* Fake an "all-up" to resync. translation. */
786 c = KBD_IDLE;
787 break;
788
789 case KBD_LAYOUT:
790 ks->kbd_expect |= KBD_EXPECT_LAYOUT;
791 return;
792
793 case KBD_ERROR:
794 log(LOG_WARNING, "%s: received error indicator\n",
795 k->k_dev.dv_xname);
796 return;
797
798 case KBD_IDLE:
799 /* Let this go to the translator. */
800 break;
801 }
802 }
803
804 /*
805 * If /dev/kbd is not connected in event mode,
806 * translate and send upstream (to console).
807 */
808 if (!k->k_evmode) {
809
810 /* Any input stops auto-repeat (i.e. key release). */
811 if (k->k_repeating) {
812 k->k_repeating = 0;
813 untimeout(kbd_repeat, k);
814 }
815
816 /* Translate this code to a keysym */
817 keysym = kbd_code_to_keysym(ks, c);
818
819 /* Pass up to the next layer. */
820 kbd_input_keysym(k, keysym);
821
822 /* Does this symbol get auto-repeat? */
823 if (KEYSYM_NOREPEAT(keysym))
824 return;
825
826 /* Setup for auto-repeat after initial delay. */
827 k->k_repeating = 1;
828 k->k_repeatsym = keysym;
829 timeout(kbd_repeat, k, k->k_repeat_start);
830 return;
831 }
832
833 /*
834 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
835 * This is bad as it means the server will not automatically resync
836 * on all-up IDLEs, but I did not drop them before, and the server
837 * goes crazy when it comes time to blank the screen....
838 */
839 if (c == KBD_IDLE)
840 return;
841
842 /*
843 * Keyboard is generating events. Turn this keystroke into an
844 * event and put it in the queue. If the queue is full, the
845 * keystroke is lost (sorry!).
846 */
847 put = k->k_events.ev_put;
848 fe = &k->k_events.ev_q[put];
849 put = (put + 1) % EV_QSIZE;
850 if (put == k->k_events.ev_get) {
851 log(LOG_WARNING, "%s: event queue overflow\n",
852 k->k_dev.dv_xname); /* ??? */
853 return;
854 }
855 fe->id = KEY_CODE(c);
856 fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
857 fe->time = time;
858 k->k_events.ev_put = put;
859 EV_WAKEUP(&k->k_events);
860 }
861
862 /****************************************************************
863 * Interface to the lower layer (zscc)
864 ****************************************************************/
865
866 static int
867 kbd_rxint(cs)
868 register struct zs_chanstate *cs;
869 {
870 register struct kbd_softc *k;
871 register int put, put_next;
872 register u_char c, rr1;
873
874 k = cs->cs_private;
875 put = k->k_rbput;
876
877 /* Read the input data ASAP. */
878 c = *(cs->cs_reg_data);
879 ZS_DELAY();
880
881 /* Save the status register too. */
882 rr1 = ZS_READ(cs, 1);
883
884 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
885 /* Clear the receive error. */
886 *(cs->cs_reg_csr) = ZSWR0_RESET_ERRORS;
887 ZS_DELAY();
888 }
889
890 /*
891 * Check NOW for a console abort sequence, so that we can
892 * abort even when interrupts are locking up the machine.
893 */
894 if (k->k_magic1_down) {
895 /* The last keycode was "MAGIC1" down. */
896 k->k_magic1_down = 0;
897 if ((c == k->k_magic2) && k->k_isconsole) {
898 /* Magic "L1-A" sequence; enter debugger. */
899 zs_abort();
900 /* Debugger done. Fake L1-up to finish it. */
901 c = k->k_magic1 | KBD_UP;
902 }
903 }
904 if (c == k->k_magic1) {
905 k->k_magic1_down = 1;
906 }
907
908 k->k_rbuf[put] = (c << 8) | rr1;
909 put_next = (put + 1) & KBD_RX_RING_MASK;
910
911 /* Would overrun if increment makes (put==get). */
912 if (put_next == k->k_rbget) {
913 k->k_intr_flags |= INTR_RX_OVERRUN;
914 } else {
915 /* OK, really increment. */
916 put = put_next;
917 }
918
919 /* Done reading. */
920 k->k_rbput = put;
921
922 /* Ask for softint() call. */
923 cs->cs_softreq = 1;
924 return(1);
925 }
926
927
928 static int
929 kbd_txint(cs)
930 register struct zs_chanstate *cs;
931 {
932 register struct kbd_softc *k;
933 register int count, rval;
934
935 k = cs->cs_private;
936
937 *(cs->cs_reg_csr) = ZSWR0_RESET_TXINT;
938 ZS_DELAY();
939
940 k->k_intr_flags |= INTR_TX_EMPTY;
941 /* Ask for softint() call. */
942 cs->cs_softreq = 1;
943 return (1);
944 }
945
946
947 static int
948 kbd_stint(cs)
949 register struct zs_chanstate *cs;
950 {
951 register struct kbd_softc *k;
952 register int rr0;
953
954 k = cs->cs_private;
955
956 rr0 = *(cs->cs_reg_csr);
957 ZS_DELAY();
958
959 *(cs->cs_reg_csr) = ZSWR0_RESET_STATUS;
960 ZS_DELAY();
961
962 #if 0
963 if (rr0 & ZSRR0_BREAK) {
964 /* Keyboard unplugged? */
965 zs_abort();
966 return (0);
967 }
968 #endif
969
970 k->k_intr_flags |= INTR_ST_CHECK;
971 /* Ask for softint() call. */
972 cs->cs_softreq = 1;
973 return (1);
974 }
975
976 /*
977 * Get input from the recieve ring and pass it on.
978 * Note: this is called at splsoftclock()
979 */
980 static int
981 kbd_softint(cs)
982 struct zs_chanstate *cs;
983 {
984 register struct kbd_softc *k;
985 register int get, c, s;
986 int intr_flags;
987 register u_short ring_data;
988 register u_char rr0, rr1;
989
990 k = cs->cs_private;
991
992 /* Atomically get and clear flags. */
993 s = splzs();
994 intr_flags = k->k_intr_flags;
995 k->k_intr_flags = 0;
996 splx(s);
997
998 /*
999 * Copy data from the receive ring to the event layer.
1000 */
1001 get = k->k_rbget;
1002 while (get != k->k_rbput) {
1003 ring_data = k->k_rbuf[get];
1004 get = (get + 1) & KBD_RX_RING_MASK;
1005
1006 /* low byte of ring_data is rr1 */
1007 c = (ring_data >> 8) & 0xff;
1008
1009 if (ring_data & ZSRR1_DO)
1010 intr_flags |= INTR_RX_OVERRUN;
1011 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
1012 /*
1013 * After garbage, flush pending input, and
1014 * send a reset to resync key translation.
1015 */
1016 log(LOG_ERR, "%s: input error (0x%x)\n",
1017 k->k_dev.dv_xname, ring_data);
1018 get = k->k_rbput; /* flush */
1019 goto send_reset;
1020 }
1021
1022 /* Pass this up to the "middle" layer. */
1023 kbd_input_raw(k, c);
1024 }
1025 if (intr_flags & INTR_RX_OVERRUN) {
1026 log(LOG_ERR, "%s: input overrun\n",
1027 k->k_dev.dv_xname);
1028 send_reset:
1029 /* Send a reset to resync translation. */
1030 kbd_output(k, KBD_CMD_RESET);
1031 kbd_start_tx(k);
1032 }
1033 k->k_rbget = get;
1034
1035 if (intr_flags & INTR_TX_EMPTY) {
1036 /*
1037 * Transmit done. Try to send more, or
1038 * clear busy and wakeup drain waiters.
1039 */
1040 k->k_txflags &= ~K_TXBUSY;
1041 kbd_start_tx(k);
1042 }
1043
1044 if (intr_flags & INTR_ST_CHECK) {
1045 /*
1046 * Status line change. (Not expected.)
1047 */
1048 log(LOG_ERR, "%s: status interrupt?\n",
1049 k->k_dev.dv_xname);
1050 }
1051
1052 return (1);
1053 }
1054
1055 struct zsops zsops_kbd = {
1056 kbd_rxint, /* receive char available */
1057 kbd_stint, /* external/status */
1058 kbd_txint, /* xmit buffer empty */
1059 kbd_softint, /* process software interrupt */
1060 };
1061
1062 /****************************************************************
1063 * misc...
1064 ****************************************************************/
1065
1066 /*
1067 * Initialization to be done at first open.
1068 * This is called from kbdopen or kdopen (in kd.c)
1069 */
1070 int
1071 kbd_iopen(unit)
1072 int unit;
1073 {
1074 struct kbd_softc *k;
1075 struct kbd_state *ks;
1076 int error, s;
1077
1078 if (unit >= kbdcd.cd_ndevs)
1079 return (ENXIO);
1080 k = kbdcd.cd_devs[unit];
1081 if (k == NULL)
1082 return (ENXIO);
1083 ks = &k->k_state;
1084 error = 0;
1085
1086 /* Tolerate extra calls. */
1087 if (k->k_isopen)
1088 return (error);
1089
1090 s = spltty();
1091
1092 /* Reset the keyboard and find out its type. */
1093 kbd_output(k, KBD_CMD_RESET);
1094 kbd_start_tx(k);
1095 kbd_drain_tx(k);
1096 /* The wakeup for this is in kbd_was_reset(). */
1097 error = tsleep((caddr_t)&ks->kbd_id,
1098 PZERO | PCATCH, devopn, hz);
1099 if (error == EWOULDBLOCK) { /* no response */
1100 error = 0;
1101 log(LOG_ERR, "%s: reset failed\n",
1102 k->k_dev.dv_xname);
1103 /*
1104 * Allow the open anyway (to keep getty happy)
1105 * but assume the "least common denominator".
1106 */
1107 ks->kbd_id = KB_SUN2;
1108 }
1109
1110 /* Earlier than type 4 does not know "layout". */
1111 if (ks->kbd_id < KB_SUN4)
1112 goto out;
1113
1114 /* Ask for the layout. */
1115 kbd_output(k, KBD_CMD_GETLAYOUT);
1116 kbd_start_tx(k);
1117 kbd_drain_tx(k);
1118 /* The wakeup for this is in kbd_new_layout(). */
1119 error = tsleep((caddr_t)&ks->kbd_layout,
1120 PZERO | PCATCH, devopn, hz);
1121 if (error == EWOULDBLOCK) { /* no response */
1122 error = 0;
1123 log(LOG_ERR, "%s: no response to get_layout\n",
1124 k->k_dev.dv_xname);
1125 ks->kbd_layout = 0;
1126 }
1127
1128 out:
1129 splx(s);
1130
1131 if (error == 0)
1132 k->k_isopen = 1;
1133
1134 return error;
1135 }
1136
1137 void
1138 kbd_was_reset(k)
1139 struct kbd_softc *k;
1140 {
1141 struct kbd_state *ks = &k->k_state;
1142
1143 /*
1144 * On first identification, wake up anyone waiting for type
1145 * and set up the table pointers.
1146 */
1147 wakeup((caddr_t)&ks->kbd_id);
1148
1149 /* Restore keyclick, if necessary */
1150 switch (ks->kbd_id) {
1151
1152 case KB_SUN2:
1153 /* Type 2 keyboards don't support keyclick */
1154 break;
1155
1156 case KB_SUN3:
1157 /* Type 3 keyboards come up with keyclick on */
1158 if (!ks->kbd_click)
1159 (void) kbd_docmd(k, KBD_CMD_NOCLICK);
1160 break;
1161
1162 case KB_SUN4:
1163 /* Type 4 keyboards come up with keyclick off */
1164 if (ks->kbd_click)
1165 (void) kbd_docmd(k, KBD_CMD_CLICK);
1166 break;
1167 }
1168
1169 /* LEDs are off after reset. */
1170 ks->kbd_leds = 0;
1171 }
1172
1173 void
1174 kbd_new_layout(k)
1175 struct kbd_softc *k;
1176 {
1177 struct kbd_state *ks = &k->k_state;
1178
1179 /*
1180 * On first identification, wake up anyone waiting for type
1181 * and set up the table pointers.
1182 */
1183 wakeup((caddr_t)&ks->kbd_layout);
1184
1185 /* XXX: switch decoding tables? */
1186 }
1187
1188
1189 /*
1190 * Wait for output to finish.
1191 * Called with user context.
1192 */
1193 int
1194 kbd_drain_tx(k)
1195 struct kbd_softc *k;
1196 {
1197 int error, s;
1198
1199 error = 0;
1200 s = spltty();
1201 while (k->k_txflags & K_TXBUSY) {
1202 k->k_txflags |= K_TXWANT;
1203 error = tsleep((caddr_t)&k->k_txflags,
1204 PZERO | PCATCH, "kbdout", 0);
1205 }
1206 splx(s);
1207 return (error);
1208 }
1209
1210 /*
1211 * Send out a byte to the keyboard (i.e. reset)
1212 * Called with user context.
1213 */
1214 void
1215 kbd_output(k, c)
1216 struct kbd_softc *k;
1217 int c; /* the data */
1218 {
1219 struct zs_chanstate *cs = k->k_cs;
1220 int put, s;
1221
1222 s = spltty();
1223 put = k->k_tbput;
1224 k->k_tbuf[put] = (u_char)c;
1225 put = (put + 1) & KBD_TX_RING_MASK;
1226
1227 /* Would overrun if increment makes (put==get). */
1228 if (put == k->k_tbget) {
1229 log(LOG_WARNING, "%s: output overrun\n",
1230 k->k_dev.dv_xname);
1231 } else {
1232 /* OK, really increment. */
1233 k->k_tbput = put;
1234 }
1235
1236 splx(s);
1237 }
1238
1239 void
1240 kbd_start_tx(k)
1241 struct kbd_softc *k;
1242 {
1243 struct zs_chanstate *cs = k->k_cs;
1244 int get, s;
1245 u_char c;
1246
1247 s = spltty();
1248 if (k->k_txflags & K_TXBUSY)
1249 goto out;
1250
1251 /* Is there anything to send? */
1252 get = k->k_tbget;
1253 if (get == k->k_tbput) {
1254 /* Nothing to send. Wake drain waiters. */
1255 if (k->k_txflags & K_TXWANT) {
1256 k->k_txflags &= ~K_TXWANT;
1257 wakeup((caddr_t)&k->k_txflags);
1258 }
1259 goto out;
1260 }
1261
1262 /* Have something to send. */
1263 c = k->k_tbuf[get];
1264 get = (get + 1) & KBD_TX_RING_MASK;
1265 k->k_tbget = get;
1266 k->k_txflags |= K_TXBUSY;
1267
1268 /* Need splzs to avoid interruption of the delay. */
1269 (void) splzs();
1270 *(cs->cs_reg_data) = c;
1271 ZS_DELAY();
1272
1273 out:
1274 splx(s);
1275 }
1276
1277
1278 int
1279 kbd_set_leds(k, data)
1280 struct kbd_softc *k;
1281 caddr_t data;
1282 {
1283 struct kbd_state *ks = &k->k_state;
1284 int error, s;
1285 char new_leds;
1286
1287 error = 0;
1288 new_leds = *(char*)data;
1289 s = spltty();
1290
1291 /* Don't send unless state changes. */
1292 if (ks->kbd_leds == new_leds)
1293 goto out;
1294 ks->kbd_leds = new_leds;
1295
1296 /* Only type 4 and later has LEDs anyway. */
1297 if (ks->kbd_id < 4)
1298 goto out;
1299
1300 error = kbd_drain_tx(k);
1301 kbd_output(k, KBD_CMD_SETLED);
1302 kbd_output(k, new_leds);
1303 kbd_start_tx(k);
1304
1305 out:
1306 splx(s);
1307 return(error);
1308 }
1309
1310
1311 /*
1312 * Execute a keyboard command; return 0 on success.
1313 */
1314 int
1315 kbd_docmd(k, cmd)
1316 struct kbd_softc *k;
1317 int cmd;
1318 {
1319 struct kbd_state *ks = &k->k_state;
1320 int error, s;
1321
1322 switch (cmd) {
1323
1324 case KBD_CMD_BELL:
1325 case KBD_CMD_NOBELL:
1326 /* Supported by type 2, 3, and 4 keyboards */
1327 break;
1328
1329 case KBD_CMD_CLICK:
1330 /* Unsupported by type 2 keyboards */
1331 if (ks->kbd_id != KB_SUN2) {
1332 ks->kbd_click = 1;
1333 break;
1334 }
1335 return (EINVAL);
1336
1337 case KBD_CMD_NOCLICK:
1338 /* Unsupported by type 2 keyboards */
1339 if (ks->kbd_id != KB_SUN2) {
1340 ks->kbd_click = 0;
1341 break;
1342 }
1343 return (EINVAL);
1344
1345 default:
1346 return (EINVAL); /* ENOTTY? EOPNOTSUPP? */
1347 }
1348
1349 kbd_output(k, cmd);
1350 kbd_start_tx(k);
1351 return (0);
1352 }
1353
1354