vrkiu.c revision 1.3 1 /* $NetBSD: vrkiu.c,v 1.3 1999/11/03 13:55:41 enami 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 detect_key(sc->sc_chip);
312
313 vrkiu_write(sc->sc_chip, KIUINT, 0x7); /* Clear all interrupt */
314
315 return 0;
316 }
317
318 static void
319 detect_key(chip)
320 struct vrkiu_chip* chip;
321 {
322 int i, j, modified, mask;
323 unsigned short scandata[KIU_NSCANLINE/2];
324
325 for (i = 0; i < KIU_NSCANLINE / 2; i++) {
326 scandata[i] = vrkiu_read(chip, KIUDATP + i * 2);
327 }
328
329 DPRINTF(("%s(%d): detect_key():", __FILE__, __LINE__));
330
331 if (chip->kc_polling) {
332 chip->kc_type = WSCONS_EVENT_ALL_KEYS_UP;
333 }
334
335 for (i = 0; i < KIU_NSCANLINE / 2; i++) {
336 modified = scandata[i] ^ chip->kc_scandata[i];
337 mask = 1;
338 for (j = 0; j < 16; j++, mask <<= 1) {
339 /* XXX: The order of keys can be a problem.
340 If CTRL and normal key are pushed simultaneously,
341 normal key can be entered in queue first.
342 Same problem would occur in key break. */
343 if (modified & mask) {
344 int key, type;
345 key = i * 16 + j;
346 if (keytrans[key] < 0) {
347 printf("vrkiu: Unkown scan code 0x%02x\n", key);
348 continue;
349 }
350 type = (scandata[i] & mask) ?
351 WSCONS_EVENT_KEY_DOWN :
352 WSCONS_EVENT_KEY_UP;
353 DPRINTF(("(%d,%d)=%s%d ", i, j,
354 (scandata[i] & mask) ? "v" : "^",
355 keytrans[key]));
356 if (chip->kc_polling) {
357 chip->kc_type = type;
358 chip->kc_data = keytrans[key];
359 } else {
360 wskbd_input(chip->kc_sc->sc_wskbddev,
361 type,
362 keytrans[key]);
363 }
364 }
365 }
366 chip->kc_scandata[i] = scandata[i];
367 }
368 DPRINTF(("\n"));
369 }
370
371 /* called from biconsdev.c */
372 int
373 vrkiu_getc()
374 {
375 int ret;
376
377 if (the_vrkiu == NULL) {
378 return 0; /* XXX */
379 }
380
381 while (the_vrkiu->keybuftail == the_vrkiu->keybufhead) {
382 detect_key(vrkiu_consdata);
383 }
384 ret = the_vrkiu->keybuf[the_vrkiu->keybuftail++];
385 if (the_vrkiu->keybuftail >= NKEYBUF) {
386 the_vrkiu->keybuftail = 0;
387 }
388 return ret;
389 }
390
391 int
392 vrkiu_enable(scx, on)
393 void *scx;
394 int on;
395 {
396 struct vrkiu_softc *sc = scx;
397
398 if (on) {
399 if (sc->sc_enabled)
400 return (EBUSY);
401 sc->sc_enabled = 1;
402 } else {
403 if (sc->sc_chip == vrkiu_consdata)
404 return (EBUSY);
405 sc->sc_enabled = 0;
406 }
407
408 return (0);
409 }
410
411 void
412 vrkiu_set_leds(scx, leds)
413 void *scx;
414 int leds;
415 {
416 /*struct pckbd_softc *sc = scx;
417 */
418
419 DPRINTF(("%s(%d): vrkiu_set_leds() not implemented\n",
420 __FILE__, __LINE__));
421 }
422
423 int
424 vrkiu_ioctl(scx, cmd, data, flag, p)
425 void *scx;
426 u_long cmd;
427 caddr_t data;
428 int flag;
429 struct proc *p;
430 {
431 /*struct vrkiu_softc *sc = scx;
432 */
433
434 switch (cmd) {
435 case WSKBDIO_GTYPE:
436 /*
437 * XXX, fix me !
438 */
439 *(int *)data = WSKBD_TYPE_PC_XT;
440 return 0;
441 case WSKBDIO_SETLEDS:
442 DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
443 return 0;
444 case WSKBDIO_GETLEDS:
445 DPRINTF(("%s(%d): no LED\n", __FILE__, __LINE__));
446 *(int *)data = 0;
447 return (0);
448 }
449 return -1;
450 }
451
452 /*
453 * console support routines
454 */
455 int
456 vrkiu_cnattach(iot, iobase)
457 bus_space_tag_t iot;
458 int iobase;
459 {
460 static struct vrkiu_chip vrkiu_consdata_body;
461 bus_space_handle_t ioh;
462
463 if (vrkiu_consdata) {
464 panic("vrkiu is already attached as the console");
465 }
466 if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
467 printf("%s(%d): can't map bus space\n", __FILE__, __LINE__);
468 return -1;
469 }
470
471 if (vrkiu_init(&vrkiu_consdata_body, iot, ioh) != 0) {
472 DPRINTF(("%s(%d): vrkiu_init() failed\n", __FILE__, __LINE__));
473 return -1;
474 }
475 vrkiu_consdata = &vrkiu_consdata_body;
476
477 wskbd_cnattach(&vrkiu_consops, vrkiu_consdata, &vrkiu_keymapdata);
478
479 return (0);
480 }
481
482 void
483 vrkiu_cngetc(chipx, type, data)
484 void *chipx;
485 u_int *type;
486 int *data;
487 {
488 struct vrkiu_chip* chip = chipx;
489 int s;
490
491 if (!chip->kc_polling) {
492 printf("%s(%d): kiu is not polled\n", __FILE__, __LINE__);
493 while (1);
494 }
495
496 s = splimp();
497 if (chip->kc_type == WSCONS_EVENT_ALL_KEYS_UP) {
498 detect_key(chip);
499 }
500 *type = chip->kc_type;
501 *data = chip->kc_data;
502 chip->kc_type = WSCONS_EVENT_ALL_KEYS_UP;
503 splx(s);
504 }
505
506 void
507 vrkiu_cnpollc(chipx, on)
508 void *chipx;
509 int on;
510 {
511 struct vrkiu_chip* chip = chipx;
512 int s = splimp();
513
514 chip->kc_polling = on;
515
516 splx(s);
517
518 DPRINTF(("%s(%d): vrkiu polling %s\n",
519 __FILE__, __LINE__, on ? "ON" : "OFF"));
520 }
521