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