mkbd.c revision 1.13.6.3 1 1.13.6.3 thorpej /* $NetBSD: mkbd.c,v 1.13.6.3 2002/12/11 05:58:28 thorpej Exp $ */
2 1.13.6.2 nathanw
3 1.13.6.2 nathanw /*-
4 1.13.6.2 nathanw * Copyright (c) 2001 Marcus Comstedt
5 1.13.6.2 nathanw * All rights reserved.
6 1.13.6.2 nathanw *
7 1.13.6.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.13.6.2 nathanw * modification, are permitted provided that the following conditions
9 1.13.6.2 nathanw * are met:
10 1.13.6.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.13.6.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.13.6.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.13.6.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.13.6.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.13.6.2 nathanw * 3. All advertising materials mentioning features or use of this software
16 1.13.6.2 nathanw * must display the following acknowledgement:
17 1.13.6.2 nathanw * This product includes software developed by Marcus Comstedt.
18 1.13.6.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.13.6.2 nathanw * contributors may be used to endorse or promote products derived
20 1.13.6.2 nathanw * from this software without specific prior written permission.
21 1.13.6.2 nathanw *
22 1.13.6.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.13.6.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.13.6.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.13.6.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.13.6.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.13.6.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.13.6.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.13.6.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.13.6.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.13.6.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.13.6.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
33 1.13.6.2 nathanw */
34 1.13.6.2 nathanw
35 1.13.6.2 nathanw #include <sys/param.h>
36 1.13.6.2 nathanw #include <sys/device.h>
37 1.13.6.2 nathanw #include <sys/fcntl.h>
38 1.13.6.2 nathanw #include <sys/poll.h>
39 1.13.6.2 nathanw #include <sys/select.h>
40 1.13.6.2 nathanw #include <sys/proc.h>
41 1.13.6.2 nathanw #include <sys/signalvar.h>
42 1.13.6.2 nathanw #include <sys/systm.h>
43 1.13.6.2 nathanw
44 1.13.6.2 nathanw #include "wskbd.h"
45 1.13.6.2 nathanw
46 1.13.6.2 nathanw #include <dev/wscons/wsconsio.h>
47 1.13.6.2 nathanw #include <dev/wscons/wskbdvar.h>
48 1.13.6.2 nathanw #include <dev/wscons/wsksymdef.h>
49 1.13.6.2 nathanw #include <dev/wscons/wsksymvar.h>
50 1.13.6.2 nathanw
51 1.13.6.2 nathanw #include <machine/cpu.h>
52 1.13.6.2 nathanw #include <machine/bus.h>
53 1.13.6.2 nathanw
54 1.13.6.2 nathanw #include <dreamcast/dev/maple/maple.h>
55 1.13.6.2 nathanw #include <dreamcast/dev/maple/mapleconf.h>
56 1.13.6.2 nathanw #include <dreamcast/dev/maple/mkbdvar.h>
57 1.13.6.2 nathanw #include <dreamcast/dev/maple/mkbdmap.h>
58 1.13.6.2 nathanw
59 1.13.6.2 nathanw /*
60 1.13.6.2 nathanw * Function declarations.
61 1.13.6.2 nathanw */
62 1.13.6.2 nathanw static int mkbdmatch(struct device *, struct cfdata *, void *);
63 1.13.6.2 nathanw static void mkbdattach(struct device *, struct device *, void *);
64 1.13.6.3 thorpej static int mkbddetach(struct device *, int);
65 1.13.6.2 nathanw
66 1.13.6.2 nathanw int mkbd_enable(void *, int);
67 1.13.6.2 nathanw void mkbd_set_leds(void *, int);
68 1.13.6.2 nathanw int mkbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
69 1.13.6.2 nathanw
70 1.13.6.2 nathanw struct wskbd_accessops mkbd_accessops = {
71 1.13.6.2 nathanw mkbd_enable,
72 1.13.6.2 nathanw mkbd_set_leds,
73 1.13.6.2 nathanw mkbd_ioctl,
74 1.13.6.2 nathanw };
75 1.13.6.2 nathanw
76 1.13.6.3 thorpej static void mkbd_intr(void *, struct maple_response *, int, int);
77 1.13.6.2 nathanw
78 1.13.6.2 nathanw void mkbd_cngetc(void *, u_int *, int *);
79 1.13.6.2 nathanw void mkbd_cnpollc(void *, int);
80 1.13.6.2 nathanw int mkbd_cnattach(void);
81 1.13.6.2 nathanw
82 1.13.6.2 nathanw struct wskbd_consops mkbd_consops = {
83 1.13.6.2 nathanw mkbd_cngetc,
84 1.13.6.2 nathanw mkbd_cnpollc,
85 1.13.6.2 nathanw };
86 1.13.6.2 nathanw
87 1.13.6.2 nathanw struct wskbd_mapdata mkbd_keymapdata = {
88 1.13.6.2 nathanw mkbd_keydesctab,
89 1.13.6.2 nathanw KB_JP,
90 1.13.6.2 nathanw };
91 1.13.6.2 nathanw
92 1.13.6.3 thorpej static struct mkbd_softc *mkbd_console_softc;
93 1.13.6.2 nathanw
94 1.13.6.3 thorpej static int mkbd_is_console;
95 1.13.6.3 thorpej static int mkbd_console_initted;
96 1.13.6.2 nathanw
97 1.13.6.2 nathanw CFATTACH_DECL(mkbd, sizeof(struct mkbd_softc),
98 1.13.6.3 thorpej mkbdmatch, mkbdattach, mkbddetach, NULL);
99 1.13.6.2 nathanw
100 1.13.6.2 nathanw static int
101 1.13.6.2 nathanw mkbdmatch(struct device *parent, struct cfdata *cf, void *aux)
102 1.13.6.2 nathanw {
103 1.13.6.2 nathanw struct maple_attach_args *ma = aux;
104 1.13.6.2 nathanw
105 1.13.6.3 thorpej return (ma->ma_function == MAPLE_FN_KEYBOARD ? MAPLE_MATCH_FUNC : 0);
106 1.13.6.2 nathanw }
107 1.13.6.2 nathanw
108 1.13.6.2 nathanw static void
109 1.13.6.2 nathanw mkbdattach(struct device *parent, struct device *self, void *aux)
110 1.13.6.2 nathanw {
111 1.13.6.2 nathanw struct mkbd_softc *sc = (struct mkbd_softc *) self;
112 1.13.6.2 nathanw struct maple_attach_args *ma = aux;
113 1.13.6.2 nathanw #if NWSKBD > 0
114 1.13.6.2 nathanw struct wskbddev_attach_args a;
115 1.13.6.2 nathanw #endif
116 1.13.6.2 nathanw u_int32_t kbdtype;
117 1.13.6.2 nathanw
118 1.13.6.2 nathanw sc->sc_parent = parent;
119 1.13.6.3 thorpej sc->sc_unit = ma->ma_unit;
120 1.13.6.2 nathanw
121 1.13.6.2 nathanw kbdtype = maple_get_function_data(ma->ma_devinfo,
122 1.13.6.3 thorpej MAPLE_FN_KEYBOARD) >> 24;
123 1.13.6.2 nathanw switch (kbdtype) {
124 1.13.6.2 nathanw case 1:
125 1.13.6.2 nathanw printf(": Japanese keyboard");
126 1.13.6.2 nathanw mkbd_keymapdata.layout = KB_JP;
127 1.13.6.2 nathanw break;
128 1.13.6.2 nathanw case 2:
129 1.13.6.2 nathanw printf(": US keyboard");
130 1.13.6.2 nathanw mkbd_keymapdata.layout = KB_US;
131 1.13.6.2 nathanw break;
132 1.13.6.2 nathanw case 3:
133 1.13.6.2 nathanw printf(": European keyboard");
134 1.13.6.2 nathanw mkbd_keymapdata.layout = KB_UK;
135 1.13.6.2 nathanw break;
136 1.13.6.2 nathanw default:
137 1.13.6.2 nathanw printf(": Unknown keyboard %d", kbdtype);
138 1.13.6.2 nathanw }
139 1.13.6.2 nathanw printf("\n");
140 1.13.6.2 nathanw
141 1.13.6.2 nathanw #if NWSKBD > 0
142 1.13.6.3 thorpej if ((a.console = mkbd_is_console) != 0) {
143 1.13.6.3 thorpej mkbd_is_console = 0;
144 1.13.6.3 thorpej if (!mkbd_console_initted)
145 1.13.6.3 thorpej wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata);
146 1.13.6.3 thorpej mkbd_console_softc = sc;
147 1.13.6.3 thorpej }
148 1.13.6.2 nathanw a.keymap = &mkbd_keymapdata;
149 1.13.6.2 nathanw a.accessops = &mkbd_accessops;
150 1.13.6.2 nathanw a.accesscookie = sc;
151 1.13.6.2 nathanw sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
152 1.13.6.2 nathanw #endif
153 1.13.6.2 nathanw
154 1.13.6.3 thorpej maple_set_callback(parent, sc->sc_unit, MAPLE_FN_KEYBOARD,
155 1.13.6.3 thorpej mkbd_intr, sc);
156 1.13.6.3 thorpej maple_enable_periodic(parent, sc->sc_unit, MAPLE_FN_KEYBOARD, 1);
157 1.13.6.2 nathanw }
158 1.13.6.2 nathanw
159 1.13.6.3 thorpej static int
160 1.13.6.3 thorpej mkbddetach(struct device *self, int flags)
161 1.13.6.3 thorpej {
162 1.13.6.3 thorpej struct mkbd_softc *sc = (struct mkbd_softc *) self;
163 1.13.6.3 thorpej int rv = 0;
164 1.13.6.3 thorpej
165 1.13.6.3 thorpej if (sc == mkbd_console_softc) {
166 1.13.6.3 thorpej /*
167 1.13.6.3 thorpej * Hack to allow another Maple keyboard to be new console.
168 1.13.6.3 thorpej * XXX Should some other type device can be console.
169 1.13.6.3 thorpej */
170 1.13.6.3 thorpej printf("%s: was console keyboard\n", sc->sc_dev.dv_xname);
171 1.13.6.3 thorpej wskbd_cndetach();
172 1.13.6.3 thorpej mkbd_console_softc = NULL;
173 1.13.6.3 thorpej mkbd_console_initted = 0;
174 1.13.6.3 thorpej mkbd_is_console = 1;
175 1.13.6.3 thorpej }
176 1.13.6.3 thorpej if (sc->sc_wskbddev)
177 1.13.6.3 thorpej rv = config_detach(sc->sc_wskbddev, flags);
178 1.13.6.2 nathanw
179 1.13.6.3 thorpej return rv;
180 1.13.6.3 thorpej }
181 1.13.6.2 nathanw
182 1.13.6.2 nathanw int
183 1.13.6.2 nathanw mkbd_enable(void *v, int on)
184 1.13.6.2 nathanw {
185 1.13.6.2 nathanw
186 1.13.6.2 nathanw return (0);
187 1.13.6.2 nathanw }
188 1.13.6.2 nathanw
189 1.13.6.2 nathanw void
190 1.13.6.2 nathanw mkbd_set_leds(void *v, int on)
191 1.13.6.2 nathanw {
192 1.13.6.2 nathanw }
193 1.13.6.2 nathanw
194 1.13.6.2 nathanw int
195 1.13.6.2 nathanw mkbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
196 1.13.6.2 nathanw {
197 1.13.6.2 nathanw
198 1.13.6.2 nathanw switch (cmd) {
199 1.13.6.2 nathanw case WSKBDIO_GTYPE:
200 1.13.6.3 thorpej *(int *) data = WSKBD_TYPE_MAPLE;
201 1.13.6.2 nathanw return (0);
202 1.13.6.2 nathanw case WSKBDIO_SETLEDS:
203 1.13.6.2 nathanw return (0);
204 1.13.6.2 nathanw case WSKBDIO_GETLEDS:
205 1.13.6.2 nathanw *(int *) data = 0;
206 1.13.6.2 nathanw return (0);
207 1.13.6.2 nathanw case WSKBDIO_BELL:
208 1.13.6.2 nathanw case WSKBDIO_COMPLEXBELL:
209 1.13.6.2 nathanw return (0);
210 1.13.6.2 nathanw }
211 1.13.6.2 nathanw
212 1.13.6.2 nathanw return (EPASSTHROUGH);
213 1.13.6.2 nathanw }
214 1.13.6.2 nathanw
215 1.13.6.2 nathanw int
216 1.13.6.2 nathanw mkbd_cnattach()
217 1.13.6.2 nathanw {
218 1.13.6.2 nathanw
219 1.13.6.2 nathanw wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata);
220 1.13.6.2 nathanw mkbd_console_initted = 1;
221 1.13.6.3 thorpej mkbd_is_console = 1;
222 1.13.6.2 nathanw
223 1.13.6.2 nathanw return (0);
224 1.13.6.2 nathanw }
225 1.13.6.2 nathanw
226 1.13.6.2 nathanw static int polledkey;
227 1.13.6.2 nathanw extern int maple_polling;
228 1.13.6.2 nathanw
229 1.13.6.2 nathanw #define SHIFT_KEYCODE_BASE 0xe0
230 1.13.6.2 nathanw #define UP_KEYCODE_FLAG 0x1000
231 1.13.6.2 nathanw
232 1.13.6.2 nathanw #define KEY_UP(n) do { \
233 1.13.6.2 nathanw if (maple_polling) \
234 1.13.6.2 nathanw polledkey = (n)|UP_KEYCODE_FLAG; \
235 1.13.6.2 nathanw else \
236 1.13.6.2 nathanw wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, (n)); \
237 1.13.6.2 nathanw } while (/*CONSTCOND*/0)
238 1.13.6.2 nathanw
239 1.13.6.2 nathanw #define KEY_DOWN(n) do { \
240 1.13.6.2 nathanw if (maple_polling) \
241 1.13.6.2 nathanw polledkey = (n); \
242 1.13.6.2 nathanw else \
243 1.13.6.2 nathanw wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, (n)); \
244 1.13.6.2 nathanw } while (/*CONSTCOND*/0)
245 1.13.6.2 nathanw
246 1.13.6.2 nathanw #define SHIFT_UP(n) KEY_UP((n) | SHIFT_KEYCODE_BASE)
247 1.13.6.2 nathanw #define SHIFT_DOWN(n) KEY_DOWN((n) | SHIFT_KEYCODE_BASE)
248 1.13.6.2 nathanw
249 1.13.6.2 nathanw static void
250 1.13.6.3 thorpej mkbd_intr(void *arg, struct maple_response *response, int sz, int flags)
251 1.13.6.2 nathanw {
252 1.13.6.3 thorpej struct mkbd_softc *sc = arg;
253 1.13.6.3 thorpej struct mkbd_condition *kbddata = (void *) response->data;
254 1.13.6.2 nathanw
255 1.13.6.3 thorpej if ((flags & MAPLE_FLAG_PERIODIC) &&
256 1.13.6.3 thorpej sz >= sizeof(struct mkbd_condition)) {
257 1.13.6.2 nathanw int i, j, v;
258 1.13.6.2 nathanw
259 1.13.6.2 nathanw v = sc->sc_condition.shift & ~kbddata->shift;
260 1.13.6.2 nathanw if (v)
261 1.13.6.2 nathanw for (i = 0; i < 8; i++)
262 1.13.6.2 nathanw if (v & (1 << i))
263 1.13.6.2 nathanw SHIFT_UP(i);
264 1.13.6.2 nathanw
265 1.13.6.2 nathanw v = kbddata->shift & ~sc->sc_condition.shift;
266 1.13.6.2 nathanw if (v)
267 1.13.6.2 nathanw for (i = 0; i < 8; i++)
268 1.13.6.2 nathanw if (v & (1 << i))
269 1.13.6.2 nathanw SHIFT_DOWN(i);
270 1.13.6.2 nathanw
271 1.13.6.2 nathanw for (i = 0, j = 0; i < 6; i++)
272 1.13.6.2 nathanw if (sc->sc_condition.key[i] < 4)
273 1.13.6.2 nathanw break;
274 1.13.6.2 nathanw else if (sc->sc_condition.key[i] == kbddata->key[j])
275 1.13.6.2 nathanw j++;
276 1.13.6.2 nathanw else
277 1.13.6.2 nathanw KEY_UP(sc->sc_condition.key[i]);
278 1.13.6.2 nathanw
279 1.13.6.2 nathanw for (; j < 6; j++)
280 1.13.6.2 nathanw if (kbddata->key[j] < 4)
281 1.13.6.2 nathanw break;
282 1.13.6.2 nathanw else
283 1.13.6.2 nathanw KEY_DOWN(kbddata->key[j]);
284 1.13.6.2 nathanw
285 1.13.6.2 nathanw memcpy(&sc->sc_condition, kbddata,
286 1.13.6.2 nathanw sizeof(struct mkbd_condition));
287 1.13.6.2 nathanw }
288 1.13.6.2 nathanw }
289 1.13.6.2 nathanw
290 1.13.6.2 nathanw void
291 1.13.6.3 thorpej mkbd_cngetc(void *v, u_int *type, int *data)
292 1.13.6.2 nathanw {
293 1.13.6.2 nathanw int key;
294 1.13.6.2 nathanw
295 1.13.6.2 nathanw polledkey = -1;
296 1.13.6.2 nathanw maple_polling = 1;
297 1.13.6.2 nathanw while (polledkey == -1) {
298 1.13.6.2 nathanw if (mkbd_console_softc != NULL ||
299 1.13.6.2 nathanw mkbd_console_softc->sc_parent != NULL) {
300 1.13.6.2 nathanw int t;
301 1.13.6.2 nathanw for (t = 0; t < 1000000; t++);
302 1.13.6.2 nathanw maple_run_polling(mkbd_console_softc->sc_parent);
303 1.13.6.2 nathanw }
304 1.13.6.2 nathanw }
305 1.13.6.2 nathanw maple_polling = 0;
306 1.13.6.2 nathanw key = polledkey;
307 1.13.6.2 nathanw
308 1.13.6.2 nathanw *data = key & ~UP_KEYCODE_FLAG;
309 1.13.6.2 nathanw *type = (key & UP_KEYCODE_FLAG) ?
310 1.13.6.2 nathanw WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
311 1.13.6.2 nathanw }
312 1.13.6.2 nathanw
313 1.13.6.2 nathanw void
314 1.13.6.2 nathanw mkbd_cnpollc(void *v, int on)
315 1.13.6.2 nathanw {
316 1.13.6.2 nathanw }
317