pms.c revision 1.4 1 /* $NetBSD: pms.c,v 1.4 2005/01/18 10:22:51 scw 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.4 2005/01/18 10:22:51 scw 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 enum pms_type tries[] = {
62 PMS_SCROLL5, PMS_SCROLL3, PMS_STANDARD, PMS_UNKNOWN
63 };
64
65 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 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 #endif
354 if (sc->sc_enabled && sc->sc_suspended) {
355 /* recheck protocol & init mouse */
356 sc->protocol = PMS_UNKNOWN;
357 sc->sc_suspended = 0;
358 do_enable(sc); /* only if we were suspended */
359 }
360 case PWR_SOFTSUSPEND:
361 case PWR_SOFTSTANDBY:
362 case PWR_SOFTRESUME:
363 break;
364 }
365 }
366 #endif /* !PMS_DISABLE_POWERHOOK */
367
368 int
369 pms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
370 {
371 struct pms_softc *sc = v;
372 u_char kbcmd[2];
373 int i;
374
375 switch (cmd) {
376 case WSMOUSEIO_GTYPE:
377 *(u_int *)data = WSMOUSE_TYPE_PS2;
378 break;
379
380 case WSMOUSEIO_SRES:
381 i = (*(u_int *)data - 12) / 25;
382
383 if (i < 0)
384 i = 0;
385
386 if (i > 3)
387 i = 3;
388
389 kbcmd[0] = PMS_SET_RES;
390 kbcmd[1] = i;
391 i = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, kbcmd,
392 2, 0, 1, 0);
393
394 if (i)
395 printf("pms_ioctl: SET_RES command error\n");
396 break;
397
398 default:
399 return EPASSTHROUGH;
400 }
401 return 0;
402 }
403
404 static void
405 pms_spawn_reset_thread(void *arg)
406 {
407 struct pms_softc *sc = arg;
408
409 kthread_create1(pms_reset_thread, sc, &sc->sc_event_thread,
410 sc->sc_dev.dv_xname);
411 }
412
413 static void
414 pms_reset_thread(void *arg)
415 {
416 struct pms_softc *sc = arg;
417 u_char cmd[1], resp[2];
418 int res;
419 int save_protocol;
420
421 for (;;) {
422 tsleep(&sc->sc_enabled, PWAIT, "pmsreset", 0);
423 #ifdef PMSDEBUG
424 if (pmsdebug)
425 #endif
426 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
427 printf("%s: resetting mouse interface\n",
428 sc->sc_dev.dv_xname);
429 #endif
430 save_protocol = sc->protocol;
431 pms_disable(sc);
432 cmd[0] = PMS_RESET;
433 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
434 1, 2, 1, resp);
435 if (res)
436 DPRINTF(("%s: reset error %d\n", sc->sc_dev.dv_xname,
437 res));
438
439 #ifdef PMS_SYNAPTICS_TOUCHPAD
440 /* For the synaptics case, leave the protocol alone. */
441 if (sc->protocol != PMS_SYNAPTICS)
442 #endif
443 sc->protocol = PMS_UNKNOWN;
444
445 pms_enable(sc);
446 if (sc->protocol != save_protocol) {
447 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
448 printf("%s: protocol change, sleeping and retrying\n",
449 sc->sc_dev.dv_xname);
450 #endif
451 pms_disable(sc);
452 cmd[0] = PMS_RESET;
453 res = pckbport_enqueue_cmd(sc->sc_kbctag,
454 sc->sc_kbcslot, cmd, 1, 2, 1, resp);
455 if (res)
456 DPRINTF(("%s: reset error %d\n",
457 sc->sc_dev.dv_xname, res));
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 sc->protocol = PMS_UNKNOWN; /* reprobe protocol */
466 pms_enable(sc);
467 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
468 if (sc->protocol != save_protocol) {
469 printf("%s: protocol changed.\n",
470 sc->sc_dev.dv_xname);
471 }
472 #endif
473 }
474 }
475 }
476
477 /* Masks for the first byte of a packet */
478 #define PMS_LBUTMASK 0x01
479 #define PMS_RBUTMASK 0x02
480 #define PMS_MBUTMASK 0x04
481 #define PMS_4BUTMASK 0x10
482 #define PMS_5BUTMASK 0x20
483
484 void
485 pmsinput(void *vsc, int data)
486 {
487 struct pms_softc *sc = vsc;
488 u_int changed;
489 int dx, dy, dz = 0;
490 int newbuttons = 0;
491 int s;
492
493 if (!sc->sc_enabled) {
494 /* Interrupts are not expected. Discard the byte. */
495 return;
496 }
497
498 s = splclock();
499 sc->current = mono_time;
500 splx(s);
501
502 if (sc->inputstate > 0) {
503 struct timeval diff;
504
505 timersub(&sc->current, &sc->last, &diff);
506 /*
507 * Empirically, the delay should be about 1700us on a standard
508 * PS/2 port. I have seen delays as large as 4500us (rarely)
509 * in regular use. When using a confused mouse, I generally
510 * see delays at least as large as 30,000us. -seebs
511 *
512 * The thinkpad trackball returns at 22-23ms. So we use
513 * >= 40ms. In the future, I'll implement adaptable timeout
514 * by increasing the timeout if the mouse reset happens
515 * too frequently -christos
516 */
517 if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
518 DPRINTF(("pms_input: unusual delay (%ld.%06ld s), "
519 "scheduling reset\n",
520 (long)diff.tv_sec, (long)diff.tv_usec));
521 sc->inputstate = 0;
522 sc->sc_enabled = 0;
523 wakeup(&sc->sc_enabled);
524 return;
525 }
526 }
527 sc->last = sc->current;
528
529 if (sc->inputstate == 0) {
530 /*
531 * Some devices (seen on trackballs anytime, and on
532 * some mice shortly after reset) output garbage bytes
533 * between packets. Just ignore them.
534 */
535 if ((data & 0xc0) != 0)
536 return; /* not in sync yet, discard input */
537 }
538
539 sc->packet[sc->inputstate++] = data & 0xff;
540 switch (sc->inputstate) {
541 case 0:
542 /* no useful processing can be done yet */
543 break;
544
545 case 1:
546 /*
547 * Why should we test for bit 0x8 and insist on it here?
548 * The old (psm.c and psm_intelli.c) drivers didn't do
549 * it, and there are devices where it does harm (that's
550 * why it is not used if using PMS_STANDARD protocol).
551 * Anyway, it does not to cause any harm to accept packets
552 * without this bit.
553 */
554 #if 0
555 if (sc->protocol == PMS_STANDARD)
556 break;
557 if (!(sc->packet[0] & 0x8)) {
558 DPRINTF(("pmsinput: 0x8 not set in first byte "
559 "[0x%02x], resetting\n", sc->packet[0]));
560 sc->inputstate = 0;
561 sc->sc_enabled = 0;
562 wakeup(&sc->sc_enabled);
563 return;
564 }
565 #endif
566 break;
567
568 case 2:
569 break;
570
571 case 4:
572 /* Case 4 is a superset of case 3. This is *not* an accident. */
573 if (sc->protocol == PMS_SCROLL3) {
574 dz = sc->packet[3];
575 if (dz >= 128)
576 dz -= 256;
577 if (dz == -128)
578 dz = -127;
579 } else if (sc->protocol == PMS_SCROLL5) {
580 dz = sc->packet[3] & 0xf;
581 if (dz >= 8)
582 dz -= 16;
583 if (sc->packet[3] & PMS_4BUTMASK)
584 newbuttons |= 0x8;
585 if (sc->packet[3] & PMS_5BUTMASK)
586 newbuttons |= 0x10;
587 } else {
588 DPRINTF(("pmsinput: why am I looking at this byte?\n"));
589 dz = 0;
590 }
591 /* FALLTHROUGH */
592 case 3:
593 /*
594 * This is only an endpoint for scroll protocols with 4
595 * bytes, or the standard protocol with 3.
596 */
597 if (sc->protocol != PMS_STANDARD && sc->inputstate == 3)
598 break;
599
600 newbuttons |= ((sc->packet[0] & PMS_LBUTMASK) ? 0x1 : 0) |
601 ((sc->packet[0] & PMS_MBUTMASK) ? 0x2 : 0) |
602 ((sc->packet[0] & PMS_RBUTMASK) ? 0x4 : 0);
603
604 dx = sc->packet[1];
605 if (dx >= 128)
606 dx -= 256;
607 if (dx == -128)
608 dx = -127;
609
610 dy = sc->packet[2];
611 if (dy >= 128)
612 dy -= 256;
613 if (dy == -128)
614 dy = -127;
615
616 sc->inputstate = 0;
617 changed = (sc->buttons ^ newbuttons);
618 sc->buttons = newbuttons;
619
620 #ifdef PMSDEBUG
621 if (sc->protocol == PMS_STANDARD) {
622 DPRINTF(("pms: packet: 0x%02x%02x%02x\n",
623 sc->packet[0], sc->packet[1], sc->packet[2]));
624 } else {
625 DPRINTF(("pms: packet: 0x%02x%02x%02x%02x\n",
626 sc->packet[0], sc->packet[1], sc->packet[2],
627 sc->packet[3]));
628 }
629 #endif
630 if (dx || dy || dz || changed) {
631 #ifdef PMSDEBUG
632 DPRINTF(("pms: x %+03d y %+03d z %+03d "
633 "buttons 0x%02x\n", dx, dy, dz, sc->buttons));
634 #endif
635 wsmouse_input(sc->sc_wsmousedev,
636 sc->buttons, dx, dy, dz,
637 WSMOUSE_INPUT_DELTA);
638 }
639 memset(sc->packet, 0, 4);
640 break;
641
642 /* If we get here, we have problems. */
643 default:
644 printf("pmsinput: very confused. resetting.\n");
645 sc->inputstate = 0;
646 sc->sc_enabled = 0;
647 wakeup(&sc->sc_enabled);
648 return;
649 }
650 }
651