]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCommandGate.cpp
xnu-792.6.56.tar.gz
[apple/xnu.git] / iokit / Kernel / IOCommandGate.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #include <IOKit/IOCommandGate.h>
24 #include <IOKit/IOWorkLoop.h>
25 #include <IOKit/IOReturn.h>
26 #include <IOKit/IOTimeStamp.h>
27
28 #define super IOEventSource
29
30 OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource)
31 OSMetaClassDefineReservedUnused(IOCommandGate, 0);
32 OSMetaClassDefineReservedUnused(IOCommandGate, 1);
33 OSMetaClassDefineReservedUnused(IOCommandGate, 2);
34 OSMetaClassDefineReservedUnused(IOCommandGate, 3);
35 OSMetaClassDefineReservedUnused(IOCommandGate, 4);
36 OSMetaClassDefineReservedUnused(IOCommandGate, 5);
37 OSMetaClassDefineReservedUnused(IOCommandGate, 6);
38 OSMetaClassDefineReservedUnused(IOCommandGate, 7);
39
40 bool IOCommandGate::checkForWork() { return false; }
41
42 bool IOCommandGate::init(OSObject *inOwner, Action inAction)
43 {
44 return super::init(inOwner, (IOEventSource::Action) inAction);
45 }
46
47 IOCommandGate *
48 IOCommandGate::commandGate(OSObject *inOwner, Action inAction)
49 {
50 IOCommandGate *me = new IOCommandGate;
51
52 if (me && !me->init(inOwner, inAction)) {
53 me->release();
54 return 0;
55 }
56
57 return me;
58 }
59
60 IOReturn IOCommandGate::runCommand(void *arg0, void *arg1,
61 void *arg2, void *arg3)
62 {
63 IOReturn res;
64
65 if (!enabled)
66 return kIOReturnNotPermitted;
67
68 if (!action)
69 return kIOReturnNoResources;
70
71 // closeGate is recursive so don't worry if we already hold the lock.
72 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
73 (unsigned int) action, (unsigned int) owner);
74
75 closeGate();
76 res = (*(Action) action)(owner, arg0, arg1, arg2, arg3);
77 openGate();
78
79 return res;
80 }
81
82 IOReturn IOCommandGate::runAction(Action inAction,
83 void *arg0, void *arg1,
84 void *arg2, void *arg3)
85 {
86 IOReturn res;
87
88 if (!enabled)
89 return kIOReturnNotPermitted;
90
91 if (!inAction)
92 return kIOReturnBadArgument;
93
94 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
95 (unsigned int) inAction, (unsigned int) owner);
96
97 // closeGate is recursive so don't worry if we already hold the lock.
98 closeGate();
99 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
100 openGate();
101
102 return res;
103 }
104
105 IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1,
106 void *arg2, void *arg3)
107 {
108 IOReturn res;
109
110 if (!enabled)
111 return kIOReturnNotPermitted;
112
113 if (!action)
114 return kIOReturnNoResources;
115
116 // Try to hold the lock if can't get return immediately.
117 if (!tryCloseGate())
118 return kIOReturnCannotLock;
119
120 // closeGate is recursive so don't worry if we already hold the lock.
121 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
122 (unsigned int) action, (unsigned int) owner);
123
124 res = (*(Action) action)(owner, arg0, arg1, arg2, arg3);
125 openGate();
126
127 return res;
128 }
129
130 IOReturn IOCommandGate::attemptAction(Action inAction,
131 void *arg0, void *arg1,
132 void *arg2, void *arg3)
133 {
134 IOReturn res;
135
136 if (!enabled)
137 return kIOReturnNotPermitted;
138
139 if (!inAction)
140 return kIOReturnBadArgument;
141
142 // Try to close the gate if can't get return immediately.
143 if (!tryCloseGate())
144 return kIOReturnCannotLock;
145
146 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
147 (unsigned int) inAction, (unsigned int) owner);
148
149 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
150 openGate();
151
152 return res;
153 }
154
155 IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible)
156 {
157 if (!workLoop->inGate())
158 return kIOReturnNotPermitted;
159
160 return sleepGate(event, interruptible);
161 }
162
163 void IOCommandGate::commandWakeup(void *event, bool oneThread)
164 {
165 wakeupGate(event, oneThread);
166 }