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