nextkbd.c revision 1.8 1 /* $NetBSD: nextkbd.c,v 1.8 2003/07/15 02:59:32 lukem 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>
33 __KERNEL_RCSID(0, "$NetBSD: nextkbd.c,v 1.8 2003/07/15 02:59:32 lukem Exp $");
34
35 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/errno.h>
44 #include <sys/queue.h>
45 #include <sys/lock.h>
46
47 #include <machine/autoconf.h>
48 #include <machine/bus.h>
49 #include <machine/cpu.h>
50 #include <machine/intr.h>
51
52 #include <next68k/dev/nextkbdvar.h>
53 #include <next68k/dev/wskbdmap_next.h>
54
55 #include <dev/wscons/wsconsio.h>
56 #include <dev/wscons/wskbdvar.h>
57 #include <dev/wscons/wsksymdef.h>
58 #include <dev/wscons/wsksymvar.h>
59
60 #include <next68k/next68k/isr.h>
61
62 #include <next68k/dev/intiovar.h>
63
64 struct nextkbd_internal {
65 int num_ints; /* interrupt total */
66 int polling;
67 int isconsole;
68
69 bus_space_tag_t iot;
70 bus_space_handle_t ioh;
71 struct nextkbd_softc *t_sc; /* back pointer */
72 u_int32_t mods;
73 };
74
75 struct mon_regs {
76 u_int32_t mon_csr;
77 u_int32_t mon_1;
78 u_int32_t mon_data;
79 };
80
81 static int attached = 0;
82
83 int nextkbd_match __P((struct device *, struct cfdata *, void *));
84 void nextkbd_attach __P((struct device *, struct device *, void *));
85
86 int nextkbc_cnattach __P((bus_space_tag_t));
87
88 CFATTACH_DECL(nextkbd, sizeof(struct nextkbd_softc),
89 nextkbd_match, nextkbd_attach, NULL, NULL);
90
91 int nextkbd_enable __P((void *, int));
92 void nextkbd_set_leds __P((void *, int));
93 int nextkbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
94
95 const struct wskbd_accessops nextkbd_accessops = {
96 nextkbd_enable,
97 nextkbd_set_leds,
98 nextkbd_ioctl,
99 };
100
101 void nextkbd_cngetc __P((void *, u_int *, int *));
102 void nextkbd_cnpollc __P((void *, int));
103
104 const struct wskbd_consops nextkbd_consops = {
105 nextkbd_cngetc,
106 nextkbd_cnpollc,
107 };
108
109 const struct wskbd_mapdata nextkbd_keymapdata = {
110 nextkbd_keydesctab,
111 KB_US,
112 };
113
114 static int nextkbd_read_data __P((struct nextkbd_internal *));
115 static int nextkbd_decode __P((struct nextkbd_internal *, int, u_int *, int *));
116
117 static struct nextkbd_internal nextkbd_consdata;
118 static int nextkbd_is_console __P((bus_space_tag_t bst));
119
120 int nextkbdhard __P((void *));
121
122 static int
123 nextkbd_is_console(bst)
124 bus_space_tag_t bst;
125 {
126 return (nextkbd_consdata.isconsole
127 && (bst == nextkbd_consdata.iot));
128 }
129
130 int
131 nextkbd_match(parent, match, aux)
132 struct device *parent;
133 struct cfdata *match;
134 void *aux;
135 {
136 struct intio_attach_args *ia = (struct intio_attach_args *)aux;
137
138 if (attached)
139 return(0);
140
141 ia->ia_addr = (void *)NEXT_P_MON;
142
143 return(1);
144 }
145
146 void
147 nextkbd_attach(parent, self, aux)
148 struct device *parent, *self;
149 void *aux;
150 {
151 struct nextkbd_softc *sc = (struct nextkbd_softc *)self;
152 struct intio_attach_args *ia = (struct intio_attach_args *)aux;
153 int isconsole;
154 struct wskbddev_attach_args a;
155
156 printf("\n");
157
158 isconsole = nextkbd_is_console(ia->ia_bst); /* XXX */
159
160 if (isconsole) {
161 sc->id = &nextkbd_consdata;
162 } else {
163 sc->id = malloc(sizeof(struct nextkbd_internal),
164 M_DEVBUF, M_WAITOK);
165
166 memset(sc->id, 0, sizeof(struct nextkbd_internal));
167 sc->id->iot = ia->ia_bst;
168 if (bus_space_map(sc->id->iot, NEXT_P_MON,
169 sizeof(struct mon_regs),
170 0, &sc->id->ioh)) {
171 printf("%s: can't map mon status control register\n",
172 sc->sc_dev.dv_xname);
173 return;
174 }
175 }
176
177 sc->id->t_sc = sc; /* set back pointer */
178
179 isrlink_autovec(nextkbdhard, sc, NEXT_I_IPL(NEXT_I_KYBD_MOUSE), 0, NULL);
180
181 INTR_ENABLE(NEXT_I_KYBD_MOUSE);
182
183 a.console = isconsole;
184 a.keymap = &nextkbd_keymapdata;
185 a.accessops = &nextkbd_accessops;
186 a.accesscookie = sc;
187
188 /*
189 * Attach the wskbd, saving a handle to it.
190 * XXX XXX XXX
191 */
192 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
193
194 attached = 1;
195 }
196
197 int
198 nextkbd_enable(v, on)
199 void *v;
200 int on;
201 {
202 /* XXX not sure if this should do anything */
203 /* printf("nextkbd_enable %d\n", on); */
204 return 0;
205 }
206
207 void
208 nextkbd_set_leds(v, leds)
209 void *v;
210 int leds;
211 {
212 struct nextkbd_softc *sc = v;
213 uint32_t hw_leds = 0;
214 int s;
215
216 sc->sc_leds &= ~ NEXT_WSKBD_LEDS;
217 sc->sc_leds |= (leds & NEXT_WSKBD_LEDS);
218
219 if (sc->sc_leds & WSKBD_LED_CAPS) {
220 hw_leds |= 0x30000;
221 }
222
223 s = spltty();
224 bus_space_write_1(sc->id->iot, sc->id->ioh, 3, 0xc5);
225 /* @@@ need to add:
226 if bit 7 of @ioh+0 set:
227 repeat 2
228 wait until bit 6 of @ioh+2 clears
229 */
230 bus_space_write_4(sc->id->iot, sc->id->ioh, 4, hw_leds);
231 /* @@@ need to add:
232 wait until bit 4 of @ioh+0 (@ioh+2 if bit 7 was set above)
233 clears
234 */
235 splx(s);
236
237 return;
238 }
239
240 int
241 nextkbd_ioctl(v, cmd, data, flag, p)
242 void *v;
243 u_long cmd;
244 caddr_t data;
245 int flag;
246 struct proc *p;
247 {
248 struct nextkbd_softc *sc = v;
249
250 switch (cmd) {
251 case WSKBDIO_GTYPE:
252 /* XXX */
253 *(int *)data = WSKBD_TYPE_NEXT;
254 return (0);
255 case WSKBDIO_SETLEDS:
256 nextkbd_set_leds (sc, *(int *)data);
257 return (0);
258 case WSKBDIO_GETLEDS:
259 *(int *)data = sc->sc_leds & NEXT_WSKBD_LEDS;
260 return (0);
261 case WSKBDIO_COMPLEXBELL:
262 return (0);
263 }
264 return EPASSTHROUGH;
265 }
266
267 int
268 nextkbdhard(arg)
269 void *arg;
270 {
271 register struct nextkbd_softc *sc = arg;
272 int type, key, val;
273
274 if (!INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) return 0;
275
276 #define CSR_INT 0x00800000
277 #define CSR_DATA 0x00400000
278
279 #define KD_KEYMASK 0x007f
280 #define KD_DIRECTION 0x0080 /* pressed or released */
281 #define KD_CNTL 0x0100
282 #define KD_LSHIFT 0x0200
283 #define KD_RSHIFT 0x0400
284 #define KD_LCOMM 0x0800
285 #define KD_RCOMM 0x1000
286 #define KD_LALT 0x2000
287 #define KD_RALT 0x4000
288 #define KD_VALID 0x8000 /* only set for scancode keys ? */
289 #define KD_MODS 0x4f00
290
291 val = nextkbd_read_data(sc->id);
292 if ((val != -1) && nextkbd_decode(sc->id, val, &type, &key)) {
293 wskbd_input(sc->sc_wskbddev, type, key);
294 }
295 return(1);
296 }
297
298 int
299 nextkbd_cnattach(bst)
300 bus_space_tag_t bst;
301 {
302 bus_space_handle_t bsh;
303
304 if (bus_space_map(bst, NEXT_P_MON, sizeof(struct mon_regs),
305 0, &bsh))
306 return (ENXIO);
307
308 memset(&nextkbd_consdata, 0, sizeof(nextkbd_consdata));
309
310 nextkbd_consdata.iot = bst;
311 nextkbd_consdata.ioh = bsh;
312 nextkbd_consdata.isconsole = 1;
313
314 wskbd_cnattach(&nextkbd_consops, &nextkbd_consdata,
315 &nextkbd_keymapdata);
316
317 return (0);
318 }
319
320 void
321 nextkbd_cngetc(v, type, data)
322 void *v;
323 u_int *type;
324 int *data;
325 {
326 struct nextkbd_internal *t = v;
327 int val;
328
329 for (;;) {
330 if (INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) {
331 val = nextkbd_read_data(t);
332 if ((val != -1) && nextkbd_decode(t, val, type, data))
333 return;
334 }
335 }
336 }
337
338 void
339 nextkbd_cnpollc(v, on)
340 void *v;
341 int on;
342 {
343 struct nextkbd_internal *t = v;
344
345 t->polling = on;
346 if (on) {
347 INTR_DISABLE(NEXT_I_KYBD_MOUSE);
348 } else {
349 INTR_ENABLE(NEXT_I_KYBD_MOUSE);
350 }
351
352 }
353
354 static int
355 nextkbd_read_data(struct nextkbd_internal *id)
356 {
357 unsigned char device;
358 struct mon_regs stat;
359
360 bus_space_read_region_4(id->iot, id->ioh, 0, &stat, 3);
361 if ((stat.mon_csr & CSR_INT) && (stat.mon_csr & CSR_DATA)) {
362 stat.mon_csr &= ~CSR_INT;
363 id->num_ints++;
364 bus_space_write_4(id->iot, id->ioh, 0, stat.mon_csr);
365 device = stat.mon_data >> 28;
366 if (device != 1) return (-1); /* XXX: mouse */
367 return (stat.mon_data & 0xffff);
368 }
369 return (-1);
370 }
371
372 static int
373 nextkbd_decode(id, datain, type, dataout)
374 struct nextkbd_internal *id;
375 int datain;
376 u_int *type;
377 int *dataout;
378 {
379 /* printf("datain %08x mods %08x\n", datain, id->mods); */
380
381 if ((datain ^ id->mods) & KD_LSHIFT) {
382 id->mods ^= KD_LSHIFT;
383 *dataout = 90;
384 if (datain & KD_LSHIFT)
385 *type = WSCONS_EVENT_KEY_DOWN;
386 else
387 *type = WSCONS_EVENT_KEY_UP;
388 } else if ((datain ^ id->mods) & KD_RSHIFT) {
389 id->mods ^= KD_RSHIFT;
390 *dataout = 91;
391 if (datain & KD_RSHIFT)
392 *type = WSCONS_EVENT_KEY_DOWN;
393 else
394 *type = WSCONS_EVENT_KEY_UP;
395 } else if ((datain ^ id->mods) & KD_LALT) {
396 id->mods ^= KD_LALT;
397 *dataout = 92;
398 if (datain & KD_LALT)
399 *type = WSCONS_EVENT_KEY_DOWN;
400 else
401 *type = WSCONS_EVENT_KEY_UP;
402 } else if ((datain ^ id->mods) & KD_RALT) {
403 id->mods ^= KD_RALT;
404 *dataout = 93;
405 if (datain & KD_RALT)
406 *type = WSCONS_EVENT_KEY_DOWN;
407 else
408 *type = WSCONS_EVENT_KEY_UP;
409 } else if ((datain ^ id->mods) & KD_CNTL) {
410 id->mods ^= KD_CNTL;
411 *dataout = 94;
412 if (datain & KD_CNTL)
413 *type = WSCONS_EVENT_KEY_DOWN;
414 else
415 *type = WSCONS_EVENT_KEY_UP;
416 } else if ((datain ^ id->mods) & KD_LCOMM) {
417 id->mods ^= KD_LCOMM;
418 *dataout = 95;
419 if (datain & KD_LCOMM)
420 *type = WSCONS_EVENT_KEY_DOWN;
421 else
422 *type = WSCONS_EVENT_KEY_UP;
423 } else if ((datain ^ id->mods) & KD_RCOMM) {
424 id->mods ^= KD_RCOMM;
425 *dataout = 96;
426 if (datain & KD_RCOMM)
427 *type = WSCONS_EVENT_KEY_DOWN;
428 else
429 *type = WSCONS_EVENT_KEY_UP;
430 } else if (datain & KD_KEYMASK) {
431 if (datain & KD_DIRECTION)
432 *type = WSCONS_EVENT_KEY_UP;
433 else
434 *type = WSCONS_EVENT_KEY_DOWN;
435
436 *dataout = (datain & KD_KEYMASK);
437 } else {
438 *dataout = 0;
439 }
440
441 return 1;
442 }
443