pms.c revision 1.2.4.10 1 1.2.4.10 skrll /* $NetBSD: pms.c,v 1.2.4.10 2005/11/10 14:07:23 skrll Exp $ */
2 1.2.4.2 skrll
3 1.2.4.2 skrll /*-
4 1.2.4.6 skrll * Copyright (c) 2004 Kentaro Kurahone.
5 1.2.4.6 skrll * Copyright (c) 2004 Ales Krenek.
6 1.2.4.2 skrll * Copyright (c) 1994 Charles M. Hannum.
7 1.2.4.2 skrll * Copyright (c) 1992, 1993 Erik Forsberg.
8 1.2.4.2 skrll * All rights reserved.
9 1.2.4.2 skrll *
10 1.2.4.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 skrll * modification, are permitted provided that the following conditions
12 1.2.4.2 skrll * are met:
13 1.2.4.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 skrll *
16 1.2.4.2 skrll * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 1.2.4.2 skrll * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 1.2.4.2 skrll * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 1.2.4.2 skrll * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 1.2.4.2 skrll * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 1.2.4.2 skrll * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 1.2.4.2 skrll * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 1.2.4.2 skrll * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 1.2.4.2 skrll * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 1.2.4.2 skrll * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.2.4.2 skrll */
27 1.2.4.2 skrll
28 1.2.4.7 skrll #include "opt_pms.h"
29 1.2.4.7 skrll
30 1.2.4.2 skrll #include <sys/cdefs.h>
31 1.2.4.10 skrll __KERNEL_RCSID(0, "$NetBSD: pms.c,v 1.2.4.10 2005/11/10 14:07:23 skrll Exp $");
32 1.2.4.2 skrll
33 1.2.4.2 skrll #include <sys/param.h>
34 1.2.4.2 skrll #include <sys/systm.h>
35 1.2.4.2 skrll #include <sys/device.h>
36 1.2.4.2 skrll #include <sys/ioctl.h>
37 1.2.4.2 skrll #include <sys/kernel.h>
38 1.2.4.2 skrll #include <sys/kthread.h>
39 1.2.4.2 skrll
40 1.2.4.2 skrll #include <machine/bus.h>
41 1.2.4.2 skrll
42 1.2.4.2 skrll #include <dev/pckbport/pckbportvar.h>
43 1.2.4.7 skrll #ifdef PMS_SYNAPTICS_TOUCHPAD
44 1.2.4.6 skrll #include <dev/pckbport/synapticsvar.h>
45 1.2.4.7 skrll #endif
46 1.2.4.2 skrll
47 1.2.4.2 skrll #include <dev/pckbport/pmsreg.h>
48 1.2.4.6 skrll #include <dev/pckbport/pmsvar.h>
49 1.2.4.6 skrll
50 1.2.4.2 skrll
51 1.2.4.2 skrll #include <dev/wscons/wsconsio.h>
52 1.2.4.2 skrll #include <dev/wscons/wsmousevar.h>
53 1.2.4.2 skrll
54 1.2.4.2 skrll #ifdef PMSDEBUG
55 1.2.4.2 skrll int pmsdebug = 1;
56 1.2.4.2 skrll #define DPRINTF(x) if (pmsdebug) printf x
57 1.2.4.2 skrll #else
58 1.2.4.2 skrll #define DPRINTF(x)
59 1.2.4.2 skrll #endif
60 1.2.4.2 skrll
61 1.2.4.10 skrll const enum pms_type tries[] = {
62 1.2.4.6 skrll PMS_SCROLL5, PMS_SCROLL3, PMS_STANDARD, PMS_UNKNOWN
63 1.2.4.6 skrll };
64 1.2.4.2 skrll
65 1.2.4.10 skrll const struct pms_protocol pms_protocols[] = {
66 1.2.4.2 skrll { { 0, 0, 0 }, 0, "unknown protocol" },
67 1.2.4.2 skrll { { 0, 0, 0 }, 0, "no scroll wheel (3 buttons)" },
68 1.2.4.2 skrll { { 200, 100, 80 }, 3, "scroll wheel (3 buttons)" },
69 1.2.4.10 skrll { { 200, 200, 80 }, 4, "scroll wheel (5 buttons)" },
70 1.2.4.10 skrll { { 0, 0, 0 }, 0, "synaptics" }
71 1.2.4.2 skrll };
72 1.2.4.2 skrll
73 1.2.4.2 skrll
74 1.2.4.2 skrll int pmsprobe(struct device *, struct cfdata *, void *);
75 1.2.4.2 skrll void pmsattach(struct device *, struct device *, void *);
76 1.2.4.2 skrll void pmsinput(void *, int);
77 1.2.4.2 skrll
78 1.2.4.2 skrll CFATTACH_DECL(pms, sizeof(struct pms_softc),
79 1.2.4.2 skrll pmsprobe, pmsattach, NULL, NULL);
80 1.2.4.2 skrll
81 1.2.4.2 skrll static int pms_protocol(pckbport_tag_t, pckbport_slot_t);
82 1.2.4.2 skrll static void do_enable(struct pms_softc *);
83 1.2.4.2 skrll static void do_disable(struct pms_softc *);
84 1.2.4.2 skrll static void pms_reset_thread(void*);
85 1.2.4.2 skrll static void pms_spawn_reset_thread(void*);
86 1.2.4.2 skrll int pms_enable(void *);
87 1.2.4.5 skrll int pms_ioctl(void *, u_long, caddr_t, int, struct lwp *);
88 1.2.4.2 skrll void pms_disable(void *);
89 1.2.4.2 skrll #ifndef PMS_DISABLE_POWERHOOK
90 1.2.4.2 skrll void pms_power(int, void *);
91 1.2.4.2 skrll #endif /* !PMS_DISABLE_POWERHOOK */
92 1.2.4.2 skrll
93 1.2.4.2 skrll const struct wsmouse_accessops pms_accessops = {
94 1.2.4.2 skrll pms_enable,
95 1.2.4.2 skrll pms_ioctl,
96 1.2.4.2 skrll pms_disable,
97 1.2.4.2 skrll };
98 1.2.4.2 skrll
99 1.2.4.2 skrll static int
100 1.2.4.2 skrll pms_protocol(pckbport_tag_t tag, pckbport_slot_t slot)
101 1.2.4.2 skrll {
102 1.2.4.2 skrll u_char cmd[2], resp[1];
103 1.2.4.2 skrll int i, j, res;
104 1.2.4.10 skrll const struct pms_protocol *p;
105 1.2.4.2 skrll
106 1.2.4.2 skrll for (j = 0; j < sizeof(tries) / sizeof(tries[0]); ++j) {
107 1.2.4.2 skrll p = &pms_protocols[tries[j]];
108 1.2.4.2 skrll if (!p->rates[0])
109 1.2.4.2 skrll break;
110 1.2.4.2 skrll cmd[0] = PMS_SET_SAMPLE;
111 1.2.4.2 skrll for (i = 0; i < 3; i++) {
112 1.2.4.2 skrll cmd[1] = p->rates[i];
113 1.2.4.2 skrll res = pckbport_enqueue_cmd(tag, slot, cmd, 2, 0, 1, 0);
114 1.2.4.2 skrll if (res)
115 1.2.4.2 skrll return PMS_STANDARD;
116 1.2.4.2 skrll }
117 1.2.4.2 skrll
118 1.2.4.2 skrll cmd[0] = PMS_SEND_DEV_ID;
119 1.2.4.2 skrll res = pckbport_enqueue_cmd(tag, slot, cmd, 1, 1, 1, resp);
120 1.2.4.2 skrll if (res)
121 1.2.4.2 skrll return PMS_UNKNOWN;
122 1.2.4.2 skrll if (resp[0] == p->response) {
123 1.2.4.2 skrll DPRINTF(("pms_protocol: found mouse protocol %d\n",
124 1.2.4.2 skrll tries[j]));
125 1.2.4.2 skrll return tries[j];
126 1.2.4.2 skrll }
127 1.2.4.2 skrll }
128 1.2.4.2 skrll DPRINTF(("pms_protocol: standard PS/2 protocol (no scroll wheel)\n"));
129 1.2.4.2 skrll return PMS_STANDARD;
130 1.2.4.2 skrll }
131 1.2.4.2 skrll
132 1.2.4.2 skrll int
133 1.2.4.2 skrll pmsprobe(struct device *parent, struct cfdata *match, void *aux)
134 1.2.4.2 skrll {
135 1.2.4.2 skrll struct pckbport_attach_args *pa = aux;
136 1.2.4.2 skrll u_char cmd[1], resp[2];
137 1.2.4.2 skrll int res;
138 1.2.4.2 skrll
139 1.2.4.2 skrll if (pa->pa_slot != PCKBPORT_AUX_SLOT)
140 1.2.4.2 skrll return 0;
141 1.2.4.2 skrll
142 1.2.4.2 skrll /* Flush any garbage. */
143 1.2.4.2 skrll pckbport_flush(pa->pa_tag, pa->pa_slot);
144 1.2.4.2 skrll
145 1.2.4.2 skrll /* reset the device */
146 1.2.4.2 skrll cmd[0] = PMS_RESET;
147 1.2.4.2 skrll res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 1);
148 1.2.4.2 skrll if (res) {
149 1.2.4.2 skrll #ifdef DEBUG
150 1.2.4.2 skrll printf("pmsprobe: reset error %d\n", res);
151 1.2.4.2 skrll #endif
152 1.2.4.2 skrll return 0;
153 1.2.4.2 skrll }
154 1.2.4.2 skrll if (resp[0] != PMS_RSTDONE) {
155 1.2.4.2 skrll printf("pmsprobe: reset response 0x%x\n", resp[0]);
156 1.2.4.2 skrll return 0;
157 1.2.4.2 skrll }
158 1.2.4.2 skrll
159 1.2.4.2 skrll /* get type number (0 = mouse) */
160 1.2.4.2 skrll if (resp[1] != 0) {
161 1.2.4.2 skrll #ifdef DEBUG
162 1.2.4.2 skrll printf("pmsprobe: type 0x%x\n", resp[1]);
163 1.2.4.2 skrll #endif
164 1.2.4.2 skrll return 0;
165 1.2.4.2 skrll }
166 1.2.4.2 skrll
167 1.2.4.2 skrll return 10;
168 1.2.4.2 skrll }
169 1.2.4.2 skrll
170 1.2.4.2 skrll void
171 1.2.4.2 skrll pmsattach(struct device *parent, struct device *self, void *aux)
172 1.2.4.2 skrll {
173 1.2.4.2 skrll struct pms_softc *sc = (void *)self;
174 1.2.4.2 skrll struct pckbport_attach_args *pa = aux;
175 1.2.4.2 skrll struct wsmousedev_attach_args a;
176 1.2.4.6 skrll u_char cmd[2], resp[2];
177 1.2.4.2 skrll int res;
178 1.2.4.2 skrll
179 1.2.4.2 skrll sc->sc_kbctag = pa->pa_tag;
180 1.2.4.2 skrll sc->sc_kbcslot = pa->pa_slot;
181 1.2.4.2 skrll
182 1.2.4.2 skrll printf("\n");
183 1.2.4.2 skrll
184 1.2.4.2 skrll /* Flush any garbage. */
185 1.2.4.2 skrll pckbport_flush(pa->pa_tag, pa->pa_slot);
186 1.2.4.2 skrll
187 1.2.4.2 skrll /* reset the device */
188 1.2.4.2 skrll cmd[0] = PMS_RESET;
189 1.2.4.2 skrll res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 1);
190 1.2.4.2 skrll #ifdef DEBUG
191 1.2.4.2 skrll if (res || resp[0] != PMS_RSTDONE || resp[1] != 0) {
192 1.2.4.2 skrll printf("pmsattach: reset error\n");
193 1.2.4.2 skrll return;
194 1.2.4.2 skrll }
195 1.2.4.2 skrll #endif
196 1.2.4.2 skrll sc->inputstate = 0;
197 1.2.4.2 skrll sc->buttons = 0;
198 1.2.4.2 skrll sc->protocol = PMS_UNKNOWN;
199 1.2.4.2 skrll
200 1.2.4.7 skrll #ifdef PMS_SYNAPTICS_TOUCHPAD
201 1.2.4.6 skrll /* Probe for synaptics touchpad. */
202 1.2.4.6 skrll if (pms_synaptics_probe_init(sc) == 0) {
203 1.2.4.6 skrll sc->protocol = PMS_SYNAPTICS;
204 1.2.4.7 skrll } else
205 1.2.4.7 skrll #endif
206 1.2.4.6 skrll /* Install generic handler. */
207 1.2.4.6 skrll pckbport_set_inputhandler(sc->sc_kbctag, sc->sc_kbcslot,
208 1.2.4.6 skrll pmsinput, sc, sc->sc_dev.dv_xname);
209 1.2.4.7 skrll
210 1.2.4.2 skrll a.accessops = &pms_accessops;
211 1.2.4.2 skrll a.accesscookie = sc;
212 1.2.4.2 skrll
213 1.2.4.2 skrll /*
214 1.2.4.2 skrll * Attach the wsmouse, saving a handle to it.
215 1.2.4.2 skrll * Note that we don't need to check this pointer against NULL
216 1.2.4.2 skrll * here or in pmsintr, because if this fails pms_enable() will
217 1.2.4.2 skrll * never be called, so pmsinput() will never be called.
218 1.2.4.2 skrll */
219 1.2.4.2 skrll sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
220 1.2.4.2 skrll
221 1.2.4.2 skrll /* no interrupts until enabled */
222 1.2.4.2 skrll cmd[0] = PMS_DEV_DISABLE;
223 1.2.4.2 skrll res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 0, 0, 0);
224 1.2.4.2 skrll if (res)
225 1.2.4.2 skrll printf("pmsattach: disable error\n");
226 1.2.4.2 skrll pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
227 1.2.4.2 skrll
228 1.2.4.2 skrll kthread_create(pms_spawn_reset_thread, sc);
229 1.2.4.2 skrll
230 1.2.4.2 skrll #ifndef PMS_DISABLE_POWERHOOK
231 1.2.4.2 skrll sc->sc_powerhook = powerhook_establish(pms_power, sc);
232 1.2.4.2 skrll sc->sc_suspended = 0;
233 1.2.4.2 skrll #endif /* !PMS_DISABLE_POWERHOOK */
234 1.2.4.2 skrll }
235 1.2.4.2 skrll
236 1.2.4.2 skrll static void
237 1.2.4.2 skrll do_enable(struct pms_softc *sc)
238 1.2.4.2 skrll {
239 1.2.4.6 skrll u_char cmd[2];
240 1.2.4.2 skrll int res;
241 1.2.4.2 skrll
242 1.2.4.2 skrll sc->inputstate = 0;
243 1.2.4.2 skrll sc->buttons = 0;
244 1.2.4.2 skrll
245 1.2.4.2 skrll pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
246 1.2.4.2 skrll
247 1.2.4.7 skrll #ifdef PMS_SYNAPTICS_TOUCHPAD
248 1.2.4.6 skrll if (sc->protocol == PMS_SYNAPTICS)
249 1.2.4.6 skrll pms_synaptics_enable(sc);
250 1.2.4.7 skrll #endif
251 1.2.4.6 skrll
252 1.2.4.2 skrll cmd[0] = PMS_DEV_ENABLE;
253 1.2.4.6 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
254 1.2.4.6 skrll 1, 0, 1, 0);
255 1.2.4.2 skrll if (res)
256 1.2.4.2 skrll printf("pms_enable: command error %d\n", res);
257 1.2.4.2 skrll
258 1.2.4.2 skrll if (sc->protocol == PMS_UNKNOWN)
259 1.2.4.2 skrll sc->protocol = pms_protocol(sc->sc_kbctag, sc->sc_kbcslot);
260 1.2.4.2 skrll DPRINTF(("pms_enable: using %s protocol\n",
261 1.2.4.2 skrll pms_protocols[sc->protocol].name));
262 1.2.4.2 skrll #if 0
263 1.2.4.2 skrll {
264 1.2.4.2 skrll u_char scmd[2];
265 1.2.4.2 skrll
266 1.2.4.2 skrll scmd[0] = PMS_SET_RES;
267 1.2.4.2 skrll scmd[1] = 3; /* 8 counts/mm */
268 1.2.4.2 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
269 1.2.4.2 skrll 2, 0, 1, 0);
270 1.2.4.2 skrll if (res)
271 1.2.4.2 skrll printf("pms_enable: setup error1 (%d)\n", res);
272 1.2.4.2 skrll
273 1.2.4.2 skrll scmd[0] = PMS_SET_SCALE21;
274 1.2.4.2 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
275 1.2.4.2 skrll 1, 0, 1, 0);
276 1.2.4.2 skrll if (res)
277 1.2.4.2 skrll printf("pms_enable: setup error2 (%d)\n", res);
278 1.2.4.2 skrll
279 1.2.4.2 skrll scmd[0] = PMS_SET_SAMPLE;
280 1.2.4.2 skrll scmd[1] = 100; /* 100 samples/sec */
281 1.2.4.2 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
282 1.2.4.2 skrll 2, 0, 1, 0);
283 1.2.4.2 skrll if (res)
284 1.2.4.2 skrll printf("pms_enable: setup error3 (%d)\n", res);
285 1.2.4.2 skrll }
286 1.2.4.2 skrll #endif
287 1.2.4.2 skrll }
288 1.2.4.2 skrll
289 1.2.4.2 skrll static void
290 1.2.4.2 skrll do_disable(struct pms_softc *sc)
291 1.2.4.2 skrll {
292 1.2.4.2 skrll u_char cmd[1];
293 1.2.4.2 skrll int res;
294 1.2.4.2 skrll
295 1.2.4.2 skrll cmd[0] = PMS_DEV_DISABLE;
296 1.2.4.6 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
297 1.2.4.6 skrll 1, 0, 1, 0);
298 1.2.4.2 skrll if (res)
299 1.2.4.2 skrll printf("pms_disable: command error\n");
300 1.2.4.2 skrll
301 1.2.4.2 skrll pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
302 1.2.4.2 skrll }
303 1.2.4.2 skrll
304 1.2.4.2 skrll int
305 1.2.4.2 skrll pms_enable(void *v)
306 1.2.4.2 skrll {
307 1.2.4.2 skrll struct pms_softc *sc = v;
308 1.2.4.2 skrll int s;
309 1.2.4.2 skrll
310 1.2.4.2 skrll if (sc->sc_enabled)
311 1.2.4.2 skrll return EBUSY;
312 1.2.4.2 skrll
313 1.2.4.2 skrll do_enable(sc);
314 1.2.4.2 skrll
315 1.2.4.2 skrll s = spltty();
316 1.2.4.2 skrll sc->sc_enabled = 1;
317 1.2.4.2 skrll splx(s);
318 1.2.4.2 skrll
319 1.2.4.2 skrll return 0;
320 1.2.4.2 skrll }
321 1.2.4.2 skrll
322 1.2.4.2 skrll void
323 1.2.4.2 skrll pms_disable(void *v)
324 1.2.4.2 skrll {
325 1.2.4.2 skrll struct pms_softc *sc = v;
326 1.2.4.2 skrll int s;
327 1.2.4.2 skrll
328 1.2.4.2 skrll do_disable(sc);
329 1.2.4.2 skrll
330 1.2.4.2 skrll s = spltty();
331 1.2.4.2 skrll sc->sc_enabled = 0;
332 1.2.4.2 skrll splx(s);
333 1.2.4.2 skrll }
334 1.2.4.2 skrll
335 1.2.4.2 skrll #ifndef PMS_DISABLE_POWERHOOK
336 1.2.4.2 skrll void
337 1.2.4.2 skrll pms_power(int why, void *v)
338 1.2.4.2 skrll {
339 1.2.4.2 skrll struct pms_softc *sc = v;
340 1.2.4.2 skrll
341 1.2.4.2 skrll switch (why) {
342 1.2.4.2 skrll case PWR_STANDBY:
343 1.2.4.2 skrll break;
344 1.2.4.2 skrll case PWR_SUSPEND:
345 1.2.4.2 skrll if (sc->sc_enabled) {
346 1.2.4.2 skrll do_disable(sc);
347 1.2.4.2 skrll sc->sc_suspended = 1;
348 1.2.4.2 skrll }
349 1.2.4.2 skrll break;
350 1.2.4.2 skrll case PWR_RESUME:
351 1.2.4.7 skrll #ifdef PMS_SYNAPTICS_TOUCHPAD
352 1.2.4.8 skrll if (sc->protocol == PMS_SYNAPTICS) {
353 1.2.4.6 skrll pms_synaptics_resume(sc);
354 1.2.4.8 skrll sc->sc_suspended = 0;
355 1.2.4.8 skrll do_enable(sc);
356 1.2.4.8 skrll }
357 1.2.4.7 skrll #endif
358 1.2.4.2 skrll if (sc->sc_enabled && sc->sc_suspended) {
359 1.2.4.6 skrll /* recheck protocol & init mouse */
360 1.2.4.6 skrll sc->protocol = PMS_UNKNOWN;
361 1.2.4.2 skrll sc->sc_suspended = 0;
362 1.2.4.2 skrll do_enable(sc); /* only if we were suspended */
363 1.2.4.2 skrll }
364 1.2.4.8 skrll break;
365 1.2.4.2 skrll case PWR_SOFTSUSPEND:
366 1.2.4.2 skrll case PWR_SOFTSTANDBY:
367 1.2.4.2 skrll case PWR_SOFTRESUME:
368 1.2.4.2 skrll break;
369 1.2.4.2 skrll }
370 1.2.4.2 skrll }
371 1.2.4.2 skrll #endif /* !PMS_DISABLE_POWERHOOK */
372 1.2.4.2 skrll
373 1.2.4.2 skrll int
374 1.2.4.5 skrll pms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
375 1.2.4.2 skrll {
376 1.2.4.2 skrll struct pms_softc *sc = v;
377 1.2.4.2 skrll u_char kbcmd[2];
378 1.2.4.2 skrll int i;
379 1.2.4.2 skrll
380 1.2.4.2 skrll switch (cmd) {
381 1.2.4.2 skrll case WSMOUSEIO_GTYPE:
382 1.2.4.2 skrll *(u_int *)data = WSMOUSE_TYPE_PS2;
383 1.2.4.2 skrll break;
384 1.2.4.9 skrll
385 1.2.4.2 skrll case WSMOUSEIO_SRES:
386 1.2.4.2 skrll i = (*(u_int *)data - 12) / 25;
387 1.2.4.9 skrll
388 1.2.4.2 skrll if (i < 0)
389 1.2.4.2 skrll i = 0;
390 1.2.4.9 skrll
391 1.2.4.2 skrll if (i > 3)
392 1.2.4.2 skrll i = 3;
393 1.2.4.2 skrll
394 1.2.4.2 skrll kbcmd[0] = PMS_SET_RES;
395 1.2.4.9 skrll kbcmd[1] = i;
396 1.2.4.9 skrll i = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, kbcmd,
397 1.2.4.2 skrll 2, 0, 1, 0);
398 1.2.4.9 skrll
399 1.2.4.2 skrll if (i)
400 1.2.4.2 skrll printf("pms_ioctl: SET_RES command error\n");
401 1.2.4.2 skrll break;
402 1.2.4.6 skrll
403 1.2.4.2 skrll default:
404 1.2.4.2 skrll return EPASSTHROUGH;
405 1.2.4.2 skrll }
406 1.2.4.2 skrll return 0;
407 1.2.4.2 skrll }
408 1.2.4.2 skrll
409 1.2.4.2 skrll static void
410 1.2.4.2 skrll pms_spawn_reset_thread(void *arg)
411 1.2.4.2 skrll {
412 1.2.4.2 skrll struct pms_softc *sc = arg;
413 1.2.4.2 skrll
414 1.2.4.2 skrll kthread_create1(pms_reset_thread, sc, &sc->sc_event_thread,
415 1.2.4.2 skrll sc->sc_dev.dv_xname);
416 1.2.4.2 skrll }
417 1.2.4.2 skrll
418 1.2.4.2 skrll static void
419 1.2.4.2 skrll pms_reset_thread(void *arg)
420 1.2.4.2 skrll {
421 1.2.4.2 skrll struct pms_softc *sc = arg;
422 1.2.4.2 skrll u_char cmd[1], resp[2];
423 1.2.4.2 skrll int res;
424 1.2.4.2 skrll int save_protocol;
425 1.2.4.2 skrll
426 1.2.4.2 skrll for (;;) {
427 1.2.4.2 skrll tsleep(&sc->sc_enabled, PWAIT, "pmsreset", 0);
428 1.2.4.2 skrll #ifdef PMSDEBUG
429 1.2.4.2 skrll if (pmsdebug)
430 1.2.4.2 skrll #endif
431 1.2.4.2 skrll #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
432 1.2.4.9 skrll printf("%s: resetting mouse interface\n",
433 1.2.4.2 skrll sc->sc_dev.dv_xname);
434 1.2.4.2 skrll #endif
435 1.2.4.2 skrll save_protocol = sc->protocol;
436 1.2.4.2 skrll pms_disable(sc);
437 1.2.4.2 skrll cmd[0] = PMS_RESET;
438 1.2.4.6 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
439 1.2.4.6 skrll 1, 2, 1, resp);
440 1.2.4.2 skrll if (res)
441 1.2.4.9 skrll DPRINTF(("%s: reset error %d\n", sc->sc_dev.dv_xname,
442 1.2.4.2 skrll res));
443 1.2.4.6 skrll
444 1.2.4.7 skrll #ifdef PMS_SYNAPTICS_TOUCHPAD
445 1.2.4.6 skrll /* For the synaptics case, leave the protocol alone. */
446 1.2.4.7 skrll if (sc->protocol != PMS_SYNAPTICS)
447 1.2.4.7 skrll #endif
448 1.2.4.6 skrll sc->protocol = PMS_UNKNOWN;
449 1.2.4.7 skrll
450 1.2.4.2 skrll pms_enable(sc);
451 1.2.4.2 skrll if (sc->protocol != save_protocol) {
452 1.2.4.2 skrll #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
453 1.2.4.2 skrll printf("%s: protocol change, sleeping and retrying\n",
454 1.2.4.2 skrll sc->sc_dev.dv_xname);
455 1.2.4.2 skrll #endif
456 1.2.4.2 skrll pms_disable(sc);
457 1.2.4.2 skrll cmd[0] = PMS_RESET;
458 1.2.4.2 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag,
459 1.2.4.2 skrll sc->sc_kbcslot, cmd, 1, 2, 1, resp);
460 1.2.4.2 skrll if (res)
461 1.2.4.2 skrll DPRINTF(("%s: reset error %d\n",
462 1.2.4.2 skrll sc->sc_dev.dv_xname, res));
463 1.2.4.2 skrll tsleep(pms_reset_thread, PWAIT, "pmsreset", hz);
464 1.2.4.2 skrll cmd[0] = PMS_RESET;
465 1.2.4.2 skrll res = pckbport_enqueue_cmd(sc->sc_kbctag,
466 1.2.4.2 skrll sc->sc_kbcslot, cmd, 1, 2, 1, resp);
467 1.2.4.2 skrll if (res)
468 1.2.4.2 skrll DPRINTF(("%s: reset error %d\n",
469 1.2.4.2 skrll sc->sc_dev.dv_xname, res));
470 1.2.4.2 skrll sc->protocol = PMS_UNKNOWN; /* reprobe protocol */
471 1.2.4.2 skrll pms_enable(sc);
472 1.2.4.2 skrll #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
473 1.2.4.2 skrll if (sc->protocol != save_protocol) {
474 1.2.4.2 skrll printf("%s: protocol changed.\n",
475 1.2.4.2 skrll sc->sc_dev.dv_xname);
476 1.2.4.2 skrll }
477 1.2.4.2 skrll #endif
478 1.2.4.2 skrll }
479 1.2.4.2 skrll }
480 1.2.4.2 skrll }
481 1.2.4.2 skrll
482 1.2.4.2 skrll /* Masks for the first byte of a packet */
483 1.2.4.2 skrll #define PMS_LBUTMASK 0x01
484 1.2.4.2 skrll #define PMS_RBUTMASK 0x02
485 1.2.4.2 skrll #define PMS_MBUTMASK 0x04
486 1.2.4.2 skrll #define PMS_4BUTMASK 0x10
487 1.2.4.2 skrll #define PMS_5BUTMASK 0x20
488 1.2.4.2 skrll
489 1.2.4.2 skrll void
490 1.2.4.2 skrll pmsinput(void *vsc, int data)
491 1.2.4.2 skrll {
492 1.2.4.2 skrll struct pms_softc *sc = vsc;
493 1.2.4.2 skrll u_int changed;
494 1.2.4.2 skrll int dx, dy, dz = 0;
495 1.2.4.2 skrll int newbuttons = 0;
496 1.2.4.2 skrll int s;
497 1.2.4.2 skrll
498 1.2.4.2 skrll if (!sc->sc_enabled) {
499 1.2.4.2 skrll /* Interrupts are not expected. Discard the byte. */
500 1.2.4.2 skrll return;
501 1.2.4.2 skrll }
502 1.2.4.2 skrll
503 1.2.4.2 skrll s = splclock();
504 1.2.4.2 skrll sc->current = mono_time;
505 1.2.4.2 skrll splx(s);
506 1.2.4.2 skrll
507 1.2.4.2 skrll if (sc->inputstate > 0) {
508 1.2.4.2 skrll struct timeval diff;
509 1.2.4.2 skrll
510 1.2.4.2 skrll timersub(&sc->current, &sc->last, &diff);
511 1.2.4.2 skrll /*
512 1.2.4.2 skrll * Empirically, the delay should be about 1700us on a standard
513 1.2.4.2 skrll * PS/2 port. I have seen delays as large as 4500us (rarely)
514 1.2.4.2 skrll * in regular use. When using a confused mouse, I generally
515 1.2.4.2 skrll * see delays at least as large as 30,000us. -seebs
516 1.2.4.2 skrll *
517 1.2.4.2 skrll * The thinkpad trackball returns at 22-23ms. So we use
518 1.2.4.2 skrll * >= 40ms. In the future, I'll implement adaptable timeout
519 1.2.4.2 skrll * by increasing the timeout if the mouse reset happens
520 1.2.4.2 skrll * too frequently -christos
521 1.2.4.2 skrll */
522 1.2.4.2 skrll if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
523 1.2.4.2 skrll DPRINTF(("pms_input: unusual delay (%ld.%06ld s), "
524 1.2.4.2 skrll "scheduling reset\n",
525 1.2.4.2 skrll (long)diff.tv_sec, (long)diff.tv_usec));
526 1.2.4.2 skrll sc->inputstate = 0;
527 1.2.4.2 skrll sc->sc_enabled = 0;
528 1.2.4.2 skrll wakeup(&sc->sc_enabled);
529 1.2.4.2 skrll return;
530 1.2.4.2 skrll }
531 1.2.4.2 skrll }
532 1.2.4.2 skrll sc->last = sc->current;
533 1.2.4.2 skrll
534 1.2.4.2 skrll if (sc->inputstate == 0) {
535 1.2.4.2 skrll /*
536 1.2.4.2 skrll * Some devices (seen on trackballs anytime, and on
537 1.2.4.2 skrll * some mice shortly after reset) output garbage bytes
538 1.2.4.2 skrll * between packets. Just ignore them.
539 1.2.4.2 skrll */
540 1.2.4.2 skrll if ((data & 0xc0) != 0)
541 1.2.4.2 skrll return; /* not in sync yet, discard input */
542 1.2.4.2 skrll }
543 1.2.4.2 skrll
544 1.2.4.2 skrll sc->packet[sc->inputstate++] = data & 0xff;
545 1.2.4.2 skrll switch (sc->inputstate) {
546 1.2.4.2 skrll case 0:
547 1.2.4.2 skrll /* no useful processing can be done yet */
548 1.2.4.2 skrll break;
549 1.2.4.2 skrll
550 1.2.4.2 skrll case 1:
551 1.2.4.2 skrll /*
552 1.2.4.2 skrll * Why should we test for bit 0x8 and insist on it here?
553 1.2.4.2 skrll * The old (psm.c and psm_intelli.c) drivers didn't do
554 1.2.4.2 skrll * it, and there are devices where it does harm (that's
555 1.2.4.2 skrll * why it is not used if using PMS_STANDARD protocol).
556 1.2.4.2 skrll * Anyway, it does not to cause any harm to accept packets
557 1.2.4.2 skrll * without this bit.
558 1.2.4.2 skrll */
559 1.2.4.2 skrll #if 0
560 1.2.4.2 skrll if (sc->protocol == PMS_STANDARD)
561 1.2.4.2 skrll break;
562 1.2.4.2 skrll if (!(sc->packet[0] & 0x8)) {
563 1.2.4.2 skrll DPRINTF(("pmsinput: 0x8 not set in first byte "
564 1.2.4.2 skrll "[0x%02x], resetting\n", sc->packet[0]));
565 1.2.4.2 skrll sc->inputstate = 0;
566 1.2.4.2 skrll sc->sc_enabled = 0;
567 1.2.4.2 skrll wakeup(&sc->sc_enabled);
568 1.2.4.2 skrll return;
569 1.2.4.2 skrll }
570 1.2.4.2 skrll #endif
571 1.2.4.2 skrll break;
572 1.2.4.2 skrll
573 1.2.4.2 skrll case 2:
574 1.2.4.2 skrll break;
575 1.2.4.2 skrll
576 1.2.4.2 skrll case 4:
577 1.2.4.2 skrll /* Case 4 is a superset of case 3. This is *not* an accident. */
578 1.2.4.2 skrll if (sc->protocol == PMS_SCROLL3) {
579 1.2.4.2 skrll dz = sc->packet[3];
580 1.2.4.2 skrll if (dz >= 128)
581 1.2.4.2 skrll dz -= 256;
582 1.2.4.2 skrll if (dz == -128)
583 1.2.4.2 skrll dz = -127;
584 1.2.4.2 skrll } else if (sc->protocol == PMS_SCROLL5) {
585 1.2.4.2 skrll dz = sc->packet[3] & 0xf;
586 1.2.4.2 skrll if (dz >= 8)
587 1.2.4.2 skrll dz -= 16;
588 1.2.4.2 skrll if (sc->packet[3] & PMS_4BUTMASK)
589 1.2.4.2 skrll newbuttons |= 0x8;
590 1.2.4.2 skrll if (sc->packet[3] & PMS_5BUTMASK)
591 1.2.4.2 skrll newbuttons |= 0x10;
592 1.2.4.2 skrll } else {
593 1.2.4.2 skrll DPRINTF(("pmsinput: why am I looking at this byte?\n"));
594 1.2.4.2 skrll dz = 0;
595 1.2.4.2 skrll }
596 1.2.4.2 skrll /* FALLTHROUGH */
597 1.2.4.2 skrll case 3:
598 1.2.4.2 skrll /*
599 1.2.4.2 skrll * This is only an endpoint for scroll protocols with 4
600 1.2.4.2 skrll * bytes, or the standard protocol with 3.
601 1.2.4.2 skrll */
602 1.2.4.2 skrll if (sc->protocol != PMS_STANDARD && sc->inputstate == 3)
603 1.2.4.2 skrll break;
604 1.2.4.2 skrll
605 1.2.4.2 skrll newbuttons |= ((sc->packet[0] & PMS_LBUTMASK) ? 0x1 : 0) |
606 1.2.4.2 skrll ((sc->packet[0] & PMS_MBUTMASK) ? 0x2 : 0) |
607 1.2.4.2 skrll ((sc->packet[0] & PMS_RBUTMASK) ? 0x4 : 0);
608 1.2.4.2 skrll
609 1.2.4.2 skrll dx = sc->packet[1];
610 1.2.4.2 skrll if (dx >= 128)
611 1.2.4.2 skrll dx -= 256;
612 1.2.4.2 skrll if (dx == -128)
613 1.2.4.2 skrll dx = -127;
614 1.2.4.2 skrll
615 1.2.4.2 skrll dy = sc->packet[2];
616 1.2.4.2 skrll if (dy >= 128)
617 1.2.4.2 skrll dy -= 256;
618 1.2.4.2 skrll if (dy == -128)
619 1.2.4.2 skrll dy = -127;
620 1.2.4.9 skrll
621 1.2.4.2 skrll sc->inputstate = 0;
622 1.2.4.2 skrll changed = (sc->buttons ^ newbuttons);
623 1.2.4.2 skrll sc->buttons = newbuttons;
624 1.2.4.2 skrll
625 1.2.4.2 skrll #ifdef PMSDEBUG
626 1.2.4.2 skrll if (sc->protocol == PMS_STANDARD) {
627 1.2.4.2 skrll DPRINTF(("pms: packet: 0x%02x%02x%02x\n",
628 1.2.4.2 skrll sc->packet[0], sc->packet[1], sc->packet[2]));
629 1.2.4.2 skrll } else {
630 1.2.4.2 skrll DPRINTF(("pms: packet: 0x%02x%02x%02x%02x\n",
631 1.2.4.2 skrll sc->packet[0], sc->packet[1], sc->packet[2],
632 1.2.4.2 skrll sc->packet[3]));
633 1.2.4.2 skrll }
634 1.2.4.2 skrll #endif
635 1.2.4.2 skrll if (dx || dy || dz || changed) {
636 1.2.4.2 skrll #ifdef PMSDEBUG
637 1.2.4.2 skrll DPRINTF(("pms: x %+03d y %+03d z %+03d "
638 1.2.4.2 skrll "buttons 0x%02x\n", dx, dy, dz, sc->buttons));
639 1.2.4.2 skrll #endif
640 1.2.4.2 skrll wsmouse_input(sc->sc_wsmousedev,
641 1.2.4.2 skrll sc->buttons, dx, dy, dz,
642 1.2.4.2 skrll WSMOUSE_INPUT_DELTA);
643 1.2.4.2 skrll }
644 1.2.4.2 skrll memset(sc->packet, 0, 4);
645 1.2.4.2 skrll break;
646 1.2.4.2 skrll
647 1.2.4.2 skrll /* If we get here, we have problems. */
648 1.2.4.2 skrll default:
649 1.2.4.2 skrll printf("pmsinput: very confused. resetting.\n");
650 1.2.4.2 skrll sc->inputstate = 0;
651 1.2.4.2 skrll sc->sc_enabled = 0;
652 1.2.4.2 skrll wakeup(&sc->sc_enabled);
653 1.2.4.2 skrll return;
654 1.2.4.2 skrll }
655 1.2.4.2 skrll }
656