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