1/* 2 * Copyright (c) 1998-2010 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 23/*! 24 * @header IOAudioDevice 25 */ 26 27#ifndef _IOKIT_IOAUDIODEVICE_H 28#define _IOKIT_IOAUDIODEVICE_H 29 30#include <IOKit/IOService.h> 31 32#ifndef IOAUDIOFAMILY_SELF_BUILD 33#include <IOKit/audio/IOAudioTypes.h> 34#include <IOKit/audio/IOAudioStream.h> 35#else 36#include "IOAudioTypes.h" 37#include "IOAudioStream.h" 38#endif 39 40class IOAudioEngine; 41class IOAudioStream; 42class IOAudioPort; 43class IOAudioControl; 44class OSDictionary; 45class OSSet; 46class OSArray; 47class IOTimerEventSource; 48class IOCommandGate; 49 50/*! 51 * @enum IOAudioDevicePowerState 52 * @abstract Identifies the power state of the audio device 53 * @discussion A newly created IOAudioDevices defaults to the idle state. 54 * @constant kIOAudioDeviceSleep State set when the system is going to sleep 55 * @constant kIOAudioDeviceIdle State when the system is awake but none of the IOAudioEngines are in use 56 * @constant kIOAudioDeviceActive State when one ore more IOAudioEngines are in use. This state transition must complete before the system will begin playing audio. 57 */ 58typedef enum _IOAudioDevicePowerState { 59 kIOAudioDeviceSleep = 0, // When sleeping 60 kIOAudioDeviceIdle = 1, // When no audio engines running 61 kIOAudioDeviceActive = 2 // audio engines running 62} IOAudioDevicePowerState; 63 64/*! 65 * @class IOAudioDevice 66 * @abstract Abstract base class for a single piece of audio hardware. The IOAudioDevice provides 67 * the central coordination point for an audio driver. 68 * @discussion An audio driver is required to subclass IOAudioDevice in order to provide 69 * working audio to the system. A single driver instance will contain a single instance of the 70 * driver's IOAudioDevice subclass. The subclass is responsible for mapping all hardware device 71 * resources from the service provider nub. It must control access to the hardware so that the 72 * hardware doesn't get into an inconsistent state. It is possible that different threads may make 73 * requests of the hardware at the same time. The IOAudioDevice superclass provides an IOWorkLoop 74 * and IOCommandGate on the IOWorkLoop through which all hardware accesses may be synchronized. 75 * All entry points to all parts of the driver controlled by the IOAudioFamily will be synchronized 76 * through this one IOWorkLoop. 77 * 78 * The IOAudioDevice subclass is responsible for creating the rest of the pieces of the driver. 79 * It must identify and create all IOAudioEngines that are not automatically created by the system 80 * (i.e. those that are not matched and instantiated by IOKit directly). 81 * 82 * The IOAudioDevice subclass must enumerate and create all IOAudioControls to match 83 * the device capabilities. 84 * 85 * It must also execute control value chages when requested by the system (i.e. volume adjustments). 86 * 87 * In order to allow sleep and wake to work on the system, the IOAudioDevice subclass is responsible 88 * for performing the necessary actions to sleep and wake its hardware (and restore necessary state 89 * on wake). 90 * 91 * The IOAudioDevice class provides timer services that allow different elements in the audio driver 92 * to receive timer notifications as needed. These services were designed with the idea that most 93 * timed events in a typical audio driver need to be done at least as often as a certain interval. 94 * Further, it is designed with the idea that being called more often than the specified interval 95 * doesn't hurt anything - and in fact may help. With this in mind, the timer services provided 96 * by the IOAudioDevice class allow different targets to register for timer callbacks at least as 97 * often as the specified interval. The actual interval will be the smallest of the intervals of 98 * all of the callbacks. This way, we avoid the overhead of having many timers in a single audio 99 * device. As an example, each output IOAudioEngine has a timer to run the erase head. It doesn't hurt 100 * to have the erase head run more often. Also, a typical IOAudioDevice subclass may need to run a timer 101 * to check for device state changes (e.g. jack insertions). 102 * 103 * There are a number of strings passed from the driver to the CoreAudio.framework and then into 104 * applications. All of those strings should be localized by the driver. In order to do that 105 * the kext bundle should have localized string files following the Mac OS X localization 106 * instructions. The IOAudioDevice should contain a property with the name of the bundle/kext 107 * that contains the localized string resources. To do that, the driver's personality in 108 * the bundle resources could have a property named 'IOAudioDeviceLocalizedBundle' with the path 109 * of the bundle/kext relative to '/System/Library/Extensions'. It could also be set by the 110 * IOAudioDevice subclass in its initHardware() function. To do so, call 111 * setProperty(kIOAudioDeviceLocalizedBundleKey, "Driver.kext"). 112 * 113 * In a typical driver, the IOAudioDevice subclass will implement initHardware() to perform the 114 * hardware initialization and driver construction. Within that initialization it must create at 115 * least one IOAudioEngine instance and activate it. In order to activate a new IOAudioEngine 116 * activateAudioEngine() should be called for each one. It must create the IOAudioControls 117 * matching the hardware capabilities to allow the system to set volume, mute and input selection. 118 * To add those controls to the driver, each control should be attached to the IOAudioEngine to 119 * which it applies by calling addDefaultAudioControl() on the IOAudioEngine. 120 * During initialization it should also call setDeviceName(), setDeviceShortName() and 121 * setManufacturerName() with localized strings representing each of the attributes. 122 * 123 * If the driver is to work properly after sleep/wake, it must implement performPowerStateChange() 124 * and deal with the sleep and wake transitions. It may also deal with the idle state transitions 125 * to turn off device power when it isn't in use (especially useful for devices attached to a 126 * portable running on battery power). 127 */ 128 129class IOAudioDevice : public IOService 130{ 131 friend class IOAudioEngine; 132 133 OSDeclareDefaultStructors(IOAudioDevice) 134 135protected: 136 /*! @var workLoop The IOWorkLoop for the driver - this is shared with the other objects in the driver */ 137 IOWorkLoop *workLoop; 138 /*! @var commandGate The IOCommandGate for this IOAudioDevice. It is attached to workLoop */ 139 IOCommandGate *commandGate; 140 /*! @var timerEventSource An IOTimerEventSource attached to workLoop used for the timer services */ 141 IOTimerEventSource *timerEventSource; 142 143 /*! @var duringStartup State variable set to true while the driver is starting up and false all other times */ 144 bool duringStartup; 145 /*! @var familyManagePower Set to true if the family is supposed to manage power - this is the default state. It can be changed early in the initialization process with a call to setFamilyManagePower(). */ 146 bool familyManagePower; 147 /*! @var asyncPowerStateChangeInProgress Set to true while an asynchronous power change is pending and false all other times. */ 148 bool asyncPowerStateChangeInProgress; 149 150 /*! @var numRunningAudioEngines The number of running IOAudioEngines. This is used to maintain idle vs active power state. */ 151 UInt32 numRunningAudioEngines; 152 153 /*! @var currentPowerState Used to track the existing power state - can be fetched by calling getPowerState() */ 154 IOAudioDevicePowerState currentPowerState; 155 /*! @var pendingPowerState If a power state change is in progress, this represents the pending power state. All other times this is the same as the currentPowerState. */ 156 IOAudioDevicePowerState pendingPowerState; 157 158 /*! @var audioEngines The set of IOAudioEngine objects vended by the IOAudioDevice. */ 159 OSArray * audioEngines; 160 /*! @var timerEvents 161 * @abstract The set of timer events in use by the device. 162 * @discussion The key for the dictionary is the target of the event. This means that a single target may 163 * have only a single event associated with it. 164 */ 165 OSDictionary * timerEvents; 166 /*! @var audioPorts The set of IOAudioPort objects associated with the IOAudioDevice */ 167 OSSet * audioPorts; 168 169 /*! @var minimumInterval The smallest timer interval requested by all timer event targets. */ 170 AbsoluteTime minimumInterval; 171 /*! @var previousTimerFire The time of the last timer event. 172 * @discussion This is used to schedule the next timer event. 173 */ 174 AbsoluteTime previousTimerFire; 175 176public: 177 /*! @var gIOAudioPlane 178 * A static IORegistryPlane representing the new IOAudioPlane that the IOAudioFamily uses 179 * to represent the signal chain of the device. 180 */ 181 static const IORegistryPlane *gIOAudioPlane; 182 183protected: 184 struct ExpansionData { 185 unsigned long long idleSleepDelayTime; 186 IOTimerEventSource * idleTimer; 187 }; 188 189 ExpansionData *reserved; 190 191public: 192 static void idleAudioSleepHandlerTimer(OSObject *owner, IOTimerEventSource *sender); 193 virtual IOReturn setAggressiveness(unsigned long type, unsigned long newLevel); 194 195 // OSMetaClassDeclareReservedUsed(IOAudioDevice, 0); 196 virtual void setDeviceTransportType(const UInt32 transportType); 197 198 // OSMetaClassDeclareReservedUsed(IOAudioDevice, 1); 199 /*! 200 * @function setIdleAudioSleepTime 201 * @abstract This function is to be called by a driver that doesn't want to be told about the audio 202 * going idle immediately, but at some point in the future. 203 * @discussion This is useful if the device will want to power down its hardware into an idle sleep 204 * state, but doesn't want to do that unless audio hasn't been used for a while. Calling this function 205 * immediately changes the idle sleep timer and queues it up if the idle is different from the previous 206 * idle time. The idle time defaults to 0, which means be called immediately (backwards compatible with 207 * previous versions of IOAudioFamily). A value of 0xffffffffffffffffULL means don't ever tell the 208 * driver about going idle. 209 * @param sleepDelay The amount of time, in nanoseconds, before the hardware should be told to go idle. 210 */ 211 virtual void setIdleAudioSleepTime(unsigned long long sleepDelay); 212 213 // OSMetaClassDeclareReservedUsed(IOAudioDevice, 2); 214 virtual void scheduleIdleAudioSleep(void); 215 216 // OSMetaClassDeclareReservedUsed(IOAudioDevice, 3); 217 /*! 218 * @function setConfigurationApplicationBundle 219 * @abstract This function is to be called if an external configuration application is available to set 220 * which application to launch. 221 * @discussion This is useful for device drivers that are too complex to be represented by the Sound Preferences 222 * panel. The bundle ID is a more flexible way of specifying where the application is than a hard coded path. 223 * @param bundleID The bundle ID of the application to be launched by the HAL for configuration of the device and its engine(s). 224 */ 225 virtual void setConfigurationApplicationBundle(const char *bundleID); 226 227 // OSMetaClassDeclareReservedUsed(IOAudioDevice, 4); 228 /*! 229 * @function setDeviceCanBeDefault 230 * @abstract This function is to be called to tell CoreAudio if this device shouldn't be a default device. 231 * @discussion This is useful for device drivers that don't want to be a default device. Can be called with 232 * kIOAudioDeviceCanBeDefaultNothing to prevent CoreAudio from allowing this device to be any default device, or it 233 * can be called with any combination of kIOAudioDeviceCanBeDefaultInput, kIOAudioDeviceCanBeDefaultOutput, or 234 * kIOAudioDeviceCanBeSystemOutput. The default is 235 * (kIOAudioDeviceCanBeDefaultInput | kIOAudioDeviceCanBeDefaultOutput | kIOAudioDeviceCanBeSystemOutput). 236 * @param defaultsFlags The flags to instruct CoreAudio to allow this device to be only the indicated default devices. 237 */ 238 virtual void setDeviceCanBeDefault(UInt32 defaultsFlags); 239 240 // OSMetaClassDeclareReservedUsed(IOAudioDevice, 5); 241 virtual void setDeviceModelName(const char * modelName); 242 243private: 244 OSMetaClassDeclareReservedUsed(IOAudioDevice, 0); 245 OSMetaClassDeclareReservedUsed(IOAudioDevice, 1); 246 OSMetaClassDeclareReservedUsed(IOAudioDevice, 2); 247 OSMetaClassDeclareReservedUsed(IOAudioDevice, 3); 248 OSMetaClassDeclareReservedUsed(IOAudioDevice, 4); 249 OSMetaClassDeclareReservedUsed(IOAudioDevice, 5); 250 251 OSMetaClassDeclareReservedUnused(IOAudioDevice, 6); 252 OSMetaClassDeclareReservedUnused(IOAudioDevice, 7); 253 OSMetaClassDeclareReservedUnused(IOAudioDevice, 8); 254 OSMetaClassDeclareReservedUnused(IOAudioDevice, 9); 255 OSMetaClassDeclareReservedUnused(IOAudioDevice, 10); 256 OSMetaClassDeclareReservedUnused(IOAudioDevice, 11); 257 OSMetaClassDeclareReservedUnused(IOAudioDevice, 12); 258 OSMetaClassDeclareReservedUnused(IOAudioDevice, 13); 259 OSMetaClassDeclareReservedUnused(IOAudioDevice, 14); 260 OSMetaClassDeclareReservedUnused(IOAudioDevice, 15); 261 OSMetaClassDeclareReservedUnused(IOAudioDevice, 16); 262 OSMetaClassDeclareReservedUnused(IOAudioDevice, 17); 263 OSMetaClassDeclareReservedUnused(IOAudioDevice, 18); 264 OSMetaClassDeclareReservedUnused(IOAudioDevice, 19); 265 OSMetaClassDeclareReservedUnused(IOAudioDevice, 20); 266 OSMetaClassDeclareReservedUnused(IOAudioDevice, 21); 267 OSMetaClassDeclareReservedUnused(IOAudioDevice, 22); 268 OSMetaClassDeclareReservedUnused(IOAudioDevice, 23); 269 OSMetaClassDeclareReservedUnused(IOAudioDevice, 24); 270 OSMetaClassDeclareReservedUnused(IOAudioDevice, 25); 271 OSMetaClassDeclareReservedUnused(IOAudioDevice, 26); 272 OSMetaClassDeclareReservedUnused(IOAudioDevice, 27); 273 OSMetaClassDeclareReservedUnused(IOAudioDevice, 28); 274 OSMetaClassDeclareReservedUnused(IOAudioDevice, 29); 275 OSMetaClassDeclareReservedUnused(IOAudioDevice, 30); 276 OSMetaClassDeclareReservedUnused(IOAudioDevice, 31); 277 278 279public: 280 // Initialization 281 282 /*! 283 * @function init 284 * @abstract Initialize a newly created instance of IOAudioDevice. 285 * @discussion This implementation initializes all of the data structures and variables used by the 286 * IOAudioDevice. The currentPowerState and pendingPowerState variables are set to kIOAudioDeviceIdle. 287 * A subclass that overrides this method must call the superclass' implementation. 288 * @param properties 289 * An OSDictionary of the device properties that gets passed to super::init and set 290 * in the IORegistry. 291 * @result true if initialization was successful 292 */ 293 virtual bool init(OSDictionary *properties); 294 295 /*! 296 * @function free 297 * @abstract Frees resources used by the IOAudioDevice instance 298 * @discussion This method will deactivate all audio audio engines and release the audioEngines OSSet. 299 * It will also deactivate all of the audio ports and release the audioPorts OSSet. It will release 300 * the timerEvents OSDictionary as well as cancel any outstanding timer callbacks. It will clean up 301 * all of the event sources and the workLoop. 302 * 303 * Do not call this directly. This is called automatically by the system when the instance's 304 * refcount goes to 0. To decrement the refcount, call release() on the object. 305 */ 306 307 virtual void free(); 308 309 /*! 310 * @function start 311 * @abstract This function is called automatically by the system to tell the driver to start vending 312 * services to the rest of the system. 313 * @discussion The start() implementation in IOAudioDevice starts by calling start() on its superclass. 314 * It then calls initHardware() which the subclass should override to properly initialize itself and 315 * the hardware. If that call succeeds, it sets up power management if the family is supposed to 316 * manage power (checking the familyManagePower variable). Then finally it calls registerService() 317 * to make the IOAudioDevice visible in the IORegistry. 318 * @param provider 319 * This is the service provider nub that provides access to the hardware resources. 320 * @result Returns true on success 321 */ 322 virtual bool start(IOService *provider); 323 324 /*! 325 * @function stop 326 * @abstract This is responsible for stopping the device after the system is done with it (or 327 * if the device is removed from the system). 328 * @discussion The IOAudioDevice implentation of stop() disables the timer services, deactivates 329 * all of the audio audio engines and audio ports and stops power management of the device. 330 * The audio engine and port deactivation causes all of the audio engines to get stopped and 331 * all of the audio engine and port resources and objects to be released. A subclass' implementation 332 * may could shut down hardware here if necessary. If this function is overridden by a subclass, 333 * the superclass' implementation must be called. 334 * @param provider 335 * The service provider nub for the device. 336 */ 337 virtual void stop(IOService *provider); 338 virtual bool willTerminate(IOService *provider, IOOptionBits options); 339 340 /*! 341 * @function initHardware 342 * @abstract This function is called by start() to provide a convenient place for the subclass to 343 * perform its initialization. 344 * @discussion In a typical implementation, a driver would implementation this function and perform 345 * a number of tasks. Those include mapping hardware resources, initializing the hardware to a known 346 * state, creating the IOAudioEngines, IOAudioControls and IOAudioStreams. Additionally it 347 * should also call setDeviceName(), setDeviceShortName(), setManufacturerName(). Upon return of 348 * this function, the device should be ready to begin vending services to the system. 349 * @param provider 350 * The service provider nub for the device. 351 * @result This function should return true on a successful initialization. 352 */ 353 virtual bool initHardware(IOService *provider); 354 355 /*! 356 * @function setDeviceName 357 * @abstract Sets the name of the device 358 * @discussion This method should be called during initialization or startup. It should 359 * be set by the time initHardware() completes. The device name is used by the 360 * CoreAudio.framework to identify the particular piece of hardware. This string should 361 * should be localized by the driver. 362 */ 363 virtual void setDeviceName(const char *deviceName); 364 365 /*! 366 * @function setDeviceShortName 367 * @abstract Sets the short name of the device 368 * @discussion The short name is a shorter representation of the device name. It may be used 369 * by applications when the device name is too long. It should be set by the time initHardware() 370 * completes. The string should be localized by the driver. 371 */ 372 virtual void setDeviceShortName(const char *shortName); 373 374 /*! 375 * @function setManufacturerName 376 * @abstract Sets the manufacturer name of the device 377 * @discussion This method should be called during initialization or startup. This should be 378 * called by the time initHardware() completes. The string should be localized by the driver. 379 */ 380 virtual void setManufacturerName(const char *manufacturerName); 381 382 383 // IOWorkLoop, IOCommandGate management 384 385 /*! 386 * @function getWorkLoop 387 * @abstract Returns the IOWorkLoop for the driver 388 * @discussion The IOWorkLoop is used to synchronized all critical aspects of the driver. This 389 * includes all entry points to the driver as well as power management. 390 */ 391 virtual IOWorkLoop *getWorkLoop() const; 392 393 /*! 394 * @function getCommandGate 395 * @abstract Returns the IOCommandGate for this IOAudioDevice 396 * @discussion This IOCommandGate allows calls into this IOAudioDevice to be synchronized on 397 * the IOWorkLoop. 398 */ 399 virtual IOCommandGate *getCommandGate() const; 400 401 402 // IOAudioEngine management 403 404 /*! 405 * @function activateAudioEngine 406 * @abstract This simply calls activateAudioEngine(IOAudioEngine *audioEngine, 407 * bool shouldStartAudioEngine) with a value of true for shouldStartAudioEngine. 408 * @param audioEngine 409 * The IOAudioEngine instance to be activated. It is treated as a newly 410 * allocated instance. 411 * @result Returns true if the audio engine was successfully activated. 412 */ 413 virtual IOReturn activateAudioEngine(IOAudioEngine *audioEngine); 414 415 /*! 416 * @function activateAudioEngine 417 * @abstract This is called to add a new IOAudioEngine object to the IOAudioDevice. 418 * @discussion Once the IOAudioEngine has been activated by this function, it is ready 419 * to begin moving audio data. This should be called either during the subclass' initHardware() 420 * implementation for each IOAudioEngine the device creates. Or it should be called by 421 * the IOAudioEngine itself if the audio engine was automatically created by IOKit's matching 422 * process. The system won't be able to properly track and control IOAudioEngines if 423 * they are not activated though this function. 424 * This implementation will retain the IOAudioEngine while it maintains control of it. 425 * When the audio engine is deactivated, the IOAudioEngine will be released. If the 426 * IOAudioDevice subclass is passing a newly instantiated IOAudioEngine, it will need to release 427 * the audio engine after it has been activated. This will insure that the refCount on the audio engine 428 * is correct when it gets deactivated when the driver is stopped. That allows the audio engine to be 429 * freed when it is no longer needed. 430 * @param audioEngine 431 * The IOAudioEngine instance to be activated. 432 * @param shouldStartAudioEngine 433 * If true, the audio engine is treated as a newly allocated IOAudioEngine 434 * instance and is appropriately attached and started according to IOKit convention. If it is false 435 * it is assumed that some other process (possibly the IOKit matching process) has started the 436 * IOAudioEngine and will skip that step. 437 * @result Returns true if the audio engine was successfully activated. 438 */ 439 virtual IOReturn activateAudioEngine(IOAudioEngine *audioEngine, bool shouldStartAudioEngine); 440 441 /*! 442 * @function deactivateAllAudioEngines 443 * @abstract Deactivates all of the audio engines in the device. 444 * @discussion This is called by the stop() and free() methods in IOAudioDevice to completely 445 * shut down all audio engines as the driver is being shut down. 446 */ 447 virtual void deactivateAllAudioEngines(); 448 449 450 // Power management 451 452 /*! 453 * @function setFamilyManagePower 454 * @abstract Called set whether or not the family should manage the device power throught the 455 * IOService power management APIs. 456 * @discussion The default behavior is for the family to manage power. It is only necessary to 457 * call this function if the driver does not want the family to manage power. It is not 458 * recommended that this function be called because it makes power management much more 459 * difficult for the driver. If this function is to be called, it must be called before 460 * initHardware() completes. Immediately after initHardware() is called by start(), 461 * the power management system is initialized if the family is to manage power. 462 * @param manage Set to false if it is not desired that the family does the power management 463 */ 464 virtual void setFamilyManagePower(bool manage); 465 466 /*! 467 * @function setPowerState 468 * @abstract Called by the power management system in IOService when the power state of this 469 * service needs to change. 470 * @discussion The default implementation of IOAudioDevice sets up two power states for IOService 471 * to use. State 0 is sleep and state 1 is wake. This function should not be called directly. 472 * It is only supposed to be used by the IOService power management services. 473 * @param powerStateOrdinal 474 * The number of the power state as defined by the IOAudioDevice - 475 * 0 for sleep, 1 for wake. 476 * @param device The power management policy maker. 477 * @result Returns kIOPMAckImplied (0) when the power state change is complete. Otherwise the an 478 * upper bound on the number of microseconds until the state change is complete is returned. 479 */ 480 virtual IOReturn setPowerState(unsigned long powerStateOrdinal, IOService *device); 481 482 /*! 483 * @function setPowerStateAction 484 * @abstract IOCommandGate Action which calls protectedSetPowerState() while holding the IOCommandGate 485 * @discussion This is needed to allow protectedSetPowerState() to be called on the IOWorkLoop 486 * @param owner The owner of the IOCommandGate (the IOAudioDevice in this case) 487 * @param arg1 The powerStateOrdinal to be passed to protectedSetPowerState() 488 * @param arg2 The device to be passed to protectedSetPowerState() 489 * @result Returns the result of protectedSetPowerState() 490 */ 491 static IOReturn setPowerStateAction(OSObject *owner, void *arg1, void *arg2, void *arg3, void *arg4); 492 493 static IOReturn _setPowerStateAction(OSObject *target, void *arg0, void *arg1, void *arg2, void *arg3); // <rdar://8508064> 494 495 /*! 496 * @function protectedSetPowerState 497 * @abstract Called by setPowerStateAction() to deal with a power state change from the IOService 498 * power management facility. 499 * @discussion This function is responsible for performing the necessary sleep and wake tasks 500 * when the system is sleeping or waking. If an outstanding power state change is in progress, 501 * it will wait until the state changes has completed. While sleeping, all audio engines are 502 * stopped before calling initiatePowerStateChange() to call performPowerStateChange() to let 503 * the driver deal with the sleep request. When waking, it determines if the device should be 504 * idle or active and continues to call initiatePowerStateChange(). If initiatePowerStateChange() 505 * indicates that the power state change is occuring asynchronously, it returns the number of 506 * microseconds. This function must be called on the IOWorkLoop, but should not be called 507 * directly. 508 * @param powerStateOrdinal Param passed to setPowerState() - 0 for sleep, 1 for wake 509 * @param device Param passed to setPowerState - the device initiating the power state change 510 * @result Returns 0 if the power state change is complete - the number of microseconds until 511 * complete if its asynchronous. 512 */ 513 virtual IOReturn protectedSetPowerState(unsigned long powerStateOrdinal, IOService *device); 514 515 /*! 516 * @function performPowerStateChange 517 * @abstract This function is called by the IOAudioDevice when a power state change is needed. 518 * @discussion In order to deal with power state changes, a subclass must override this function. 519 * Any combination of old and new power states may be passed to this function. If work is to 520 * be performed while transitioning to sleep, check for a newPowerState of kIOAudioDeviceSleep. 521 * If work is to be performed while transitioning from sleep, check for an oldPowerState of 522 * kIOAudioDeviceSleep. A power state of kIOAudioDeviceIdle means the system is awake, but 523 * no clients are currently playing or recording audio (i.e. no IOAudioEngines are active). 524 * A power state of kIOAudioDeviceActive means that at least one IOAudioEngine is active. 525 * It is possible for a power state change to be performed synchronously or asynchronously. 526 * In the case of a synchronous power state change, simple leave microsecondsUntilComplete 527 * alone and return kIOReturnSuccess. If an asynchronous power state change is needed the 528 * driver should do whatever needed to schedule another thread to finish the state change 529 * and set the microsecondsUntilComplete to an upper bound on the amount of time it will 530 * take to complete the power state change. Then when the power state change is complete, 531 * a call must be made to completePowerStateChange(). During an asynchronous power state 532 * change, the current power state will remain the same as before the transition began, 533 * and the pendingPowerState is set to the new power state that will be set when the 534 * change is complete. 535 * @param oldPowerState The power state before the power state change 536 * @param newPowerState The power state being transitioned to 537 * @param microsecondsUntilComplete 538 * A pointer to a value representing an upper bound on 539 * the number of microseconds to complete an asynchronous power state change. It points 540 * to a value of zero at the start and if it remains zero, the state change is complete 541 * upon a successful return from the function. 542 * @result Returns kIOReturnSuccess on a successful completion 543 */ 544 virtual IOReturn performPowerStateChange(IOAudioDevicePowerState oldPowerState, 545 IOAudioDevicePowerState newPowerState, 546 UInt32 *microsecondsUntilComplete); 547 548 /*! 549 * @function completePowerStateChange 550 * @abstract Called when a power state change is complete 551 * @discussion In the case of an asynchronous power state change, a subclass is responsible 552 * for calling this function. It is safe to call this function if not on the IOWorkLoop. 553 * This function calls protectedCompletePowerStateChange() through the IOCommandGate and 554 * completePowerStateChangeAction(). If the call is already on the IOWorkLoop, it is safe 555 * to call protectedCompletePowerStateChange() directly. 556 * @result Returns kIOReturnSuccess on a successful completion 557 */ 558 virtual IOReturn completePowerStateChange(); 559 560 /*! 561 * @function completePowerStateChangeAction 562 * @abstract IOCommandGate Action which calls protectedCompletePowerStateChange() while holding the 563 * IOCommandGate. 564 * @discussion This is needed to allow protectedCompletePowerStateChange() to be called on the IOWorkLoop. 565 * @param owner The owner of the IOCommandGate (the IOAudioDevice in this case) 566 * @result Returns the result of protectedCompletePowerStateChange() 567 */ 568 static IOReturn completePowerStateChangeAction(OSObject *owner, void *arg1, void *arg2, void *arg3, void *arg4); 569 570 /*! 571 * @function protectedCompletePowerStateChange 572 * @abstract Called on the IOWorkLoop when a power state change is complete. 573 * @discussion This function does the work to complete a power state change (both synchronous and 574 * asynchronous). If the system is waking from sleep, the timer system is restarted and the 575 * audio engines are resumed. If this was called as a result of an asynchronous power state changed 576 * it makes the IOService power management call acknowledgePowerChange() and resets the 577 * asyncPowerStateChangeInProgress variable. Finally it sets the currentPowerState to the 578 * pendingPowerState. This function must be called on the IOWorkLoop. If a subclass is not 579 * on the IOWorkLoop (e.g. holding the IOCommandGate), call completePowerStateChange() instead. 580 * @result Returns kIOReturnSuccess on success 581 */ 582 virtual IOReturn protectedCompletePowerStateChange(); 583 584 /*! 585 * @function getPowerState 586 * @abstract Returns the current power state (the old power state if a change is in progress). 587 * @result The current power state 588 */ 589 virtual IOAudioDevicePowerState getPowerState(); 590 591 /*! 592 * @function getPendingPowerState 593 * @abstract Returns the pending power state if a state change is in progress. Otherwise it 594 * returns the current power state change. 595 * @result The pending power state 596 */ 597 virtual IOAudioDevicePowerState getPendingPowerState(); 598 599 /*! 600 * @function waitForPendingPowerStateChange 601 * @abstract Called internally to wait until a pending power state change is complete. 602 * @discussion This is only used by internal functions to wait during pending power 603 * state changes. It is used to prevent multiple power state changes at the same time. 604 * This function must be called while holding the IOCommandGate. If an asynchronous 605 * power state change is in progress this function will block until the state change 606 * if complete. Once complete, it will return while still holding the IOCommandGate. 607 */ 608 virtual void waitForPendingPowerStateChange(); 609 610 /*! 611 * @function initiatePowerStateChange 612 * @abstract Called internally to execute a power state change 613 * @discussion This function must be called on the IOWorkLoop. It calls performPowerStateChange() 614 * to let the driver process the state change. If the state change is completed synchronously 615 * by the driver (subclass) it calls protectedCompletePowerStateChange(). If done asynchronously 616 * it returns the microsecondsUntilComplete that was set by performPowerStateChange(). This 617 * function should not be called directly. 618 * @param microsecondsUntilComplete 619 * Pointer to the microsecondsUntilComplete that should be set 620 * by performPowerStateChange if an asynchronous power state change was started. 621 * @result Returns kIOReturnSuccess on success 622 */ 623 virtual IOReturn initiatePowerStateChange(UInt32 *microsecondsUntilComplete = NULL); 624 625 626 // IOAudioControl management 627 628 /*! 629 * @function flushAudioControls 630 * @abstract Forces each IOAudioControl in the driver to have its value flushed out to the hardware. 631 * That will cause either the IOAudioControl's ValueChangeHandler to be called. 632 * @discussion This can be used to force the hardware to get updated with the current value 633 * of each control. It may be useful during wake for example. 634 */ 635 virtual void flushAudioControls(); 636 637 638 // Timer services 639 640 /*! 641 * @typedef TimerEvent 642 * @abstract Generic timer event callback for IOAudioDevice timer targets 643 * @discussion TimerEvent callback function takes two arguments; the target of 644 * the timer event and the IOAudioDevice sending the event. 645 * @param target The target of the timer event - passed in when the timer event was registered 646 * @param audioDevice The IOAudioDevice sending the event 647 */ 648 typedef void (*TimerEvent)(OSObject *target, IOAudioDevice *audioDevice); 649 650 /*! 651 * @function addTimerEvent 652 * @abstract Adds a TimerEvent callback for the given target called at least as often 653 * as specified in interval. 654 * @discussion The frequency of the timer event callbacks will be the smallest interval 655 * specified by all targets. Only one interval and callback may be specified per target. 656 * If a addTimerEvent is called twice with the same target, the second one overrides the 657 * first. There is currently a bug triggered if the first call had the smallest interval. 658 * In that case, that smallest interval would still be used. 659 * @param target This parameter is the target object of the TimerEvent. 660 * @param event The callback function called each time the timer fires. 661 * @param interval The callback will be called at least this often. 662 * @result Returns kIOReturnSuccess if the timer event was successfully added. 663 */ 664 virtual IOReturn addTimerEvent(OSObject *target, TimerEvent event, AbsoluteTime interval); 665 666 /*! 667 * @function removeTimerEvent 668 * @abstract Removes the timer event for the given target. 669 * @discussion If the interval for the target to be removed is the smallest interval, 670 * the timer interval is recalculated based on the remaining targets. The next fire 671 * time is readjusted based on the new interval compared to the last fire time. 672 * @param target The target whose timer event will be removed. 673 */ 674 virtual void removeTimerEvent(OSObject *target); 675 676 /*! 677 * @function removeAllTimerEvents 678 * @abstract Removes all timer events and stops the timer 679 * @discussion Called during teardown of the driver 680 */ 681 virtual void removeAllTimerEvents(); 682 683 684 // IOAudioPort management 685 686 /*! 687 * @function attachAudioPort 688 * @abstract Adds the port to the IOAudioDevice's list of ports and attaches the port to its parent 689 * and attaches the child to the port. 690 * @discussion This function provides the functionality to represent the device's signal chain in the 691 * IOAudioPlane in the IORegistry. An IOAudioPort's parent(s) are before it in the signal chain 692 * and its children are after it. This method may be called multiple times for a single IOAudioPort. 693 * This is necessary when there are many children or parents. Once a relationship is made, it is not 694 * necessary to make the reverse relationship. A NULL value may be passed in for either the parent 695 * or child or both. 696 * The IOAudioPort passed in should be a newly allocated IOAudioPort instance. This function will 697 * appropriately attach and start the port object. NOTE: It is not necessary to use IOAudioPorts 698 * in a fully functional audio driver. 699 * @param port The newly created IOAudioPort instance to be activated. 700 * @param parent A parent IOAudioPort or IOAudioEngine of the given port. 701 * @param child A child IOAudioPort or IOAudioEngine of the given port. 702 * @result Returns true when the port has been successfully added and attached. 703 */ 704 virtual IOReturn attachAudioPort(IOAudioPort *port, IORegistryEntry *parent, IORegistryEntry *child); 705 706 /*! 707 * @function detachAllAudioPorts 708 * @abstract Deactivates all of the ports in the device. 709 * @discussion This is called by the stop() and free() methods in IOAudioDevice to completely 710 * shut down all ports as the driver is being shut down. 711 */ 712 virtual void detachAllAudioPorts(); 713 714protected: 715 /*! 716 * @function timerFired 717 * @abstract Internal static function called when the timer fires. 718 * @discussion This function simply calls dispatchTimerEvents() on the IOAudioDevice to do just that. 719 * @param target The IOAudioDevice instance that initiated the timer callback. 720 * @param sender The IOTimerEventSources calling this callback 721 */ 722 static void timerFired(OSObject *target, IOTimerEventSource *sender); 723 724 /*! 725 * @function dispatchTimerEvents 726 * @abstract Called by timerFired() to cause the timer event callbacks to be called. 727 * @discussion This method iterates through all of the timer event targets and calls 728 * the callback on each. Unless the force flag is set to true, the timer events will 729 * only be dispatched if the power state is not kIOAudioDeviceSleep. This prevents 730 * unexpected timer firings while making wake->sleep->wake transitions. This function must 731 * be called on the IOWorkLoop. 732 * @param force 733 * A bool param to allow the timer events to be dispatched even if the 734 * device is in the kIOAudioDeviceSleep power state. 735 */ 736 virtual void dispatchTimerEvents(bool force); 737 738 /*! 739 * @function audioEngineStarting 740 * @abstract Called by IOAudioEngine when it is starting up 741 * @discussion This should only be called while on the IOWorkLoop. It is not intended to be called 742 * directly. It is called when an IOAudioEngine is starting up to allow the IOAudioDevice 743 * to keep track of running audio engines and change the power state from kIOAudioDeviceIdle to 744 * kIOAudioDeviceActive when the first audio engine starts up. If the state change is done 745 * asynchronously, it waits for the state change to complete. This is to ensure that the 746 * system doesn't start playing audio until the IOAudioDevice has completed its transition 747 * to kIOAudioDeviceActive. 748 */ 749 virtual void audioEngineStarting(); 750 751 /*! 752 * @function audioEngineStopped 753 * @abstract Called by IOAudioEngine when it has stopped 754 * @discussion This should only be called while on the IOWorkLoop. It is not intended to be called 755 * directly. It is called when an IOAudioEngine has stopped to allow the IOAudioDevice 756 * to keep track of running audio engines and change the power state from kIOAudioDeviceActive 757 * to kIOAudioDeviceIdle when the last audio engine stops. If the state change is done 758 * asynchronously, it waits for the state change to complete. 759 */ 760 virtual void audioEngineStopped(); 761 762}; 763 764#endif /* _IOKIT_IOAUDIODEVICE_H */ 765