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