pms.c revision 1.19.4.1 1 /* $NetBSD: pms.c,v 1.19.4.1 2007/08/03 22:17:23 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.19.4.1 2007/08/03 22:17:23 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 <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 { { 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 pnp_status_t pms_power(device_t, pnp_request_t, void *);
89
90 const struct wsmouse_accessops pms_accessops = {
91 pms_enable,
92 pms_ioctl,
93 pms_disable,
94 };
95
96 static int
97 pms_protocol(pckbport_tag_t tag, pckbport_slot_t slot)
98 {
99 u_char cmd[2], resp[1];
100 int i, j, res;
101 const struct pms_protocol *p;
102
103 for (j = 0; j < sizeof(tries) / sizeof(tries[0]); ++j) {
104 p = &pms_protocols[tries[j]];
105 if (!p->rates[0])
106 break;
107 cmd[0] = PMS_SET_SAMPLE;
108 for (i = 0; i < 3; i++) {
109 cmd[1] = p->rates[i];
110 res = pckbport_enqueue_cmd(tag, slot, cmd, 2, 0, 1, 0);
111 if (res)
112 return PMS_STANDARD;
113 }
114
115 cmd[0] = PMS_SEND_DEV_ID;
116 res = pckbport_enqueue_cmd(tag, slot, cmd, 1, 1, 1, resp);
117 if (res)
118 return PMS_UNKNOWN;
119 if (resp[0] == p->response) {
120 DPRINTF(("pms_protocol: found mouse protocol %d\n",
121 tries[j]));
122 return tries[j];
123 }
124 }
125 DPRINTF(("pms_protocol: standard PS/2 protocol (no scroll wheel)\n"));
126 return PMS_STANDARD;
127 }
128
129 int
130 pmsprobe(struct device *parent, struct cfdata *match,
131 void *aux)
132 {
133 struct pckbport_attach_args *pa = aux;
134 u_char cmd[1], resp[2];
135 int res;
136
137 if (pa->pa_slot != PCKBPORT_AUX_SLOT)
138 return 0;
139
140 /* Flush any garbage. */
141 pckbport_flush(pa->pa_tag, pa->pa_slot);
142
143 /* reset the device */
144 cmd[0] = PMS_RESET;
145 res = pckbport_poll_cmd(pa->pa_tag, pa->pa_slot, cmd, 1, 2, resp, 1);
146 if (res) {
147 #ifdef DEBUG
148 printf("pmsprobe: reset error %d\n", res);
149 #endif
150 return 0;
151 }
152 if (resp[0] != PMS_RSTDONE) {
153 printf("pmsprobe: reset response 0x%x\n", resp[0]);
154 return 0;
155 }
156
157 /* get type number (0 = mouse) */
158 if (resp[1] != 0) {
159 #ifdef DEBUG
160 printf("pmsprobe: type 0x%x\n", resp[1]);
161 #endif
162 return 0;
163 }
164
165 return 10;
166 }
167
168 void
169 pmsattach(struct device *parent, struct device *self, void *aux)
170 {
171 struct pms_softc *sc = device_private(self);
172 struct pckbport_attach_args *pa = aux;
173 struct wsmousedev_attach_args a;
174 pnp_status_t status;
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(PRI_NONE, 0, NULL, pms_reset_thread, sc,
228 &sc->sc_event_thread, sc->sc_dev.dv_xname);
229
230 sc->sc_suspended = 0;
231 status = pnp_register(self, pms_power);
232 if (status != PNP_STATUS_SUCCESS)
233 aprint_error("%s: couldn't establish power handler\n",
234 device_xname(self));
235 }
236
237 static void
238 do_enable(struct pms_softc *sc)
239 {
240 u_char cmd[2];
241 int res;
242
243 sc->inputstate = 0;
244 sc->buttons = 0;
245
246 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 1);
247
248 #ifdef PMS_SYNAPTICS_TOUCHPAD
249 if (sc->protocol == PMS_SYNAPTICS)
250 pms_synaptics_enable(sc);
251 #endif
252
253 cmd[0] = PMS_DEV_ENABLE;
254 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
255 1, 0, 1, 0);
256 if (res)
257 printf("pms_enable: command error %d\n", res);
258
259 if (sc->protocol == PMS_UNKNOWN)
260 sc->protocol = pms_protocol(sc->sc_kbctag, sc->sc_kbcslot);
261 DPRINTF(("pms_enable: using %s protocol\n",
262 pms_protocols[sc->protocol].name));
263 #if 0
264 {
265 u_char scmd[2];
266
267 scmd[0] = PMS_SET_RES;
268 scmd[1] = 3; /* 8 counts/mm */
269 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
270 2, 0, 1, 0);
271 if (res)
272 printf("pms_enable: setup error1 (%d)\n", res);
273
274 scmd[0] = PMS_SET_SCALE21;
275 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
276 1, 0, 1, 0);
277 if (res)
278 printf("pms_enable: setup error2 (%d)\n", res);
279
280 scmd[0] = PMS_SET_SAMPLE;
281 scmd[1] = 100; /* 100 samples/sec */
282 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, scmd,
283 2, 0, 1, 0);
284 if (res)
285 printf("pms_enable: setup error3 (%d)\n", res);
286 }
287 #endif
288 }
289
290 static void
291 do_disable(struct pms_softc *sc)
292 {
293 u_char cmd[1];
294 int res;
295
296 cmd[0] = PMS_DEV_DISABLE;
297 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
298 1, 0, 1, 0);
299 if (res)
300 printf("pms_disable: command error\n");
301
302 pckbport_slot_enable(sc->sc_kbctag, sc->sc_kbcslot, 0);
303 }
304
305 int
306 pms_enable(void *v)
307 {
308 struct pms_softc *sc = v;
309 int s;
310
311 if (sc->sc_enabled)
312 return EBUSY;
313
314 do_enable(sc);
315
316 s = spltty();
317 sc->sc_enabled = 1;
318 splx(s);
319
320 return 0;
321 }
322
323 void
324 pms_disable(void *v)
325 {
326 struct pms_softc *sc = v;
327 int s;
328
329 do_disable(sc);
330
331 s = spltty();
332 sc->sc_enabled = 0;
333 splx(s);
334 }
335
336 pnp_status_t
337 pms_power(device_t dv, pnp_request_t req, void *opaque)
338 {
339 #ifndef PMS_DISABLE_POWERHOOK
340 struct pms_softc *sc = (struct pms_softc *)dv;
341 pnp_state_t *pstate;
342 pnp_capabilities_t *pcaps;
343
344 switch (req) {
345 case PNP_REQUEST_GET_CAPABILITIES:
346 pcaps = opaque;
347 pcaps->state |= PNP_STATE_D0 | PNP_STATE_D3;
348 break;
349
350 case PNP_REQUEST_GET_STATE:
351 pstate = opaque;
352 *pstate = PNP_STATE_D0; /* XXX */
353 break;
354
355 case PNP_REQUEST_SET_STATE:
356 pstate = opaque;
357
358 switch (*pstate) {
359 case PNP_STATE_D3:
360 if (sc->sc_enabled) {
361 do_disable(sc);
362 sc->sc_suspended = 1;
363 }
364 break;
365 case PNP_STATE_D0:
366 #ifdef PMS_SYNAPTICS_TOUCHPAD
367 if (sc->protocol == PMS_SYNAPTICS) {
368 pms_synaptics_resume(sc);
369 sc->sc_suspended = 0;
370 do_enable(sc);
371 }
372 #endif
373 if (sc->sc_enabled && sc->sc_suspended) {
374 /* recheck protocol & init mouse */
375 sc->protocol = PMS_UNKNOWN;
376 sc->sc_suspended = 0;
377 do_enable(sc); /* only if we were suspended */
378 }
379 break;
380 default:
381 return PNP_STATUS_UNSUPPORTED;
382 }
383 break;
384
385 default:
386 return PNP_STATUS_UNSUPPORTED;
387 }
388
389 #endif /* !PMS_DISABLE_POWERHOOK */
390 return PNP_STATUS_SUCCESS;
391 }
392
393 int
394 pms_ioctl(void *v, u_long cmd, void *data, int flag,
395 struct lwp *l)
396 {
397 struct pms_softc *sc = v;
398 u_char kbcmd[2];
399 int i;
400
401 switch (cmd) {
402 case WSMOUSEIO_GTYPE:
403 *(u_int *)data = WSMOUSE_TYPE_PS2;
404 break;
405
406 case WSMOUSEIO_SRES:
407 i = (*(u_int *)data - 12) / 25;
408
409 if (i < 0)
410 i = 0;
411
412 if (i > 3)
413 i = 3;
414
415 kbcmd[0] = PMS_SET_RES;
416 kbcmd[1] = i;
417 i = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, kbcmd,
418 2, 0, 1, 0);
419
420 if (i)
421 printf("pms_ioctl: SET_RES command error\n");
422 break;
423
424 default:
425 return EPASSTHROUGH;
426 }
427 return 0;
428 }
429
430 static void
431 pms_reset_thread(void *arg)
432 {
433 struct pms_softc *sc = arg;
434 u_char cmd[1], resp[2];
435 int res;
436 int save_protocol;
437
438 for (;;) {
439 tsleep(&sc->sc_enabled, PWAIT, "pmsreset", 0);
440 #ifdef PMSDEBUG
441 if (pmsdebug)
442 #endif
443 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
444 printf("%s: resetting mouse interface\n",
445 sc->sc_dev.dv_xname);
446 #endif
447 save_protocol = sc->protocol;
448 pms_disable(sc);
449 cmd[0] = PMS_RESET;
450 res = pckbport_enqueue_cmd(sc->sc_kbctag, sc->sc_kbcslot, cmd,
451 1, 2, 1, resp);
452 if (res) {
453 DPRINTF(("%s: reset error %d\n", sc->sc_dev.dv_xname,
454 res));
455 }
456
457 #ifdef PMS_SYNAPTICS_TOUCHPAD
458 /* For the synaptics case, leave the protocol alone. */
459 if (sc->protocol != PMS_SYNAPTICS)
460 #endif
461 sc->protocol = PMS_UNKNOWN;
462
463 pms_enable(sc);
464 if (sc->protocol != save_protocol) {
465 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
466 printf("%s: protocol change, sleeping and retrying\n",
467 sc->sc_dev.dv_xname);
468 #endif
469 pms_disable(sc);
470 cmd[0] = PMS_RESET;
471 res = pckbport_enqueue_cmd(sc->sc_kbctag,
472 sc->sc_kbcslot, cmd, 1, 2, 1, resp);
473 if (res) {
474 DPRINTF(("%s: reset error %d\n",
475 sc->sc_dev.dv_xname, res));
476 }
477 tsleep(pms_reset_thread, PWAIT, "pmsreset", hz);
478 cmd[0] = PMS_RESET;
479 res = pckbport_enqueue_cmd(sc->sc_kbctag,
480 sc->sc_kbcslot, cmd, 1, 2, 1, resp);
481 if (res) {
482 DPRINTF(("%s: reset error %d\n",
483 sc->sc_dev.dv_xname, res));
484 }
485 sc->protocol = PMS_UNKNOWN; /* reprobe protocol */
486 pms_enable(sc);
487 #if defined(PMSDEBUG) || defined(DIAGNOSTIC)
488 if (sc->protocol != save_protocol) {
489 printf("%s: protocol changed.\n",
490 sc->sc_dev.dv_xname);
491 }
492 #endif
493 }
494 }
495 }
496
497 /* Masks for the first byte of a packet */
498 #define PMS_LBUTMASK 0x01
499 #define PMS_RBUTMASK 0x02
500 #define PMS_MBUTMASK 0x04
501 #define PMS_4BUTMASK 0x10
502 #define PMS_5BUTMASK 0x20
503
504 void
505 pmsinput(void *vsc, int data)
506 {
507 struct pms_softc *sc = vsc;
508 u_int changed;
509 int dx, dy, dz = 0;
510 int newbuttons = 0;
511
512 if (!sc->sc_enabled) {
513 /* Interrupts are not expected. Discard the byte. */
514 return;
515 }
516
517 getmicrouptime(&sc->current);
518
519 if (sc->inputstate > 0) {
520 struct timeval diff;
521
522 timersub(&sc->current, &sc->last, &diff);
523 /*
524 * Empirically, the delay should be about 1700us on a standard
525 * PS/2 port. I have seen delays as large as 4500us (rarely)
526 * in regular use. When using a confused mouse, I generally
527 * see delays at least as large as 30,000us. -seebs
528 *
529 * The thinkpad trackball returns at 22-23ms. So we use
530 * >= 40ms. In the future, I'll implement adaptable timeout
531 * by increasing the timeout if the mouse reset happens
532 * too frequently -christos
533 */
534 if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
535 DPRINTF(("pms_input: unusual delay (%ld.%06ld s), "
536 "scheduling reset\n",
537 (long)diff.tv_sec, (long)diff.tv_usec));
538 sc->inputstate = 0;
539 sc->sc_enabled = 0;
540 wakeup(&sc->sc_enabled);
541 return;
542 }
543 }
544 sc->last = sc->current;
545
546 if (sc->inputstate == 0) {
547 /*
548 * Some devices (seen on trackballs anytime, and on
549 * some mice shortly after reset) output garbage bytes
550 * between packets. Just ignore them.
551 */
552 if ((data & 0xc0) != 0)
553 return; /* not in sync yet, discard input */
554 }
555
556 sc->packet[sc->inputstate++] = data & 0xff;
557 switch (sc->inputstate) {
558 case 0:
559 /* no useful processing can be done yet */
560 break;
561
562 case 1:
563 /*
564 * Why should we test for bit 0x8 and insist on it here?
565 * The old (psm.c and psm_intelli.c) drivers didn't do
566 * it, and there are devices where it does harm (that's
567 * why it is not used if using PMS_STANDARD protocol).
568 * Anyway, it does not to cause any harm to accept packets
569 * without this bit.
570 */
571 #if 0
572 if (sc->protocol == PMS_STANDARD)
573 break;
574 if (!(sc->packet[0] & 0x8)) {
575 DPRINTF(("pmsinput: 0x8 not set in first byte "
576 "[0x%02x], resetting\n", sc->packet[0]));
577 sc->inputstate = 0;
578 sc->sc_enabled = 0;
579 wakeup(&sc->sc_enabled);
580 return;
581 }
582 #endif
583 break;
584
585 case 2:
586 break;
587
588 case 4:
589 /* Case 4 is a superset of case 3. This is *not* an accident. */
590 if (sc->protocol == PMS_SCROLL3) {
591 dz = sc->packet[3];
592 if (dz >= 128)
593 dz -= 256;
594 if (dz == -128)
595 dz = -127;
596 } else if (sc->protocol == PMS_SCROLL5) {
597 dz = sc->packet[3] & 0xf;
598 if (dz >= 8)
599 dz -= 16;
600 if (sc->packet[3] & PMS_4BUTMASK)
601 newbuttons |= 0x8;
602 if (sc->packet[3] & PMS_5BUTMASK)
603 newbuttons |= 0x10;
604 } else {
605 DPRINTF(("pmsinput: why am I looking at this byte?\n"));
606 dz = 0;
607 }
608 /* FALLTHROUGH */
609 case 3:
610 /*
611 * This is only an endpoint for scroll protocols with 4
612 * bytes, or the standard protocol with 3.
613 */
614 if (sc->protocol != PMS_STANDARD && sc->inputstate == 3)
615 break;
616
617 newbuttons |= ((sc->packet[0] & PMS_LBUTMASK) ? 0x1 : 0) |
618 ((sc->packet[0] & PMS_MBUTMASK) ? 0x2 : 0) |
619 ((sc->packet[0] & PMS_RBUTMASK) ? 0x4 : 0);
620
621 dx = sc->packet[1];
622 if (dx >= 128)
623 dx -= 256;
624 if (dx == -128)
625 dx = -127;
626
627 dy = sc->packet[2];
628 if (dy >= 128)
629 dy -= 256;
630 if (dy == -128)
631 dy = -127;
632
633 sc->inputstate = 0;
634 changed = (sc->buttons ^ newbuttons);
635 sc->buttons = newbuttons;
636
637 #ifdef PMSDEBUG
638 if (sc->protocol == PMS_STANDARD) {
639 DPRINTF(("pms: packet: 0x%02x%02x%02x\n",
640 sc->packet[0], sc->packet[1], sc->packet[2]));
641 } else {
642 DPRINTF(("pms: packet: 0x%02x%02x%02x%02x\n",
643 sc->packet[0], sc->packet[1], sc->packet[2],
644 sc->packet[3]));
645 }
646 #endif
647 if (dx || dy || dz || changed) {
648 #ifdef PMSDEBUG
649 DPRINTF(("pms: x %+03d y %+03d z %+03d "
650 "buttons 0x%02x\n", dx, dy, dz, sc->buttons));
651 #endif
652 wsmouse_input(sc->sc_wsmousedev,
653 sc->buttons, dx, dy, dz, 0,
654 WSMOUSE_INPUT_DELTA);
655 }
656 memset(sc->packet, 0, 4);
657 break;
658
659 /* If we get here, we have problems. */
660 default:
661 printf("pmsinput: very confused. resetting.\n");
662 sc->inputstate = 0;
663 sc->sc_enabled = 0;
664 wakeup(&sc->sc_enabled);
665 return;
666 }
667 }
668