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