dtkbd.c revision 1.1.2.2 1 /* $NetBSD: dtkbd.c,v 1.1.2.2 2002/03/15 14:26:48 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: dtkbd.c,v 1.1.2.2 2002/03/15 14:26:48 ad Exp $");
41
42 #include "locators.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 #include <sys/ioctl.h>
48 #include <sys/callout.h>
49
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wskbdvar.h>
52 #include <dev/wscons/wsksymdef.h>
53 #include <dev/wscons/wsksymvar.h>
54 #include <dev/dec/wskbdmap_lk201.h>
55
56 #include <machine/bus.h>
57
58 #include <pmax/tc/dtreg.h>
59 #include <pmax/tc/dtvar.h>
60
61 struct dtkbd_softc {
62 struct device sc_dv;
63 struct device *sc_wskbddev;
64 int sc_enabled;
65 };
66
67 int dtkbd_match(struct device *, struct cfdata *, void *);
68 void dtkbd_attach(struct device *, struct device *, void *);
69 int dtkbd_enable(void *, int);
70 int dtkbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
71 void dtkbd_cngetc(void *, u_int *, int *);
72 void dtkbd_cnpollc(void *, int);
73 int dtkbd_process_msg(struct dt_msg *, u_int *, int *);
74 void dtkbd_handler(void *);
75 void dtkbd_set_leds(void *, int);
76 void dtkbd_cnattach(void);
77
78 const struct wskbd_accessops dtkbd_accessops = {
79 dtkbd_enable,
80 dtkbd_set_leds,
81 dtkbd_ioctl,
82 };
83
84 const struct wskbd_consops dtkbd_consops = {
85 dtkbd_cngetc,
86 dtkbd_cnpollc,
87 };
88
89 struct cfattach dtkbd_ca = {
90 sizeof(struct dtkbd_softc), dtkbd_match, dtkbd_attach,
91 };
92
93 const struct wskbd_mapdata dtkbd_keymapdata = {
94 lkkbd_keydesctab,
95 #ifdef DTKBD_LAYOUT
96 DTKBD_LAYOUT,
97 #else
98 KB_US | KB_LK401,
99 #endif
100 };
101
102 int dtkbd_isconsole;
103 int dtkbd_map[10];
104 int dtkbd_maplen;
105
106 int
107 dtkbd_match(struct device *parent, struct cfdata *cf, void *aux)
108 {
109 struct dt_attach_args *dta;
110 struct dt_ident ident;
111
112 dta = aux;
113
114 /*
115 * Allow hard-wiring of addresses.
116 */
117 if (cf->cf_loc[DTCF_ADDR] == dta->dta_addr)
118 return (2);
119
120 /*
121 * The keyboard and mouse addresses are often swapped. So, we
122 * accept the standard address for either, and ask the device to
123 * identify itself.
124 */
125 if (cf->cf_loc[DTCF_ADDR] == DTCF_ADDR_DEFAULT &&
126 (dta->dta_addr == DT_ADDR_KBD || dta->dta_addr == DT_ADDR_MOUSE)) {
127 dt_identify(dta->dta_addr, &ident);
128 return (strncmp(ident.vendor, "DEC ", 8) == 0 &&
129 strncmp(ident.module, "LK501-", 6) == 0);
130 }
131
132 return (0);
133 }
134
135 void
136 dtkbd_attach(struct device *parent, struct device *self, void *aux)
137 {
138 struct dt_softc *dt;
139 struct dtkbd_softc *sc;
140 struct dt_attach_args *dta;
141 struct wskbddev_attach_args a;
142
143 dt = (struct dt_softc *)parent;
144 sc = (struct dtkbd_softc *)self;
145 dta = aux;
146
147 printf("\n");
148
149 if (dt_establish_handler(dt, dta->dta_addr, self, dtkbd_handler)) {
150 printf("%s: unable to establish handler\n", self->dv_xname);
151 return;
152 }
153
154 sc->sc_enabled = 1;
155
156 a.console = dtkbd_isconsole;
157 a.keymap = &dtkbd_keymapdata;
158 a.accessops = &dtkbd_accessops;
159 a.accesscookie = sc;
160 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
161 }
162
163 void
164 dtkbd_cnattach(void)
165 {
166
167 dtkbd_isconsole = 1;
168 dt_cninit();
169
170 wskbd_cnattach(&dtkbd_consops, &dtkbd_map, &dtkbd_keymapdata);
171 }
172
173 int
174 dtkbd_enable(void *v, int on)
175 {
176 struct dtkbd_softc *sc;
177
178 sc = v;
179 sc->sc_enabled = on;
180
181 return (0);
182 }
183
184 void
185 dtkbd_cngetc(void *v, u_int *type, int *data)
186 {
187 struct dt_msg msg;
188 static u_int types[20];
189 static int cnt, i, vals[20];
190
191 if (i >= cnt) {
192 for (;;) {
193 if (dt_msg_get(&msg, 0) == DT_GET_DONE)
194 if (msg.src == DT_ADDR_KBD)
195 break;
196 DELAY(1000);
197 }
198
199 cnt = dtkbd_process_msg(&msg, types, vals);
200 i = 0;
201 }
202
203 *type = types[i++];
204 *data = vals[i++];
205 }
206
207 void
208 dtkbd_cnpollc(void *v, int on)
209 {
210
211 /* XXX */
212 }
213
214 int
215 dtkbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
216 {
217 struct dtkbd_softc *sc;
218
219 sc = (struct dtkbd_softc *)v;
220
221 switch (cmd) {
222 case WSKBDIO_GTYPE:
223 *(int *)data = WSKBD_TYPE_LK201;
224 return 0;
225 default:
226 /* XXX */
227 return (-1);
228 }
229 }
230
231 void
232 dtkbd_set_leds(void *v, int state)
233 {
234
235 /* XXX */
236 }
237
238 void
239 dtkbd_handler(void *cookie)
240 {
241 struct dtkbd_softc *sc;
242 struct dt_softc *dt;
243 struct dt_device *dtdv;
244 struct dt_msg *msg;
245 u_int types[20];
246 int i, cnt, vals[20];
247
248 dtdv = cookie;
249 sc = (struct dtkbd_softc *)dtdv->dtdv_dv;
250 dt = (struct dt_softc *)sc->sc_dv.dv_parent;
251
252 while ((msg = dt_msg_dequeue(dtdv)) != NULL) {
253 if (sc->sc_enabled) {
254 cnt = dtkbd_process_msg(msg, types, vals);
255 for (i = 0; i < cnt; i++)
256 wskbd_input(sc->sc_wskbddev, types[i],
257 vals[i]);
258 }
259
260 dt_msg_release(dt, msg);
261 }
262 }
263
264 int
265 dtkbd_process_msg(struct dt_msg *msg, u_int *types, int *vals)
266 {
267 u_int len, c, count;
268 int i, j;
269
270 len = msg->code.val.len;
271
272 if ((msg->body[0] < DT_KBD_KEY_MIN && msg->body[0] != DT_KBD_EMPTY) ||
273 len > 10) {
274 printf("dtkbd0: error: %x %x %x\n", len, msg->body[0],
275 msg->body[1]);
276
277 /*
278 * Fake an "all ups" to avoid the stuck key syndrome.
279 */
280 msg->body[0] = DT_KBD_EMPTY;
281 len = 1;
282 }
283
284 if (msg->body[0] == DT_KBD_EMPTY) {
285 types[0] = WSCONS_EVENT_ALL_KEYS_UP;
286 vals[0] = 0;
287 dtkbd_maplen = 0;
288 return (1);
289 }
290
291 count = 0;
292
293 for (i = 0; i < len; i++) {
294 c = msg->body[i];
295
296 for (j = 0; j < dtkbd_maplen; j++)
297 if (dtkbd_map[j] == c)
298 break;
299
300 if (j == dtkbd_maplen) {
301 types[count] = WSCONS_EVENT_KEY_DOWN;
302 vals[count] = c - MIN_LK201_KEY;
303 count++;
304 }
305 }
306
307 for (j = 0; j < dtkbd_maplen; j++) {
308 c = dtkbd_map[j];
309
310 for (i = 0; i < len; i++)
311 if (msg->body[i] == c)
312 break;
313
314 if (i == len) {
315 types[count] = WSCONS_EVENT_KEY_UP;
316 vals[count] = c - MIN_LK201_KEY;
317 count++;
318 }
319 }
320
321 memcpy(dtkbd_map, msg->body, len);
322 dtkbd_maplen = len;
323
324 return (count);
325 }
326