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