lms.c revision 1.37.8.1 1 /* $NetBSD: lms.c,v 1.37.8.1 2000/11/20 20:09:32 bouyer 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/param.h>
27 #include <sys/systm.h>
28 #include <sys/ioctl.h>
29 #include <sys/device.h>
30
31 #include <machine/bus.h>
32 #include <machine/intr.h>
33
34 #include <dev/isa/isavar.h>
35
36 #include <dev/wscons/wsconsio.h>
37 #include <dev/wscons/wsmousevar.h>
38
39 #define LMS_DATA 0 /* offset for data port, read-only */
40 #define LMS_SIGN 1 /* offset for signature port, read-write */
41 #define LMS_INTR 2 /* offset for interrupt port, read-only */
42 #define LMS_CNTRL 2 /* offset for control port, write-only */
43 #define LMS_CONFIG 3 /* for configuration port, read-write */
44 #define LMS_NPORTS 4
45
46 struct lms_softc { /* driver status information */
47 struct device sc_dev;
48 void *sc_ih;
49
50 bus_space_tag_t sc_iot; /* bus i/o space identifier */
51 bus_space_handle_t sc_ioh; /* bus i/o handle */
52
53 int sc_enabled; /* device is open */
54 int oldbuttons; /* mouse button status */
55
56 struct device *sc_wsmousedev;
57 };
58
59 int lmsprobe __P((struct device *, struct cfdata *, void *));
60 void lmsattach __P((struct device *, struct device *, void *));
61 int lmsintr __P((void *));
62
63 struct cfattach lms_ca = {
64 sizeof(struct lms_softc), lmsprobe, lmsattach
65 };
66
67 int lms_enable __P((void *));
68 int lms_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
69 void lms_disable __P((void *));
70
71 const struct wsmouse_accessops lms_accessops = {
72 lms_enable,
73 lms_ioctl,
74 lms_disable,
75 };
76
77 int
78 lmsprobe(parent, match, aux)
79 struct device *parent;
80 struct cfdata *match;
81 void *aux;
82 {
83 struct isa_attach_args *ia = aux;
84 bus_space_tag_t iot = ia->ia_iot;
85 bus_space_handle_t ioh;
86 int rv;
87
88 /* Disallow wildcarded i/o base. */
89 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
90 return 0;
91
92 /* Map the i/o space. */
93 if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh))
94 return 0;
95
96 rv = 0;
97
98 /* Configure and check for port present. */
99 bus_space_write_1(iot, ioh, LMS_CONFIG, 0x91);
100 delay(10);
101 bus_space_write_1(iot, ioh, LMS_SIGN, 0x0c);
102 delay(10);
103 if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x0c)
104 goto out;
105 bus_space_write_1(iot, ioh, LMS_SIGN, 0x50);
106 delay(10);
107 if (bus_space_read_1(iot, ioh, LMS_SIGN) != 0x50)
108 goto out;
109
110 /* Disable interrupts. */
111 bus_space_write_1(iot, ioh, LMS_CNTRL, 0x10);
112
113 rv = 1;
114 ia->ia_iosize = LMS_NPORTS;
115 ia->ia_msize = 0;
116
117 out:
118 bus_space_unmap(iot, ioh, LMS_NPORTS);
119 return rv;
120 }
121
122 void
123 lmsattach(parent, self, aux)
124 struct device *parent, *self;
125 void *aux;
126 {
127 struct lms_softc *sc = (void *)self;
128 struct isa_attach_args *ia = aux;
129 bus_space_tag_t iot = ia->ia_iot;
130 bus_space_handle_t ioh;
131 struct wsmousedev_attach_args a;
132
133 printf("\n");
134
135 if (bus_space_map(iot, ia->ia_iobase, LMS_NPORTS, 0, &ioh)) {
136 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
137 return;
138 }
139
140 /* Other initialization was done by lmsprobe. */
141 sc->sc_iot = iot;
142 sc->sc_ioh = ioh;
143 sc->sc_enabled = 0;
144
145 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_PULSE,
146 IPL_TTY, lmsintr, sc);
147
148 a.accessops = &lms_accessops;
149 a.accesscookie = sc;
150
151 /*
152 * Attach the wsmouse, saving a handle to it.
153 * Note that we don't need to check this pointer against NULL
154 * here or in psmintr, because if this fails lms_enable() will
155 * never be called, so lmsintr() will never be called.
156 */
157 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
158 }
159
160 int
161 lms_enable(v)
162 void *v;
163 {
164 struct lms_softc *sc = v;
165
166 if (sc->sc_enabled)
167 return EBUSY;
168
169 sc->sc_enabled = 1;
170 sc->oldbuttons = 0;
171
172 /* Enable interrupts. */
173 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0);
174
175 return 0;
176 }
177
178 void
179 lms_disable(v)
180 void *v;
181 {
182 struct lms_softc *sc = v;
183
184 /* Disable interrupts. */
185 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMS_CNTRL, 0x10);
186
187 sc->sc_enabled = 0;
188 }
189
190 int
191 lms_ioctl(v, cmd, data, flag, p)
192 void *v;
193 u_long cmd;
194 caddr_t data;
195 int flag;
196 struct proc *p;
197 {
198 #if 0
199 struct lms_softc *sc = v;
200 #endif
201
202 switch (cmd) {
203 case WSMOUSEIO_GTYPE:
204 *(u_int *)data = WSMOUSE_TYPE_LMS;
205 return (0);
206 }
207 return (-1);
208 }
209
210 int
211 lmsintr(arg)
212 void *arg;
213 {
214 struct lms_softc *sc = arg;
215 bus_space_tag_t iot = sc->sc_iot;
216 bus_space_handle_t ioh = sc->sc_ioh;
217 u_char hi, lo;
218 signed char dx, dy;
219 u_int buttons;
220 int changed;
221
222 if (!sc->sc_enabled)
223 /* Interrupts are not expected. */
224 return 0;
225
226 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xab);
227 hi = bus_space_read_1(iot, ioh, LMS_DATA);
228 bus_space_write_1(iot, ioh, LMS_CNTRL, 0x90);
229 lo = bus_space_read_1(iot, ioh, LMS_DATA);
230 dx = ((hi & 0x0f) << 4) | (lo & 0x0f);
231 /* Bounding at -127 avoids a bug in XFree86. */
232 dx = (dx == -128) ? -127 : dx;
233
234 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xf0);
235 hi = bus_space_read_1(iot, ioh, LMS_DATA);
236 bus_space_write_1(iot, ioh, LMS_CNTRL, 0xd0);
237 lo = bus_space_read_1(iot, ioh, LMS_DATA);
238 dy = ((hi & 0x0f) << 4) | (lo & 0x0f);
239 dy = (dy == -128) ? 127 : -dy;
240
241 bus_space_write_1(iot, ioh, LMS_CNTRL, 0);
242
243 buttons = ((hi & 0x80) ? 0 : 0x1) |
244 ((hi & 0x40) ? 0 : 0x2) |
245 ((hi & 0x20) ? 0 : 0x4);
246 changed = (buttons ^ sc->oldbuttons);
247 sc->oldbuttons = buttons;
248
249 if (dx || dy || changed)
250 wsmouse_input(sc->sc_wsmousedev,
251 buttons, dx, dy, 0, WSMOUSE_INPUT_DELTA);
252
253 return -1;
254 }
255