pms.c revision 1.18 1 /* $NetBSD: pms.c,v 1.18 2007/07/09 21:01:19 ad 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 "opt_pms.h"
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pms.c,v 1.18 2007/07/09 21:01:19 ad Exp $");
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 <machine/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(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 #ifndef PMS_DISABLE_POWERHOOK
89 void pms_power(int, void *);
90 #endif /* !PMS_DISABLE_POWERHOOK */
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_kbctag = pa->pa_tag;
180 sc->sc_kbcslot = pa->pa_slot;
181
182 printf("\n");
183
184 /* Flush any garbage. */
185 pckbport_flush(pa->pa_tag, pa->pa_slot);
186
187 /* reset the device */
188 cmd[0] = PMS_RESET;
189 res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 1);
190 #ifdef DEBUG
191 if (res || resp[0] != PMS_RSTDONE || resp[1] != 0) {
192 printf("pmsattach: reset error\n");
193 return;
194 }
195 #endif
196 sc->inputstate = 0;
197 sc->buttons = 0;
198 sc->protocol = PMS_UNKNOWN;
199
200 #ifdef PMS_SYNAPTICS_TOUCHPAD
201 /* Probe for synaptics touchpad. */
202 if (pms_synaptics_probe_init(sc) == 0) {
203 sc->protocol = PMS_SYNAPTICS;
204 } else
205 #endif
206 /* Install generic handler. */
207 pckbport_set_inputhandler(sc->sc_kbctag, sc->sc_kbcslot,
208 pmsinput, sc, sc->sc_dev.dv_xname);
209
210 a.accessops = &pms_accessops;
211 a.accesscookie = sc;
212
213 /*
214 * Attach the wsmouse, saving a handle to it.
215 * Note that we don't need to check this pointer against NULL
216 * here or in pmsintr, because if this fails pms_enable() will
217 * never be called, so pmsinput() will never be called.
218 */
219 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
220
221 /* no interrupts until enabled */
222 cmd[0] = PMS_DEV_DISABLE;
223 res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 0, 0, 0);
224 if (res)
225 printf("pmsattach: disable error\n");
226 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
227
228 kthread_create(PRI_NONE, 0, NULL, pms_reset_thread, sc,
229 &sc->sc_event_thread, sc->sc_dev.dv_xname);
230
231 #ifndef PMS_DISABLE_POWERHOOK
232 sc->sc_powerhook = powerhook_establish(self->dv_xname, pms_power, sc);
233 sc->sc_suspended = 0;
234 #endif /* !PMS_DISABLE_POWERHOOK */
235 }
236
237 static void
238 do_enable(struct pms_softc *sc)
239 {
240 u_char cmd[2];
241 int res;
242
243 sc->inputstate = 0;
244 sc->buttons = 0;
245
246 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
247
248 #ifdef PMS_SYNAPTICS_TOUCHPAD
249 if (sc->protocol == PMS_SYNAPTICS)
250 pms_synaptics_enable(sc);
251 #endif
252
253 cmd[0] = PMS_DEV_ENABLE;
254 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
255 1, 0, 1, 0);
256 if (res)
257 printf("pms_enable: command error %d\n", res);
258
259 if (sc->protocol == PMS_UNKNOWN)
260 sc->protocol = pms_protocol(sc->sc_kbctag, sc->sc_kbcslot);
261 DPRINTF(("pms_enable: using %s protocol\n",
262 pms_protocols[sc->protocol].name));
263 #if 0
264 {
265 u_char scmd[2];
266
267 scmd[0] = PMS_SET_RES;
268 scmd[1] = 3; /* 8 counts/mm */
269 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
270 2, 0, 1, 0);
271 if (res)
272 printf("pms_enable: setup error1 (%d)\n", res);
273
274 scmd[0] = PMS_SET_SCALE21;
275 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
276 1, 0, 1, 0);
277 if (res)
278 printf("pms_enable: setup error2 (%d)\n", res);
279
280 scmd[0] = PMS_SET_SAMPLE;
281 scmd[1] = 100; /* 100 samples/sec */
282 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
283 2, 0, 1, 0);
284 if (res)
285 printf("pms_enable: setup error3 (%d)\n", res);
286 }
287 #endif
288 }
289
290 static void
291 do_disable(struct pms_softc *sc)
292 {
293 u_char cmd[1];
294 int res;
295
296 cmd[0] = PMS_DEV_DISABLE;
297 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
298 1, 0, 1, 0);
299 if (res)
300 printf("pms_disable: command error\n");
301
302 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
303 }
304
305 int
306 pms_enable(void *v)
307 {
308 struct pms_softc *sc = v;
309 int s;
310
311 if (sc->sc_enabled)
312 return EBUSY;
313
314 do_enable(sc);
315
316 s = spltty();
317 sc->sc_enabled = 1;
318 splx(s);
319
320 return 0;
321 }
322
323 void
324 pms_disable(void *v)
325 {
326 struct pms_softc *sc = v;
327 int s;
328
329 do_disable(sc);
330
331 s = spltty();
332 sc->sc_enabled = 0;
333 splx(s);
334 }
335
336 #ifndef PMS_DISABLE_POWERHOOK
337 void
338 pms_power(int why, void *v)
339 {
340 struct pms_softc *sc = v;
341
342 switch (why) {
343 case PWR_STANDBY:
344 break;
345 case PWR_SUSPEND:
346 if (sc->sc_enabled) {
347 do_disable(sc);
348 sc->sc_suspended = 1;
349 }
350 break;
351 case PWR_RESUME:
352 #ifdef PMS_SYNAPTICS_TOUCHPAD
353 if (sc->protocol == PMS_SYNAPTICS) {
354 pms_synaptics_resume(sc);
355 sc->sc_suspended = 0;
356 do_enable(sc);
357 }
358 #endif
359 if (sc->sc_enabled && sc->sc_suspended) {
360 /* recheck protocol & init mouse */
361 sc->protocol = PMS_UNKNOWN;
362 sc->sc_suspended = 0;
363 do_enable(sc); /* only if we were suspended */
364 }
365 break;
366 case PWR_SOFTSUSPEND:
367 case PWR_SOFTSTANDBY:
368 case PWR_SOFTRESUME:
369 break;
370 }
371 }
372 #endif /* !PMS_DISABLE_POWERHOOK */
373
374 int
375 pms_ioctl(void *v, u_long cmd, void *data, int flag,
376 struct lwp *l)
377 {
378 struct pms_softc *sc = v;
379 u_char kbcmd[2];
380 int i;
381
382 switch (cmd) {
383 case WSMOUSEIO_GTYPE:
384 *(u_int *)data = WSMOUSE_TYPE_PS2;
385 break;
386
387 case WSMOUSEIO_SRES:
388 i = (*(u_int *)data - 12) / 25;
389
390 if (i < 0)
391 i = 0;
392
393 if (i > 3)
394 i = 3;
395
396 kbcmd[0] = PMS_SET_RES;
397 kbcmd[1] = i;
398 i = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, kbcmd,
399 2, 0, 1, 0);
400
401 if (i)
402 printf("pms_ioctl: SET_RES command error\n");
403 break;
404
405 default:
406 return EPASSTHROUGH;
407 }
408 return 0;
409 }
410
411 static void
412 pms_reset_thread(void *arg)
413 {
414 struct pms_softc *sc = arg;
415 u_char cmd[1], resp[2];
416 int res;
417 int save_protocol;
418
419 for (;;) {
420 tsleep(&sc->sc_enabled, PWAIT, "pmsreset", 0);
421 #ifdef PMSDEBUG
422 if (pmsdebug)
423 #endif
424 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
425 printf("%s: resetting mouse interface\n",
426 sc->sc_dev.dv_xname);
427 #endif
428 save_protocol = sc->protocol;
429 pms_disable(sc);
430 cmd[0] = PMS_RESET;
431 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
432 1, 2, 1, resp);
433 if (res) {
434 DPRINTF(("%s: reset error %d\n", sc->sc_dev.dv_xname,
435 res));
436 }
437
438 #ifdef PMS_SYNAPTICS_TOUCHPAD
439 /* For the synaptics case, leave the protocol alone. */
440 if (sc->protocol != PMS_SYNAPTICS)
441 #endif
442 sc->protocol = PMS_UNKNOWN;
443
444 pms_enable(sc);
445 if (sc->protocol != save_protocol) {
446 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
447 printf("%s: protocol change, sleeping and retrying\n",
448 sc->sc_dev.dv_xname);
449 #endif
450 pms_disable(sc);
451 cmd[0] = PMS_RESET;
452 res = pckbport_enqueue_cmd(sc->sc_kbctag,
453 sc->sc_kbcslot, cmd, 1, 2, 1, resp);
454 if (res) {
455 DPRINTF(("%s: reset error %d\n",
456 sc->sc_dev.dv_xname, res));
457 }
458 tsleep(pms_reset_thread, PWAIT, "pmsreset", hz);
459 cmd[0] = PMS_RESET;
460 res = pckbport_enqueue_cmd(sc->sc_kbctag,
461 sc->sc_kbcslot, cmd, 1, 2, 1, resp);
462 if (res) {
463 DPRINTF(("%s: reset error %d\n",
464 sc->sc_dev.dv_xname, res));
465 }
466 sc->protocol = PMS_UNKNOWN; /* reprobe protocol */
467 pms_enable(sc);
468 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
469 if (sc->protocol != save_protocol) {
470 printf("%s: protocol changed.\n",
471 sc->sc_dev.dv_xname);
472 }
473 #endif
474 }
475 }
476 }
477
478 /* Masks for the first byte of a packet */
479 #define PMS_LBUTMASK 0x01
480 #define PMS_RBUTMASK 0x02
481 #define PMS_MBUTMASK 0x04
482 #define PMS_4BUTMASK 0x10
483 #define PMS_5BUTMASK 0x20
484
485 void
486 pmsinput(void *vsc, int data)
487 {
488 struct pms_softc *sc = vsc;
489 u_int changed;
490 int dx, dy, dz = 0;
491 int newbuttons = 0;
492
493 if (!sc->sc_enabled) {
494 /* Interrupts are not expected. Discard the byte. */
495 return;
496 }
497
498 getmicrouptime(&sc->current);
499
500 if (sc->inputstate > 0) {
501 struct timeval diff;
502
503 timersub(&sc->current, &sc->last, &diff);
504 /*
505 * Empirically, the delay should be about 1700us on a standard
506 * PS/2 port. I have seen delays as large as 4500us (rarely)
507 * in regular use. When using a confused mouse, I generally
508 * see delays at least as large as 30,000us. -seebs
509 *
510 * The thinkpad trackball returns at 22-23ms. So we use
511 * >= 40ms. In the future, I'll implement adaptable timeout
512 * by increasing the timeout if the mouse reset happens
513 * too frequently -christos
514 */
515 if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
516 DPRINTF(("pms_input: unusual delay (%ld.%06ld s), "
517 "scheduling reset\n",
518 (long)diff.tv_sec, (long)diff.tv_usec));
519 sc->inputstate = 0;
520 sc->sc_enabled = 0;
521 wakeup(&sc->sc_enabled);
522 return;
523 }
524 }
525 sc->last = sc->current;
526
527 if (sc->inputstate == 0) {
528 /*
529 * Some devices (seen on trackballs anytime, and on
530 * some mice shortly after reset) output garbage bytes
531 * between packets. Just ignore them.
532 */
533 if ((data & 0xc0) != 0)
534 return; /* not in sync yet, discard input */
535 }
536
537 sc->packet[sc->inputstate++] = data & 0xff;
538 switch (sc->inputstate) {
539 case 0:
540 /* no useful processing can be done yet */
541 break;
542
543 case 1:
544 /*
545 * Why should we test for bit 0x8 and insist on it here?
546 * The old (psm.c and psm_intelli.c) drivers didn't do
547 * it, and there are devices where it does harm (that's
548 * why it is not used if using PMS_STANDARD protocol).
549 * Anyway, it does not to cause any harm to accept packets
550 * without this bit.
551 */
552 #if 0
553 if (sc->protocol == PMS_STANDARD)
554 break;
555 if (!(sc->packet[0] & 0x8)) {
556 DPRINTF(("pmsinput: 0x8 not set in first byte "
557 "[0x%02x], resetting\n", sc->packet[0]));
558 sc->inputstate = 0;
559 sc->sc_enabled = 0;
560 wakeup(&sc->sc_enabled);
561 return;
562 }
563 #endif
564 break;
565
566 case 2:
567 break;
568
569 case 4:
570 /* Case 4 is a superset of case 3. This is *not* an accident. */
571 if (sc->protocol == PMS_SCROLL3) {
572 dz = sc->packet[3];
573 if (dz >= 128)
574 dz -= 256;
575 if (dz == -128)
576 dz = -127;
577 } else if (sc->protocol == PMS_SCROLL5) {
578 dz = sc->packet[3] & 0xf;
579 if (dz >= 8)
580 dz -= 16;
581 if (sc->packet[3] & PMS_4BUTMASK)
582 newbuttons |= 0x8;
583 if (sc->packet[3] & PMS_5BUTMASK)
584 newbuttons |= 0x10;
585 } else {
586 DPRINTF(("pmsinput: why am I looking at this byte?\n"));
587 dz = 0;
588 }
589 /* FALLTHROUGH */
590 case 3:
591 /*
592 * This is only an endpoint for scroll protocols with 4
593 * bytes, or the standard protocol with 3.
594 */
595 if (sc->protocol != PMS_STANDARD && sc->inputstate == 3)
596 break;
597
598 newbuttons |= ((sc->packet[0] & PMS_LBUTMASK) ? 0x1 : 0) |
599 ((sc->packet[0] & PMS_MBUTMASK) ? 0x2 : 0) |
600 ((sc->packet[0] & PMS_RBUTMASK) ? 0x4 : 0);
601
602 dx = sc->packet[1];
603 if (dx >= 128)
604 dx -= 256;
605 if (dx == -128)
606 dx = -127;
607
608 dy = sc->packet[2];
609 if (dy >= 128)
610 dy -= 256;
611 if (dy == -128)
612 dy = -127;
613
614 sc->inputstate = 0;
615 changed = (sc->buttons ^ newbuttons);
616 sc->buttons = newbuttons;
617
618 #ifdef PMSDEBUG
619 if (sc->protocol == PMS_STANDARD) {
620 DPRINTF(("pms: packet: 0x%02x%02x%02x\n",
621 sc->packet[0], sc->packet[1], sc->packet[2]));
622 } else {
623 DPRINTF(("pms: packet: 0x%02x%02x%02x%02x\n",
624 sc->packet[0], sc->packet[1], sc->packet[2],
625 sc->packet[3]));
626 }
627 #endif
628 if (dx || dy || dz || changed) {
629 #ifdef PMSDEBUG
630 DPRINTF(("pms: x %+03d y %+03d z %+03d "
631 "buttons 0x%02x\n", dx, dy, dz, sc->buttons));
632 #endif
633 wsmouse_input(sc->sc_wsmousedev,
634 sc->buttons, dx, dy, dz, 0,
635 WSMOUSE_INPUT_DELTA);
636 }
637 memset(sc->packet, 0, 4);
638 break;
639
640 /* If we get here, we have problems. */
641 default:
642 printf("pmsinput: very confused. resetting.\n");
643 sc->inputstate = 0;
644 sc->sc_enabled = 0;
645 wakeup(&sc->sc_enabled);
646 return;
647 }
648 }
649