dzkbd.c revision 1.33 1 /* $NetBSD: dzkbd.c,v 1.33 2025/03/04 16:18:27 hans Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
41 */
42
43 /*
44 * LK200/LK400 keyboard attached to line 0 of the DZ*-11
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: dzkbd.c,v 1.33 2025/03/04 16:18:27 hans Exp $");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/ioctl.h>
54 #include <sys/syslog.h>
55 #include <sys/malloc.h>
56 #include <sys/intr.h>
57
58 #include <dev/wscons/wsconsio.h>
59 #include <dev/wscons/wskbdvar.h>
60 #include <dev/wscons/wsksymdef.h>
61 #include <dev/wscons/wsksymvar.h>
62 #include <dev/dec/wskbdmap_lk201.h>
63
64 #include <sys/bus.h>
65
66 #include <dev/dec/dzreg.h>
67 #include <dev/dec/dzvar.h>
68 #include <dev/dec/dzkbdvar.h>
69 #include <dev/dec/lk201reg.h>
70 #include <dev/dec/lk201var.h>
71
72 #include "locators.h"
73
74 struct dzkbd_internal {
75 struct dz_linestate *dzi_ls;
76 struct lk201_state dzi_ks;
77 };
78
79 struct dzkbd_internal dzkbd_console_internal;
80
81 struct dzkbd_softc {
82 struct dzkbd_internal *sc_itl;
83
84 int sc_enabled;
85 int kbd_type;
86
87 device_t sc_wskbddev;
88 };
89
90 static int dzkbd_input(void *, int);
91
92 static int dzkbd_match(device_t, cfdata_t, void *);
93 static void dzkbd_attach(device_t, device_t, void *);
94
95 CFATTACH_DECL_NEW(dzkbd, sizeof(struct dzkbd_softc),
96 dzkbd_match, dzkbd_attach, NULL, NULL);
97
98 static int dzkbd_enable(void *, int);
99 static void dzkbd_set_leds(void *, int);
100 static int dzkbd_ioctl(void *, u_long, void *, int, struct lwp *);
101
102 const struct wskbd_accessops dzkbd_accessops = {
103 dzkbd_enable,
104 dzkbd_set_leds,
105 dzkbd_ioctl,
106 };
107
108 static void dzkbd_cngetc(void *, u_int *, int *);
109 static void dzkbd_cnpollc(void *, int);
110
111 const struct wskbd_consops dzkbd_consops = {
112 dzkbd_cngetc,
113 dzkbd_cnpollc,
114 };
115
116 static int dzkbd_sendchar(void *, u_char);
117
118 const struct wskbd_mapdata dzkbd_keymapdata = {
119 lkkbd_keydesctab,
120 #ifdef DZKBD_LAYOUT
121 DZKBD_LAYOUT,
122 #else
123 KB_US | KB_LK401,
124 #endif
125 };
126
127 /*
128 * kbd_match: how is this dz line configured?
129 */
130 static int
131 dzkbd_match(device_t parent, cfdata_t cf, void *aux)
132 {
133 struct dzkm_attach_args *daa = aux;
134
135 /* Exact match is better than wildcard. */
136 if (cf->cf_loc[DZCF_LINE] == daa->daa_line)
137 return 2;
138
139 /* This driver accepts wildcard. */
140 if (cf->cf_loc[DZCF_LINE] == DZCF_LINE_DEFAULT)
141 return 1;
142
143 return 0;
144 }
145
146 static void
147 dzkbd_attach(device_t parent, device_t self, void *aux)
148 {
149 struct dz_softc *dz = device_private(parent);
150 struct dzkbd_softc *dzkbd = device_private(self);
151 struct dzkm_attach_args *daa = aux;
152 struct dz_linestate *ls;
153 struct dzkbd_internal *dzi;
154 struct wskbddev_attach_args a;
155 int isconsole;
156
157 dz->sc_dz[daa->daa_line].dz_catch = dzkbd_input;
158 dz->sc_dz[daa->daa_line].dz_private = dzkbd;
159 ls = &dz->sc_dz[daa->daa_line];
160
161 isconsole = (daa->daa_flags & DZKBD_CONSOLE);
162
163 if (isconsole)
164 dzi = &dzkbd_console_internal;
165 else
166 dzi = malloc(sizeof(struct dzkbd_internal), M_DEVBUF, M_WAITOK);
167
168 dzi->dzi_ks.attmt.sendchar = dzkbd_sendchar;
169 dzi->dzi_ks.attmt.cookie = ls;
170 dzi->dzi_ls = ls;
171 dzkbd->sc_itl = dzi;
172
173 aprint_normal("\n");
174
175 DELAY(100000);
176 lk201_init(&dzi->dzi_ks);
177
178 /* XXX should identify keyboard ID here XXX */
179 /* XXX layout and the number of LED is varying XXX */
180
181 dzkbd->kbd_type = WSKBD_TYPE_LK201;
182
183 dzkbd->sc_enabled = 1;
184
185 a.console = isconsole;
186 a.keymap = &dzkbd_keymapdata;
187 a.accessops = &dzkbd_accessops;
188 a.accesscookie = dzkbd;
189
190 dzkbd->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
191 }
192
193 int
194 dzkbd_cnattach(struct dz_linestate *ls)
195 {
196
197 dzkbd_console_internal.dzi_ks.attmt.sendchar = dzkbd_sendchar;
198 dzkbd_console_internal.dzi_ks.attmt.cookie = ls;
199 lk201_init(&dzkbd_console_internal.dzi_ks);
200 dzkbd_console_internal.dzi_ls = ls;
201
202 wskbd_cnattach(&dzkbd_consops, &dzkbd_console_internal,
203 &dzkbd_keymapdata);
204
205 return 0;
206 }
207
208 static int
209 dzkbd_enable(void *v, int on)
210 {
211 struct dzkbd_softc *sc = v;
212
213 sc->sc_enabled = on;
214 return 0;
215 }
216
217 static int
218 dzkbd_sendchar(void *v, u_char c)
219 {
220 struct dz_linestate *ls = v;
221 int s;
222
223 s = spltty();
224 dzputc(ls, c);
225 splx(s);
226 return (0);
227 }
228
229 static void
230 dzkbd_cngetc(void *v, u_int *type, int *data)
231 {
232 struct dzkbd_internal *dzi = v;
233 int c;
234
235 do {
236 c = dzgetc(dzi->dzi_ls);
237 } while (lk201_decode(&dzi->dzi_ks, 0, c, type, data) == LKD_NODATA);
238 }
239
240 static void
241 dzkbd_cnpollc(void *v, int on)
242 {
243 #if 0
244 struct dzkbd_internal *dzi = v;
245 #endif
246 }
247
248 static void
249 dzkbd_set_leds(void *v, int leds)
250 {
251 struct dzkbd_softc *sc = (struct dzkbd_softc *)v;
252
253 //printf("dzkbd_set_leds\n");
254 lk201_set_leds(&sc->sc_itl->dzi_ks, leds);
255 }
256
257 static int
258 dzkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
259 {
260 struct dzkbd_softc *sc = (struct dzkbd_softc *)v;
261
262 switch (cmd) {
263 case WSKBDIO_GTYPE:
264 *(int *)data = sc->kbd_type;
265 return 0;
266 case WSKBDIO_SETLEDS:
267 lk201_set_leds(&sc->sc_itl->dzi_ks, *(int *)data);
268 return 0;
269 case WSKBDIO_GETLEDS:
270 /* XXX don't dig in kbd internals */
271 *(int *)data = sc->sc_itl->dzi_ks.leds_state;
272 return 0;
273 case WSKBDIO_COMPLEXBELL:
274 lk201_bell(&sc->sc_itl->dzi_ks,
275 (struct wskbd_bell_data *)data);
276 return 0;
277 case WSKBDIO_SETKEYCLICK:
278 lk201_set_keyclick(&sc->sc_itl->dzi_ks, *(int *)data);
279 return 0;
280 case WSKBDIO_GETKEYCLICK:
281 /* XXX don't dig in kbd internals */
282 *(int *)data = sc->sc_itl->dzi_ks.kcvol;
283 return 0;
284 }
285 return (EPASSTHROUGH);
286 }
287
288 static int
289 dzkbd_input(void *v, int data)
290 {
291 struct dzkbd_softc *sc = (struct dzkbd_softc *)v;
292 u_int type;
293 int val;
294 int decode;
295
296 do {
297 decode = lk201_decode(&sc->sc_itl->dzi_ks, 1,
298 data, &type, &val);
299 if (decode != LKD_NODATA)
300 wskbd_input(sc->sc_wskbddev, type, val);
301 } while (decode == LKD_MORE);
302
303 return(1);
304 }
305