1*c54f35caSApple OSS Distributions /* 2*c54f35caSApple OSS Distributions * Copyright (c) 1998-2019 Apple Inc. All rights reserved. 3*c54f35caSApple OSS Distributions * 4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*c54f35caSApple OSS Distributions * 6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*c54f35caSApple OSS Distributions * 15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at 16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*c54f35caSApple OSS Distributions * 18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are 19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and 24*c54f35caSApple OSS Distributions * limitations under the License. 25*c54f35caSApple OSS Distributions * 26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*c54f35caSApple OSS Distributions */ 28*c54f35caSApple OSS Distributions /* 29*c54f35caSApple OSS Distributions * Copyright (c) 1998 Apple Computer, Inc. All rights reserved. 30*c54f35caSApple OSS Distributions * HISTORY 31*c54f35caSApple OSS Distributions * 1998-7-13 Godfrey van der Linden(gvdl) 32*c54f35caSApple OSS Distributions * Created. 33*c54f35caSApple OSS Distributions * 1998-10-30 Godfrey van der Linden(gvdl) 34*c54f35caSApple OSS Distributions * Converted to C++ 35*c54f35caSApple OSS Distributions */ 36*c54f35caSApple OSS Distributions #ifndef _IOKIT_IOEVENTSOURCE_H 37*c54f35caSApple OSS Distributions #define _IOKIT_IOEVENTSOURCE_H 38*c54f35caSApple OSS Distributions 39*c54f35caSApple OSS Distributions #include <sys/cdefs.h> 40*c54f35caSApple OSS Distributions 41*c54f35caSApple OSS Distributions #include <libkern/c++/OSObject.h> 42*c54f35caSApple OSS Distributions 43*c54f35caSApple OSS Distributions #include <IOKit/IOLib.h> 44*c54f35caSApple OSS Distributions #include <IOKit/system.h> 45*c54f35caSApple OSS Distributions #include <IOKit/IOWorkLoop.h> 46*c54f35caSApple OSS Distributions 47*c54f35caSApple OSS Distributions #if IOKITSTATS 48*c54f35caSApple OSS Distributions #include <IOKit/IOStatisticsPrivate.h> 49*c54f35caSApple OSS Distributions #endif 50*c54f35caSApple OSS Distributions 51*c54f35caSApple OSS Distributions __BEGIN_DECLS 52*c54f35caSApple OSS Distributions #include <mach/clock_types.h> 53*c54f35caSApple OSS Distributions #include <kern/clock.h> 54*c54f35caSApple OSS Distributions __END_DECLS 55*c54f35caSApple OSS Distributions 56*c54f35caSApple OSS Distributions /*! 57*c54f35caSApple OSS Distributions * @class IOEventSource : public OSObject 58*c54f35caSApple OSS Distributions * @abstract Abstract class for all work-loop event sources. 59*c54f35caSApple OSS Distributions * @discussion The IOEventSource declares the abstract super class that all 60*c54f35caSApple OSS Distributions * event sources must inherit from if an IOWorkLoop is to receive events from them. 61*c54f35caSApple OSS Distributions * <br><br> 62*c54f35caSApple OSS Distributions * An event source can represent any event that should cause the work-loop of a 63*c54f35caSApple OSS Distributions * device to wake up and perform work. Two examples of event sources are the 64*c54f35caSApple OSS Distributions * IOInterruptEventSource which delivers interrupt notifications and IOCommandGate 65*c54f35caSApple OSS Distributions * which delivers command requests. 66*c54f35caSApple OSS Distributions * <br><br> 67*c54f35caSApple OSS Distributions * A kernel module can always use the work-loop model for serialising access to 68*c54f35caSApple OSS Distributions * anything at all. The IOEventSource is used for communicating events to the 69*c54f35caSApple OSS Distributions * work-loop, and the chain of event sources should be used to walk the possible 70*c54f35caSApple OSS Distributions * event sources and demultipex them. Note a particular instance of an event 71*c54f35caSApple OSS Distributions * source may only be a member of 1 linked list chain. If you need to move it 72*c54f35caSApple OSS Distributions * between chains than make sure it is removed from the original chain before 73*c54f35caSApple OSS Distributions * attempting to move it. 74*c54f35caSApple OSS Distributions * <br><br> 75*c54f35caSApple OSS Distributions * The IOEventSource makes no attempt to maintain the consistency of its internal data across multi-threading. It is assumed that the user of these basic tools will protect the data that these objects represent in some sort of device wide instance lock. For example the IOWorkLoop maintains the event chain by using an IOCommandGate and thus single threading access to its state. 76*c54f35caSApple OSS Distributions * <br><br> 77*c54f35caSApple OSS Distributions * All subclasses of IOEventSource that wish to perform work on the work-loop thread are expected to implement the checkForWork() member function. As of Mac OS X, 10.7 (Darwin 11), checkForWork is no longer pure virtual, and should not be overridden if there is no work to be done. 78*c54f35caSApple OSS Distributions * 79*c54f35caSApple OSS Distributions * <br><br> 80*c54f35caSApple OSS Distributions * checkForWork() is the key method in this class. It is called by some work-loop when convienient and is expected to evaluate its internal state and determine if an event has occurred since the last call. In the case of an event having occurred then the instance defined target(owner)/action will be called. The action is stored as an ordinary C function pointer but the first parameter is always the owner. This means that a C++ member function can be used as an action function though this depends on the ABI. 81*c54f35caSApple OSS Distributions * <br><br> 82*c54f35caSApple OSS Distributions * Although the eventChainNext variable contains a reference to the next event source in the chain this reference is not retained. The list 'owner' i.e. the client that creates the event, not the work-loop, is expected to retain the source. 83*c54f35caSApple OSS Distributions */ 84*c54f35caSApple OSS Distributions class IOEventSource : public OSObject 85*c54f35caSApple OSS Distributions { 86*c54f35caSApple OSS Distributions OSDeclareAbstractStructors(IOEventSource); 87*c54f35caSApple OSS Distributions friend class IOWorkLoop; 88*c54f35caSApple OSS Distributions #if IOKITSTATS 89*c54f35caSApple OSS Distributions friend class IOStatistics; 90*c54f35caSApple OSS Distributions #endif 91*c54f35caSApple OSS Distributions 92*c54f35caSApple OSS Distributions public: 93*c54f35caSApple OSS Distributions /*! 94*c54f35caSApple OSS Distributions * @typedef Action 95*c54f35caSApple OSS Distributions * @discussion Placeholder type for C++ function overloading discrimination. 96*c54f35caSApple OSS Distributions * As the all event sources require an action and it has to be stored somewhere 97*c54f35caSApple OSS Distributions * and be of some type, this is that type. 98*c54f35caSApple OSS Distributions * @param owner 99*c54f35caSApple OSS Distributions * Target of the function, can be used as a refcon. The owner is set 100*c54f35caSApple OSS Distributions * during initialisation. Note if a C++ function was specified this parameter 101*c54f35caSApple OSS Distributions * is implicitly the first paramter in the target member function's parameter list. 102*c54f35caSApple OSS Distributions */ 103*c54f35caSApple OSS Distributions typedef void (*Action)(OSObject *owner, ...); 104*c54f35caSApple OSS Distributions 105*c54f35caSApple OSS Distributions /*! @defined IOEventSourceAction 106*c54f35caSApple OSS Distributions * @discussion Backward compatibilty define for the old non-class scoped type definition. See $link IOEventSource::Action */ 107*c54f35caSApple OSS Distributions #define IOEventSourceAction IOEventSource::Action 108*c54f35caSApple OSS Distributions 109*c54f35caSApple OSS Distributions #ifdef __BLOCKS__ 110*c54f35caSApple OSS Distributions typedef IOReturn (^ActionBlock)(); 111*c54f35caSApple OSS Distributions #endif /* __BLOCKS__ */ 112*c54f35caSApple OSS Distributions 113*c54f35caSApple OSS Distributions protected: 114*c54f35caSApple OSS Distributions /*! @var eventChainNext 115*c54f35caSApple OSS Distributions * The next event source in the event chain. nil at end of chain. */ 116*c54f35caSApple OSS Distributions IOEventSource *eventChainNext; 117*c54f35caSApple OSS Distributions 118*c54f35caSApple OSS Distributions /*! @var owner The owner object called when an event has been delivered. */ 119*c54f35caSApple OSS Distributions OSObject *owner; 120*c54f35caSApple OSS Distributions 121*c54f35caSApple OSS Distributions /*! @var action 122*c54f35caSApple OSS Distributions * The action method called when an event has been delivered */ 123*c54f35caSApple OSS Distributions 124*c54f35caSApple OSS Distributions #if XNU_KERNEL_PRIVATE 125*c54f35caSApple OSS Distributions union { Action action; ActionBlock actionBlock; }; 126*c54f35caSApple OSS Distributions #else /* XNU_KERNEL_PRIVATE */ 127*c54f35caSApple OSS Distributions Action action; 128*c54f35caSApple OSS Distributions #endif /* !XNU_KERNEL_PRIVATE */ 129*c54f35caSApple OSS Distributions 130*c54f35caSApple OSS Distributions /*! @var enabled 131*c54f35caSApple OSS Distributions * Is this event source enabled to deliver requests to the work-loop. */ 132*c54f35caSApple OSS Distributions bool enabled; 133*c54f35caSApple OSS Distributions 134*c54f35caSApple OSS Distributions #if XNU_KERNEL_PRIVATE 135*c54f35caSApple OSS Distributions enum{ 136*c54f35caSApple OSS Distributions kPassive = 0x0001, 137*c54f35caSApple OSS Distributions kActive = 0x0002, 138*c54f35caSApple OSS Distributions kActionBlock = 0x0004, 139*c54f35caSApple OSS Distributions kSubClass0 = 0x0008, 140*c54f35caSApple OSS Distributions }; 141*c54f35caSApple OSS Distributions uint8_t eventSourceReserved1[1]; 142*c54f35caSApple OSS Distributions uint16_t flags; 143*c54f35caSApple OSS Distributions #if __LP64__ 144*c54f35caSApple OSS Distributions uint8_t eventSourceReserved2[4]; 145*c54f35caSApple OSS Distributions #endif /* __LP64__ */ 146*c54f35caSApple OSS Distributions 147*c54f35caSApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */ 148*c54f35caSApple OSS Distributions 149*c54f35caSApple OSS Distributions /*! @var workLoop What is the work-loop for this event source. */ 150*c54f35caSApple OSS Distributions IOWorkLoop *workLoop; 151*c54f35caSApple OSS Distributions 152*c54f35caSApple OSS Distributions /*! @var refcon What ever the client wants to do, see $link setRefcon. */ 153*c54f35caSApple OSS Distributions void *refcon; 154*c54f35caSApple OSS Distributions 155*c54f35caSApple OSS Distributions /*! @struct ExpansionData 156*c54f35caSApple OSS Distributions * @discussion This structure will be used to expand the capablilties of the IOEventSource in the future. 157*c54f35caSApple OSS Distributions */ 158*c54f35caSApple OSS Distributions struct ExpansionData { 159*c54f35caSApple OSS Distributions #if IOKITSTATS 160*c54f35caSApple OSS Distributions struct IOEventSourceCounter *counter; 161*c54f35caSApple OSS Distributions #else 162*c54f35caSApple OSS Distributions void *iokitstatsReserved; 163*c54f35caSApple OSS Distributions #endif 164*c54f35caSApple OSS Distributions }; 165*c54f35caSApple OSS Distributions 166*c54f35caSApple OSS Distributions /*! @var reserved 167*c54f35caSApple OSS Distributions * Reserved for future use. (Internal use only) */ 168*c54f35caSApple OSS Distributions ExpansionData *reserved; 169*c54f35caSApple OSS Distributions 170*c54f35caSApple OSS Distributions /*! @function init 171*c54f35caSApple OSS Distributions * @abstract Primary initialiser for the IOEventSource class. 172*c54f35caSApple OSS Distributions * @param owner 173*c54f35caSApple OSS Distributions * Owner of this instance of an event source. Used as the first parameter 174*c54f35caSApple OSS Distributions * of the action callout. Owner must be an OSObject. 175*c54f35caSApple OSS Distributions * @param action 176*c54f35caSApple OSS Distributions * Pointer to C call out function. Action is a pointer to a C function 177*c54f35caSApple OSS Distributions * that gets called when this event source has outstanding work. It will usually 178*c54f35caSApple OSS Distributions * be called by the checkForWork member function. The first parameter of the 179*c54f35caSApple OSS Distributions * action call out will always be the owner, this allows C++ member functions to 180*c54f35caSApple OSS Distributions * be used as actions. Defaults to 0. 181*c54f35caSApple OSS Distributions * @result true if the inherited classes and this instance initialise 182*c54f35caSApple OSS Distributions * successfully. 183*c54f35caSApple OSS Distributions */ 184*c54f35caSApple OSS Distributions virtual bool init(OSObject *owner, IOEventSource::Action action = NULL); 185*c54f35caSApple OSS Distributions 186*c54f35caSApple OSS Distributions virtual void free( void ) APPLE_KEXT_OVERRIDE; 187*c54f35caSApple OSS Distributions 188*c54f35caSApple OSS Distributions /*! @function checkForWork 189*c54f35caSApple OSS Distributions * @abstract Virtual member function used by IOWorkLoop for work 190*c54f35caSApple OSS Distributions * scheduling. 191*c54f35caSApple OSS Distributions * @discussion This function will be called to request a subclass to check 192*c54f35caSApple OSS Distributions * its internal state for any work to do and then to call out the owner/action. 193*c54f35caSApple OSS Distributions * If this event source never performs any work (e.g. IOCommandGate), this 194*c54f35caSApple OSS Distributions * method should not be overridden. NOTE: This method is no longer declared pure 195*c54f35caSApple OSS Distributions * virtual. A default implementation is provided in IOEventSource. 196*c54f35caSApple OSS Distributions * @result Return true if this function needs to be called again before all its outstanding events have been processed. 197*c54f35caSApple OSS Distributions */ 198*c54f35caSApple OSS Distributions virtual bool checkForWork(); 199*c54f35caSApple OSS Distributions 200*c54f35caSApple OSS Distributions /*! @function setWorkLoop 201*c54f35caSApple OSS Distributions * @abstract Set'ter for $link workLoop variable. 202*c54f35caSApple OSS Distributions * @param workLoop 203*c54f35caSApple OSS Distributions * Target work-loop of this event source instance. A subclass of 204*c54f35caSApple OSS Distributions * IOWorkLoop that at least reacts to signalWorkAvailable() and onThread functions. 205*c54f35caSApple OSS Distributions */ 206*c54f35caSApple OSS Distributions virtual void setWorkLoop(IOWorkLoop *workLoop); 207*c54f35caSApple OSS Distributions 208*c54f35caSApple OSS Distributions /*! @function setNext 209*c54f35caSApple OSS Distributions * @abstract Set'ter for $link eventChainNext variable. 210*c54f35caSApple OSS Distributions * @param next 211*c54f35caSApple OSS Distributions * Pointer to another IOEventSource instance. 212*c54f35caSApple OSS Distributions */ 213*c54f35caSApple OSS Distributions virtual void setNext(IOEventSource *next); 214*c54f35caSApple OSS Distributions 215*c54f35caSApple OSS Distributions /*! @function getNext 216*c54f35caSApple OSS Distributions * @abstract Get'ter for $link eventChainNext variable. 217*c54f35caSApple OSS Distributions * @result value of eventChainNext. 218*c54f35caSApple OSS Distributions */ 219*c54f35caSApple OSS Distributions virtual IOEventSource *getNext() const; 220*c54f35caSApple OSS Distributions 221*c54f35caSApple OSS Distributions 222*c54f35caSApple OSS Distributions protected: 223*c54f35caSApple OSS Distributions // Methods to access the IOWorkLoop exported fields 224*c54f35caSApple OSS Distributions void signalWorkAvailable(); 225*c54f35caSApple OSS Distributions void openGate(); 226*c54f35caSApple OSS Distributions void closeGate(); 227*c54f35caSApple OSS Distributions bool tryCloseGate(); 228*c54f35caSApple OSS Distributions int sleepGate(void *event, UInt32 type); 229*c54f35caSApple OSS Distributions int sleepGate(void *event, AbsoluteTime deadline, UInt32 type); 230*c54f35caSApple OSS Distributions void wakeupGate(void *event, bool oneThread); 231*c54f35caSApple OSS Distributions 232*c54f35caSApple OSS Distributions public: 233*c54f35caSApple OSS Distributions /*! @function setAction 234*c54f35caSApple OSS Distributions * @abstract Set'ter for $link action variable. 235*c54f35caSApple OSS Distributions * @param action Pointer to a C function of type IOEventSource::Action. */ 236*c54f35caSApple OSS Distributions virtual void setAction(IOEventSource::Action action); 237*c54f35caSApple OSS Distributions 238*c54f35caSApple OSS Distributions /*! @function getAction 239*c54f35caSApple OSS Distributions * @abstract Get'ter for $link action variable. 240*c54f35caSApple OSS Distributions * @result value of action. */ 241*c54f35caSApple OSS Distributions virtual IOEventSource::Action getAction() const; 242*c54f35caSApple OSS Distributions 243*c54f35caSApple OSS Distributions #ifdef __BLOCKS__ 244*c54f35caSApple OSS Distributions /*! @function setActionBlock 245*c54f35caSApple OSS Distributions * @abstract Setter for action ivar. The current block is released, & the new block is retained. 246*c54f35caSApple OSS Distributions * @param block Block pointer of type IOEventSource::ActionBlock. */ 247*c54f35caSApple OSS Distributions void setActionBlock(ActionBlock block); 248*c54f35caSApple OSS Distributions /*! @function getActionBlock 249*c54f35caSApple OSS Distributions * @abstract Getter for action ivar. 250*c54f35caSApple OSS Distributions * @result Block pointer of type IOEventSource::ActionBlock, if set, or NULL. */ 251*c54f35caSApple OSS Distributions ActionBlock getActionBlock(ActionBlock) const; 252*c54f35caSApple OSS Distributions #endif /* __BLOCKS__ */ 253*c54f35caSApple OSS Distributions 254*c54f35caSApple OSS Distributions /*! @function setRefcon 255*c54f35caSApple OSS Distributions * @abstract Setter for refcon ivar. This function will assert if a block action has been set. 256*c54f35caSApple OSS Distributions * @param refcon Refcon. */ 257*c54f35caSApple OSS Distributions void setRefcon(void *refcon); 258*c54f35caSApple OSS Distributions /*! @function getRefcon 259*c54f35caSApple OSS Distributions * @abstract Getter for refcon ivar. 260*c54f35caSApple OSS Distributions * @result The refcon. This function will assert if a block action has been set. */ 261*c54f35caSApple OSS Distributions void * getRefcon() const; 262*c54f35caSApple OSS Distributions 263*c54f35caSApple OSS Distributions /*! @function enable 264*c54f35caSApple OSS Distributions * @abstract Enable event source. 265*c54f35caSApple OSS Distributions * @discussion A subclass implementation is expected to respect the enabled 266*c54f35caSApple OSS Distributions * state when checkForWork is called. Calling this function will cause the 267*c54f35caSApple OSS Distributions * work-loop to be signalled so that a checkForWork is performed. */ 268*c54f35caSApple OSS Distributions virtual void enable(); 269*c54f35caSApple OSS Distributions 270*c54f35caSApple OSS Distributions /*! @function disable 271*c54f35caSApple OSS Distributions * @abstract Disable event source. 272*c54f35caSApple OSS Distributions * @discussion A subclass implementation is expected to respect the enabled 273*c54f35caSApple OSS Distributions * state when checkForWork is called. */ 274*c54f35caSApple OSS Distributions virtual void disable(); 275*c54f35caSApple OSS Distributions 276*c54f35caSApple OSS Distributions /*! @function isEnabled 277*c54f35caSApple OSS Distributions * @abstract Get'ter for $link enable variable. 278*c54f35caSApple OSS Distributions * @result true if enabled. */ 279*c54f35caSApple OSS Distributions virtual bool isEnabled() const; 280*c54f35caSApple OSS Distributions 281*c54f35caSApple OSS Distributions /*! @function getWorkLoop 282*c54f35caSApple OSS Distributions * @abstract Get'ter for $link workLoop variable. 283*c54f35caSApple OSS Distributions * @result value of workLoop. */ 284*c54f35caSApple OSS Distributions virtual IOWorkLoop *getWorkLoop() const; 285*c54f35caSApple OSS Distributions 286*c54f35caSApple OSS Distributions /*! @function onThread 287*c54f35caSApple OSS Distributions * @abstract Convenience function for workLoop->onThread. 288*c54f35caSApple OSS Distributions * @result true if called on the work-loop thread. 289*c54f35caSApple OSS Distributions */ 290*c54f35caSApple OSS Distributions virtual bool onThread() const; 291*c54f35caSApple OSS Distributions 292*c54f35caSApple OSS Distributions private: 293*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 0); 294*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 1); 295*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 2); 296*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 3); 297*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 4); 298*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 5); 299*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 6); 300*c54f35caSApple OSS Distributions OSMetaClassDeclareReservedUnused(IOEventSource, 7); 301*c54f35caSApple OSS Distributions }; 302*c54f35caSApple OSS Distributions 303*c54f35caSApple OSS Distributions #endif /* !_IOKIT_IOEVENTSOURCE_H */ 304