kbd.c revision 1.32 1 1.32 uwe /* $NetBSD: kbd.c,v 1.32 2002/10/03 16:13:25 uwe Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1992, 1993
5 1.1 gwr * The Regents of the University of California. All rights reserved.
6 1.1 gwr *
7 1.1 gwr * This software was developed by the Computer Systems Engineering group
8 1.1 gwr * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 gwr * contributed to Berkeley.
10 1.1 gwr *
11 1.1 gwr * All advertising materials mentioning features or use of this software
12 1.1 gwr * must display the following acknowledgement:
13 1.1 gwr * This product includes software developed by the University of
14 1.1 gwr * California, Lawrence Berkeley Laboratory.
15 1.1 gwr *
16 1.1 gwr * Redistribution and use in source and binary forms, with or without
17 1.1 gwr * modification, are permitted provided that the following conditions
18 1.1 gwr * are met:
19 1.1 gwr * 1. Redistributions of source code must retain the above copyright
20 1.1 gwr * notice, this list of conditions and the following disclaimer.
21 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 gwr * notice, this list of conditions and the following disclaimer in the
23 1.1 gwr * documentation and/or other materials provided with the distribution.
24 1.1 gwr * 3. All advertising materials mentioning features or use of this software
25 1.1 gwr * must display the following acknowledgement:
26 1.1 gwr * This product includes software developed by the University of
27 1.1 gwr * California, Berkeley and its contributors.
28 1.1 gwr * 4. Neither the name of the University nor the names of its contributors
29 1.1 gwr * may be used to endorse or promote products derived from this software
30 1.1 gwr * without specific prior written permission.
31 1.1 gwr *
32 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 gwr * SUCH DAMAGE.
43 1.1 gwr *
44 1.1 gwr * @(#)kbd.c 8.2 (Berkeley) 10/30/93
45 1.1 gwr */
46 1.1 gwr
47 1.1 gwr /*
48 1.1 gwr * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
49 1.1 gwr * [yet?]). Translates incoming bytes to ASCII or to `firm_events' and
50 1.1 gwr * passes them up to the appropriate reader.
51 1.1 gwr */
52 1.29 lukem
53 1.29 lukem #include <sys/cdefs.h>
54 1.32 uwe __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.32 2002/10/03 16:13:25 uwe Exp $");
55 1.1 gwr
56 1.1 gwr #include <sys/param.h>
57 1.1 gwr #include <sys/systm.h>
58 1.13 gwr #include <sys/conf.h>
59 1.1 gwr #include <sys/device.h>
60 1.1 gwr #include <sys/ioctl.h>
61 1.13 gwr #include <sys/kernel.h>
62 1.13 gwr #include <sys/proc.h>
63 1.13 gwr #include <sys/signal.h>
64 1.13 gwr #include <sys/signalvar.h>
65 1.1 gwr #include <sys/time.h>
66 1.1 gwr #include <sys/syslog.h>
67 1.9 mrg #include <sys/select.h>
68 1.9 mrg #include <sys/poll.h>
69 1.27 eeh #include <sys/file.h>
70 1.1 gwr
71 1.1 gwr #include <machine/vuid_event.h>
72 1.1 gwr #include <machine/kbd.h>
73 1.1 gwr #include <machine/kbio.h>
74 1.22 mrg #include <dev/sun/event_var.h>
75 1.22 mrg #include <dev/sun/kbd_xlate.h>
76 1.22 mrg #include <dev/sun/kbdvar.h>
77 1.1 gwr
78 1.14 jtk #include "locators.h"
79 1.1 gwr
80 1.31 gehenna extern struct cfdriver kbd_cd;
81 1.1 gwr
82 1.31 gehenna dev_type_open(kbdopen);
83 1.31 gehenna dev_type_close(kbdclose);
84 1.31 gehenna dev_type_read(kbdread);
85 1.31 gehenna dev_type_ioctl(kbdioctl);
86 1.31 gehenna dev_type_poll(kbdpoll);
87 1.31 gehenna
88 1.31 gehenna const struct cdevsw kbd_cdevsw = {
89 1.31 gehenna kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
90 1.31 gehenna nostop, notty, kbdpoll, nommap,
91 1.31 gehenna };
92 1.1 gwr
93 1.32 uwe
94 1.32 uwe /* ioctl helpers */
95 1.32 uwe static int kbd_iockeymap(struct kbd_state *ks,
96 1.32 uwe u_long cmd, struct kiockeymap *kio);
97 1.32 uwe #ifdef KIOCGETKEY
98 1.32 uwe static int kbd_oldkeymap(struct kbd_state *ks,
99 1.32 uwe u_long cmd, struct okiockey *okio);
100 1.32 uwe #endif
101 1.32 uwe
102 1.32 uwe /* console input helpers */
103 1.32 uwe static void kbd_input_string(struct kbd_softc *, char *);
104 1.32 uwe static void kbd_input_funckey(struct kbd_softc *, int);
105 1.32 uwe static void kbd_update_leds(struct kbd_softc *);
106 1.32 uwe
107 1.32 uwe
108 1.1 gwr /****************************************************************
109 1.1 gwr * Entry points for /dev/kbd
110 1.1 gwr * (open,close,read,write,...)
111 1.1 gwr ****************************************************************/
112 1.1 gwr
113 1.1 gwr /*
114 1.1 gwr * Open:
115 1.1 gwr * Check exclusion, open actual device (_iopen),
116 1.1 gwr * setup event channel, clear ASCII repeat stuff.
117 1.1 gwr */
118 1.1 gwr int
119 1.1 gwr kbdopen(dev, flags, mode, p)
120 1.1 gwr dev_t dev;
121 1.1 gwr int flags, mode;
122 1.1 gwr struct proc *p;
123 1.1 gwr {
124 1.1 gwr struct kbd_softc *k;
125 1.13 gwr int error, unit;
126 1.1 gwr
127 1.32 uwe /* locate device */
128 1.1 gwr unit = minor(dev);
129 1.5 thorpej if (unit >= kbd_cd.cd_ndevs)
130 1.1 gwr return (ENXIO);
131 1.5 thorpej k = kbd_cd.cd_devs[unit];
132 1.1 gwr if (k == NULL)
133 1.1 gwr return (ENXIO);
134 1.1 gwr
135 1.32 uwe /* exclusive open required for /dev/kbd */
136 1.1 gwr if (k->k_events.ev_io)
137 1.1 gwr return (EBUSY);
138 1.1 gwr k->k_events.ev_io = p;
139 1.1 gwr
140 1.32 uwe /* open actual underlying device */
141 1.32 uwe if (k->k_ops != NULL && k->k_ops->open != NULL)
142 1.32 uwe if ((error = (*k->k_ops->open)(k)) != 0) {
143 1.32 uwe k->k_events.ev_io = NULL;
144 1.32 uwe return (error);
145 1.32 uwe }
146 1.32 uwe
147 1.1 gwr ev_init(&k->k_events);
148 1.27 eeh k->k_evmode = 0; /* XXX: OK? */
149 1.1 gwr
150 1.1 gwr return (0);
151 1.1 gwr }
152 1.1 gwr
153 1.32 uwe
154 1.1 gwr /*
155 1.1 gwr * Close:
156 1.1 gwr * Turn off event mode, dump the queue, and close the keyboard
157 1.1 gwr * unless it is supplying console input.
158 1.1 gwr */
159 1.1 gwr int
160 1.1 gwr kbdclose(dev, flags, mode, p)
161 1.1 gwr dev_t dev;
162 1.1 gwr int flags, mode;
163 1.1 gwr struct proc *p;
164 1.1 gwr {
165 1.1 gwr struct kbd_softc *k;
166 1.1 gwr
167 1.5 thorpej k = kbd_cd.cd_devs[minor(dev)];
168 1.1 gwr k->k_evmode = 0;
169 1.1 gwr ev_fini(&k->k_events);
170 1.1 gwr k->k_events.ev_io = NULL;
171 1.32 uwe
172 1.32 uwe if (k->k_ops != NULL && k->k_ops->close != NULL) {
173 1.32 uwe int error;
174 1.32 uwe if ((error = (*k->k_ops->close)(k)) != 0)
175 1.32 uwe return (error);
176 1.32 uwe }
177 1.1 gwr return (0);
178 1.1 gwr }
179 1.1 gwr
180 1.32 uwe
181 1.1 gwr int
182 1.1 gwr kbdread(dev, uio, flags)
183 1.1 gwr dev_t dev;
184 1.1 gwr struct uio *uio;
185 1.1 gwr int flags;
186 1.1 gwr {
187 1.1 gwr struct kbd_softc *k;
188 1.1 gwr
189 1.5 thorpej k = kbd_cd.cd_devs[minor(dev)];
190 1.1 gwr return (ev_read(&k->k_events, uio, flags));
191 1.1 gwr }
192 1.1 gwr
193 1.32 uwe
194 1.1 gwr int
195 1.9 mrg kbdpoll(dev, events, p)
196 1.1 gwr dev_t dev;
197 1.9 mrg int events;
198 1.1 gwr struct proc *p;
199 1.1 gwr {
200 1.1 gwr struct kbd_softc *k;
201 1.1 gwr
202 1.5 thorpej k = kbd_cd.cd_devs[minor(dev)];
203 1.9 mrg return (ev_poll(&k->k_events, events, p));
204 1.1 gwr }
205 1.1 gwr
206 1.1 gwr
207 1.1 gwr int
208 1.1 gwr kbdioctl(dev, cmd, data, flag, p)
209 1.1 gwr dev_t dev;
210 1.1 gwr u_long cmd;
211 1.23 pk caddr_t data;
212 1.1 gwr int flag;
213 1.1 gwr struct proc *p;
214 1.1 gwr {
215 1.1 gwr struct kbd_softc *k;
216 1.1 gwr struct kbd_state *ks;
217 1.1 gwr int error = 0;
218 1.1 gwr
219 1.5 thorpej k = kbd_cd.cd_devs[minor(dev)];
220 1.1 gwr ks = &k->k_state;
221 1.1 gwr
222 1.1 gwr switch (cmd) {
223 1.1 gwr
224 1.1 gwr case KIOCTRANS: /* Set translation mode */
225 1.1 gwr /* We only support "raw" mode on /dev/kbd */
226 1.16 gwr if (*(int *)data != TR_UNTRANS_EVENT)
227 1.1 gwr error = EINVAL;
228 1.1 gwr break;
229 1.1 gwr
230 1.1 gwr case KIOCGTRANS: /* Get translation mode */
231 1.1 gwr /* We only support "raw" mode on /dev/kbd */
232 1.16 gwr *(int *)data = TR_UNTRANS_EVENT;
233 1.1 gwr break;
234 1.1 gwr
235 1.32 uwe #ifdef KIOCGETKEY
236 1.1 gwr case KIOCGETKEY: /* Get keymap entry (old format) */
237 1.1 gwr error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
238 1.1 gwr break;
239 1.32 uwe #endif /* KIOCGETKEY */
240 1.1 gwr
241 1.1 gwr case KIOCSKEY: /* Set keymap entry */
242 1.32 uwe /* FALLTHROUGH */
243 1.1 gwr case KIOCGKEY: /* Get keymap entry */
244 1.1 gwr error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
245 1.1 gwr break;
246 1.1 gwr
247 1.32 uwe case KIOCCMD: /* Send a command to the keyboard */
248 1.32 uwe /* pass it to the middle layer */
249 1.32 uwe if (k->k_ops != NULL && k->k_ops->docmd != NULL)
250 1.32 uwe error = (*k->k_ops->docmd)(k, *(int *)data, 1);
251 1.1 gwr break;
252 1.1 gwr
253 1.32 uwe case KIOCTYPE: /* Get keyboard type */
254 1.16 gwr *(int *)data = ks->kbd_id;
255 1.1 gwr break;
256 1.1 gwr
257 1.32 uwe case KIOCSDIRECT: /* Where to send input */
258 1.16 gwr k->k_evmode = *(int *)data;
259 1.1 gwr break;
260 1.1 gwr
261 1.1 gwr case KIOCLAYOUT: /* Get keyboard layout */
262 1.16 gwr *(int *)data = ks->kbd_layout;
263 1.1 gwr break;
264 1.1 gwr
265 1.32 uwe case KIOCSLED: /* Set keyboard LEDs */
266 1.32 uwe /* pass the request to the middle layer */
267 1.32 uwe if (k->k_ops != NULL && k->k_ops->setleds != NULL)
268 1.32 uwe error = (*k->k_ops->setleds)(k, *(char *)data, 1);
269 1.1 gwr break;
270 1.1 gwr
271 1.32 uwe case KIOCGLED: /* Get keyboard LEDs */
272 1.1 gwr *(char *)data = ks->kbd_leds;
273 1.1 gwr break;
274 1.1 gwr
275 1.1 gwr case FIONBIO: /* we will remove this someday (soon???) */
276 1.1 gwr break;
277 1.1 gwr
278 1.1 gwr case FIOASYNC:
279 1.32 uwe k->k_events.ev_async = (*(int *)data != 0);
280 1.1 gwr break;
281 1.1 gwr
282 1.1 gwr case TIOCSPGRP:
283 1.16 gwr if (*(int *)data != k->k_events.ev_io->p_pgid)
284 1.1 gwr error = EPERM;
285 1.1 gwr break;
286 1.1 gwr
287 1.16 gwr default:
288 1.16 gwr error = ENOTTY;
289 1.16 gwr break;
290 1.1 gwr }
291 1.1 gwr
292 1.1 gwr return (error);
293 1.1 gwr }
294 1.1 gwr
295 1.32 uwe
296 1.1 gwr /****************************************************************
297 1.1 gwr * ioctl helpers
298 1.1 gwr ****************************************************************/
299 1.1 gwr
300 1.1 gwr /*
301 1.1 gwr * Get/Set keymap entry
302 1.1 gwr */
303 1.7 gwr static int
304 1.1 gwr kbd_iockeymap(ks, cmd, kio)
305 1.1 gwr struct kbd_state *ks;
306 1.1 gwr u_long cmd;
307 1.1 gwr struct kiockeymap *kio;
308 1.1 gwr {
309 1.13 gwr u_short *km;
310 1.1 gwr u_int station;
311 1.1 gwr
312 1.1 gwr switch (kio->kio_tablemask) {
313 1.1 gwr case KIOC_NOMASK:
314 1.1 gwr km = ks->kbd_k.k_normal;
315 1.1 gwr break;
316 1.1 gwr case KIOC_SHIFTMASK:
317 1.1 gwr km = ks->kbd_k.k_shifted;
318 1.1 gwr break;
319 1.1 gwr case KIOC_CTRLMASK:
320 1.1 gwr km = ks->kbd_k.k_control;
321 1.1 gwr break;
322 1.1 gwr case KIOC_UPMASK:
323 1.1 gwr km = ks->kbd_k.k_release;
324 1.1 gwr break;
325 1.1 gwr default:
326 1.1 gwr /* Silently ignore unsupported masks */
327 1.1 gwr return (0);
328 1.1 gwr }
329 1.1 gwr
330 1.1 gwr /* Range-check the table position. */
331 1.1 gwr station = kio->kio_station;
332 1.1 gwr if (station >= KEYMAP_SIZE)
333 1.1 gwr return (EINVAL);
334 1.1 gwr
335 1.1 gwr switch (cmd) {
336 1.1 gwr
337 1.1 gwr case KIOCGKEY: /* Get keymap entry */
338 1.13 gwr kio->kio_entry = km[station];
339 1.1 gwr break;
340 1.1 gwr
341 1.1 gwr case KIOCSKEY: /* Set keymap entry */
342 1.13 gwr km[station] = kio->kio_entry;
343 1.1 gwr break;
344 1.1 gwr
345 1.1 gwr default:
346 1.1 gwr return(ENOTTY);
347 1.1 gwr }
348 1.1 gwr return (0);
349 1.1 gwr }
350 1.1 gwr
351 1.32 uwe
352 1.32 uwe #ifdef KIOCGETKEY
353 1.1 gwr /*
354 1.1 gwr * Get/Set keymap entry,
355 1.1 gwr * old format (compatibility)
356 1.1 gwr */
357 1.1 gwr int
358 1.1 gwr kbd_oldkeymap(ks, cmd, kio)
359 1.1 gwr struct kbd_state *ks;
360 1.1 gwr u_long cmd;
361 1.1 gwr struct okiockey *kio;
362 1.1 gwr {
363 1.1 gwr int error = 0;
364 1.1 gwr
365 1.1 gwr switch (cmd) {
366 1.1 gwr
367 1.1 gwr case KIOCGETKEY:
368 1.1 gwr if (kio->kio_station == 118) {
369 1.1 gwr /*
370 1.1 gwr * This is X11 asking if a type 3 keyboard is
371 1.1 gwr * really a type 3 keyboard. Say yes, it is,
372 1.1 gwr * by reporting key station 118 as a "hole".
373 1.1 gwr * Note old (SunOS 3.5) definition of HOLE!
374 1.1 gwr */
375 1.1 gwr kio->kio_entry = 0xA2;
376 1.1 gwr break;
377 1.1 gwr }
378 1.1 gwr /* fall through */
379 1.1 gwr
380 1.1 gwr default:
381 1.1 gwr error = ENOTTY;
382 1.1 gwr break;
383 1.1 gwr }
384 1.1 gwr
385 1.1 gwr return (error);
386 1.1 gwr }
387 1.32 uwe #endif /* KIOCGETKEY */
388 1.1 gwr
389 1.7 gwr
390 1.32 uwe /****************************************************************
391 1.32 uwe * Callbacks we supply to console driver
392 1.32 uwe ****************************************************************/
393 1.32 uwe
394 1.7 gwr /*
395 1.32 uwe * Open/close routines called upon opening /dev/console
396 1.32 uwe * if we serve console input.
397 1.7 gwr */
398 1.15 gwr int
399 1.32 uwe kbd_cc_open(cc)
400 1.32 uwe struct cons_channel *cc;
401 1.15 gwr {
402 1.7 gwr struct kbd_softc *k;
403 1.15 gwr
404 1.32 uwe if (cc == NULL)
405 1.32 uwe return (0);
406 1.7 gwr
407 1.32 uwe k = (struct kbd_softc *)cc->cc_dev;
408 1.32 uwe if (k != NULL && k->k_ops != NULL && k->k_ops->open != NULL)
409 1.32 uwe return ((*k->k_ops->open)(k));
410 1.32 uwe else
411 1.7 gwr return (0);
412 1.32 uwe }
413 1.7 gwr
414 1.7 gwr
415 1.32 uwe int
416 1.32 uwe kbd_cc_close(cc)
417 1.32 uwe struct cons_channel *cc;
418 1.32 uwe {
419 1.7 gwr struct kbd_softc *k;
420 1.7 gwr
421 1.32 uwe if (cc == NULL)
422 1.32 uwe return (0);
423 1.7 gwr
424 1.32 uwe k = (struct kbd_softc *)cc->cc_dev;
425 1.32 uwe if (k != NULL && k->k_ops != NULL && k->k_ops->close != NULL)
426 1.32 uwe return ((*k->k_ops->close)(k));
427 1.32 uwe else
428 1.32 uwe return (0);
429 1.7 gwr }
430 1.7 gwr
431 1.7 gwr
432 1.1 gwr /****************************************************************
433 1.32 uwe * Console input - called by middle layer at spltty().
434 1.1 gwr ****************************************************************/
435 1.1 gwr
436 1.1 gwr /*
437 1.32 uwe * Entry point for the middle layer to supply keysym as console input.
438 1.32 uwe * Convert keysym to character(s) and pass them up to cons_channel's
439 1.32 uwe * upstream hook.
440 1.17 gwr *
441 1.32 uwe * Return zero on success, else the keysym that we could not handle
442 1.32 uwe * (so that the caller may complain).
443 1.1 gwr */
444 1.17 gwr int
445 1.1 gwr kbd_input_keysym(k, keysym)
446 1.1 gwr struct kbd_softc *k;
447 1.23 pk int keysym;
448 1.1 gwr {
449 1.1 gwr struct kbd_state *ks = &k->k_state;
450 1.23 pk int data;
451 1.30 pk
452 1.30 pk /* Check if a recipient has been configured */
453 1.32 uwe if (k->k_cc == NULL || k->k_cc->cc_upstream == NULL)
454 1.30 pk return (0);
455 1.1 gwr
456 1.4 gwr switch (KEYSYM_CLASS(keysym)) {
457 1.1 gwr
458 1.1 gwr case KEYSYM_ASCII:
459 1.1 gwr data = KEYSYM_DATA(keysym);
460 1.1 gwr if (ks->kbd_modbits & KBMOD_META_MASK)
461 1.1 gwr data |= 0x80;
462 1.23 pk (*k->k_cc->cc_upstream)(data);
463 1.1 gwr break;
464 1.1 gwr
465 1.1 gwr case KEYSYM_STRING:
466 1.1 gwr data = keysym & 0xF;
467 1.1 gwr kbd_input_string(k, kbd_stringtab[data]);
468 1.1 gwr break;
469 1.1 gwr
470 1.1 gwr case KEYSYM_FUNC:
471 1.1 gwr kbd_input_funckey(k, keysym);
472 1.1 gwr break;
473 1.1 gwr
474 1.1 gwr case KEYSYM_CLRMOD:
475 1.1 gwr data = 1 << (keysym & 0x1F);
476 1.1 gwr ks->kbd_modbits &= ~data;
477 1.1 gwr break;
478 1.1 gwr
479 1.1 gwr case KEYSYM_SETMOD:
480 1.1 gwr data = 1 << (keysym & 0x1F);
481 1.1 gwr ks->kbd_modbits |= data;
482 1.1 gwr break;
483 1.1 gwr
484 1.1 gwr case KEYSYM_INVMOD:
485 1.1 gwr data = 1 << (keysym & 0x1F);
486 1.1 gwr ks->kbd_modbits ^= data;
487 1.4 gwr kbd_update_leds(k);
488 1.1 gwr break;
489 1.1 gwr
490 1.1 gwr case KEYSYM_ALL_UP:
491 1.1 gwr ks->kbd_modbits &= ~0xFFFF;
492 1.1 gwr break;
493 1.1 gwr
494 1.1 gwr case KEYSYM_SPECIAL:
495 1.1 gwr if (keysym == KEYSYM_NOP)
496 1.1 gwr break;
497 1.32 uwe /* FALLTHROUGH */
498 1.1 gwr default:
499 1.17 gwr /* We could not handle it. */
500 1.17 gwr return (keysym);
501 1.1 gwr }
502 1.32 uwe
503 1.17 gwr return (0);
504 1.1 gwr }
505 1.1 gwr
506 1.32 uwe
507 1.1 gwr /*
508 1.32 uwe * Send string upstream.
509 1.1 gwr */
510 1.13 gwr static void
511 1.32 uwe kbd_input_string(k, str)
512 1.32 uwe struct kbd_softc *k;
513 1.32 uwe char *str;
514 1.1 gwr {
515 1.1 gwr
516 1.32 uwe while (*str) {
517 1.32 uwe (*k->k_cc->cc_upstream)(*str);
518 1.32 uwe ++str;
519 1.1 gwr }
520 1.1 gwr }
521 1.1 gwr
522 1.32 uwe
523 1.1 gwr /*
524 1.32 uwe * Format the F-key sequence and send as a string.
525 1.32 uwe * XXX: Ugly compatibility mappings.
526 1.1 gwr */
527 1.32 uwe static void
528 1.32 uwe kbd_input_funckey(k, keysym)
529 1.1 gwr struct kbd_softc *k;
530 1.32 uwe int keysym;
531 1.1 gwr {
532 1.32 uwe int n;
533 1.32 uwe char str[12];
534 1.32 uwe
535 1.32 uwe n = 0xC0 + (keysym & 0x3F);
536 1.32 uwe sprintf(str, "\033[%dz", n);
537 1.32 uwe kbd_input_string(k, str);
538 1.32 uwe }
539 1.1 gwr
540 1.1 gwr
541 1.32 uwe /*
542 1.32 uwe * Update LEDs to reflect console input state.
543 1.32 uwe */
544 1.32 uwe static void
545 1.32 uwe kbd_update_leds(k)
546 1.32 uwe struct kbd_softc *k;
547 1.32 uwe {
548 1.32 uwe struct kbd_state *ks = &k->k_state;
549 1.32 uwe char leds;
550 1.1 gwr
551 1.32 uwe leds = ks->kbd_leds;
552 1.32 uwe leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
553 1.1 gwr
554 1.32 uwe if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
555 1.32 uwe leds |= LED_CAPS_LOCK;
556 1.32 uwe if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
557 1.32 uwe leds |= LED_NUM_LOCK;
558 1.1 gwr
559 1.32 uwe if (k->k_ops != NULL && k->k_ops->setleds != NULL)
560 1.32 uwe (void)(*k->k_ops->setleds)(k, leds, 0);
561 1.32 uwe }
562 1.1 gwr
563 1.1 gwr
564 1.1 gwr
565 1.32 uwe /****************************************************************
566 1.32 uwe * Events input - called by middle layer at spltty().
567 1.32 uwe ****************************************************************/
568 1.1 gwr
569 1.32 uwe /*
570 1.32 uwe * Entry point for the middle layer to supply raw keystrokes when
571 1.32 uwe * keyboard is open (i.e. is in event mode).
572 1.32 uwe *
573 1.32 uwe * Turn the keystroke into an event and put it in the queue.
574 1.32 uwe * If the queue is full, the keystroke is lost (sorry!).
575 1.32 uwe */
576 1.32 uwe void
577 1.32 uwe kbd_input_event(k, c)
578 1.32 uwe struct kbd_softc *k;
579 1.32 uwe int c;
580 1.32 uwe {
581 1.32 uwe struct firm_event *fe;
582 1.32 uwe int put;
583 1.1 gwr
584 1.32 uwe #ifdef DIAGNOSTIC
585 1.32 uwe if (!k->k_evmode) {
586 1.32 uwe printf("%s: kbd_input_event called when not in event mode\n",
587 1.32 uwe k->k_dev.dv_xname);
588 1.1 gwr return;
589 1.1 gwr }
590 1.32 uwe #endif
591 1.1 gwr put = k->k_events.ev_put;
592 1.1 gwr fe = &k->k_events.ev_q[put];
593 1.1 gwr put = (put + 1) % EV_QSIZE;
594 1.1 gwr if (put == k->k_events.ev_get) {
595 1.1 gwr log(LOG_WARNING, "%s: event queue overflow\n",
596 1.32 uwe k->k_dev.dv_xname);
597 1.1 gwr return;
598 1.1 gwr }
599 1.1 gwr fe->id = KEY_CODE(c);
600 1.1 gwr fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
601 1.1 gwr fe->time = time;
602 1.1 gwr k->k_events.ev_put = put;
603 1.1 gwr EV_WAKEUP(&k->k_events);
604 1.1 gwr }
605 1.1 gwr
606 1.32 uwe
607 1.32 uwe /****************************************************************
608 1.32 uwe * Translation stuff declared in kbd_xlate.h
609 1.32 uwe ****************************************************************/
610 1.24 pk
611 1.24 pk /*
612 1.32 uwe * Initialization - called by either lower layer attach or by kdcninit.
613 1.24 pk */
614 1.32 uwe void
615 1.32 uwe kbd_xlate_init(ks)
616 1.32 uwe struct kbd_state *ks;
617 1.24 pk {
618 1.32 uwe struct keyboard *ktbls;
619 1.32 uwe int id;
620 1.32 uwe
621 1.32 uwe id = ks->kbd_id;
622 1.32 uwe if (id < KBD_MIN_TYPE)
623 1.32 uwe id = KBD_MIN_TYPE;
624 1.32 uwe if (id > kbd_max_type)
625 1.32 uwe id = kbd_max_type;
626 1.32 uwe ktbls = keyboards[id];
627 1.24 pk
628 1.32 uwe ks->kbd_k = *ktbls; /* struct assignment */
629 1.32 uwe ks->kbd_modbits = 0;
630 1.24 pk }
631 1.1 gwr
632 1.1 gwr /*
633 1.32 uwe * Turn keyboard up/down codes into a KEYSYM.
634 1.32 uwe * Note that the "kd" driver (on sun3 and sparc64) uses this too!
635 1.1 gwr */
636 1.1 gwr int
637 1.32 uwe kbd_code_to_keysym(ks, c)
638 1.32 uwe struct kbd_state *ks;
639 1.32 uwe int c;
640 1.1 gwr {
641 1.32 uwe u_short *km;
642 1.32 uwe int keysym;
643 1.1 gwr
644 1.32 uwe /*
645 1.32 uwe * Get keymap pointer. One of these:
646 1.32 uwe * release, control, shifted, normal, ...
647 1.32 uwe */
648 1.32 uwe if (KEY_UP(c))
649 1.32 uwe km = ks->kbd_k.k_release;
650 1.32 uwe else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
651 1.32 uwe km = ks->kbd_k.k_control;
652 1.32 uwe else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
653 1.32 uwe km = ks->kbd_k.k_shifted;
654 1.32 uwe else
655 1.32 uwe km = ks->kbd_k.k_normal;
656 1.23 pk
657 1.32 uwe if (km == NULL) {
658 1.1 gwr /*
659 1.32 uwe * Do not know how to translate yet.
660 1.32 uwe * We will find out when a RESET comes along.
661 1.1 gwr */
662 1.32 uwe return (KEYSYM_NOP);
663 1.1 gwr }
664 1.32 uwe keysym = km[KEY_CODE(c)];
665 1.1 gwr
666 1.1 gwr /*
667 1.32 uwe * Post-processing for Caps-lock
668 1.1 gwr */
669 1.32 uwe if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
670 1.32 uwe (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
671 1.32 uwe {
672 1.32 uwe if (('a' <= keysym) && (keysym <= 'z'))
673 1.32 uwe keysym -= ('a' - 'A');
674 1.1 gwr }
675 1.1 gwr
676 1.1 gwr /*
677 1.32 uwe * Post-processing for Num-lock. All "function"
678 1.32 uwe * keysyms get indirected through another table.
679 1.32 uwe * (XXX: Only if numlock on. Want off also!)
680 1.1 gwr */
681 1.32 uwe if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
682 1.32 uwe (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
683 1.32 uwe {
684 1.32 uwe keysym = kbd_numlock_map[keysym & 0x3F];
685 1.1 gwr }
686 1.7 gwr
687 1.32 uwe return (keysym);
688 1.1 gwr }
689 1.1 gwr
690 1.1 gwr
691 1.7 gwr /*
692 1.32 uwe * Back door for rcons (fb.c)
693 1.7 gwr */
694 1.22 mrg void
695 1.32 uwe kbd_bell(on)
696 1.32 uwe int on;
697 1.1 gwr {
698 1.32 uwe /* XXX: stub out for now */
699 1.32 uwe return;
700 1.1 gwr }
701