kbd.c revision 1.14 1 /* $NetBSD: kbd.c,v 1.14 1997/07/17 01:17:45 jtk 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/conf.h>
63 #include <sys/device.h>
64 #include <sys/ioctl.h>
65 #include <sys/kernel.h>
66 #include <sys/proc.h>
67 #include <sys/signal.h>
68 #include <sys/signalvar.h>
69 #include <sys/time.h>
70 #include <sys/syslog.h>
71 #include <sys/select.h>
72 #include <sys/poll.h>
73
74 #include <dev/ic/z8530reg.h>
75 #include <machine/z8530var.h>
76 #include <machine/vuid_event.h>
77 #include <machine/kbd.h>
78 #include <machine/kbio.h>
79
80 #include "event_var.h"
81 #include "kbd_xlate.h"
82 #include "locators.h"
83
84 /*
85 * Ideas:
86 * /dev/kbd is not a tty (plain device)
87 */
88
89 /*
90 * How many input characters we can buffer.
91 * The port-specific var.h may override this.
92 * Note: must be a power of two!
93 */
94 #define KBD_RX_RING_SIZE 256
95 #define KBD_RX_RING_MASK (KBD_RX_RING_SIZE-1)
96 /*
97 * Output buffer. Only need a few chars.
98 */
99 #define KBD_TX_RING_SIZE 16
100 #define KBD_TX_RING_MASK (KBD_TX_RING_SIZE-1)
101 /*
102 * Keyboard serial line speed is fixed at 1200 bps.
103 */
104 #define KBD_BPS 1200
105 #define KBD_RESET_TIMO 1000 /* mS. */
106
107 /*
108 * XXX - Historical comment - no longer quite right...
109 * Keyboard driver state. The ascii and kbd links go up and down and
110 * we just sit in the middle doing translation. Note that it is possible
111 * to get just one of the two links, in which case /dev/kbd is unavailable.
112 * The downlink supplies us with `internal' open and close routines which
113 * will enable dataflow across the downlink. We promise to call open when
114 * we are willing to take keystrokes, and to call close when we are not.
115 * If /dev/kbd is not the console tty input source, we do this whenever
116 * /dev/kbd is in use; otherwise we just leave it open forever.
117 */
118 struct kbd_softc {
119 struct device k_dev; /* required first: base device */
120 struct zs_chanstate *k_cs;
121
122 /* Flags to communicate with kbd_softint() */
123 volatile int k_intr_flags;
124 #define INTR_RX_OVERRUN 1
125 #define INTR_TX_EMPTY 2
126 #define INTR_ST_CHECK 4
127
128 /* Transmit state */
129 volatile int k_txflags;
130 #define K_TXBUSY 1
131 #define K_TXWANT 2
132
133 /*
134 * State of upper interface.
135 */
136 int k_isopen; /* set if open has been done */
137 int k_evmode; /* set if we should produce events */
138 struct evvar k_events; /* event queue state */
139
140 /*
141 * ACSI translation state
142 */
143 int k_repeat_start; /* initial delay */
144 int k_repeat_step; /* inter-char delay */
145 int k_repeatsym; /* repeating symbol */
146 int k_repeating; /* we've called timeout() */
147 struct kbd_state k_state; /* ASCII translation state */
148
149 /*
150 * Magic sequence stuff (L1-A)
151 */
152 char k_isconsole;
153 char k_magic1_down;
154 u_char k_magic1; /* L1 */
155 u_char k_magic2; /* A */
156
157 /*
158 * The transmit ring buffer.
159 */
160 volatile u_int k_tbget; /* transmit buffer `get' index */
161 volatile u_int k_tbput; /* transmit buffer `put' index */
162 u_char k_tbuf[KBD_TX_RING_SIZE]; /* data */
163
164 /*
165 * The receive ring buffer.
166 */
167 u_int k_rbget; /* ring buffer `get' index */
168 volatile u_int k_rbput; /* ring buffer `put' index */
169 u_short k_rbuf[KBD_RX_RING_SIZE]; /* rr1, data pairs */
170
171 };
172
173 /* Prototypes */
174 static void kbd_new_layout(struct kbd_softc *k);
175 static void kbd_output(struct kbd_softc *k, int c);
176 static void kbd_repeat(void *arg);
177 static void kbd_set_leds(struct kbd_softc *k, int leds);
178 static void kbd_start_tx(struct kbd_softc *k);
179 static void kbd_update_leds(struct kbd_softc *k);
180 static void kbd_was_reset(struct kbd_softc *k);
181 static int kbd_drain_tx(struct kbd_softc *k);
182
183 cdev_decl(kbd); /* open, close, read, write, ioctl, stop, ... */
184
185 struct zsops zsops_kbd;
186
187 /****************************************************************
188 * Definition of the driver for autoconfig.
189 ****************************************************************/
190
191 static int kbd_match(struct device *, struct cfdata *, void *);
192 static void kbd_attach(struct device *, struct device *, void *);
193
194 struct cfattach kbd_ca = {
195 sizeof(struct kbd_softc), kbd_match, kbd_attach
196 };
197
198 struct cfdriver kbd_cd = {
199 NULL, "kbd", DV_DULL
200 };
201
202
203 /*
204 * kbd_match: how is this zs channel configured?
205 */
206 int
207 kbd_match(parent, cf, aux)
208 struct device *parent;
209 struct cfdata *cf;
210 void *aux;
211 {
212 struct zsc_attach_args *args = aux;
213
214 /* Exact match required for keyboard. */
215 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
216 return 2;
217
218 return 0;
219 }
220
221 void
222 kbd_attach(parent, self, aux)
223 struct device *parent, *self;
224 void *aux;
225
226 {
227 struct zsc_softc *zsc = (void *) parent;
228 struct kbd_softc *k = (void *) self;
229 struct zsc_attach_args *args = aux;
230 struct zs_chanstate *cs;
231 struct cfdata *cf;
232 int channel, kbd_unit;
233 int reset, s;
234
235 cf = k->k_dev.dv_cfdata;
236 kbd_unit = k->k_dev.dv_unit;
237 channel = args->channel;
238 cs = zsc->zsc_cs[channel];
239 cs->cs_private = k;
240 cs->cs_ops = &zsops_kbd;
241 k->k_cs = cs;
242
243 if (args->hwflags & ZS_HWFLAG_CONSOLE) {
244 k->k_isconsole = 1;
245 printf(" (console)");
246 }
247 printf("\n");
248
249 /* Initialize the speed, etc. */
250 s = splzs();
251 if (k->k_isconsole == 0) {
252 /* Not the console; may need reset. */
253 reset = (channel == 0) ?
254 ZSWR9_A_RESET : ZSWR9_B_RESET;
255 zs_write_reg(cs, 9, reset);
256 }
257 /* These are OK as set by zscc: WR3, WR4, WR5 */
258 /* We don't care about status interrupts. */
259 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
260 (void) zs_set_speed(cs, KBD_BPS);
261 zs_loadchannelregs(cs);
262 splx(s);
263
264 /* Do this before any calls to kbd_rint(). */
265 kbd_xlate_init(&k->k_state);
266
267 /* XXX - Do this in open? */
268 k->k_repeat_start = hz/2;
269 k->k_repeat_step = hz/20;
270
271 /* Magic sequence. */
272 k->k_magic1 = KBD_L1;
273 k->k_magic2 = KBD_A;
274
275 /* Now attach the (kd) pseudo-driver. */
276 kd_init(kbd_unit);
277 }
278
279
280 /****************************************************************
281 * Entry points for /dev/kbd
282 * (open,close,read,write,...)
283 ****************************************************************/
284
285 /*
286 * Open:
287 * Check exclusion, open actual device (_iopen),
288 * setup event channel, clear ASCII repeat stuff.
289 */
290 int
291 kbdopen(dev, flags, mode, p)
292 dev_t dev;
293 int flags, mode;
294 struct proc *p;
295 {
296 struct kbd_softc *k;
297 int error, unit;
298
299 unit = minor(dev);
300 if (unit >= kbd_cd.cd_ndevs)
301 return (ENXIO);
302 k = kbd_cd.cd_devs[unit];
303 if (k == NULL)
304 return (ENXIO);
305
306 /* Exclusive open required for /dev/kbd */
307 if (k->k_events.ev_io)
308 return (EBUSY);
309 k->k_events.ev_io = p;
310
311 if ((error = kbd_iopen(unit)) != 0) {
312 k->k_events.ev_io = NULL;
313 return (error);
314 }
315 ev_init(&k->k_events);
316 k->k_evmode = 1; /* XXX: OK? */
317
318 if (k->k_repeating) {
319 k->k_repeating = 0;
320 untimeout(kbd_repeat, k);
321 }
322
323 return (0);
324 }
325
326 /*
327 * Close:
328 * Turn off event mode, dump the queue, and close the keyboard
329 * unless it is supplying console input.
330 */
331 int
332 kbdclose(dev, flags, mode, p)
333 dev_t dev;
334 int flags, mode;
335 struct proc *p;
336 {
337 struct kbd_softc *k;
338
339 k = kbd_cd.cd_devs[minor(dev)];
340 k->k_evmode = 0;
341 ev_fini(&k->k_events);
342 k->k_events.ev_io = NULL;
343 return (0);
344 }
345
346 int
347 kbdread(dev, uio, flags)
348 dev_t dev;
349 struct uio *uio;
350 int flags;
351 {
352 struct kbd_softc *k;
353
354 k = kbd_cd.cd_devs[minor(dev)];
355 return (ev_read(&k->k_events, uio, flags));
356 }
357
358 /* this routine should not exist, but is convenient to write here for now */
359 int
360 kbdwrite(dev, uio, flags)
361 dev_t dev;
362 struct uio *uio;
363 int flags;
364 {
365
366 return (EOPNOTSUPP);
367 }
368
369 int
370 kbdpoll(dev, events, p)
371 dev_t dev;
372 int events;
373 struct proc *p;
374 {
375 struct kbd_softc *k;
376
377 k = kbd_cd.cd_devs[minor(dev)];
378 return (ev_poll(&k->k_events, events, p));
379 }
380
381
382 static int kbd_ioccmd(struct kbd_softc *k, int *data);
383 static int kbd_iockeymap __P((struct kbd_state *ks,
384 u_long cmd, struct kiockeymap *kio));
385
386 static int kbd_iocsled(struct kbd_softc *k, int *data);
387
388 #ifdef KIOCGETKEY
389 static int kbd_oldkeymap __P((struct kbd_state *ks,
390 u_long cmd, struct okiockey *okio));
391 #endif
392
393 int
394 kbdioctl(dev, cmd, data, flag, p)
395 dev_t dev;
396 u_long cmd;
397 register caddr_t data;
398 int flag;
399 struct proc *p;
400 {
401 struct kbd_softc *k;
402 struct kbd_state *ks;
403 int *ip;
404 int error = 0;
405
406 k = kbd_cd.cd_devs[minor(dev)];
407 ks = &k->k_state;
408
409 switch (cmd) {
410
411 case KIOCTRANS: /* Set translation mode */
412 ip = (int *)data;
413 /* We only support "raw" mode on /dev/kbd */
414 if (*ip != TR_UNTRANS_EVENT)
415 error = EINVAL;
416 break;
417
418 case KIOCGTRANS: /* Get translation mode */
419 ip = (int *)data;
420 /* We only support "raw" mode on /dev/kbd */
421 *ip = TR_UNTRANS_EVENT;
422 break;
423
424 #ifdef KIOCGETKEY
425 case KIOCGETKEY: /* Get keymap entry (old format) */
426 error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
427 break;
428 #endif KIOCGETKEY */
429
430 case KIOCSKEY: /* Set keymap entry */
431 /* Don't let just anyone hose the keyboard. */
432 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
433 return (error);
434 /* fallthrough */
435 case KIOCGKEY: /* Get keymap entry */
436 error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
437 break;
438
439 case KIOCCMD: /* Send a command to the keyboard */
440 error = kbd_ioccmd(k, (int *)data);
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_iocsled(k, (int *)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 static int
491 kbd_iockeymap(ks, cmd, kio)
492 struct kbd_state *ks;
493 u_long cmd;
494 struct kiockeymap *kio;
495 {
496 u_short *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[station];
526 break;
527
528 case KIOCSKEY: /* Set keymap entry */
529 km[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 /*
577 * keyboard command ioctl
578 * ``unimplemented commands are ignored'' (blech)
579 */
580 static int
581 kbd_ioccmd(k, data)
582 struct kbd_softc *k;
583 int *data;
584 {
585 struct kbd_state *ks = &k->k_state;
586 int cmd, error, s;
587
588 cmd = *data;
589 switch (cmd) {
590
591 case KBD_CMD_BELL:
592 case KBD_CMD_NOBELL:
593 /* Supported by type 2, 3, and 4 keyboards */
594 break;
595
596 case KBD_CMD_CLICK:
597 case KBD_CMD_NOCLICK:
598 /* Unsupported by type 2 keyboards */
599 if (ks->kbd_id <= KB_SUN2)
600 return (0);
601 ks->kbd_click = (cmd == KBD_CMD_CLICK);
602 break;
603
604 default:
605 return (0);
606 }
607
608 s = spltty();
609
610 error = kbd_drain_tx(k);
611 if (error == 0) {
612 kbd_output(k, cmd);
613 kbd_start_tx(k);
614 }
615
616 splx(s);
617
618 return (error);
619 }
620
621 /*
622 * Set LEDs ioctl.
623 */
624 static int
625 kbd_iocsled(k, data)
626 struct kbd_softc *k;
627 int *data;
628 {
629 int leds, error, s;
630
631 leds = *data;
632
633 s = spltty();
634 error = kbd_drain_tx(k);
635 if (error == 0) {
636 kbd_set_leds(k, leds);
637 }
638 splx(s);
639
640 return (error);
641 }
642
643
644 /****************************************************************
645 * middle layers:
646 * - keysym to ASCII sequence
647 * - raw key codes to keysym
648 ****************************************************************/
649
650 static void kbd_input_string __P((struct kbd_softc *, char *));
651 static void kbd_input_funckey __P((struct kbd_softc *, int));
652 static void kbd_input_keysym __P((struct kbd_softc *, int));
653 static void kbd_input_raw __P((struct kbd_softc *k, int));
654
655 /*
656 * Initialization done by either kdcninit or kbd_iopen
657 */
658 void
659 kbd_xlate_init(ks)
660 struct kbd_state *ks;
661 {
662 struct keyboard *ktbls;
663 int id;
664
665 id = ks->kbd_id;
666 if (id < KBD_MIN_TYPE)
667 id = KBD_MIN_TYPE;
668 if (id > kbd_max_type)
669 id = kbd_max_type;
670 ktbls = keyboards[id];
671
672 ks->kbd_k = *ktbls; /* struct assignment */
673 ks->kbd_modbits = 0;
674 }
675
676 /*
677 * Turn keyboard up/down codes into a KEYSYM.
678 * Note that the "kd" driver uses this too!
679 */
680 int
681 kbd_code_to_keysym(ks, c)
682 register struct kbd_state *ks;
683 register int c;
684 {
685 u_short *km;
686 int keysym;
687
688 /*
689 * Get keymap pointer. One of these:
690 * release, control, shifted, normal, ...
691 */
692 if (KEY_UP(c))
693 km = ks->kbd_k.k_release;
694 else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
695 km = ks->kbd_k.k_control;
696 else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
697 km = ks->kbd_k.k_shifted;
698 else
699 km = ks->kbd_k.k_normal;
700
701 if (km == NULL) {
702 /*
703 * Do not know how to translate yet.
704 * We will find out when a RESET comes along.
705 */
706 return (KEYSYM_NOP);
707 }
708 keysym = km[KEY_CODE(c)];
709
710 /*
711 * Post-processing for Caps-lock
712 */
713 if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
714 (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
715 {
716 if (('a' <= keysym) && (keysym <= 'z'))
717 keysym -= ('a' - 'A');
718 }
719
720 /*
721 * Post-processing for Num-lock
722 */
723 if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
724 (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
725 {
726 keysym = kbd_numlock_map[keysym & 0x3F];
727 }
728
729 return (keysym);
730 }
731
732 void
733 kbd_input_string(k, str)
734 struct kbd_softc *k;
735 char *str;
736 {
737 while (*str) {
738 kd_input(*str);
739 str++;
740 }
741 }
742
743 void
744 kbd_input_funckey(k, keysym)
745 struct kbd_softc *k;
746 register int keysym;
747 {
748 register int n;
749 char str[12];
750
751 /*
752 * Format the F-key sequence and send as a string.
753 * XXX: Ugly compatibility mappings.
754 */
755 n = 0xC0 + (keysym & 0x3F);
756 sprintf(str, "\033[%dz", n);
757 kbd_input_string(k, str);
758 }
759
760 /*
761 * This is called by kbd_input_raw() or by kb_repeat()
762 * to deliver ASCII input. Called at spltty().
763 */
764 void
765 kbd_input_keysym(k, keysym)
766 struct kbd_softc *k;
767 register int keysym;
768 {
769 struct kbd_state *ks = &k->k_state;
770 register int data;
771
772 switch (KEYSYM_CLASS(keysym)) {
773
774 case KEYSYM_ASCII:
775 data = KEYSYM_DATA(keysym);
776 if (ks->kbd_modbits & KBMOD_META_MASK)
777 data |= 0x80;
778 kd_input(data);
779 break;
780
781 case KEYSYM_STRING:
782 data = keysym & 0xF;
783 kbd_input_string(k, kbd_stringtab[data]);
784 break;
785
786 case KEYSYM_FUNC:
787 kbd_input_funckey(k, keysym);
788 break;
789
790 case KEYSYM_CLRMOD:
791 data = 1 << (keysym & 0x1F);
792 ks->kbd_modbits &= ~data;
793 break;
794
795 case KEYSYM_SETMOD:
796 data = 1 << (keysym & 0x1F);
797 ks->kbd_modbits |= data;
798 break;
799
800 case KEYSYM_INVMOD:
801 data = 1 << (keysym & 0x1F);
802 ks->kbd_modbits ^= data;
803 kbd_update_leds(k);
804 break;
805
806 case KEYSYM_ALL_UP:
807 ks->kbd_modbits &= ~0xFFFF;
808 break;
809
810 case KEYSYM_SPECIAL:
811 if (keysym == KEYSYM_NOP)
812 break;
813 /* fall through */
814 default:
815 log(LOG_WARNING, "%s: unexpected keysym 0x%x\n",
816 k->k_dev.dv_xname, keysym);
817 break;
818 }
819 }
820
821 /*
822 * This is the autorepeat timeout function.
823 * Called at splsoftclock().
824 */
825 static void
826 kbd_repeat(void *arg)
827 {
828 struct kbd_softc *k = (struct kbd_softc *)arg;
829 int s = spltty();
830
831 if (k->k_repeating && k->k_repeatsym >= 0) {
832 kbd_input_keysym(k, k->k_repeatsym);
833 timeout(kbd_repeat, k, k->k_repeat_step);
834 }
835 splx(s);
836 }
837
838 /*
839 * Called by our kbd_softint() routine on input,
840 * which passes the raw hardware scan codes.
841 * Called at spltty()
842 */
843 void
844 kbd_input_raw(k, c)
845 struct kbd_softc *k;
846 register int c;
847 {
848 struct kbd_state *ks = &k->k_state;
849 struct firm_event *fe;
850 int put, keysym;
851
852 /* XXX - Input errors already handled. */
853
854 /* Are we expecting special input? */
855 if (ks->kbd_expect) {
856 if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
857 /* We read a KBD_RESET last time. */
858 ks->kbd_id = c;
859 kbd_was_reset(k);
860 }
861 if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
862 /* We read a KBD_LAYOUT last time. */
863 ks->kbd_layout = c;
864 kbd_new_layout(k);
865 }
866 ks->kbd_expect = 0;
867 return;
868 }
869
870 /* Is this one of the "special" input codes? */
871 if (KBD_SPECIAL(c)) {
872 switch (c) {
873 case KBD_RESET:
874 ks->kbd_expect |= KBD_EXPECT_IDCODE;
875 /* Fake an "all-up" to resync. translation. */
876 c = KBD_IDLE;
877 break;
878
879 case KBD_LAYOUT:
880 ks->kbd_expect |= KBD_EXPECT_LAYOUT;
881 return;
882
883 case KBD_ERROR:
884 log(LOG_WARNING, "%s: received error indicator\n",
885 k->k_dev.dv_xname);
886 return;
887
888 case KBD_IDLE:
889 /* Let this go to the translator. */
890 break;
891 }
892 }
893
894 /*
895 * If /dev/kbd is not connected in event mode,
896 * translate and send upstream (to console).
897 */
898 if (!k->k_evmode) {
899
900 /* Any input stops auto-repeat (i.e. key release). */
901 if (k->k_repeating) {
902 k->k_repeating = 0;
903 untimeout(kbd_repeat, k);
904 }
905
906 /* Translate this code to a keysym */
907 keysym = kbd_code_to_keysym(ks, c);
908
909 /* Pass up to the next layer. */
910 kbd_input_keysym(k, keysym);
911
912 /* Does this symbol get auto-repeat? */
913 if (KEYSYM_NOREPEAT(keysym))
914 return;
915
916 /* Setup for auto-repeat after initial delay. */
917 k->k_repeating = 1;
918 k->k_repeatsym = keysym;
919 timeout(kbd_repeat, k, k->k_repeat_start);
920 return;
921 }
922
923 /*
924 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
925 * This is bad as it means the server will not automatically resync
926 * on all-up IDLEs, but I did not drop them before, and the server
927 * goes crazy when it comes time to blank the screen....
928 */
929 if (c == KBD_IDLE)
930 return;
931
932 /*
933 * Keyboard is generating events. Turn this keystroke into an
934 * event and put it in the queue. If the queue is full, the
935 * keystroke is lost (sorry!).
936 */
937 put = k->k_events.ev_put;
938 fe = &k->k_events.ev_q[put];
939 put = (put + 1) % EV_QSIZE;
940 if (put == k->k_events.ev_get) {
941 log(LOG_WARNING, "%s: event queue overflow\n",
942 k->k_dev.dv_xname); /* ??? */
943 return;
944 }
945 fe->id = KEY_CODE(c);
946 fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
947 fe->time = time;
948 k->k_events.ev_put = put;
949 EV_WAKEUP(&k->k_events);
950 }
951
952 /****************************************************************
953 * Interface to the lower layer (zscc)
954 ****************************************************************/
955
956 static void kbd_rxint __P((struct zs_chanstate *));
957 static void kbd_txint __P((struct zs_chanstate *));
958 static void kbd_stint __P((struct zs_chanstate *));
959 static void kbd_softint __P((struct zs_chanstate *));
960
961 static void
962 kbd_rxint(cs)
963 register struct zs_chanstate *cs;
964 {
965 register struct kbd_softc *k;
966 register int put, put_next;
967 register u_char c, rr1;
968
969 k = cs->cs_private;
970 put = k->k_rbput;
971
972 /*
973 * First read the status, because reading the received char
974 * destroys the status of this char.
975 */
976 rr1 = zs_read_reg(cs, 1);
977 c = zs_read_data(cs);
978
979 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
980 /* Clear the receive error. */
981 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
982 }
983
984 /*
985 * Check NOW for a console abort sequence, so that we can
986 * abort even when interrupts are locking up the machine.
987 */
988 if (k->k_magic1_down) {
989 /* The last keycode was "MAGIC1" down. */
990 k->k_magic1_down = 0;
991 if ((c == k->k_magic2) && k->k_isconsole) {
992 /* Magic "L1-A" sequence; enter debugger. */
993 zs_abort(cs);
994 /* Debugger done. Fake L1-up to finish it. */
995 c = k->k_magic1 | KBD_UP;
996 }
997 }
998 if (c == k->k_magic1) {
999 k->k_magic1_down = 1;
1000 }
1001
1002 k->k_rbuf[put] = (c << 8) | rr1;
1003 put_next = (put + 1) & KBD_RX_RING_MASK;
1004
1005 /* Would overrun if increment makes (put==get). */
1006 if (put_next == k->k_rbget) {
1007 k->k_intr_flags |= INTR_RX_OVERRUN;
1008 } else {
1009 /* OK, really increment. */
1010 put = put_next;
1011 }
1012
1013 /* Done reading. */
1014 k->k_rbput = put;
1015
1016 /* Ask for softint() call. */
1017 cs->cs_softreq = 1;
1018 }
1019
1020
1021 static void
1022 kbd_txint(cs)
1023 register struct zs_chanstate *cs;
1024 {
1025 register struct kbd_softc *k;
1026
1027 k = cs->cs_private;
1028 zs_write_csr(cs, ZSWR0_RESET_TXINT);
1029 k->k_intr_flags |= INTR_TX_EMPTY;
1030 /* Ask for softint() call. */
1031 cs->cs_softreq = 1;
1032 }
1033
1034
1035 static void
1036 kbd_stint(cs)
1037 register struct zs_chanstate *cs;
1038 {
1039 register struct kbd_softc *k;
1040 register int rr0;
1041
1042 k = cs->cs_private;
1043
1044 rr0 = zs_read_csr(cs);
1045 zs_write_csr(cs, ZSWR0_RESET_STATUS);
1046
1047 #if 0
1048 if (rr0 & ZSRR0_BREAK) {
1049 /* Keyboard unplugged? */
1050 zs_abort(cs);
1051 return (0);
1052 }
1053 #endif
1054
1055 /*
1056 * We have to accumulate status line changes here.
1057 * Otherwise, if we get multiple status interrupts
1058 * before the softint runs, we could fail to notice
1059 * some status line changes in the softint routine.
1060 * Fix from Bill Studenmund, October 1996.
1061 */
1062 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
1063 cs->cs_rr0 = rr0;
1064 k->k_intr_flags |= INTR_ST_CHECK;
1065
1066 /* Ask for softint() call. */
1067 cs->cs_softreq = 1;
1068 }
1069
1070 /*
1071 * Get input from the recieve ring and pass it on.
1072 * Note: this is called at splsoftclock()
1073 */
1074 static void
1075 kbd_softint(cs)
1076 struct zs_chanstate *cs;
1077 {
1078 register struct kbd_softc *k;
1079 register int get, c, s;
1080 int intr_flags;
1081 register u_short ring_data;
1082
1083 k = cs->cs_private;
1084
1085 /* Atomically get and clear flags. */
1086 s = splzs();
1087 intr_flags = k->k_intr_flags;
1088 k->k_intr_flags = 0;
1089
1090 /* Now lower to spltty for the rest. */
1091 (void) spltty();
1092
1093 /*
1094 * Copy data from the receive ring to the event layer.
1095 */
1096 get = k->k_rbget;
1097 while (get != k->k_rbput) {
1098 ring_data = k->k_rbuf[get];
1099 get = (get + 1) & KBD_RX_RING_MASK;
1100
1101 /* low byte of ring_data is rr1 */
1102 c = (ring_data >> 8) & 0xff;
1103
1104 if (ring_data & ZSRR1_DO)
1105 intr_flags |= INTR_RX_OVERRUN;
1106 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
1107 /*
1108 * After garbage, flush pending input, and
1109 * send a reset to resync key translation.
1110 */
1111 log(LOG_ERR, "%s: input error (0x%x)\n",
1112 k->k_dev.dv_xname, ring_data);
1113 get = k->k_rbput; /* flush */
1114 goto send_reset;
1115 }
1116
1117 /* Pass this up to the "middle" layer. */
1118 kbd_input_raw(k, c);
1119 }
1120 if (intr_flags & INTR_RX_OVERRUN) {
1121 log(LOG_ERR, "%s: input overrun\n",
1122 k->k_dev.dv_xname);
1123 send_reset:
1124 /* Send a reset to resync translation. */
1125 kbd_output(k, KBD_CMD_RESET);
1126 kbd_start_tx(k);
1127 }
1128 k->k_rbget = get;
1129
1130 if (intr_flags & INTR_TX_EMPTY) {
1131 /*
1132 * Transmit done. Try to send more, or
1133 * clear busy and wakeup drain waiters.
1134 */
1135 k->k_txflags &= ~K_TXBUSY;
1136 kbd_start_tx(k);
1137 }
1138
1139 if (intr_flags & INTR_ST_CHECK) {
1140 /*
1141 * Status line change. (Not expected.)
1142 */
1143 log(LOG_ERR, "%s: status interrupt?\n",
1144 k->k_dev.dv_xname);
1145 cs->cs_rr0_delta = 0;
1146 }
1147
1148 splx(s);
1149 }
1150
1151 struct zsops zsops_kbd = {
1152 kbd_rxint, /* receive char available */
1153 kbd_stint, /* external/status */
1154 kbd_txint, /* xmit buffer empty */
1155 kbd_softint, /* process software interrupt */
1156 };
1157
1158 /****************************************************************
1159 * misc...
1160 ****************************************************************/
1161
1162 /*
1163 * Initialization to be done at first open.
1164 * This is called from kbdopen or kdopen (in kd.c)
1165 * Called with user context.
1166 */
1167 int
1168 kbd_iopen(unit)
1169 int unit;
1170 {
1171 struct kbd_softc *k;
1172 struct kbd_state *ks;
1173 int error, s;
1174
1175 if (unit >= kbd_cd.cd_ndevs)
1176 return (ENXIO);
1177 k = kbd_cd.cd_devs[unit];
1178 if (k == NULL)
1179 return (ENXIO);
1180 ks = &k->k_state;
1181 error = 0;
1182
1183 /* Tolerate extra calls. */
1184 if (k->k_isopen)
1185 return (error);
1186
1187 s = spltty();
1188
1189 /* Reset the keyboard and find out its type. */
1190 kbd_output(k, KBD_CMD_RESET);
1191 kbd_start_tx(k);
1192 kbd_drain_tx(k);
1193 /* The wakeup for this is in kbd_was_reset(). */
1194 error = tsleep((caddr_t)&ks->kbd_id,
1195 PZERO | PCATCH, devopn, hz);
1196 if (error == EWOULDBLOCK) { /* no response */
1197 error = 0;
1198 log(LOG_ERR, "%s: reset failed\n",
1199 k->k_dev.dv_xname);
1200 /*
1201 * Allow the open anyway (to keep getty happy)
1202 * but assume the "least common denominator".
1203 */
1204 ks->kbd_id = KB_SUN2;
1205 }
1206
1207 /* Earlier than type 4 does not know "layout". */
1208 if (ks->kbd_id < KB_SUN4)
1209 goto out;
1210
1211 /* Ask for the layout. */
1212 kbd_output(k, KBD_CMD_GETLAYOUT);
1213 kbd_start_tx(k);
1214 kbd_drain_tx(k);
1215 /* The wakeup for this is in kbd_new_layout(). */
1216 error = tsleep((caddr_t)&ks->kbd_layout,
1217 PZERO | PCATCH, devopn, hz);
1218 if (error == EWOULDBLOCK) { /* no response */
1219 error = 0;
1220 log(LOG_ERR, "%s: no response to get_layout\n",
1221 k->k_dev.dv_xname);
1222 ks->kbd_layout = 0;
1223 }
1224
1225 out:
1226 splx(s);
1227
1228 if (error == 0)
1229 k->k_isopen = 1;
1230
1231 return error;
1232 }
1233
1234 /*
1235 * Called by kbd_input_raw, at spltty()
1236 */
1237 static void
1238 kbd_was_reset(k)
1239 struct kbd_softc *k;
1240 {
1241 struct kbd_state *ks = &k->k_state;
1242
1243 /*
1244 * On first identification, wake up anyone waiting for type
1245 * and set up the table pointers.
1246 */
1247 wakeup((caddr_t)&ks->kbd_id);
1248
1249 /* Restore keyclick, if necessary */
1250 switch (ks->kbd_id) {
1251
1252 case KB_SUN2:
1253 /* Type 2 keyboards don't support keyclick */
1254 break;
1255
1256 case KB_SUN3:
1257 /* Type 3 keyboards come up with keyclick on */
1258 if (!ks->kbd_click) {
1259 /* turn off the click */
1260 kbd_output(k, KBD_CMD_NOCLICK);
1261 kbd_start_tx(k);
1262 }
1263 break;
1264
1265 case KB_SUN4:
1266 /* Type 4 keyboards come up with keyclick off */
1267 if (ks->kbd_click) {
1268 /* turn on the click */
1269 kbd_output(k, KBD_CMD_CLICK);
1270 kbd_start_tx(k);
1271 }
1272 break;
1273 }
1274
1275 /* LEDs are off after reset. */
1276 ks->kbd_leds = 0;
1277 }
1278
1279 /*
1280 * Called by kbd_input_raw, at spltty()
1281 */
1282 static void
1283 kbd_new_layout(k)
1284 struct kbd_softc *k;
1285 {
1286 struct kbd_state *ks = &k->k_state;
1287
1288 /*
1289 * On first identification, wake up anyone waiting for type
1290 * and set up the table pointers.
1291 */
1292 wakeup((caddr_t)&ks->kbd_layout);
1293
1294 /* XXX: switch decoding tables? */
1295 }
1296
1297
1298 /*
1299 * Wait for output to finish.
1300 * Called at spltty(). Has user context.
1301 */
1302 static int
1303 kbd_drain_tx(k)
1304 struct kbd_softc *k;
1305 {
1306 int error;
1307
1308 error = 0;
1309
1310 while (k->k_txflags & K_TXBUSY) {
1311 k->k_txflags |= K_TXWANT;
1312 error = tsleep((caddr_t)&k->k_txflags,
1313 PZERO | PCATCH, "kbdout", 0);
1314 }
1315
1316 return (error);
1317 }
1318
1319 /*
1320 * Enqueue some output for the keyboard
1321 * Called at spltty().
1322 */
1323 static void
1324 kbd_output(k, c)
1325 struct kbd_softc *k;
1326 int c; /* the data */
1327 {
1328 int put;
1329
1330 put = k->k_tbput;
1331 k->k_tbuf[put] = (u_char)c;
1332 put = (put + 1) & KBD_TX_RING_MASK;
1333
1334 /* Would overrun if increment makes (put==get). */
1335 if (put == k->k_tbget) {
1336 log(LOG_WARNING, "%s: output overrun\n",
1337 k->k_dev.dv_xname);
1338 } else {
1339 /* OK, really increment. */
1340 k->k_tbput = put;
1341 }
1342 }
1343
1344 /*
1345 * Start the sending data from the output queue
1346 * Called at spltty().
1347 */
1348 static void
1349 kbd_start_tx(k)
1350 struct kbd_softc *k;
1351 {
1352 struct zs_chanstate *cs = k->k_cs;
1353 int get, s;
1354 u_char c;
1355
1356 if (k->k_txflags & K_TXBUSY)
1357 return;
1358
1359 /* Is there anything to send? */
1360 get = k->k_tbget;
1361 if (get == k->k_tbput) {
1362 /* Nothing to send. Wake drain waiters. */
1363 if (k->k_txflags & K_TXWANT) {
1364 k->k_txflags &= ~K_TXWANT;
1365 wakeup((caddr_t)&k->k_txflags);
1366 }
1367 return;
1368 }
1369
1370 /* Have something to send. */
1371 c = k->k_tbuf[get];
1372 get = (get + 1) & KBD_TX_RING_MASK;
1373 k->k_tbget = get;
1374 k->k_txflags |= K_TXBUSY;
1375
1376 /* Need splzs to avoid interruption of the delay. */
1377 s = splzs();
1378 zs_write_data(cs, c);
1379 splx(s);
1380 }
1381
1382 /*
1383 * Called at spltty by:
1384 * kbd_update_leds, kbd_iocsled
1385 */
1386 static void
1387 kbd_set_leds(k, new_leds)
1388 struct kbd_softc *k;
1389 int new_leds;
1390 {
1391 struct kbd_state *ks = &k->k_state;
1392
1393 /* Don't send unless state changes. */
1394 if (ks->kbd_leds == new_leds)
1395 return;
1396
1397 ks->kbd_leds = new_leds;
1398
1399 /* Only type 4 and later has LEDs anyway. */
1400 if (ks->kbd_id < 4)
1401 return;
1402
1403 kbd_output(k, KBD_CMD_SETLED);
1404 kbd_output(k, new_leds);
1405 kbd_start_tx(k);
1406 }
1407
1408 /*
1409 * Called at spltty by:
1410 * kbd_input_keysym
1411 */
1412 static void
1413 kbd_update_leds(k)
1414 struct kbd_softc *k;
1415 {
1416 struct kbd_state *ks = &k->k_state;
1417 register char leds;
1418
1419 leds = ks->kbd_leds;
1420 leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
1421
1422 if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
1423 leds |= LED_CAPS_LOCK;
1424 if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
1425 leds |= LED_NUM_LOCK;
1426
1427 kbd_set_leds(k, leds);
1428 }
1429
1430