kbd.c revision 1.3 1 /* $NetBSD: kbd.c,v 1.3 1995/06/09 20:00:14 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 /*
59 * The ringbuffer is the interface between the hard and soft interrupt handler.
60 * The hard interrupt runs straight from the MFP interrupt.
61 */
62 #define KBD_RING_SIZE 256 /* Sz of input ring buffer, must be power of 2 */
63 #define KBD_RING_MASK 255 /* Modulo mask for above */
64
65 static u_char kbd_ring[KBD_RING_SIZE];
66 static volatile u_int kbd_rbput = 0; /* 'put' index */
67 static u_int kbd_rbget = 0; /* 'get' index */
68 static u_char kbd_soft = 0; /* 1: Softint has been scheduled*/
69
70 struct kbd_softc {
71 int k_event_mode; /* if 1, collect events, */
72 /* else pass to ite */
73 struct evvar k_events; /* event queue state */
74 u_char k_soft_cs; /* control-reg. copy */
75 u_char k_package[20]; /* XXX package being build */
76 u_char k_pkg_size; /* Size of the package */
77 u_char k_pkg_idx;
78 u_char *k_sendp; /* Output pointer */
79 int k_send_cnt; /* Chars left for output */
80 };
81
82 static struct kbd_softc kbd_softc;
83
84 static void kbdsoft __P((void));
85 static void kbdattach __P((struct device *, struct device *, void *));
86 static int kbdmatch __P((struct device *, struct cfdata *, void *));
87 static int kbd_wite_poll __P((u_char *, int));
88 static void kbd_write __P((u_char *, int));
89 static void kbd_pkg_start __P((struct kbd_softc *, u_char));
90
91 struct cfdriver kbdcd = {
92 NULL, "kbd", (cfmatch_t)kbdmatch, kbdattach,
93 DV_DULL, sizeof(struct device), NULL, 0 };
94
95
96 /*ARGSUSED*/
97 static int
98 kbdmatch(pdp, cfp, auxp)
99 struct device *pdp;
100 struct cfdata *cfp;
101 void *auxp;
102 {
103 if (!strcmp((char *)auxp, "kbd"))
104 return (1);
105 return (0);
106 }
107
108 /*ARGSUSED*/
109 static void
110 kbdattach(pdp, dp, auxp)
111 struct device *pdp, *dp;
112 void *auxp;
113 {
114 int timeout;
115 u_char rst[] = { 0x80, 0x01 };
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_wite_poll(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 printf("\n");
150 }
151
152 /* definitions for atari keyboard encoding. */
153 #define KEY_CODE(c) ((u_char)(c) & 0x7f)
154 #define KEY_UP(c) ((u_char)(c) & 0x80)
155 #define IS_KEY(c) ((u_char)(c) < 0xf6)
156
157 void
158 kbdenable()
159 {
160 int s, code;
161 u_char kbd_icmd[] = { 0x12, 0x15 };
162
163 s = spltty();
164
165 /*
166 * Clear error conditions...
167 */
168 while (KBD->ac_cs & (A_IRQ|A_RXRDY))
169 code = KBD->ac_da;
170 /*
171 * Enable interrupts from MFP
172 */
173 MFP->mf_iprb &= ~IB_AINT;
174 MFP->mf_ierb |= IB_AINT;
175 MFP->mf_imrb |= IB_AINT;
176
177 kbd_softc.k_event_mode = 0;
178 kbd_softc.k_events.ev_io = 0;
179 kbd_softc.k_pkg_size = 0;
180 splx(s);
181
182 /*
183 * Send init command: disable mice & joysticks
184 */
185 kbd_write(kbd_icmd, sizeof(kbd_icmd));
186 }
187
188 int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
189 {
190 int s, error;
191
192 if (kbd_softc.k_events.ev_io)
193 return EBUSY;
194
195 kbd_softc.k_events.ev_io = p;
196 ev_init(&kbd_softc.k_events);
197 return (0);
198 }
199
200 int
201 kbdclose(dev_t dev, int flags, int mode, struct proc *p)
202 {
203 /* Turn off event mode, dump the queue */
204 kbd_softc.k_event_mode = 0;
205 ev_fini(&kbd_softc.k_events);
206 kbd_softc.k_events.ev_io = NULL;
207 return (0);
208 }
209
210 int
211 kbdread(dev_t dev, struct uio *uio, int flags)
212 {
213 return ev_read(&kbd_softc.k_events, uio, flags);
214 }
215
216 int
217 kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
218 {
219 register struct kbd_softc *k = &kbd_softc;
220
221 switch (cmd) {
222 case KIOCTRANS:
223 if (*(int *)data == TR_UNTRANS_EVENT)
224 return 0;
225 break;
226
227 case KIOCGTRANS:
228 /*
229 * Get translation mode
230 */
231 *(int *)data = TR_UNTRANS_EVENT;
232 return 0;
233
234 case KIOCSDIRECT:
235 k->k_event_mode = *(int *)data;
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 kbdselect (dev_t dev, int rw, struct proc *p)
262 {
263 return ev_select (&kbd_softc.k_events, rw, p);
264 }
265
266 /*
267 * Keyboard interrupt handler called straight from MFP at spl6.
268 */
269 int
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();
316 }
317 }
318 }
319
320 /*
321 * Keyboard soft interrupt handler
322 */
323 void
324 kbdsoft()
325 {
326 int s;
327 u_char code;
328 struct kbd_softc *k = &kbd_softc;
329 struct firm_event *fe;
330 int put;
331 int n, get;
332
333 kbd_soft = 0;
334 get = kbd_rbget;
335
336 for (;;) {
337 n = kbd_rbput;
338 if (get == n) /* We're done */
339 break;
340 n -= get;
341 if (n > KBD_RING_SIZE) { /* Ring buffer overflow */
342 get += n - KBD_RING_SIZE;
343 n = KBD_RING_SIZE;
344 }
345 while (--n >= 0) {
346 code = kbd_ring[get++ & KBD_RING_MASK];
347
348 /*
349 * If collecting a package, stuff it in and
350 * continue.
351 */
352 if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
353 k->k_package[k->k_pkg_idx++] = code;
354 if (k->k_pkg_idx == k->k_pkg_size) {
355 k->k_pkg_size = 0;
356 /*
357 * Package is complete, we can now
358 * send it to (the not yet present)
359 * mouse driver...
360 */
361 /* mouse_soft(k->k_package,k->k_pkg_size);*/
362 }
363 continue;
364 }
365 /*
366 * If this is a package header, init pkg. handling.
367 */
368 if (!IS_KEY(code)) {
369 kbd_pkg_start(k, code);
370 continue;
371 }
372 /*
373 * if not in event mode, deliver straight to ite to
374 * process key stroke
375 */
376 if (!k->k_event_mode) {
377 /* Gets to spltty() by itself */
378 ite_filter(code, ITEFILT_TTY);
379 continue;
380 }
381
382 /*
383 * Keyboard is generating events. Turn this keystroke
384 * into an event and put it in the queue. If the queue
385 * is full, the keystroke is lost (sorry!).
386 */
387 s = spltty();
388 put = k->k_events.ev_put;
389 fe = &k->k_events.ev_q[put];
390 put = (put + 1) % EV_QSIZE;
391 if (put == k->k_events.ev_get) {
392 log(LOG_WARNING,
393 "keyboard event queue overflow\n");
394 splx(s);
395 continue;
396 }
397 fe->id = KEY_CODE(code);
398 fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
399 fe->time = time;
400 k->k_events.ev_put = put;
401 EV_WAKEUP(&k->k_events);
402 splx(s);
403 }
404 kbd_rbget = get;
405 }
406 }
407
408 static char sound[] = {
409 0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
410 0xF8,0x10,0x10,0x10,0x00,0x20,0x03
411 };
412
413 int
414 kbdbell()
415 {
416 register int i, sps;
417
418 sps = spltty();
419 for (i = 0; i < sizeof(sound); i++) {
420 SOUND->sd_selr = i;
421 SOUND->sd_wdat = sound[i];
422 }
423 splx(sps);
424 }
425
426 int
427 kbdgetcn()
428 {
429 u_char code;
430 int s = spltty();
431
432 MFP->mf_imrb &= ~IB_AINT;
433 for (;;) {
434 while (!(KBD->ac_cs & (A_IRQ|A_RXRDY)))
435 ; /* Wait for key */
436 if (KBD->ac_cs & (A_OE|A_PE)) {
437 code = KBD->ac_da; /* Silently ignore errors */
438 continue;
439 }
440 break;
441 }
442
443 MFP->mf_iprb &= ~IB_AINT;
444 MFP->mf_imrb |= IB_AINT;
445
446 code = KBD->ac_da;
447 splx (s);
448 return code;
449 }
450
451 /*
452 * Write a command to the keyboard in 'polled' mode.
453 */
454 static int
455 kbd_wite_poll(cmd, len)
456 u_char *cmd;
457 int len;
458 {
459 int timeout;
460
461 while (len-- > 0) {
462 KBD->ac_da = *cmd++;
463 for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
464 delay(10);
465 if (!(KBD->ac_cs & A_TXRDY))
466 return (0);
467 }
468 return (1);
469 }
470
471 /*
472 * Write a command to the keyboard. Return when command is send.
473 */
474 static void
475 kbd_write(cmd, len)
476 u_char *cmd;
477 int len;
478 {
479 struct kbd_softc *k = &kbd_softc;
480 int sps;
481
482 /*
483 * Get to splhigh, 'real' interrupts arrive at spl6!
484 */
485 sps = splhigh();
486
487 /*
488 * Make sure any privious write has ended...
489 */
490 while (k->k_sendp != NULL)
491 tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
492
493 /*
494 * If the KBD-acia is not currently busy, send the first
495 * character now.
496 */
497 KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
498 if (KBD->ac_cs & A_TXRDY) {
499 KBD->ac_da = *cmd++;
500 len--;
501 }
502
503 /*
504 * If we're not yet done, wait until all characters are send.
505 */
506 if (len > 0) {
507 k->k_sendp = cmd;
508 k->k_send_cnt = len;
509 tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
510 }
511 splx(sps);
512
513 /*
514 * Wakeup all procs waiting for us.
515 */
516 wakeup((caddr_t)&k->k_sendp);
517 }
518
519 /*
520 * Setup softc-fields to assemble a keyboard package.
521 */
522 static void
523 kbd_pkg_start(kp, msg_start)
524 struct kbd_softc *kp;
525 u_char msg_start;
526 {
527 kp->k_pkg_idx = 1;
528 kp->k_package[0] = msg_start;
529 switch (msg_start) {
530 case 0xf6:
531 kp->k_pkg_size = 8;
532 break;
533 case 0xf7:
534 kp->k_pkg_size = 6;
535 break;
536 case 0xf8:
537 case 0xf9:
538 case 0xfa:
539 case 0xfb:
540 kp->k_pkg_size = 3;
541 break;
542 case 0xfc:
543 kp->k_pkg_size = 7;
544 break;
545 case 0xfe:
546 case 0xff:
547 kp->k_pkg_size = 2;
548 break;
549 default:
550 printf("kbd: Unknown packet 0x%x\n", msg_start);
551 break;
552 }
553 }
554