]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCommandGate.cpp
3c7c228549e1f16cb62412e4779dd45c1f3e88b0
[apple/xnu.git] / iokit / Kernel / IOCommandGate.cpp
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 #include <IOKit/IOCommandGate.h>
29 #include <IOKit/IOWorkLoop.h>
30 #include <IOKit/IOReturn.h>
31 #include <IOKit/IOTimeStamp.h>
32
33 #define super IOEventSource
34
35 OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource)
36 OSMetaClassDefineReservedUnused(IOCommandGate, 0);
37 OSMetaClassDefineReservedUnused(IOCommandGate, 1);
38 OSMetaClassDefineReservedUnused(IOCommandGate, 2);
39 OSMetaClassDefineReservedUnused(IOCommandGate, 3);
40 OSMetaClassDefineReservedUnused(IOCommandGate, 4);
41 OSMetaClassDefineReservedUnused(IOCommandGate, 5);
42 OSMetaClassDefineReservedUnused(IOCommandGate, 6);
43 OSMetaClassDefineReservedUnused(IOCommandGate, 7);
44
45 bool IOCommandGate::checkForWork() { return false; }
46
47 bool IOCommandGate::init(OSObject *inOwner, Action inAction)
48 {
49 return super::init(inOwner, (IOEventSource::Action) inAction);
50 }
51
52 IOCommandGate *
53 IOCommandGate::commandGate(OSObject *inOwner, Action inAction)
54 {
55 IOCommandGate *me = new IOCommandGate;
56
57 if (me && !me->init(inOwner, inAction)) {
58 me->release();
59 return 0;
60 }
61
62 return me;
63 }
64
65 IOReturn IOCommandGate::runCommand(void *arg0, void *arg1,
66 void *arg2, void *arg3)
67 {
68 IOReturn res;
69
70 if (!enabled)
71 return kIOReturnNotPermitted;
72
73 if (!action)
74 return kIOReturnNoResources;
75
76 // closeGate is recursive so don't worry if we already hold the lock.
77 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
78 (unsigned int) action, (unsigned int) owner);
79
80 closeGate();
81 res = (*(Action) action)(owner, arg0, arg1, arg2, arg3);
82 openGate();
83
84 return res;
85 }
86
87 IOReturn IOCommandGate::runAction(Action inAction,
88 void *arg0, void *arg1,
89 void *arg2, void *arg3)
90 {
91 IOReturn res;
92
93 if (!enabled)
94 return kIOReturnNotPermitted;
95
96 if (!inAction)
97 return kIOReturnBadArgument;
98
99 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
100 (unsigned int) inAction, (unsigned int) owner);
101
102 // closeGate is recursive so don't worry if we already hold the lock.
103 closeGate();
104 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
105 openGate();
106
107 return res;
108 }
109
110 IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1,
111 void *arg2, void *arg3)
112 {
113 IOReturn res;
114
115 if (!enabled)
116 return kIOReturnNotPermitted;
117
118 if (!action)
119 return kIOReturnNoResources;
120
121 // Try to hold the lock if can't get return immediately.
122 if (!tryCloseGate())
123 return kIOReturnCannotLock;
124
125 // closeGate is recursive so don't worry if we already hold the lock.
126 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
127 (unsigned int) action, (unsigned int) owner);
128
129 res = (*(Action) action)(owner, arg0, arg1, arg2, arg3);
130 openGate();
131
132 return res;
133 }
134
135 IOReturn IOCommandGate::attemptAction(Action inAction,
136 void *arg0, void *arg1,
137 void *arg2, void *arg3)
138 {
139 IOReturn res;
140
141 if (!enabled)
142 return kIOReturnNotPermitted;
143
144 if (!inAction)
145 return kIOReturnBadArgument;
146
147 // Try to close the gate if can't get return immediately.
148 if (!tryCloseGate())
149 return kIOReturnCannotLock;
150
151 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
152 (unsigned int) inAction, (unsigned int) owner);
153
154 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
155 openGate();
156
157 return res;
158 }
159
160 IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible)
161 {
162 if (!workLoop->inGate())
163 return kIOReturnNotPermitted;
164
165 return sleepGate(event, interruptible);
166 }
167
168 void IOCommandGate::commandWakeup(void *event, bool oneThread)
169 {
170 wakeupGate(event, oneThread);
171 }