kbd.c revision 1.26 1 1.26 christos /* $NetBSD: kbd.c,v 1.26 1996/10/13 03:07:22 christos 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.17 mhitch int kbdmatch __P((struct device *, void *, 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.16 thorpej
83 1.16 thorpej struct cfdriver kbd_cd = {
84 1.16 thorpej NULL, "kbd", DV_DULL, NULL, 0
85 1.16 thorpej };
86 1.9 chopps
87 1.9 chopps /*ARGSUSED*/
88 1.9 chopps int
89 1.16 thorpej kbdmatch(pdp, match, auxp)
90 1.9 chopps struct device *pdp;
91 1.16 thorpej void *match, *auxp;
92 1.9 chopps {
93 1.16 thorpej
94 1.9 chopps if (matchname((char *)auxp, "kbd"))
95 1.9 chopps return(1);
96 1.9 chopps return(0);
97 1.9 chopps }
98 1.9 chopps
99 1.9 chopps /*ARGSUSED*/
100 1.9 chopps void
101 1.9 chopps kbdattach(pdp, dp, auxp)
102 1.9 chopps struct device *pdp, *dp;
103 1.9 chopps void *auxp;
104 1.9 chopps {
105 1.19 is #ifdef DRACO
106 1.19 is /*
107 1.19 is * XXX Must be kept in sync with kbdenable() switch.
108 1.19 is * XXX This should be probed, but this way we dont need to initialize
109 1.19 is * the keyboards.
110 1.19 is */
111 1.19 is switch (is_draco()) {
112 1.19 is case 0:
113 1.19 is case 1:
114 1.19 is case 2:
115 1.26 christos printf(": CIA A type Amiga\n");
116 1.19 is break;
117 1.19 is case 3:
118 1.19 is case 4:
119 1.19 is default:
120 1.26 christos printf(": QuickLogic type MF-II\n");
121 1.19 is break;
122 1.19 is }
123 1.19 is #else
124 1.26 christos printf(": CIA A type Amiga\n");
125 1.19 is #endif
126 1.19 is
127 1.9 chopps }
128 1.5 mw
129 1.5 mw /* definitions for amiga keyboard encoding. */
130 1.5 mw #define KEY_CODE(c) ((c) & 0x7f)
131 1.5 mw #define KEY_UP(c) ((c) & 0x80)
132 1.5 mw
133 1.1 mw void
134 1.18 veego kbdenable()
135 1.1 mw {
136 1.9 chopps int s;
137 1.22 is
138 1.21 is #ifdef DRACO
139 1.21 is u_char c;
140 1.21 is #endif
141 1.9 chopps /*
142 1.9 chopps * collides with external ints from SCSI, watch out for this when
143 1.9 chopps * enabling/disabling interrupts there !!
144 1.9 chopps */
145 1.9 chopps s = spltty();
146 1.19 is #ifdef DRACO
147 1.19 is switch (is_draco()) {
148 1.19 is case 0:
149 1.19 is custom.intena = INTF_SETCLR | INTF_PORTS;
150 1.19 is
151 1.19 is ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;
152 1.19 is /* SP interrupt enable */
153 1.19 is ciaa.cra &= ~(1<<6); /* serial line == input */
154 1.19 is break;
155 1.19 is case 1:
156 1.19 is case 2:
157 1.19 is /* XXX: tobedone: conditionally enable that one */
158 1.19 is /* XXX: for now, just enable DraCo ports and CIA */
159 1.19 is *draco_intena |= DRIRQ_INT2;
160 1.19 is ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;
161 1.19 is /* SP interrupt enable */
162 1.19 is ciaa.cra &= ~(1<<6); /* serial line == input */
163 1.19 is break;
164 1.19 is
165 1.19 is case 3:
166 1.19 is ciaa.icr = CIA_ICR_SP; /* CIA SP interrupt disable */
167 1.19 is ciaa.cra &= ~(1<<6); /* serial line == input */
168 1.19 is /* FALLTHROUGH */
169 1.19 is case 4:
170 1.19 is default:
171 1.19 is /* XXX: for now: always enable own keyboard */
172 1.19 is
173 1.19 is while (draco_ioct->io_status & DRSTAT_KBDRECV) {
174 1.19 is c = draco_ioct->io_kbddata;
175 1.19 is draco_ioct->io_kbdrst = 0;
176 1.19 is DELAY(2000);
177 1.19 is }
178 1.19 is
179 1.19 is draco_ioct->io_control &= ~DRCNTRL_KBDINTENA;
180 1.19 is break;
181 1.19 is }
182 1.19 is #else
183 1.9 chopps custom.intena = INTF_SETCLR | INTF_PORTS;
184 1.9 chopps ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP; /* SP interrupt enable */
185 1.9 chopps ciaa.cra &= ~(1<<6); /* serial line == input */
186 1.19 is #endif
187 1.9 chopps kbd_softc.k_event_mode = 0;
188 1.9 chopps kbd_softc.k_events.ev_io = 0;
189 1.9 chopps splx(s);
190 1.1 mw }
191 1.1 mw
192 1.1 mw
193 1.1 mw int
194 1.18 veego kbdopen(dev, flags, mode, p)
195 1.18 veego dev_t dev;
196 1.18 veego int flags, mode;
197 1.18 veego struct proc *p;
198 1.5 mw {
199 1.5 mw
200 1.18 veego if (kbd_softc.k_events.ev_io)
201 1.18 veego return EBUSY;
202 1.5 mw
203 1.18 veego kbd_softc.k_events.ev_io = p;
204 1.18 veego ev_init(&kbd_softc.k_events);
205 1.18 veego return (0);
206 1.5 mw }
207 1.5 mw
208 1.5 mw int
209 1.18 veego kbdclose(dev, flags, mode, p)
210 1.18 veego dev_t dev;
211 1.18 veego int flags, mode;
212 1.18 veego struct proc *p;
213 1.5 mw {
214 1.18 veego
215 1.18 veego /* Turn off event mode, dump the queue */
216 1.18 veego kbd_softc.k_event_mode = 0;
217 1.18 veego ev_fini(&kbd_softc.k_events);
218 1.18 veego kbd_softc.k_events.ev_io = NULL;
219 1.18 veego return (0);
220 1.5 mw }
221 1.5 mw
222 1.5 mw int
223 1.18 veego kbdread(dev, uio, flags)
224 1.18 veego dev_t dev;
225 1.18 veego struct uio *uio;
226 1.18 veego int flags;
227 1.5 mw {
228 1.18 veego return ev_read (&kbd_softc.k_events, uio, flags);
229 1.5 mw }
230 1.5 mw
231 1.5 mw int
232 1.18 veego kbdioctl(dev, cmd, data, flag, p)
233 1.18 veego dev_t dev;
234 1.18 veego u_long cmd;
235 1.18 veego register caddr_t data;
236 1.18 veego int flag;
237 1.18 veego struct proc *p;
238 1.5 mw {
239 1.18 veego register struct kbd_softc *k = &kbd_softc;
240 1.18 veego
241 1.18 veego switch (cmd) {
242 1.18 veego case KIOCTRANS:
243 1.18 veego if (*(int *)data == TR_UNTRANS_EVENT)
244 1.18 veego return 0;
245 1.18 veego break;
246 1.18 veego
247 1.18 veego case KIOCGTRANS:
248 1.18 veego /* Get translation mode */
249 1.18 veego *(int *)data = TR_UNTRANS_EVENT;
250 1.18 veego return 0;
251 1.18 veego
252 1.18 veego case KIOCSDIRECT:
253 1.18 veego k->k_event_mode = *(int *)data;
254 1.18 veego return 0;
255 1.18 veego
256 1.18 veego case FIONBIO: /* we will remove this someday (soon???) */
257 1.18 veego return 0;
258 1.18 veego
259 1.18 veego case FIOASYNC:
260 1.18 veego k->k_events.ev_async = *(int *)data != 0;
261 1.18 veego return 0;
262 1.18 veego
263 1.18 veego case TIOCSPGRP:
264 1.18 veego if (*(int *)data != k->k_events.ev_io->p_pgid)
265 1.18 veego return EPERM;
266 1.18 veego return 0;
267 1.18 veego
268 1.18 veego default:
269 1.18 veego return ENOTTY;
270 1.18 veego }
271 1.5 mw
272 1.18 veego /* We identified the ioctl, but we do not handle it. */
273 1.18 veego return EOPNOTSUPP; /* misuse, but what the heck */
274 1.5 mw }
275 1.5 mw
276 1.5 mw int
277 1.25 mhitch kbdpoll(dev, events, p)
278 1.18 veego dev_t dev;
279 1.25 mhitch int events;
280 1.18 veego struct proc *p;
281 1.5 mw {
282 1.25 mhitch return ev_poll (&kbd_softc.k_events, events, p);
283 1.5 mw }
284 1.5 mw
285 1.5 mw
286 1.18 veego void
287 1.18 veego kbdintr(mask)
288 1.18 veego int mask;
289 1.1 mw {
290 1.18 veego u_char c;
291 1.13 chopps #ifdef KBDRESET
292 1.18 veego static int reset_warn;
293 1.13 chopps #endif
294 1.1 mw
295 1.19 is /*
296 1.19 is * now only invoked from generic CIA interrupt handler if there *is*
297 1.18 veego * a keyboard interrupt pending
298 1.18 veego */
299 1.1 mw
300 1.18 veego c = ~ciaa.sdr; /* keyboard data is inverted */
301 1.18 veego /* ack */
302 1.18 veego ciaa.cra |= (1 << 6); /* serial line output */
303 1.13 chopps #ifdef KBDRESET
304 1.18 veego if (reset_warn && c == 0xf0) {
305 1.13 chopps #ifdef DEBUG
306 1.26 christos printf ("kbdintr: !!!! Reset Warning !!!!\n");
307 1.13 chopps #endif
308 1.18 veego bootsync();
309 1.18 veego reset_warn = 0;
310 1.18 veego DELAY(30000000);
311 1.18 veego }
312 1.13 chopps #endif
313 1.18 veego /* wait 200 microseconds (for bloody Cherry keyboards..) */
314 1.18 veego DELAY(2000); /* fudge delay a bit for some keyboards */
315 1.18 veego ciaa.cra &= ~(1 << 6);
316 1.18 veego
317 1.18 veego /* process the character */
318 1.18 veego c = (c >> 1) | (c << 7); /* rotate right once */
319 1.1 mw
320 1.13 chopps #ifdef KBDRESET
321 1.18 veego if (c == 0x78) {
322 1.13 chopps #ifdef DEBUG
323 1.26 christos printf ("kbdintr: Reset Warning started\n");
324 1.13 chopps #endif
325 1.18 veego ++reset_warn;
326 1.18 veego return;
327 1.18 veego }
328 1.13 chopps #endif
329 1.19 is kbdstuffchar(c);
330 1.19 is }
331 1.19 is
332 1.19 is #ifdef DRACO
333 1.19 is /* maps MF-II keycodes to Amiga keycodes */
334 1.19 is
335 1.19 is u_char drkbdtab[] = {
336 1.19 is 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x50,
337 1.19 is 0x45, 0xff, 0xff, 0xff, 0xff, 0x42, 0x00, 0x51,
338 1.19 is
339 1.19 is 0xff, 0x64, 0x60, 0x30, 0x63, 0x10, 0x01, 0x52,
340 1.19 is 0xff, 0x66, 0x31, 0x21, 0x20, 0x11, 0x02, 0x53,
341 1.19 is
342 1.19 is 0xff, 0x33, 0x32, 0x22, 0x12, 0x04, 0x03, 0x54,
343 1.19 is 0xff, 0x40, 0x34, 0x23, 0x14, 0x13, 0x05, 0x55,
344 1.19 is
345 1.19 is 0xff, 0x36, 0x35, 0x25, 0x24, 0x15, 0x06, 0x56,
346 1.19 is 0xff, 0x67, 0x37, 0x26, 0x16, 0x07, 0x08, 0x57,
347 1.19 is /* --- */
348 1.19 is 0xff, 0x38, 0x27, 0x17, 0x18, 0x0a, 0x09, 0x58,
349 1.19 is 0xff, 0x39, 0x3a, 0x28, 0x29, 0x19, 0x0b, 0x59,
350 1.19 is
351 1.19 is 0xff, 0xff, 0x2a, 0x2b, 0x1a, 0x0c, 0x4b, 0xff,
352 1.19 is 0x65, 0x61, 0x44, 0x1b, 0xff, 0xff, 0x6f, 0xff,
353 1.19 is
354 1.19 is 0x4d, 0x4f, 0xff, 0x4c, 0x0d, 0xff, 0x41, 0x46,
355 1.19 is 0xff, 0x1d, 0x4e, 0x2d, 0x3d, 0x4a, 0x5f, 0x62,
356 1.19 is
357 1.19 is 0x0f, 0x3c, 0x1e, 0x2e, 0x2f, 0x3e, 0x5a, 0x5b,
358 1.19 is 0xff, 0x43, 0x1f, 0xff, 0x5e, 0x3f, 0x5c, 0xff,
359 1.19 is /* --- */
360 1.19 is 0xff, 0xff, 0xff, 0xff, 0x5d
361 1.19 is };
362 1.19 is #endif
363 1.19 is
364 1.19 is
365 1.19 is int
366 1.19 is kbdgetcn ()
367 1.19 is {
368 1.19 is int s;
369 1.19 is u_char ints, mask, c, in;
370 1.19 is
371 1.19 is #ifdef DRACO
372 1.19 is /*
373 1.19 is * XXX todo: if CIA DraCo, get from cia if cia kbd
374 1.19 is * installed.
375 1.19 is */
376 1.19 is if (is_draco()) {
377 1.19 is c = 0;
378 1.19 is s = spltty ();
379 1.19 is while ((draco_ioct->io_status & DRSTAT_KBDRECV) == 0);
380 1.19 is in = draco_ioct->io_kbddata;
381 1.19 is draco_ioct->io_kbdrst = 0;
382 1.19 is if (in == 0xF0) { /* release prefix */
383 1.19 is c = 0x80;
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 }
388 1.19 is splx(s);
389 1.19 is #ifdef DRACORAWKEYDEBUG
390 1.26 christos printf("<%02x>", in);
391 1.19 is #endif
392 1.19 is return (in>=sizeof(drkbdtab) ? 0xff : drkbdtab[in]|c);
393 1.19 is }
394 1.19 is #endif
395 1.19 is s = spltty();
396 1.19 is for (ints = 0; ! ((mask = ciaa.icr) & CIA_ICR_SP);
397 1.19 is ints |= mask) ;
398 1.19 is
399 1.19 is in = ciaa.sdr;
400 1.19 is c = ~in;
401 1.19 is
402 1.19 is /* ack */
403 1.19 is ciaa.cra |= (1 << 6); /* serial line output */
404 1.19 is ciaa.sdr = 0xff; /* ack */
405 1.19 is /* wait 200 microseconds */
406 1.19 is DELAY(2000); /* XXXX only works as long as DELAY doesn't
407 1.19 is * use a timer and waits.. */
408 1.19 is ciaa.cra &= ~(1 << 6);
409 1.19 is ciaa.sdr = in;
410 1.19 is
411 1.19 is splx (s);
412 1.19 is c = (c >> 1) | (c << 7);
413 1.19 is
414 1.19 is /* take care that no CIA-interrupts are lost */
415 1.19 is if (ints)
416 1.19 is dispatch_cia_ints (0, ints);
417 1.19 is
418 1.19 is return c;
419 1.19 is }
420 1.19 is
421 1.19 is void
422 1.19 is kbdstuffchar(c)
423 1.19 is u_char c;
424 1.19 is {
425 1.19 is struct firm_event *fe;
426 1.19 is struct kbd_softc *k = &kbd_softc;
427 1.19 is int put;
428 1.19 is
429 1.19 is /*
430 1.19 is * If not in event mode, deliver straight to ite to process
431 1.19 is * key stroke
432 1.19 is */
433 1.19 is
434 1.18 veego if (! k->k_event_mode) {
435 1.18 veego ite_filter (c, ITEFILT_TTY);
436 1.18 veego return;
437 1.18 veego }
438 1.18 veego
439 1.19 is /*
440 1.19 is * Keyboard is generating events. Turn this keystroke into an
441 1.19 is * event and put it in the queue. If the queue is full, the
442 1.18 veego * keystroke is lost (sorry!).
443 1.18 veego */
444 1.19 is
445 1.18 veego put = k->k_events.ev_put;
446 1.18 veego fe = &k->k_events.ev_q[put];
447 1.18 veego put = (put + 1) % EV_QSIZE;
448 1.18 veego if (put == k->k_events.ev_get) {
449 1.19 is log(LOG_WARNING, "keyboard event queue overflow\n");
450 1.19 is /* ??? */
451 1.18 veego return;
452 1.18 veego }
453 1.18 veego fe->id = KEY_CODE(c);
454 1.18 veego fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
455 1.18 veego fe->time = time;
456 1.18 veego k->k_events.ev_put = put;
457 1.18 veego EV_WAKEUP(&k->k_events);
458 1.1 mw }
459 1.1 mw
460 1.1 mw
461 1.19 is #ifdef DRACO
462 1.19 is void
463 1.19 is drkbdintr()
464 1.1 mw {
465 1.19 is u_char in;
466 1.19 is struct kbd_softc *k = &kbd_softc;
467 1.18 veego
468 1.19 is in = draco_ioct->io_kbddata;
469 1.19 is draco_ioct->io_kbdrst = 0;
470 1.1 mw
471 1.19 is if (in == 0xF0)
472 1.19 is k->k_rlprfx = 0x80;
473 1.19 is else {
474 1.19 is kbdstuffchar(in>=sizeof(drkbdtab) ? 0xff :
475 1.19 is drkbdtab[in] | k->k_rlprfx);
476 1.19 is k->k_rlprfx = 0;
477 1.19 is }
478 1.19 is }
479 1.1 mw
480 1.19 is #endif
481