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