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