-
Notifications
You must be signed in to change notification settings - Fork 87
/
VoodooPS2Keyboard.cpp
2202 lines (1978 loc) · 77.8 KB
/
VoodooPS2Keyboard.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) 1998-2000 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.1 (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@
*/
// enable for keyboard debugging
#ifdef DEBUG_MSG
//#define DEBUG_VERBOSE
#define DEBUG_LITE
#endif
#include "LegacyIOService.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winconsistent-missing-override"
#include <IOKit/IOLib.h>
#include <IOKit/hidsystem/IOHIDParameter.h>
#include <IOKit/pwr_mgt/IOPM.h>
#include <IOKit/pwr_mgt/RootDomain.h>
#include <IOKit/IOTimerEventSource.h>
#pragma clang diagnostic pop
#include "ApplePS2ToADBMap.h"
#include "VoodooPS2Controller.h"
#include "VoodooPS2Keyboard.h"
#include "ApplePS2ToADBMap.h"
#include "AppleACPIPS2Nub.h"
#include <IOKit/hidsystem/ev_keymap.h>
// Constants for Info.plist settings
#define kSleepPressTime "SleepPressTime"
#define kHIDFKeyMode "HIDFKeyMode"
#define kHIDF12EjectDelay "HIDF12EjectDelay"
#define kFunctionKeysStandard "Function Keys Standard"
#define kFunctionKeysSpecial "Function Keys Special"
#define kSwapCapsLockLeftControl "Swap capslock and left control"
#define kSwapCommandOption "Swap command and option"
#define kMakeApplicationKeyRightWindows "Make Application key into right windows"
#define kMakeApplicationKeyAppleFN "Make Application key into Apple Fn key"
#define kMakeRightModsHangulHanja "Make right modifier keys into Hangul and Hanja"
#define kUseISOLayoutKeyboard "Use ISO layout keyboard"
#define kLogScanCodes "LogScanCodes"
#define kBrightnessHack "BrightnessHack"
#define kMacroInversion "Macro Inversion"
#define kMacroTranslation "Macro Translation"
#define kMaxMacroTime "MaximumMacroTime"
// Definitions for Macro Inversion data format
//REVIEW: This should really be defined as some sort of structure
#define kIgnoreBytes 2 // first two bytes of macro data are ignored (always 0xffff)
#define kOutputBytes 2 // two bytes of Macro Inversion are used to specify output
#define kModifierBytes 4 // 4 bytes specify modifier key match criteria
#define kOutputBytesOffset (kIgnoreBytes+0)
#define kModifierBytesOffset (kIgnoreBytes+kOutputBytes+0)
#define kPrefixBytes (kIgnoreBytes+kOutputBytes+kModifierBytes)
#define kSequenceBytesOffset (kPrefixBytes+0)
#define kMinMacroInversion (kPrefixBytes+2)
// Constants for other services to communicate with
#define kIOHIDSystem "IOHIDSystem"
// =============================================================================
// ApplePS2Keyboard Class Implementation
//
// get some keyboard id information from IOHIDFamily/IOHIDKeyboard.h and Gestalt.h
//#define APPLEPS2KEYBOARD_DEVICE_TYPE 205 // Generic ISO keyboard
#define APPLEPS2KEYBOARD_DEVICE_TYPE 3 // Unknown ANSI keyboard
OSDefineMetaClassAndStructors(ApplePS2Keyboard, IOHIKeyboard);
UInt32 ApplePS2Keyboard::deviceType()
{
OSNumber *xml_handlerID;
UInt32 ret_id;
if ( (xml_handlerID = OSDynamicCast( OSNumber, getProperty("alt_handler_id"))) )
ret_id = xml_handlerID->unsigned32BitValue();
else
ret_id = APPLEPS2KEYBOARD_DEVICE_TYPE;
return ret_id;
}
UInt32 ApplePS2Keyboard::interfaceID() { return NX_EVS_DEVICE_INTERFACE_ADB; }
UInt32 ApplePS2Keyboard::maxKeyCodes() { return NX_NUMKEYCODES; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static const char* parseHex(const char *psz, char term1, char term2, unsigned& out)
{
int n = 0;
for (; 0 != *psz && term1 != *psz && term2 != *psz; ++psz)
{
n <<= 4;
if (*psz >= '0' && *psz <= '9')
n += *psz - '0';
else if (*psz >= 'a' && *psz <= 'f')
n += *psz - 'a' + 10;
else if (*psz >= 'A' && *psz <= 'F')
n += *psz - 'A' + 10;
else
return NULL;
}
out = n;
return psz;
}
static bool parseRemap(const char *psz, UInt16 &scanFrom, UInt16& scanTo)
{
// psz is of the form: "scanfrom=scanto", examples:
// non-extended: "1d=3a"
// extended: "e077=e017"
// of course, extended can be mapped to non-extended or non-extended to extended
unsigned n;
psz = parseHex(psz, '=', 0, n);
if (NULL == psz || *psz != '=' || n > 0xFFFF)
return false;
scanFrom = n;
psz = parseHex(psz+1, '\n', ';', n);
if (NULL == psz || n > 0xFFFF)
return false;
scanTo = n;
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ApplePS2Keyboard::init(OSDictionary * dict)
{
//
// Initialize this object's minimal state. This is invoked right after this
// object is instantiated.
//
if (!super::init(dict))
return false;
// initialize state
_device = 0;
_extendCount = 0;
_interruptHandlerInstalled = false;
_ledState = 0;
_lastdata = 0;
_swapcommandoption = false;
_sleepEjectTimer = 0;
_cmdGate = 0;
_fkeymode = 0;
_fkeymodesupported = false;
_keysStandard = 0;
_keysSpecial = 0;
_f12ejectdelay = 250; // default is 250 ms
// initialize ACPI support for keyboard backlight/screen brightness
_provider = 0;
_brightnessLevels = 0;
_backlightLevels = 0;
_logscancodes = 0;
_brightnessHack = false;
// initalize macro translation
_macroInversion = 0;
_macroTranslation = 0;
_macroBuffer = 0;
_macroCurrent = 0;
_macroMax = 0;
_macroMaxTime = 25000000ULL;
_macroTimer = 0;
// start out with all keys up
bzero(_keyBitVector, sizeof(_keyBitVector));
// make separate copy of ADB translation table.
bcopy(PS2ToADBMapStock, _PS2ToADBMapMapped, sizeof(_PS2ToADBMapMapped));
// Setup the PS2 -> PS2 scan code mapper
for (int i = 0; i < countof(_PS2ToPS2Map); i++)
{
// by default, each map entry is just itself (no mapping)
// first half of map is normal scan codes, second half is extended scan codes (e0)
_PS2ToPS2Map[i] = i;
}
bcopy(_PS2flagsStock, _PS2flags, sizeof(_PS2flags));
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ApplePS2Keyboard* ApplePS2Keyboard::probe(IOService * provider, SInt32 * score)
{
DEBUG_LOG("ApplePS2Keyboard::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
// keyboard clock is enabled and the keyboard itself is disabled (thus
// it won't send any asynchronous scan codes that may mess up the
// responses expected by the commands we send it). This is invoked
// after the init.
//
if (!super::probe(provider, score))
return 0;
// find config specific to Platform Profile
OSDictionary* list = OSDynamicCast(OSDictionary, getProperty(kPlatformProfile));
ApplePS2Device* device = (ApplePS2Device*)provider;
OSDictionary* config = device->getController()->makeConfigurationNode(list, "Keyboard");
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 0;
}
#ifdef DEBUG
// save configuration for later/diagnostics...
setProperty(kMergedConfiguration, config);
#endif
}
//
// Load settings specfic to the Platform Profile...
//
if (config)
{
// now load PS2 -> PS2 configuration data
loadCustomPS2Map(OSDynamicCast(OSArray, config->getObject("Custom PS2 Map")));
loadBreaklessPS2(config, "Breakless PS2");
// now load PS2 -> ADB configuration data
loadCustomADBMap(config, "Custom ADB Map");
// determine if _fkeymode property should be handled in setParamProperties
_keysStandard = OSDynamicCast(OSArray, config->getObject(kFunctionKeysStandard));
_keysSpecial = OSDynamicCast(OSArray, config->getObject(kFunctionKeysSpecial));
_fkeymodesupported = _keysStandard && _keysSpecial;
if (_fkeymodesupported)
{
setProperty(kHIDFKeyMode, (uint64_t)0, 64);
_keysStandard->retain();
_keysSpecial->retain();
loadCustomPS2Map(_keysSpecial);
}
else
{
_keysStandard = NULL;
_keysSpecial = NULL;
}
// load custom macro data
_macroTranslation = loadMacroData(config, kMacroTranslation);
_macroInversion = loadMacroData(config, kMacroInversion);
if (_macroInversion)
{
int max = 0;
for (OSData** p = _macroInversion; *p; p++)
{
int length = (*p)->getLength()-kPrefixBytes;
if (length > max)
max = length;
}
_macroBuffer = new UInt8[max*kPacketLength];
_macroMax = max;
}
}
// now copy to our PS2ToADBMap -- working copy...
bcopy(_PS2ToADBMapMapped, _PS2ToADBMap, sizeof(_PS2ToADBMap));
// populate rest of values via setParamProperties
setParamPropertiesGated(config);
OSSafeReleaseNULL(config);
// Note: always return success for keyboard, so no need to do this!
// But we do it in the DEBUG build just for information's sake.
#ifdef DEBUG
//
// Check to see if the keyboard responds to a basic diagnostic echo.
//
// (diagnostic echo command)
TPS2Request<2> request;
request.commands[0].command = kPS2C_WriteDataPort;
request.commands[0].inOrOut = kDP_TestKeyboardEcho;
request.commands[1].command = kPS2C_ReadDataPort;
request.commands[1].inOrOut = 0x00;
request.commandsCount = 2;
assert(request.commandsCount <= countof(request.commands));
device->submitRequestAndBlock(&request);
UInt8 result = request.commands[1].inOrOut;
if (2 != request.commandsCount || (0x00 != result && kDP_TestKeyboardEcho != result && kSC_Acknowledge != result))
{
IOLog("%s: TestKeyboardEcho $EE failed: %d (%02x)\n", getName(), request.commandsCount, result);
IOLog("%s: ApplePS2Keyboard::probe would normally return failure\n", getName());
}
#endif
DEBUG_LOG("ApplePS2Keyboard::probe leaving.\n");
// Note: forced success regardless of "test keyboard echo"
return this;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ApplePS2Keyboard::start(IOService * provider)
{
DEBUG_LOG("ApplePS2Keyboard::start entered...\n");
setProperty(kDeliverNotifications, kOSBooleanTrue);
setProperty(kDeliverNotifications, kOSBooleanTrue);
//
// The driver has been instructed to start. This is called after a
// successful attach.
//
if (!super::start(provider))
return false;
//
// Maintain a pointer to and retain the provider object.
//
_device = (ApplePS2KeyboardDevice *)provider;
_device->retain();
//
// Setup workloop with command gate for thread syncronization...
//
IOWorkLoop* pWorkLoop = getWorkLoop();
_cmdGate = IOCommandGate::commandGate(this);
if (!pWorkLoop || !_cmdGate)
{
_device->release();
_device = 0;
return false;
}
_sleepEjectTimer = IOTimerEventSource::timerEventSource(this, OSMemberFunctionCast(IOTimerEventSource::Action, this, &ApplePS2Keyboard::onSleepEjectTimer));
if (!_sleepEjectTimer)
{
_cmdGate->release();
_cmdGate = 0;
_device->release();
_device = 0;
return false;
}
pWorkLoop->addEventSource(_sleepEjectTimer);
pWorkLoop->addEventSource(_cmdGate);
// _macroTimer is used in for macro inversion
_macroTimer = IOTimerEventSource::timerEventSource(this, OSMemberFunctionCast(IOTimerEventSource::Action, this, &ApplePS2Keyboard::onMacroTimer));
if (_macroTimer)
pWorkLoop->addEventSource(_macroTimer);
// get IOACPIPlatformDevice for Device (PS2K)
//REVIEW: should really look at the parent chain for IOACPIPlatformDevice instead.
_provider = (IOACPIPlatformDevice*)IORegistryEntry::fromPath("IOService:/AppleACPIPlatformExpert/PS2K");
//
// get brightness levels for ACPI based brightness keys
//
OSObject* result = 0;
if (_provider) do
{
// check for brightness methods
if (kIOReturnSuccess != _provider->validateObject("KBCL") || kIOReturnSuccess != _provider->validateObject("KBCM") || kIOReturnSuccess != _provider->validateObject("KBQC"))
{
break;
}
// methods are there, so now try to collect brightness levels
if (kIOReturnSuccess != _provider->evaluateObject("KBCL", &result))
{
DEBUG_LOG("ps2br: KBCL returned error\n");
break;
}
OSArray* array = OSDynamicCast(OSArray, result);
if (!array)
{
DEBUG_LOG("ps2br: KBCL returned non-array package\n");
break;
}
int count = array->getCount();
if (count < 4)
{
DEBUG_LOG("ps2br: KBCL returned invalid package\n");
break;
}
_brightnessCount = count;
_brightnessLevels = new int[_brightnessCount];
if (!_brightnessLevels)
{
DEBUG_LOG("ps2br: _brightnessLevels new int[] failed\n");
break;
}
for (int i = 0; i < _brightnessCount; i++)
{
OSNumber* num = OSDynamicCast(OSNumber, array->getObject(i));
int brightness = num ? num->unsigned32BitValue() : 0;
_brightnessLevels[i] = brightness;
}
#ifdef DEBUG_VERBOSE
DEBUG_LOG("ps2br: Brightness levels: { ");
for (int i = 0; i < _brightnessCount; i++)
DEBUG_LOG("%d, ", _brightnessLevels[i]);
DEBUG_LOG("}\n");
#endif
break;
} while (false);
OSSafeReleaseNULL(result);
//
// get keyboard backlight levels for ACPI based backlight keys
//
if (_provider) do
{
// check for brightness methods
if (kIOReturnSuccess != _provider->validateObject("KKCL") || kIOReturnSuccess != _provider->validateObject("KKCM") || kIOReturnSuccess != _provider->validateObject("KKQC"))
{
DEBUG_LOG("ps2bl: KKCL, KKCM, KKQC methods not found in DSDT\n");
break;
}
// methods are there, so now try to collect brightness levels
if (kIOReturnSuccess != _provider->evaluateObject("KKCL", &result))
{
DEBUG_LOG("ps2bl: KKCL returned error\n");
break;
}
OSArray* array = OSDynamicCast(OSArray, result);
if (!array)
{
DEBUG_LOG("ps2bl: KKCL returned non-array package\n");
break;
}
int count = array->getCount();
if (count < 2)
{
DEBUG_LOG("ps2bl: KKCL returned invalid package\n");
break;
}
_backlightCount = count;
_backlightLevels = new int[_backlightCount];
if (!_backlightLevels)
{
DEBUG_LOG("ps2bl: _backlightLevels new int[] failed\n");
break;
}
for (int i = 0; i < _backlightCount; i++)
{
OSNumber* num = OSDynamicCast(OSNumber, array->getObject(i));
int brightness = num ? num->unsigned32BitValue() : 0;
_backlightLevels[i] = brightness;
}
#ifdef DEBUG_VERBOSE
DEBUG_LOG("ps2bl: Keyboard backlight levels: { ");
for (int i = 0; i < _backlightCount; i++)
DEBUG_LOG("%d, ", _backlightLevels[i]);
DEBUG_LOG("}\n");
#endif
break;
} while (false);
OSSafeReleaseNULL(result);
//
// Lock the controller during initialization
//
_device->lock();
//
// Reset and enable the keyboard.
//
initKeyboard();
//
// Install our driver's interrupt handler, for asynchronous data delivery.
//
_device->installInterruptAction(this,
OSMemberFunctionCast(PS2InterruptAction, this, &ApplePS2Keyboard::interruptOccurred),
OSMemberFunctionCast(PS2PacketAction,this,&ApplePS2Keyboard::packetReady));
_interruptHandlerInstalled = true;
// now safe to allow other threads
_device->unlock();
//
// Install our power control handler.
//
_device->installPowerControlAction( this,
OSMemberFunctionCast(PS2PowerControlAction,this, &ApplePS2Keyboard::setDevicePowerState ));
_powerControlHandlerInstalled = true;
//
// Tell ACPIPS2Nub that we are interested in ACPI notifications
//
//setProperty(kDeliverNotifications, true);
DEBUG_LOG("ApplePS2Keyboard::start leaving.\n");
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ApplePS2Keyboard::loadCustomPS2Map(OSArray* pArray)
{
if (NULL != pArray)
{
int count = pArray->getCount();
for (int i = 0; i < count; i++)
{
OSString* pString = OSDynamicCast(OSString, pArray->getObject(i));
if (NULL == pString)
continue;
const char* psz = pString->getCStringNoCopy();
// check for comment
if (';' == *psz)
continue;
// otherwise, try to parse it
UInt16 scanIn, scanOut;
if (!parseRemap(psz, scanIn, scanOut))
{
IOLog("VoodooPS2Keyboard: invalid custom PS2 map entry: \"%s\"\n", psz);
continue;
}
// must be normal scan code or extended, nothing else
UInt8 exIn = scanIn >> 8;
UInt8 exOut = scanOut >> 8;
if ((exIn != 0 && exIn != 0xe0) || (exOut != 0 && exOut != 0xe0))
{
IOLog("VoodooPS2Keyboard: scan code invalid for PS2 map entry: \"%s\"\n", psz);
continue;
}
// modify PS2 to PS2 map per remap entry
int index = (scanIn & 0xff) + (exIn == 0xe0 ? KBV_NUM_SCANCODES : 0);
assert(index < countof(_PS2ToPS2Map));
_PS2ToPS2Map[index] = (scanOut & 0xff) + (exOut == 0xe0 ? KBV_NUM_SCANCODES : 0);
}
}
}
void ApplePS2Keyboard::loadBreaklessPS2(OSDictionary* dict, const char* name)
{
OSArray* pArray = OSDynamicCast(OSArray, dict->getObject(name));
if (NULL != pArray)
{
int count = pArray->getCount();
for (int i = 0; i < count; i++)
{
OSString* pString = OSDynamicCast(OSString, pArray->getObject(i));
if (NULL == pString)
continue;
const char* psz = pString->getCStringNoCopy();
// check for comment
if (';' == *psz)
continue;
// otherwise, try to parse it
unsigned scanIn;
if (!parseHex(psz, '\n', ';', scanIn))
{
IOLog("VoodooPS2Keyboard: invalid breakless PS2 entry: \"%s\"\n", psz);
continue;
}
// must be normal scan code or extended, nothing else
UInt8 exIn = scanIn >> 8;
if ((exIn != 0 && exIn != 0xe0))
{
IOLog("VoodooPS2Keyboard: scan code invalid for breakless PS2 entry: \"%s\"\n", psz);
continue;
}
// modify PS2 to PS2 map per remap entry
int index = (scanIn & 0xff) + (exIn == 0xe0 ? KBV_NUM_SCANCODES : 0);
assert(index < countof(_PS2flags));
_PS2flags[index] |= kBreaklessKey;
}
}
}
void ApplePS2Keyboard::loadCustomADBMap(OSDictionary* dict, const char* name)
{
OSArray* pArray = OSDynamicCast(OSArray, dict->getObject(name));
if (NULL != pArray)
{
int count = pArray->getCount();
for (int i = 0; i < count; i++)
{
OSString* pString = OSDynamicCast(OSString, pArray->getObject(i));
if (NULL == pString)
continue;
const char* psz = pString->getCStringNoCopy();
// check for comment
if (';' == *psz)
continue;
// otherwise, try to parse it
UInt16 scanIn, adbOut;
if (!parseRemap(psz, scanIn, adbOut))
{
IOLog("VoodooPS2Keyboard: invalid custom ADB map entry: \"%s\"\n", psz);
continue;
}
// must be normal scan code or extended, nothing else, adbOut is only a byte
UInt8 exIn = scanIn >> 8;
if ((exIn != 0 && exIn != 0xe0) || adbOut > 0xFF)
{
IOLog("VoodooPS2Keyboard: scan code invalid for ADB map entry: \"%s\"\n", psz);
continue;
}
// modify PS2 to ADB map per remap entry
int index = (scanIn & 0xff) + (exIn == 0xe0 ? ADB_CONVERTER_EX_START : 0);
assert(index < countof(_PS2ToADBMapMapped));
_PS2ToADBMapMapped[index] = adbOut;
}
}
}
OSData** ApplePS2Keyboard::loadMacroData(OSDictionary* dict, const char* name)
{
OSData** result = 0;
OSArray* pArray = OSDynamicCast(OSArray, dict->getObject(name));
if (NULL != pArray)
{
// count valid entries
int total = 0;
int count = pArray->getCount();
for (int i = 0; i < count; i++)
{
if (OSData* pData = OSDynamicCast(OSData, pArray->getObject(i)))
{
int length = pData->getLength();
if (length >= kMinMacroInversion && !(length & 0x01))
{
const UInt8* p = static_cast<const UInt8*>(pData->getBytesNoCopy());
if (p[0] == 0xFF && p[1] == 0xFF)
total++;
}
}
}
if (total)
{
// store valid entries
result = new OSData*[total+1];
if (result)
{
bzero(result, sizeof(char*)*(total+1));
int index = 0;
for (int i = 0; i < count; i++)
{
if (OSData* pData = OSDynamicCast(OSData, pArray->getObject(i)))
{
int length = pData->getLength();
if (length >= kMinMacroInversion && !(length & 0x01))
{
const UInt8* p = static_cast<const UInt8*>(pData->getBytesNoCopy());
if (p[0] == 0xFF && p[1] == 0xFF)
{
result[index++] = pData;
pData->retain();
}
}
}
}
}
}
}
return result;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ApplePS2Keyboard::setParamPropertiesGated(OSDictionary * dict)
{
if (NULL == dict)
return;
//REVIEW: this code needs cleanup (should be table driven like mouse/trackpad)
// get time before sleep button takes effect
if (OSNumber* num = OSDynamicCast(OSNumber, dict->getObject(kSleepPressTime)))
{
_maxsleeppresstime = num->unsigned32BitValue();
setProperty(kSleepPressTime, _maxsleeppresstime, 32);
}
// get time before eject button takes effect (no modifiers)
if (OSNumber* num = OSDynamicCast(OSNumber, dict->getObject(kHIDF12EjectDelay)))
{
_f12ejectdelay = num->unsigned32BitValue();
setProperty(kHIDF12EjectDelay, _f12ejectdelay, 32);
}
// get time between keys part of a macro "inversion"
if (OSNumber* num = OSDynamicCast(OSNumber, dict->getObject(kMaxMacroTime)))
{
_macroMaxTime = num->unsigned64BitValue();
setProperty(kMaxMacroTime, _macroMaxTime, 64);
}
if (_fkeymodesupported)
{
// get function key mode
UInt32 oldfkeymode = _fkeymode;
if (OSNumber* num = OSDynamicCast(OSNumber, dict->getObject(kHIDFKeyMode)))
{
_fkeymode = num->unsigned32BitValue();
setProperty(kHIDFKeyMode, _fkeymode, 32);
}
if (oldfkeymode != _fkeymode)
{
OSArray* keys = _fkeymode ? _keysStandard : _keysSpecial;
assert(keys);
loadCustomPS2Map(keys);
}
}
//
// Configure user preferences from Info.plist
//
OSBoolean* xml = OSDynamicCast(OSBoolean, dict->getObject(kSwapCapsLockLeftControl));
if (xml) {
if (xml->isTrue()) {
_PS2ToADBMap[0x3a] = _PS2ToADBMapMapped[0x1d];
_PS2ToADBMap[0x1d] = _PS2ToADBMapMapped[0x3a];
}
else {
_PS2ToADBMap[0x3a] = _PS2ToADBMapMapped[0x3a];
_PS2ToADBMap[0x1d] = _PS2ToADBMapMapped[0x1d];
}
setProperty(kSwapCapsLockLeftControl, xml->isTrue() ? kOSBooleanTrue : kOSBooleanFalse);
}
xml = OSDynamicCast(OSBoolean, dict->getObject(kSwapCommandOption));
if (xml) {
if (xml->isTrue()) {
_swapcommandoption = true;
_PS2ToADBMap[0x38] = _PS2ToADBMapMapped[0x15b];
_PS2ToADBMap[0x15b] = _PS2ToADBMapMapped[0x38];
_PS2ToADBMap[0x138] = _PS2ToADBMapMapped[0x15c];
_PS2ToADBMap[0x15c] = _PS2ToADBMapMapped[0x138];
}
else {
_swapcommandoption = false;
_PS2ToADBMap[0x38] = _PS2ToADBMapMapped[0x38];
_PS2ToADBMap[0x15b] = _PS2ToADBMapMapped[0x15b];
_PS2ToADBMap[0x138] = _PS2ToADBMapMapped[0x138];
_PS2ToADBMap[0x15c] = _PS2ToADBMapMapped[0x15c];
}
setProperty(kSwapCommandOption, xml->isTrue() ? kOSBooleanTrue : kOSBooleanFalse);
}
// special hack for HP Envy brightness
xml = OSDynamicCast(OSBoolean, dict->getObject(kBrightnessHack));
if (xml && xml->isTrue())
{
//REVIEW: should really read the key assignments via Info.plist instead of hardcoding to F2/F3
_brightnessHack = true;
}
// these two options are mutually exclusive
// kMakeApplicationKeyAppleFN is ignored if kMakeApplicationKeyRightWindows is set
bool temp = false;
xml = OSDynamicCast(OSBoolean, dict->getObject(kMakeApplicationKeyRightWindows));
if (xml) {
if (xml->isTrue()) {
_PS2ToADBMap[0x15d] = _swapcommandoption ? 0x3d : 0x36; // ADB = right-option/right-command
temp = true;
}
else {
_PS2ToADBMap[0x15d] = _PS2ToADBMapMapped[0x15d];
}
setProperty(kMakeApplicationKeyRightWindows, xml->isTrue() ? kOSBooleanTrue : kOSBooleanFalse);
}
// not implemented yet (Note: maybe not true any more)
// Apple Fn key works well, but no combined key action was made.
xml = OSDynamicCast(OSBoolean, dict->getObject(kMakeApplicationKeyAppleFN));
if (xml) {
if (!temp) {
if (xml->isTrue()) {
_PS2ToADBMap[0x15d] = 0x3f; // ADB = AppleFN
}
else {
_PS2ToADBMap[0x15d] = _PS2ToADBMapMapped[0x15d];
}
}
setProperty(kMakeApplicationKeyAppleFN, xml->isTrue() ? kOSBooleanTrue : kOSBooleanFalse);
}
xml = OSDynamicCast(OSBoolean, dict->getObject(kMakeRightModsHangulHanja));
if (xml) {
if (xml->isTrue()) {
_PS2ToADBMap[0x138] = _PS2ToADBMapMapped[0xf2]; // Right alt becomes Hangul
_PS2ToADBMap[0x11d] = _PS2ToADBMapMapped[0xf1]; // Right control becomes Hanja
}
else {
if (_swapcommandoption)
_PS2ToADBMap[0x138] = _PS2ToADBMapMapped[0x15c];
else
_PS2ToADBMap[0x138] = _PS2ToADBMapMapped[0x138];
_PS2ToADBMap[0x11d] = _PS2ToADBMapMapped[0x11d];
}
setProperty(kMakeRightModsHangulHanja, xml->isTrue() ? kOSBooleanTrue : kOSBooleanFalse);
}
// ISO specific mapping to match ADB keyboards
// This should really be done in the keymaps.
xml = OSDynamicCast(OSBoolean, dict->getObject(kUseISOLayoutKeyboard));
if (xml) {
if (xml->isTrue()) {
_PS2ToADBMap[0x29] = _PS2ToADBMapMapped[0x56]; //Europe2 '¤º'
_PS2ToADBMap[0x56] = _PS2ToADBMapMapped[0x29]; //Grave '~'
}
else {
_PS2ToADBMap[0x29] = _PS2ToADBMapMapped[0x29];
_PS2ToADBMap[0x56] = _PS2ToADBMapMapped[0x56];
}
setProperty(kUseISOLayoutKeyboard, xml->isTrue() ? kOSBooleanTrue : kOSBooleanFalse);
}
if (OSNumber* num = OSDynamicCast(OSNumber, dict->getObject(kLogScanCodes))) {
_logscancodes = num->unsigned32BitValue();
setProperty(kLogScanCodes, num);
}
}
IOReturn ApplePS2Keyboard::setParamProperties(OSDictionary *dict)
{
////IOReturn result = super::setParamProperties(dict);
if (_cmdGate)
{
// syncronize through workloop...
////_cmdGate->runAction(OSMemberFunctionCast(IOCommandGate::Action, this, &ApplePS2Keyboard::setParamPropertiesGated), dict);
setParamPropertiesGated(dict);
}
return super::setParamProperties(dict);
////return result;
}
IOReturn ApplePS2Keyboard::setProperties(OSObject *props)
{
OSDictionary *dict = OSDynamicCast(OSDictionary, props);
if (dict && _cmdGate)
{
// syncronize through workloop...
_cmdGate->runAction(OSMemberFunctionCast(IOCommandGate::Action, this, &ApplePS2Keyboard::setParamPropertiesGated), dict);
}
return super::setProperties(props);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ApplePS2Keyboard::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 keyboard itself, so that it may stop reporting key events.
//
setKeyboardEnable(false);
// free up the command gate
IOWorkLoop* pWorkLoop = getWorkLoop();
if (pWorkLoop)
{
if (_cmdGate)
{
pWorkLoop->removeEventSource(_cmdGate);
_cmdGate->release();
_cmdGate = 0;
}
if (_sleepEjectTimer)
{
pWorkLoop->removeEventSource(_sleepEjectTimer);
_sleepEjectTimer->release();
_sleepEjectTimer = 0;
}
if (_macroTimer)
{
pWorkLoop->removeEventSource(_macroTimer);
_macroTimer->release();
_macroTimer = 0;
}
}
//
// 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);
//
// Release ACPI provider for PS2K ACPI device
//
OSSafeReleaseNULL(_provider);
//
// Release data related to screen brightness
//
if (_brightnessLevels)
{
delete[] _brightnessLevels;
_brightnessLevels = 0;
}
//
// Release data related to screen brightness
//
if (_backlightLevels)
{
delete[] _backlightLevels;
_backlightLevels = 0;
}
OSSafeReleaseNULL(_keysStandard);
OSSafeReleaseNULL(_keysSpecial);
if (_macroInversion)
{
for (OSData** p = _macroInversion; *p; p++)
(*p)->release();
delete[] _macroInversion;
_macroInversion = 0;
}
if (_macroTranslation)
{
for (OSData** p = _macroTranslation; *p; p++)
(*p)->release();
delete[] _macroTranslation;
_macroTranslation = 0;
}
if (_macroBuffer)
{
delete[] _macroBuffer;
_macroBuffer = 0;
}
super::stop(provider);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IOReturn ApplePS2Keyboard::message(UInt32 type, IOService* provider, void* argument)