kbd.c revision 1.18.14.1 1 /* $NetBSD: kbd.c,v 1.18.14.1 2001/10/10 11:56:00 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman
5 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/ioctl.h>
41 #include <sys/tty.h>
42 #include <sys/proc.h>
43 #include <sys/conf.h>
44 #include <sys/file.h>
45 #include <sys/kernel.h>
46 #include <sys/signalvar.h>
47 #include <sys/syslog.h>
48 #include <dev/cons.h>
49 #include <machine/cpu.h>
50 #include <machine/iomap.h>
51 #include <machine/mfp.h>
52 #include <machine/acia.h>
53 #include <atari/dev/itevar.h>
54 #include <atari/dev/event_var.h>
55 #include <atari/dev/vuid_event.h>
56 #include <atari/dev/ym2149reg.h>
57 #include <atari/dev/kbdreg.h>
58 #include <atari/dev/kbdvar.h>
59 #include <atari/dev/kbdmap.h>
60 #include <atari/dev/msvar.h>
61
62 #include "mouse.h"
63
64 u_char kbd_modifier; /* Modifier mask */
65
66 static u_char kbd_ring[KBD_RING_SIZE];
67 static volatile u_int kbd_rbput = 0; /* 'put' index */
68 static u_int kbd_rbget = 0; /* 'get' index */
69 static u_char kbd_soft = 0; /* 1: Softint has been scheduled*/
70
71 static struct kbd_softc kbd_softc;
72
73 /* {b,c}devsw[] function prototypes */
74 dev_type_open(kbdopen);
75 dev_type_close(kbdclose);
76 dev_type_read(kbdread);
77 dev_type_ioctl(kbdioctl);
78 dev_type_poll(kbdpoll);
79
80 /* Interrupt handler */
81 void kbdintr __P((int));
82
83 static void kbdsoft __P((void *, void *));
84 static void kbdattach __P((struct device *, struct device *, void *));
85 static int kbdmatch __P((struct device *, struct cfdata *, void *));
86 static int kbd_do_modifier __P((u_char));
87 static int kbd_write_poll __P((u_char *, int));
88 static void kbd_pkg_start __P((struct kbd_softc *, u_char));
89
90 struct cfattach kbd_ca = {
91 sizeof(struct device), kbdmatch, kbdattach
92 };
93
94 /*ARGSUSED*/
95 static int
96 kbdmatch(pdp, cfp, auxp)
97 struct device *pdp;
98 struct cfdata *cfp;
99 void *auxp;
100 {
101 if (!strcmp((char *)auxp, "kbd"))
102 return (1);
103 return (0);
104 }
105
106 /*ARGSUSED*/
107 static void
108 kbdattach(pdp, dp, auxp)
109 struct device *pdp, *dp;
110 void *auxp;
111 {
112 int timeout;
113 u_char kbd_rst[] = { 0x80, 0x01 };
114 u_char kbd_icmd[] = { 0x12, 0x15 };
115
116 /*
117 * Disable keyboard interrupts from MFP
118 */
119 MFP->mf_ierb &= ~IB_AINT;
120
121 /*
122 * Reset ACIA and intialize to:
123 * divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
124 */
125 KBD->ac_cs = A_RESET;
126 delay(100); /* XXX: enough? */
127 KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
128
129 /*
130 * Clear error conditions
131 */
132 while (KBD->ac_cs & (A_IRQ|A_RXRDY))
133 timeout = KBD->ac_da;
134
135 /*
136 * Now send the reset string, and read+ignore it's response
137 */
138 if (!kbd_write_poll(kbd_rst, 2))
139 printf("kbd: error cannot reset keyboard\n");
140 for (timeout = 1000; timeout > 0; timeout--) {
141 if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
142 timeout = KBD->ac_da;
143 timeout = 100;
144 }
145 delay(100);
146 }
147 /*
148 * Send init command: disable mice & joysticks
149 */
150 kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
151
152 printf("\n");
153 }
154
155 void
156 kbdenable()
157 {
158 int s, code;
159
160 s = spltty();
161
162 /*
163 * Clear error conditions...
164 */
165 while (KBD->ac_cs & (A_IRQ|A_RXRDY))
166 code = KBD->ac_da;
167 /*
168 * Enable interrupts from MFP
169 */
170 MFP->mf_iprb = (u_int8_t)~IB_AINT;
171 MFP->mf_ierb |= IB_AINT;
172 MFP->mf_imrb |= IB_AINT;
173
174 kbd_softc.k_event_mode = 0;
175 kbd_softc.k_events.ev_io = 0;
176 kbd_softc.k_pkg_size = 0;
177 splx(s);
178 }
179
180 int kbdopen(struct vnode *devvp, int flags, int mode, struct proc *p)
181 {
182 if (kbd_softc.k_events.ev_io)
183 return EBUSY;
184
185 kbd_softc.k_events.ev_io = p;
186 ev_init(&kbd_softc.k_events);
187 return (0);
188 }
189
190 int
191 kbdclose(struct vnode *devvp, int flags, int mode, struct proc *p)
192 {
193 /* Turn off event mode, dump the queue */
194 kbd_softc.k_event_mode = 0;
195 ev_fini(&kbd_softc.k_events);
196 kbd_softc.k_events.ev_io = NULL;
197 return (0);
198 }
199
200 int
201 kbdread(struct vnode *devvp, struct uio *uio, int flags)
202 {
203 return ev_read(&kbd_softc.k_events, uio, flags);
204 }
205
206 int
207 kbdioctl(struct vnode *devvp, u_long cmd, caddr_t data, int flag,
208 struct proc *p)
209 {
210 struct kbd_softc *k = &kbd_softc;
211 struct kbdbell *kb;
212
213 switch (cmd) {
214 case KIOCTRANS:
215 if (*(int *)data == TR_UNTRANS_EVENT)
216 return 0;
217 break;
218
219 case KIOCGTRANS:
220 /*
221 * Get translation mode
222 */
223 *(int *)data = TR_UNTRANS_EVENT;
224 return 0;
225
226 case KIOCSDIRECT:
227 k->k_event_mode = *(int *)data;
228 return 0;
229
230 case KIOCRINGBELL:
231 kb = (struct kbdbell *)data;
232 if (kb)
233 kbd_bell_sparms(kb->volume, kb->pitch,
234 kb->duration);
235 kbdbell();
236 return 0;
237
238 case FIONBIO: /* we will remove this someday (soon???) */
239 return 0;
240
241 case FIOASYNC:
242 k->k_events.ev_async = *(int *)data != 0;
243 return 0;
244
245 case TIOCSPGRP:
246 if (*(int *)data != k->k_events.ev_io->p_pgid)
247 return EPERM;
248 return 0;
249
250 default:
251 return ENOTTY;
252 }
253
254 /*
255 * We identified the ioctl, but we do not handle it.
256 */
257 return EOPNOTSUPP; /* misuse, but what the heck */
258 }
259
260 int
261 kbdpoll(struct vnode *devvp, int events, struct proc *p)
262 {
263 return ev_poll (&kbd_softc.k_events, events, p);
264 }
265
266 /*
267 * Keyboard interrupt handler called straight from MFP at spl6.
268 */
269 void
270 kbdintr(sr)
271 int sr; /* sr at time of interrupt */
272 {
273 int code;
274 int got_char = 0;
275
276 /*
277 * There may be multiple keys available. Read them all.
278 */
279 while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
280 got_char = 1;
281 if (KBD->ac_cs & (A_OE|A_PE)) {
282 code = KBD->ac_da; /* Silently ignore errors */
283 continue;
284 }
285 kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
286 }
287
288 /*
289 * If characters are waiting for transmit, send them.
290 */
291 if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
292 if (kbd_softc.k_sendp != NULL)
293 KBD->ac_da = *kbd_softc.k_sendp++;
294 if (--kbd_softc.k_send_cnt <= 0) {
295 /*
296 * The total package has been transmitted,
297 * wakeup anyone waiting for it.
298 */
299 KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
300 kbd_softc.k_sendp = NULL;
301 kbd_softc.k_send_cnt = 0;
302 wakeup((caddr_t)&kbd_softc.k_send_cnt);
303 }
304 }
305
306 /*
307 * Activate software-level to handle possible input.
308 */
309 if (got_char) {
310 if (!BASEPRI(sr)) {
311 if (!kbd_soft++)
312 add_sicallback(kbdsoft, 0, 0);
313 } else {
314 spl1();
315 kbdsoft(NULL, NULL);
316 }
317 }
318 }
319
320 /*
321 * Keyboard soft interrupt handler
322 */
323 void
324 kbdsoft(junk1, junk2)
325 void *junk1, *junk2;
326 {
327 int s;
328 u_char code;
329 struct kbd_softc *k = &kbd_softc;
330 struct firm_event *fe;
331 int put;
332 int n, get;
333
334 kbd_soft = 0;
335 get = kbd_rbget;
336
337 for (;;) {
338 n = kbd_rbput;
339 if (get == n) /* We're done */
340 break;
341 n -= get;
342 if (n > KBD_RING_SIZE) { /* Ring buffer overflow */
343 get += n - KBD_RING_SIZE;
344 n = KBD_RING_SIZE;
345 }
346 while (--n >= 0) {
347 code = kbd_ring[get++ & KBD_RING_MASK];
348
349 /*
350 * If collecting a package, stuff it in and
351 * continue.
352 */
353 if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
354 k->k_package[k->k_pkg_idx++] = code;
355 if (k->k_pkg_idx == k->k_pkg_size) {
356 /*
357 * Package is complete.
358 */
359 switch(k->k_pkg_type) {
360 #if NMOUSE > 0
361 case KBD_AMS_PKG:
362 case KBD_RMS_PKG:
363 case KBD_JOY1_PKG:
364 mouse_soft((REL_MOUSE *)k->k_package,
365 k->k_pkg_size, k->k_pkg_type);
366 #endif /* NMOUSE */
367 }
368 k->k_pkg_size = 0;
369 }
370 continue;
371 }
372 /*
373 * If this is a package header, init pkg. handling.
374 */
375 if (!KBD_IS_KEY(code)) {
376 kbd_pkg_start(k, code);
377 continue;
378 }
379 if (kbd_do_modifier(code) && !k->k_event_mode)
380 continue;
381
382 /*
383 * if not in event mode, deliver straight to ite to
384 * process key stroke
385 */
386 if (!k->k_event_mode) {
387 /* Gets to spltty() by itself */
388 ite_filter(code, ITEFILT_TTY);
389 continue;
390 }
391
392 /*
393 * Keyboard is generating events. Turn this keystroke
394 * into an event and put it in the queue. If the queue
395 * is full, the keystroke is lost (sorry!).
396 */
397 s = spltty();
398 put = k->k_events.ev_put;
399 fe = &k->k_events.ev_q[put];
400 put = (put + 1) % EV_QSIZE;
401 if (put == k->k_events.ev_get) {
402 log(LOG_WARNING,
403 "keyboard event queue overflow\n");
404 splx(s);
405 continue;
406 }
407 fe->id = KBD_SCANCODE(code);
408 fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
409 fe->time = time;
410 k->k_events.ev_put = put;
411 EV_WAKEUP(&k->k_events);
412 splx(s);
413 }
414 kbd_rbget = get;
415 }
416 }
417
418 static u_char sound[] = {
419 0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
420 0xF8,0x10,0x10,0x10,0x00,0x20,0x03
421 };
422
423 void
424 kbdbell()
425 {
426 register int i, sps;
427
428 sps = splhigh();
429 for (i = 0; i < sizeof(sound); i++) {
430 YM2149->sd_selr = i;
431 YM2149->sd_wdat = sound[i];
432 }
433 splx(sps);
434 }
435
436
437 /*
438 * Set the parameters of the 'default' beep.
439 */
440
441 #define KBDBELLCLOCK 125000 /* 2MHz / 16 */
442 #define KBDBELLDURATION 128 /* 256 / 2MHz */
443
444 void
445 kbd_bell_gparms(volume, pitch, duration)
446 u_int *volume, *pitch, *duration;
447 {
448 u_int tmp;
449
450 tmp = sound[11] | (sound[12] << 8);
451 *duration = (tmp * KBDBELLDURATION) / 1000;
452
453 tmp = sound[0] | (sound[1] << 8);
454 *pitch = KBDBELLCLOCK / tmp;
455
456 *volume = 0;
457 }
458
459 void
460 kbd_bell_sparms(volume, pitch, duration)
461 u_int volume, pitch, duration;
462 {
463 u_int f, t;
464
465 f = pitch > 10 ? pitch : 10; /* minimum pitch */
466 if (f > 20000)
467 f = 20000; /* maximum pitch */
468
469 f = KBDBELLCLOCK / f;
470
471 t = (duration * 1000) / KBDBELLDURATION;
472
473 sound[ 0] = f & 0xff;
474 sound[ 1] = (f >> 8) & 0xf;
475 f -= 1;
476 sound[ 2] = f & 0xff;
477 sound[ 3] = (f >> 8) & 0xf;
478 f += 2;
479 sound[ 4] = f & 0xff;
480 sound[ 5] = (f >> 8) & 0xf;
481
482 sound[11] = t & 0xff;
483 sound[12] = (t >> 8) & 0xff;
484
485 sound[13] = 0x03;
486 }
487
488 int
489 kbdgetcn()
490 {
491 u_char code;
492 int s = spltty();
493 int ints_active;
494
495 ints_active = 0;
496 if (MFP->mf_imrb & IB_AINT) {
497 ints_active = 1;
498 MFP->mf_imrb &= ~IB_AINT;
499 }
500 for (;;) {
501 while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
502 ; /* Wait for key */
503 if (KBD->ac_cs & (A_OE|A_PE)) {
504 code = KBD->ac_da; /* Silently ignore errors */
505 continue;
506 }
507 code = KBD->ac_da;
508 if (!kbd_do_modifier(code))
509 break;
510 }
511
512 if (ints_active) {
513 MFP->mf_iprb = (u_int8_t)~IB_AINT;
514 MFP->mf_imrb |= IB_AINT;
515 }
516
517 splx (s);
518 return code;
519 }
520
521 /*
522 * Write a command to the keyboard in 'polled' mode.
523 */
524 static int
525 kbd_write_poll(cmd, len)
526 u_char *cmd;
527 int len;
528 {
529 int timeout;
530
531 while (len-- > 0) {
532 KBD->ac_da = *cmd++;
533 for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
534 delay(10);
535 if (!(KBD->ac_cs & A_TXRDY))
536 return (0);
537 }
538 return (1);
539 }
540
541 /*
542 * Write a command to the keyboard. Return when command is send.
543 */
544 void
545 kbd_write(cmd, len)
546 u_char *cmd;
547 int len;
548 {
549 struct kbd_softc *k = &kbd_softc;
550 int sps;
551
552 /*
553 * Get to splhigh, 'real' interrupts arrive at spl6!
554 */
555 sps = splhigh();
556
557 /*
558 * Make sure any privious write has ended...
559 */
560 while (k->k_sendp != NULL)
561 tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
562
563 /*
564 * If the KBD-acia is not currently busy, send the first
565 * character now.
566 */
567 KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
568 if (KBD->ac_cs & A_TXRDY) {
569 KBD->ac_da = *cmd++;
570 len--;
571 }
572
573 /*
574 * If we're not yet done, wait until all characters are send.
575 */
576 if (len > 0) {
577 k->k_sendp = cmd;
578 k->k_send_cnt = len;
579 tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
580 }
581 splx(sps);
582
583 /*
584 * Wakeup all procs waiting for us.
585 */
586 wakeup((caddr_t)&k->k_sendp);
587 }
588
589 /*
590 * Setup softc-fields to assemble a keyboard package.
591 */
592 static void
593 kbd_pkg_start(kp, msg_start)
594 struct kbd_softc *kp;
595 u_char msg_start;
596 {
597 kp->k_pkg_idx = 1;
598 kp->k_package[0] = msg_start;
599 switch (msg_start) {
600 case 0xf6:
601 kp->k_pkg_type = KBD_MEM_PKG;
602 kp->k_pkg_size = 8;
603 break;
604 case 0xf7:
605 kp->k_pkg_type = KBD_AMS_PKG;
606 kp->k_pkg_size = 6;
607 break;
608 case 0xf8:
609 case 0xf9:
610 case 0xfa:
611 case 0xfb:
612 kp->k_pkg_type = KBD_RMS_PKG;
613 kp->k_pkg_size = 3;
614 break;
615 case 0xfc:
616 kp->k_pkg_type = KBD_CLK_PKG;
617 kp->k_pkg_size = 7;
618 break;
619 case 0xfe:
620 kp->k_pkg_type = KBD_JOY0_PKG;
621 kp->k_pkg_size = 2;
622 break;
623 case 0xff:
624 kp->k_pkg_type = KBD_JOY1_PKG;
625 kp->k_pkg_size = 2;
626 break;
627 default:
628 printf("kbd: Unknown packet 0x%x\n", msg_start);
629 break;
630 }
631 }
632
633 /*
634 * Modifier processing
635 */
636 static int
637 kbd_do_modifier(code)
638 u_char code;
639 {
640 u_char up, mask;
641
642 up = KBD_RELEASED(code);
643 mask = 0;
644
645 switch(KBD_SCANCODE(code)) {
646 case KBD_LEFT_SHIFT:
647 mask = KBD_MOD_LSHIFT;
648 break;
649 case KBD_RIGHT_SHIFT:
650 mask = KBD_MOD_RSHIFT;
651 break;
652 case KBD_CTRL:
653 mask = KBD_MOD_CTRL;
654 break;
655 case KBD_ALT:
656 mask = KBD_MOD_ALT;
657 break;
658 case KBD_CAPS_LOCK:
659 /* CAPSLOCK is a toggle */
660 if(!up)
661 kbd_modifier ^= KBD_MOD_CAPS;
662 return 1;
663 }
664 if(mask) {
665 if(up)
666 kbd_modifier &= ~mask;
667 else
668 kbd_modifier |= mask;
669 return 1;
670 }
671 return 0;
672 }
673