SamplePCIAudioDevice.cpp [plain text]
#include "SamplePCIAudioDevice.h"
#include "SamplePCIAudioEngine.h"
#include <IOKit/audio/IOAudioControl.h>
#include <IOKit/audio/IOAudioLevelControl.h>
#include <IOKit/audio/IOAudioToggleControl.h>
#include <IOKit/audio/IOAudioDefines.h>
#include <IOKit/IOLib.h>
#include <IOKit/pci/IOPCIDevice.h>
#define super IOAudioDevice
OSDefineMetaClassAndStructors(SamplePCIAudioDevice, IOAudioDevice)
bool SamplePCIAudioDevice::initHardware(IOService *provider)
{
bool result = false;
IOLog("SamplePCIAudioDevice[%p]::initHardware(%p)\n", this, provider);
if (!super::initHardware(provider)) {
goto Done;
}
pciDevice = OSDynamicCast(IOPCIDevice, provider);
if (!pciDevice) {
goto Done;
}
deviceMap = pciDevice->mapDeviceMemoryWithRegister(kIOPCIConfigBaseAddress0);
if (!deviceMap) {
goto Done;
}
deviceRegisters = (SamplePCIAudioDeviceRegisters *)deviceMap->getVirtualAddress();
if (!deviceRegisters) {
goto Done;
}
pciDevice->setMemoryEnable(true);
setDeviceName("Sample PCI Audio Device");
setDeviceShortName("PCIAudio");
setManufacturerName("My Company");
#error Put your own hardware initialization code here...and in other routines!!
if (!createAudioEngine()) {
goto Done;
}
result = true;
Done:
if (!result) {
if (deviceMap) {
deviceMap->release();
deviceMap = NULL;
}
}
return result;
}
void SamplePCIAudioDevice::free()
{
IOLog("SamplePCIAudioDevice[%p]::free()\n", this);
if (deviceMap) {
deviceMap->release();
deviceMap = NULL;
}
super::free();
}
bool SamplePCIAudioDevice::createAudioEngine()
{
bool result = false;
SamplePCIAudioEngine *audioEngine = NULL;
IOAudioControl *control;
IOLog("SamplePCIAudioDevice[%p]::createAudioEngine()\n", this);
audioEngine = new SamplePCIAudioEngine;
if (!audioEngine) {
goto Done;
}
if (!audioEngine->init(deviceRegisters)) {
goto Done;
}
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, (-22 << 16) + (32768), 0, kIOAudioControlChannelIDDefaultLeft,
kIOAudioControlChannelNameLeft,
0, kIOAudioControlUsageOutput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)volumeChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, (-22 << 16) + (32768), 0, kIOAudioControlChannelIDDefaultRight, kIOAudioControlChannelNameRight,
0, kIOAudioControlUsageOutput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)volumeChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioToggleControl::createMuteControl(false, kIOAudioControlChannelIDAll, kIOAudioControlChannelNameAll,
0, kIOAudioControlUsageOutput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)outputMuteChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, 0, (22 << 16) + (32768), kIOAudioControlChannelIDDefaultLeft,
kIOAudioControlChannelNameLeft,
0, kIOAudioControlUsageInput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)gainChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioLevelControl::createVolumeControl(65535, 0, 65535, 0, (22 << 16) + (32768), kIOAudioControlChannelIDDefaultRight, kIOAudioControlChannelNameRight,
0, kIOAudioControlUsageInput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)gainChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
control = IOAudioToggleControl::createMuteControl(false, kIOAudioControlChannelIDAll, kIOAudioControlChannelNameAll,
0, kIOAudioControlUsageInput);
if (!control) {
goto Done;
}
control->setValueChangeHandler((IOAudioControl::IntValueChangeHandler)inputMuteChangeHandler, this);
audioEngine->addDefaultAudioControl(control);
control->release();
activateAudioEngine(audioEngine);
audioEngine->release();
result = true;
Done:
if (!result && (audioEngine != NULL)) {
audioEngine->release();
}
return result;
}
IOReturn SamplePCIAudioDevice::volumeChangeHandler(IOService *target, IOAudioControl *volumeControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SamplePCIAudioDevice *audioDevice;
audioDevice = (SamplePCIAudioDevice *)target;
if (audioDevice) {
result = audioDevice->volumeChanged(volumeControl, oldValue, newValue);
}
return result;
}
IOReturn SamplePCIAudioDevice::volumeChanged(IOAudioControl *volumeControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SamplePCIAudioDevice[%p]::volumeChanged(%p, %ld, %ld)\n", this, volumeControl, oldValue, newValue);
if (volumeControl) {
IOLog("\t-> Channel %ld\n", volumeControl->getChannelID());
}
return kIOReturnSuccess;
}
IOReturn SamplePCIAudioDevice::outputMuteChangeHandler(IOService *target, IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SamplePCIAudioDevice *audioDevice;
audioDevice = (SamplePCIAudioDevice *)target;
if (audioDevice) {
result = audioDevice->outputMuteChanged(muteControl, oldValue, newValue);
}
return result;
}
IOReturn SamplePCIAudioDevice::outputMuteChanged(IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SamplePCIAudioDevice[%p]::outputMuteChanged(%p, %ld, %ld)\n", this, muteControl, oldValue, newValue);
return kIOReturnSuccess;
}
IOReturn SamplePCIAudioDevice::gainChangeHandler(IOService *target, IOAudioControl *gainControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SamplePCIAudioDevice *audioDevice;
audioDevice = (SamplePCIAudioDevice *)target;
if (audioDevice) {
result = audioDevice->gainChanged(gainControl, oldValue, newValue);
}
return result;
}
IOReturn SamplePCIAudioDevice::gainChanged(IOAudioControl *gainControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SamplePCIAudioDevice[%p]::gainChanged(%p, %ld, %ld)\n", this, gainControl, oldValue, newValue);
if (gainControl) {
IOLog("\t-> Channel %ld\n", gainControl->getChannelID());
}
return kIOReturnSuccess;
}
IOReturn SamplePCIAudioDevice::inputMuteChangeHandler(IOService *target, IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOReturn result = kIOReturnBadArgument;
SamplePCIAudioDevice *audioDevice;
audioDevice = (SamplePCIAudioDevice *)target;
if (audioDevice) {
result = audioDevice->inputMuteChanged(muteControl, oldValue, newValue);
}
return result;
}
IOReturn SamplePCIAudioDevice::inputMuteChanged(IOAudioControl *muteControl, SInt32 oldValue, SInt32 newValue)
{
IOLog("SamplePCIAudioDevice[%p]::inputMuteChanged(%p, %ld, %ld)\n", this, muteControl, oldValue, newValue);
return kIOReturnSuccess;
}