-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathpsm.c
7647 lines (6831 loc) · 219 KB
/
psm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1992, 1993 Erik Forsberg.
* Copyright (c) 1996, 1997 Kazutaka YOKOTA.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Ported to 386bsd Oct 17, 1992
* Sandi Donno, Computer Science, University of Cape Town, South Africa
* Please send bug reports to sandi@cs.uct.ac.za
*
* Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
* although I was only partially successful in getting the alpha release
* of his "driver for the Logitech and ATI Inport Bus mice for use with
* 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
* found his code to be an invaluable reference when porting this driver
* to 386bsd.
*
* Further modifications for latest 386BSD+patchkit and port to NetBSD,
* Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
*
* Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
* Andrew Herbert - 12 June 1993
*
* Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
* - 13 June 1993
*
* Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
* - 24 October 1993
*
* Hardware access routines and probe logic rewritten by
* Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
* - 3, 14, 22 October 1996.
* - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
* - 14, 30 November 1996. Uses `kbdio.c'.
* - 13 December 1996. Uses queuing version of `kbdio.c'.
* - January/February 1997. Tweaked probe logic for
* HiNote UltraII/Latitude/Armada laptops.
* - 30 July 1997. Added APM support.
* - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
* Improved sync check logic.
* Vendor specific support routines.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_isa.h"
#include "opt_psm.h"
#include "opt_evdev.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/filio.h>
#include <sys/mutex.h>
#include <sys/poll.h>
#include <sys/sigio.h>
#include <sys/signalvar.h>
#include <sys/syslog.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <sys/selinfo.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/limits.h>
#include <sys/mouse.h>
#include <machine/resource.h>
#ifdef DEV_ISA
#include <isa/isavar.h>
#endif
#ifdef EVDEV_SUPPORT
#include <dev/evdev/evdev.h>
#include <dev/evdev/input.h>
#endif
#include <dev/atkbdc/atkbdcreg.h>
#include <dev/atkbdc/psm.h>
/*
* Driver specific options: the following options may be set by
* `options' statements in the kernel configuration file.
*/
/* debugging */
#ifndef PSM_DEBUG
#define PSM_DEBUG 0 /*
* logging: 0: none, 1: brief, 2: verbose
* 3: sync errors, 4: all packets
*/
#endif
#define VLOG(level, args) do { \
if (verbose >= level) \
log args; \
} while (0)
#ifndef PSM_INPUT_TIMEOUT
#define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
#endif
#ifndef PSM_TAP_TIMEOUT
#define PSM_TAP_TIMEOUT 125000
#endif
#ifndef PSM_TAP_THRESHOLD
#define PSM_TAP_THRESHOLD 25
#endif
/* end of driver specific options */
#define PSMCPNP_DRIVER_NAME "psmcpnp"
struct psmcpnp_softc {
enum {
PSMCPNP_GENERIC,
PSMCPNP_FORCEPAD,
PSMCPNP_TOPBUTTONPAD,
} type; /* Based on PnP ID */
};
/* input queue */
#define PSM_BUFSIZE 960
#define PSM_SMALLBUFSIZE 240
/* operation levels */
#define PSM_LEVEL_BASE 0
#define PSM_LEVEL_STANDARD 1
#define PSM_LEVEL_NATIVE 2
#define PSM_LEVEL_MIN PSM_LEVEL_BASE
#define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
/* Active PS/2 multiplexing */
#define PSM_NOMUX (-1)
/* Logitech PS2++ protocol */
#define MOUSE_PS2PLUS_CHECKBITS(b) \
((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
#define MOUSE_PS2PLUS_PACKET_TYPE(b) \
(((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
/* ring buffer */
typedef struct ringbuf {
int count; /* # of valid elements in the buffer */
int head; /* head pointer */
int tail; /* tail poiner */
u_char buf[PSM_BUFSIZE];
} ringbuf_t;
/* data buffer */
typedef struct packetbuf {
u_char ipacket[16]; /* interim input buffer */
int inputbytes; /* # of bytes in the input buffer */
} packetbuf_t;
#ifndef PSM_PACKETQUEUE
#define PSM_PACKETQUEUE 128
#endif
/*
* Synaptics command definitions.
*/
#define SYNAPTICS_READ_IDENTITY 0x00
#define SYNAPTICS_READ_MODES 0x01
#define SYNAPTICS_READ_CAPABILITIES 0x02
#define SYNAPTICS_READ_MODEL_ID 0x03
#define SYNAPTICS_READ_SERIAL_PREFIX 0x06
#define SYNAPTICS_READ_SERIAL_SUFFIX 0x07
#define SYNAPTICS_READ_RESOLUTIONS 0x08
#define SYNAPTICS_READ_EXTENDED 0x09
#define SYNAPTICS_READ_CAPABILITIES_CONT 0x0c
#define SYNAPTICS_READ_MAX_COORDS 0x0d
#define SYNAPTICS_READ_DELUXE_LED 0x0e
#define SYNAPTICS_READ_MIN_COORDS 0x0f
typedef struct synapticsinfo {
struct sysctl_ctx_list sysctl_ctx;
struct sysctl_oid *sysctl_tree;
int directional_scrolls;
int two_finger_scroll;
int min_pressure;
int max_pressure;
int max_width;
int margin_top;
int margin_right;
int margin_bottom;
int margin_left;
int na_top;
int na_right;
int na_bottom;
int na_left;
int window_min;
int window_max;
int multiplicator;
int weight_current;
int weight_previous;
int weight_previous_na;
int weight_len_squared;
int div_min;
int div_max;
int div_max_na;
int div_len;
int tap_max_delta;
int tap_min_queue;
int taphold_timeout;
int vscroll_ver_area;
int vscroll_hor_area;
int vscroll_min_delta;
int vscroll_div_min;
int vscroll_div_max;
int touchpad_off;
int softbuttons_y;
int softbutton2_x;
int softbutton3_x;
int max_x;
int max_y;
int three_finger_drag;
int natural_scroll;
} synapticsinfo_t;
typedef struct synapticspacket {
int x;
int y;
} synapticspacket_t;
#define SYNAPTICS_PACKETQUEUE 10
#define SYNAPTICS_QUEUE_CURSOR(x) \
(x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE
#define SYNAPTICS_VERSION_GE(synhw, major, minor) \
((synhw).infoMajor > (major) || \
((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor)))
typedef struct smoother {
synapticspacket_t queue[SYNAPTICS_PACKETQUEUE];
int queue_len;
int queue_cursor;
int start_x;
int start_y;
int avg_dx;
int avg_dy;
int squelch_x;
int squelch_y;
int is_fuzzy;
int active;
} smoother_t;
typedef struct gesture {
int window_min;
int fingers_nb;
int tap_button;
int in_taphold;
int in_vscroll;
int zmax; /* maximum pressure value */
struct timeval taptimeout; /* tap timeout for touchpads */
} gesture_t;
enum {
TRACKPOINT_SYSCTL_SENSITIVITY,
TRACKPOINT_SYSCTL_NEGATIVE_INERTIA,
TRACKPOINT_SYSCTL_UPPER_PLATEAU,
TRACKPOINT_SYSCTL_BACKUP_RANGE,
TRACKPOINT_SYSCTL_DRAG_HYSTERESIS,
TRACKPOINT_SYSCTL_MINIMUM_DRAG,
TRACKPOINT_SYSCTL_UP_THRESHOLD,
TRACKPOINT_SYSCTL_THRESHOLD,
TRACKPOINT_SYSCTL_JENKS_CURVATURE,
TRACKPOINT_SYSCTL_Z_TIME,
TRACKPOINT_SYSCTL_PRESS_TO_SELECT,
TRACKPOINT_SYSCTL_SKIP_BACKUPS
};
typedef struct trackpointinfo {
struct sysctl_ctx_list sysctl_ctx;
struct sysctl_oid *sysctl_tree;
int sensitivity;
int inertia;
int uplateau;
int reach;
int draghys;
int mindrag;
int upthresh;
int threshold;
int jenks;
int ztime;
int pts;
int skipback;
} trackpointinfo_t;
typedef struct finger {
int x;
int y;
int p;
int w;
int flags;
} finger_t;
#define PSM_FINGERS 2 /* # of processed fingers */
#define PSM_FINGER_IS_PEN (1<<0)
#define PSM_FINGER_FUZZY (1<<1)
#define PSM_FINGER_DEFAULT_P tap_threshold
#define PSM_FINGER_DEFAULT_W 1
#define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0)
#define PSM_FINGER_RESET(f) do { \
(f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \
} while (0)
typedef struct elantechhw {
int hwversion;
int fwversion;
int sizex;
int sizey;
int dpmmx;
int dpmmy;
int ntracesx;
int ntracesy;
int dptracex;
int dptracey;
int issemimt;
int isclickpad;
int hascrc;
int hastrackpoint;
int haspressure;
} elantechhw_t;
/* minimum versions supported by this driver */
#define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600)
#define ELANTECH_MAGIC(magic) \
((magic)[0] == 0x3c && (magic)[1] == 0x03 && \
((magic)[2] == 0xc8 || (magic)[2] == 0x00))
#define ELANTECH_FW_ID 0x00
#define ELANTECH_FW_VERSION 0x01
#define ELANTECH_CAPABILITIES 0x02
#define ELANTECH_SAMPLE 0x03
#define ELANTECH_RESOLUTION 0x04
#define ELANTECH_REG_READ 0x10
#define ELANTECH_REG_WRITE 0x11
#define ELANTECH_REG_RDWR 0x00
#define ELANTECH_CUSTOM_CMD 0xf8
#ifdef EVDEV_SUPPORT
#define ELANTECH_MAX_FINGERS 5
#else
#define ELANTECH_MAX_FINGERS PSM_FINGERS
#endif
#define ELANTECH_FINGER_MAX_P 255
#define ELANTECH_FINGER_MAX_W 15
#define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \
.x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \
.y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \
.p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \
.w = PSM_FINGER_DEFAULT_W, \
.flags = 0 \
}
enum {
ELANTECH_PKT_NOP,
ELANTECH_PKT_TRACKPOINT,
ELANTECH_PKT_V2_COMMON,
ELANTECH_PKT_V2_2FINGER,
ELANTECH_PKT_V3,
ELANTECH_PKT_V4_STATUS,
ELANTECH_PKT_V4_HEAD,
ELANTECH_PKT_V4_MOTION
};
#define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06)
#define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \
(pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \
(pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \
(pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \
(pb)->ipacket[5] == 0xff)
#define ELANTECH_PKT_IS_V2(pb) \
(((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02)
#define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \
((pb)->ipacket[3] & 0x09) == 0x08 : \
((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02)
#define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \
((pb)->ipacket[3] & 0x09) == 0x09 : \
((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c)
#define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \
((pb)->ipacket[3] & 0x08) == 0x00 : \
((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10)
typedef struct elantechaction {
finger_t fingers[ELANTECH_MAX_FINGERS];
int mask;
int mask_v4wait;
} elantechaction_t;
/* driver control block */
struct psm_softc { /* Driver status information */
int unit;
struct selinfo rsel; /* Process selecting for Input */
u_char state; /* Mouse driver state */
int config; /* driver configuration flags */
int flags; /* other flags */
KBDC kbdc; /* handle to access kbd controller */
struct resource *intr; /* IRQ resource */
void *ih; /* interrupt handle */
mousehw_t hw; /* hardware information */
synapticshw_t synhw; /* Synaptics hardware information */
synapticsinfo_t syninfo; /* Synaptics configuration */
smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */
gesture_t gesture; /* Gesture context */
elantechhw_t elanhw; /* Elantech hardware information */
elantechaction_t elanaction; /* Elantech action context */
int tphw; /* TrackPoint hardware information */
trackpointinfo_t tpinfo; /* TrackPoint configuration */
mousemode_t mode; /* operation mode */
mousemode_t dflt_mode; /* default operation mode */
mousestatus_t status; /* accumulated mouse movement */
ringbuf_t queue; /* mouse status queue */
packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */
int pqueue_start; /* start of data in queue */
int pqueue_end; /* end of data in queue */
int button; /* the latest button state */
int xold; /* previous absolute X position */
int yold; /* previous absolute Y position */
int xaverage; /* average X position */
int yaverage; /* average Y position */
int squelch; /* level to filter movement at low speed */
int syncerrors; /* # of bytes discarded to synchronize */
int pkterrors; /* # of packets failed during quaranteen. */
int fpcount; /* forcePad valid packet counter */
struct timeval inputtimeout;
struct timeval lastsoftintr; /* time of last soft interrupt */
struct timeval lastinputerr; /* time last sync error happened */
struct timeval idletimeout;
packetbuf_t idlepacket; /* packet to send after idle timeout */
int watchdog; /* watchdog timer flag */
struct callout callout; /* watchdog timer call out */
struct callout softcallout; /* buffer timer call out */
struct cdev *dev;
struct cdev *bdev;
int lasterr;
int cmdcount;
struct sigio *async; /* Processes waiting for SIGIO */
int extended_buttons;
int muxport; /* MUX port with attached Synaptics */
u_char muxsave[3]; /* 3->6 byte proto conversion buffer */
int muxtpbuttons; /* Touchpad button state */
int muxmsbuttons; /* Mouse (trackpoint) button state */
struct timeval muxmidtimeout; /* middle button supression timeout */
int muxsinglesyna; /* Probe result of single Synaptics */
#ifdef EVDEV_SUPPORT
struct evdev_dev *evdev_a; /* Absolute reporting device */
struct evdev_dev *evdev_r; /* Relative reporting device */
#endif
};
static devclass_t psm_devclass;
/* driver state flags (state) */
#define PSM_VALID 0x80
#define PSM_OPEN 1 /* Device is open */
#define PSM_ASLP 2 /* Waiting for mouse data */
#define PSM_SOFTARMED 4 /* Software interrupt armed */
#define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */
#define PSM_EV_OPEN_R 0x10 /* Relative evdev device is open */
#define PSM_EV_OPEN_A 0x20 /* Absolute evdev device is open */
/* driver configuration flags (config) */
#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
#define PSM_CONFIG_FLAGS \
(PSM_CONFIG_RESOLUTION | \
PSM_CONFIG_ACCEL | \
PSM_CONFIG_NOCHECKSYNC | \
PSM_CONFIG_NOIDPROBE | \
PSM_CONFIG_NORESET | \
PSM_CONFIG_FORCETAP | \
PSM_CONFIG_IGNPORTERROR | \
PSM_CONFIG_HOOKRESUME | \
PSM_CONFIG_INITAFTERSUSPEND)
/* other flags (flags) */
#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
#define kbdcp(p) ((atkbdc_softc_t *)(p))
#define ALWAYS_RESTORE_CONTROLLER(kbdc) !(kbdcp(kbdc)->quirks \
& KBDC_QUIRK_KEEP_ACTIVATED)
/* Tunables */
static int tap_enabled = -1;
static int verbose = PSM_DEBUG;
static int synaptics_support = 1;
static int trackpoint_support = 1;
static int elantech_support = 1;
static int mux_disabled = -1;
/* for backward compatibility */
#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
typedef struct old_mousehw {
int buttons;
int iftype;
int type;
int hwid;
} old_mousehw_t;
typedef struct old_mousemode {
int protocol;
int rate;
int resolution;
int accelfactor;
} old_mousemode_t;
#define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field)
enum {
SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure),
SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure),
SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width),
SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top),
SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right),
SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom),
SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left),
SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top),
SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right),
SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom),
SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left),
SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min),
SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max),
SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator),
SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current),
SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous),
SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na),
SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared),
SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min),
SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max),
SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na),
SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len),
SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta),
SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue),
SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout),
SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area),
SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area),
SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta),
SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min),
SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max),
SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off),
SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y),
SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x),
SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x),
SYNAPTICS_SYSCTL_THREE_FINGER_DRAG = SYN_OFFSET(three_finger_drag),
SYNAPTICS_SYSCTL_NATURAL_SCROLL = SYN_OFFSET(natural_scroll),
#define SYNAPTICS_SYSCTL_LAST SYNAPTICS_SYSCTL_NATURAL_SCROLL
};
/* packet formatting function */
typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int,
mousestatus_t *);
/* function prototypes */
static void psmidentify(driver_t *, device_t);
static int psmprobe(device_t);
static int psmattach(device_t);
static int psmdetach(device_t);
static int psmresume(device_t);
static d_open_t psm_cdev_open;
static d_close_t psm_cdev_close;
static d_read_t psmread;
static d_write_t psmwrite;
static d_ioctl_t psmioctl;
static d_poll_t psmpoll;
static int psmopen(struct psm_softc *);
static int psmclose(struct psm_softc *);
#ifdef EVDEV_SUPPORT
static evdev_open_t psm_ev_open_r;
static evdev_close_t psm_ev_close_r;
static evdev_open_t psm_ev_open_a;
static evdev_close_t psm_ev_close_a;
#endif
static int enable_aux_dev(KBDC);
static int disable_aux_dev(KBDC);
static int get_mouse_status(KBDC, int *, int, int);
static int get_aux_id(KBDC);
static int set_mouse_sampling_rate(KBDC, int);
static int set_mouse_scaling(KBDC, int);
static int set_mouse_resolution(KBDC, int);
static int set_mouse_mode(KBDC);
static int get_mouse_buttons(KBDC);
static int is_a_mouse(int);
static void recover_from_error(KBDC);
static int restore_controller(KBDC, int);
static int doinitialize(struct psm_softc *, mousemode_t *);
static int doopen(struct psm_softc *, int);
static int reinitialize(struct psm_softc *, int);
static char *model_name(int);
static void psmsoftintr(void *);
static void psmsoftintridle(void *);
static void psmintr(void *);
static void psmtimeout(void *);
static int timeelapsed(const struct timeval *, int, int,
const struct timeval *);
static void dropqueue(struct psm_softc *);
static void flushpackets(struct psm_softc *);
static void proc_mmanplus(struct psm_softc *, packetbuf_t *,
mousestatus_t *, int *, int *, int *);
static int proc_synaptics(struct psm_softc *, packetbuf_t *,
mousestatus_t *, int *, int *, int *);
static int proc_synaptics_mux(struct psm_softc *, packetbuf_t *);
static void proc_versapad(struct psm_softc *, packetbuf_t *,
mousestatus_t *, int *, int *, int *);
static int proc_elantech(struct psm_softc *, packetbuf_t *,
mousestatus_t *, int *, int *, int *);
static int psmpalmdetect(struct psm_softc *, finger_t *, int);
static void psmgestures(struct psm_softc *, finger_t *, int,
mousestatus_t *);
static void psmsmoother(struct psm_softc *, finger_t *, int,
mousestatus_t *, int *, int *);
static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *,
u_char *);
/* vendor specific features */
enum probearg { PROBE, REINIT };
typedef int probefunc_t(struct psm_softc *, enum probearg);
static int mouse_id_proc1(KBDC, int, int, int *);
static int mouse_ext_command(KBDC, int);
static probefunc_t enable_groller;
static probefunc_t enable_gmouse;
static probefunc_t enable_aglide;
static probefunc_t enable_kmouse;
static probefunc_t enable_msexplorer;
static probefunc_t enable_msintelli;
static probefunc_t enable_4dmouse;
static probefunc_t enable_4dplus;
static probefunc_t enable_mmanplus;
static probefunc_t enable_synaptics;
static probefunc_t enable_synaptics_mux;
static probefunc_t enable_single_synaptics_mux;
static probefunc_t enable_trackpoint;
static probefunc_t enable_versapad;
static probefunc_t enable_elantech;
static void set_trackpoint_parameters(struct psm_softc *sc);
static void synaptics_passthrough_on(struct psm_softc *sc);
static void synaptics_passthrough_off(struct psm_softc *sc);
static int synaptics_preferred_mode(struct psm_softc *sc);
static void synaptics_set_mode(struct psm_softc *sc, int mode_byte);
static struct {
int model;
u_char syncmask;
int packetsize;
probefunc_t *probefunc;
} vendortype[] = {
/*
* WARNING: the order of probe is very important. Don't mess it
* unless you know what you are doing.
*/
{ MOUSE_MODEL_SYNAPTICS, /* Synaptics + mouse on Active Mux */
0x00, MOUSE_PS2_PACKETSIZE, enable_synaptics_mux },
{ MOUSE_MODEL_SYNAPTICS, /* Single Synaptics on Active Mux */
0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_single_synaptics_mux },
{ MOUSE_MODEL_NET, /* Genius NetMouse */
0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse },
{ MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
0xc8, 6, enable_groller },
{ MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus },
{ MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer },
{ MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse },
{ MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus },
{ MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */
0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics },
{ MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */
0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech },
{ MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli },
{ MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide },
{ MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */
0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse },
{ MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad },
{ MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */
0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint },
{ MOUSE_MODEL_GENERIC,
0xc0, MOUSE_PS2_PACKETSIZE, NULL },
};
#define GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1)
/* device driver declarateion */
static device_method_t psm_methods[] = {
/* Device interface */
DEVMETHOD(device_identify, psmidentify),
DEVMETHOD(device_probe, psmprobe),
DEVMETHOD(device_attach, psmattach),
DEVMETHOD(device_detach, psmdetach),
DEVMETHOD(device_resume, psmresume),
{ 0, 0 }
};
static driver_t psm_driver = {
PSM_DRIVER_NAME,
psm_methods,
sizeof(struct psm_softc),
};
static struct cdevsw psm_cdevsw = {
.d_version = D_VERSION,
.d_flags = D_NEEDGIANT,
.d_open = psm_cdev_open,
.d_close = psm_cdev_close,
.d_read = psmread,
.d_write = psmwrite,
.d_ioctl = psmioctl,
.d_poll = psmpoll,
.d_name = PSM_DRIVER_NAME,
};
#ifdef EVDEV_SUPPORT
static const struct evdev_methods psm_ev_methods_r = {
.ev_open = psm_ev_open_r,
.ev_close = psm_ev_close_r,
};
static const struct evdev_methods psm_ev_methods_a = {
.ev_open = psm_ev_open_a,
.ev_close = psm_ev_close_a,
};
#endif
/* device I/O routines */
static int
enable_aux_dev(KBDC kbdc)
{
int res;
res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res));
return (res == PSM_ACK);
}
static int
disable_aux_dev(KBDC kbdc)
{
int res;
res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res));
return (res == PSM_ACK);
}
static int
get_mouse_status(KBDC kbdc, int *status, int flag, int len)
{
int cmd;
int res;
int i;
switch (flag) {
case 0:
default:
cmd = PSMC_SEND_DEV_STATUS;
break;
case 1:
cmd = PSMC_SEND_DEV_DATA;
break;
}
empty_aux_buffer(kbdc, 5);
res = send_aux_command(kbdc, cmd);
VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
(flag == 1) ? "DATA" : "STATUS", res));
if (res != PSM_ACK)
return (0);
for (i = 0; i < len; ++i) {
status[i] = read_aux_data(kbdc);
if (status[i] < 0)
break;
}
if (len >= 3) {
for (; i < 3; ++i)
status[i] = 0;
VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n",
(flag == 1) ? "data" : "status", status[0], status[1], status[2]));
}
return (i);
}
static int
get_aux_id(KBDC kbdc)
{
int res;
int id;
empty_aux_buffer(kbdc, 5);
res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res));
if (res != PSM_ACK)
return (-1);
/* 10ms delay */
DELAY(10000);
id = read_aux_data(kbdc);
VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id));
return (id);
}
static int
set_mouse_sampling_rate(KBDC kbdc, int rate)
{
int res;
res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res));
return ((res == PSM_ACK) ? rate : -1);
}
static int
set_mouse_scaling(KBDC kbdc, int scale)
{
int res;
switch (scale) {
case 1:
default:
scale = PSMC_SET_SCALING11;
break;
case 2:
scale = PSMC_SET_SCALING21;
break;
}
res = send_aux_command(kbdc, scale);
VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
(scale == PSMC_SET_SCALING21) ? "21" : "11", res));
return (res == PSM_ACK);
}
/* `val' must be 0 through PSMD_MAX_RESOLUTION */
static int
set_mouse_resolution(KBDC kbdc, int val)
{
int res;
res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res));
return ((res == PSM_ACK) ? val : -1);
}
/*
* NOTE: once `set_mouse_mode()' is called, the mouse device must be
* re-enabled by calling `enable_aux_dev()'
*/
static int
set_mouse_mode(KBDC kbdc)
{
int res;
res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res));
return (res == PSM_ACK);
}
static int
get_mouse_buttons(KBDC kbdc)
{
int c = 2; /* assume two buttons by default */
int status[3];
/*
* NOTE: a special sequence to obtain Logitech Mouse specific
* information: set resolution to 25 ppi, set scaling to 1:1, set
* scaling to 1:1, set scaling to 1:1. Then the second byte of the
* mouse status bytes is the number of available buttons.
* Some manufactures also support this sequence.
*/
if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
return (c);
if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) &&
set_mouse_scaling(kbdc, 1) &&
get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0)
return (status[1]);
return (c);
}
/* misc subroutines */
/*
* Someday, I will get the complete list of valid pointing devices and
* their IDs... XXX
*/
static int
is_a_mouse(int id)
{
#if 0
static int valid_ids[] = {
PSM_MOUSE_ID, /* mouse */
PSM_BALLPOINT_ID, /* ballpoint device */
PSM_INTELLI_ID, /* Intellimouse */
PSM_EXPLORER_ID, /* Intellimouse Explorer */
-1 /* end of table */
};
int i;
for (i = 0; valid_ids[i] >= 0; ++i)
if (valid_ids[i] == id)
return (TRUE);
return (FALSE);
#else
return (TRUE);
#endif
}
static char *
model_name(int model)
{
static struct {
int model_code;
char *model_name;
} models[] = {
{ MOUSE_MODEL_NETSCROLL, "NetScroll" },
{ MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
{ MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
{ MOUSE_MODEL_THINK, "ThinkingMouse" },
{ MOUSE_MODEL_INTELLI, "IntelliMouse" },
{ MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
{ MOUSE_MODEL_VERSAPAD, "VersaPad" },
{ MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
{ MOUSE_MODEL_4D, "4D Mouse" },
{ MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
{ MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" },
{ MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" },
{ MOUSE_MODEL_ELANTECH, "Elantech Touchpad" },
{ MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
{ MOUSE_MODEL_UNKNOWN, "Unknown" },
};
int i;
for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i)
if (models[i].model_code == model)
break;
return (models[i].model_name);
}
static void
recover_from_error(KBDC kbdc)
{
/* discard anything left in the output buffer */
empty_both_buffers(kbdc, 10);
#if 0
/*
* NOTE: KBDC_RESET_KBD may not restore the communication between the
* keyboard and the controller.
*/
reset_kbd(kbdc);
#else
/*