1/*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef __AppleSmartBattery__
25#define __AppleSmartBattery__
26
27#include <IOKit/IOService.h>
28#include <IOKit/pwr_mgt/IOPMPowerSource.h>
29#include <IOKit/smbus/IOSMBusController.h>
30#include <IOKit/acpi/IOACPIPlatformDevice.h>
31
32
33#include "AppleSmartBatteryCommands.h"
34#include "AppleSmartBatteryManager.h"
35
36#define kBatteryPollingDebugKey     "BatteryPollingPeriodOverride"
37
38class AppleSmartBatteryManager;
39
40typedef struct {
41    uint32_t cmd;
42    int addr;
43    int protocol;
44    uint32_t smcKey;
45    const OSSymbol *setItAndForgetItSym;
46    int pathBits;
47} CommandStruct;
48
49typedef struct {
50    CommandStruct   *table;
51    int             count;
52} CommandTable;
53
54class AppleSmartBattery : public IOPMPowerSource {
55    OSDeclareDefaultStructors(AppleSmartBattery)
56
57protected:
58    AppleSmartBatteryManager    *fProvider;
59    IOWorkLoop                  *fWorkLoop;
60    IOTimerEventSource          *fBatteryReadAllTimer;
61    bool                        fStalledByUserClient;
62    bool                        fCancelPolling;
63    bool                        fPollingNow;
64    IOSMBusTransaction          fTransaction;
65    uint16_t                    fMachinePath;
66    bool                        fRebootPolling;
67    uint8_t                     fReadingExtendedCmd;
68    bool                        fInflowDisabled;
69    bool                        fChargeInhibited;
70    uint16_t                    fRemainingCapacity;
71    uint16_t                    fFullChargeCapacity;
72
73    uint8_t                     fInitialPollCountdown;
74    uint8_t                     fIncompleteReadRetries;
75    int                         fRetryAttempts;
76
77    IOService *                 fPowerServiceToAck;
78    bool                        fSystemSleeping;
79
80    bool                        fPermanentFailure;
81    bool                        fFullyDischarged;
82    bool                        fFullyCharged;
83    int                         fBatteryPresent;
84    int                         fACConnected;
85    int                         fInstantCurrent;
86    int                         fAvgCurrent;
87    OSArray                     *fCellVoltages;
88
89    CommandTable                cmdTable;
90
91    IOACPIPlatformDevice        *fACPIProvider;
92
93
94
95    // Accessor for MaxError reading
96    // Percent error in MaxCapacity reading
97    void    setMaxErr(int error);
98    int     maxErr(void);
99
100    // SmartBattery reports a device name
101    void    setDeviceName(OSSymbol *sym);
102    OSSymbol *deviceName(void);
103
104    // Set when battery is fully charged;
105    // Clear when battery starts discharging/AC is removed
106    void    setFullyCharged(bool);
107    bool    fullyCharged(void);
108
109    // Time remaining estimate - as measured instantaneously
110    void    setInstantaneousTimeToEmpty(int seconds);
111
112    // Instantaneous amperage
113    void    setInstantAmperage(int mA);
114
115    // Time remaining estimate - 1 minute average
116    void    setAverageTimeToEmpty(int seconds);
117    int     averageTimeToEmpty(void);
118
119    // Time remaining until full estimate - 1 minute average
120    void    setAverageTimeToFull(int seconds);
121    int     averageTimeToFull(void);
122
123    void    setManufactureDate(int date);
124    int     manufactureDate(void);
125
126    void    setSerialNumber(uint16_t sernum);
127    uint16_t    serialNumber(void);
128
129    void    setChargeStatus(const OSSymbol *sym);
130    const OSSymbol    *chargeStatus(void);
131
132    // An OSData container of manufacturer specific data
133    void    setManufacturerData(uint8_t *buffer, uint32_t bufferSize);
134
135    void    oneTimeBatterySetup(void);
136
137    void    constructAppleSerialNumber(void);
138
139    CommandStruct *commandForState(uint32_t state);
140    void    initializeCommands(void);
141    bool    initiateTransaction(const CommandStruct *cs, bool retry);
142    bool    initiateNextTransaction(uint32_t state);
143    bool    retryCurrentTransaction(uint32_t state);
144    bool    handleSetItAndForgetIt(int state, int val16,
145                                   const uint8_t *str32, uint32_t len);
146
147public:
148    static AppleSmartBattery *smartBattery(void);
149    virtual bool init(void);
150    virtual bool start(IOService *provider);
151    bool    pollBatteryState(int path);
152    void    handleBatteryInserted(void);
153    void    handleBatteryRemoved(void);
154    void    handleInflowDisabled(bool inflow_state);
155    void    handleChargeInhibited(bool charge_state);
156    void    handleExclusiveAccess(bool exclusive);
157    IOReturn handleSystemSleepWake(IOService * powerService, bool isSystemSleep);
158
159protected:
160    void    logReadError( const char *error_type,
161                          uint16_t additional_error,
162                          IOSMBusTransaction *t);
163
164    void    clearBatteryState(bool do_update);
165
166    void    incompleteReadTimeOut(void);
167
168    void    rebuildLegacyIOBatteryInfo(void);
169
170    bool        transactionCompletion(void *ref, IOSMBusTransaction *transaction);
171    uint32_t    transactionCompletion_requiresRetryGetMicroSec(IOSMBusTransaction *transaction);
172    bool        transactionCompletion_shouldAbortTransactions(IOSMBusTransaction *transaction);
173    void        handlePollingFinished(bool visitedEntirePath);
174
175    IOReturn readWordAsync(uint32_t refnum, uint8_t address, uint8_t cmd);
176
177    IOReturn writeWordAsync(uint32_t refnum, uint8_t address, uint8_t cmd, uint16_t writeWord);
178
179    IOReturn readBlockAsync(uint32_t refnum, uint8_t address, uint8_t cmd);
180
181    void    acknowledgeSystemSleepWake( void );
182};
183
184#endif
185