xref: /netbsd-src/sys/arch/i386/isa/lms.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: lms.c,v 1.39 2001/11/15 07:03:32 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994 Charles M. Hannum.
5  * Copyright (c) 1992, 1993 Erik Forsberg.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
17  * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: lms.c,v 1.39 2001/11/15 07:03:32 lukem Exp $");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/ioctl.h>
32 #include <sys/device.h>
33 
34 #include <machine/bus.h>
35 #include <machine/intr.h>
36 
37 #include <dev/isa/isavar.h>
38 
39 #include <dev/wscons/wsconsio.h>
40 #include <dev/wscons/wsmousevar.h>
41 
42 #define	LMS_DATA	0       /* offset for data port, read-only */
43 #define	LMS_SIGN	1       /* offset for signature port, read-write */
44 #define	LMS_INTR	2       /* offset for interrupt port, read-only */
45 #define	LMS_CNTRL	2       /* offset for control port, write-only */
46 #define	LMS_CONFIG	3	/* for configuration port, read-write */
47 #define	LMS_NPORTS	4
48 
49 struct lms_softc {		/* driver status information */
50 	struct device sc_dev;
51 	void *sc_ih;
52 
53 	bus_space_tag_t sc_iot;		/* bus i/o space identifier */
54 	bus_space_handle_t sc_ioh;	/* bus i/o handle */
55 
56 	int sc_enabled; /* device is open */
57 	int oldbuttons;	/* mouse button status */
58 
59 	struct device *sc_wsmousedev;
60 };
61 
62 int lmsprobe __P((struct device *, struct cfdata *, void *));
63 void lmsattach __P((struct device *, struct device *, void *));
64 int lmsintr __P((void *));
65 
66 struct cfattach lms_ca = {
67 	sizeof(struct lms_softc), lmsprobe, lmsattach
68 };
69 
70 int	lms_enable __P((void *));
71 int	lms_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
72 void	lms_disable __P((void *));
73 
74 const struct wsmouse_accessops lms_accessops = {
75 	lms_enable,
76 	lms_ioctl,
77 	lms_disable,
78 };
79 
80 int
81 lmsprobe(parent, match, aux)
82 	struct device *parent;
83 	struct cfdata *match;
84 	void *aux;
85 {
86 	struct isa_attach_args *ia = aux;
87 	bus_space_tag_t iot = ia->ia_iot;
88 	bus_space_handle_t ioh;
89 	int rv;
90 
91 	/* Disallow wildcarded i/o base. */
92 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
93 		return 0;
94 
95 	/* Map the i/o space. */
96 	if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh))
97 		return 0;
98 
99 	rv = 0;
100 
101 	/* Configure and check for port present. */
102 	bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
103 	delay(10);
104 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
105 	delay(10);
106 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
107 		goto out;
108 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
109 	delay(10);
110 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
111 		goto out;
112 
113 	/* Disable interrupts. */
114 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
115 
116 	rv = 1;
117 	ia->ia_iosize = LMS_NPORTS;
118 	ia->ia_msize = 0;
119 
120 out:
121 	bus_space_unmap(iot, ioh, LMS_NPORTS);
122 	return rv;
123 }
124 
125 void
126 lmsattach(parent, self, aux)
127 	struct device *parent, *self;
128 	void *aux;
129 {
130 	struct lms_softc *sc = (void *)self;
131 	struct isa_attach_args *ia = aux;
132 	bus_space_tag_t iot = ia->ia_iot;
133 	bus_space_handle_t ioh;
134 	struct wsmousedev_attach_args a;
135 
136 	printf("\n");
137 
138 	if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh)) {
139 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
140 		return;
141 	}
142 
143 	/* Other initialization was done by lmsprobe. */
144 	sc->sc_iot = iot;
145 	sc->sc_ioh = ioh;
146 	sc->sc_enabled = 0;
147 
148 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
149 	    IPL_TTY, lmsintr, sc);
150 
151 	a.accessops = &lms_accessops;
152 	a.accesscookie = sc;
153 
154 	/*
155 	 * Attach the wsmouse, saving a handle to it.
156 	 * Note that we don't need to check this pointer against NULL
157 	 * here or in psmintr, because if this fails lms_enable() will
158 	 * never be called, so lmsintr() will never be called.
159 	 */
160 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
161 }
162 
163 int
164 lms_enable(v)
165 	void *v;
166 {
167 	struct lms_softc *sc = v;
168 
169 	if (sc->sc_enabled)
170 		return EBUSY;
171 
172 	sc->sc_enabled = 1;
173 	sc->oldbuttons = 0;
174 
175 	/* Enable interrupts. */
176 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
177 
178 	return 0;
179 }
180 
181 void
182 lms_disable(v)
183 	void *v;
184 {
185 	struct lms_softc *sc = v;
186 
187 	/* Disable interrupts. */
188 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
189 
190 	sc->sc_enabled = 0;
191 }
192 
193 int
194 lms_ioctl(v, cmd, data, flag, p)
195 	void *v;
196 	u_long cmd;
197 	caddr_t data;
198 	int flag;
199 	struct proc *p;
200 {
201 #if 0
202 	struct lms_softc *sc = v;
203 #endif
204 
205 	switch (cmd) {
206 	case WSMOUSEIO_GTYPE:
207 		*(u_int *)data = WSMOUSE_TYPE_LMS;
208 		return (0);
209 	}
210 	return (-1);
211 }
212 
213 int
214 lmsintr(arg)
215 	void *arg;
216 {
217 	struct lms_softc *sc = arg;
218 	bus_space_tag_t iot = sc->sc_iot;
219 	bus_space_handle_t ioh = sc->sc_ioh;
220 	u_char hi, lo;
221 	signed char dx, dy;
222 	u_int buttons;
223 	int changed;
224 
225 	if (!sc->sc_enabled)
226 		/* Interrupts are not expected. */
227 		return 0;
228 
229 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
230 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
231 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
232 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
233 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
234 	/* Bounding at -127 avoids a bug in XFree86. */
235 	dx = (dx == -128) ? -127 : dx;
236 
237 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
238 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
239 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
240 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
241 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
242 	dy = (dy == -128) ? 127 : -dy;
243 
244 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
245 
246 	buttons = ((hi & 0x80) ? 0 : 0x1) |
247 		((hi & 0x40) ? 0 : 0x2) |
248 		((hi & 0x20) ? 0 : 0x4);
249 	changed = (buttons ^ sc->oldbuttons);
250 	sc->oldbuttons = buttons;
251 
252 	if (dx || dy || changed)
253 		wsmouse_input(sc->sc_wsmousedev,
254 			      buttons, dx, dy, 0, WSMOUSE_INPUT_DELTA);
255 
256 	return -1;
257 }
258