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