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