1/*
2 * Copyright (c) 1998-2002 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// public
24#include <IOKit/firewire/IOFireWirePowerManager.h>
25// protected
26#include <IOKit/firewire/IOFireWireController.h>
27
28// private
29#import "FWDebugging.h"
30
31OSDefineMetaClassAndStructors(IOFireWirePowerManager, OSObject)
32OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 0);
33OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 1);
34OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 2);
35OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 3);
36OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 4);
37OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 5);
38OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 6);
39OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 7);
40OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 8);
41OSMetaClassDefineReservedUnused(IOFireWirePowerManager, 9);
42
43#pragma mark -
44
45/////////////////////////////////////////////////////////////////////////////
46
47// createWithController
48//
49//
50
51IOFireWirePowerManager * IOFireWirePowerManager::createWithController( IOFireWireController * controller )
52{
53    IOFireWirePowerManager * me = OSTypeAlloc( IOFireWirePowerManager );
54	if( me != NULL )
55	{
56		if( !me->initWithController(controller) )
57		{
58            me->release();
59            me = NULL;
60        }
61	}
62
63    return me;
64}
65
66// initWithController
67//
68//
69
70bool IOFireWirePowerManager::initWithController( IOFireWireController * controller )
71{
72	bool success = true;		// assume success
73
74	// init super
75
76    if( !OSObject::init() )
77        success = false;
78
79	if( success )
80	{
81		fControl = controller;
82		fMaximumDeciwatts = 0;
83		fAllocatedDeciwatts = 0;
84	}
85
86	return success;
87}
88
89#pragma mark -
90
91/////////////////////////////////////////////////////////////////////////////
92
93// setMaximumDeciwatts
94//
95//
96
97void IOFireWirePowerManager::setMaximumDeciwatts( UInt32 deciwatts )
98{
99	FWKLOG(( "IOFireWirePowerManager::setMaximumDeciwatts - setting maximum milliwats to %d\n", deciwatts ));
100
101	fMaximumDeciwatts = deciwatts;
102}
103
104// allocateDeciwatts
105//
106//
107
108IOReturn IOFireWirePowerManager::allocateDeciwatts( UInt32 deciwatts )
109{
110	IOReturn status = kIOReturnSuccess;
111
112	fControl->closeGate();
113
114	FWKLOG(( "IOFireWirePowerManager::allocateDeciwatts - allocating %d deciwatts\n", deciwatts ));
115
116	if( fAllocatedDeciwatts + deciwatts <= fMaximumDeciwatts )
117	{
118		fAllocatedDeciwatts += deciwatts;
119	}
120	else
121	{
122		status = kIOReturnNoResources;
123	}
124
125	fControl->openGate();
126
127	return status;
128}
129
130// deallocateDeciwatts
131//
132//
133
134void IOFireWirePowerManager::deallocateDeciwatts( UInt32 deciwatts )
135{
136	fControl->closeGate();
137
138	FWKLOG(( "IOFireWirePowerManager::deallocateDeciwatts - freeing %d deciwatts\n", deciwatts ));
139
140	if( deciwatts <= fAllocatedDeciwatts )
141	{
142		fAllocatedDeciwatts -= deciwatts;
143	}
144	else
145	{
146		IOLog( "IOFireWirePowerManager::deallocateDeciwatts - freed deciwatts %d > allocated deciwatts %d!\n", (uint32_t)deciwatts, (uint32_t)fAllocatedDeciwatts );
147		fAllocatedDeciwatts = 0;
148	}
149
150	// notify clients that more power has been made available
151	if( deciwatts != 0 )
152	{
153		fControl->messageClients( kIOFWMessagePowerStateChanged );
154	}
155
156	fControl->openGate();
157}
158