1 /* $NetBSD: dt.c,v 1.9 2007/10/17 19:56:16 garbled Exp $ */
4 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (c) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * Ralph Campbell and Rick Macklem.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
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.
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
63 * @(#)dtop.c 8.2 (Berkeley) 11/30/93
67 * Mach Operating System
68 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
69 * All Rights Reserved.
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.
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.
81 * Carnegie Mellon requests users of this software to return to
83 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
84 * School of Computer Science
85 * Carnegie Mellon University
86 * Pittsburgh PA 15213-3890
88 * any improvements or extensions that they make and grant Carnegie the
89 * rights to redistribute these changes.
92 * Author: Alessandro Forin, Carnegie Mellon University
94 * Hardware-level operations for the Desktop serial line
95 * bus (i2c aka ACCESS).
97 /************************************************************
98 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
99 and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
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.
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
119 ********************************************************/
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.
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.
135 #include <sys/cdefs.h>
136 __KERNEL_RCSID(0, "$NetBSD: dt.c,v 1.9 2007/10/17 19:56:16 garbled Exp $");
138 #include <sys/param.h>
139 #include <sys/systm.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>
149 #include <dev/dec/lk201.h>
151 #include <dev/tc/tcvar.h>
152 #include <dev/tc/ioasicreg.h>
153 #include <dev/tc/ioasicvar.h>
155 #include <pmax/pmax/maxine.h>
157 #include <pmax/tc/dtreg.h>
158 #include <pmax/tc/dtvar.h>
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 */
165 #define DT_GET_BYTE(data) (((*(data)) >> 8) & 0xff)
166 #define DT_PUT_BYTE(data,c) { *(data) = (c) << 8; wbflush(); }
168 #define DT_RX_AVAIL(poll) ((*(poll) & 1) != 0)
169 #define DT_TX_AVAIL(poll) ((*(poll) & 2) != 0)
171 int dt_match(struct device
*, struct cfdata
*, void *);
172 void dt_attach(struct device
*, struct device
*, void *);
174 int dt_null_handler(struct device
*, struct dt_msg
*, int);
175 int dt_print(void *, const char *);
176 void dt_strvis(uint8_t *, char *, int);
177 void dt_dispatch(void *);
179 int dt_kbd_addr
= DT_ADDR_KBD
;
180 struct dt_device dt_kbd_dv
;
181 int dt_ms_addr
= DT_ADDR_MOUSE
;
182 struct dt_device dt_ms_dv
;
183 struct dt_state dt_state
;
185 CFATTACH_DECL(dt
, sizeof(struct dt_softc
),
186 dt_match
, dt_attach
, NULL
, NULL
);
189 dt_match(struct device
*parent
, struct cfdata
*match
, void *aux
)
191 struct ioasicdev_attach_args
*d
;
195 if (strcmp(d
->iada_modname
, "dtop") != 0)
198 if (badaddr((void *)(d
->iada_addr
), 2))
205 dt_attach(struct device
*parent
, struct device
*self
, void *aux
)
207 struct ioasicdev_attach_args
*d
;
208 struct dt_attach_args dta
;
214 sc
= (struct dt_softc
*)self
;
218 msg
= malloc(sizeof(*msg
) * DT_BUF_CNT
, M_DEVBUF
, M_NOWAIT
);
220 printf("%s: memory exhausted\n", sc
->sc_dv
.dv_xname
);
224 sc
->sc_sih
= softint_establish(SOFTINT_SERIAL
, dt_dispatch
, sc
);
225 if (sc
->sc_sih
== NULL
) {
226 printf("%s: memory exhausted\n", sc
->sc_dv
.dv_xname
);
230 SIMPLEQ_INIT(&sc
->sc_queue
);
231 SLIST_INIT(&sc
->sc_free
);
232 for (i
= 0; i
< DT_BUF_CNT
; i
++, msg
++)
233 SLIST_INSERT_HEAD(&sc
->sc_free
, msg
, chain
.slist
);
235 ioasic_intr_establish(parent
, d
->iada_cookie
, TC_IPL_TTY
, dt_intr
, sc
);
238 dta
.dta_addr
= DT_ADDR_KBD
;
239 config_found(self
, &dta
, dt_print
);
240 dta
.dta_addr
= DT_ADDR_MOUSE
;
241 config_found(self
, &dta
, dt_print
);
248 dt_state
.ds_poll
= (volatile u_int
*)
249 MIPS_PHYS_TO_KSEG1(XINE_REG_INTR
);
250 dt_state
.ds_data
= (volatile u_int
*)
251 MIPS_PHYS_TO_KSEG1(XINE_PHYS_TC_3_START
+ 0x280000);
255 dt_print(void *aux
, const char *pnp
)
262 dt_establish_handler(struct dt_softc
*sc
, struct dt_device
*dtdv
,
263 struct device
*dv
, void (*hdlr
)(void *, struct dt_msg
*))
267 dtdv
->dtdv_handler
= hdlr
;
272 dt_intr(void *cookie
)
275 struct dt_msg
*msg
, *pend
;
279 switch (dt_msg_get(&sc
->sc_msg
, 1)) {
282 * Ugh! The most common occurrence of a data overrun is upon
283 * a key press and the result is a software generated "stuck
284 * key". All I can think to do is fake an "all keys up"
285 * whenever a data overrun occurs.
287 sc
->sc_msg
.src
= dt_kbd_addr
;
288 sc
->sc_msg
.ctl
= DT_CTL(1, 0, 0);
289 sc
->sc_msg
.body
[0] = DT_KBD_EMPTY
;
291 printf("%s: data overrun or stray interrupt\n",
303 if ((msg
= SLIST_FIRST(&sc
->sc_free
)) == NULL
) {
304 printf("%s: input overflow\n", sc
->sc_dv
.dv_xname
);
307 SLIST_REMOVE_HEAD(&sc
->sc_free
, chain
.slist
);
308 memcpy(msg
, &sc
->sc_msg
, sizeof(*msg
));
310 pend
= SIMPLEQ_FIRST(&sc
->sc_queue
);
311 SIMPLEQ_INSERT_TAIL(&sc
->sc_queue
, msg
, chain
.simpleq
);
313 softint_schedule(sc
->sc_sih
);
319 dt_dispatch(void *cookie
)
324 struct dt_device
*dtdv
;
332 SLIST_INSERT_HEAD(&sc
->sc_free
, msg
, chain
.slist
);
333 msg
= SIMPLEQ_FIRST(&sc
->sc_queue
);
335 SIMPLEQ_REMOVE_HEAD(&sc
->sc_queue
, chain
.simpleq
);
340 if (msg
->src
!= DT_ADDR_MOUSE
&& msg
->src
!= DT_ADDR_KBD
) {
341 printf("%s: message from unknown dev 0x%x\n",
342 sc
->sc_dv
.dv_xname
, sc
->sc_msg
.src
);
346 if (DT_CTL_P(msg
->ctl
) != 0) {
347 printf("%s: received control message\n",
354 * 1. Mouse should have no more than eight buttons, so first
355 * 8 bits of body will be zero.
356 * 2. Mouse should always send full locator report.
357 * Note: my mouse does not send 'z' data, so the size
358 * did not match the size of struct dt_locator_msg - mhitch
359 * 3. Keyboard should never report all-up (0x00) in
360 * a packet with size > 1.
362 if (DT_CTL_LEN(msg
->ctl
) >= 6 &&
363 msg
->body
[0] == 0 && msg
->src
!= dt_ms_addr
) {
364 dt_kbd_addr
= dt_ms_addr
;
365 dt_ms_addr
= msg
->src
;
366 } else if (DT_CTL_LEN(msg
->ctl
) < 6 && msg
->body
[0] != 0 &&
367 msg
->src
!= dt_kbd_addr
) {
368 dt_ms_addr
= dt_kbd_addr
;
369 dt_kbd_addr
= msg
->src
;
372 if (msg
->src
== dt_kbd_addr
)
377 if (dtdv
->dtdv_handler
!= NULL
)
378 (*dtdv
->dtdv_handler
)(dtdv
->dtdv_dv
, msg
);
383 dt_msg_get(struct dt_msg
*msg
, int intr
)
385 volatile u_int
*poll
, *data
;
389 poll
= dt_state
.ds_poll
;
390 data
= dt_state
.ds_data
;
393 * The interface does not hand us the first byte, which is our
394 * address and cannot ever be anything else but 0x50.
396 if (dt_state
.ds_state
== 0) {
397 dt_state
.ds_escaped
= 0;
402 max_polls
= DT_MAX_POLL
;
404 while (!DT_RX_AVAIL(poll
)) {
406 return (DT_GET_NOTYET
);
407 if (max_polls
-- <= 0)
412 if (max_polls
<= 0) {
413 if (dt_state
.ds_state
!= 0) {
414 dt_state
.ds_bad_pkts
++;
415 dt_state
.ds_state
= 0;
417 return (DT_GET_ERROR
);
420 c
= DT_GET_BYTE(data
);
422 if (dt_state
.ds_escaped
) {
432 dt_state
.ds_bad_pkts
++;
433 dt_state
.ds_state
= 0;
434 return (DT_GET_ERROR
);
436 dt_state
.ds_escaped
= 0;
437 } else if (c
== DT_ESC_CHAR
) {
438 dt_state
.ds_escaped
= 1;
442 if (dt_state
.ds_state
== 0) {
444 dt_state
.ds_state
= 1;
445 } else if (dt_state
.ds_state
== 1) {
447 dt_state
.ds_state
= 2;
448 dt_state
.ds_len
= DT_CTL_LEN(msg
->ctl
) + 1;
449 if (dt_state
.ds_len
> sizeof(msg
->body
))
450 printf("dt_msg_get: msg truncated: %d\n",
452 } else /* if (dt_state.ds_state == 2) */ {
453 if (dt_state
.ds_ptr
< sizeof(msg
->body
))
454 msg
->body
[dt_state
.ds_ptr
++] = c
;
455 if (dt_state
.ds_ptr
>= dt_state
.ds_len
)
460 msg
->dst
= DT_ADDR_HOST
;
461 dt_state
.ds_state
= 0;
462 return (DT_GET_DONE
);
466 dt_msg_dump(struct dt_msg
*msg
)
470 l
= DT_CTL_LEN(msg
->ctl
);
472 printf("hdr: dst=%02x src=%02x p=%02x sub=%02x len=%02x\n",
473 msg
->dst
, msg
->src
, DT_CTL_P(msg
->ctl
), DT_CTL_SUBADDR(msg
->ctl
),
477 for (i
= 0; i
< l
&& i
< 20; i
++)
478 printf("%02x ", msg
->body
[i
]);
482 printf("%02x ", msg
->body
[i
]);