kbd.c revision 1.30 1 1.30 is /* $NetBSD: kbd.c,v 1.30 1998/02/23 00:47:31 is Exp $ */
2 1.11 cgd
3 1.1 mw /*
4 1.1 mw * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
5 1.1 mw * All rights reserved.
6 1.1 mw *
7 1.1 mw * Redistribution and use in source and binary forms, with or without
8 1.1 mw * modification, are permitted provided that the following conditions
9 1.1 mw * are met:
10 1.1 mw * 1. Redistributions of source code must retain the above copyright
11 1.1 mw * notice, this list of conditions and the following disclaimer.
12 1.1 mw * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mw * notice, this list of conditions and the following disclaimer in the
14 1.1 mw * documentation and/or other materials provided with the distribution.
15 1.1 mw * 3. All advertising materials mentioning features or use of this software
16 1.1 mw * must display the following acknowledgement:
17 1.1 mw * This product includes software developed by the University of
18 1.1 mw * California, Berkeley and its contributors.
19 1.1 mw * 4. Neither the name of the University nor the names of its contributors
20 1.1 mw * may be used to endorse or promote products derived from this software
21 1.1 mw * without specific prior written permission.
22 1.1 mw *
23 1.1 mw * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mw * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mw * SUCH DAMAGE.
34 1.1 mw *
35 1.4 mw * kbd.c
36 1.1 mw */
37 1.7 chopps #include <sys/param.h>
38 1.7 chopps #include <sys/systm.h>
39 1.9 chopps #include <sys/device.h>
40 1.7 chopps #include <sys/ioctl.h>
41 1.7 chopps #include <sys/tty.h>
42 1.7 chopps #include <sys/proc.h>
43 1.7 chopps #include <sys/file.h>
44 1.7 chopps #include <sys/kernel.h>
45 1.7 chopps #include <sys/syslog.h>
46 1.18 veego #include <sys/signalvar.h>
47 1.8 chopps #include <dev/cons.h>
48 1.7 chopps #include <machine/cpu.h>
49 1.9 chopps #include <amiga/amiga/device.h>
50 1.7 chopps #include <amiga/amiga/custom.h>
51 1.19 is #ifdef DRACO
52 1.19 is #include <amiga/amiga/drcustom.h>
53 1.19 is #endif
54 1.7 chopps #include <amiga/amiga/cia.h>
55 1.9 chopps #include <amiga/dev/itevar.h>
56 1.9 chopps #include <amiga/dev/kbdreg.h>
57 1.18 veego #include <amiga/dev/kbdmap.h>
58 1.7 chopps #include <amiga/dev/event_var.h>
59 1.7 chopps #include <amiga/dev/vuid_event.h>
60 1.9 chopps #include "kbd.h"
61 1.5 mw
62 1.18 veego #include <sys/conf.h>
63 1.18 veego #include <machine/conf.h>
64 1.18 veego
65 1.5 mw struct kbd_softc {
66 1.9 chopps int k_event_mode; /* if true, collect events, else pass to ite */
67 1.9 chopps struct evvar k_events; /* event queue state */
68 1.19 is #ifdef DRACO
69 1.19 is u_char k_rlprfx; /* MF-II rel. prefix has been seen */
70 1.19 is #endif
71 1.9 chopps };
72 1.9 chopps struct kbd_softc kbd_softc;
73 1.9 chopps
74 1.28 veego int kbdmatch __P((struct device *, struct cfdata *, void *));
75 1.17 mhitch void kbdattach __P((struct device *, struct device *, void *));
76 1.18 veego void kbdintr __P((int));
77 1.19 is void kbdstuffchar __P((u_char));
78 1.9 chopps
79 1.16 thorpej struct cfattach kbd_ca = {
80 1.16 thorpej sizeof(struct device), kbdmatch, kbdattach
81 1.16 thorpej };
82 1.9 chopps
83 1.9 chopps /*ARGSUSED*/
84 1.9 chopps int
85 1.28 veego kbdmatch(pdp, cfp, auxp)
86 1.9 chopps struct device *pdp;
87 1.28 veego struct cfdata *cfp;
88 1.28 veego void *auxp;
89 1.9 chopps {
90 1.16 thorpej
91 1.9 chopps if (matchname((char *)auxp, "kbd"))
92 1.9 chopps return(1);
93 1.9 chopps return(0);
94 1.9 chopps }
95 1.9 chopps
96 1.9 chopps /*ARGSUSED*/
97 1.9 chopps void
98 1.9 chopps kbdattach(pdp, dp, auxp)
99 1.9 chopps struct device *pdp, *dp;
100 1.9 chopps void *auxp;
101 1.9 chopps {
102 1.19 is #ifdef DRACO
103 1.19 is /*
104 1.19 is * XXX Must be kept in sync with kbdenable() switch.
105 1.19 is * XXX This should be probed, but this way we dont need to initialize
106 1.19 is * the keyboards.
107 1.19 is */
108 1.19 is switch (is_draco()) {
109 1.19 is case 0:
110 1.19 is case 1:
111 1.19 is case 2:
112 1.26 christos printf(": CIA A type Amiga\n");
113 1.19 is break;
114 1.19 is case 3:
115 1.19 is case 4:
116 1.19 is default:
117 1.26 christos printf(": QuickLogic type MF-II\n");
118 1.19 is break;
119 1.19 is }
120 1.19 is #else
121 1.26 christos printf(": CIA A type Amiga\n");
122 1.19 is #endif
123 1.19 is
124 1.9 chopps }
125 1.5 mw
126 1.5 mw /* definitions for amiga keyboard encoding. */
127 1.5 mw #define KEY_CODE(c) ((c) & 0x7f)
128 1.5 mw #define KEY_UP(c) ((c) & 0x80)
129 1.5 mw
130 1.1 mw void
131 1.18 veego kbdenable()
132 1.1 mw {
133 1.30 is static int kbd_inited = 0;
134 1.30 is
135 1.9 chopps int s;
136 1.22 is
137 1.21 is #ifdef DRACO
138 1.21 is u_char c;
139 1.21 is #endif
140 1.9 chopps /*
141 1.9 chopps * collides with external ints from SCSI, watch out for this when
142 1.9 chopps * enabling/disabling interrupts there !!
143 1.9 chopps */
144 1.30 is s = splhigh(); /* don't lower; might be called from early ddb */
145 1.30 is if (kbd_inited) {
146 1.30 is splx(s);
147 1.30 is return;
148 1.30 is }
149 1.30 is kbd_inited = 1;
150 1.19 is #ifdef DRACO
151 1.19 is switch (is_draco()) {
152 1.19 is case 0:
153 1.19 is custom.intena = INTF_SETCLR | INTF_PORTS;
154 1.19 is
155 1.19 is ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;
156 1.19 is /* SP interrupt enable */
157 1.19 is ciaa.cra &= ~(1<<6); /* serial line == input */
158 1.19 is break;
159 1.19 is case 1:
160 1.19 is case 2:
161 1.19 is /* XXX: tobedone: conditionally enable that one */
162 1.19 is /* XXX: for now, just enable DraCo ports and CIA */
163 1.19 is *draco_intena |= DRIRQ_INT2;
164 1.19 is ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;
165 1.19 is /* SP interrupt enable */
166 1.19 is ciaa.cra &= ~(1<<6); /* serial line == input */
167 1.19 is break;
168 1.19 is
169 1.19 is case 3:
170 1.19 is ciaa.icr = CIA_ICR_SP; /* CIA SP interrupt disable */
171 1.19 is ciaa.cra &= ~(1<<6); /* serial line == input */
172 1.19 is /* FALLTHROUGH */
173 1.19 is case 4:
174 1.19 is default:
175 1.19 is /* XXX: for now: always enable own keyboard */
176 1.19 is
177 1.19 is while (draco_ioct->io_status & DRSTAT_KBDRECV) {
178 1.19 is c = draco_ioct->io_kbddata;
179 1.19 is draco_ioct->io_kbdrst = 0;
180 1.19 is DELAY(2000);
181 1.19 is }
182 1.19 is
183 1.19 is draco_ioct->io_control &= ~DRCNTRL_KBDINTENA;
184 1.19 is break;
185 1.19 is }
186 1.19 is #else
187 1.9 chopps custom.intena = INTF_SETCLR | INTF_PORTS;
188 1.9 chopps ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP; /* SP interrupt enable */
189 1.9 chopps ciaa.cra &= ~(1<<6); /* serial line == input */
190 1.19 is #endif
191 1.9 chopps kbd_softc.k_event_mode = 0;
192 1.9 chopps kbd_softc.k_events.ev_io = 0;
193 1.9 chopps splx(s);
194 1.1 mw }
195 1.1 mw
196 1.1 mw
197 1.1 mw int
198 1.18 veego kbdopen(dev, flags, mode, p)
199 1.18 veego dev_t dev;
200 1.18 veego int flags, mode;
201 1.18 veego struct proc *p;
202 1.5 mw {
203 1.5 mw
204 1.30 is kbdenable();
205 1.18 veego if (kbd_softc.k_events.ev_io)
206 1.18 veego return EBUSY;
207 1.5 mw
208 1.18 veego kbd_softc.k_events.ev_io = p;
209 1.18 veego ev_init(&kbd_softc.k_events);
210 1.18 veego return (0);
211 1.5 mw }
212 1.5 mw
213 1.5 mw int
214 1.18 veego kbdclose(dev, flags, mode, p)
215 1.18 veego dev_t dev;
216 1.18 veego int flags, mode;
217 1.18 veego struct proc *p;
218 1.5 mw {
219 1.18 veego
220 1.18 veego /* Turn off event mode, dump the queue */
221 1.18 veego kbd_softc.k_event_mode = 0;
222 1.18 veego ev_fini(&kbd_softc.k_events);
223 1.18 veego kbd_softc.k_events.ev_io = NULL;
224 1.18 veego return (0);
225 1.5 mw }
226 1.5 mw
227 1.5 mw int
228 1.18 veego kbdread(dev, uio, flags)
229 1.18 veego dev_t dev;
230 1.18 veego struct uio *uio;
231 1.18 veego int flags;
232 1.5 mw {
233 1.18 veego return ev_read (&kbd_softc.k_events, uio, flags);
234 1.5 mw }
235 1.5 mw
236 1.5 mw int
237 1.18 veego kbdioctl(dev, cmd, data, flag, p)
238 1.18 veego dev_t dev;
239 1.18 veego u_long cmd;
240 1.18 veego register caddr_t data;
241 1.18 veego int flag;
242 1.18 veego struct proc *p;
243 1.5 mw {
244 1.18 veego register struct kbd_softc *k = &kbd_softc;
245 1.18 veego
246 1.18 veego switch (cmd) {
247 1.18 veego case KIOCTRANS:
248 1.18 veego if (*(int *)data == TR_UNTRANS_EVENT)
249 1.18 veego return 0;
250 1.18 veego break;
251 1.18 veego
252 1.18 veego case KIOCGTRANS:
253 1.18 veego /* Get translation mode */
254 1.18 veego *(int *)data = TR_UNTRANS_EVENT;
255 1.18 veego return 0;
256 1.18 veego
257 1.18 veego case KIOCSDIRECT:
258 1.18 veego k->k_event_mode = *(int *)data;
259 1.18 veego return 0;
260 1.18 veego
261 1.18 veego case FIONBIO: /* we will remove this someday (soon???) */
262 1.18 veego return 0;
263 1.18 veego
264 1.18 veego case FIOASYNC:
265 1.18 veego k->k_events.ev_async = *(int *)data != 0;
266 1.18 veego return 0;
267 1.18 veego
268 1.18 veego case TIOCSPGRP:
269 1.18 veego if (*(int *)data != k->k_events.ev_io->p_pgid)
270 1.18 veego return EPERM;
271 1.18 veego return 0;
272 1.18 veego
273 1.18 veego default:
274 1.18 veego return ENOTTY;
275 1.18 veego }
276 1.5 mw
277 1.18 veego /* We identified the ioctl, but we do not handle it. */
278 1.18 veego return EOPNOTSUPP; /* misuse, but what the heck */
279 1.5 mw }
280 1.5 mw
281 1.5 mw int
282 1.25 mhitch kbdpoll(dev, events, p)
283 1.18 veego dev_t dev;
284 1.25 mhitch int events;
285 1.18 veego struct proc *p;
286 1.5 mw {
287 1.25 mhitch return ev_poll (&kbd_softc.k_events, events, p);
288 1.5 mw }
289 1.5 mw
290 1.5 mw
291 1.18 veego void
292 1.18 veego kbdintr(mask)
293 1.18 veego int mask;
294 1.1 mw {
295 1.18 veego u_char c;
296 1.13 chopps #ifdef KBDRESET
297 1.18 veego static int reset_warn;
298 1.13 chopps #endif
299 1.1 mw
300 1.19 is /*
301 1.19 is * now only invoked from generic CIA interrupt handler if there *is*
302 1.18 veego * a keyboard interrupt pending
303 1.18 veego */
304 1.1 mw
305 1.18 veego c = ~ciaa.sdr; /* keyboard data is inverted */
306 1.18 veego /* ack */
307 1.18 veego ciaa.cra |= (1 << 6); /* serial line output */
308 1.13 chopps #ifdef KBDRESET
309 1.18 veego if (reset_warn && c == 0xf0) {
310 1.13 chopps #ifdef DEBUG
311 1.26 christos printf ("kbdintr: !!!! Reset Warning !!!!\n");
312 1.13 chopps #endif
313 1.18 veego bootsync();
314 1.18 veego reset_warn = 0;
315 1.18 veego DELAY(30000000);
316 1.18 veego }
317 1.13 chopps #endif
318 1.18 veego /* wait 200 microseconds (for bloody Cherry keyboards..) */
319 1.18 veego DELAY(2000); /* fudge delay a bit for some keyboards */
320 1.18 veego ciaa.cra &= ~(1 << 6);
321 1.18 veego
322 1.18 veego /* process the character */
323 1.18 veego c = (c >> 1) | (c << 7); /* rotate right once */
324 1.1 mw
325 1.13 chopps #ifdef KBDRESET
326 1.18 veego if (c == 0x78) {
327 1.13 chopps #ifdef DEBUG
328 1.26 christos printf ("kbdintr: Reset Warning started\n");
329 1.13 chopps #endif
330 1.18 veego ++reset_warn;
331 1.18 veego return;
332 1.18 veego }
333 1.13 chopps #endif
334 1.19 is kbdstuffchar(c);
335 1.19 is }
336 1.19 is
337 1.19 is #ifdef DRACO
338 1.19 is /* maps MF-II keycodes to Amiga keycodes */
339 1.19 is
340 1.27 is const u_char drkbdtab[] = {
341 1.19 is 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x50,
342 1.19 is 0x45, 0xff, 0xff, 0xff, 0xff, 0x42, 0x00, 0x51,
343 1.19 is
344 1.19 is 0xff, 0x64, 0x60, 0x30, 0x63, 0x10, 0x01, 0x52,
345 1.19 is 0xff, 0x66, 0x31, 0x21, 0x20, 0x11, 0x02, 0x53,
346 1.19 is
347 1.19 is 0xff, 0x33, 0x32, 0x22, 0x12, 0x04, 0x03, 0x54,
348 1.19 is 0xff, 0x40, 0x34, 0x23, 0x14, 0x13, 0x05, 0x55,
349 1.19 is
350 1.19 is 0xff, 0x36, 0x35, 0x25, 0x24, 0x15, 0x06, 0x56,
351 1.19 is 0xff, 0x67, 0x37, 0x26, 0x16, 0x07, 0x08, 0x57,
352 1.19 is /* --- */
353 1.19 is 0xff, 0x38, 0x27, 0x17, 0x18, 0x0a, 0x09, 0x58,
354 1.19 is 0xff, 0x39, 0x3a, 0x28, 0x29, 0x19, 0x0b, 0x59,
355 1.19 is
356 1.19 is 0xff, 0xff, 0x2a, 0x2b, 0x1a, 0x0c, 0x4b, 0xff,
357 1.19 is 0x65, 0x61, 0x44, 0x1b, 0xff, 0xff, 0x6f, 0xff,
358 1.19 is
359 1.19 is 0x4d, 0x4f, 0xff, 0x4c, 0x0d, 0xff, 0x41, 0x46,
360 1.19 is 0xff, 0x1d, 0x4e, 0x2d, 0x3d, 0x4a, 0x5f, 0x62,
361 1.19 is
362 1.19 is 0x0f, 0x3c, 0x1e, 0x2e, 0x2f, 0x3e, 0x5a, 0x5b,
363 1.19 is 0xff, 0x43, 0x1f, 0xff, 0x5e, 0x3f, 0x5c, 0xff,
364 1.19 is /* --- */
365 1.19 is 0xff, 0xff, 0xff, 0xff, 0x5d
366 1.19 is };
367 1.19 is #endif
368 1.19 is
369 1.19 is
370 1.19 is int
371 1.19 is kbdgetcn ()
372 1.19 is {
373 1.19 is int s;
374 1.19 is u_char ints, mask, c, in;
375 1.19 is
376 1.19 is #ifdef DRACO
377 1.19 is /*
378 1.19 is * XXX todo: if CIA DraCo, get from cia if cia kbd
379 1.19 is * installed.
380 1.19 is */
381 1.19 is if (is_draco()) {
382 1.19 is c = 0;
383 1.19 is s = spltty ();
384 1.19 is while ((draco_ioct->io_status & DRSTAT_KBDRECV) == 0);
385 1.19 is in = draco_ioct->io_kbddata;
386 1.19 is draco_ioct->io_kbdrst = 0;
387 1.19 is if (in == 0xF0) { /* release prefix */
388 1.19 is c = 0x80;
389 1.19 is while ((draco_ioct->io_status & DRSTAT_KBDRECV) == 0);
390 1.19 is in = draco_ioct->io_kbddata;
391 1.19 is draco_ioct->io_kbdrst = 0;
392 1.19 is }
393 1.19 is splx(s);
394 1.19 is #ifdef DRACORAWKEYDEBUG
395 1.26 christos printf("<%02x>", in);
396 1.19 is #endif
397 1.19 is return (in>=sizeof(drkbdtab) ? 0xff : drkbdtab[in]|c);
398 1.19 is }
399 1.19 is #endif
400 1.19 is s = spltty();
401 1.19 is for (ints = 0; ! ((mask = ciaa.icr) & CIA_ICR_SP);
402 1.19 is ints |= mask) ;
403 1.19 is
404 1.19 is in = ciaa.sdr;
405 1.19 is c = ~in;
406 1.19 is
407 1.19 is /* ack */
408 1.19 is ciaa.cra |= (1 << 6); /* serial line output */
409 1.19 is ciaa.sdr = 0xff; /* ack */
410 1.19 is /* wait 200 microseconds */
411 1.19 is DELAY(2000); /* XXXX only works as long as DELAY doesn't
412 1.19 is * use a timer and waits.. */
413 1.19 is ciaa.cra &= ~(1 << 6);
414 1.19 is ciaa.sdr = in;
415 1.19 is
416 1.19 is splx (s);
417 1.19 is c = (c >> 1) | (c << 7);
418 1.19 is
419 1.19 is /* take care that no CIA-interrupts are lost */
420 1.19 is if (ints)
421 1.19 is dispatch_cia_ints (0, ints);
422 1.19 is
423 1.19 is return c;
424 1.19 is }
425 1.19 is
426 1.19 is void
427 1.19 is kbdstuffchar(c)
428 1.19 is u_char c;
429 1.19 is {
430 1.19 is struct firm_event *fe;
431 1.19 is struct kbd_softc *k = &kbd_softc;
432 1.19 is int put;
433 1.19 is
434 1.19 is /*
435 1.19 is * If not in event mode, deliver straight to ite to process
436 1.19 is * key stroke
437 1.19 is */
438 1.19 is
439 1.18 veego if (! k->k_event_mode) {
440 1.18 veego ite_filter (c, ITEFILT_TTY);
441 1.18 veego return;
442 1.18 veego }
443 1.18 veego
444 1.19 is /*
445 1.19 is * Keyboard is generating events. Turn this keystroke into an
446 1.19 is * event and put it in the queue. If the queue is full, the
447 1.18 veego * keystroke is lost (sorry!).
448 1.18 veego */
449 1.19 is
450 1.18 veego put = k->k_events.ev_put;
451 1.18 veego fe = &k->k_events.ev_q[put];
452 1.18 veego put = (put + 1) % EV_QSIZE;
453 1.18 veego if (put == k->k_events.ev_get) {
454 1.19 is log(LOG_WARNING, "keyboard event queue overflow\n");
455 1.19 is /* ??? */
456 1.18 veego return;
457 1.18 veego }
458 1.18 veego fe->id = KEY_CODE(c);
459 1.18 veego fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
460 1.18 veego fe->time = time;
461 1.18 veego k->k_events.ev_put = put;
462 1.18 veego EV_WAKEUP(&k->k_events);
463 1.1 mw }
464 1.1 mw
465 1.1 mw
466 1.19 is #ifdef DRACO
467 1.19 is void
468 1.19 is drkbdintr()
469 1.1 mw {
470 1.19 is u_char in;
471 1.19 is struct kbd_softc *k = &kbd_softc;
472 1.18 veego
473 1.19 is in = draco_ioct->io_kbddata;
474 1.19 is draco_ioct->io_kbdrst = 0;
475 1.1 mw
476 1.19 is if (in == 0xF0)
477 1.19 is k->k_rlprfx = 0x80;
478 1.19 is else {
479 1.19 is kbdstuffchar(in>=sizeof(drkbdtab) ? 0xff :
480 1.19 is drkbdtab[in] | k->k_rlprfx);
481 1.19 is k->k_rlprfx = 0;
482 1.19 is }
483 1.19 is }
484 1.1 mw
485 1.19 is #endif
486