1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/* $NetBSD: nextkbd.c,v 1.19 2023/02/03 23:13:00 tsutsui Exp $ */
/*
* Copyright (c) 1998 Matt DeBergalis
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Matt DeBergalis
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nextkbd.c,v 1.19 2023/02/03 23:13:00 tsutsui Exp $");
#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/errno.h>
#include <sys/queue.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/intr.h>
#include <machine/autoconf.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsksymvar.h>
#include <next68k/dev/nextkbdvar.h>
#include <next68k/dev/wskbdmap_next.h>
#include <next68k/next68k/isr.h>
#include <next68k/dev/intiovar.h>
struct nextkbd_internal {
int num_ints; /* interrupt total */
int polling;
int isconsole;
bus_space_tag_t iot;
bus_space_handle_t ioh;
struct nextkbd_softc *t_sc; /* back pointer */
uint32_t mods;
};
struct mon_regs {
uint32_t mon_csr;
uint32_t mon_1;
uint32_t mon_data;
};
static int attached = 0;
int nextkbd_match(device_t, cfdata_t, void *);
void nextkbd_attach(device_t, device_t, void *);
int nextkbc_cnattach(bus_space_tag_t);
CFATTACH_DECL_NEW(nextkbd, sizeof(struct nextkbd_softc),
nextkbd_match, nextkbd_attach, NULL, NULL);
int nextkbd_enable(void *, int);
void nextkbd_set_leds(void *, int);
int nextkbd_ioctl(void *, u_long, void *, int, struct lwp *);
const struct wskbd_accessops nextkbd_accessops = {
nextkbd_enable,
nextkbd_set_leds,
nextkbd_ioctl,
};
void nextkbd_cngetc(void *, u_int *, int *);
void nextkbd_cnpollc(void *, int);
const struct wskbd_consops nextkbd_consops = {
nextkbd_cngetc,
nextkbd_cnpollc,
};
const struct wskbd_mapdata nextkbd_keymapdata = {
nextkbd_keydesctab,
KB_US,
};
static int nextkbd_read_data(struct nextkbd_internal *);
static int nextkbd_decode(struct nextkbd_internal *, int, u_int *, int *);
static struct nextkbd_internal nextkbd_consdata;
static int nextkbd_is_console(bus_space_tag_t);
int nextkbdhard(void *);
static int
nextkbd_is_console(bus_space_tag_t bst)
{
return (nextkbd_consdata.isconsole && (bst == nextkbd_consdata.iot));
}
int
nextkbd_match(device_t parent, cfdata_t match, void *aux)
{
struct intio_attach_args *ia = (struct intio_attach_args *)aux;
if (attached)
return 0;
ia->ia_addr = (void *)NEXT_P_MON;
return 1;
}
void
nextkbd_attach(device_t parent, device_t self, void *aux)
{
struct nextkbd_softc *sc = device_private(self);
struct intio_attach_args *ia = aux;
int isconsole;
struct wskbddev_attach_args a;
printf("\n");
isconsole = nextkbd_is_console(ia->ia_bst); /* XXX */
if (isconsole) {
sc->id = &nextkbd_consdata;
} else {
sc->id = kmem_zalloc(sizeof(struct nextkbd_internal), KM_SLEEP);
sc->id->iot = ia->ia_bst;
if (bus_space_map(sc->id->iot, NEXT_P_MON,
sizeof(struct mon_regs), 0, &sc->id->ioh)) {
printf("%s: can't map mon status control register\n",
device_xname(self));
return;
}
}
sc->id->t_sc = sc; /* set back pointer */
isrlink_autovec(nextkbdhard, sc, NEXT_I_IPL(NEXT_I_KYBD_MOUSE),
0, NULL);
INTR_ENABLE(NEXT_I_KYBD_MOUSE);
a.console = isconsole;
a.keymap = &nextkbd_keymapdata;
a.accessops = &nextkbd_accessops;
a.accesscookie = sc;
/*
* Attach the wskbd, saving a handle to it.
* XXX XXX XXX
*/
sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
attached = 1;
}
int
nextkbd_enable(void *v, int on)
{
/* XXX not sure if this should do anything */
/* printf("nextkbd_enable %d\n", on); */
return 0;
}
void
nextkbd_set_leds(void *v, int leds)
{
struct nextkbd_softc *sc = v;
uint32_t hw_leds = 0;
int s;
sc->sc_leds &= ~ NEXT_WSKBD_LEDS;
sc->sc_leds |= (leds & NEXT_WSKBD_LEDS);
if (sc->sc_leds & WSKBD_LED_CAPS) {
hw_leds |= 0x30000;
}
s = spltty();
bus_space_write_1(sc->id->iot, sc->id->ioh, 3, 0xc5);
/*
* @@@ need to add:
* if bit 7 of @ioh+0 set:
* repeat 2
* wait until bit 6 of @ioh+2 clears
*/
bus_space_write_4(sc->id->iot, sc->id->ioh, 4, hw_leds);
/*
* @@@ need to add:
* wait until bit 4 of @ioh+0 (@ioh+2 if bit 7 was set above)
* clears
*/
splx(s);
}
int
nextkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
struct nextkbd_softc *sc = v;
switch (cmd) {
case WSKBDIO_GTYPE:
/* XXX */
*(int *)data = WSKBD_TYPE_NEXT;
return 0;
case WSKBDIO_SETLEDS:
nextkbd_set_leds (sc, *(int *)data);
return 0;
case WSKBDIO_GETLEDS:
*(int *)data = sc->sc_leds & NEXT_WSKBD_LEDS;
return 0;
case WSKBDIO_COMPLEXBELL:
return 0;
}
return EPASSTHROUGH;
}
int
nextkbdhard(void *arg)
{
struct nextkbd_softc *sc = arg;
int type, key, val;
if (!INTR_OCCURRED(NEXT_I_KYBD_MOUSE))
return 0;
#define CSR_INT 0x00800000
#define CSR_DATA 0x00400000
#define KD_KEYMASK 0x007f
#define KD_DIRECTION 0x0080 /* pressed or released */
#define KD_CNTL 0x0100
#define KD_LSHIFT 0x0200
#define KD_RSHIFT 0x0400
#define KD_LCOMM 0x0800
#define KD_RCOMM 0x1000
#define KD_LALT 0x2000
#define KD_RALT 0x4000
#define KD_VALID 0x8000 /* only set for scancode keys ? */
#define KD_MODS 0x4f00
val = nextkbd_read_data(sc->id);
if ((val != -1) && nextkbd_decode(sc->id, val, &type, &key)) {
wskbd_input(sc->sc_wskbddev, type, key);
}
return 1;
}
int
nextkbd_cnattach(bus_space_tag_t bst)
{
bus_space_handle_t bsh;
if (bus_space_map(bst, NEXT_P_MON, sizeof(struct mon_regs), 0, &bsh))
return ENXIO;
memset(&nextkbd_consdata, 0, sizeof(nextkbd_consdata));
nextkbd_consdata.iot = bst;
nextkbd_consdata.ioh = bsh;
nextkbd_consdata.isconsole = 1;
wskbd_cnattach(&nextkbd_consops, &nextkbd_consdata,
&nextkbd_keymapdata);
return 0;
}
void
nextkbd_cngetc(void *v, u_int *type, int *data)
{
struct nextkbd_internal *t = v;
int val;
for (;;) {
if (INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) {
val = nextkbd_read_data(t);
if (val != -1 && nextkbd_decode(t, val, type, data))
return;
}
}
}
void
nextkbd_cnpollc(void *v, int on)
{
struct nextkbd_internal *t = v;
t->polling = on;
if (on) {
INTR_DISABLE(NEXT_I_KYBD_MOUSE);
} else {
INTR_ENABLE(NEXT_I_KYBD_MOUSE);
}
}
static int
nextkbd_read_data(struct nextkbd_internal *id)
{
unsigned char device;
struct mon_regs stat = { 0 };
bus_space_read_region_4(id->iot, id->ioh, 0, &stat, 3);
if ((stat.mon_csr & CSR_INT) != 0 &&
(stat.mon_csr & CSR_DATA) != 0) {
stat.mon_csr &= ~CSR_INT;
id->num_ints++;
bus_space_write_4(id->iot, id->ioh, 0, stat.mon_csr);
device = stat.mon_data >> 28;
if (device != 1)
return -1; /* XXX: mouse */
return stat.mon_data & 0xffff;
}
return -1;
}
static int
nextkbd_decode(struct nextkbd_internal *id, int datain, u_int *type,
int *dataout)
{
#if 0
printf("datain %08x mods %08x\n", datain, id->mods);
#endif
if (((datain ^ id->mods) & KD_LSHIFT) != 0) {
id->mods ^= KD_LSHIFT;
*dataout = 90;
if (datain & KD_LSHIFT)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_RSHIFT) != 0) {
id->mods ^= KD_RSHIFT;
*dataout = 91;
if ((datain & KD_RSHIFT) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_LALT) != 0) {
id->mods ^= KD_LALT;
*dataout = 92;
if ((datain & KD_LALT) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_RALT) != 0) {
id->mods ^= KD_RALT;
*dataout = 93;
if ((datain & KD_RALT) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_CNTL) != 0) {
id->mods ^= KD_CNTL;
*dataout = 94;
if ((datain & KD_CNTL) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_LCOMM) != 0) {
id->mods ^= KD_LCOMM;
*dataout = 95;
if ((datain & KD_LCOMM) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if (((datain ^ id->mods) & KD_RCOMM) != 0) {
id->mods ^= KD_RCOMM;
*dataout = 96;
if ((datain & KD_RCOMM) != 0)
*type = WSCONS_EVENT_KEY_DOWN;
else
*type = WSCONS_EVENT_KEY_UP;
} else if ((datain & KD_KEYMASK) != 0) {
if ((datain & KD_DIRECTION) != 0)
*type = WSCONS_EVENT_KEY_UP;
else
*type = WSCONS_EVENT_KEY_DOWN;
*dataout = (datain & KD_KEYMASK);
} else {
*dataout = 0;
}
return 1;
}
|