]>
Commit | Line | Data |
---|---|---|
2d21ac55 A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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. | |
25 | * | |
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 | |
36 | OSDefineMetaClassAndStructors(IODMAEventSource, IOEventSource); | |
37 | ||
38 | bool IODMAEventSource::init(OSObject *inOwner, | |
39 | IOService *inProvider, | |
40 | Action inCompletion, | |
41 | Action inNotification, | |
42 | UInt32 inDMAIndex) | |
43 | { | |
44 | IOReturn result; | |
45 | ||
46 | if (!super::init(inOwner)) return false; | |
47 | ||
48 | if (inProvider == 0) return false; | |
49 | ||
50 | dmaProvider = inProvider; | |
51 | dmaIndex = 0xFFFFFFFF; | |
52 | dmaCompletionAction = inCompletion; | |
53 | dmaNotificationAction = inNotification; | |
54 | ||
55 | dmaController = IODMAController::getController(dmaProvider, inDMAIndex); | |
56 | if (dmaController == 0) return false; | |
57 | dmaController->retain(); | |
58 | ||
59 | result = dmaController->initDMAChannel(dmaProvider, this, &dmaIndex, inDMAIndex); | |
60 | if (result != kIOReturnSuccess) return false; | |
61 | ||
62 | queue_init(&dmaCommandsCompleted); | |
63 | dmaCommandsCompletedLock = IOSimpleLockAlloc(); | |
64 | ||
65 | return true; | |
66 | } | |
67 | ||
68 | IODMAEventSource *IODMAEventSource::dmaEventSource(OSObject *inOwner, | |
69 | IOService *inProvider, | |
70 | Action inCompletion, | |
71 | Action inNotification, | |
72 | UInt32 inDMAIndex) | |
73 | { | |
74 | IODMAEventSource *dmaES = new IODMAEventSource; | |
75 | ||
76 | if (dmaES && !dmaES->init(inOwner, inProvider, inCompletion, inNotification, inDMAIndex)) { | |
77 | dmaES->release(); | |
78 | return 0; | |
79 | } | |
80 | ||
81 | return dmaES; | |
82 | } | |
83 | ||
84 | IOReturn IODMAEventSource::startDMACommand(IODMACommand *dmaCommand, IODirection direction, IOByteCount byteCount, IOByteCount byteOffset) | |
85 | { | |
86 | IOReturn result; | |
87 | ||
88 | if ((dmaController == 0) || (dmaIndex == 0xFFFFFFFF)) return kIOReturnError; | |
89 | ||
90 | if (dmaSynchBusy) return kIOReturnBusy; | |
91 | ||
92 | if (dmaCompletionAction == 0) dmaSynchBusy = true; | |
93 | ||
94 | result = dmaController->startDMACommand(dmaIndex, dmaCommand, direction, byteCount, byteOffset); | |
95 | ||
96 | if (result != kIOReturnSuccess) { | |
97 | dmaSynchBusy = false; | |
98 | return result; | |
99 | } | |
100 | ||
101 | while (dmaSynchBusy) sleepGate(&dmaSynchBusy, THREAD_UNINT); | |
102 | ||
103 | return kIOReturnSuccess; | |
104 | } | |
105 | ||
106 | IOReturn IODMAEventSource::stopDMACommand(bool flush, mach_timespec_t *timeout) | |
107 | { | |
108 | if ((dmaController == 0) || (dmaIndex == 0xFFFFFFFF)) return kIOReturnError; | |
109 | ||
110 | return dmaController->stopDMACommand(dmaIndex, flush, timeout); | |
111 | } | |
112 | ||
113 | ||
114 | IOReturn IODMAEventSource::queryDMACommand(IODMACommand **dmaCommand, IOByteCount *transferCount, bool waitForIdle) | |
115 | { | |
116 | if ((dmaController == 0) || (dmaIndex == 0xFFFFFFFF)) return kIOReturnError; | |
117 | ||
118 | return dmaController->queryDMACommand(dmaIndex, dmaCommand, transferCount, waitForIdle); | |
119 | } | |
120 | ||
121 | ||
122 | // protected | |
123 | ||
124 | bool IODMAEventSource::checkForWork(void) | |
125 | { | |
126 | IODMACommand *dmaCommand = NULL; | |
127 | bool work, again; | |
128 | ||
129 | IOSimpleLockLock(dmaCommandsCompletedLock); | |
130 | work = !queue_empty(&dmaCommandsCompleted); | |
131 | if (work) { | |
132 | queue_remove_first(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain); | |
133 | again = !queue_empty(&dmaCommandsCompleted); | |
134 | } else { | |
135 | again = false; | |
136 | } | |
137 | IOSimpleLockUnlock(dmaCommandsCompletedLock); | |
138 | ||
139 | if (work) { | |
140 | (*dmaCompletionAction)(owner, this, dmaCommand, dmaCommand->reserved->fStatus, dmaCommand->reserved->fActualByteCount); | |
141 | } | |
142 | ||
143 | return again; | |
144 | } | |
145 | ||
146 | void IODMAEventSource::completeDMACommand(IODMACommand *dmaCommand) | |
147 | { | |
148 | if (dmaCompletionAction != 0) { | |
149 | IOSimpleLockLock(dmaCommandsCompletedLock); | |
150 | queue_enter(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain); | |
151 | IOSimpleLockUnlock(dmaCommandsCompletedLock); | |
152 | ||
153 | signalWorkAvailable(); | |
154 | } else { | |
155 | dmaSynchBusy = false; | |
156 | wakeupGate(&dmaSynchBusy, true); | |
157 | } | |
158 | } | |
159 | ||
160 | void IODMAEventSource::notifyDMACommand(IODMACommand *dmaCommand, IOReturn status, IOByteCount actualByteCount) | |
161 | { | |
162 | dmaCommand->reserved->fStatus = status; | |
163 | dmaCommand->reserved->fActualByteCount = actualByteCount; | |
164 | ||
165 | if (dmaNotificationAction != 0) (*dmaNotificationAction)(owner, this, dmaCommand, status, actualByteCount); | |
166 | } |