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