kbd.c revision 1.15 1 /* $NetBSD: kbd.c,v 1.15 1997/10/03 23:04:46 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 #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_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_docmd(*((int *)data), 1);
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 * This is also export to the fb driver.
579 */
580 int
581 kbd_docmd(cmd, isuser)
582 int cmd;
583 int isuser;
584 {
585 struct kbd_softc *k;
586 struct kbd_state *ks;
587 int error, s;
588
589 error = 0;
590 k = kbd_cd.cd_devs[0];
591 ks = &k->k_state;
592
593 switch (cmd) {
594
595 case KBD_CMD_BELL:
596 case KBD_CMD_NOBELL:
597 /* Supported by type 2, 3, and 4 keyboards */
598 break;
599
600 case KBD_CMD_CLICK:
601 case KBD_CMD_NOCLICK:
602 /* Unsupported by type 2 keyboards */
603 if (ks->kbd_id <= KB_SUN2)
604 return (0);
605 ks->kbd_click = (cmd == KBD_CMD_CLICK);
606 break;
607
608 default:
609 return (0);
610 }
611
612 s = spltty();
613
614 if (isuser)
615 error = kbd_drain_tx(k);
616
617 if (error == 0) {
618 kbd_output(k, cmd);
619 kbd_start_tx(k);
620 }
621
622 splx(s);
623
624 return (error);
625 }
626
627 /*
628 * Set LEDs ioctl.
629 */
630 static int
631 kbd_iocsled(k, data)
632 struct kbd_softc *k;
633 int *data;
634 {
635 int leds, error, s;
636
637 leds = *data;
638
639 s = spltty();
640 error = kbd_drain_tx(k);
641 if (error == 0) {
642 kbd_set_leds(k, leds);
643 }
644 splx(s);
645
646 return (error);
647 }
648
649
650 /****************************************************************
651 * middle layers:
652 * - keysym to ASCII sequence
653 * - raw key codes to keysym
654 ****************************************************************/
655
656 static void kbd_input_string __P((struct kbd_softc *, char *));
657 static void kbd_input_funckey __P((struct kbd_softc *, int));
658 static void kbd_input_keysym __P((struct kbd_softc *, int));
659 static void kbd_input_raw __P((struct kbd_softc *k, int));
660
661 /*
662 * Initialization done by either kdcninit or kbd_iopen
663 */
664 void
665 kbd_xlate_init(ks)
666 struct kbd_state *ks;
667 {
668 struct keyboard *ktbls;
669 int id;
670
671 id = ks->kbd_id;
672 if (id < KBD_MIN_TYPE)
673 id = KBD_MIN_TYPE;
674 if (id > kbd_max_type)
675 id = kbd_max_type;
676 ktbls = keyboards[id];
677
678 ks->kbd_k = *ktbls; /* struct assignment */
679 ks->kbd_modbits = 0;
680 }
681
682 /*
683 * Turn keyboard up/down codes into a KEYSYM.
684 * Note that the "kd" driver uses this too!
685 */
686 int
687 kbd_code_to_keysym(ks, c)
688 register struct kbd_state *ks;
689 register int c;
690 {
691 u_short *km;
692 int keysym;
693
694 /*
695 * Get keymap pointer. One of these:
696 * release, control, shifted, normal, ...
697 */
698 if (KEY_UP(c))
699 km = ks->kbd_k.k_release;
700 else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
701 km = ks->kbd_k.k_control;
702 else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
703 km = ks->kbd_k.k_shifted;
704 else
705 km = ks->kbd_k.k_normal;
706
707 if (km == NULL) {
708 /*
709 * Do not know how to translate yet.
710 * We will find out when a RESET comes along.
711 */
712 return (KEYSYM_NOP);
713 }
714 keysym = km[KEY_CODE(c)];
715
716 /*
717 * Post-processing for Caps-lock
718 */
719 if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
720 (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
721 {
722 if (('a' <= keysym) && (keysym <= 'z'))
723 keysym -= ('a' - 'A');
724 }
725
726 /*
727 * Post-processing for Num-lock
728 */
729 if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
730 (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
731 {
732 keysym = kbd_numlock_map[keysym & 0x3F];
733 }
734
735 return (keysym);
736 }
737
738 void
739 kbd_input_string(k, str)
740 struct kbd_softc *k;
741 char *str;
742 {
743 while (*str) {
744 kd_input(*str);
745 str++;
746 }
747 }
748
749 void
750 kbd_input_funckey(k, keysym)
751 struct kbd_softc *k;
752 register int keysym;
753 {
754 register int n;
755 char str[12];
756
757 /*
758 * Format the F-key sequence and send as a string.
759 * XXX: Ugly compatibility mappings.
760 */
761 n = 0xC0 + (keysym & 0x3F);
762 sprintf(str, "\033[%dz", n);
763 kbd_input_string(k, str);
764 }
765
766 /*
767 * This is called by kbd_input_raw() or by kb_repeat()
768 * to deliver ASCII input. Called at spltty().
769 */
770 void
771 kbd_input_keysym(k, keysym)
772 struct kbd_softc *k;
773 register int keysym;
774 {
775 struct kbd_state *ks = &k->k_state;
776 register int data;
777
778 switch (KEYSYM_CLASS(keysym)) {
779
780 case KEYSYM_ASCII:
781 data = KEYSYM_DATA(keysym);
782 if (ks->kbd_modbits & KBMOD_META_MASK)
783 data |= 0x80;
784 kd_input(data);
785 break;
786
787 case KEYSYM_STRING:
788 data = keysym & 0xF;
789 kbd_input_string(k, kbd_stringtab[data]);
790 break;
791
792 case KEYSYM_FUNC:
793 kbd_input_funckey(k, keysym);
794 break;
795
796 case KEYSYM_CLRMOD:
797 data = 1 << (keysym & 0x1F);
798 ks->kbd_modbits &= ~data;
799 break;
800
801 case KEYSYM_SETMOD:
802 data = 1 << (keysym & 0x1F);
803 ks->kbd_modbits |= data;
804 break;
805
806 case KEYSYM_INVMOD:
807 data = 1 << (keysym & 0x1F);
808 ks->kbd_modbits ^= data;
809 kbd_update_leds(k);
810 break;
811
812 case KEYSYM_ALL_UP:
813 ks->kbd_modbits &= ~0xFFFF;
814 break;
815
816 case KEYSYM_SPECIAL:
817 if (keysym == KEYSYM_NOP)
818 break;
819 /* fall through */
820 default:
821 log(LOG_WARNING, "%s: unexpected keysym 0x%x\n",
822 k->k_dev.dv_xname, keysym);
823 break;
824 }
825 }
826
827 /*
828 * This is the autorepeat timeout function.
829 * Called at splsoftclock().
830 */
831 static void
832 kbd_repeat(void *arg)
833 {
834 struct kbd_softc *k = (struct kbd_softc *)arg;
835 int s = spltty();
836
837 if (k->k_repeating && k->k_repeatsym >= 0) {
838 kbd_input_keysym(k, k->k_repeatsym);
839 timeout(kbd_repeat, k, k->k_repeat_step);
840 }
841 splx(s);
842 }
843
844 /*
845 * Called by our kbd_softint() routine on input,
846 * which passes the raw hardware scan codes.
847 * Called at spltty()
848 */
849 void
850 kbd_input_raw(k, c)
851 struct kbd_softc *k;
852 register int c;
853 {
854 struct kbd_state *ks = &k->k_state;
855 struct firm_event *fe;
856 int put, keysym;
857
858 /* XXX - Input errors already handled. */
859
860 /* Are we expecting special input? */
861 if (ks->kbd_expect) {
862 if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
863 /* We read a KBD_RESET last time. */
864 ks->kbd_id = c;
865 kbd_was_reset(k);
866 }
867 if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
868 /* We read a KBD_LAYOUT last time. */
869 ks->kbd_layout = c;
870 kbd_new_layout(k);
871 }
872 ks->kbd_expect = 0;
873 return;
874 }
875
876 /* Is this one of the "special" input codes? */
877 if (KBD_SPECIAL(c)) {
878 switch (c) {
879 case KBD_RESET:
880 ks->kbd_expect |= KBD_EXPECT_IDCODE;
881 /* Fake an "all-up" to resync. translation. */
882 c = KBD_IDLE;
883 break;
884
885 case KBD_LAYOUT:
886 ks->kbd_expect |= KBD_EXPECT_LAYOUT;
887 return;
888
889 case KBD_ERROR:
890 log(LOG_WARNING, "%s: received error indicator\n",
891 k->k_dev.dv_xname);
892 return;
893
894 case KBD_IDLE:
895 /* Let this go to the translator. */
896 break;
897 }
898 }
899
900 /*
901 * If /dev/kbd is not connected in event mode,
902 * translate and send upstream (to console).
903 */
904 if (!k->k_evmode) {
905
906 /* Any input stops auto-repeat (i.e. key release). */
907 if (k->k_repeating) {
908 k->k_repeating = 0;
909 untimeout(kbd_repeat, k);
910 }
911
912 /* Translate this code to a keysym */
913 keysym = kbd_code_to_keysym(ks, c);
914
915 /* Pass up to the next layer. */
916 kbd_input_keysym(k, keysym);
917
918 /* Does this symbol get auto-repeat? */
919 if (KEYSYM_NOREPEAT(keysym))
920 return;
921
922 /* Setup for auto-repeat after initial delay. */
923 k->k_repeating = 1;
924 k->k_repeatsym = keysym;
925 timeout(kbd_repeat, k, k->k_repeat_start);
926 return;
927 }
928
929 /*
930 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
931 * This is bad as it means the server will not automatically resync
932 * on all-up IDLEs, but I did not drop them before, and the server
933 * goes crazy when it comes time to blank the screen....
934 */
935 if (c == KBD_IDLE)
936 return;
937
938 /*
939 * Keyboard is generating events. Turn this keystroke into an
940 * event and put it in the queue. If the queue is full, the
941 * keystroke is lost (sorry!).
942 */
943 put = k->k_events.ev_put;
944 fe = &k->k_events.ev_q[put];
945 put = (put + 1) % EV_QSIZE;
946 if (put == k->k_events.ev_get) {
947 log(LOG_WARNING, "%s: event queue overflow\n",
948 k->k_dev.dv_xname); /* ??? */
949 return;
950 }
951 fe->id = KEY_CODE(c);
952 fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
953 fe->time = time;
954 k->k_events.ev_put = put;
955 EV_WAKEUP(&k->k_events);
956 }
957
958 /****************************************************************
959 * Interface to the lower layer (zscc)
960 ****************************************************************/
961
962 static void kbd_rxint __P((struct zs_chanstate *));
963 static void kbd_txint __P((struct zs_chanstate *));
964 static void kbd_stint __P((struct zs_chanstate *));
965 static void kbd_softint __P((struct zs_chanstate *));
966
967 static void
968 kbd_rxint(cs)
969 register struct zs_chanstate *cs;
970 {
971 register struct kbd_softc *k;
972 register int put, put_next;
973 register u_char c, rr1;
974
975 k = cs->cs_private;
976 put = k->k_rbput;
977
978 /*
979 * First read the status, because reading the received char
980 * destroys the status of this char.
981 */
982 rr1 = zs_read_reg(cs, 1);
983 c = zs_read_data(cs);
984
985 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
986 /* Clear the receive error. */
987 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
988 }
989
990 /*
991 * Check NOW for a console abort sequence, so that we can
992 * abort even when interrupts are locking up the machine.
993 */
994 if (k->k_magic1_down) {
995 /* The last keycode was "MAGIC1" down. */
996 k->k_magic1_down = 0;
997 if ((c == k->k_magic2) && k->k_isconsole) {
998 /* Magic "L1-A" sequence; enter debugger. */
999 zs_abort(cs);
1000 /* Debugger done. Fake L1-up to finish it. */
1001 c = k->k_magic1 | KBD_UP;
1002 }
1003 }
1004 if (c == k->k_magic1) {
1005 k->k_magic1_down = 1;
1006 }
1007
1008 k->k_rbuf[put] = (c << 8) | rr1;
1009 put_next = (put + 1) & KBD_RX_RING_MASK;
1010
1011 /* Would overrun if increment makes (put==get). */
1012 if (put_next == k->k_rbget) {
1013 k->k_intr_flags |= INTR_RX_OVERRUN;
1014 } else {
1015 /* OK, really increment. */
1016 put = put_next;
1017 }
1018
1019 /* Done reading. */
1020 k->k_rbput = put;
1021
1022 /* Ask for softint() call. */
1023 cs->cs_softreq = 1;
1024 }
1025
1026
1027 static void
1028 kbd_txint(cs)
1029 register struct zs_chanstate *cs;
1030 {
1031 register struct kbd_softc *k;
1032
1033 k = cs->cs_private;
1034 zs_write_csr(cs, ZSWR0_RESET_TXINT);
1035 k->k_intr_flags |= INTR_TX_EMPTY;
1036 /* Ask for softint() call. */
1037 cs->cs_softreq = 1;
1038 }
1039
1040
1041 static void
1042 kbd_stint(cs)
1043 register struct zs_chanstate *cs;
1044 {
1045 register struct kbd_softc *k;
1046 register int rr0;
1047
1048 k = cs->cs_private;
1049
1050 rr0 = zs_read_csr(cs);
1051 zs_write_csr(cs, ZSWR0_RESET_STATUS);
1052
1053 #if 0
1054 if (rr0 & ZSRR0_BREAK) {
1055 /* Keyboard unplugged? */
1056 zs_abort(cs);
1057 return (0);
1058 }
1059 #endif
1060
1061 /*
1062 * We have to accumulate status line changes here.
1063 * Otherwise, if we get multiple status interrupts
1064 * before the softint runs, we could fail to notice
1065 * some status line changes in the softint routine.
1066 * Fix from Bill Studenmund, October 1996.
1067 */
1068 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
1069 cs->cs_rr0 = rr0;
1070 k->k_intr_flags |= INTR_ST_CHECK;
1071
1072 /* Ask for softint() call. */
1073 cs->cs_softreq = 1;
1074 }
1075
1076 /*
1077 * Get input from the recieve ring and pass it on.
1078 * Note: this is called at splsoftclock()
1079 */
1080 static void
1081 kbd_softint(cs)
1082 struct zs_chanstate *cs;
1083 {
1084 register struct kbd_softc *k;
1085 register int get, c, s;
1086 int intr_flags;
1087 register u_short ring_data;
1088
1089 k = cs->cs_private;
1090
1091 /* Atomically get and clear flags. */
1092 s = splzs();
1093 intr_flags = k->k_intr_flags;
1094 k->k_intr_flags = 0;
1095
1096 /* Now lower to spltty for the rest. */
1097 (void) spltty();
1098
1099 /*
1100 * Copy data from the receive ring to the event layer.
1101 */
1102 get = k->k_rbget;
1103 while (get != k->k_rbput) {
1104 ring_data = k->k_rbuf[get];
1105 get = (get + 1) & KBD_RX_RING_MASK;
1106
1107 /* low byte of ring_data is rr1 */
1108 c = (ring_data >> 8) & 0xff;
1109
1110 if (ring_data & ZSRR1_DO)
1111 intr_flags |= INTR_RX_OVERRUN;
1112 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
1113 /*
1114 * After garbage, flush pending input, and
1115 * send a reset to resync key translation.
1116 */
1117 log(LOG_ERR, "%s: input error (0x%x)\n",
1118 k->k_dev.dv_xname, ring_data);
1119 get = k->k_rbput; /* flush */
1120 goto send_reset;
1121 }
1122
1123 /* Pass this up to the "middle" layer. */
1124 kbd_input_raw(k, c);
1125 }
1126 if (intr_flags & INTR_RX_OVERRUN) {
1127 log(LOG_ERR, "%s: input overrun\n",
1128 k->k_dev.dv_xname);
1129 send_reset:
1130 /* Send a reset to resync translation. */
1131 kbd_output(k, KBD_CMD_RESET);
1132 kbd_start_tx(k);
1133 }
1134 k->k_rbget = get;
1135
1136 if (intr_flags & INTR_TX_EMPTY) {
1137 /*
1138 * Transmit done. Try to send more, or
1139 * clear busy and wakeup drain waiters.
1140 */
1141 k->k_txflags &= ~K_TXBUSY;
1142 kbd_start_tx(k);
1143 }
1144
1145 if (intr_flags & INTR_ST_CHECK) {
1146 /*
1147 * Status line change. (Not expected.)
1148 */
1149 log(LOG_ERR, "%s: status interrupt?\n",
1150 k->k_dev.dv_xname);
1151 cs->cs_rr0_delta = 0;
1152 }
1153
1154 splx(s);
1155 }
1156
1157 struct zsops zsops_kbd = {
1158 kbd_rxint, /* receive char available */
1159 kbd_stint, /* external/status */
1160 kbd_txint, /* xmit buffer empty */
1161 kbd_softint, /* process software interrupt */
1162 };
1163
1164 /****************************************************************
1165 * misc...
1166 ****************************************************************/
1167
1168 /*
1169 * Initialization to be done at first open.
1170 * This is called from kbdopen or kdopen (in kd.c)
1171 * Called with user context.
1172 */
1173 int
1174 kbd_iopen(unit)
1175 int unit;
1176 {
1177 struct kbd_softc *k;
1178 struct kbd_state *ks;
1179 int error, s;
1180
1181 if (unit >= kbd_cd.cd_ndevs)
1182 return (ENXIO);
1183 k = kbd_cd.cd_devs[unit];
1184 if (k == NULL)
1185 return (ENXIO);
1186 ks = &k->k_state;
1187 error = 0;
1188
1189 /* Tolerate extra calls. */
1190 if (k->k_isopen)
1191 return (error);
1192
1193 s = spltty();
1194
1195 /* Reset the keyboard and find out its type. */
1196 kbd_output(k, KBD_CMD_RESET);
1197 kbd_start_tx(k);
1198 kbd_drain_tx(k);
1199 /* The wakeup for this is in kbd_was_reset(). */
1200 error = tsleep((caddr_t)&ks->kbd_id,
1201 PZERO | PCATCH, devopn, hz);
1202 if (error == EWOULDBLOCK) { /* no response */
1203 error = 0;
1204 log(LOG_ERR, "%s: reset failed\n",
1205 k->k_dev.dv_xname);
1206 /*
1207 * Allow the open anyway (to keep getty happy)
1208 * but assume the "least common denominator".
1209 */
1210 ks->kbd_id = KB_SUN2;
1211 }
1212
1213 /* Earlier than type 4 does not know "layout". */
1214 if (ks->kbd_id < KB_SUN4)
1215 goto out;
1216
1217 /* Ask for the layout. */
1218 kbd_output(k, KBD_CMD_GETLAYOUT);
1219 kbd_start_tx(k);
1220 kbd_drain_tx(k);
1221 /* The wakeup for this is in kbd_new_layout(). */
1222 error = tsleep((caddr_t)&ks->kbd_layout,
1223 PZERO | PCATCH, devopn, hz);
1224 if (error == EWOULDBLOCK) { /* no response */
1225 error = 0;
1226 log(LOG_ERR, "%s: no response to get_layout\n",
1227 k->k_dev.dv_xname);
1228 ks->kbd_layout = 0;
1229 }
1230
1231 out:
1232 splx(s);
1233
1234 if (error == 0)
1235 k->k_isopen = 1;
1236
1237 return error;
1238 }
1239
1240 /*
1241 * Called by kbd_input_raw, at spltty()
1242 */
1243 static void
1244 kbd_was_reset(k)
1245 struct kbd_softc *k;
1246 {
1247 struct kbd_state *ks = &k->k_state;
1248
1249 /*
1250 * On first identification, wake up anyone waiting for type
1251 * and set up the table pointers.
1252 */
1253 wakeup((caddr_t)&ks->kbd_id);
1254
1255 /* Restore keyclick, if necessary */
1256 switch (ks->kbd_id) {
1257
1258 case KB_SUN2:
1259 /* Type 2 keyboards don't support keyclick */
1260 break;
1261
1262 case KB_SUN3:
1263 /* Type 3 keyboards come up with keyclick on */
1264 if (!ks->kbd_click) {
1265 /* turn off the click */
1266 kbd_output(k, KBD_CMD_NOCLICK);
1267 kbd_start_tx(k);
1268 }
1269 break;
1270
1271 case KB_SUN4:
1272 /* Type 4 keyboards come up with keyclick off */
1273 if (ks->kbd_click) {
1274 /* turn on the click */
1275 kbd_output(k, KBD_CMD_CLICK);
1276 kbd_start_tx(k);
1277 }
1278 break;
1279 }
1280
1281 /* LEDs are off after reset. */
1282 ks->kbd_leds = 0;
1283 }
1284
1285 /*
1286 * Called by kbd_input_raw, at spltty()
1287 */
1288 static void
1289 kbd_new_layout(k)
1290 struct kbd_softc *k;
1291 {
1292 struct kbd_state *ks = &k->k_state;
1293
1294 /*
1295 * On first identification, wake up anyone waiting for type
1296 * and set up the table pointers.
1297 */
1298 wakeup((caddr_t)&ks->kbd_layout);
1299
1300 /* XXX: switch decoding tables? */
1301 }
1302
1303
1304 /*
1305 * Wait for output to finish.
1306 * Called at spltty(). Has user context.
1307 */
1308 static int
1309 kbd_drain_tx(k)
1310 struct kbd_softc *k;
1311 {
1312 int error;
1313
1314 error = 0;
1315
1316 while (k->k_txflags & K_TXBUSY) {
1317 k->k_txflags |= K_TXWANT;
1318 error = tsleep((caddr_t)&k->k_txflags,
1319 PZERO | PCATCH, "kbdout", 0);
1320 }
1321
1322 return (error);
1323 }
1324
1325 /*
1326 * Enqueue some output for the keyboard
1327 * Called at spltty().
1328 */
1329 static void
1330 kbd_output(k, c)
1331 struct kbd_softc *k;
1332 int c; /* the data */
1333 {
1334 int put;
1335
1336 put = k->k_tbput;
1337 k->k_tbuf[put] = (u_char)c;
1338 put = (put + 1) & KBD_TX_RING_MASK;
1339
1340 /* Would overrun if increment makes (put==get). */
1341 if (put == k->k_tbget) {
1342 log(LOG_WARNING, "%s: output overrun\n",
1343 k->k_dev.dv_xname);
1344 } else {
1345 /* OK, really increment. */
1346 k->k_tbput = put;
1347 }
1348 }
1349
1350 /*
1351 * Start the sending data from the output queue
1352 * Called at spltty().
1353 */
1354 static void
1355 kbd_start_tx(k)
1356 struct kbd_softc *k;
1357 {
1358 struct zs_chanstate *cs = k->k_cs;
1359 int get, s;
1360 u_char c;
1361
1362 if (k->k_txflags & K_TXBUSY)
1363 return;
1364
1365 /* Is there anything to send? */
1366 get = k->k_tbget;
1367 if (get == k->k_tbput) {
1368 /* Nothing to send. Wake drain waiters. */
1369 if (k->k_txflags & K_TXWANT) {
1370 k->k_txflags &= ~K_TXWANT;
1371 wakeup((caddr_t)&k->k_txflags);
1372 }
1373 return;
1374 }
1375
1376 /* Have something to send. */
1377 c = k->k_tbuf[get];
1378 get = (get + 1) & KBD_TX_RING_MASK;
1379 k->k_tbget = get;
1380 k->k_txflags |= K_TXBUSY;
1381
1382 /* Need splzs to avoid interruption of the delay. */
1383 s = splzs();
1384 zs_write_data(cs, c);
1385 splx(s);
1386 }
1387
1388 /*
1389 * Called at spltty by:
1390 * kbd_update_leds, kbd_iocsled
1391 */
1392 static void
1393 kbd_set_leds(k, new_leds)
1394 struct kbd_softc *k;
1395 int new_leds;
1396 {
1397 struct kbd_state *ks = &k->k_state;
1398
1399 /* Don't send unless state changes. */
1400 if (ks->kbd_leds == new_leds)
1401 return;
1402
1403 ks->kbd_leds = new_leds;
1404
1405 /* Only type 4 and later has LEDs anyway. */
1406 if (ks->kbd_id < 4)
1407 return;
1408
1409 kbd_output(k, KBD_CMD_SETLED);
1410 kbd_output(k, new_leds);
1411 kbd_start_tx(k);
1412 }
1413
1414 /*
1415 * Called at spltty by:
1416 * kbd_input_keysym
1417 */
1418 static void
1419 kbd_update_leds(k)
1420 struct kbd_softc *k;
1421 {
1422 struct kbd_state *ks = &k->k_state;
1423 register char leds;
1424
1425 leds = ks->kbd_leds;
1426 leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
1427
1428 if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
1429 leds |= LED_CAPS_LOCK;
1430 if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
1431 leds |= LED_NUM_LOCK;
1432
1433 kbd_set_leds(k, leds);
1434 }
1435
1436