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