nextkbd.c revision 1.5 1 /* $NetBSD: nextkbd.c,v 1.5 2002/09/11 01:46:32 mycroft Exp $ */
2 /*
3 * Copyright (c) 1998 Matt DeBergalis
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Matt DeBergalis
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <sys/errno.h>
41 #include <sys/queue.h>
42 #include <sys/lock.h>
43
44 #include <machine/autoconf.h>
45 #include <machine/bus.h>
46 #include <machine/cpu.h>
47 #include <machine/intr.h>
48
49 #include <next68k/dev/nextkbdvar.h>
50 #include <next68k/dev/wskbdmap_next.h>
51
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wscons/wskbdvar.h>
54 #include <dev/wscons/wsksymdef.h>
55 #include <dev/wscons/wsksymvar.h>
56
57 #include <next68k/next68k/isr.h>
58
59 #include <next68k/dev/intiovar.h>
60
61 struct nextkbd_internal {
62 int num_ints; /* interrupt total */
63 int polling;
64 int isconsole;
65
66 bus_space_tag_t iot;
67 bus_space_handle_t ioh;
68 struct nextkbd_softc *t_sc; /* back pointer */
69 u_int32_t mods;
70 };
71
72 struct mon_regs {
73 u_int32_t mon_csr;
74 u_int32_t mon_1;
75 u_int32_t mon_data;
76 };
77
78 static int attached = 0;
79
80 int nextkbd_match __P((struct device *, struct cfdata *, void *));
81 void nextkbd_attach __P((struct device *, struct device *, void *));
82
83 int nextkbc_cnattach __P((bus_space_tag_t));
84
85 struct cfattach nextkbd_ca = {
86 sizeof(struct nextkbd_softc), nextkbd_match, nextkbd_attach
87 };
88
89 int nextkbd_enable __P((void *, int));
90 void nextkbd_set_leds __P((void *, int));
91 int nextkbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
92
93 const struct wskbd_accessops nextkbd_accessops = {
94 nextkbd_enable,
95 nextkbd_set_leds,
96 nextkbd_ioctl,
97 };
98
99 void nextkbd_cngetc __P((void *, u_int *, int *));
100 void nextkbd_cnpollc __P((void *, int));
101
102 const struct wskbd_consops nextkbd_consops = {
103 nextkbd_cngetc,
104 nextkbd_cnpollc,
105 };
106
107 const struct wskbd_mapdata nextkbd_keymapdata = {
108 nextkbd_keydesctab,
109 KB_US,
110 };
111
112 static int nextkbd_read_data __P((struct nextkbd_internal *));
113 static int nextkbd_decode __P((struct nextkbd_internal *, int, u_int *, int *));
114
115 static struct nextkbd_internal nextkbd_consdata;
116 static int nextkbd_is_console __P((bus_space_tag_t bst));
117
118 int nextkbdhard __P((void *));
119
120 static int
121 nextkbd_is_console(bst)
122 bus_space_tag_t bst;
123 {
124 return (nextkbd_consdata.isconsole
125 && (bst == nextkbd_consdata.iot));
126 }
127
128 int
129 nextkbd_match(parent, match, aux)
130 struct device *parent;
131 struct cfdata *match;
132 void *aux;
133 {
134 struct intio_attach_args *ia = (struct intio_attach_args *)aux;
135
136 if (attached)
137 return(0);
138
139 ia->ia_addr = (void *)NEXT_P_MON;
140
141 return(1);
142 }
143
144 void
145 nextkbd_attach(parent, self, aux)
146 struct device *parent, *self;
147 void *aux;
148 {
149 struct nextkbd_softc *sc = (struct nextkbd_softc *)self;
150 struct intio_attach_args *ia = (struct intio_attach_args *)aux;
151 int isconsole;
152 struct wskbddev_attach_args a;
153
154 printf("\n");
155
156 isconsole = nextkbd_is_console(ia->ia_bst); /* XXX */
157
158 if (isconsole) {
159 sc->id = &nextkbd_consdata;
160 } else {
161 sc->id = malloc(sizeof(struct nextkbd_internal),
162 M_DEVBUF, M_WAITOK);
163
164 memset(sc->id, 0, sizeof(struct nextkbd_internal));
165 sc->id->iot = ia->ia_bst;
166 if (bus_space_map(sc->id->iot, NEXT_P_MON,
167 sizeof(struct mon_regs),
168 0, &sc->id->ioh)) {
169 printf("%s: can't map mon status control register\n",
170 sc->sc_dev.dv_xname);
171 return;
172 }
173 }
174
175 sc->id->t_sc = sc; /* set back pointer */
176
177 isrlink_autovec(nextkbdhard, sc, NEXT_I_IPL(NEXT_I_KYBD_MOUSE), 0, NULL);
178
179 INTR_ENABLE(NEXT_I_KYBD_MOUSE);
180
181 a.console = isconsole;
182 a.keymap = &nextkbd_keymapdata;
183 a.accessops = &nextkbd_accessops;
184 a.accesscookie = sc;
185
186 /*
187 * Attach the wskbd, saving a handle to it.
188 * XXX XXX XXX
189 */
190 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
191
192 attached = 1;
193 }
194
195 int
196 nextkbd_enable(v, on)
197 void *v;
198 int on;
199 {
200 /* XXX not sure if this should do anything */
201 /* printf("nextkbd_enable %d\n", on); */
202 return 0;
203 }
204
205 void
206 nextkbd_set_leds(v, leds)
207 void *v;
208 int leds;
209 {
210 struct nextkbd_softc *sc = v;
211 uint32_t hw_leds = 0;
212 int s;
213
214 sc->sc_leds &= ~ NEXT_WSKBD_LEDS;
215 sc->sc_leds |= (leds & NEXT_WSKBD_LEDS);
216
217 if (sc->sc_leds & WSKBD_LED_CAPS) {
218 hw_leds |= 0x30000;
219 }
220
221 s = spltty();
222 bus_space_write_1(sc->id->iot, sc->id->ioh, 3, 0xc5);
223 /* @@@ need to add:
224 if bit 7 of @ioh+0 set:
225 repeat 2
226 wait until bit 6 of @ioh+2 clears
227 */
228 bus_space_write_4(sc->id->iot, sc->id->ioh, 4, hw_leds);
229 /* @@@ need to add:
230 wait until bit 4 of @ioh+0 (@ioh+2 if bit 7 was set above)
231 clears
232 */
233 splx(s);
234
235 return;
236 }
237
238 int
239 nextkbd_ioctl(v, cmd, data, flag, p)
240 void *v;
241 u_long cmd;
242 caddr_t data;
243 int flag;
244 struct proc *p;
245 {
246 struct nextkbd_softc *sc = v;
247
248 switch (cmd) {
249 case WSKBDIO_GTYPE:
250 /* XXX */
251 *(int *)data = WSKBD_TYPE_NEXT;
252 return (0);
253 case WSKBDIO_SETLEDS:
254 nextkbd_set_leds (sc, *(int *)data);
255 return (0);
256 case WSKBDIO_GETLEDS:
257 *(int *)data = sc->sc_leds & NEXT_WSKBD_LEDS;
258 return (0);
259 case WSKBDIO_COMPLEXBELL:
260 return (0);
261 }
262 return EPASSTHROUGH;
263 }
264
265 int
266 nextkbdhard(arg)
267 void *arg;
268 {
269 register struct nextkbd_softc *sc = arg;
270 int type, key, val;
271
272 if (!INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) return 0;
273
274 #define CSR_INT 0x00800000
275 #define CSR_DATA 0x00400000
276
277 #define KD_KEYMASK 0x007f
278 #define KD_DIRECTION 0x0080 /* pressed or released */
279 #define KD_CNTL 0x0100
280 #define KD_LSHIFT 0x0200
281 #define KD_RSHIFT 0x0400
282 #define KD_LCOMM 0x0800
283 #define KD_RCOMM 0x1000
284 #define KD_LALT 0x2000
285 #define KD_RALT 0x4000
286 #define KD_VALID 0x8000 /* only set for scancode keys ? */
287 #define KD_MODS 0x4f00
288
289 val = nextkbd_read_data(sc->id);
290 if ((val != -1) && nextkbd_decode(sc->id, val, &type, &key)) {
291 wskbd_input(sc->sc_wskbddev, type, key);
292 }
293 return(1);
294 }
295
296 int
297 nextkbd_cnattach(bst)
298 bus_space_tag_t bst;
299 {
300 bus_space_handle_t bsh;
301
302 if (bus_space_map(bst, NEXT_P_MON, sizeof(struct mon_regs),
303 0, &bsh))
304 return (ENXIO);
305
306 memset(&nextkbd_consdata, 0, sizeof(nextkbd_consdata));
307
308 nextkbd_consdata.iot = bst;
309 nextkbd_consdata.ioh = bsh;
310 nextkbd_consdata.isconsole = 1;
311
312 wskbd_cnattach(&nextkbd_consops, &nextkbd_consdata,
313 &nextkbd_keymapdata);
314
315 return (0);
316 }
317
318 void
319 nextkbd_cngetc(v, type, data)
320 void *v;
321 u_int *type;
322 int *data;
323 {
324 struct nextkbd_internal *t = v;
325 int val;
326
327 for (;;) {
328 if (INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) {
329 val = nextkbd_read_data(t);
330 if ((val != -1) && nextkbd_decode(t, val, type, data))
331 return;
332 }
333 }
334 }
335
336 void
337 nextkbd_cnpollc(v, on)
338 void *v;
339 int on;
340 {
341 struct nextkbd_internal *t = v;
342
343 t->polling = on;
344 if (on) {
345 INTR_DISABLE(NEXT_I_KYBD_MOUSE);
346 } else {
347 INTR_ENABLE(NEXT_I_KYBD_MOUSE);
348 }
349
350 }
351
352 static int
353 nextkbd_read_data(struct nextkbd_internal *id)
354 {
355 unsigned char device;
356 struct mon_regs stat;
357
358 bus_space_read_region_4(id->iot, id->ioh, 0, &stat, 3);
359 if ((stat.mon_csr & CSR_INT) && (stat.mon_csr & CSR_DATA)) {
360 stat.mon_csr &= ~CSR_INT;
361 id->num_ints++;
362 bus_space_write_4(id->iot, id->ioh, 0, stat.mon_csr);
363 device = stat.mon_data >> 28;
364 if (device != 1) return (-1); /* XXX: mouse */
365 return (stat.mon_data & 0xffff);
366 }
367 return (-1);
368 }
369
370 static int
371 nextkbd_decode(id, datain, type, dataout)
372 struct nextkbd_internal *id;
373 int datain;
374 u_int *type;
375 int *dataout;
376 {
377 /* printf("datain %08x mods %08x\n", datain, id->mods); */
378
379 if ((datain ^ id->mods) & KD_LSHIFT) {
380 id->mods ^= KD_LSHIFT;
381 *dataout = 90;
382 if (datain & KD_LSHIFT)
383 *type = WSCONS_EVENT_KEY_DOWN;
384 else
385 *type = WSCONS_EVENT_KEY_UP;
386 } else if ((datain ^ id->mods) & KD_RSHIFT) {
387 id->mods ^= KD_RSHIFT;
388 *dataout = 91;
389 if (datain & KD_RSHIFT)
390 *type = WSCONS_EVENT_KEY_DOWN;
391 else
392 *type = WSCONS_EVENT_KEY_UP;
393 } else if ((datain ^ id->mods) & KD_LALT) {
394 id->mods ^= KD_LALT;
395 *dataout = 92;
396 if (datain & KD_LALT)
397 *type = WSCONS_EVENT_KEY_DOWN;
398 else
399 *type = WSCONS_EVENT_KEY_UP;
400 } else if ((datain ^ id->mods) & KD_RALT) {
401 id->mods ^= KD_RALT;
402 *dataout = 93;
403 if (datain & KD_RALT)
404 *type = WSCONS_EVENT_KEY_DOWN;
405 else
406 *type = WSCONS_EVENT_KEY_UP;
407 } else if ((datain ^ id->mods) & KD_CNTL) {
408 id->mods ^= KD_CNTL;
409 *dataout = 94;
410 if (datain & KD_CNTL)
411 *type = WSCONS_EVENT_KEY_DOWN;
412 else
413 *type = WSCONS_EVENT_KEY_UP;
414 } else if ((datain ^ id->mods) & KD_LCOMM) {
415 id->mods ^= KD_LCOMM;
416 *dataout = 95;
417 if (datain & KD_LCOMM)
418 *type = WSCONS_EVENT_KEY_DOWN;
419 else
420 *type = WSCONS_EVENT_KEY_UP;
421 } else if ((datain ^ id->mods) & KD_RCOMM) {
422 id->mods ^= KD_RCOMM;
423 *dataout = 96;
424 if (datain & KD_RCOMM)
425 *type = WSCONS_EVENT_KEY_DOWN;
426 else
427 *type = WSCONS_EVENT_KEY_UP;
428 } else if (datain & KD_KEYMASK) {
429 if (datain & KD_DIRECTION)
430 *type = WSCONS_EVENT_KEY_UP;
431 else
432 *type = WSCONS_EVENT_KEY_DOWN;
433
434 *dataout = (datain & KD_KEYMASK);
435 } else {
436 *dataout = 0;
437 }
438
439 return 1;
440 }
441