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