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