dt.c revision 1.12 1 /* $NetBSD: dt.c,v 1.12 2015/06/28 09:15:45 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2003 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Ralph Campbell and Rick Macklem.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)dtop.c 8.2 (Berkeley) 11/30/93
64 */
65
66 /*
67 * Mach Operating System
68 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
69 * All Rights Reserved.
70 *
71 * Permission to use, copy, modify and distribute this software and its
72 * documentation is hereby granted, provided that both the copyright
73 * notice and this permission notice appear in all copies of the
74 * software, derivative works or modified versions, and any portions
75 * thereof, and that both notices appear in supporting documentation.
76 *
77 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
78 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
79 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
80 *
81 * Carnegie Mellon requests users of this software to return to
82 *
83 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
84 * School of Computer Science
85 * Carnegie Mellon University
86 * Pittsburgh PA 15213-3890
87 *
88 * any improvements or extensions that they make and grant Carnegie the
89 * rights to redistribute these changes.
90 */
91 /*
92 * Author: Alessandro Forin, Carnegie Mellon University
93 *
94 * Hardware-level operations for the Desktop serial line
95 * bus (i2c aka ACCESS).
96 */
97 /************************************************************
98 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
99 and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
100
101 All Rights Reserved
102
103 Permission to use, copy, modify, and distribute this software and its
104 documentation for any purpose and without fee is hereby granted,
105 provided that the above copyright notice appear in all copies and that
106 both that copyright notice and this permission notice appear in
107 supporting documentation, and that the names of Digital or MIT not be
108 used in advertising or publicity pertaining to distribution of the
109 software without specific, written prior permission.
110
111 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
112 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
113 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
114 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
115 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
116 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
117 SOFTWARE.
118
119 ********************************************************/
120
121 /*
122 * ACCESS.bus device support for the Personal DECstation. This code handles
123 * only the keyboard and mouse, and will likely not work if other ACCESS.bus
124 * devices are physically attached to the system.
125 *
126 * Since we do not know how to drive the hardware (the only reference being
127 * Mach), we can't identify which devices are connected to the system by
128 * sending idenfication requests. With only a mouse and keyboard attached
129 * to the system, we do know which two slave addresses will be in use.
130 * However, we don't know which is the mouse, and which is the keyboard.
131 * So, we resort to inspecting device reports and making an educated guess
132 * as to which is which.
133 */
134
135 #include <sys/cdefs.h>
136 __KERNEL_RCSID(0, "$NetBSD: dt.c,v 1.12 2015/06/28 09:15:45 maxv Exp $");
137
138 #include <sys/param.h>
139 #include <sys/systm.h>
140 #include <sys/tty.h>
141 #include <sys/proc.h>
142 #include <sys/conf.h>
143 #include <sys/file.h>
144 #include <sys/kernel.h>
145 #include <sys/device.h>
146 #include <sys/malloc.h>
147 #include <sys/intr.h>
148
149 #include <dev/dec/lk201.h>
150
151 #include <dev/tc/tcvar.h>
152 #include <dev/tc/ioasicreg.h>
153 #include <dev/tc/ioasicvar.h>
154
155 #include <pmax/pmax/maxine.h>
156
157 #include <pmax/tc/dtreg.h>
158 #include <pmax/tc/dtvar.h>
159
160 #define DT_BUF_CNT 16
161 #define DT_ESC_CHAR 0xf8
162 #define DT_XMT_OK 0xfb
163 #define DT_MAX_POLL 0x70000 /* about half a sec */
164
165 #define DT_GET_BYTE(data) (((*(data)) >> 8) & 0xff)
166 #define DT_PUT_BYTE(data,c) { *(data) = (c) << 8; wbflush(); }
167
168 #define DT_RX_AVAIL(poll) ((*(poll) & 1) != 0)
169 #define DT_TX_AVAIL(poll) ((*(poll) & 2) != 0)
170
171 int dt_match(device_t, cfdata_t, void *);
172 void dt_attach(device_t, device_t, void *);
173 int dt_intr(void *);
174 int dt_print(void *, const char *);
175 void dt_strvis(uint8_t *, char *, int);
176 void dt_dispatch(void *);
177
178 int dt_kbd_addr = DT_ADDR_KBD;
179 struct dt_device dt_kbd_dv;
180 int dt_ms_addr = DT_ADDR_MOUSE;
181 struct dt_device dt_ms_dv;
182 struct dt_state dt_state;
183
184 CFATTACH_DECL_NEW(dt, sizeof(struct dt_softc),
185 dt_match, dt_attach, NULL, NULL);
186
187 int
188 dt_match(device_t parent, cfdata_t cf, void *aux)
189 {
190 struct ioasicdev_attach_args *d;
191
192 d = aux;
193
194 if (strcmp(d->iada_modname, "dtop") != 0)
195 return (0);
196
197 if (badaddr((void *)(d->iada_addr), 2))
198 return (0);
199
200 return (1);
201 }
202
203 void
204 dt_attach(device_t parent, device_t self, void *aux)
205 {
206 struct ioasicdev_attach_args *d;
207 struct dt_attach_args dta;
208 struct dt_softc *sc;
209 struct dt_msg *msg;
210 int i;
211
212 d = aux;
213 sc = device_private(self);
214 sc->sc_dev = self;
215
216 dt_cninit();
217
218 msg = malloc(sizeof(*msg) * DT_BUF_CNT, M_DEVBUF, M_NOWAIT);
219 if (msg == NULL) {
220 printf("%s: memory exhausted\n", device_xname(self));
221 return;
222 }
223
224 sc->sc_sih = softint_establish(SOFTINT_SERIAL, dt_dispatch, sc);
225 if (sc->sc_sih == NULL) {
226 printf("%s: memory exhausted\n", device_xname(self));
227 free(msg, M_DEVBUF);
228 return;
229 }
230
231 SIMPLEQ_INIT(&sc->sc_queue);
232 SLIST_INIT(&sc->sc_free);
233 for (i = 0; i < DT_BUF_CNT; i++, msg++)
234 SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);
235
236 ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_TTY, dt_intr, sc);
237 printf("\n");
238
239 dta.dta_addr = DT_ADDR_KBD;
240 config_found(self, &dta, dt_print);
241 dta.dta_addr = DT_ADDR_MOUSE;
242 config_found(self, &dta, dt_print);
243 }
244
245 void
246 dt_cninit(void)
247 {
248
249 dt_state.ds_poll = (volatile u_int *)
250 MIPS_PHYS_TO_KSEG1(XINE_REG_INTR);
251 dt_state.ds_data = (volatile u_int *)
252 MIPS_PHYS_TO_KSEG1(XINE_PHYS_TC_3_START + 0x280000);
253 }
254
255 int
256 dt_print(void *aux, const char *pnp)
257 {
258
259 return (QUIET);
260 }
261
262 int
263 dt_establish_handler(struct dt_softc *sc, struct dt_device *dtdv,
264 void *arg, void (*hdlr)(void *, struct dt_msg *))
265 {
266
267 dtdv->dtdv_arg = arg;
268 dtdv->dtdv_handler = hdlr;
269 return (0);
270 }
271
272 int
273 dt_intr(void *cookie)
274 {
275 struct dt_softc *sc;
276 struct dt_msg *msg, *pend;
277
278 sc = cookie;
279
280 switch (dt_msg_get(&sc->sc_msg, 1)) {
281 case DT_GET_ERROR:
282 /*
283 * Ugh! The most common occurrence of a data overrun is upon
284 * a key press and the result is a software generated "stuck
285 * key". All I can think to do is fake an "all keys up"
286 * whenever a data overrun occurs.
287 */
288 sc->sc_msg.src = dt_kbd_addr;
289 sc->sc_msg.ctl = DT_CTL(1, 0, 0);
290 sc->sc_msg.body[0] = DT_KBD_EMPTY;
291 #ifdef DIAGNOSTIC
292 printf("%s: data overrun or stray interrupt\n",
293 device_xname(sc->sc_dev));
294 #endif
295 break;
296
297 case DT_GET_DONE:
298 break;
299
300 case DT_GET_NOTYET:
301 return (1);
302 }
303
304 if ((msg = SLIST_FIRST(&sc->sc_free)) == NULL) {
305 printf("%s: input overflow\n", device_xname(sc->sc_dev));
306 return (1);
307 }
308 SLIST_REMOVE_HEAD(&sc->sc_free, chain.slist);
309 memcpy(msg, &sc->sc_msg, sizeof(*msg));
310
311 pend = SIMPLEQ_FIRST(&sc->sc_queue);
312 SIMPLEQ_INSERT_TAIL(&sc->sc_queue, msg, chain.simpleq);
313 if (pend == NULL)
314 softint_schedule(sc->sc_sih);
315
316 return (1);
317 }
318
319 void
320 dt_dispatch(void *cookie)
321 {
322 struct dt_softc *sc;
323 struct dt_msg *msg;
324 int s;
325 struct dt_device *dtdv;
326
327 sc = cookie;
328 msg = NULL;
329
330 for (;;) {
331 s = spltty();
332 if (msg != NULL)
333 SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);
334 msg = SIMPLEQ_FIRST(&sc->sc_queue);
335 if (msg != NULL)
336 SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, chain.simpleq);
337 splx(s);
338 if (msg == NULL)
339 break;
340
341 if (msg->src != DT_ADDR_MOUSE && msg->src != DT_ADDR_KBD) {
342 printf("%s: message from unknown dev 0x%x\n",
343 device_xname(sc->sc_dev), sc->sc_msg.src);
344 dt_msg_dump(msg);
345 continue;
346 }
347 if (DT_CTL_P(msg->ctl) != 0) {
348 printf("%s: received control message\n",
349 device_xname(sc->sc_dev));
350 dt_msg_dump(msg);
351 continue;
352 }
353
354 /*
355 * 1. Mouse should have no more than eight buttons, so first
356 * 8 bits of body will be zero.
357 * 2. Mouse should always send full locator report.
358 * Note: my mouse does not send 'z' data, so the size
359 * did not match the size of struct dt_locator_msg - mhitch
360 * 3. Keyboard should never report all-up (0x00) in
361 * a packet with size > 1.
362 */
363 if (DT_CTL_LEN(msg->ctl) >= 6 &&
364 msg->body[0] == 0 && msg->src != dt_ms_addr) {
365 dt_kbd_addr = dt_ms_addr;
366 dt_ms_addr = msg->src;
367 } else if (DT_CTL_LEN(msg->ctl) < 6 && msg->body[0] != 0 &&
368 msg->src != dt_kbd_addr) {
369 dt_ms_addr = dt_kbd_addr;
370 dt_kbd_addr = msg->src;
371 }
372
373 if (msg->src == dt_kbd_addr)
374 dtdv = &dt_kbd_dv;
375 else
376 dtdv = &dt_ms_dv;
377
378 if (dtdv->dtdv_handler != NULL)
379 (*dtdv->dtdv_handler)(dtdv->dtdv_arg, msg);
380 }
381 }
382
383 int
384 dt_msg_get(struct dt_msg *msg, int intr)
385 {
386 volatile u_int *poll, *data;
387 uint8_t c;
388 int max_polls;
389
390 poll = dt_state.ds_poll;
391 data = dt_state.ds_data;
392
393 /*
394 * The interface does not hand us the first byte, which is our
395 * address and cannot ever be anything else but 0x50.
396 */
397 if (dt_state.ds_state == 0) {
398 dt_state.ds_escaped = 0;
399 dt_state.ds_ptr = 0;
400 }
401
402 for (;;) {
403 max_polls = DT_MAX_POLL;
404
405 while (!DT_RX_AVAIL(poll)) {
406 if (intr)
407 return (DT_GET_NOTYET);
408 if (max_polls-- <= 0)
409 break;
410 DELAY(1);
411 }
412
413 if (max_polls <= 0) {
414 if (dt_state.ds_state != 0) {
415 dt_state.ds_bad_pkts++;
416 dt_state.ds_state = 0;
417 }
418 return (DT_GET_ERROR);
419 }
420
421 c = DT_GET_BYTE(data);
422
423 if (dt_state.ds_escaped) {
424 switch (c) {
425 case 0xe8:
426 case 0xe9:
427 case 0xea:
428 case 0xeb:
429 c += 0x10;
430 break;
431 }
432 if (c == 'O') {
433 dt_state.ds_bad_pkts++;
434 dt_state.ds_state = 0;
435 return (DT_GET_ERROR);
436 }
437 dt_state.ds_escaped = 0;
438 } else if (c == DT_ESC_CHAR) {
439 dt_state.ds_escaped = 1;
440 continue;
441 }
442
443 if (dt_state.ds_state == 0) {
444 msg->src = c;
445 dt_state.ds_state = 1;
446 } else if (dt_state.ds_state == 1) {
447 msg->ctl = c;
448 dt_state.ds_state = 2;
449 dt_state.ds_len = DT_CTL_LEN(msg->ctl) + 1;
450 if (dt_state.ds_len > sizeof(msg->body))
451 printf("dt_msg_get: msg truncated: %d\n",
452 dt_state.ds_len);
453 } else /* if (dt_state.ds_state == 2) */ {
454 if (dt_state.ds_ptr < sizeof(msg->body))
455 msg->body[dt_state.ds_ptr++] = c;
456 if (dt_state.ds_ptr >= dt_state.ds_len)
457 break;
458 }
459 }
460
461 msg->dst = DT_ADDR_HOST;
462 dt_state.ds_state = 0;
463 return (DT_GET_DONE);
464 }
465
466 void
467 dt_msg_dump(struct dt_msg *msg)
468 {
469 int i, l;
470
471 l = DT_CTL_LEN(msg->ctl);
472
473 printf("hdr: dst=%02x src=%02x p=%02x sub=%02x len=%02x\n",
474 msg->dst, msg->src, DT_CTL_P(msg->ctl), DT_CTL_SUBADDR(msg->ctl),
475 l);
476
477 printf("body: ");
478 for (i = 0; i < l && i < 20; i++)
479 printf("%02x ", msg->body[i]);
480 if (i < l) {
481 printf("\n");
482 for (; i < l; i++)
483 printf("%02x ", msg->body[i]);
484 }
485 printf("\n");
486 }
487