2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * 12 Nov 1998 suurballe Created.
26 #include <IOKit/IOSyncer.h>
27 #include "IOPMUADBController.h"
29 #define super IOADBController
30 OSDefineMetaClassAndStructors(IOPMUADBController
, IOADBController
)
32 // **********************************************************************************
35 // **********************************************************************************
36 IOService
* IOPMUADBController::probe( IOService
* provider
, SInt32
* score
)
38 if (super::probe(provider
, score
) == NULL
)
41 // this adb controller must interface with the pmu, so let's check if it is of the right type:
42 // so in any case if this is a powerbook G3 1998 or 1999 it has a pmu so:
43 if (IODTMatchNubWithKeys(getPlatform()->getProvider(), "'AAPL,PowerBook1998'") ||
44 IODTMatchNubWithKeys(getPlatform()->getProvider(), "'PowerBook1,1'"))
47 // If it is a different machine the compatible property will tell us if it is a pmu-driven
49 OSData
*kl
= OSDynamicCast(OSData
, provider
->getProperty("compatible"));
50 if ((kl
!= NULL
) && kl
->isEqualTo("pmu", 3))
53 // In all the other cases we do not handle it:
57 // **********************************************************************************
60 // **********************************************************************************
61 bool IOPMUADBController::start ( IOService
* nub
)
63 // Wait for the PMU to show up:
64 PMUdriver
= waitForService(serviceMatching("ApplePMU"));
66 // All the commands in this file will generate an interrupt.
67 // since the interrupt is the logical conclusion of those commands
68 // we need a syncer to sincronize the begin/end of these functions:
69 waitingForData
= NULL
;
71 // Registers for the two interrupts that needs to handle:
72 if (PMUdriver
->callPlatformFunction("registerForPMUInterrupts", true, (void*)kPMUADBint
, (void*)handleADBInterrupt
, (void*)this, NULL
) != kIOReturnSuccess
) {
73 #ifdef VERBOSE_LOGS_ON
74 IOLog("IOPMUADBController::start registerForPMUInterrupts kPMUADBint fails\n");
75 #endif // VERBOSE_LOGS_ON
80 // Creates the mutex lock to protect the clients list:
81 requestMutexLock
= NULL
;
82 requestMutexLock
= IOLockAlloc();
83 if (!requestMutexLock
)
86 // This happens last (while the most common place is the begin) because
87 // trhe superclass may need the services of the functions above.
88 if( !super::start(nub
))
94 // **********************************************************************************
97 // **********************************************************************************
98 void IOPMUADBController::free ( )
100 // Releases the mutex lock used to protect the clients lists:
101 if (requestMutexLock
!= NULL
) {
102 IOLockFree (requestMutexLock
);
103 requestMutexLock
= NULL
;
106 // And removes the interrupt handler:
107 if (PMUdriver
!= NULL
)
108 PMUdriver
->callPlatformFunction("deRegisterClient", true, (void*)this, (void*)kPMUADBint
, NULL
, NULL
);
111 // **********************************************************************************
112 // localSendMiscCommand
114 // **********************************************************************************
115 IOReturn
IOPMUADBController::localSendMiscCommand(int command
, IOByteCount sLength
, UInt8
*sBuffer
)
117 IOReturn returnValue
= kIOReturnError
;
118 IOByteCount rLength
= 1;
121 // The poupose of this method is to free us from the pain to create a parameter block each time
122 // we wish to talk to the pmu:
123 SendMiscCommandParameterBlock prmBlock
= {command
, sLength
, sBuffer
, &rLength
, &rBuffer
};
125 #ifdef VERBOSE_LOGS_ON
126 IOLog("ApplePMUInterface::localSendMiscCommand 0x%02x %d 0x%08lx 0x%08lx 0x%08lx\n",
127 command
, sLength
, sBuffer
, rLength
, rBuffer
);
130 if (PMUdriver
!= NULL
) {
131 #ifdef VERBOSE_LOGS_ON
132 IOLog("IOPMUADBController::localSendMiscCommand calling PMUdriver->callPlatformFunction\n");
134 returnValue
= PMUdriver
->callPlatformFunction("sendMiscCommand", true, (void*)&prmBlock
, NULL
, NULL
, NULL
);
137 // If we are here we do not have a dreive to talk to:
138 #ifdef VERBOSE_LOGS_ON
139 IOLog("IOPMUADBController::localSendMiscCommand end 0x%08lx\n", returnValue
);
145 // **********************************************************************************
146 // this is the interrupt handler for all ADB interrupts:
148 // **********************************************************************************
151 IOPMUADBController::handleADBInterrupt(IOService
*client
, UInt8 interruptMask
, UInt32 length
, UInt8
*buffer
)
153 IOPMUADBController
*myThis
= OSDynamicCast(IOPMUADBController
, client
);
155 // Check if we are the right client for this interrupt:
159 if ((interruptMask
& kPMUautopoll
) && (myThis
->autopollOn
))
160 autopollHandler(client
, buffer
[0], length
- 1, buffer
+ 1); // yes, call adb input handler
162 if (myThis
->waitingForData
!= NULL
) {
163 // Complets the adb transaction
164 myThis
->dataLen
= length
- 1;
165 bcopy(buffer
+ 1, myThis
->dataBuffer
, myThis
->dataLen
);
166 myThis
->waitingForData
->signal();
172 // **********************************************************************************
175 // **********************************************************************************
176 IOReturn
IOPMUADBController::cancelAllIO ( void )
178 if (waitingForData
!= NULL
) {
179 dataLen
= 0; // read fails with error, write fails quietly
180 waitingForData
->signal();
186 // **********************************************************************************
189 // **********************************************************************************
190 IOReturn
IOPMUADBController::setAutoPollPeriod ( int )
192 return kPMUNotSupported
;
196 // **********************************************************************************
199 // **********************************************************************************
200 IOReturn
IOPMUADBController::getAutoPollPeriod ( int * )
202 return kPMUNotSupported
;
206 // **********************************************************************************
209 // **********************************************************************************
210 IOReturn
IOPMUADBController::setAutoPollList ( UInt16 PollBitField
)
212 pollList
= PollBitField
; // remember the new poll list
217 oBuffer
[0] = 0; // Byte count in the resto of the command
218 oBuffer
[1] = 0x86; // adb Command op.
219 oBuffer
[2] = (UInt8
)(PollBitField
>> 8); // ??
220 oBuffer
[3] = (UInt8
)(PollBitField
& 0xff); // ??
222 localSendMiscCommand (kPMUpMgrADB
, 4, oBuffer
);
228 // **********************************************************************************
231 // **********************************************************************************
232 IOReturn
IOPMUADBController::getAutoPollList ( UInt16
* activeAddressMask
)
234 *activeAddressMask
= pollList
;
239 // **********************************************************************************
242 // **********************************************************************************
243 IOReturn
IOPMUADBController::setAutoPollEnable ( bool enable
)
249 if ( enable
) { // enabling autopoll
252 oBuffer
[2] = (UInt8
)(pollList
>> 8);
253 oBuffer
[3] = (UInt8
)(pollList
& 0xff);
255 localSendMiscCommand (kPMUpMgrADB
, 4, oBuffer
);
257 else { // disabling autopoll;
258 /* Waits one second for the trackpads to be up (this is needed only in old machines)
259 This is placed here because this is the fist call at wake. */
260 if (IODTMatchNubWithKeys(getPlatform()->getProvider(), "'PowerBook1,1'") ||
261 IODTMatchNubWithKeys(getPlatform()->getProvider(), "'AAPL,PowerBook1998'"))
264 localSendMiscCommand (kPMUpMgrADBoff
, 0, NULL
);
271 // **********************************************************************************
274 // **********************************************************************************
275 IOReturn
IOPMUADBController::resetBus ( void )
277 if (requestMutexLock
!= NULL
)
278 IOLockLock(requestMutexLock
);
282 oBuffer
[0] = kPMUResetADBBus
;
286 // Reset bus needs to wait for the interrupt to terminate the transaction:
287 waitingForData
= IOSyncer::create();
288 localSendMiscCommand (kPMUpMgrADB
, 3, oBuffer
);
289 waitingForData
->wait(); // wait till done
292 if (requestMutexLock
!= NULL
)
293 IOLockUnlock(requestMutexLock
);
295 /* Waits one second for the trackpads to be up (this is needed only in old machines) */
296 if (IODTMatchNubWithKeys(getPlatform()->getProvider(), "'PowerBook1,1'") ||
297 IODTMatchNubWithKeys(getPlatform()->getProvider(), "'AAPL,PowerBook1998'"))
304 // **********************************************************************************
307 // **********************************************************************************
308 IOReturn
IOPMUADBController::flushDevice ( IOADBAddress address
)
310 if (requestMutexLock
!= NULL
)
311 IOLockLock(requestMutexLock
);
315 oBuffer
[0] = kPMUFlushADB
| (address
<< kPMUADBAddressField
);
316 oBuffer
[1] = ( autopollOn
? 2 : 0 );
319 // flush device needs to wait for the interrupt to terminate the transaction
320 waitingForData
= IOSyncer::create();
321 localSendMiscCommand (kPMUpMgrADB
, 3, oBuffer
);
322 waitingForData
->wait(); // wait till done
325 if (requestMutexLock
!= NULL
)
326 IOLockUnlock(requestMutexLock
);
332 // **********************************************************************************
335 // The length parameter is ignored on entry. It is set on exit to reflect
336 // the number of bytes read from the device.
337 // **********************************************************************************
338 IOReturn
IOPMUADBController::readFromDevice ( IOADBAddress address
, IOADBRegister adbRegister
,
339 UInt8
* data
, IOByteCount
* length
)
341 if ( (length
== NULL
) || (data
== NULL
) ) {
342 return kPMUParameterError
;
345 if (requestMutexLock
!= NULL
)
346 IOLockLock(requestMutexLock
);
350 oBuffer
[0] = kPMUReadADB
| (address
<< kPMUADBAddressField
) | (adbRegister
);
351 oBuffer
[1] = ( autopollOn
? 2 : 0 );
354 // read from device needs to wait for the interrupt to terminate the transaction
355 // and to obtain the data from the device.
356 waitingForData
= IOSyncer::create();
357 localSendMiscCommand (kPMUpMgrADB
, 3, oBuffer
);
358 waitingForData
->wait(); // wait till done
361 // set caller's length
362 *length
= (dataLen
< *length
? dataLen
: *length
);
363 bcopy(dataBuffer
, data
, *length
);
365 if (requestMutexLock
!= NULL
)
366 IOLockUnlock(requestMutexLock
);
368 if (dataLen
== 0 ) { // nothing read; device isn't there
369 return ADB_RET_NOTPRESENT
;
376 // **********************************************************************************
379 // **********************************************************************************
380 IOReturn
IOPMUADBController::writeToDevice ( IOADBAddress address
, IOADBRegister adbRegister
,
381 UInt8
* data
, IOByteCount
* length
)
383 // Last check on * length > (252): since the pmu registers are 8 bit
384 // and the buffer has the first 3 bytes used for the standard paramters
385 // the max lenght can not be more than 252 bytes.
386 if ( (* length
== 0) || (data
== NULL
) || (* length
> 252) )
388 return kPMUParameterError
;
392 return kPMUNoError
; // for now let's ignore these ...
394 if (requestMutexLock
!= NULL
)
395 IOLockLock(requestMutexLock
);
399 oBuffer
[0] = kPMUWriteADB
| (address
<< kPMUADBAddressField
) | (adbRegister
);
400 oBuffer
[1] = ( autopollOn
? 2 : 0 );
401 oBuffer
[2] = *length
;
402 bcopy(data
, &oBuffer
[3], *length
);
404 // write to the device needs to wait for the interrupt to terminate the transaction
405 waitingForData
= IOSyncer::create();
406 localSendMiscCommand (kPMUpMgrADB
, 3 + *length
, oBuffer
);
407 waitingForData
->wait();
410 if (requestMutexLock
!= NULL
)
411 IOLockUnlock(requestMutexLock
);