/
VoodooPS2CypressTouchPad.cpp
1448 lines (1288 loc) · 48 KB
/
VoodooPS2CypressTouchPad.cpp
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) 2002 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.2 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*
* =====================================================================
* This file as been created/added by Ulysse31, in order to implement the
* Cypress PS2 Trackpad protocol/support.
* Some basic functions as been ported/adapted from linux, but the processing
* could not be done the same way ...
* Thanks to Dudley Du from cypress for his short, but enought doc sheet
* Ulysse31 aka Nix
* ulysse31<at>gmail<dot>com
*/
#define UNDOCUMENTED_INIT_SEQUENCE_POST
#include <IOKit/IOLib.h>
#include <IOKit/hidsystem/IOHIDParameter.h>
#include "VoodooPS2Controller.h"
#include "VoodooPS2CypressTouchPad.h"
enum {
//
//
kTapEnabled = 0x01
};
// =============================================================================
// kalFilter: kalman filtering for mouse coordinates (three fingers smoothing)
//
kalFilter::kalFilter(int noiseLevel)
{
_it = 0;
_xk[KFLAST] = 0;
_xk[KFCURRENT] = 0;
_pk[KFLAST] = 0;
_pk[KFCURRENT] = 0;
_k = 0;
_zk = 0;
_r = noiseLevel;
_r /= 1000.0f;
}
int
kalFilter::getNewValue(int rawval)
{
int result;
if (_it == 0)
{
_xk[KFLAST] = 0;
_pk[KFLAST] = 1;
}
// TIME UPDATE
_xk[KFCURRENT] = _xk[KFLAST];
_pk[KFCURRENT] = _pk[KFLAST];
_zk = rawval;
_zk /= 1000.0f; // ratio to 0.001
// MEASUREMENT UPDATE
_k = (float)((float)_pk[KFLAST] / (float)(_pk[KFLAST] + _r));
_xk[KFCURRENT] = _xk[KFLAST] + (_k * (_zk - _xk[KFLAST]));
_pk[KFCURRENT] = _pk[KFLAST]*(1 - _k);
// PREPARE NEXT ROUND
result = (int)KFCEIL((float)(_xk[KFCURRENT] * 1000.0f));
_xk[KFLAST] = _xk[KFCURRENT];
_pk[KFLAST] = _pk[KFCURRENT];
_it++;
return (result);
}
void
kalFilter::resetFilter()
{
_it = 0;
_xk[KFLAST] = 0;
_xk[KFCURRENT] = 0;
_pk[KFLAST] = 0;
_pk[KFCURRENT] = 0;
_k = 0;
_zk = 0;
}
int
kalFilter::noiseLevel(int noiseLevel)
{
if (noiseLevel)
{
_r = noiseLevel;
_r = (float)( _r / 1000.0f);
DEBUG_LOG("CYPRESS: kalFilter: set noise level to %lu\n", (unsigned long)(_r * 1000.0f));
}
return ((int)(_r * 1000.0f));
}
// =============================================================================
// cypressFrame: coord analysis Class implementation
//
#define MAX_REPORTS 4
#define abs(x) ((x) < 0 ? -(x) : (x))
cypressFrame::cypressFrame(int max)
{
_maxElems = max;
_list = 0;
_end = 0;
_elems = 0;
}
void cypressFrame::addReport(t_reportData *p)
{
// int k;
// t_reportList *l;
if (_elems == 0)
{
}
}
// =============================================================================
// ApplePS2CypressTouchPad Class Implementation
//
OSDefineMetaClassAndStructors(ApplePS2CypressTouchPad, IOHIPointing);
UInt32 ApplePS2CypressTouchPad::deviceType()
{ return NX_EVS_DEVICE_TYPE_MOUSE; };
UInt32 ApplePS2CypressTouchPad::interfaceID()
{ return NX_EVS_DEVICE_INTERFACE_BUS_ACE; };
IOItemCount ApplePS2CypressTouchPad::buttonCount() { return 2; };
IOFixed ApplePS2CypressTouchPad::resolution() { return _resolution; };
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ApplePS2CypressTouchPad::init(OSDictionary * dict)
{
//
// Initialize this object's minimal state. This is invoked right after this
// object is instantiated.
//
if (!super::init(dict))
return false;
// find config specific to Platform Profile
OSDictionary* list = OSDynamicCast(OSDictionary, dict->getObject(kPlatformProfile));
// initialize state/default settings...
_device = 0;
_interruptHandlerInstalled = false;
_packetByteCount = 0;
_resolution = (100) << 16; // (100 dpi, 4 counts/mm)
_xpos = -1;
_ypos = -1;
_x4pos = -1;
_y4pos = -1;
_xscrollpos = -1;
_yscrollpos = -1;
_pendingButtons = 0;
_frameCounter = 0;
_frameType = -1;
_framePressure = 0;
_swipey = 0;
_swipex = 0;
_swiped = false;
_tapFrameMax = 5;
_lockFrameMin = 40;
_swipexThreshold = 30;
_swipeyThreshold = 30;
_twoFingersMaxCount = 2;
_threeFingersMaxCount = 2;
_fourFingersMaxCount = 3;
_onefingervdivider = 2;
_onefingerhdivider = 2;
_twoFingerDivider = 65536;
_twofingervdivider = 2;
_twofingerhdivider = 2;
_threefingervdivider = 1;
_threefingerhdivider = 1;
_onefingermaxtaptime = 200000000;
_twofingermaxtaptime = 200000000;
_threefingermaxtaptime = 200000000;
_fourfingermaxtaptime = 50000000;
_fivefingermaxtaptime = 200000000;
_fivefingerscreenlocktime = 800000000;
_fivefingersleeptime = 3000000000;
_dragPressureAverage = 85;
// _onefingervdivider = 1;
// _onefingerhdivider = 1;
// _twofingervdivider = 1;
// _twofingerhdivider = 1;
// _threefingervdivider = 1;
// _threefingerhdivider = 1;
_activeDragLock = false;
_lastOneTap = 0;
_oneTapCounter = 0;
_frameTimer = 0;
_slept = false;
_cytp_resolution[0] = 0x00;
_cytp_resolution[1] = 0x01;
_cytp_resolution[2] = 0x02;
_cytp_resolution[3] = 0x03;
_cytp_rate[0] = 10;
_cytp_rate[1] = 20;
_cytp_rate[2] = 40;
_cytp_rate[3] = 60;
_cytp_rate[4] = 100;
_cytp_rate[5] = 200;
_wakeDelay = 350;
// Default Props
_clicking = true;
_twoFingerRightClick = true;
_threeFingerDrag = true;
_fourFingerHorizSwipeGesture = true;
_fourFingerVertSwipeGesture = true;
_fiveFingerScreenLock = true;
_fiveFingerSleep = true;
_pressureFiltering = 20;
_twoFingerFiltering = 1;
_threeFingerFiltering = 20;
_fourFingerFiltering = 20;
_packetLength = 8; // default len
_tpMode = 0;
_tpWidth = CYTP_DEFAULT_WIDTH;
_tpHigh = CYTP_DEFAULT_HIGH;
_tpMaxAbsX = CYTP_ABS_MAX_X;
_tpMaxAbsY = CYTP_ABS_MAX_Y;
_tpMinPressure = CYTP_MIN_PRESSURE;
_tpMaxPressure = CYTP_MAX_PRESSURE;
_tpResX = _tpMaxAbsY / _tpWidth;
_tpResY = _tpMaxAbsY / _tpHigh;
// this->setParamProperties(config);
// OSSafeRelease(config);
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ApplePS2CypressTouchPad* ApplePS2CypressTouchPad::probe( IOService * provider, SInt32 * score )
{
DEBUG_LOG("ApplePS2CypressTouchPad::probe entered...\n");
//
// The driver has been instructed to verify the presence of the actual
// hardware we represent. We are guaranteed by the controller that the
// mouse clock is enabled and the mouse itself is disabled (thus it
// won't send any asynchronous mouse data that may mess up the
// responses expected by the commands we send it).
//
bool success = false;
if (!super::probe(provider, score))
return 0;
OSDictionary* list = OSDynamicCast(OSDictionary, getProperty(kPlatformProfile));
ApplePS2Device* d = (ApplePS2Device*)provider;
OSDictionary* config = d->getController()->makeConfigurationNode(list, "Cypress Touchpad"); //ApplePS2Controller::makeConfigurationNode(list);
if (config)
{
// if DisableDevice is Yes, then do not load at all...
OSBoolean* disable = OSDynamicCast(OSBoolean, config->getObject(kDisableDevice));
if (disable && disable->isTrue())
{
config->release();
return false;
}
#ifdef DEBUG
// save configuration for later/diagnostics...
setProperty(kMergedConfiguration, config);
#endif
}
_device = (ApplePS2MouseDevice *) d;
cypressReset();
success = cypressReadFwVersion();
if (success &&_touchPadVersion > 11)
{
_tapFrameMax = 11;
_lockFrameMin = 80; // sync packet rate is superior on > 11 firmware ... should be good ... (not tested) ...
}
_device = 0;
DEBUG_LOG("CYPRESS: ApplePS2CypressTouchPad::probe leaving.\n");
return (success) ? this : 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ApplePS2CypressTouchPad::start( IOService * provider )
{
UInt64 enabledProperty;
UInt64 disabledProperty;
//
// The driver has been instructed to start. This is called after a
// successful probe and match.
//
if (!super::start(provider))
return false;
//
// Maintain a pointer to and retain the provider object.
//
_device = (ApplePS2MouseDevice *) provider;
_device->retain();
//
// Announce hardware properties.
//
IOLog("CYPRESS: ApplePS2Trackpad: Cypress Trackpad firmware v%d\n", (UInt8)(_touchPadVersion));
//
// Advertise some supported features (tapping, edge scrolling).
//
// enabledProperty = 1;
// disabledProperty = 0;
// setProperty("Clicking", enabledProperty,
// sizeof(enabledProperty) * 8);
// setProperty("DragLock", disabledProperty,
// sizeof(disabledProperty) * 8);
// setProperty("Dragging", disabledProperty,
// sizeof(disabledProperty) * 8);
// setProperty("TrackpadScroll", enabledProperty,
// sizeof(enabledProperty) * 8);
// setProperty("TrackpadHorizScroll", enabledProperty,
// sizeof(enabledProperty) * 8);
// setProperty("CypressFourFingerHorizSwipeGesture", ( _fourFingerHorizSwipeGesture ? enabledProperty : disabledProperty),
// sizeof(enabledProperty) * 8);
// setProperty("CypressFourFingerVertSwipeGesture", ( _fourFingerVertSwipeGesture ? enabledProperty : disabledProperty),
// sizeof(enabledProperty) * 8);
// setProperty("CypressFiveFingerScreenLock", ( _fiveFingerScreenLock ? enabledProperty : disabledProperty),
// sizeof(enabledProperty) * 8);
// setProperty("CypressFiveFingerSleep", (_fiveFingerSleep ? enabledProperty : disabledProperty),
// sizeof(enabledProperty) * 8);
// setProperty("CypressThreeFingerDrag", (_threeFingerDrag ? enabledProperty : disabledProperty),
// sizeof(enabledProperty) * 8);
// float i = 200;
// setProperty("1FingersMaxTapTime", i, sizeof(i) * 8);
// setProperty("2FingersMaxTapTime", i, sizeof(i) * 8);
// setProperty("3FingersMaxTapTime", i, sizeof(i) * 8);
//
// Must add this property to let our superclass know that it should handle
// trackpad acceleration settings from user space. Without this, tracking
// speed adjustments from the mouse prefs panel have no effect.
//
setProperty(kIOHIDPointerAccelerationTypeKey, kIOHIDTrackpadAccelerationType);
setProperty(kIOHIDScrollAccelerationTypeKey, kIOHIDTrackpadScrollAccelerationKey);
//
// Lock the controller during initialization
//
_device->lock();
if (_touchPadVersion > 11)
_tapFrameMax = 11;
setTouchpadModeByte();
_device->installInterruptAction(this,
OSMemberFunctionCast(PS2InterruptAction, this, &ApplePS2CypressTouchPad::interruptOccurred),
OSMemberFunctionCast(PS2PacketAction, this, &ApplePS2CypressTouchPad::packetReady));
_interruptHandlerInstalled = true;
// now safe to allow other threads
_device->unlock();
//
// Install our power control handler.
//
_device->installPowerControlAction( this, OSMemberFunctionCast(PS2PowerControlAction,this,
&ApplePS2CypressTouchPad::setDevicePowerState) );
_powerControlHandlerInstalled = true;
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ApplePS2CypressTouchPad::stop( IOService * provider )
{
//
// The driver has been instructed to stop. Note that we must break all
// connections to other service objects now (ie. no registered actions,
// no pointers and retains to objects, etc), if any.
//
assert(_device == provider);
//
// Disable the mouse itself, so that it may stop reporting mouse events.
//
setTouchPadEnable(false);
//
// Uninstall the interrupt handler.
//
if ( _interruptHandlerInstalled ) _device->uninstallInterruptAction();
_interruptHandlerInstalled = false;
//
// Uninstall the power control handler.
//
if ( _powerControlHandlerInstalled ) _device->uninstallPowerControlAction();
_powerControlHandlerInstalled = false;
//
// Release the pointer to the provider object.
//
OSSafeReleaseNULL(_device);
super::stop(provider);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PS2InterruptResult ApplePS2CypressTouchPad::interruptOccurred(UInt8 data)
{
int index = _packetByteCount;
UInt8* packet = _ringBuffer.head();
packet[_packetByteCount++] = data;
if (index == 0)
this->updatePacketSize(packet[0]);
//DEBUG_LOG("CYPRESS: interruptOccured: rec 0x%02x byte number %d of %d (queued %d)\n", data, _packetByteCount - 1, this->packetSize(), _ringBuffer.count());
if (this->packetSize() == _packetByteCount)
{
if (this->packetSize() == kPacketLengthSmall)
{
packet[_packetByteCount++] = 0;
packet[_packetByteCount++] = 0;
packet[_packetByteCount++] = 0;
}
_ringBuffer.advanceHead(kPacketLengthLarge);
_packetByteCount = 0;
return kPS2IR_packetReady;
}
return kPS2IR_packetBuffering;
}
void ApplePS2CypressTouchPad::cypressSimulateEvent(char button)
{
uint64_t now_abs;
clock_get_uptime(&now_abs);
dispatchRelativePointerEventX(0, 0, button, now_abs);
if (_activeDragLock)
return ;
clock_get_uptime(&now_abs);
dispatchRelativePointerEventX(0, 0, 0x00, now_abs);
}
void ApplePS2CypressTouchPad::cypressSimulateLastEvents()
{
uint64_t now_abs;
clock_get_uptime(&now_abs);
#ifdef DEBUG
if (_frameType >= 0)
DEBUG_LOG("CYPRESS: _frameType = %d, _frameCounter = %d, _frameTimer %llu, diff %llu\n", _frameType, _frameCounter, _frameTimer, now_abs - _frameTimer);
#endif
if (_clicking && _frameType == 1 && _frameCounter > 0 && ((now_abs - _frameTimer) < _onefingermaxtaptime)) // 200 ms, all value less than that should be considered as a tap
{
// simulate a tap here
if ((now_abs - _lastOneTap) < 250000000) // 250ms for triple tap drag locking
_oneTapCounter++;
else
{
_oneTapCounter = 0;
_activeDragLock = false;
}
if (_dragLock && _oneTapCounter >= 2)
_activeDragLock = true;
this->cypressSimulateEvent(0x01);
DEBUG_LOG("CYPRESS: one finger tap detected (_oneTapCounter = %d, _lastOneTap = %llu, diff = %llu)\n", _oneTapCounter, _lastOneTap, now_abs - _lastOneTap);
_lastOneTap = now_abs;
}
if (_twoFingerRightClick && _frameType == 2 && _frameCounter > 0 && ((now_abs - _frameTimer) < _twofingermaxtaptime)) // 200 ms, all value less than that should be considered as a tap
{
// simulate a tap right click here
this->cypressSimulateEvent(0x02);
DEBUG_LOG("CYPRESS: two fingers tap detected\n");
}
if (_frameType > 1)
{
_kalX.resetFilter();
_kalY.resetFilter();
}
_kalZ.resetFilter();
// if (_frameType == 3 && _frameCounter > 0 && ((now_abs - _frameTimer) < _threefingermaxtaptime)) // 200 ms, all value less than that should be considered as a tap
// {
// // simulate a tap here
// this->cypressSimulateEvent(0x01);
// DEBUG_LOG("CYPRESS: three fingers tap detected\n");
// }
// Finally thinking that it may not be a good idea to implement a third action on 5 fingers ... may be confusing use between screenlock and show desktop ...
// More generally, adding more than 2 time based actions on the same finger count may be a bad idea ... so commenting out this code
// if (_fiveFingerShowDesktop && _frameType == 5 && (now_abs - _frameTimer) < _fivefingermaxtaptime) // 100ms
// {
// // Show Desktop here
// _device->dispatchKeyboardMessage(kPS2M_showDesktop, &now_abs);
// DEBUG_LOG("CYPRESS: 5 fingers short frame (tap) detected (size=%d), Showing Desktop\n", _frameCounter);
// }
if (_fiveFingerScreenLock && _frameType == 5 && (now_abs - _frameTimer) > _fivefingerscreenlocktime && _slept == false) // 800ms, a bit less than 1 sec
{
// Lock Screen here
_device->dispatchKeyboardMessage(kPS2M_screenLock, &now_abs);
DEBUG_LOG("CYPRESS: 5 fingers long frame detected (size=%d), locking screen\n", _frameCounter);
}
}
void ApplePS2CypressTouchPad::cypressResetCounters()
{
_xpos = -1;
_ypos = -1;
_x4pos = -1;
_y4pos = -1;
_xscrollpos = -1;
_yscrollpos = -1;
_swipey = 0;
_swipex = 0;
_swiped = false;
_slept = false;
_frameType = -1;
_frameCounter = 0;
_frameTimer = 0;
_framePressure = 0;
}
bool ApplePS2CypressTouchPad::cypressCheckPacketEndFrame(UInt8 *packet)
{
if (packet[0] == 0x00 && packet[1] == 0x00 && packet[2] == 0x00 && packet[3] == 0x00
&& packet[4] == 0x00 && packet[5] == 0x00 && packet[6] == 0x00 && packet[7] == 0x00)
{
// Empty packet, received lots (500 bytes) when an action ends (finger leave)
_ringBuffer.advanceTail(kPacketLengthLarge);
this->cypressSimulateLastEvents();
this->cypressResetCounters();
if (_pendingButtons && packet[0] == 0 && packet[1] == 0 && packet[2] == 0 && packet[3] == 0
&& packet[4] == 0 && packet[5] == 0 && packet[6] == 0 && packet[7] == 0) // buttons were used need to clear events
{
this->cypressProcessPacket(packet);
_pendingButtons = 0;
}
return true;
}
return false;
}
bool ApplePS2CypressTouchPad::cypressCheckPacketValidity(UInt8 *packet)
{
UInt8 header;
UInt8 fingers;
fingers = fingersCount(packet[0]);
header = packet[0];
if (((((header & 0x40) != 0x40) && ((header & 0x80) != 0x80) && ((header & 0x20) != 0x20) && (header > 2))) || ((header & 0xe0) == 0xe0 || (header & 0xf0) == 0xf0) // palm detect packet
|| ((header & 0x08) == 0x08 && fingers < 0) || ((header & 0x01) == 0x01 && (header & 0x02) == 0x02) || ((fingers == 0 || fingers > 2) && ((header & 0x03) == 0x03)) // trying to match de-sync with tap bit
|| (packet[0] == 0 && (packet[1] != 0x00 || packet[2] != 0x00 || packet[3] != 0x00 || packet[4] != 0x00 || packet[5] != 0x00 || packet[6] != 0x00 || packet[7] != 0x00) ))
{
// incomplete packet : invalid header, de-sync'ed packets (start and ends in middle of 2 stored packets), etc ... => drop queue and ask re-send
// Thanks to dudley at cypress for is (short but enought) doc sheet
this->cypressResetCounters();
_packetByteCount = 0;
_pendingButtons = 0;
_ringBuffer.advanceTail(_ringBuffer.count());
if (!((header & 0xe0) == 0xe0 || (header & 0xf0) == 0xf0)) // only ask resend if packet is not palm detect
cypressSendByte(0xFE);
return (false);
}
return (true);
}
void ApplePS2CypressTouchPad::packetReady()
{
while (_ringBuffer.count() >= kPacketLengthLarge)
{
UInt8 *packet = _ringBuffer.tail();
this->updatePacketSize(packet[0]);
UInt8 size = packetSize();
if (_ringBuffer.count() < size)
return ;
// should be deleted once communication with PS2 cypress trackpad stable
// v34 and later seems to send 0x04 header with 4 fingers touch (??), so lets make a little trick ...
if (_touchPadVersion >= 34 && (packet[0] == 0x04))
packet[0] = 0x20; // 4 fingers should be 0x20 ...
if (_touchPadVersion >= 34 && (packet[0] == 0x44))
packet[0] = 0xa0; // 5 fingers should be 0xa0 ...
#ifdef DEBUG
if (packet[0] || packet[1] || packet[2] || packet[3] || packet[4] || packet[5] || packet[6] || packet[7])
DEBUG_LOG("CYPRESS: %s: packet dump { 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x }\n", getName(), packet[0], packet[1], packet[2], packet[3], packet[4], packet[5], packet[6], packet[7]);
#endif
if (this->cypressCheckPacketEndFrame(packet))
continue ;
if (this->cypressCheckPacketValidity(packet) == false)
return ;
this->cypressProcessPacket(packet);
_ringBuffer.advanceTail(kPacketLengthLarge);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ApplePS2CypressTouchPad::setTouchPadEnable( bool enable )
{
//
// Instructs the trackpad to start or stop the reporting of data packets.
// It is safe to issue this request from the interrupt/completion context.
//
// (mouse enable/disable command)
cypressSendByte(enable ? kDP_Enable : kDP_SetDefaultsAndDisable);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IOReturn ApplePS2CypressTouchPad::setParamProperties( OSDictionary * dict )
{
if (dict == 0)
return super::setParamProperties(dict);
OSNumber * clicking = OSDynamicCast( OSNumber, dict->getObject("Clicking") );
OSNumber * dragging = OSDynamicCast( OSNumber, dict->getObject("Dragging") );
OSNumber * draglock = OSDynamicCast( OSNumber, dict->getObject("DragLock") );
OSNumber * hscroll = OSDynamicCast( OSNumber, dict->getObject("TrackpadHorizScroll") );
OSNumber * vscroll = OSDynamicCast( OSNumber, dict->getObject("TrackpadScroll") );
OSNumber * scrollspeed = OSDynamicCast( OSNumber, dict->getObject("HIDTrackpadScrollAcceleration") );
OSNumber * onefingermaxtaptime = OSDynamicCast( OSNumber, dict->getObject("Cypress1FingerMaxTapTime") );
OSNumber * twofingermaxtaptime = OSDynamicCast( OSNumber, dict->getObject("Cypress2FingerMaxTapTime") );
OSNumber * threefingermaxtaptime = OSDynamicCast( OSNumber, dict->getObject("Cypress3FingerMaxTapTime") );
OSNumber * fourfingermaxtaptime = OSDynamicCast( OSNumber, dict->getObject("Cypress4FingerMaxTapTime") );
OSNumber * fivefingermaxtaptime = OSDynamicCast( OSNumber, dict->getObject("Cypress5FingerMaxTapTime") );
OSNumber * dragPressureAverage = OSDynamicCast( OSNumber, dict->getObject("CypressDragPressureAverage"));
OSNumber * fiveFingerSleepTimer = OSDynamicCast( OSNumber, dict->getObject("Cypress5FingerSleepTimer"));
OSNumber * fiveFingerScreenLockTimer = OSDynamicCast( OSNumber, dict->getObject("Cypress5FingerScreenLockTimer"));
OSNumber * pressureFiltering = OSDynamicCast( OSNumber, dict->getObject("CypressPressureFiltering"));
OSNumber * twoFingerFiltering = OSDynamicCast( OSNumber, dict->getObject("Cypress2FingerFiltering"));
OSNumber * threeFingerFiltering = OSDynamicCast( OSNumber, dict->getObject("Cypress3FingerFiltering"));
OSNumber * fourFingerFiltering = OSDynamicCast( OSNumber, dict->getObject("Cypress4FingerFiltering"));
OSBoolean * fourFingerHorizSwipeGesture = OSDynamicCast( OSBoolean, dict->getObject("CypressFourFingerHorizSwipeGesture") );
OSBoolean * fourFingerVertSwipeGesture = OSDynamicCast( OSBoolean, dict->getObject("CypressFourFingerVertSwipeGesture") );
OSBoolean * twoFingerRightClick = OSDynamicCast( OSBoolean, dict->getObject("CypressTwoFingerRightClick") );
OSBoolean * threeFingerDrag = OSDynamicCast( OSBoolean, dict->getObject("CypressThreeFingerDrag"));
OSBoolean * fiveFingerSleep = OSDynamicCast( OSBoolean, dict->getObject("CypressFiveFingerSleep"));
OSBoolean * fiveFingerScreenLock = OSDynamicCast( OSBoolean, dict->getObject("CypressFiveFingerScreenLock"));
#ifdef DEBUG
OSCollectionIterator* iter = OSCollectionIterator::withCollection( dict );
OSObject* obj;
iter->reset();
while ((obj = iter->getNextObject()) != NULL)
{
OSString* str = OSDynamicCast( OSString, obj );
OSNumber* val = OSDynamicCast( OSNumber, dict->getObject( str ) );
if (val)
DEBUG_LOG("%s: Dictionary Object: %s Value: %d\n", getName(),
str->getCStringNoCopy(), val->unsigned32BitValue());
else
DEBUG_LOG("%s: Dictionary Object: %s Value: ??\n", getName(),
str->getCStringNoCopy());
}
#endif
if (pressureFiltering)
{
_pressureFiltering = pressureFiltering->unsigned32BitValue();
_kalZ.noiseLevel(_pressureFiltering);
setProperty("CypressPressureFiltering", pressureFiltering);
}
if (twoFingerFiltering)
{
_twoFingerFiltering = twoFingerFiltering->unsigned32BitValue();
setProperty("Cypress2FingerFiltering", twoFingerFiltering);
}
if (threeFingerFiltering)
{
_threeFingerFiltering = threeFingerFiltering->unsigned32BitValue();
setProperty("Cypress3FingerFiltering", threeFingerFiltering);
}
if (fourFingerFiltering)
{
_fourFingerFiltering = fourFingerFiltering->unsigned32BitValue();
setProperty("Cypress4FingerFiltering", fourFingerFiltering);
}
if (clicking)
{
_clicking = clicking->unsigned32BitValue() & 0x1 ? true : false;
setProperty("Clicking", clicking);
}
_clicking = true;
if (dragging)
{
_dragging = dragging->unsigned32BitValue() & 0x1 ? true : false;
setProperty("Dragging", dragging);
}
_dragging = true;
if (draglock)
{
_dragLock = draglock->unsigned32BitValue() & 0x1 ? true : false;
setProperty("DragLock", draglock);
}
_dragLock = true;
if (hscroll)
{
_trackpadHorizScroll = hscroll->unsigned32BitValue() & 0x1 ? true : false;
setProperty("TrackpadHorizScroll", hscroll);
}
_trackpadHorizScroll = true;
if (vscroll)
{
_trackpadScroll = vscroll->unsigned32BitValue() & 0x1 ? true : false;
setProperty("TrackpadScroll", vscroll);
}
_trackpadScroll = true;
if (scrollspeed)
{
_twoFingerDivider = (float)(scrollspeed->unsigned32BitValue());
setProperty("HIDTrackpadScrollAcceleration", scrollspeed);
}
if (dragPressureAverage)
{
_dragPressureAverage = dragPressureAverage->unsigned32BitValue();
setProperty("CypressDragPressureAverage", dragPressureAverage);
}
_dragPressureAverage = 85;
if (onefingermaxtaptime)
{
_onefingermaxtaptime = ((onefingermaxtaptime->unsigned32BitValue()) * 1000000);
setProperty("Cypress1FingerMaxTapTime", onefingermaxtaptime);
}
if (twofingermaxtaptime)
{
_twofingermaxtaptime = ((twofingermaxtaptime->unsigned32BitValue()) * 1000000);
setProperty("Cypress2FingerMaxTapTime", twofingermaxtaptime);
}
if (threefingermaxtaptime)
{
_threefingermaxtaptime = ((threefingermaxtaptime->unsigned32BitValue()) * 1000000);
setProperty("Cypress3FingerMaxTapTime", threefingermaxtaptime);
}
if (fourfingermaxtaptime)
{
_fourfingermaxtaptime = ((fourfingermaxtaptime->unsigned32BitValue()) * 1000000);
setProperty("Cypress4FingerMaxTapTime", fourfingermaxtaptime);
}
if (fivefingermaxtaptime)
{
_fivefingermaxtaptime = ((fivefingermaxtaptime->unsigned32BitValue()) * 1000000);
setProperty("Cypress3FingerMaxTapTime", fivefingermaxtaptime);
}
if (fourFingerHorizSwipeGesture)
{
_fourFingerHorizSwipeGesture = fourFingerHorizSwipeGesture->isTrue();
setProperty("CypressFourFingerHorizSwipeGesture", fourFingerHorizSwipeGesture->isTrue());
}
_fourFingerHorizSwipeGesture = true;
if (fourFingerVertSwipeGesture)
{
_fourFingerVertSwipeGesture = fourFingerVertSwipeGesture->isTrue();
setProperty("CypressFourFingerVertSwipeGesture", fourFingerVertSwipeGesture->isTrue());
}
_fourFingerVertSwipeGesture = true;
if (threeFingerDrag)
{
_threeFingerDrag = threeFingerDrag->isTrue();
setProperty("CypressThreeFingerDrag", threeFingerDrag->isTrue());
}
_threeFingerDrag = true;
if (twoFingerRightClick)
{
_twoFingerRightClick = twoFingerRightClick->isTrue();
setProperty("CypressTwoFingerRightClick", twoFingerRightClick->isTrue());
}
if (fiveFingerScreenLock)
{
_fiveFingerScreenLock = fiveFingerScreenLock->isTrue();
setProperty("CypressFiveFingerScreenLock", fiveFingerScreenLock->isTrue());
}
_fiveFingerScreenLock = true;
if (fiveFingerSleep)
{
_fiveFingerSleep = fiveFingerSleep->isTrue();
setProperty("CypressFiveFingerSleep", fiveFingerSleep->isTrue());
}
_fiveFingerSleep = true;
if (fiveFingerSleepTimer)
{
_fivefingersleeptime = ((fiveFingerSleepTimer->unsigned32BitValue()) * 1000000);
setProperty("Cypress5FingerSleepTimer", fiveFingerSleepTimer);
}
_fivefingersleeptime = 4 * 1000000;
if (fiveFingerScreenLockTimer)
{
_fivefingerscreenlocktime = ((fiveFingerScreenLockTimer->unsigned32BitValue()) * 1000000);
setProperty("Cypress5FingerScreenLockTimer", fiveFingerScreenLockTimer);
}
_fivefingerscreenlocktime = 1 * 1000000;
return super::setParamProperties(dict);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ApplePS2CypressTouchPad::setDevicePowerState( UInt32 whatToDo )
{
switch ( whatToDo )
{
case kPS2C_DisableDevice:
//
// Disable touchpad.
//
setTouchPadEnable( false );
break;
case kPS2C_EnableDevice:
//
// Finally, we enable the trackpad itself, so that it may
// start reporting asynchronous events.
//
IOSleep(_wakeDelay);
initTouchPad();
break;
}
}
bool ApplePS2CypressTouchPad::cypressVerifyCmdState(UInt8 cmd, UInt8 *param)
{
bool rate_match = false;
bool resolution_match = false;
int i;
/* callers will do further checking. */
if (cmd == CYTP_CMD_READ_CYPRESS_ID ||
cmd == CYTP_CMD_STANDARD_MODE ||
cmd == CYTP_CMD_READ_TP_METRICS)
return true;
if ((~param[0] & DFLT_RESP_BITS_VALID) == DFLT_RESP_BITS_VALID &&
(param[0] & DFLT_RESP_BIT_MODE) == DFLT_RESP_STREAM_MODE)
{
for (i = 0; i < sizeof(_cytp_resolution); i++)
if (_cytp_resolution[i] == param[1])
resolution_match = true;
for (i = 0; i < sizeof(_cytp_rate); i++)
if (_cytp_rate[i] == param[2])
rate_match = true;
if (resolution_match && rate_match)
return true;
}
DEBUG_LOG("CYPRESS: cypressVerifyCmdState: verify cmd state failed\n");
return false;
}
UInt8 ApplePS2CypressTouchPad::cypressSendByte(UInt8 cmd)
{
TPS2Request<1> request;
request.commands[0].command = kPS2C_SendMouseCommandAndCompareAck;
request.commands[0].inOrOut = cmd & 0xFF; // & 0xFF is useless but here to hint/guess from where it comes from ^^'
request.commandsCount = 1;
assert(request.commandsCount <= countof(request.commands));
_device->submitRequestAndBlock(&request);
if (request.commandsCount != 1)
{
DEBUG_LOG("CYPRESS: cypressSendByte: RETRY\n");
return (CYTP_PS2_RETRY);
}
return (0);
}
UInt8 ApplePS2CypressTouchPad::cypressExtCmd(UInt8 cmd, UInt8 data)
{
int tries = CYTP_PS2_CMD_TRIES;
UInt8 rc;
do {
/*
* Send extension command byte (0xE8 or 0xF3).
* If sending the command fails, send recovery command
* to make the device return to the ready state.
*/
rc = cypressSendByte(cmd & 0xff); // & 0xFF is useless but here to hint/guess from where it comes from ^^'
if (rc == CYTP_PS2_RETRY)
{
rc = cypressSendByte(0x00);
if (rc == CYTP_PS2_RETRY)
rc = cypressSendByte(0x0a);
}
if (rc == CYTP_PS2_ERROR)
continue;
rc = cypressSendByte(data);
if (rc == CYTP_PS2_RETRY)
rc = cypressSendByte(data);
if (rc == CYTP_PS2_ERROR)
continue;
else
break;
} while (--tries > 0);
return (rc);
}
UInt8 ApplePS2CypressTouchPad::cypressReadCmdStatus(UInt8 cmd, UInt8 *param)
{
TPS2Request<> request;
int i;
request.commands[0].command = kPS2C_SendMouseCommandAndCompareAck;
request.commands[0].inOrOut = kDP_GetMouseInformation;
for (i = 1; i < ((cmd == CYTP_CMD_READ_TP_METRICS ? 8 : 3) + 1); i++)
{
request.commands[i].command = kPS2C_ReadDataPort;
request.commands[i].inOrOut = 0;
}
request.commandsCount = i;
assert(request.commandsCount <= countof(request.commands));
_device->submitRequestAndBlock(&request);
DEBUG_LOG("CYPRESS: cypressReadCmdStatus: storing { ");
for (i = 1; i < ((cmd == CYTP_CMD_READ_TP_METRICS ? 8 : 3) + 1); i++)
{
param[i - 1] = request.commands[i].inOrOut;
DEBUG_LOG(" 0x%02x,", param[i - 1]);
}
DEBUG_LOG(" }\n");
return (0);
}
bool ApplePS2CypressTouchPad::cypressSendCmd(UInt8 cmd, UInt8 *param)
{
int tries = CYTP_PS2_CMD_TRIES;
int rc;
DEBUG_LOG("CYPRESS: cypressSendCmd: send extension cmd 0x%02x, [%d %d %d %d]\n", cmd, DECODE_CMD_AA(cmd), DECODE_CMD_BB(cmd), DECODE_CMD_CC(cmd), DECODE_CMD_DD(cmd));
do {
cypressExtCmd(kDP_SetMouseResolution, DECODE_CMD_DD(cmd));
cypressExtCmd(kDP_SetMouseResolution, DECODE_CMD_CC(cmd));
cypressExtCmd(kDP_SetMouseResolution, DECODE_CMD_BB(cmd));
cypressExtCmd(kDP_SetMouseResolution, DECODE_CMD_AA(cmd));
rc = cypressReadCmdStatus(cmd, param);
if (rc)
continue;
if (cypressVerifyCmdState(cmd, param))
return (true);
} while (--tries > 0);
return (false);