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