kbd.c revision 1.70 1 /* $NetBSD: kbd.c,v 1.70 2019/11/10 21:16:37 chs 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. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
41 */
42
43 /*
44 * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
45 * [yet?]). Translates incoming bytes to ASCII or to `firm_events' and
46 * passes them up to the appropriate reader.
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.70 2019/11/10 21:16:37 chs Exp $");
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 #include <sys/ioctl.h>
57 #include <sys/kernel.h>
58 #include <sys/proc.h>
59 #include <sys/malloc.h>
60 #include <sys/signal.h>
61 #include <sys/signalvar.h>
62 #include <sys/time.h>
63 #include <sys/syslog.h>
64 #include <sys/select.h>
65 #include <sys/poll.h>
66 #include <sys/file.h>
67
68 #include <dev/sysmon/sysmon_taskq.h>
69
70 #include <dev/wscons/wsksymdef.h>
71
72 #include <dev/sun/kbd_reg.h>
73 #include <dev/sun/kbio.h>
74 #include <dev/sun/vuid_event.h>
75 #include <dev/sun/event_var.h>
76 #include <dev/sun/kbd_xlate.h>
77 #include <dev/sun/kbdvar.h>
78
79 #include "ioconf.h"
80 #include "locators.h"
81 #include "opt_sunkbd.h"
82 #include "sysmon_envsys.h"
83
84 dev_type_open(kbdopen);
85 dev_type_close(kbdclose);
86 dev_type_read(kbdread);
87 dev_type_ioctl(kbdioctl);
88 dev_type_poll(kbdpoll);
89 dev_type_kqfilter(kbdkqfilter);
90
91 const struct cdevsw kbd_cdevsw = {
92 .d_open = kbdopen,
93 .d_close = kbdclose,
94 .d_read = kbdread,
95 .d_write = nowrite,
96 .d_ioctl = kbdioctl,
97 .d_stop = nostop,
98 .d_tty = notty,
99 .d_poll = kbdpoll,
100 .d_mmap = nommap,
101 .d_kqfilter = kbdkqfilter,
102 .d_discard = nodiscard,
103 .d_flag = D_OTHER
104 };
105
106 #if NWSKBD > 0
107 static int wssunkbd_enable(void *, int);
108 static void wssunkbd_set_leds(void *, int);
109 static int wssunkbd_ioctl(void *, u_long, void *, int, struct lwp *);
110 static void sunkbd_wskbd_cngetc(void *, u_int *, int *);
111 static void sunkbd_wskbd_cnpollc(void *, int);
112 static void sunkbd_wskbd_cnbell(void *, u_int, u_int, u_int);
113 static void sunkbd_bell_off(void *v);
114 static void kbd_enable(device_t); /* deferred keyboard init */
115
116 const struct wskbd_accessops sunkbd_wskbd_accessops = {
117 wssunkbd_enable,
118 wssunkbd_set_leds,
119 wssunkbd_ioctl,
120 };
121
122 extern const struct wscons_keydesc wssun_keydesctab[];
123 const struct wskbd_mapdata sunkbd_wskbd_keymapdata = {
124 wssun_keydesctab,
125 #ifdef SUNKBD_LAYOUT
126 SUNKBD_LAYOUT,
127 #else
128 KB_US,
129 #endif
130 };
131
132 const struct wskbd_consops sunkbd_wskbd_consops = {
133 sunkbd_wskbd_cngetc,
134 sunkbd_wskbd_cnpollc,
135 sunkbd_wskbd_cnbell,
136 };
137
138 void kbd_wskbd_attach(struct kbd_softc *, int);
139 #endif
140
141 /* ioctl helpers */
142 static int kbd_iockeymap(struct kbd_state *, u_long, struct kiockeymap *);
143 #ifdef KIOCGETKEY
144 static int kbd_oldkeymap(struct kbd_state *, u_long, struct okiockey *);
145 #endif
146
147
148 /* callbacks for console driver */
149 static int kbd_cc_open(struct cons_channel *);
150 static int kbd_cc_close(struct cons_channel *);
151
152 /* console input */
153 static void kbd_input_console(struct kbd_softc *, int);
154 static void kbd_repeat(void *);
155 static int kbd_input_keysym(struct kbd_softc *, int);
156 static void kbd_input_string(struct kbd_softc *, char *);
157 static void kbd_input_funckey(struct kbd_softc *, int);
158 static void kbd_update_leds(struct kbd_softc *);
159
160 #if NWSKBD > 0
161 static void kbd_input_wskbd(struct kbd_softc *, int);
162 #endif
163
164 /* firm events input */
165 static void kbd_input_event(struct kbd_softc *, int);
166
167 /****************************************************************
168 * Entry points for /dev/kbd
169 * (open,close,read,write,...)
170 ****************************************************************/
171
172 /*
173 * Open:
174 * Check exclusion, open actual device (_iopen),
175 * setup event channel, clear ASCII repeat stuff.
176 */
177 int
178 kbdopen(dev_t dev, int flags, int mode, struct lwp *l)
179 {
180 struct kbd_softc *k;
181 int error;
182
183 /* locate device */
184 k = device_lookup_private(&kbd_cd, minor(dev));
185 if (k == NULL)
186 return ENXIO;
187
188 #if NWSKBD > 0
189 /*
190 * NB: wscons support: while we can track if wskbd has called
191 * enable(), we can't tell if that's for console input or for
192 * events input, so we should probably just let the open to
193 * always succeed regardless (e.g. Xsun opening /dev/kbd).
194 */
195 if (!k->k_wsenabled)
196 wssunkbd_enable(k, 1);
197 #endif
198
199 /* exclusive open required for /dev/kbd */
200 if (k->k_events.ev_io)
201 return EBUSY;
202 k->k_events.ev_io = l->l_proc;
203
204 /* stop pending autorepeat of console input */
205 if (k->k_repeating) {
206 k->k_repeating = 0;
207 callout_stop(&k->k_repeat_ch);
208 }
209
210 /* open actual underlying device */
211 if (k->k_ops != NULL && k->k_ops->open != NULL)
212 if ((error = (*k->k_ops->open)(k)) != 0) {
213 k->k_events.ev_io = NULL;
214 return error;
215 }
216
217 ev_init(&k->k_events);
218 k->k_evmode = 0; /* XXX: OK? */
219
220 return 0;
221 }
222
223
224 /*
225 * Close:
226 * Turn off event mode, dump the queue, and close the keyboard
227 * unless it is supplying console input.
228 */
229 int
230 kbdclose(dev_t dev, int flags, int mode, struct lwp *l)
231 {
232 struct kbd_softc *k;
233
234 k = device_lookup_private(&kbd_cd, minor(dev));
235 k->k_evmode = 0;
236 ev_fini(&k->k_events);
237 k->k_events.ev_io = NULL;
238
239 if (k->k_ops != NULL && k->k_ops->close != NULL) {
240 int error;
241 if ((error = (*k->k_ops->close)(k)) != 0)
242 return error;
243 }
244 return 0;
245 }
246
247
248 int
249 kbdread(dev_t dev, struct uio *uio, int flags)
250 {
251 struct kbd_softc *k;
252
253 k = device_lookup_private(&kbd_cd, minor(dev));
254 return ev_read(&k->k_events, uio, flags);
255 }
256
257
258 int
259 kbdpoll(dev_t dev, int events, struct lwp *l)
260 {
261 struct kbd_softc *k;
262
263 k = device_lookup_private(&kbd_cd, minor(dev));
264 return ev_poll(&k->k_events, events, l);
265 }
266
267 int
268 kbdkqfilter(dev_t dev, struct knote *kn)
269 {
270 struct kbd_softc *k;
271
272 k = device_lookup_private(&kbd_cd, minor(dev));
273 return ev_kqfilter(&k->k_events, kn);
274 }
275
276 int
277 kbdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
278 {
279 struct kbd_softc *k;
280 struct kbd_state *ks;
281 int error = 0;
282
283 k = device_lookup_private(&kbd_cd, minor(dev));
284 ks = &k->k_state;
285
286 switch (cmd) {
287
288 case KIOCTRANS: /* Set translation mode */
289 /* We only support "raw" mode on /dev/kbd */
290 if (*(int *)data != TR_UNTRANS_EVENT)
291 error = EINVAL;
292 break;
293
294 case KIOCGTRANS: /* Get translation mode */
295 /* We only support "raw" mode on /dev/kbd */
296 *(int *)data = TR_UNTRANS_EVENT;
297 break;
298
299 #ifdef KIOCGETKEY
300 case KIOCGETKEY: /* Get keymap entry (old format) */
301 error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
302 break;
303 #endif /* KIOCGETKEY */
304
305 case KIOCSKEY: /* Set keymap entry */
306 /* FALLTHROUGH */
307 case KIOCGKEY: /* Get keymap entry */
308 error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
309 break;
310
311 case KIOCCMD: /* Send a command to the keyboard */
312 /* pass it to the middle layer */
313 if (k->k_ops != NULL && k->k_ops->docmd != NULL)
314 error = (*k->k_ops->docmd)(k, *(int *)data, 1);
315 break;
316
317 case KIOCTYPE: /* Get keyboard type */
318 *(int *)data = ks->kbd_id;
319 break;
320
321 case KIOCSDIRECT: /* Where to send input */
322 k->k_evmode = *(int *)data;
323 break;
324
325 case KIOCLAYOUT: /* Get keyboard layout */
326 *(int *)data = ks->kbd_layout;
327 break;
328
329 case KIOCSLED: /* Set keyboard LEDs */
330 /* pass the request to the middle layer */
331 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
332 error = (*k->k_ops->setleds)(k, *(char *)data, 1);
333 break;
334
335 case KIOCGLED: /* Get keyboard LEDs */
336 *(char *)data = ks->kbd_leds;
337 break;
338
339 case FIONBIO: /* we will remove this someday (soon???) */
340 break;
341
342 case FIOASYNC:
343 k->k_events.ev_async = (*(int *)data != 0);
344 break;
345
346 case FIOSETOWN:
347 if (-*(int *)data != k->k_events.ev_io->p_pgid
348 && *(int *)data != k->k_events.ev_io->p_pid)
349 error = EPERM;
350 break;
351
352 case TIOCSPGRP:
353 if (*(int *)data != k->k_events.ev_io->p_pgid)
354 error = EPERM;
355 break;
356
357 default:
358 error = ENOTTY;
359 break;
360 }
361
362 return error;
363 }
364
365
366 /****************************************************************
367 * ioctl helpers
368 ****************************************************************/
369
370 /*
371 * Get/Set keymap entry
372 */
373 static int
374 kbd_iockeymap(struct kbd_state *ks, u_long cmd, struct kiockeymap *kio)
375 {
376 u_short *km;
377 u_int station;
378
379 switch (kio->kio_tablemask) {
380 case KIOC_NOMASK:
381 km = ks->kbd_k.k_normal;
382 break;
383 case KIOC_SHIFTMASK:
384 km = ks->kbd_k.k_shifted;
385 break;
386 case KIOC_CTRLMASK:
387 km = ks->kbd_k.k_control;
388 break;
389 case KIOC_UPMASK:
390 km = ks->kbd_k.k_release;
391 break;
392 default:
393 /* Silently ignore unsupported masks */
394 return 0;
395 }
396
397 /* Range-check the table position. */
398 station = kio->kio_station;
399 if (station >= KEYMAP_SIZE)
400 return EINVAL;
401
402 switch (cmd) {
403
404 case KIOCGKEY: /* Get keymap entry */
405 kio->kio_entry = km[station];
406 break;
407
408 case KIOCSKEY: /* Set keymap entry */
409 km[station] = kio->kio_entry;
410 break;
411
412 default:
413 return ENOTTY;
414 }
415 return 0;
416 }
417
418
419 #ifdef KIOCGETKEY
420 /*
421 * Get/Set keymap entry,
422 * old format (compatibility)
423 */
424 int
425 kbd_oldkeymap(struct kbd_state *ks, u_long cmd, struct okiockey *kio)
426 {
427 int error = 0;
428
429 switch (cmd) {
430
431 case KIOCGETKEY:
432 if (kio->kio_station == 118) {
433 /*
434 * This is X11 asking if a type 3 keyboard is
435 * really a type 3 keyboard. Say yes, it is,
436 * by reporting key station 118 as a "hole".
437 * Note old (SunOS 3.5) definition of HOLE!
438 */
439 kio->kio_entry = 0xA2;
440 break;
441 }
442 /* fall through */
443
444 default:
445 error = ENOTTY;
446 break;
447 }
448
449 return error;
450 }
451 #endif /* KIOCGETKEY */
452
453
454
455 /****************************************************************
456 * Keyboard input - called by middle layer at spltty().
457 ****************************************************************/
458
459 void
460 kbd_input(struct kbd_softc *k, int code)
461 {
462 if (k->k_evmode) {
463 /*
464 * XXX: is this still true?
465 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
466 * This is bad as it means the server will not automatically resync
467 * on all-up IDLEs, but I did not drop them before, and the server
468 * goes crazy when it comes time to blank the screen....
469 */
470 if (code == KBD_IDLE)
471 return;
472
473 /*
474 * Keyboard is generating firm events. Turn this keystroke
475 * into an event and put it in the queue.
476 */
477 kbd_input_event(k, code);
478 return;
479 }
480
481 #if NWSKBD > 0
482 if (k->k_wskbd != NULL && k->k_wsenabled) {
483 /*
484 * We are using wskbd input mode, pass the event up.
485 */
486 if (code == KBD_IDLE)
487 return; /* this key is not in the mapped */
488 kbd_input_wskbd(k, code);
489 return;
490 }
491 #endif
492
493 /*
494 * If /dev/kbd is not connected in event mode, or wskbd mode,
495 * translate and send upstream (to console).
496 */
497 kbd_input_console(k, code);
498 }
499
500
501
502 /****************************************************************
503 * Open/close routines called upon opening /dev/console
504 * if we serve console input.
505 ****************************************************************/
506
507 struct cons_channel *
508 kbd_cc_alloc(struct kbd_softc *k)
509 {
510 struct cons_channel *cc;
511
512 cc = malloc(sizeof *cc, M_DEVBUF, M_WAITOK);
513
514 /* our callbacks for the console driver */
515 cc->cc_private = k;
516 cc->cc_iopen = kbd_cc_open;
517 cc->cc_iclose = kbd_cc_close;
518
519 /* will be provided by the console driver so that we can feed input */
520 cc->cc_upstream = NULL;
521
522 /*
523 * TODO: clean up cons_attach_input() vs kd_attach_input() in
524 * lower layers and move that code here.
525 */
526
527 k->k_cc = cc;
528 return cc;
529 }
530
531
532 static int
533 kbd_cc_open(struct cons_channel *cc)
534 {
535 struct kbd_softc *k;
536 int ret;
537
538 if (cc == NULL)
539 return 0;
540
541 k = cc->cc_private;
542 if (k == NULL)
543 return 0;
544
545 if (k->k_ops != NULL && k->k_ops->open != NULL)
546 ret = (*k->k_ops->open)(k);
547 else
548 ret = 0;
549
550 /* XXX: verify that callout is not active? */
551 k->k_repeat_start = hz/2;
552 k->k_repeat_step = hz/20;
553 callout_init(&k->k_repeat_ch, 0);
554
555 return ret;
556 }
557
558
559 static int
560 kbd_cc_close(struct cons_channel *cc)
561 {
562 struct kbd_softc *k;
563 int ret;
564
565 if (cc == NULL)
566 return 0;
567
568 k = cc->cc_private;
569 if (k == NULL)
570 return 0;
571
572 if (k->k_ops != NULL && k->k_ops->close != NULL)
573 ret = (*k->k_ops->close)(k);
574 else
575 ret = 0;
576
577 /* stop any pending auto-repeat */
578 if (k->k_repeating) {
579 k->k_repeating = 0;
580 callout_stop(&k->k_repeat_ch);
581 }
582
583 return ret;
584 }
585
586
587
588 /****************************************************************
589 * Console input - called by middle layer at spltty().
590 ****************************************************************/
591
592 static void
593 kbd_input_console(struct kbd_softc *k, int code)
594 {
595 struct kbd_state *ks= &k->k_state;
596 int keysym;
597
598 /* any input stops auto-repeat (i.e. key release) */
599 if (k->k_repeating) {
600 k->k_repeating = 0;
601 callout_stop(&k->k_repeat_ch);
602 }
603
604 keysym = kbd_code_to_keysym(ks, code);
605
606 /* pass to console */
607 if (kbd_input_keysym(k, keysym)) {
608 log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
609 " produced unexpected keysym 0x%x\n",
610 device_xname(k->k_dev),
611 code, ks->kbd_modbits, keysym);
612 return; /* no point in auto-repeat here */
613 }
614
615 if (KEYSYM_NOREPEAT(keysym))
616 return;
617
618 /* setup for auto-repeat after initial delay */
619 k->k_repeating = 1;
620 k->k_repeatsym = keysym;
621 callout_reset(&k->k_repeat_ch, k->k_repeat_start,
622 kbd_repeat, k);
623 }
624
625
626 /*
627 * This is the autorepeat callout function scheduled by kbd_input() above.
628 * Called at splsoftclock().
629 */
630 static void
631 kbd_repeat(void *arg)
632 {
633 struct kbd_softc *k = arg;
634 int s;
635
636 s = spltty();
637 if (k->k_repeating && k->k_repeatsym >= 0) {
638 /* feed typematic keysym to the console */
639 (void)kbd_input_keysym(k, k->k_repeatsym);
640
641 /* reschedule next repeat */
642 callout_reset(&k->k_repeat_ch, k->k_repeat_step,
643 kbd_repeat, k);
644 }
645 splx(s);
646 }
647
648
649
650 /*
651 * Supply keysym as console input. Convert keysym to character(s) and
652 * pass them up to cons_channel's upstream hook.
653 *
654 * Return zero on success, else the keysym that we could not handle
655 * (so that the caller may complain).
656 */
657 static int
658 kbd_input_keysym(struct kbd_softc *k, int keysym)
659 {
660 struct kbd_state *ks = &k->k_state;
661 int data;
662 /* Check if a recipient has been configured */
663 if (k->k_cc == NULL || k->k_cc->cc_upstream == NULL)
664 return 0;
665
666 switch (KEYSYM_CLASS(keysym)) {
667
668 case KEYSYM_ASCII:
669 data = KEYSYM_DATA(keysym);
670 if (ks->kbd_modbits & KBMOD_META_MASK)
671 data |= 0x80;
672 (*k->k_cc->cc_upstream)(data);
673 break;
674
675 case KEYSYM_STRING:
676 data = keysym & 0xF;
677 kbd_input_string(k, kbd_stringtab[data]);
678 break;
679
680 case KEYSYM_FUNC:
681 kbd_input_funckey(k, keysym);
682 break;
683
684 case KEYSYM_CLRMOD:
685 data = 1 << (keysym & 0x1F);
686 ks->kbd_modbits &= ~data;
687 break;
688
689 case KEYSYM_SETMOD:
690 data = 1 << (keysym & 0x1F);
691 ks->kbd_modbits |= data;
692 break;
693
694 case KEYSYM_INVMOD:
695 data = 1 << (keysym & 0x1F);
696 ks->kbd_modbits ^= data;
697 kbd_update_leds(k);
698 break;
699
700 case KEYSYM_ALL_UP:
701 ks->kbd_modbits &= ~0xFFFF;
702 break;
703
704 case KEYSYM_SPECIAL:
705 if (keysym == KEYSYM_NOP)
706 break;
707 /* FALLTHROUGH */
708 default:
709 /* We could not handle it. */
710 return keysym;
711 }
712
713 return 0;
714 }
715
716
717 /*
718 * Send string upstream.
719 */
720 static void
721 kbd_input_string(struct kbd_softc *k, char *str)
722 {
723
724 while (*str) {
725 (*k->k_cc->cc_upstream)(*str);
726 ++str;
727 }
728 }
729
730
731 /*
732 * Format the F-key sequence and send as a string.
733 * XXX: Ugly compatibility mappings.
734 */
735 static void
736 kbd_input_funckey(struct kbd_softc *k, int keysym)
737 {
738 int n;
739 char str[12];
740
741 n = 0xC0 + (keysym & 0x3F);
742 snprintf(str, sizeof(str), "\033[%dz", n);
743 kbd_input_string(k, str);
744 }
745
746
747 /*
748 * Update LEDs to reflect console input state.
749 */
750 static void
751 kbd_update_leds(struct kbd_softc *k)
752 {
753 struct kbd_state *ks = &k->k_state;
754 char leds;
755
756 leds = ks->kbd_leds;
757 leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
758
759 if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
760 leds |= LED_CAPS_LOCK;
761 if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
762 leds |= LED_NUM_LOCK;
763
764 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
765 (void)(*k->k_ops->setleds)(k, leds, 0);
766 }
767
768
769
770 /****************************************************************
771 * Events input - called by middle layer at spltty().
772 ****************************************************************/
773
774 /*
775 * Supply raw keystrokes when keyboard is open in firm event mode.
776 *
777 * Turn the keystroke into an event and put it in the queue.
778 * If the queue is full, the keystroke is lost (sorry!).
779 */
780 static void
781 kbd_input_event(struct kbd_softc *k, int code)
782 {
783 struct firm_event *fe;
784 int put;
785
786 #ifdef DIAGNOSTIC
787 if (!k->k_evmode) {
788 printf("%s: kbd_input_event called when not in event mode\n",
789 device_xname(k->k_dev));
790 return;
791 }
792 #endif
793 put = k->k_events.ev_put;
794 fe = &k->k_events.ev_q[put];
795 put = (put + 1) % EV_QSIZE;
796 if (put == k->k_events.ev_get) {
797 log(LOG_WARNING, "%s: event queue overflow\n",
798 device_xname(k->k_dev));
799 return;
800 }
801
802 fe->id = KEY_CODE(code);
803 fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
804 firm_gettime(fe);
805 k->k_events.ev_put = put;
806 EV_WAKEUP(&k->k_events);
807 }
808
809
810
811 /****************************************************************
812 * Translation stuff declared in kbd_xlate.h
813 ****************************************************************/
814
815 /*
816 * Initialization - called by either lower layer attach or by kdcninit.
817 */
818 void
819 kbd_xlate_init(struct kbd_state *ks)
820 {
821 struct keyboard *ktbls;
822 int id;
823
824 id = ks->kbd_id;
825 if (id < KBD_MIN_TYPE)
826 id = KBD_MIN_TYPE;
827 if (id > kbd_max_type)
828 id = kbd_max_type;
829 ktbls = keyboards[id];
830
831 ks->kbd_k = *ktbls; /* struct assignment */
832 ks->kbd_modbits = 0;
833 }
834
835 /*
836 * Turn keyboard up/down codes into a KEYSYM.
837 * Note that the "kd" driver (on sun3 and sparc64) uses this too!
838 */
839 int
840 kbd_code_to_keysym(struct kbd_state *ks, int c)
841 {
842 u_short *km;
843 int keysym;
844
845 /*
846 * Get keymap pointer. One of these:
847 * release, control, shifted, normal, ...
848 */
849 if (KEY_UP(c))
850 km = ks->kbd_k.k_release;
851 else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
852 km = ks->kbd_k.k_control;
853 else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
854 km = ks->kbd_k.k_shifted;
855 else
856 km = ks->kbd_k.k_normal;
857
858 if (km == NULL) {
859 /*
860 * Do not know how to translate yet.
861 * We will find out when a RESET comes along.
862 */
863 return KEYSYM_NOP;
864 }
865 keysym = km[KEY_CODE(c)];
866
867 /*
868 * Post-processing for Caps-lock
869 */
870 if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
871 (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
872 {
873 if (('a' <= keysym) && (keysym <= 'z'))
874 keysym -= ('a' - 'A');
875 }
876
877 /*
878 * Post-processing for Num-lock. All "function"
879 * keysyms get indirected through another table.
880 * (XXX: Only if numlock on. Want off also!)
881 */
882 if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
883 (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
884 {
885 keysym = kbd_numlock_map[keysym & 0x3F];
886 }
887
888 return keysym;
889 }
890
891
892 /*
893 * Back door for rcons (fb.c)
894 */
895 void
896 kbd_bell(int on)
897 {
898 struct kbd_softc *k;
899
900 k = device_lookup_private(&kbd_cd, 0); /* XXX: hardcoded minor */
901
902 if (k == NULL || k->k_ops == NULL || k->k_ops->docmd == NULL)
903 return;
904
905 (void)(*k->k_ops->docmd)(k, on ? KBD_CMD_BELL : KBD_CMD_NOBELL, 0);
906 }
907
908 #if NWSKBD > 0
909
910 #if NSYSMON_ENVSYS
911 static void
912 kbd_powerbutton(void *cookie)
913 {
914 struct kbd_softc *k = cookie;
915
916 sysmon_pswitch_event(&k->k_sm_pbutton, k->k_ev);
917 }
918 #endif
919
920 static void
921 kbd_input_wskbd(struct kbd_softc *k, int code)
922 {
923 int type, key;
924
925 #ifdef WSDISPLAY_COMPAT_RAWKBD
926 if (k->k_wsraw) {
927 u_char buf;
928
929 buf = code;
930 wskbd_rawinput(k->k_wskbd, &buf, 1);
931 return;
932 }
933 #endif
934
935 type = KEY_UP(code) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
936 key = KEY_CODE(code);
937
938 if (type == WSCONS_EVENT_KEY_DOWN) {
939 switch (key) {
940 #ifdef KBD_HIJACK_VOLUME_BUTTONS
941 case 0x02:
942 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
943 return;
944 case 0x04:
945 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
946 return;
947 #endif
948 case 0x30:
949 #if NSYSMON_ENVSYS
950 if (k->k_isconsole) {
951 k->k_ev = KEY_UP(code) ?
952 PSWITCH_EVENT_RELEASED :
953 PSWITCH_EVENT_PRESSED;
954 sysmon_task_queue_sched(0,
955 kbd_powerbutton, k);
956 }
957 #endif
958 return;
959 }
960 }
961
962 wskbd_input(k->k_wskbd, type, key);
963 }
964
965 int
966 wssunkbd_enable(void *v, int on)
967 {
968 struct kbd_softc *k = v;
969
970 if (k->k_wsenabled != on) {
971 k->k_wsenabled = on;
972 if (on) {
973 /* open actual underlying device */
974 if (k->k_ops != NULL && k->k_ops->open != NULL)
975 (*k->k_ops->open)(k);
976 ev_init(&k->k_events);
977 k->k_evmode = 0; /* XXX: OK? */
978 } else {
979 /* close underlying device */
980 if (k->k_ops != NULL && k->k_ops->close != NULL)
981 (*k->k_ops->close)(k);
982 }
983 }
984 return 0;
985 }
986
987 void
988 wssunkbd_set_leds(void *v, int leds)
989 {
990 struct kbd_softc *k = v;
991 int l = 0;
992
993 if (leds & WSKBD_LED_CAPS)
994 l |= LED_CAPS_LOCK;
995 if (leds & WSKBD_LED_NUM)
996 l |= LED_NUM_LOCK;
997 if (leds & WSKBD_LED_SCROLL)
998 l |= LED_SCROLL_LOCK;
999 if (leds & WSKBD_LED_COMPOSE)
1000 l |= LED_COMPOSE;
1001 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
1002 (*k->k_ops->setleds)(k, l, 0);
1003 k->k_leds=l;
1004 }
1005
1006 static int
1007 wssunkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
1008 {
1009 struct kbd_softc *k = v;
1010
1011 switch (cmd) {
1012 case WSKBDIO_GTYPE:
1013 /* we can't tell 4 from 5 or 6 */
1014 *(int *)data = k->k_state.kbd_id < KB_SUN4 ?
1015 WSKBD_TYPE_SUN : WSKBD_TYPE_SUN5;
1016 return 0;
1017 case WSKBDIO_SETLEDS:
1018 wssunkbd_set_leds(v, *(int *)data);
1019 return 0;
1020 case WSKBDIO_GETLEDS:
1021 *(int *)data = k->k_leds;
1022 return 0;
1023 #ifdef WSDISPLAY_COMPAT_RAWKBD
1024 case WSKBDIO_SETMODE:
1025 k->k_wsraw = *(int *)data == WSKBD_RAW;
1026 return 0;
1027 #endif
1028 }
1029 return EPASSTHROUGH;
1030 }
1031
1032 extern int prom_cngetc(dev_t);
1033
1034 static void
1035 sunkbd_wskbd_cngetc(void *v, u_int *type, int *data)
1036 {
1037 /* struct kbd_sun_softc *k = v; */
1038
1039 *data = prom_cngetc(0);
1040 *type = WSCONS_EVENT_ASCII;
1041 }
1042
1043 void
1044 sunkbd_wskbd_cnpollc(void *v, int on)
1045 {
1046 }
1047
1048 static void
1049 sunkbd_bell_off(void *v)
1050 {
1051 struct kbd_softc *k = v;
1052
1053 k->k_ops->docmd(k, KBD_CMD_NOBELL, 0);
1054 }
1055
1056 void
1057 sunkbd_wskbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
1058 {
1059 struct kbd_softc *k = v;
1060
1061 callout_reset(&k->k_wsbell, period * 1000 / hz, sunkbd_bell_off, v);
1062 k->k_ops->docmd(k, KBD_CMD_BELL, 0);
1063 }
1064
1065 void
1066 kbd_enable(device_t dev)
1067 {
1068 struct kbd_softc *k = device_private(dev);
1069 struct wskbddev_attach_args a;
1070
1071 if (k->k_isconsole)
1072 wskbd_cnattach(&sunkbd_wskbd_consops, k,
1073 &sunkbd_wskbd_keymapdata);
1074
1075 a.console = k->k_isconsole;
1076 a.keymap = &sunkbd_wskbd_keymapdata;
1077 a.accessops = &sunkbd_wskbd_accessops;
1078 a.accesscookie = k;
1079
1080 /* XXX why? */
1081 k->k_wsenabled = 0;
1082
1083 /* Attach the wskbd */
1084 k->k_wskbd = config_found(k->k_dev, &a, wskbddevprint);
1085
1086 callout_init(&k->k_wsbell, 0);
1087
1088 wssunkbd_enable(k,1);
1089
1090 wssunkbd_set_leds(k, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
1091 delay(100000);
1092 wssunkbd_set_leds(k, 0);
1093 }
1094
1095 void
1096 kbd_wskbd_attach(struct kbd_softc *k, int isconsole)
1097 {
1098 k->k_isconsole = isconsole;
1099 if (isconsole) {
1100 #if NSYSMON_ENVSYS
1101 sysmon_task_queue_init();
1102 memset(&k->k_sm_pbutton, 0, sizeof(struct sysmon_pswitch));
1103 k->k_sm_pbutton.smpsw_name = device_xname(k->k_dev);
1104 k->k_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
1105 if (sysmon_pswitch_register(&k->k_sm_pbutton) != 0)
1106 aprint_error_dev(k->k_dev,
1107 "unable to register power button with sysmon\n");
1108 #endif
1109 }
1110 config_interrupts(k->k_dev, kbd_enable);
1111 }
1112 #endif
1113