vrkiu.c revision 1.4 1 /* $NetBSD: vrkiu.c,v 1.4 1999/11/05 04:28:14 takemura Exp $ */
2
3 /*-
4 * Copyright (c) 1999 SASAKI Takesi All rights reserved.
5 * Copyright (c) 1999 TAKEMRUA, Shin All rights reserved.
6 * Copyright (c) 1999 PocketBSD Project. 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 PocketBSD project
19 * and its contributors.
20 * 4. Neither the name of the project 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
38 #define VRKIUDEBUG
39
40 #include <sys/param.h>
41 #include <sys/tty.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/conf.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47
48 #include <machine/intr.h>
49 #include <machine/cpu.h>
50 #include <machine/bus.h>
51 #include <machine/platid.h>
52
53 #include <hpcmips/vr/vr.h>
54 #include <hpcmips/vr/vripvar.h>
55 #include <hpcmips/vr/vrkiuvar.h>
56 #include <hpcmips/vr/vrkiureg.h>
57 #include <hpcmips/vr/icureg.h>
58
59 #include <dev/wscons/wsconsio.h>
60 #include <dev/wscons/wskbdvar.h>
61 #include <dev/wscons/wsksymdef.h>
62 #include <dev/wscons/wsksymvar.h>
63 #include <dev/pckbc/wskbdmap_mfii.h>
64
65 #ifdef VRKIUDEBUG
66 int vrkiu_debug = 0;
67 #define DPRINTF(arg) if (vrkiu_debug) printf arg;
68 #else
69 #define DPRINTF(arg)
70 #endif
71
72 /*
73 * structure and data types
74 */
75 struct vrkiu_chip {
76 bus_space_tag_t kc_iot;
77 bus_space_handle_t kc_ioh;
78 unsigned short kc_scandata[KIU_NSCANLINE/2];
79 int kc_polling;
80 u_int kc_type;
81 int kc_data;
82
83 int kc_sft:1, kc_alt:1, kc_ctrl:1;
84
85 struct vrkiu_softc* kc_sc; /* back link */
86 };
87
88 struct vrkiu_softc {
89 struct device sc_dev;
90 struct vrkiu_chip *sc_chip;
91 struct vrkiu_chip sc_chip_body;
92 int sc_enabled;
93 struct device *sc_wskbddev;
94
95 void *sc_handler;
96 #define NKEYBUF 32
97 unsigned char keybuf[NKEYBUF];
98 int keybufhead, keybuftail;
99 };
100
101 /*
102 * function prototypes
103 */
104 static int vrkiumatch __P((struct device *, struct cfdata *, void *));
105 static void vrkiuattach __P((struct device *, struct device *, void *));
106
107 int vrkiu_intr __P((void *));
108
109 static int vrkiu_init(struct vrkiu_chip*, bus_space_tag_t, bus_space_handle_t);
110 static void vrkiu_write __P((struct vrkiu_chip *, int, unsigned short));
111 static unsigned short vrkiu_read __P((struct vrkiu_chip *, int));
112 static int vrkiu_is_console(bus_space_tag_t, bus_space_handle_t);
113 static void detect_key __P((struct vrkiu_chip *));
114
115 static struct vrkiu_softc *the_vrkiu = NULL; /* XXX: kludge!! */
116
117 /* wskbd accessopts */
118 int vrkiu_enable __P((void *, int));
119 void vrkiu_set_leds __P((void *, int));
120 int vrkiu_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
121
122 /* consopts */
123 void vrkiu_cngetc __P((void*, u_int*, int*));
124 void vrkiu_cnpollc __P((void *, int));
125
126 /*
127 * global/static data
128 */
129 struct cfattach vrkiu_ca = {
130 sizeof(struct vrkiu_softc), vrkiumatch, vrkiuattach
131 };
132
133 const struct wskbd_accessops vrkiu_accessops = {
134 vrkiu_enable,
135 vrkiu_set_leds,
136 vrkiu_ioctl,
137 };
138
139 const struct wskbd_consops vrkiu_consops = {
140 vrkiu_cngetc,
141 vrkiu_cnpollc,
142 };
143
144 const struct wskbd_mapdata vrkiu_keymapdata = {
145 pckbd_keydesctab,
146 KB_US,
147 };
148
149 struct vrkiu_chip *vrkiu_consdata = NULL;
150
151 /* XXX: This tranlation table may depend on each machine.
152 Should I build it in? */
153 static char keytrans[] = {
154 -1, 28, 25, 52, 21, 48, 44, 57, /* - enter p . y b z space */
155 -1, 53, 24, 51, 20, 47, 30, -1, /* - / o , t v a - */
156 -1, -1, 23, 50, 19, 46, 17, -1, /* - - i m r c w - */
157 13, -1, 22, -1, 18, 45, 16, 2, /* = - u - e x q 1 */
158 -1, -1, 11, 38, 40, 34, 15, 59, /* - - 0 l ' g tab f1 */
159 -1, 39, 10, 49, 6, 33, 3, 37, /* - ; 9 n 5 f 2 k */
160 -1, 27, 9, 36, 5, 32, 7, -1, /* - ] 8 j 4 d 6 - */
161 12, 26, 8, 35, 4, 41, 31, -1, /* - [ 7 h 3 ` s - */
162 58, -1, -1, -1, 14, -1, 66, 61, /* caps - - - bs - f8 f3 */
163 -1, 56, -1, -1, 43, -1, 65, 62, /* - alt - - \ - f7 f4 */
164 -1, -1, 29, -1, 68, -1, 64, 60, /* - - ctrl - f10 - f6 f2 */
165 -1, -1, -1, 42, -1, 67, 63, 1, /* - - - shift - f9 f5 esc */
166 };
167 /* XXX: fill the field of funct. keys, ex. arrow, fnc, nfer... */
168
169 #define SCROLL 0x0001 /* stop output */
170 #define NUM 0x0002 /* numeric shift cursors vs. numeric */
171 #define CAPS 0x0004 /* caps shift -- swaps case of letter */
172 #define SHIFT 0x0008 /* keyboard shift */
173 #define CTL 0x0010 /* control shift -- allows ctl function */
174 #define ASCII 0x0020 /* ascii code for this key */
175 #define ALT 0x0080 /* alternate shift -- alternate chars */
176 #define FUNC 0x0100 /* function key */
177 #define KP 0x0200 /* Keypad keys */
178 #define NONE 0x0400 /* no function */
179
180 /*
181 * utilities
182 */
183 static inline void
184 vrkiu_write(chip, port, val)
185 struct vrkiu_chip *chip;
186 int port;
187 unsigned short val;
188 {
189 bus_space_write_2(chip->kc_iot, chip->kc_ioh, port, val);
190 }
191
192 static inline unsigned short
193 vrkiu_read(chip, port)
194 struct vrkiu_chip *chip;
195 int port;
196 {
197 return bus_space_read_2(chip->kc_iot, chip->kc_ioh, port);
198 }
199
200 static inline int
201 vrkiu_is_console(iot, ioh)
202 bus_space_tag_t iot;
203 bus_space_handle_t ioh;
204 {
205 if (vrkiu_consdata &&
206 vrkiu_consdata->kc_iot == iot &&
207 vrkiu_consdata->kc_ioh == ioh) {
208 return 1;
209 } else {
210 return 0;
211 }
212 }
213
214 /*
215 * initialize device
216 */
217 static int
218 vrkiu_init(chip, iot, ioh)
219 struct vrkiu_chip* chip;
220 bus_space_tag_t iot;
221 bus_space_handle_t ioh;
222 {
223 memset(chip, 0, sizeof(struct vrkiu_chip));
224 chip->kc_iot = iot;
225 chip->kc_ioh = ioh;
226 chip->kc_polling = 0;
227
228 /* set KIU */
229 vrkiu_write(chip, KIURST, 1); /* reset */
230 vrkiu_write(chip, KIUSCANLINE, 0); /* 96keys */
231 vrkiu_write(chip, KIUWKS, 0x18a4); /* XXX: scan timing! */
232 vrkiu_write(chip, KIUWKI, 450);
233 vrkiu_write(chip, KIUSCANREP, 0x8023);
234 /* KEYEN | STPREP = 2 | ATSTP | ATSCAN */
235 return 0;
236 }
237
238 /*
239 * probe
240 */
241 static int
242 vrkiumatch(parent, cf, aux)
243 struct device *parent;
244 struct cfdata *cf;
245 void *aux;
246 {
247 return 1;
248 }
249
250 /*
251 * attach
252 */
253 static void
254 vrkiuattach(parent, self, aux)
255 struct device *parent;
256 struct device *self;
257 void *aux;
258 {
259 struct vrkiu_softc *sc = (struct vrkiu_softc *)self;
260 struct vrip_attach_args *va = aux;
261 struct wskbddev_attach_args wa;
262 int isconsole;
263
264 bus_space_tag_t iot = va->va_iot;
265 bus_space_handle_t ioh;
266
267 if (bus_space_map(iot, va->va_addr, 1, 0, &ioh)) {
268 printf(": can't map bus space\n");
269 return;
270 }
271
272 isconsole = vrkiu_is_console(iot, ioh);
273 if (isconsole) {
274 sc->sc_chip = vrkiu_consdata;
275 } else {
276 sc->sc_chip = &sc->sc_chip_body;
277 vrkiu_init(sc->sc_chip, iot, ioh);
278 }
279 sc->sc_chip->kc_sc = sc;
280
281 if (!(sc->sc_handler =
282 vrip_intr_establish(va->va_vc, va->va_intr, IPL_TTY,
283 vrkiu_intr, sc))) {
284 printf (": can't map interrupt line.\n");
285 return;
286 }
287 /* Level2 register setting */
288 vrip_intr_setmask2(va->va_vc, sc->sc_handler, KIUINT_KDATRDY, 1);
289
290 printf("\n");
291
292 wa.console = isconsole;
293 wa.keymap = &vrkiu_keymapdata;
294 wa.accessops = &vrkiu_accessops;
295 wa.accesscookie = sc;
296
297 sc->sc_wskbddev = config_found(self, &wa, wskbddevprint);
298 }
299
300 int
301 vrkiu_intr(arg)
302 void *arg;
303 {
304 struct vrkiu_softc *sc = arg;
305
306 /* When key scan finisshed, this entry is called. */
307 DPRINTF(("%s(%d): vrkiu_intr: %d\n",
308 __FILE__, __LINE__,
309 vrkiu_read(sc->sc_chip, KIUINT) & 7));
310
311 /*
312 * First, we must clear the interrupt register because
313 * detect_key() may takes long time if a bitmap screen
314 * scrolls up and it makes us to miss some key release
315 * event.
316 */
317 vrkiu_write(sc->sc_chip, KIUINT, 0x7); /* Clear all interrupt */
318 detect_key(sc->sc_chip);
319
320 return 0;
321 }
322
323 static void
324 detect_key(chip)
325 struct vrkiu_chip* chip;
326 {
327 int i, j, modified, mask;
328 unsigned short scandata[KIU_NSCANLINE/2];
329
330 for (i = 0; i < KIU_NSCANLINE / 2; i++) {
331 scandata[i] = vrkiu_read(chip, KIUDATP + i * 2);
332 }
333
334 DPRINTF(("%s(%d): detect_key():", __FILE__, __LINE__));
335
336 if (chip->kc_polling) {
337 chip->kc_type = WSCONS_EVENT_ALL_KEYS_UP;
338 }
339
340 for (i = 0; i < KIU_NSCANLINE / 2; i++) {
341 modified = scandata[i] ^ chip->kc_scandata[i];
342 mask = 1;
343 for (j = 0; j < 16; j++, mask <<= 1) {
344 /* XXX: The order of keys can be a problem.
345 If CTRL and normal key are pushed simultaneously,
346 normal key can be entered in queue first.
347 Same problem would occur in key break. */
348 if (modified & mask) {
349 int key, type;
350 key = i * 16 + j;
351 if (keytrans[key] < 0) {
352 printf("vrkiu: Unkown scan code 0x%02x\n", key);
353 continue;
354 }
355 type = (scandata[i] & mask) ?
356 WSCONS_EVENT_KEY_DOWN :
357 WSCONS_EVENT_KEY_UP;
358 DPRINTF(("(%d,%d)=%s%d ", i, j,
359 (scandata[i] & mask) ? "v" : "^",
360 keytrans[key]));
361 if (chip->kc_polling) {
362 chip->kc_type = type;
363 chip->kc_data = keytrans[key];
364 } else {
365 wskbd_input(chip->kc_sc->sc_wskbddev,
366 type,
367 keytrans[key]);
368 }
369 }
370 }
371 chip->kc_scandata[i] = scandata[i];
372 }
373 DPRINTF(("\n"));
374 }
375
376 /* called from biconsdev.c */
377 int
378 vrkiu_getc()
379 {
380 int ret;
381
382 if (the_vrkiu == NULL) {
383 return 0; /* XXX */
384 }
385
386 while (the_vrkiu->keybuftail == the_vrkiu->keybufhead) {
387 detect_key(vrkiu_consdata);
388 }
389 ret = the_vrkiu->keybuf[the_vrkiu->keybuftail++];
390 if (the_vrkiu->keybuftail >= NKEYBUF) {
391 the_vrkiu->keybuftail = 0;
392 }
393 return ret;
394 }
395
396 int
397 vrkiu_enable(scx, on)
398 void *scx;
399 int on;
400 {
401 struct vrkiu_softc *sc = scx;
402
403 if (on) {
404 if (sc->sc_enabled)
405 return (EBUSY);
406 sc->sc_enabled = 1;
407 } else {
408 if (sc->sc_chip == vrkiu_consdata)
409 return (EBUSY);
410 sc->sc_enabled = 0;
411 }
412
413 return (0);
414 }
415
416 void
417 vrkiu_set_leds(scx, leds)
418 void *scx;
419 int leds;
420 {
421 /*struct pckbd_softc *sc = scx;
422 */
423
424 DPRINTF(("%s(%d): vrkiu_set_leds() not implemented\n",
425 __FILE__, __LINE__));
426 }
427
428 int
429 vrkiu_ioctl(scx, cmd, data, flag, p)
430 void *scx;
431 u_long cmd;
432 caddr_t data;
433 int flag;
434 struct proc *p;
435 {
436 /*struct vrkiu_softc *sc = scx;
437 */
438
439 switch (cmd) {
440 case WSKBDIO_GTYPE:
441 /*
442 * XXX, fix me !
443 */
444 *(int *)data = WSKBD_TYPE_PC_XT;
445 return 0;
446 case WSKBDIO_SETLEDS:
447 DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
448 return 0;
449 case WSKBDIO_GETLEDS:
450 DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
451 *(int *)data = 0;
452 return (0);
453 }
454 return -1;
455 }
456
457 /*
458 * console support routines
459 */
460 int
461 vrkiu_cnattach(iot, iobase)
462 bus_space_tag_t iot;
463 int iobase;
464 {
465 static struct vrkiu_chip vrkiu_consdata_body;
466 bus_space_handle_t ioh;
467
468 if (vrkiu_consdata) {
469 panic("vrkiu is already attached as the console");
470 }
471 if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
472 printf("%s(%d): can't map bus space\n", __FILE__, __LINE__);
473 return -1;
474 }
475
476 if (vrkiu_init(&vrkiu_consdata_body, iot, ioh) != 0) {
477 DPRINTF(("%s(%d): vrkiu_init() failed\n", __FILE__, __LINE__));
478 return -1;
479 }
480 vrkiu_consdata = &vrkiu_consdata_body;
481
482 wskbd_cnattach(&vrkiu_consops, vrkiu_consdata, &vrkiu_keymapdata);
483
484 return (0);
485 }
486
487 void
488 vrkiu_cngetc(chipx, type, data)
489 void *chipx;
490 u_int *type;
491 int *data;
492 {
493 struct vrkiu_chip* chip = chipx;
494 int s;
495
496 if (!chip->kc_polling) {
497 printf("%s(%d): kiu is not polled\n", __FILE__, __LINE__);
498 while (1);
499 }
500
501 s = splimp();
502 if (chip->kc_type == WSCONS_EVENT_ALL_KEYS_UP) {
503 detect_key(chip);
504 }
505 *type = chip->kc_type;
506 *data = chip->kc_data;
507 chip->kc_type = WSCONS_EVENT_ALL_KEYS_UP;
508 splx(s);
509 }
510
511 void
512 vrkiu_cnpollc(chipx, on)
513 void *chipx;
514 int on;
515 {
516 struct vrkiu_chip* chip = chipx;
517 int s = splimp();
518
519 chip->kc_polling = on;
520
521 splx(s);
522
523 DPRINTF(("%s(%d): vrkiu polling %s\n",
524 __FILE__, __LINE__, on ? "ON" : "OFF"));
525 }
526