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