-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppleADBKeyboard.cpp
1242 lines (1093 loc) · 46.6 KB
/
AppleADBKeyboard.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-2004 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@
*/
/*
* 18 June 1998
* Start IOKit version.
*/
#include "AppleADBKeyboard.h"
#include <IOKit/hidsystem/IOHIDTypes.h>
#include <IOKit/IOLib.h>
#include <IOKit/IODeviceTreeSupport.h>
#define super IOHIKeyboard
#ifndef kIOHIDFKeyModeKey
#define kIOHIDFKeyModeKey "HIDFKeyMode"
#endif
enum {
kCapsLockState_CapsLockEngaged = 0x01,
kCapsLockState_CapsLockGoingDown = 0x02,
kCapsLockState_PowerEngaged = 0x04,
kCapsLockState_PowerCapsMapped = 0x08
};
OSDefineMetaClassAndStructors(AppleADBKeyboard,IOHIKeyboard)
#define RB_HALT 0x08 /* don't reboot, just halt */
extern "C" {
void Debugger( const char * );
void boot(int paniced, int howto, char * command);
}
static void AppleADBKeyboardReboot( thread_call_param_t arg, thread_call_param_t );
static void new_kbd_data ( IOService * us, UInt8 adbCommand, IOByteCount length, UInt8 * data );
static void asyncSetLEDFunc ( thread_call_param_t, thread_call_param_t );
#if 0 //The following table is in Info.plist now
//Convert raw ADB codes to MacOS 9 KMAP virtual key codes in dispatchKeyboardEvent()
static unsigned char kmapConvert[] =
{
//00,00,00,00, These 4 are in System resource, but are unused
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
0x30,0x31,0x32,0x33,0x34,0x35,0x3B,0x37,0x38,0x39,0x3A,0x7B,0x7C,0x7D,0x7E,0x3F,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x3C,0x3D,0x3E,0x36,0x7F,
00,00
};
#endif
void AppleADBKeyboard::stop( IOService * provider )
{
if (adbDevice)
{
IORegistryEntry * us;
us = this;
adbDevice->releaseFromClient(us); //us is just a placeholder, not used
adbDevice = 0;
}
super::stop(provider);
}
void AppleADBKeyboard::free( void )
{
if (_keybrdLock)
{
IOLockFree(_keybrdLock);
_keybrdLock = NULL;
}
if ( _packetLock )
{
IOLockFree( _packetLock );
_packetLock = NULL;
}
if (adbDevice)
{
IORegistryEntry * us;
us = this;
adbDevice->releaseFromClient(us); //us is just a placeholder, not used
adbDevice = 0;
}
if ( ledThreadCall )
{
thread_call_free ( ledThreadCall );
ledThreadCall = 0;
}
if ( rebootThreadCall )
{
thread_call_free ( rebootThreadCall );
rebootThreadCall = 0;
}
super::free();
}
// **********************************************************************************
// start
//
// **********************************************************************************
bool AppleADBKeyboard::start ( IOService * theNub )
{
OSString * data;
const char * pTable;
OSNumber *enable_fwd_delete;
_keybrdLock = IOLockAlloc();
_packetLock = IOLockAlloc();
_fn_key_invoked_power = false; //Used by iBook and PowerBooks
enable_fwd_delete = OSDynamicCast( OSNumber, getProperty("PowerBook fn Foward Delete"));
if (enable_fwd_delete)
{
_enable_fwd_delete = (bool) enable_fwd_delete->unsigned32BitValue();
}
if ( _packetLock )
IOLockLock( _packetLock );
adbDevice = (IOADBDevice *)theNub;
if( !adbDevice->seizeForClient(this, new_kbd_data) ) {
IOLog("%s: Seize failed\n", getName());
if ( _packetLock )
IOLockUnlock( _packetLock );
return false;
}
_oneshotCAPSLOCK = ((getLEDStatus() & ADBKS_LED_CAPSLOCK) != 0);
if ( _packetLock )
IOLockUnlock( _packetLock );
//Get virtual key map table
data = OSDynamicCast( OSString, getProperty( "ADBVirtualKeys" ));
if (data)
{
pTable = data->getCStringNoCopy();
if (data->getLength() < (5 * 0x7f)) //7f codes, 5 bytes per code in XML string
{
IOLog("AppleADBKeyboard: too few virtual keys found in Info.plist");
for (int i=0; i<128; i++)
{
_virtualmap[i] = i; //Somewhat usable keymap
}
}
else //pTable could get access violation if not all bytes are accounted for
{
for (int i=0; i< 128; i++) //No codes greater than 0x7f allowed in packet()
{
_virtualmap[i] = strtol(pTable, NULL, 16); //Must be base 16
pTable += 5; //Format is "0x00,0x01,0x02"
}
}
}
else
{
return false;
}
_sticky_fn_ON = _stickymodeON = false;
data = OSDynamicCast( OSString, getProperty( "fnVirtualKeys" )); //
if (data)
{
pTable = data->getCStringNoCopy();
if (data->getLength() < (5 * 0x7f)) //7f codes, 5 bytes per code in XML string
{
IOLog("AppleADBKeyboard: too few sticky fn keys found in Info.plist");
for (int i=0; i<128; i++)
{
_fnvirtualmap[i] = i; //Somewhat usable keymap
}
}
else
{
for (int i=0; i< 128; i++)
{
_fnvirtualmap[i] = strtol(pTable, NULL, 16); //Must be base 16
pTable += 5; //Format is "0x00,0x01,0x02"
}
}
}
else
{
IOLog("AppleADBKeyboard: no fn keymap found in Info.plist");
//No error return if sticky fn data does not exist, it's not as critical since
// numlock on PB would accomplish the same thing. However, _fnvirtualmap
// must be set to something valid
memcpy(_fnvirtualmap, _virtualmap, 128);
}
_get_last_keydown = OSSymbol::withCString("get_last_keydown");
_get_handler_id = OSSymbol::withCString("get_handler_id");
_get_device_flags = OSSymbol::withCString("get_device_flags");
turnLEDon = ADBKS_LED_CAPSLOCK | ADBKS_LED_NUMLOCK | ADBKS_LED_SCROLLLOCK; //negative logic
setAlphaLockFeedback(false);
setNumLockFeedback(false);
clock_interval_to_absolutetime_interval( 4, kSecondScale, &rebootTime);
clock_interval_to_absolutetime_interval( 1, kSecondScale, &debuggerTime);
_hasDualModeFunctionKeys = false;
updateFKeyMap(); //UPDATE PMU DATA IF APPROPRIATE
getFKeyMode(); // this will update _hasDualModeFunctionKeys
if (_hasDualModeFunctionKeys)
{
setFKeyMode( 0 ); //set PowerBook Function keys to default (0) mode.
}
// RY: allocate thread calls for led and reboot
ledThreadCall = thread_call_allocate ( asyncSetLEDFunc, (thread_call_param_t) this);
rebootThreadCall = thread_call_allocate ( AppleADBKeyboardReboot, (thread_call_param_t) RB_HALT);
return super::start(theNub);
}
bool AppleADBKeyboard::open(IOService * client,
IOOptionBits options,
KeyboardEventAction keAction,
KeyboardSpecialEventAction kseAction,
UpdateEventFlagsAction uefAction)
{
if (!super::open(client, options, keAction, kseAction, uefAction))
return false;
// RY: Check to see if LED was set prior to start. If so dispatch
// event so that software state matches hardware state.
if ( _packetLock )
IOLockLock( _packetLock );
if (_oneshotCAPSLOCK)
{
_oneshotCAPSLOCK = false; // only set to true by start()
//check handler ID since keyboards of type 1 fail register 2 check
if (adbDevice && adbDevice->handlerID() > 0xc0) //Anything beyond 0xc0 is WallStreet or higher, and USB and BT
{
AbsoluteTime locktime;
clock_get_uptime(&locktime);
// setting caps lock
super::dispatchKeyboardEvent(ADBK_CAPSLOCK, true, locktime);
super::dispatchKeyboardEvent(ADBK_CAPSLOCK, false, locktime);
_capsLockState |= ( kCapsLockState_CapsLockEngaged | kCapsLockState_PowerCapsMapped );
_capsLockState &= ~kCapsLockState_CapsLockGoingDown;
}
}
_opened = true;
if ( _packetLock )
IOLockUnlock( _packetLock );
return true;
}
// --------------------------------------------------------------------------
//
// Method: setFKeyMode
//
// Purpose: set PowerBook function keys mode. An argument of zero means
// default mode, while an argument of 1 means F keys primary. The default
// mode will trigger functions as they appear on the keyboard. Older
// portables like WallStreet will not be affected since F keys don't
// have dual purposes. And of course external ADB keyboards are not affected.
void
AppleADBKeyboard::setFKeyMode(UInt8 mode)
{
UInt16 value;
IOByteCount length = sizeof( UInt16);
if (_hasDualModeFunctionKeys && adbDevice)
{
value = mode;
adbDevice->writeRegister(1, (UInt8 *)&value, &length);
setProperty(kIOHIDFKeyModeKey, mode, sizeof(UInt32));
}
}
// --------------------------------------------------------------------------
//
// Method: getFKeyMode
//
// Purpose: get PowerBook function keys mode. Also set up instance variable
// to keep track of whether this device has dual mode Function Keys or not.
SInt8
AppleADBKeyboard::getFKeyMode(void)
{
UInt8 adbdata[8], isFnPrimary = 0;
IOByteCount length = 8;
OSDictionary * FKeyModeParamDict;
SInt32 dictData = -1;
OSNumber * datan;
if (!adbDevice)
return -1; // -1 means feature unsupported
FKeyModeParamDict = OSDictionary::withCapacity(4);
bzero(adbdata, 8);
adbDevice->readRegister(1, adbdata, &length);
// [1] contains F key mode and [0] contains world region (ANSI, JIS, ISO)
_hasDualModeFunctionKeys = false;
if (adbdata[0]) //is 0 for external keyboards.
{
_hasDualModeFunctionKeys = true;
if (adbdata[1] & 0x0001)
isFnPrimary = 1;
setProperty(kIOHIDFKeyModeKey, isFnPrimary, sizeof(UInt32));
dictData = isFnPrimary;
}
datan = OSNumber::withNumber((unsigned long long) dictData, 32 ); // 0, 1, or -1
if( datan) {
FKeyModeParamDict->setObject( kIOHIDFKeyModeKey, datan);
datan->release();
}
if (_keyboardEventTarget)
{
((IOHIDSystem *)_keyboardEventTarget)->setParamProperties(FKeyModeParamDict);
//Merge new property with HIDSystem properties
}
FKeyModeParamDict->release();
if (_hasDualModeFunctionKeys)
return isFnPrimary;
else
return -1; // -1 means feature unsupported
}
// --------------------------------------------------------------------------
//
// Method: updateFKeyMap
//
// Purpose: Update the Fkey map in PMU with the values from the name
// registry, if needed. This came from the UpdateFKeyMap routine
// in OS9 Excelsior:OS:Wallyworld sources.
/* static */ void
AppleADBKeyboard::updateFKeyMap()
{
IORegistryEntry *devicetreeRegEntry;
typedef char keystrtype[4];
unsigned long buttonkeyvalue; // button translation value from name registry
unsigned char fkeyindex; // f key number being checked
keystrtype mapentry[] =
{
{"F0"}, {"F1"}, {"F2"}, {"F3"}, {"F4"}, {"F5"}, {"F6"},
{"F7"}, {"F8"}, {"F9"}, {"F10"}, {"F11"}, {"F12"},
};
devicetreeRegEntry = fromPath("mac-io/via-pmu/adb/keyboard", gIODTPlane);
if(devicetreeRegEntry != NULL) {
if (OSDynamicCast(OSData, devicetreeRegEntry->getProperty("AAPL,has-embedded-fn-keys"))) {
for (fkeyindex = 1; fkeyindex <= 12; fkeyindex++)
{
OSData *tmpData = OSDynamicCast(OSData, devicetreeRegEntry->getProperty(mapentry[fkeyindex]));
if(tmpData != NULL) {
memcpy(&buttonkeyvalue, (UInt8 *)tmpData->getBytesNoCopy(), sizeof(buttonkeyvalue));
setButtonTransTableEntry(fkeyindex, buttonkeyvalue);
}
}
}
devicetreeRegEntry->release(); //3172112
}
}
// ******************************************************************************************************
// Method: setButtonTransTableEntry
//
// Purpose: Used for local write to PMU adb interface (to set up PMU registers).
// This is essentially a writeToDevice routine (as in ADB device).
// The command string typically looks like (on the ApplePMU driver
// sendMiscCommand side) as an example:
// ADBcmd ADBcnt IntKbdCmd AutoPollbit ADBcount PMUcount selector F12key keyvalue
// 20 07 28 02 04 03 01 0c 8b
// ******************************************************************************************************
/* static */ void
AppleADBKeyboard::setButtonTransTableEntry(unsigned char fkeynum, unsigned char transvalue)
{
if (!adbDevice)
return;
// CMD: int KBD 2 reg 0 listen filled in by IOPMUADBController
UInt8 oBuffer[3]; // embed. data count =3 to PMU filled in by IOPMUADBController
oBuffer[0] = 1; // selector for PMU: set fkey command
oBuffer[1] = fkeynum; // fkey to set
oBuffer[2] = transvalue; // button code to attach
IOByteCount oLength = sizeof(oBuffer);
adbDevice->writeRegister(0, oBuffer, &oLength); // adbRegister = 0, IOADBDevice adds addr of 0x02 => 0x28
}
// **********************************************************************************
// interfaceID
//
// **********************************************************************************
UInt32 AppleADBKeyboard::interfaceID ( void )
{
return NX_EVS_DEVICE_INTERFACE_ADB;
}
// **********************************************************************************
// deviceType
//
// **********************************************************************************
UInt32 AppleADBKeyboard::deviceType ( void )
{
UInt32 id; //We need handler ID to remap adjustable JIS keyboard
IORegistryEntry *regEntry;
OSData *data = 0;
UInt32 *dataptr;
OSNumber *xml_handlerID;
if (!adbDevice)
return 2;
xml_handlerID = OSDynamicCast( OSNumber, getProperty("alt_handler_id"));
if (xml_handlerID)
{
id = xml_handlerID->unsigned32BitValue();
}
else
{
id = adbDevice->handlerID();
}
if (id == 18) //Adjustable JIS
{
_virtualmap[0x32] = 0x35; //tilde to ESC
_fnvirtualmap[0x32] = 0x35;
}
if ((id == 2) || (id == 3)) //External ADB keyboards may retain ID = 3 between boots
{
adbDevice->setHandlerID(3);
id = 2; // Gestalt.h has no idea what type 3 is since historically it was never used
}
else
if (id == 5) //ISO extended keyboard
{
adbDevice->setHandlerID(3);
id = 5;
}
if ((id == kgestaltPwrBkEKDomKbd) || (id == kgestaltPwrBkEKISOKbd) ||
(id == kgestaltPwrBkEKJISKbd) || (id == kgestaltPwrBk99JISKbd))
{
if( (regEntry = IORegistryEntry::fromPath( "/pci@f2000000/mac-io/via-pmu/adb/keyboard", gIODTPlane )))
{
data = OSDynamicCast(OSData, regEntry->getProperty( "keyboard-id", gIODTPlane, kIORegistryIterateRecursively ));
if (data)
{
dataptr = (UInt32 *)data->getBytesNoCopy();
id = *dataptr; //make sure no byte swapping
}
regEntry->release();
}
}
return id;
}
// **********************************************************************************
// setAlphaLockFeedback
// This is usually called on a call-out thread after the caps-lock key is pressed.
// ADB operations to PMU are synchronous, and this is must not be done
// on the call-out thread since that is the PMU driver workloop thread, and
// it will block itself.
//
// Therefore, we schedule the ADB write to disconnect the call-out thread
// and the one that initiates the ADB write.
//
// **********************************************************************************
void AppleADBKeyboard::setAlphaLockFeedback ( bool to )
{
if (to)
turnLEDon &= ~ADBKS_LED_CAPSLOCK; //Inverse logic applies here
else
turnLEDon |= ADBKS_LED_CAPSLOCK;
if ( !isInactive() && ledThreadCall) {
thread_call_enter(ledThreadCall);
}
}
void AppleADBKeyboard::setNumLockFeedback ( bool to )
{
if (to) //LED on means clear that bit
turnLEDon &= ~ ADBKS_LED_NUMLOCK;
else
turnLEDon |= ADBKS_LED_NUMLOCK;
if ( !isInactive() && ledThreadCall) {
thread_call_enter(ledThreadCall);
}
}
// **********************************************************************************
// asyncSetLEDFunc
//
// Called asynchronously to turn on/off the capslock and numlock LED
//
// **********************************************************************************
static void asyncSetLEDFunc ( thread_call_param_t self, thread_call_param_t )
{
UInt16 value;
IOByteCount length = sizeof( UInt16);
if (!((AppleADBKeyboard*)self)->adbDevice)
return;
value = ((AppleADBKeyboard*)self)->turnLEDon;
((AppleADBKeyboard*)self)->adbDevice->writeRegister(2, (UInt8 *)&value, &length);
}
/**********************************************************************
Get LED status by reading hardware. Register 2 has 16 bits.
Note that early ADB keyboards don't support this much data in
register 2, so it will come back all zeros and that is interpreted
as every LED being turned on.
**********************************************************************/
unsigned AppleADBKeyboard::getLEDStatus (void )
{
UInt8 data[8]; //8 bytes max for ADB read (talk) operation
IOByteCount length = 8;
if (!adbDevice)
return 0;
bzero(data, 8);
LEDStatus = 0;
adbDevice->readRegister(2, data, &length);
if ((data[1] & ADBKS_LED_NUMLOCK) == 0)
LEDStatus |= ADBKS_LED_NUMLOCK;
if ((data[1] & ADBKS_LED_CAPSLOCK) == 0)
LEDStatus |= ADBKS_LED_CAPSLOCK;
if ((data[1] & ADBKS_LED_SCROLLLOCK) == 0)
LEDStatus |= ADBKS_LED_SCROLLLOCK;
return LEDStatus;
}
// **********************************************************************************
// new_kbd_data
//
// **********************************************************************************
static void new_kbd_data ( IOService * us, UInt8 adbCommand, IOByteCount length, UInt8 * data )
{
((AppleADBKeyboard *)us)->packet(data,length,adbCommand);
}
// **********************************************************************************
// dispatchKeyboardEvent
//
// **********************************************************************************
static void AppleADBKeyboardReboot( thread_call_param_t arg, thread_call_param_t )
{
boot( 0, (int) arg, 0 );
}
void AppleADBKeyboard::dispatchKeyboardEvent(unsigned int keyCode,
/* direction */ bool goingDown,
/* timeStamp */ AbsoluteTime time)
{
char * pvirtualmap;
// ----------------------------------------------------------------
// RY: To better support the BSD (hacker) community, the behavior
// of the caps lock key needs to be altered. On portables, the
// keyboard behaves a bit differently than external ADB keyboards.
// The keyboard still dispatches ADBK_CAPSLOCK in line with the
// behavior of a locking capslock, but we have also noticed
// that power keyup events are issued that correspond to the
// events not dispatched by the capslock key.
//
// Here is the sequence of events turning on capslock and the LED:
//
// Physicall Event Dispatched Event
// --------------- ----------------
// CapsLock down capslock keydown event dispatched
// CapsLock up power keyup event dispatched
//
// Here is the sequence of events turning off capslock and the LED:
//
// Physicall Event Dispatched Event
// --------------- ----------------
// CapsLock down power keyup event dispatched
// CapsLock up capslock keyup event dispatched
//
// We can use this information to maintain the state of caps lock
// in SW and remove the designation of the CAPSLOCK as being locked
// by AppleADBKeyboard::doesKeyLock. This change in behavior will
// allow us to remap the capslock to control and appease the will
// of the hacker community.
//
// FYI: The LED will still continue to be controlled by the PMU
// and change regardless of the state in SW. While this may not
// be desired, it has been the position of the BSD management and
// WWDR that the developement community is not concerned.
if ( adbDevice && (adbDevice->handlerID() > 0xc0) && (keyCode == ADBK_CAPSLOCK) )
{
// The LED state check in AppleADBKeyboard::setParamProperties
// has still not taken place. Consume the key to avoid an
// inconsistant capslock state.
if ( !_opened )
{
_oneshotCAPSLOCK = goingDown;
return;
}
if (goingDown)
{
if ((_capsLockState & kCapsLockState_CapsLockEngaged) != 0)
{
return;
}
_capsLockState |= (kCapsLockState_CapsLockEngaged | kCapsLockState_CapsLockGoingDown);
}
else if (!goingDown)
{
if ((_capsLockState & kCapsLockState_CapsLockEngaged) == 0)
{
return;
}
else if ((_capsLockState & kCapsLockState_PowerCapsMapped) == 0)
{
super::dispatchKeyboardEvent(ADBK_CAPSLOCK, false, time);
super::dispatchKeyboardEvent(ADBK_CAPSLOCK, true, time);
}
else if ((_capsLockState & kCapsLockState_CapsLockGoingDown) == 0)
{
super::dispatchKeyboardEvent(ADBK_CAPSLOCK, true, time);
}
_capsLockState &= ~(kCapsLockState_CapsLockEngaged | kCapsLockState_CapsLockGoingDown);
}
_capsLockState &= ~kCapsLockState_PowerCapsMapped;
}
// RY: Apply special handling for case of POWER keyup during CAPSLOCK key sequence.
else if ( (_capsLockState & kCapsLockState_CapsLockEngaged) &&
!(_capsLockState & kCapsLockState_PowerEngaged) &&
(keyCode == ADBK_POWER) && (goingDown == false) )
{
if (((_capsLockState & kCapsLockState_CapsLockGoingDown) != 0) &&
((_capsLockState & kCapsLockState_PowerCapsMapped) == 0))
{
_capsLockState |= kCapsLockState_PowerCapsMapped;
_capsLockState &= ~kCapsLockState_CapsLockGoingDown;
keyCode = ADBK_CAPSLOCK;
}
else if (((_capsLockState & kCapsLockState_CapsLockGoingDown) == 0) &&
((_capsLockState & kCapsLockState_PowerCapsMapped) != 0))
{
_capsLockState |= kCapsLockState_CapsLockGoingDown;
goingDown = true;
keyCode = ADBK_CAPSLOCK;
}
}
// ----------------------------------------------------------------
pvirtualmap = _virtualmap;
if ((_stickymodeON) && (_sticky_fn_ON))
{
pvirtualmap = _fnvirtualmap;
}
if( !goingDown && programmerKey) {
programmerKey = false;
EVK_KEYUP( ADBK_CONTROL, _keyState);
SUB_ABSOLUTETIME( &time, &programmerKeyTime );
if( CMP_ABSOLUTETIME( &time, &rebootTime) >= 0) {
if ( rebootThreadCall ) {
thread_call_enter( rebootThreadCall );
}
} else if( CMP_ABSOLUTETIME( &time, &debuggerTime) >= 0) {
Debugger("Programmer Key");
}
}
else if ( keyCode == ADBK_POWER )
{
if ( goingDown )
_capsLockState |= kCapsLockState_PowerEngaged;
else
_capsLockState &= ~kCapsLockState_PowerEngaged;
if (EVK_IS_KEYDOWN( ADBK_CONTROL, _keyState)) {
if( !programmerKey) {
programmerKey = true;
programmerKeyTime = time;
}
return;
}
else if (deviceFlags() & NX_SECONDARYFNMASK)
{
//On PowerBooks fn + CMD creates a POWER keycode, so change it now to CMD code
keyCode = ADBK_FLOWER;
//If fn is released before CMD key, we need to fake another CMD key later,
// but if CMD key is released first then we don't need to do anything else
// later since the CMD key will still generate a POWER key-up when fn is down.
_fn_key_invoked_power = goingDown;
}
else if (_fn_key_invoked_power)
{
//If fn key is released before CMD key, then when CMD key is released
// the keycode is still 7f (POWER). But that makes CMD key look like
// it is stuck in the down position from line above, so fake CMD up now
keyCode = ADBK_FLOWER;
_fn_key_invoked_power = false;
}
}
if ((_enable_fwd_delete) && (deviceFlags() & NX_SECONDARYFNMASK))
{
if (keyCode == ADBK_DELETE)
{
keyCode = ADBK_FORWARD_DELETE;
_fwd_delete_down = goingDown;
}
else if (keyCode == ADBK_PBFNKEY)
{
if ((!goingDown) && (_fwd_delete_down))
{
super::dispatchKeyboardEvent( pvirtualmap[ADBK_FORWARD_DELETE],
false, time );
_fwd_delete_down = false;
}
}
}
// RY: For portable keyboards, we need to convert right hand modifiers
// keycodes to left hand keycodes. This is necessary because said
// keycodes can only be generated with the use of the fn modifier. To
// elminate confusion, fn + modifier should be used to differentiate
// the keystrokes.
if ( adbDevice && (adbDevice->handlerID() > 0xc0))
{
switch ( keyCode )
{
case ADBK_CONTROL_R:
keyCode = ADBK_CONTROL;
break;
case ADBK_SHIFT_R:
keyCode = ADBK_SHIFT;
break;
case ADBK_OPTION_R:
keyCode = ADBK_OPTION;
break;
}
}
super::dispatchKeyboardEvent( pvirtualmap[keyCode], goingDown, time );
}
// **********************************************************************************
// packet
//
// **********************************************************************************
IOReturn AppleADBKeyboard::packet (UInt8 * data, IOByteCount, UInt8 adbCommand )
{
unsigned int keycode1, keycode2;
bool down;
AbsoluteTime now;
//---Since packet is re-entrant, lock around it - [3418665] ---
if ( _packetLock )
IOLockLock( _packetLock );
keycode1 = *data;
down = ((keycode1 & 0x80) == 0);
keycode1 &= 0x7f;
if(keycode1 == 0x7e) keycode1 = ADBK_POWER;
clock_get_uptime(&now);
setTimeLastNonmodKeydown(now, keycode1);
dispatchKeyboardEvent(keycode1,down,now);
keycode2 = *(data + 1);
if( keycode2 != 0xff ) {
down = ((keycode2 & 0x80) == 0);
keycode2 &= 0x7f;
if( keycode2 == 0x7e) keycode2 = ADBK_POWER;
if( (keycode1 != ADBK_POWER) || (keycode2 != ADBK_POWER))
dispatchKeyboardEvent(keycode2,down,now);
}
if ( _packetLock )
IOLockUnlock( _packetLock );
return kIOReturnSuccess;
}
//This is needed by trackpads to correct jitter when gestures
// are enabled.
void AppleADBKeyboard::setTimeLastNonmodKeydown (AbsoluteTime now, unsigned int keycode)
{
switch (_virtualmap[keycode]) {
case 0x3b: //left control, not raw
case ADBK_FLOWER:
case ADBK_SHIFT:
case ADBK_SHIFT_R:
case ADBK_CAPSLOCK:
case ADBK_OPTION:
case ADBK_OPTION_R:
case ADBK_PBFNKEY: //PowerBook secondary fn key
break;
default:
_lastkeydown = now;
break;
}
}
AbsoluteTime AppleADBKeyboard::getTimeLastNonmodKeydown (void)
{
if (CMP_ABSOLUTETIME( &_lastkeyCGEvent, &_lastkeydown) > 0)
return _lastkeyCGEvent; //time of autorepeat
else
return _lastkeydown; //time of physical keypress
}
//The only reason this method is subclassed is to access autorepeat
// events without changing IOHIKeyboard's public APIs. All normal
// as well as autorepeated keys must come through here
void AppleADBKeyboard::keyboardEvent(unsigned eventType,
/* flags */ unsigned flags,
/* keyCode */ unsigned keyCode,
/* charCode */ unsigned charCode,
/* charSet */ unsigned charSet,
/* originalCharCode */ unsigned origCharCode,
/* originalCharSet */ unsigned origCharSet)
{
//only save time if autorepeated key
//save my own time, ignore super's time.
switch ( _codeToRepeat)
{
case ADBK_F9:
case ADBK_F10:
case ADBK_F11:
case ADBK_F12:
case (unsigned) -1:
break;
default:
clock_get_uptime(&_lastkeyCGEvent);
break;
}
super::keyboardEvent( eventType, flags, keyCode, charCode, charSet,
origCharCode, origCharSet);
}
IOReturn AppleADBKeyboard::callPlatformFunction(const OSSymbol *functionName,
bool waitForFunction,
void *param1, void *param2,
void *param3, void *param4)
{
if (functionName == _get_last_keydown)
{
AbsoluteTime *timeptr;
timeptr = (AbsoluteTime *)param1;
*timeptr = getTimeLastNonmodKeydown();
return kIOReturnSuccess;
}
if (functionName == _get_handler_id)
{
UInt32 *id;
id = (UInt32 *)param1;
*id = deviceType();
return kIOReturnSuccess;
}
if (functionName == _get_device_flags)
{
UInt32 *id;
id = (UInt32 *)param1;
//TBD: check if keyboard is external or embedded?
*id = deviceFlags();
return kIOReturnSuccess;
}
return kIOReturnBadArgument;
}
// **********************************************************************************
// maxKeyCodes
//
// **********************************************************************************
UInt32 AppleADBKeyboard::maxKeyCodes ( void )
{
return 0x80;
}
//Get key values from ev_keymap.h
bool AppleADBKeyboard::doesKeyLock ( unsigned key)
{
switch (key) {
case NX_KEYTYPE_CAPS_LOCK:
if ( adbDevice && (adbDevice->handlerID() > 0xc0))
return false;
else
return true;
case NX_KEYTYPE_NUM_LOCK:
return false;
default:
return false;
}
}
// **********************************************************************************
// setParamProperties
//
// **********************************************************************************
IOReturn AppleADBKeyboard::setParamProperties( OSDictionary * dict )
{
OSNumber *datan;
if (_keybrdLock)
IOLockLock (_keybrdLock);
if (datan = OSDynamicCast(OSNumber, dict->getObject(kIOHIDFKeyModeKey)))
{
bool theFkeyMode;
theFkeyMode = (bool) datan->unsigned32BitValue();
setFKeyMode(theFkeyMode); //This calls setProperty too
}
if (datan = OSDynamicCast(OSNumber, dict->getObject(kIOHIDStickyKeysOnKey)))
{
UInt32 propertydata;
propertydata = (bool) datan->unsigned32BitValue();;
if (propertydata)
{
_stickymodeON = true;
}
else
{
_stickymodeON = false;
}
}