]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IODMAEventSource.cpp
xnu-6153.41.3.tar.gz
[apple/xnu.git] / iokit / Kernel / IODMAEventSource.cpp
CommitLineData
2d21ac55
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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.
0a7de745 25 *
2d21ac55
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <IOKit/IODMAEventSource.h>
30#include <IOKit/IOService.h>
31
32#include "IOKitKernelInternal.h"
33
34
35#define super IOEventSource
36OSDefineMetaClassAndStructors(IODMAEventSource, IOEventSource);
37
0a7de745
A
38bool
39IODMAEventSource::init(OSObject *inOwner,
40 IOService *inProvider,
41 Action inCompletion,
42 Action inNotification,
43 UInt32 inDMAIndex)
2d21ac55 44{
0a7de745
A
45 IOReturn result;
46
47 if (!super::init(inOwner)) {
48 return false;
49 }
50
cb323159 51 if (inProvider == NULL) {
0a7de745
A
52 return false;
53 }
54
55 dmaProvider = inProvider;
56 dmaIndex = 0xFFFFFFFF;
57 dmaCompletionAction = inCompletion;
58 dmaNotificationAction = inNotification;
59
60 dmaController = IODMAController::getController(dmaProvider, inDMAIndex);
cb323159 61 if (dmaController == NULL) {
0a7de745
A
62 return false;
63 }
64 dmaController->retain();
65
66 result = dmaController->initDMAChannel(dmaProvider, this, &dmaIndex, inDMAIndex);
67 if (result != kIOReturnSuccess) {
68 return false;
69 }
70
71 queue_init(&dmaCommandsCompleted);
72 dmaCommandsCompletedLock = IOSimpleLockAlloc();
73
74 return true;
2d21ac55
A
75}
76
0a7de745
A
77void
78IODMAEventSource::free()
39236c6e 79{
0a7de745
A
80 if (dmaCommandsCompletedLock != NULL) {
81 IOSimpleLockFree(dmaCommandsCompletedLock);
82 }
83 super::free();
39236c6e
A
84}
85
0a7de745
A
86IODMAEventSource *
87IODMAEventSource::dmaEventSource(OSObject *inOwner,
88 IOService *inProvider,
89 Action inCompletion,
90 Action inNotification,
91 UInt32 inDMAIndex)
2d21ac55 92{
0a7de745
A
93 IODMAEventSource *dmaES = new IODMAEventSource;
94
95 if (dmaES && !dmaES->init(inOwner, inProvider, inCompletion, inNotification, inDMAIndex)) {
96 dmaES->release();
cb323159 97 return NULL;
0a7de745
A
98 }
99
100 return dmaES;
2d21ac55
A
101}
102
0a7de745
A
103IOReturn
104IODMAEventSource::startDMACommand(IODMACommand *dmaCommand, IODirection direction, IOByteCount byteCount, IOByteCount byteOffset)
2d21ac55 105{
0a7de745
A
106 IOReturn result;
107
cb323159 108 if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
0a7de745
A
109 return kIOReturnError;
110 }
111
112 if (dmaSynchBusy) {
113 return kIOReturnBusy;
114 }
115
cb323159 116 if (dmaCompletionAction == NULL) {
0a7de745
A
117 dmaSynchBusy = true;
118 }
119
120 result = dmaController->startDMACommand(dmaIndex, dmaCommand, direction, byteCount, byteOffset);
121
122 if (result != kIOReturnSuccess) {
123 dmaSynchBusy = false;
124 return result;
125 }
126
127 while (dmaSynchBusy) {
128 sleepGate(&dmaSynchBusy, THREAD_UNINT);
129 }
130
131 return kIOReturnSuccess;
2d21ac55
A
132}
133
0a7de745
A
134IOReturn
135IODMAEventSource::stopDMACommand(bool flush, uint64_t timeout)
2d21ac55 136{
cb323159 137 if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
0a7de745
A
138 return kIOReturnError;
139 }
140
141 return dmaController->stopDMACommand(dmaIndex, flush, timeout);
2d21ac55
A
142}
143
144
0a7de745
A
145IOReturn
146IODMAEventSource::queryDMACommand(IODMACommand **dmaCommand, IOByteCount *transferCount, bool waitForIdle)
2d21ac55 147{
cb323159 148 if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
0a7de745
A
149 return kIOReturnError;
150 }
151
152 return dmaController->queryDMACommand(dmaIndex, dmaCommand, transferCount, waitForIdle);
2d21ac55
A
153}
154
155
0a7de745
A
156IOByteCount
157IODMAEventSource::getFIFODepth(IODirection direction)
39236c6e 158{
cb323159 159 if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
0a7de745
A
160 return 0;
161 }
162
163 return dmaController->getFIFODepth(dmaIndex, direction);
39236c6e
A
164}
165
166
0a7de745
A
167IOReturn
168IODMAEventSource::setFIFODepth(IOByteCount depth)
39236c6e 169{
cb323159 170 if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
0a7de745
A
171 return kIOReturnError;
172 }
173
174 return dmaController->setFIFODepth(dmaIndex, depth);
39236c6e
A
175}
176
177
0a7de745
A
178IOByteCount
179IODMAEventSource::validFIFODepth(IOByteCount depth, IODirection direction)
b0d623f7 180{
cb323159 181 if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
0a7de745
A
182 return kIOReturnError;
183 }
184
185 return dmaController->validFIFODepth(dmaIndex, depth, direction);
b0d623f7
A
186}
187
188
0a7de745
A
189IOReturn
190IODMAEventSource::setFrameSize(UInt8 byteCount)
3e170ce0 191{
cb323159 192 if ((dmaController == NULL) || (dmaIndex == 0xFFFFFFFF)) {
0a7de745
A
193 return kIOReturnError;
194 }
195
196 return dmaController->setFrameSize(dmaIndex, byteCount);
3e170ce0
A
197}
198
2d21ac55
A
199// protected
200
0a7de745
A
201bool
202IODMAEventSource::checkForWork(void)
2d21ac55 203{
0a7de745
A
204 IODMACommand *dmaCommand = NULL;
205 bool work, again;
206
207 IOSimpleLockLock(dmaCommandsCompletedLock);
208 work = !queue_empty(&dmaCommandsCompleted);
209 if (work) {
210 queue_remove_first(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
211 again = !queue_empty(&dmaCommandsCompleted);
212 } else {
213 again = false;
214 }
215 IOSimpleLockUnlock(dmaCommandsCompletedLock);
216
217 if (work) {
218 (*dmaCompletionAction)(owner, this, dmaCommand, dmaCommand->reserved->fStatus, dmaCommand->reserved->fActualByteCount, dmaCommand->reserved->fTimeStamp);
219 }
220
221 return again;
2d21ac55
A
222}
223
0a7de745
A
224void
225IODMAEventSource::completeDMACommand(IODMACommand *dmaCommand)
2d21ac55 226{
cb323159 227 if (dmaCompletionAction != NULL) {
0a7de745
A
228 IOSimpleLockLock(dmaCommandsCompletedLock);
229 queue_enter(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
230 IOSimpleLockUnlock(dmaCommandsCompletedLock);
231
232 signalWorkAvailable();
233 } else {
234 dmaSynchBusy = false;
235 wakeupGate(&dmaSynchBusy, true);
236 }
2d21ac55
A
237}
238
0a7de745
A
239void
240IODMAEventSource::notifyDMACommand(IODMACommand *dmaCommand, IOReturn status, IOByteCount actualByteCount, AbsoluteTime timeStamp)
2d21ac55 241{
0a7de745
A
242 dmaCommand->reserved->fStatus = status;
243 dmaCommand->reserved->fActualByteCount = actualByteCount;
244 dmaCommand->reserved->fTimeStamp = timeStamp;
245
cb323159 246 if (dmaNotificationAction != NULL) {
0a7de745
A
247 (*dmaNotificationAction)(owner, this, dmaCommand, status, actualByteCount, timeStamp);
248 }
39236c6e
A
249}
250
0a7de745
A
251IOReturn
252IODMAEventSource::setDMAConfig(UInt32 newReqIndex)
39236c6e 253{
0a7de745 254 return dmaController->setDMAConfig(dmaIndex, dmaProvider, newReqIndex);
39236c6e
A
255}
256
0a7de745
A
257bool
258IODMAEventSource::validDMAConfig(UInt32 newReqIndex)
39236c6e 259{
0a7de745 260 return dmaController->validDMAConfig(dmaIndex, dmaProvider, newReqIndex);
2d21ac55 261}