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