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