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