2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IODMAEventSource.h>
30 #include <IOKit/IOService.h>
32 #include "IOKitKernelInternal.h"
35 #define super IOEventSource
36 OSDefineMetaClassAndStructors(IODMAEventSource
, IOEventSource
);
38 bool IODMAEventSource::init(OSObject
*inOwner
,
39 IOService
*inProvider
,
41 Action inNotification
,
46 if (!super::init(inOwner
)) return false;
48 if (inProvider
== 0) return false;
50 dmaProvider
= inProvider
;
51 dmaIndex
= 0xFFFFFFFF;
52 dmaCompletionAction
= inCompletion
;
53 dmaNotificationAction
= inNotification
;
55 dmaController
= IODMAController::getController(dmaProvider
, inDMAIndex
);
56 if (dmaController
== 0) return false;
57 dmaController
->retain();
59 result
= dmaController
->initDMAChannel(dmaProvider
, this, &dmaIndex
, inDMAIndex
);
60 if (result
!= kIOReturnSuccess
) return false;
62 queue_init(&dmaCommandsCompleted
);
63 dmaCommandsCompletedLock
= IOSimpleLockAlloc();
68 void IODMAEventSource::free()
70 if (dmaCommandsCompletedLock
!= NULL
) IOSimpleLockFree(dmaCommandsCompletedLock
);
74 IODMAEventSource
*IODMAEventSource::dmaEventSource(OSObject
*inOwner
,
75 IOService
*inProvider
,
77 Action inNotification
,
80 IODMAEventSource
*dmaES
= new IODMAEventSource
;
82 if (dmaES
&& !dmaES
->init(inOwner
, inProvider
, inCompletion
, inNotification
, inDMAIndex
)) {
90 IOReturn
IODMAEventSource::startDMACommand(IODMACommand
*dmaCommand
, IODirection direction
, IOByteCount byteCount
, IOByteCount byteOffset
)
94 if ((dmaController
== 0) || (dmaIndex
== 0xFFFFFFFF)) return kIOReturnError
;
96 if (dmaSynchBusy
) return kIOReturnBusy
;
98 if (dmaCompletionAction
== 0) dmaSynchBusy
= true;
100 result
= dmaController
->startDMACommand(dmaIndex
, dmaCommand
, direction
, byteCount
, byteOffset
);
102 if (result
!= kIOReturnSuccess
) {
103 dmaSynchBusy
= false;
107 while (dmaSynchBusy
) sleepGate(&dmaSynchBusy
, THREAD_UNINT
);
109 return kIOReturnSuccess
;
112 IOReturn
IODMAEventSource::stopDMACommand(bool flush
, uint64_t timeout
)
114 if ((dmaController
== 0) || (dmaIndex
== 0xFFFFFFFF)) return kIOReturnError
;
116 return dmaController
->stopDMACommand(dmaIndex
, flush
, timeout
);
120 IOReturn
IODMAEventSource::queryDMACommand(IODMACommand
**dmaCommand
, IOByteCount
*transferCount
, bool waitForIdle
)
122 if ((dmaController
== 0) || (dmaIndex
== 0xFFFFFFFF)) return kIOReturnError
;
124 return dmaController
->queryDMACommand(dmaIndex
, dmaCommand
, transferCount
, waitForIdle
);
128 IOByteCount
IODMAEventSource::getFIFODepth(IODirection direction
)
130 if ((dmaController
== 0) || (dmaIndex
== 0xFFFFFFFF)) return 0;
132 return dmaController
->getFIFODepth(dmaIndex
, direction
);
136 IOReturn
IODMAEventSource::setFIFODepth(IOByteCount depth
)
138 if ((dmaController
== 0) || (dmaIndex
== 0xFFFFFFFF)) return kIOReturnError
;
140 return dmaController
->setFIFODepth(dmaIndex
, depth
);
144 IOByteCount
IODMAEventSource::validFIFODepth(IOByteCount depth
, IODirection direction
)
146 if ((dmaController
== 0) || (dmaIndex
== 0xFFFFFFFF)) return kIOReturnError
;
148 return dmaController
->validFIFODepth(dmaIndex
, depth
, direction
);
154 bool IODMAEventSource::checkForWork(void)
156 IODMACommand
*dmaCommand
= NULL
;
159 IOSimpleLockLock(dmaCommandsCompletedLock
);
160 work
= !queue_empty(&dmaCommandsCompleted
);
162 queue_remove_first(&dmaCommandsCompleted
, dmaCommand
, IODMACommand
*, fCommandChain
);
163 again
= !queue_empty(&dmaCommandsCompleted
);
167 IOSimpleLockUnlock(dmaCommandsCompletedLock
);
170 (*dmaCompletionAction
)(owner
, this, dmaCommand
, dmaCommand
->reserved
->fStatus
, dmaCommand
->reserved
->fActualByteCount
, dmaCommand
->reserved
->fTimeStamp
);
176 void IODMAEventSource::completeDMACommand(IODMACommand
*dmaCommand
)
178 if (dmaCompletionAction
!= 0) {
179 IOSimpleLockLock(dmaCommandsCompletedLock
);
180 queue_enter(&dmaCommandsCompleted
, dmaCommand
, IODMACommand
*, fCommandChain
);
181 IOSimpleLockUnlock(dmaCommandsCompletedLock
);
183 signalWorkAvailable();
185 dmaSynchBusy
= false;
186 wakeupGate(&dmaSynchBusy
, true);
190 void IODMAEventSource::notifyDMACommand(IODMACommand
*dmaCommand
, IOReturn status
, IOByteCount actualByteCount
, AbsoluteTime timeStamp
)
192 dmaCommand
->reserved
->fStatus
= status
;
193 dmaCommand
->reserved
->fActualByteCount
= actualByteCount
;
194 dmaCommand
->reserved
->fTimeStamp
= timeStamp
;
196 if (dmaNotificationAction
!= 0) (*dmaNotificationAction
)(owner
, this, dmaCommand
, status
, actualByteCount
, timeStamp
);
199 IOReturn
IODMAEventSource::setDMAConfig(UInt32 newReqIndex
)
201 return dmaController
->setDMAConfig(dmaIndex
, dmaProvider
, newReqIndex
);
204 bool IODMAEventSource::validDMAConfig(UInt32 newReqIndex
)
206 return dmaController
->validDMAConfig(dmaIndex
, dmaProvider
, newReqIndex
);