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 __AppleSmartBatteryManager__
25#define __AppleSmartBatteryManager__
26
27#include <IOKit/IOService.h>
28#include <IOKit/smbus/IOSMBusController.h>
29#include "AppleSmartBattery.h"
30#include "AppleSmartBatteryManagerUserClient.h"
31
32
33void BattLog(const char *fmt, ...);
34
35class AppleSmartBattery;
36class AppleSmartBatteryManagerUserClient;
37
38/*
39 * Support for external transactions (from user space)
40 */
41enum {
42    kEXDefaultBatterySelector = 0,
43    kEXManagerSelector = 1
44};
45
46enum {
47    kEXReadWord     = 0,
48    kEXWriteWord    = 1,
49    kEXReadBlock    = 2,
50    kEXWriteBlock   = 3,
51    kEXReadByte     = 4,
52    kEXWriteByte    = 5,
53    kEXSendByte     = 6
54};
55
56enum {
57    kEXFlagRetry = 1
58};
59
60#define MAX_SMBUS_DATA_SIZE     32
61
62// * WriteBlock note
63// rdar://5433060
64// For block writes, clients always increment inByteCount +1
65// greater than the actual byte count. (e.g. 32 bytes to write becomes a 33 byte count.)
66// Other types of transactions are not affected by this workaround.
67typedef struct {
68    uint8_t         flags;
69    uint8_t         type;
70    uint8_t         batterySelector;
71    uint8_t         address;
72    uint8_t         inByteCount;
73    uint8_t         inBuf[MAX_SMBUS_DATA_SIZE];
74} EXSMBUSInputStruct;
75
76typedef struct {
77    uint32_t        status;
78    uint32_t        outByteCount;
79    uint32_t        outBuf[MAX_SMBUS_DATA_SIZE];
80} EXSMBUSOutputStruct;
81
82/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
83
84class AppleSmartBatteryManager : public IOService {
85    friend class AppleSmartBatteryManagerUserClient;
86
87    OSDeclareDefaultStructors(AppleSmartBatteryManager)
88
89public:
90    bool start(IOService *provider);
91
92    IOReturn performTransaction(IOSMBusTransaction * transaction,
93				    IOSMBusTransactionCompletion completion = 0,
94				    OSObject * target = 0,
95				    void * reference = 0);
96
97    IOReturn setPowerState(unsigned long which, IOService *whom);
98
99    IOReturn message(UInt32 type, IOService *provider, void * argument);
100
101    // Called by AppleSmartBattery
102    // Re-enables AC inflow if appropriate
103    void handleFullDischarge(void);
104
105    // bool argument true means "set", false means "clear" exclusive acces
106    // return: false means "exclusive access already granted", "true" means success
107    bool requestExclusiveSMBusAccess(bool request);
108
109    // Returns true if an exclusive AppleSmartBatteryUserClient is attached. False otherwise.
110    bool hasExclusiveClient(void);
111
112    bool requestPoll(int type);
113
114private:
115    // Called by AppleSmartBatteryManagerUserClient
116    IOReturn inhibitCharging(int level);
117
118    // Called by AppleSmartBatteryManagerUserClient
119    IOReturn disableInflow(int level);
120
121    // Called by AppleSmartBatteryManagerUserClient
122    // Called by Battery Updater application
123    IOReturn performExternalTransaction(
124                        void            *in,    // struct EXSMBUSInputStruct
125                        void            *out,   // struct EXSMBUSOutputStruct
126                        IOByteCount     inSize,
127                        IOByteCount     *outSize);
128
129    IOReturn performExternalTransactionGated(void *arg0, void *arg1,
130                                             void *arg2, void *arg3);
131
132    void    gatedSendCommand(int cmd, int level, IOReturn *ret_code);
133
134    // transactionCompletion is the guts of the state machine
135    bool    transactionCompletion(void *ref, IOSMBusTransaction *transaction);
136
137private:
138    IOSMBusTransaction          fTransaction;
139    IOCommandGate               * fBatteryGate;
140    IOCommandGate               * fManagerGate;
141    IOSMBusController           * fProvider;
142    AppleSmartBattery           * fBattery;
143    bool                        fExclusiveUserClient;
144};
145
146#endif
147