1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions *
4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions *
6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions *
15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions *
18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions * limitations under the License.
25*1b191cb5SApple OSS Distributions *
26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions */
28*1b191cb5SApple OSS Distributions
29*1b191cb5SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*1b191cb5SApple OSS Distributions
31*1b191cb5SApple OSS Distributions #include <IOKit/IODMAEventSource.h>
32*1b191cb5SApple OSS Distributions #include <IOKit/IOService.h>
33*1b191cb5SApple OSS Distributions
34*1b191cb5SApple OSS Distributions #include "IOKitKernelInternal.h"
35*1b191cb5SApple OSS Distributions
36*1b191cb5SApple OSS Distributions
37*1b191cb5SApple OSS Distributions #define super IOEventSource
38*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructors(IODMAEventSource, IOEventSource);
39*1b191cb5SApple OSS Distributions
40*1b191cb5SApple OSS Distributions bool
init(OSObject * inOwner,IOService * inProvider,Action inCompletion,Action inNotification,UInt32 inDMAIndex)41*1b191cb5SApple OSS Distributions IODMAEventSource::init(OSObject *inOwner,
42*1b191cb5SApple OSS Distributions IOService *inProvider,
43*1b191cb5SApple OSS Distributions Action inCompletion,
44*1b191cb5SApple OSS Distributions Action inNotification,
45*1b191cb5SApple OSS Distributions UInt32 inDMAIndex)
46*1b191cb5SApple OSS Distributions {
47*1b191cb5SApple OSS Distributions IOReturn result;
48*1b191cb5SApple OSS Distributions
49*1b191cb5SApple OSS Distributions if (!super::init(inOwner)) {
50*1b191cb5SApple OSS Distributions return false;
51*1b191cb5SApple OSS Distributions }
52*1b191cb5SApple OSS Distributions
53*1b191cb5SApple OSS Distributions if (inProvider == NULL) {
54*1b191cb5SApple OSS Distributions return false;
55*1b191cb5SApple OSS Distributions }
56*1b191cb5SApple OSS Distributions
57*1b191cb5SApple OSS Distributions dmaProvider = inProvider;
58*1b191cb5SApple OSS Distributions dmaIndex = 0xFFFFFFFF;
59*1b191cb5SApple OSS Distributions dmaCompletionAction = inCompletion;
60*1b191cb5SApple OSS Distributions dmaNotificationAction = inNotification;
61*1b191cb5SApple OSS Distributions
62*1b191cb5SApple OSS Distributions dmaController.reset(IODMAController::getController(dmaProvider, inDMAIndex), OSRetain);
63*1b191cb5SApple OSS Distributions if (dmaController == NULL) {
64*1b191cb5SApple OSS Distributions return false;
65*1b191cb5SApple OSS Distributions }
66*1b191cb5SApple OSS Distributions
67*1b191cb5SApple OSS Distributions result = dmaController->initDMAChannel(dmaProvider, this, &dmaIndex, inDMAIndex);
68*1b191cb5SApple OSS Distributions if (result != kIOReturnSuccess) {
69*1b191cb5SApple OSS Distributions return false;
70*1b191cb5SApple OSS Distributions }
71*1b191cb5SApple OSS Distributions
72*1b191cb5SApple OSS Distributions queue_init(&dmaCommandsCompleted);
73*1b191cb5SApple OSS Distributions dmaCommandsCompletedLock = IOSimpleLockAlloc();
74*1b191cb5SApple OSS Distributions
75*1b191cb5SApple OSS Distributions return true;
76*1b191cb5SApple OSS Distributions }
77*1b191cb5SApple OSS Distributions
78*1b191cb5SApple OSS Distributions void
free()79*1b191cb5SApple OSS Distributions IODMAEventSource::free()
80*1b191cb5SApple OSS Distributions {
81*1b191cb5SApple OSS Distributions if (dmaCommandsCompletedLock != NULL) {
82*1b191cb5SApple OSS Distributions IOSimpleLockFree(dmaCommandsCompletedLock);
83*1b191cb5SApple OSS Distributions }
84*1b191cb5SApple OSS Distributions super::free();
85*1b191cb5SApple OSS Distributions }
86*1b191cb5SApple OSS Distributions
87*1b191cb5SApple OSS Distributions OSSharedPtr<IODMAEventSource>
dmaEventSource(OSObject * inOwner,IOService * inProvider,Action inCompletion,Action inNotification,UInt32 inDMAIndex)88*1b191cb5SApple OSS Distributions IODMAEventSource::dmaEventSource(OSObject *inOwner,
89*1b191cb5SApple OSS Distributions IOService *inProvider,
90*1b191cb5SApple OSS Distributions Action inCompletion,
91*1b191cb5SApple OSS Distributions Action inNotification,
92*1b191cb5SApple OSS Distributions UInt32 inDMAIndex)
93*1b191cb5SApple OSS Distributions {
94*1b191cb5SApple OSS Distributions OSSharedPtr<IODMAEventSource> dmaES = OSMakeShared<IODMAEventSource>();
95*1b191cb5SApple OSS Distributions
96*1b191cb5SApple OSS Distributions if (dmaES && !dmaES->init(inOwner, inProvider, inCompletion, inNotification, inDMAIndex)) {
97*1b191cb5SApple OSS Distributions return nullptr;
98*1b191cb5SApple OSS Distributions }
99*1b191cb5SApple OSS Distributions
100*1b191cb5SApple OSS Distributions return dmaES;
101*1b191cb5SApple OSS Distributions }
102*1b191cb5SApple OSS Distributions
103*1b191cb5SApple OSS Distributions IOReturn
startDMACommand(IODMACommand * dmaCommand,IODirection direction,IOByteCount byteCount,IOByteCount byteOffset)104*1b191cb5SApple OSS Distributions IODMAEventSource::startDMACommand(IODMACommand *dmaCommand, IODirection direction, IOByteCount byteCount, IOByteCount byteOffset)
105*1b191cb5SApple OSS Distributions {
106*1b191cb5SApple OSS Distributions IOReturn result;
107*1b191cb5SApple OSS Distributions
108*1b191cb5SApple OSS Distributions if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
109*1b191cb5SApple OSS Distributions return kIOReturnError;
110*1b191cb5SApple OSS Distributions }
111*1b191cb5SApple OSS Distributions
112*1b191cb5SApple OSS Distributions if (dmaSynchBusy) {
113*1b191cb5SApple OSS Distributions return kIOReturnBusy;
114*1b191cb5SApple OSS Distributions }
115*1b191cb5SApple OSS Distributions
116*1b191cb5SApple OSS Distributions if (dmaCompletionAction == NULL) {
117*1b191cb5SApple OSS Distributions dmaSynchBusy = true;
118*1b191cb5SApple OSS Distributions }
119*1b191cb5SApple OSS Distributions
120*1b191cb5SApple OSS Distributions result = dmaController->startDMACommand(dmaIndex, dmaCommand, direction, byteCount, byteOffset);
121*1b191cb5SApple OSS Distributions
122*1b191cb5SApple OSS Distributions if (result != kIOReturnSuccess) {
123*1b191cb5SApple OSS Distributions dmaSynchBusy = false;
124*1b191cb5SApple OSS Distributions return result;
125*1b191cb5SApple OSS Distributions }
126*1b191cb5SApple OSS Distributions
127*1b191cb5SApple OSS Distributions while (dmaSynchBusy) {
128*1b191cb5SApple OSS Distributions sleepGate(&dmaSynchBusy, THREAD_UNINT);
129*1b191cb5SApple OSS Distributions }
130*1b191cb5SApple OSS Distributions
131*1b191cb5SApple OSS Distributions return kIOReturnSuccess;
132*1b191cb5SApple OSS Distributions }
133*1b191cb5SApple OSS Distributions
134*1b191cb5SApple OSS Distributions IOReturn
stopDMACommand(bool flush,uint64_t timeout)135*1b191cb5SApple OSS Distributions IODMAEventSource::stopDMACommand(bool flush, uint64_t timeout)
136*1b191cb5SApple OSS Distributions {
137*1b191cb5SApple OSS Distributions if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
138*1b191cb5SApple OSS Distributions return kIOReturnError;
139*1b191cb5SApple OSS Distributions }
140*1b191cb5SApple OSS Distributions
141*1b191cb5SApple OSS Distributions return dmaController->stopDMACommand(dmaIndex, flush, timeout);
142*1b191cb5SApple OSS Distributions }
143*1b191cb5SApple OSS Distributions
144*1b191cb5SApple OSS Distributions
145*1b191cb5SApple OSS Distributions IOReturn
queryDMACommand(IODMACommand ** dmaCommand,IOByteCount * transferCount,bool waitForIdle)146*1b191cb5SApple OSS Distributions IODMAEventSource::queryDMACommand(IODMACommand **dmaCommand, IOByteCount *transferCount, bool waitForIdle)
147*1b191cb5SApple OSS Distributions {
148*1b191cb5SApple OSS Distributions if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
149*1b191cb5SApple OSS Distributions return kIOReturnError;
150*1b191cb5SApple OSS Distributions }
151*1b191cb5SApple OSS Distributions
152*1b191cb5SApple OSS Distributions return dmaController->queryDMACommand(dmaIndex, dmaCommand, transferCount, waitForIdle);
153*1b191cb5SApple OSS Distributions }
154*1b191cb5SApple OSS Distributions
155*1b191cb5SApple OSS Distributions
156*1b191cb5SApple OSS Distributions IOByteCount
getFIFODepth(IODirection direction)157*1b191cb5SApple OSS Distributions IODMAEventSource::getFIFODepth(IODirection direction)
158*1b191cb5SApple OSS Distributions {
159*1b191cb5SApple OSS Distributions if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
160*1b191cb5SApple OSS Distributions return 0;
161*1b191cb5SApple OSS Distributions }
162*1b191cb5SApple OSS Distributions
163*1b191cb5SApple OSS Distributions return dmaController->getFIFODepth(dmaIndex, direction);
164*1b191cb5SApple OSS Distributions }
165*1b191cb5SApple OSS Distributions
166*1b191cb5SApple OSS Distributions
167*1b191cb5SApple OSS Distributions IOReturn
setFIFODepth(IOByteCount depth)168*1b191cb5SApple OSS Distributions IODMAEventSource::setFIFODepth(IOByteCount depth)
169*1b191cb5SApple OSS Distributions {
170*1b191cb5SApple OSS Distributions if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
171*1b191cb5SApple OSS Distributions return kIOReturnError;
172*1b191cb5SApple OSS Distributions }
173*1b191cb5SApple OSS Distributions
174*1b191cb5SApple OSS Distributions return dmaController->setFIFODepth(dmaIndex, depth);
175*1b191cb5SApple OSS Distributions }
176*1b191cb5SApple OSS Distributions
177*1b191cb5SApple OSS Distributions
178*1b191cb5SApple OSS Distributions IOByteCount
validFIFODepth(IOByteCount depth,IODirection direction)179*1b191cb5SApple OSS Distributions IODMAEventSource::validFIFODepth(IOByteCount depth, IODirection direction)
180*1b191cb5SApple OSS Distributions {
181*1b191cb5SApple OSS Distributions if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
182*1b191cb5SApple OSS Distributions return kIOReturnError;
183*1b191cb5SApple OSS Distributions }
184*1b191cb5SApple OSS Distributions
185*1b191cb5SApple OSS Distributions return dmaController->validFIFODepth(dmaIndex, depth, direction);
186*1b191cb5SApple OSS Distributions }
187*1b191cb5SApple OSS Distributions
188*1b191cb5SApple OSS Distributions
189*1b191cb5SApple OSS Distributions IOReturn
setFrameSize(UInt8 byteCount)190*1b191cb5SApple OSS Distributions IODMAEventSource::setFrameSize(UInt8 byteCount)
191*1b191cb5SApple OSS Distributions {
192*1b191cb5SApple OSS Distributions if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
193*1b191cb5SApple OSS Distributions return kIOReturnError;
194*1b191cb5SApple OSS Distributions }
195*1b191cb5SApple OSS Distributions
196*1b191cb5SApple OSS Distributions return dmaController->setFrameSize(dmaIndex, byteCount);
197*1b191cb5SApple OSS Distributions }
198*1b191cb5SApple OSS Distributions
199*1b191cb5SApple OSS Distributions // protected
200*1b191cb5SApple OSS Distributions
201*1b191cb5SApple OSS Distributions bool
checkForWork(void)202*1b191cb5SApple OSS Distributions IODMAEventSource::checkForWork(void)
203*1b191cb5SApple OSS Distributions {
204*1b191cb5SApple OSS Distributions IODMACommand *dmaCommand = NULL;
205*1b191cb5SApple OSS Distributions bool work, again;
206*1b191cb5SApple OSS Distributions
207*1b191cb5SApple OSS Distributions IOSimpleLockLock(dmaCommandsCompletedLock);
208*1b191cb5SApple OSS Distributions work = !queue_empty(&dmaCommandsCompleted);
209*1b191cb5SApple OSS Distributions if (work) {
210*1b191cb5SApple OSS Distributions queue_remove_first(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
211*1b191cb5SApple OSS Distributions again = !queue_empty(&dmaCommandsCompleted);
212*1b191cb5SApple OSS Distributions } else {
213*1b191cb5SApple OSS Distributions again = false;
214*1b191cb5SApple OSS Distributions }
215*1b191cb5SApple OSS Distributions IOSimpleLockUnlock(dmaCommandsCompletedLock);
216*1b191cb5SApple OSS Distributions
217*1b191cb5SApple OSS Distributions if (work) {
218*1b191cb5SApple OSS Distributions (*dmaCompletionAction)(owner, this, dmaCommand, dmaCommand->reserved->fStatus, dmaCommand->reserved->fActualByteCount, dmaCommand->reserved->fTimeStamp);
219*1b191cb5SApple OSS Distributions }
220*1b191cb5SApple OSS Distributions
221*1b191cb5SApple OSS Distributions return again;
222*1b191cb5SApple OSS Distributions }
223*1b191cb5SApple OSS Distributions
224*1b191cb5SApple OSS Distributions void
completeDMACommand(IODMACommand * dmaCommand)225*1b191cb5SApple OSS Distributions IODMAEventSource::completeDMACommand(IODMACommand *dmaCommand)
226*1b191cb5SApple OSS Distributions {
227*1b191cb5SApple OSS Distributions if (dmaCompletionAction != NULL) {
228*1b191cb5SApple OSS Distributions IOSimpleLockLock(dmaCommandsCompletedLock);
229*1b191cb5SApple OSS Distributions queue_enter(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
230*1b191cb5SApple OSS Distributions IOSimpleLockUnlock(dmaCommandsCompletedLock);
231*1b191cb5SApple OSS Distributions
232*1b191cb5SApple OSS Distributions signalWorkAvailable();
233*1b191cb5SApple OSS Distributions } else {
234*1b191cb5SApple OSS Distributions dmaSynchBusy = false;
235*1b191cb5SApple OSS Distributions wakeupGate(&dmaSynchBusy, true);
236*1b191cb5SApple OSS Distributions }
237*1b191cb5SApple OSS Distributions }
238*1b191cb5SApple OSS Distributions
239*1b191cb5SApple OSS Distributions void
notifyDMACommand(IODMACommand * dmaCommand,IOReturn status,IOByteCount actualByteCount,AbsoluteTime timeStamp)240*1b191cb5SApple OSS Distributions IODMAEventSource::notifyDMACommand(IODMACommand *dmaCommand, IOReturn status, IOByteCount actualByteCount, AbsoluteTime timeStamp)
241*1b191cb5SApple OSS Distributions {
242*1b191cb5SApple OSS Distributions dmaCommand->reserved->fStatus = status;
243*1b191cb5SApple OSS Distributions dmaCommand->reserved->fActualByteCount = actualByteCount;
244*1b191cb5SApple OSS Distributions dmaCommand->reserved->fTimeStamp = timeStamp;
245*1b191cb5SApple OSS Distributions
246*1b191cb5SApple OSS Distributions if (dmaNotificationAction != NULL) {
247*1b191cb5SApple OSS Distributions (*dmaNotificationAction)(owner, this, dmaCommand, status, actualByteCount, timeStamp);
248*1b191cb5SApple OSS Distributions }
249*1b191cb5SApple OSS Distributions }
250*1b191cb5SApple OSS Distributions
251*1b191cb5SApple OSS Distributions IOReturn
setDMAConfig(UInt32 newReqIndex)252*1b191cb5SApple OSS Distributions IODMAEventSource::setDMAConfig(UInt32 newReqIndex)
253*1b191cb5SApple OSS Distributions {
254*1b191cb5SApple OSS Distributions return dmaController->setDMAConfig(dmaIndex, dmaProvider, newReqIndex);
255*1b191cb5SApple OSS Distributions }
256*1b191cb5SApple OSS Distributions
257*1b191cb5SApple OSS Distributions bool
validDMAConfig(UInt32 newReqIndex)258*1b191cb5SApple OSS Distributions IODMAEventSource::validDMAConfig(UInt32 newReqIndex)
259*1b191cb5SApple OSS Distributions {
260*1b191cb5SApple OSS Distributions return dmaController->validDMAConfig(dmaIndex, dmaProvider, newReqIndex);
261*1b191cb5SApple OSS Distributions }
262