xref: /netbsd-src/sys/arch/i386/isa/lms.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: lms.c,v 1.48 2006/02/19 14:59:22 thorpej 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.48 2006/02/19 14:59:22 thorpej 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(struct device *, struct cfdata *, void *);
63 void lmsattach(struct device *, struct device *, void *);
64 int lmsintr(void *);
65 
66 CFATTACH_DECL(lms, sizeof(struct lms_softc),
67     lmsprobe, lmsattach, NULL, NULL);
68 
69 int	lms_enable(void *);
70 int	lms_ioctl(void *, u_long, caddr_t, int, struct lwp *);
71 void	lms_disable(void *);
72 
73 const struct wsmouse_accessops lms_accessops = {
74 	lms_enable,
75 	lms_ioctl,
76 	lms_disable,
77 };
78 
79 int
80 lmsprobe(struct device *parent, struct cfdata *match, void *aux)
81 {
82 	struct isa_attach_args *ia = aux;
83 	bus_space_tag_t iot = ia->ia_iot;
84 	bus_space_handle_t ioh;
85 	int rv;
86 
87 	if (ia->ia_nio < 1)
88 		return (0);
89 	if (ia->ia_nirq < 1)
90 		return (0);
91 
92 	if (ISA_DIRECT_CONFIG(ia))
93 		return (0);
94 
95 	/* Disallow wildcarded i/o base. */
96 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
97 		return 0;
98 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
99 		return 0;
100 
101 	/* Map the i/o space. */
102 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, LMS_NPORTS, 0, &ioh))
103 		return 0;
104 
105 	rv = 0;
106 
107 	/* Configure and check for port present. */
108 	bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
109 	delay(10);
110 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
111 	delay(10);
112 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
113 		goto out;
114 	bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
115 	delay(10);
116 	if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
117 		goto out;
118 
119 	/* Disable interrupts. */
120 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
121 
122 	rv = 1;
123 	ia->ia_nio = 1;
124 	ia->ia_io[0].ir_size = LMS_NPORTS;
125 
126 	ia->ia_nirq = 1;
127 
128 	ia->ia_niomem = 0;
129 	ia->ia_ndrq = 0;
130 
131 out:
132 	bus_space_unmap(iot, ioh, LMS_NPORTS);
133 	return rv;
134 }
135 
136 void
137 lmsattach(struct device *parent, struct device *self, void *aux)
138 {
139 	struct lms_softc *sc = (void *)self;
140 	struct isa_attach_args *ia = aux;
141 	bus_space_tag_t iot = ia->ia_iot;
142 	bus_space_handle_t ioh;
143 	struct wsmousedev_attach_args a;
144 
145 	aprint_naive(": Mouse\n");
146 	aprint_normal(": Logitech Mouse\n");
147 
148 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, LMS_NPORTS, 0, &ioh)) {
149 		aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
150 		return;
151 	}
152 
153 	/* Other initialization was done by lmsprobe. */
154 	sc->sc_iot = iot;
155 	sc->sc_ioh = ioh;
156 	sc->sc_enabled = 0;
157 
158 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
159 	    IST_PULSE, IPL_TTY, lmsintr, sc);
160 
161 	a.accessops = &lms_accessops;
162 	a.accesscookie = sc;
163 
164 	/*
165 	 * Attach the wsmouse, saving a handle to it.
166 	 * Note that we don't need to check this pointer against NULL
167 	 * here or in psmintr, because if this fails lms_enable() will
168 	 * never be called, so lmsintr() will never be called.
169 	 */
170 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
171 }
172 
173 int
174 lms_enable(void *v)
175 {
176 	struct lms_softc *sc = v;
177 
178 	if (sc->sc_enabled)
179 		return EBUSY;
180 
181 	sc->sc_enabled = 1;
182 	sc->oldbuttons = 0;
183 
184 	/* Enable interrupts. */
185 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
186 
187 	return 0;
188 }
189 
190 void
191 lms_disable(void *v)
192 {
193 	struct lms_softc *sc = v;
194 
195 	/* Disable interrupts. */
196 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
197 
198 	sc->sc_enabled = 0;
199 }
200 
201 int
202 lms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
203 {
204 #if 0
205 	struct lms_softc *sc = v;
206 #endif
207 
208 	switch (cmd) {
209 	case WSMOUSEIO_GTYPE:
210 		*(u_int *)data = WSMOUSE_TYPE_LMS;
211 		return (0);
212 	}
213 	return (EPASSTHROUGH);
214 }
215 
216 int
217 lmsintr(void *arg)
218 {
219 	struct lms_softc *sc = arg;
220 	bus_space_tag_t iot = sc->sc_iot;
221 	bus_space_handle_t ioh = sc->sc_ioh;
222 	u_char hi, lo;
223 	signed char dx, dy;
224 	u_int buttons;
225 	int changed;
226 
227 	if (!sc->sc_enabled)
228 		/* Interrupts are not expected. */
229 		return 0;
230 
231 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
232 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
233 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
234 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
235 	dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
236 	/* Bounding at -127 avoids a bug in XFree86. */
237 	dx = (dx == -128) ? -127 : dx;
238 
239 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
240 	hi = bus_space_read_1(iot, ioh, LMS_DATA);
241 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
242 	lo = bus_space_read_1(iot, ioh, LMS_DATA);
243 	dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
244 	dy = (dy == -128) ? 127 : -dy;
245 
246 	bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
247 
248 	buttons = ((hi & 0x80) ? 0 : 0x1) |
249 		((hi & 0x40) ? 0 : 0x2) |
250 		((hi & 0x20) ? 0 : 0x4);
251 	changed = (buttons ^ sc->oldbuttons);
252 	sc->oldbuttons = buttons;
253 
254 	if (dx || dy || changed)
255 		wsmouse_input(sc->sc_wsmousedev,
256 			      buttons, dx, dy, 0, WSMOUSE_INPUT_DELTA);
257 
258 	return -1;
259 }
260