kbd.c revision 1.14 1 /* $NetBSD: kbd.c,v 1.14 1996/12/20 12:49:41 leo 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/msvar.h>
60
61 #include "mouse.h"
62
63 static u_char kbd_ring[KBD_RING_SIZE];
64 static volatile u_int kbd_rbput = 0; /* 'put' index */
65 static u_int kbd_rbget = 0; /* 'get' index */
66 static u_char kbd_soft = 0; /* 1: Softint has been scheduled*/
67
68 static struct kbd_softc kbd_softc;
69
70 /* {b,c}devsw[] function prototypes */
71 dev_type_open(kbdopen);
72 dev_type_close(kbdclose);
73 dev_type_read(kbdread);
74 dev_type_ioctl(kbdioctl);
75 dev_type_poll(kbdpoll);
76
77 /* Interrupt handler */
78 void kbdintr __P((int));
79
80 static void kbdsoft __P((void *, void *));
81 static void kbdattach __P((struct device *, struct device *, void *));
82 static int kbdmatch __P((struct device *, struct cfdata *, void *));
83 static int kbd_write_poll __P((u_char *, int));
84 static void kbd_pkg_start __P((struct kbd_softc *, u_char));
85
86 struct cfattach kbd_ca = {
87 sizeof(struct device), kbdmatch, kbdattach
88 };
89
90 struct cfdriver kbd_cd = {
91 NULL, "kbd", DV_DULL, NULL, 0
92 };
93
94
95 /*ARGSUSED*/
96 static int
97 kbdmatch(pdp, cfp, auxp)
98 struct device *pdp;
99 struct cfdata *cfp;
100 void *auxp;
101 {
102 if (!strcmp((char *)auxp, "kbd"))
103 return (1);
104 return (0);
105 }
106
107 /*ARGSUSED*/
108 static void
109 kbdattach(pdp, dp, auxp)
110 struct device *pdp, *dp;
111 void *auxp;
112 {
113 int timeout;
114 u_char kbd_rst[] = { 0x80, 0x01 };
115 u_char kbd_icmd[] = { 0x12, 0x15 };
116
117 /*
118 * Disable keyboard interrupts from MFP
119 */
120 MFP->mf_ierb &= ~IB_AINT;
121
122 /*
123 * Reset ACIA and intialize to:
124 * divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
125 */
126 KBD->ac_cs = A_RESET;
127 delay(100); /* XXX: enough? */
128 KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
129
130 /*
131 * Clear error conditions
132 */
133 while (KBD->ac_cs & (A_IRQ|A_RXRDY))
134 timeout = KBD->ac_da;
135
136 /*
137 * Now send the reset string, and read+ignore it's response
138 */
139 if (!kbd_write_poll(kbd_rst, 2))
140 printf("kbd: error cannot reset keyboard\n");
141 for (timeout = 1000; timeout > 0; timeout--) {
142 if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
143 timeout = KBD->ac_da;
144 timeout = 100;
145 }
146 delay(100);
147 }
148 /*
149 * Send init command: disable mice & joysticks
150 */
151 kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
152
153 printf("\n");
154 }
155
156 /* definitions for atari keyboard encoding. */
157 #define KEY_CODE(c) ((u_char)(c) & 0x7f)
158 #define KEY_UP(c) ((u_char)(c) & 0x80)
159 #define IS_KEY(c) ((u_char)(c) < 0xf6)
160
161 void
162 kbdenable()
163 {
164 int s, code;
165
166 s = spltty();
167
168 /*
169 * Clear error conditions...
170 */
171 while (KBD->ac_cs & (A_IRQ|A_RXRDY))
172 code = KBD->ac_da;
173 /*
174 * Enable interrupts from MFP
175 */
176 MFP->mf_iprb &= ~IB_AINT;
177 MFP->mf_ierb |= IB_AINT;
178 MFP->mf_imrb |= IB_AINT;
179
180 kbd_softc.k_event_mode = 0;
181 kbd_softc.k_events.ev_io = 0;
182 kbd_softc.k_pkg_size = 0;
183 splx(s);
184 }
185
186 int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
187 {
188 if (kbd_softc.k_events.ev_io)
189 return EBUSY;
190
191 kbd_softc.k_events.ev_io = p;
192 ev_init(&kbd_softc.k_events);
193 return (0);
194 }
195
196 int
197 kbdclose(dev_t dev, int flags, int mode, struct proc *p)
198 {
199 /* Turn off event mode, dump the queue */
200 kbd_softc.k_event_mode = 0;
201 ev_fini(&kbd_softc.k_events);
202 kbd_softc.k_events.ev_io = NULL;
203 return (0);
204 }
205
206 int
207 kbdread(dev_t dev, struct uio *uio, int flags)
208 {
209 return ev_read(&kbd_softc.k_events, uio, flags);
210 }
211
212 int
213 kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
214 {
215 register struct kbd_softc *k = &kbd_softc;
216
217 switch (cmd) {
218 case KIOCTRANS:
219 if (*(int *)data == TR_UNTRANS_EVENT)
220 return 0;
221 break;
222
223 case KIOCGTRANS:
224 /*
225 * Get translation mode
226 */
227 *(int *)data = TR_UNTRANS_EVENT;
228 return 0;
229
230 case KIOCSDIRECT:
231 k->k_event_mode = *(int *)data;
232 return 0;
233
234 case FIONBIO: /* we will remove this someday (soon???) */
235 return 0;
236
237 case FIOASYNC:
238 k->k_events.ev_async = *(int *)data != 0;
239 return 0;
240
241 case TIOCSPGRP:
242 if (*(int *)data != k->k_events.ev_io->p_pgid)
243 return EPERM;
244 return 0;
245
246 default:
247 return ENOTTY;
248 }
249
250 /*
251 * We identified the ioctl, but we do not handle it.
252 */
253 return EOPNOTSUPP; /* misuse, but what the heck */
254 }
255
256 int
257 kbdpoll (dev_t dev, int events, struct proc *p)
258 {
259 return ev_poll (&kbd_softc.k_events, events, p);
260 }
261
262 /*
263 * Keyboard interrupt handler called straight from MFP at spl6.
264 */
265 void
266 kbdintr(sr)
267 int sr; /* sr at time of interrupt */
268 {
269 int code;
270 int got_char = 0;
271
272 /*
273 * There may be multiple keys available. Read them all.
274 */
275 while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
276 got_char = 1;
277 if (KBD->ac_cs & (A_OE|A_PE)) {
278 code = KBD->ac_da; /* Silently ignore errors */
279 continue;
280 }
281 kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
282 }
283
284 /*
285 * If characters are waiting for transmit, send them.
286 */
287 if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
288 if (kbd_softc.k_sendp != NULL)
289 KBD->ac_da = *kbd_softc.k_sendp++;
290 if (--kbd_softc.k_send_cnt <= 0) {
291 /*
292 * The total package has been transmitted,
293 * wakeup anyone waiting for it.
294 */
295 KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
296 kbd_softc.k_sendp = NULL;
297 kbd_softc.k_send_cnt = 0;
298 wakeup((caddr_t)&kbd_softc.k_send_cnt);
299 }
300 }
301
302 /*
303 * Activate software-level to handle possible input.
304 */
305 if (got_char) {
306 if (!BASEPRI(sr)) {
307 if (!kbd_soft++)
308 add_sicallback(kbdsoft, 0, 0);
309 } else {
310 spl1();
311 kbdsoft(NULL, NULL);
312 }
313 }
314 }
315
316 /*
317 * Keyboard soft interrupt handler
318 */
319 void
320 kbdsoft(junk1, junk2)
321 void *junk1, *junk2;
322 {
323 int s;
324 u_char code;
325 struct kbd_softc *k = &kbd_softc;
326 struct firm_event *fe;
327 int put;
328 int n, get;
329
330 kbd_soft = 0;
331 get = kbd_rbget;
332
333 for (;;) {
334 n = kbd_rbput;
335 if (get == n) /* We're done */
336 break;
337 n -= get;
338 if (n > KBD_RING_SIZE) { /* Ring buffer overflow */
339 get += n - KBD_RING_SIZE;
340 n = KBD_RING_SIZE;
341 }
342 while (--n >= 0) {
343 code = kbd_ring[get++ & KBD_RING_MASK];
344
345 /*
346 * If collecting a package, stuff it in and
347 * continue.
348 */
349 if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
350 k->k_package[k->k_pkg_idx++] = code;
351 if (k->k_pkg_idx == k->k_pkg_size) {
352 /*
353 * Package is complete.
354 */
355 switch(k->k_pkg_type) {
356 #if NMOUSE > 0
357 case KBD_AMS_PKG:
358 case KBD_RMS_PKG:
359 case KBD_JOY1_PKG:
360 mouse_soft((REL_MOUSE *)k->k_package,
361 k->k_pkg_size, k->k_pkg_type);
362 #endif /* NMOUSE */
363 }
364 k->k_pkg_size = 0;
365 }
366 continue;
367 }
368 /*
369 * If this is a package header, init pkg. handling.
370 */
371 if (!IS_KEY(code)) {
372 kbd_pkg_start(k, code);
373 continue;
374 }
375 /*
376 * if not in event mode, deliver straight to ite to
377 * process key stroke
378 */
379 if (!k->k_event_mode) {
380 /* Gets to spltty() by itself */
381 ite_filter(code, ITEFILT_TTY);
382 continue;
383 }
384
385 /*
386 * Keyboard is generating events. Turn this keystroke
387 * into an event and put it in the queue. If the queue
388 * is full, the keystroke is lost (sorry!).
389 */
390 s = spltty();
391 put = k->k_events.ev_put;
392 fe = &k->k_events.ev_q[put];
393 put = (put + 1) % EV_QSIZE;
394 if (put == k->k_events.ev_get) {
395 log(LOG_WARNING,
396 "keyboard event queue overflow\n");
397 splx(s);
398 continue;
399 }
400 fe->id = KEY_CODE(code);
401 fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
402 fe->time = time;
403 k->k_events.ev_put = put;
404 EV_WAKEUP(&k->k_events);
405 splx(s);
406 }
407 kbd_rbget = get;
408 }
409 }
410
411 static char sound[] = {
412 0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
413 0xF8,0x10,0x10,0x10,0x00,0x20,0x03
414 };
415
416 void
417 kbdbell()
418 {
419 register int i, sps;
420
421 sps = splhigh();
422 for (i = 0; i < sizeof(sound); i++) {
423 YM2149->sd_selr = i;
424 YM2149->sd_wdat = sound[i];
425 }
426 splx(sps);
427 }
428
429 int
430 kbdgetcn()
431 {
432 u_char code;
433 int s = spltty();
434 int ints_active;
435
436 ints_active = 0;
437 if (MFP->mf_imrb & IB_AINT) {
438 ints_active = 1;
439 MFP->mf_imrb &= ~IB_AINT;
440 }
441 for (;;) {
442 while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
443 ; /* Wait for key */
444 if (KBD->ac_cs & (A_OE|A_PE)) {
445 code = KBD->ac_da; /* Silently ignore errors */
446 continue;
447 }
448 break;
449 }
450
451 code = KBD->ac_da;
452 if (ints_active) {
453 MFP->mf_iprb &= ~IB_AINT;
454 MFP->mf_imrb |= IB_AINT;
455 }
456
457 splx (s);
458 return code;
459 }
460
461 /*
462 * Write a command to the keyboard in 'polled' mode.
463 */
464 static int
465 kbd_write_poll(cmd, len)
466 u_char *cmd;
467 int len;
468 {
469 int timeout;
470
471 while (len-- > 0) {
472 KBD->ac_da = *cmd++;
473 for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
474 delay(10);
475 if (!(KBD->ac_cs & A_TXRDY))
476 return (0);
477 }
478 return (1);
479 }
480
481 /*
482 * Write a command to the keyboard. Return when command is send.
483 */
484 void
485 kbd_write(cmd, len)
486 u_char *cmd;
487 int len;
488 {
489 struct kbd_softc *k = &kbd_softc;
490 int sps;
491
492 /*
493 * Get to splhigh, 'real' interrupts arrive at spl6!
494 */
495 sps = splhigh();
496
497 /*
498 * Make sure any privious write has ended...
499 */
500 while (k->k_sendp != NULL)
501 tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
502
503 /*
504 * If the KBD-acia is not currently busy, send the first
505 * character now.
506 */
507 KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
508 if (KBD->ac_cs & A_TXRDY) {
509 KBD->ac_da = *cmd++;
510 len--;
511 }
512
513 /*
514 * If we're not yet done, wait until all characters are send.
515 */
516 if (len > 0) {
517 k->k_sendp = cmd;
518 k->k_send_cnt = len;
519 tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
520 }
521 splx(sps);
522
523 /*
524 * Wakeup all procs waiting for us.
525 */
526 wakeup((caddr_t)&k->k_sendp);
527 }
528
529 /*
530 * Setup softc-fields to assemble a keyboard package.
531 */
532 static void
533 kbd_pkg_start(kp, msg_start)
534 struct kbd_softc *kp;
535 u_char msg_start;
536 {
537 kp->k_pkg_idx = 1;
538 kp->k_package[0] = msg_start;
539 switch (msg_start) {
540 case 0xf6:
541 kp->k_pkg_type = KBD_MEM_PKG;
542 kp->k_pkg_size = 8;
543 break;
544 case 0xf7:
545 kp->k_pkg_type = KBD_AMS_PKG;
546 kp->k_pkg_size = 6;
547 break;
548 case 0xf8:
549 case 0xf9:
550 case 0xfa:
551 case 0xfb:
552 kp->k_pkg_type = KBD_RMS_PKG;
553 kp->k_pkg_size = 3;
554 break;
555 case 0xfc:
556 kp->k_pkg_type = KBD_CLK_PKG;
557 kp->k_pkg_size = 7;
558 break;
559 case 0xfe:
560 kp->k_pkg_type = KBD_JOY0_PKG;
561 kp->k_pkg_size = 2;
562 break;
563 case 0xff:
564 kp->k_pkg_type = KBD_JOY1_PKG;
565 kp->k_pkg_size = 2;
566 break;
567 default:
568 printf("kbd: Unknown packet 0x%x\n", msg_start);
569 break;
570 }
571 }
572