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