Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1 | /* |
| 2 | * acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $) |
| 3 | * |
| 4 | * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu> |
| 5 | * |
| 6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or (at |
| 11 | * your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 | * |
| 22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | */ |
| 24 | |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/proc_fs.h> |
| 30 | #include <linux/seq_file.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <linux/acpi.h> |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 33 | #include <linux/timer.h> |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 34 | #include <linux/jiffies.h> |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 35 | #include <linux/delay.h> |
| 36 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 37 | #include "sbshc.h" |
| 38 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 39 | #define ACPI_SBS_COMPONENT 0x00080000 |
| 40 | #define ACPI_SBS_CLASS "sbs" |
| 41 | #define ACPI_AC_CLASS "ac_adapter" |
| 42 | #define ACPI_BATTERY_CLASS "battery" |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 43 | #define ACPI_SBS_DEVICE_NAME "Smart Battery System" |
| 44 | #define ACPI_SBS_FILE_INFO "info" |
| 45 | #define ACPI_SBS_FILE_STATE "state" |
| 46 | #define ACPI_SBS_FILE_ALARM "alarm" |
| 47 | #define ACPI_BATTERY_DIR_NAME "BAT%i" |
| 48 | #define ACPI_AC_DIR_NAME "AC0" |
| 49 | #define ACPI_SBC_SMBUS_ADDR 0x9 |
| 50 | #define ACPI_SBSM_SMBUS_ADDR 0xa |
| 51 | #define ACPI_SB_SMBUS_ADDR 0xb |
| 52 | #define ACPI_SBS_AC_NOTIFY_STATUS 0x80 |
| 53 | #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80 |
| 54 | #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81 |
| 55 | |
| 56 | #define _COMPONENT ACPI_SBS_COMPONENT |
| 57 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 58 | ACPI_MODULE_NAME("sbs"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 59 | |
| 60 | MODULE_AUTHOR("Rich Townsend"); |
| 61 | MODULE_DESCRIPTION("Smart Battery System ACPI interface driver"); |
| 62 | MODULE_LICENSE("GPL"); |
| 63 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 64 | #define DEF_CAPACITY_UNIT 3 |
| 65 | #define MAH_CAPACITY_UNIT 1 |
| 66 | #define MWH_CAPACITY_UNIT 2 |
| 67 | #define CAPACITY_UNIT DEF_CAPACITY_UNIT |
| 68 | |
| 69 | #define REQUEST_UPDATE_MODE 1 |
| 70 | #define QUEUE_UPDATE_MODE 2 |
| 71 | |
| 72 | #define DATA_TYPE_COMMON 0 |
| 73 | #define DATA_TYPE_INFO 1 |
| 74 | #define DATA_TYPE_STATE 2 |
| 75 | #define DATA_TYPE_ALARM 3 |
| 76 | #define DATA_TYPE_AC_STATE 4 |
| 77 | |
| 78 | extern struct proc_dir_entry *acpi_lock_ac_dir(void); |
| 79 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
| 80 | extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); |
| 81 | extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); |
| 82 | |
| 83 | #define MAX_SBS_BAT 4 |
| 84 | #define ACPI_SBS_BLOCK_MAX 32 |
| 85 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 86 | #define UPDATE_DELAY 10 |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 87 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 88 | /* 0 - every time, > 0 - by update_time */ |
| 89 | static unsigned int update_time = 120; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 90 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 91 | static unsigned int mode = CAPACITY_UNIT; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 92 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 93 | module_param(update_time, uint, 0644); |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 94 | module_param(mode, uint, 0444); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 95 | |
| 96 | static int acpi_sbs_add(struct acpi_device *device); |
| 97 | static int acpi_sbs_remove(struct acpi_device *device, int type); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 98 | static int acpi_sbs_resume(struct acpi_device *device); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 99 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 100 | static const struct acpi_device_id sbs_device_ids[] = { |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 101 | {"ACPI0002", 0}, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 102 | {"", 0}, |
| 103 | }; |
| 104 | MODULE_DEVICE_TABLE(acpi, sbs_device_ids); |
| 105 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 106 | static struct acpi_driver acpi_sbs_driver = { |
Len Brown | c2b6705 | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 107 | .name = "sbs", |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 108 | .class = ACPI_SBS_CLASS, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 109 | .ids = sbs_device_ids, |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 110 | .ops = { |
| 111 | .add = acpi_sbs_add, |
| 112 | .remove = acpi_sbs_remove, |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 113 | .resume = acpi_sbs_resume, |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 114 | }, |
| 115 | }; |
| 116 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 117 | struct acpi_battery { |
| 118 | struct acpi_sbs *sbs; |
| 119 | struct proc_dir_entry *proc_entry; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 120 | int vscale; |
| 121 | int ipscale; |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 122 | char manufacturer_name[ACPI_SBS_BLOCK_MAX]; |
| 123 | char device_name[ACPI_SBS_BLOCK_MAX]; |
| 124 | char device_chemistry[ACPI_SBS_BLOCK_MAX]; |
| 125 | u16 full_charge_capacity; |
| 126 | u16 design_capacity; |
| 127 | u16 design_voltage; |
| 128 | u16 serial_number; |
| 129 | u16 voltage_now; |
| 130 | s16 current_now; |
| 131 | u16 capacity_now; |
| 132 | u16 state; |
| 133 | u16 alarm_capacity; |
| 134 | u16 mode; |
| 135 | u8 id; |
| 136 | u8 alive:1; |
| 137 | u8 init_state:1; |
| 138 | u8 present:1; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | struct acpi_sbs { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 142 | struct acpi_device *device; |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 143 | struct acpi_smb_hc *hc; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 144 | struct mutex mutex; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 145 | struct proc_dir_entry *ac_entry; |
| 146 | struct acpi_battery battery[MAX_SBS_BAT]; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 147 | int zombie; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 148 | struct timer_list update_timer; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 149 | int run_cnt; |
| 150 | int update_proc_flg; |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 151 | u8 batteries_supported; |
| 152 | u8 manager_present:1; |
| 153 | u8 charger_present:1; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 154 | }; |
| 155 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 156 | static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type); |
| 157 | static void acpi_sbs_update_time(void *data); |
| 158 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 159 | static int sbs_zombie(struct acpi_sbs *sbs) |
| 160 | { |
| 161 | return (sbs->zombie); |
| 162 | } |
| 163 | |
| 164 | static int sbs_mutex_lock(struct acpi_sbs *sbs) |
| 165 | { |
| 166 | if (sbs_zombie(sbs)) { |
| 167 | return -ENODEV; |
| 168 | } |
| 169 | mutex_lock(&sbs->mutex); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static void sbs_mutex_unlock(struct acpi_sbs *sbs) |
| 174 | { |
| 175 | mutex_unlock(&sbs->mutex); |
| 176 | } |
| 177 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 178 | /* -------------------------------------------------------------------------- |
| 179 | Smart Battery System Management |
| 180 | -------------------------------------------------------------------------- */ |
| 181 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 182 | static int acpi_check_update_proc(struct acpi_sbs *sbs) |
| 183 | { |
| 184 | acpi_status status = AE_OK; |
| 185 | |
| 186 | if (update_time == 0) { |
| 187 | sbs->update_proc_flg = 0; |
| 188 | return 0; |
| 189 | } |
| 190 | if (sbs->update_proc_flg == 0) { |
| 191 | status = acpi_os_execute(OSL_GPE_HANDLER, |
| 192 | acpi_sbs_update_time, sbs); |
| 193 | if (status != AE_OK) { |
| 194 | ACPI_EXCEPTION((AE_INFO, status, |
| 195 | "acpi_os_execute() failed")); |
| 196 | return 1; |
| 197 | } |
| 198 | sbs->update_proc_flg = 1; |
| 199 | } |
| 200 | return 0; |
| 201 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 202 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 203 | static int acpi_battery_get_present(struct acpi_battery *battery) |
| 204 | { |
| 205 | s16 state; |
| 206 | int result = 0; |
| 207 | int is_present = 0; |
| 208 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 209 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, |
| 210 | ACPI_SBSM_SMBUS_ADDR, 0x01, (u8 *)&state); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 211 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 212 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 213 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 214 | } |
| 215 | if (!result) { |
| 216 | is_present = (state & 0x000f) & (1 << battery->id); |
| 217 | } |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 218 | battery->present = is_present; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 219 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 220 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 223 | static int acpi_battery_select(struct acpi_battery *battery) |
| 224 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 225 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 226 | int result = 0; |
| 227 | s16 state; |
| 228 | int foo; |
| 229 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 230 | if (sbs->manager_present) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 231 | |
| 232 | /* Take special care not to knobble other nibbles of |
| 233 | * state (aka selector_state), since |
| 234 | * it causes charging to halt on SBSELs */ |
| 235 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 236 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, |
| 237 | ACPI_SBSM_SMBUS_ADDR, 0x01, (u8 *)&state); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 238 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 239 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 240 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | foo = (state & 0x0fff) | (1 << (battery->id + 12)); |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 245 | result = acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD, |
| 246 | ACPI_SBSM_SMBUS_ADDR, 0x01, (u8 *)&foo, 2); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 247 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 248 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 249 | "acpi_smbus_write() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 250 | goto end; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 255 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int acpi_sbsm_get_info(struct acpi_sbs *sbs) |
| 259 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 260 | int result = 0; |
| 261 | s16 battery_system_info; |
| 262 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 263 | result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBSM_SMBUS_ADDR, 0x04, |
| 264 | (u8 *)&battery_system_info); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 265 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 266 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 267 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 268 | goto end; |
| 269 | } |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 270 | sbs->manager_present = 1; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 271 | |
| 272 | end: |
| 273 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 274 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static int acpi_battery_get_info(struct acpi_battery *battery) |
| 278 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 279 | int result = 0; |
| 280 | s16 battery_mode; |
| 281 | s16 specification_info; |
| 282 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 283 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x03, |
| 284 | (u8 *)&battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 285 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 286 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 287 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 288 | goto end; |
| 289 | } |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 290 | battery->mode = (battery_mode & 0x8000) >> 15; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 291 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 292 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x10, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 293 | (u8 *)&battery->full_charge_capacity); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 294 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 295 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 296 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 297 | goto end; |
| 298 | } |
| 299 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 300 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x18, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 301 | (u8 *)&battery->design_capacity); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 302 | |
| 303 | if (result) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 304 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 305 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 306 | goto end; |
| 307 | } |
| 308 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 309 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x19, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 310 | (u8 *)&battery->design_voltage); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 311 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 312 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 313 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 314 | goto end; |
| 315 | } |
| 316 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 317 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x1a, |
| 318 | (u8 *)&specification_info); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 319 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 320 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 321 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 322 | goto end; |
| 323 | } |
| 324 | |
| 325 | switch ((specification_info & 0x0f00) >> 8) { |
| 326 | case 1: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 327 | battery->vscale = 10; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 328 | break; |
| 329 | case 2: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 330 | battery->vscale = 100; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 331 | break; |
| 332 | case 3: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 333 | battery->vscale = 1000; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 334 | break; |
| 335 | default: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 336 | battery->vscale = 1; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | switch ((specification_info & 0xf000) >> 12) { |
| 340 | case 1: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 341 | battery->ipscale = 10; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 342 | break; |
| 343 | case 2: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 344 | battery->ipscale = 100; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 345 | break; |
| 346 | case 3: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 347 | battery->ipscale = 1000; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 348 | break; |
| 349 | default: |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 350 | battery->ipscale = 1; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 353 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x1c, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 354 | (u8 *)&battery->serial_number); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 355 | if (result) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 356 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 357 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 358 | goto end; |
| 359 | } |
| 360 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 361 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_BLOCK, ACPI_SB_SMBUS_ADDR, 0x20, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 362 | (u8 *)battery->manufacturer_name); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 363 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 364 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 365 | "acpi_sbs_read_str() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 366 | goto end; |
| 367 | } |
| 368 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 369 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_BLOCK, ACPI_SB_SMBUS_ADDR, 0x21, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 370 | (u8 *)battery->device_name); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 371 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 372 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 373 | "acpi_sbs_read_str() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 374 | goto end; |
| 375 | } |
| 376 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 377 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_BLOCK, ACPI_SB_SMBUS_ADDR, 0x22, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 378 | (u8 *)battery->device_chemistry); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 379 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 380 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 381 | "acpi_sbs_read_str() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 382 | goto end; |
| 383 | } |
| 384 | |
| 385 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 386 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 389 | static int acpi_battery_get_state(struct acpi_battery *battery) |
| 390 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 391 | int result = 0; |
| 392 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 393 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x09, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 394 | (u8 *)&battery->voltage_now); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 395 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 396 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 397 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 398 | goto end; |
| 399 | } |
| 400 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 401 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x0a, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 402 | (u8 *)&battery->current_now); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 403 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 404 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 405 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 406 | goto end; |
| 407 | } |
| 408 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 409 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x0f, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 410 | (u8 *)&battery->capacity_now); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 411 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 412 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 413 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 414 | goto end; |
| 415 | } |
| 416 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 417 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x16, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 418 | (u8 *)&battery->state); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 419 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 420 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 421 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 422 | goto end; |
| 423 | } |
| 424 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 425 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 426 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static int acpi_battery_get_alarm(struct acpi_battery *battery) |
| 430 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 431 | int result = 0; |
| 432 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 433 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x01, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 434 | (u8 *)&battery->alarm_capacity); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 435 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 436 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 437 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 438 | goto end; |
| 439 | } |
| 440 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 441 | end: |
| 442 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 443 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static int acpi_battery_set_alarm(struct acpi_battery *battery, |
| 447 | unsigned long alarm) |
| 448 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 449 | int result = 0; |
| 450 | s16 battery_mode; |
| 451 | int foo; |
| 452 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 453 | result = acpi_battery_select(battery); |
| 454 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 455 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 456 | "acpi_battery_select() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 457 | goto end; |
| 458 | } |
| 459 | |
| 460 | /* If necessary, enable the alarm */ |
| 461 | |
| 462 | if (alarm > 0) { |
| 463 | result = |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 464 | acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x03, |
| 465 | (u8 *)&battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 466 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 467 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 468 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 469 | goto end; |
| 470 | } |
| 471 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 472 | battery_mode &= 0xbfff; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 473 | result = |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 474 | acpi_smbus_write(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x01, |
| 475 | (u8 *)&battery_mode, 2); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 476 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 477 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 478 | "acpi_smbus_write() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 479 | goto end; |
| 480 | } |
| 481 | } |
| 482 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 483 | foo = alarm / (battery->mode ? 10 : 1); |
| 484 | result = acpi_smbus_write(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x01, |
| 485 | (u8 *)&foo, 2); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 486 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 487 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 488 | "acpi_smbus_write() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 489 | goto end; |
| 490 | } |
| 491 | |
| 492 | end: |
| 493 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 494 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static int acpi_battery_set_mode(struct acpi_battery *battery) |
| 498 | { |
| 499 | int result = 0; |
| 500 | s16 battery_mode; |
| 501 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 502 | if (mode == DEF_CAPACITY_UNIT) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 503 | goto end; |
| 504 | } |
| 505 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 506 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 507 | ACPI_SB_SMBUS_ADDR, 0x03, (u8 *)&battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 508 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 509 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 510 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 511 | goto end; |
| 512 | } |
| 513 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 514 | if (mode == MAH_CAPACITY_UNIT) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 515 | battery_mode &= 0x7fff; |
| 516 | } else { |
| 517 | battery_mode |= 0x8000; |
| 518 | } |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 519 | result = acpi_smbus_write(battery->sbs->hc, SMBUS_READ_WORD, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 520 | ACPI_SB_SMBUS_ADDR, 0x03, (u8 *)&battery_mode, 2); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 521 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 522 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 523 | "acpi_smbus_write() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 524 | goto end; |
| 525 | } |
| 526 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 527 | result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 528 | ACPI_SB_SMBUS_ADDR, 0x03, (u8 *)&battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 529 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 530 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 531 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 532 | goto end; |
| 533 | } |
| 534 | |
| 535 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 536 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static int acpi_battery_init(struct acpi_battery *battery) |
| 540 | { |
| 541 | int result = 0; |
| 542 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 543 | result = acpi_battery_select(battery); |
| 544 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 545 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 546 | "acpi_battery_select() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 547 | goto end; |
| 548 | } |
| 549 | |
| 550 | result = acpi_battery_set_mode(battery); |
| 551 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 552 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 553 | "acpi_battery_set_mode() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 554 | goto end; |
| 555 | } |
| 556 | |
| 557 | result = acpi_battery_get_info(battery); |
| 558 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 559 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 560 | "acpi_battery_get_info() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 561 | goto end; |
| 562 | } |
| 563 | |
| 564 | result = acpi_battery_get_state(battery); |
| 565 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 566 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 567 | "acpi_battery_get_state() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 568 | goto end; |
| 569 | } |
| 570 | |
| 571 | result = acpi_battery_get_alarm(battery); |
| 572 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 573 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 574 | "acpi_battery_get_alarm() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 579 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static int acpi_ac_get_present(struct acpi_sbs *sbs) |
| 583 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 584 | int result = 0; |
| 585 | s16 charger_status; |
| 586 | |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 587 | result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBC_SMBUS_ADDR, 0x13, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 588 | (u8 *)&charger_status); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 589 | |
| 590 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 591 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 592 | "acpi_smbus_read() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 593 | goto end; |
| 594 | } |
| 595 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 596 | sbs->charger_present = (charger_status & 0x8000) >> 15; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 597 | |
| 598 | end: |
| 599 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 600 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* -------------------------------------------------------------------------- |
| 604 | FS Interface (/proc/acpi) |
| 605 | -------------------------------------------------------------------------- */ |
| 606 | |
| 607 | /* Generic Routines */ |
| 608 | |
| 609 | static int |
| 610 | acpi_sbs_generic_add_fs(struct proc_dir_entry **dir, |
| 611 | struct proc_dir_entry *parent_dir, |
| 612 | char *dir_name, |
| 613 | struct file_operations *info_fops, |
| 614 | struct file_operations *state_fops, |
| 615 | struct file_operations *alarm_fops, void *data) |
| 616 | { |
| 617 | struct proc_dir_entry *entry = NULL; |
| 618 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 619 | if (!*dir) { |
| 620 | *dir = proc_mkdir(dir_name, parent_dir); |
| 621 | if (!*dir) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 622 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 623 | "proc_mkdir() failed")); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 624 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 625 | } |
| 626 | (*dir)->owner = THIS_MODULE; |
| 627 | } |
| 628 | |
| 629 | /* 'info' [R] */ |
| 630 | if (info_fops) { |
| 631 | entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir); |
| 632 | if (!entry) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 633 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 634 | "create_proc_entry() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 635 | } else { |
| 636 | entry->proc_fops = info_fops; |
| 637 | entry->data = data; |
| 638 | entry->owner = THIS_MODULE; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* 'state' [R] */ |
| 643 | if (state_fops) { |
| 644 | entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir); |
| 645 | if (!entry) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 646 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 647 | "create_proc_entry() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 648 | } else { |
| 649 | entry->proc_fops = state_fops; |
| 650 | entry->data = data; |
| 651 | entry->owner = THIS_MODULE; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /* 'alarm' [R/W] */ |
| 656 | if (alarm_fops) { |
| 657 | entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir); |
| 658 | if (!entry) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 659 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 660 | "create_proc_entry() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 661 | } else { |
| 662 | entry->proc_fops = alarm_fops; |
| 663 | entry->data = data; |
| 664 | entry->owner = THIS_MODULE; |
| 665 | } |
| 666 | } |
| 667 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 668 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static void |
| 672 | acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir, |
| 673 | struct proc_dir_entry *parent_dir) |
| 674 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 675 | |
| 676 | if (*dir) { |
| 677 | remove_proc_entry(ACPI_SBS_FILE_INFO, *dir); |
| 678 | remove_proc_entry(ACPI_SBS_FILE_STATE, *dir); |
| 679 | remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir); |
| 680 | remove_proc_entry((*dir)->name, parent_dir); |
| 681 | *dir = NULL; |
| 682 | } |
| 683 | |
| 684 | } |
| 685 | |
| 686 | /* Smart Battery Interface */ |
| 687 | |
| 688 | static struct proc_dir_entry *acpi_battery_dir = NULL; |
| 689 | |
| 690 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) |
| 691 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 692 | struct acpi_battery *battery = seq->private; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 693 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 694 | int cscale; |
| 695 | int result = 0; |
| 696 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 697 | if (sbs_mutex_lock(sbs)) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 698 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 699 | } |
| 700 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 701 | result = acpi_check_update_proc(sbs); |
| 702 | if (result) |
| 703 | goto end; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 704 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 705 | if (update_time == 0) { |
| 706 | result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 707 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 708 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 709 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 713 | if (battery->present) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 714 | seq_printf(seq, "present: yes\n"); |
| 715 | } else { |
| 716 | seq_printf(seq, "present: no\n"); |
| 717 | goto end; |
| 718 | } |
| 719 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 720 | if (battery->mode) { |
| 721 | cscale = battery->vscale * battery->ipscale; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 722 | } else { |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 723 | cscale = battery->ipscale; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 724 | } |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 725 | seq_printf(seq, "design capacity: %i%s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 726 | battery->design_capacity * cscale, |
| 727 | battery->mode ? "0 mWh" : " mAh"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 728 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 729 | seq_printf(seq, "last full capacity: %i%s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 730 | battery->full_charge_capacity * cscale, |
| 731 | battery->mode ? "0 mWh" : " mAh"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 732 | |
| 733 | seq_printf(seq, "battery technology: rechargeable\n"); |
| 734 | |
| 735 | seq_printf(seq, "design voltage: %i mV\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 736 | battery->design_voltage * battery->vscale); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 737 | |
| 738 | seq_printf(seq, "design capacity warning: unknown\n"); |
| 739 | seq_printf(seq, "design capacity low: unknown\n"); |
| 740 | seq_printf(seq, "capacity granularity 1: unknown\n"); |
| 741 | seq_printf(seq, "capacity granularity 2: unknown\n"); |
| 742 | |
| 743 | seq_printf(seq, "model number: %s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 744 | battery->device_name); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 745 | |
| 746 | seq_printf(seq, "serial number: %i\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 747 | battery->serial_number); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 748 | |
| 749 | seq_printf(seq, "battery type: %s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 750 | battery->device_chemistry); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 751 | |
| 752 | seq_printf(seq, "OEM info: %s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 753 | battery->manufacturer_name); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 754 | |
| 755 | end: |
| 756 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 757 | sbs_mutex_unlock(sbs); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 758 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 759 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) |
| 763 | { |
| 764 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); |
| 765 | } |
| 766 | |
| 767 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) |
| 768 | { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 769 | struct acpi_battery *battery = seq->private; |
| 770 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 771 | int result = 0; |
| 772 | int cscale; |
| 773 | int foo; |
| 774 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 775 | if (sbs_mutex_lock(sbs)) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 776 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 777 | } |
| 778 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 779 | result = acpi_check_update_proc(sbs); |
| 780 | if (result) |
| 781 | goto end; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 782 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 783 | if (update_time == 0) { |
| 784 | result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 785 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 786 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 787 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 791 | if (battery->present) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 792 | seq_printf(seq, "present: yes\n"); |
| 793 | } else { |
| 794 | seq_printf(seq, "present: no\n"); |
| 795 | goto end; |
| 796 | } |
| 797 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 798 | if (battery->mode) { |
| 799 | cscale = battery->vscale * battery->ipscale; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 800 | } else { |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 801 | cscale = battery->ipscale; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 802 | } |
| 803 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 804 | if (battery->state & 0x0010) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 805 | seq_printf(seq, "capacity state: critical\n"); |
| 806 | } else { |
| 807 | seq_printf(seq, "capacity state: ok\n"); |
| 808 | } |
Vladimir Lebedev | e6d0f56 | 2007-02-10 01:51:13 -0500 | [diff] [blame] | 809 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 810 | foo = (s16) battery->current_now * battery->ipscale; |
| 811 | if (battery->mode) { |
| 812 | foo = foo * battery->design_voltage / 1000; |
Vladimir Lebedev | e6d0f56 | 2007-02-10 01:51:13 -0500 | [diff] [blame] | 813 | } |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 814 | if (battery->current_now < 0) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 815 | seq_printf(seq, "charging state: discharging\n"); |
Vladimir Lebedev | e6d0f56 | 2007-02-10 01:51:13 -0500 | [diff] [blame] | 816 | seq_printf(seq, "present rate: %d %s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 817 | -foo, battery->mode ? "mW" : "mA"); |
| 818 | } else if (battery->current_now > 0) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 819 | seq_printf(seq, "charging state: charging\n"); |
Vladimir Lebedev | e6d0f56 | 2007-02-10 01:51:13 -0500 | [diff] [blame] | 820 | seq_printf(seq, "present rate: %d %s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 821 | foo, battery->mode ? "mW" : "mA"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 822 | } else { |
| 823 | seq_printf(seq, "charging state: charged\n"); |
| 824 | seq_printf(seq, "present rate: 0 %s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 825 | battery->mode ? "mW" : "mA"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 826 | } |
| 827 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 828 | seq_printf(seq, "remaining capacity: %i%s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 829 | battery->capacity_now * cscale, |
| 830 | battery->mode ? "0 mWh" : " mAh"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 831 | |
| 832 | seq_printf(seq, "present voltage: %i mV\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 833 | battery->voltage_now * battery->vscale); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 834 | |
| 835 | end: |
| 836 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 837 | sbs_mutex_unlock(sbs); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 838 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 839 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) |
| 843 | { |
| 844 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); |
| 845 | } |
| 846 | |
| 847 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) |
| 848 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 849 | struct acpi_battery *battery = seq->private; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 850 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 851 | int result = 0; |
| 852 | int cscale; |
| 853 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 854 | if (sbs_mutex_lock(sbs)) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 855 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 856 | } |
| 857 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 858 | result = acpi_check_update_proc(sbs); |
| 859 | if (result) |
| 860 | goto end; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 861 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 862 | if (update_time == 0) { |
| 863 | result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 864 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 865 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 866 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 870 | if (!battery->present) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 871 | seq_printf(seq, "present: no\n"); |
| 872 | goto end; |
| 873 | } |
| 874 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 875 | if (battery->mode) { |
| 876 | cscale = battery->vscale * battery->ipscale; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 877 | } else { |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 878 | cscale = battery->ipscale; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | seq_printf(seq, "alarm: "); |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 882 | if (battery->alarm_capacity) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 883 | seq_printf(seq, "%i%s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 884 | battery->alarm_capacity * cscale, |
| 885 | battery->mode ? "0 mWh" : " mAh"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 886 | } else { |
| 887 | seq_printf(seq, "disabled\n"); |
| 888 | } |
| 889 | |
| 890 | end: |
| 891 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 892 | sbs_mutex_unlock(sbs); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 893 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 894 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static ssize_t |
| 898 | acpi_battery_write_alarm(struct file *file, const char __user * buffer, |
| 899 | size_t count, loff_t * ppos) |
| 900 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 901 | struct seq_file *seq = file->private_data; |
| 902 | struct acpi_battery *battery = seq->private; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 903 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 904 | char alarm_string[12] = { '\0' }; |
| 905 | int result, old_alarm, new_alarm; |
| 906 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 907 | if (sbs_mutex_lock(sbs)) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 908 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 909 | } |
| 910 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 911 | result = acpi_check_update_proc(sbs); |
| 912 | if (result) |
| 913 | goto end; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 914 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 915 | if (!battery->present) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 916 | result = -ENODEV; |
| 917 | goto end; |
| 918 | } |
| 919 | |
| 920 | if (count > sizeof(alarm_string) - 1) { |
| 921 | result = -EINVAL; |
| 922 | goto end; |
| 923 | } |
| 924 | |
| 925 | if (copy_from_user(alarm_string, buffer, count)) { |
| 926 | result = -EFAULT; |
| 927 | goto end; |
| 928 | } |
| 929 | |
| 930 | alarm_string[count] = 0; |
| 931 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 932 | old_alarm = battery->alarm_capacity; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 933 | new_alarm = simple_strtoul(alarm_string, NULL, 0); |
| 934 | |
| 935 | result = acpi_battery_set_alarm(battery, new_alarm); |
| 936 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 937 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 938 | "acpi_battery_set_alarm() failed")); |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 939 | acpi_battery_set_alarm(battery, old_alarm); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 940 | goto end; |
| 941 | } |
| 942 | result = acpi_battery_get_alarm(battery); |
| 943 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 944 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 945 | "acpi_battery_get_alarm() failed")); |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 946 | acpi_battery_set_alarm(battery, old_alarm); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 947 | goto end; |
| 948 | } |
| 949 | |
| 950 | end: |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 951 | sbs_mutex_unlock(sbs); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 952 | |
| 953 | if (result) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 954 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 955 | } else { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 956 | return count; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
| 960 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) |
| 961 | { |
| 962 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); |
| 963 | } |
| 964 | |
| 965 | static struct file_operations acpi_battery_info_fops = { |
| 966 | .open = acpi_battery_info_open_fs, |
| 967 | .read = seq_read, |
| 968 | .llseek = seq_lseek, |
| 969 | .release = single_release, |
| 970 | .owner = THIS_MODULE, |
| 971 | }; |
| 972 | |
| 973 | static struct file_operations acpi_battery_state_fops = { |
| 974 | .open = acpi_battery_state_open_fs, |
| 975 | .read = seq_read, |
| 976 | .llseek = seq_lseek, |
| 977 | .release = single_release, |
| 978 | .owner = THIS_MODULE, |
| 979 | }; |
| 980 | |
| 981 | static struct file_operations acpi_battery_alarm_fops = { |
| 982 | .open = acpi_battery_alarm_open_fs, |
| 983 | .read = seq_read, |
| 984 | .write = acpi_battery_write_alarm, |
| 985 | .llseek = seq_lseek, |
| 986 | .release = single_release, |
| 987 | .owner = THIS_MODULE, |
| 988 | }; |
| 989 | |
| 990 | /* Legacy AC Adapter Interface */ |
| 991 | |
| 992 | static struct proc_dir_entry *acpi_ac_dir = NULL; |
| 993 | |
| 994 | static int acpi_ac_read_state(struct seq_file *seq, void *offset) |
| 995 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 996 | struct acpi_sbs *sbs = seq->private; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 997 | int result; |
| 998 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 999 | if (sbs_mutex_lock(sbs)) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1000 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1003 | if (update_time == 0) { |
| 1004 | result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1005 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1006 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1007 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | seq_printf(seq, "state: %s\n", |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1012 | sbs->charger_present ? "on-line" : "off-line"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1013 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1014 | sbs_mutex_unlock(sbs); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1015 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1016 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static int acpi_ac_state_open_fs(struct inode *inode, struct file *file) |
| 1020 | { |
| 1021 | return single_open(file, acpi_ac_read_state, PDE(inode)->data); |
| 1022 | } |
| 1023 | |
| 1024 | static struct file_operations acpi_ac_state_fops = { |
| 1025 | .open = acpi_ac_state_open_fs, |
| 1026 | .read = seq_read, |
| 1027 | .llseek = seq_lseek, |
| 1028 | .release = single_release, |
| 1029 | .owner = THIS_MODULE, |
| 1030 | }; |
| 1031 | |
| 1032 | /* -------------------------------------------------------------------------- |
| 1033 | Driver Interface |
| 1034 | -------------------------------------------------------------------------- */ |
| 1035 | |
| 1036 | /* Smart Battery */ |
| 1037 | |
| 1038 | static int acpi_battery_add(struct acpi_sbs *sbs, int id) |
| 1039 | { |
| 1040 | int is_present; |
| 1041 | int result; |
| 1042 | char dir_name[32]; |
| 1043 | struct acpi_battery *battery; |
| 1044 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1045 | battery = &sbs->battery[id]; |
| 1046 | |
| 1047 | battery->alive = 0; |
| 1048 | |
| 1049 | battery->init_state = 0; |
| 1050 | battery->id = id; |
| 1051 | battery->sbs = sbs; |
| 1052 | |
| 1053 | result = acpi_battery_select(battery); |
| 1054 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1055 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1056 | "acpi_battery_select() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1057 | goto end; |
| 1058 | } |
| 1059 | |
| 1060 | result = acpi_battery_get_present(battery); |
| 1061 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1062 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1063 | "acpi_battery_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1064 | goto end; |
| 1065 | } |
| 1066 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1067 | is_present = battery->present; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1068 | |
| 1069 | if (is_present) { |
| 1070 | result = acpi_battery_init(battery); |
| 1071 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1072 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1073 | "acpi_battery_init() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1074 | goto end; |
| 1075 | } |
| 1076 | battery->init_state = 1; |
| 1077 | } |
| 1078 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1079 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1080 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1081 | result = acpi_sbs_generic_add_fs(&battery->proc_entry, |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1082 | acpi_battery_dir, |
| 1083 | dir_name, |
| 1084 | &acpi_battery_info_fops, |
| 1085 | &acpi_battery_state_fops, |
| 1086 | &acpi_battery_alarm_fops, battery); |
| 1087 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1088 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1089 | "acpi_sbs_generic_add_fs() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1090 | goto end; |
| 1091 | } |
| 1092 | battery->alive = 1; |
| 1093 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1094 | printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n", |
| 1095 | ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1096 | sbs->battery->present ? "present" : "absent"); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1097 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1098 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1099 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | static void acpi_battery_remove(struct acpi_sbs *sbs, int id) |
| 1103 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1104 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1105 | if (sbs->battery[id].proc_entry) { |
| 1106 | acpi_sbs_generic_remove_fs(&(sbs->battery[id].proc_entry), |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1107 | acpi_battery_dir); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | static int acpi_ac_add(struct acpi_sbs *sbs) |
| 1112 | { |
| 1113 | int result; |
| 1114 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1115 | result = acpi_ac_get_present(sbs); |
| 1116 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1117 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1118 | "acpi_ac_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1119 | goto end; |
| 1120 | } |
| 1121 | |
| 1122 | result = acpi_sbs_generic_add_fs(&sbs->ac_entry, |
| 1123 | acpi_ac_dir, |
| 1124 | ACPI_AC_DIR_NAME, |
| 1125 | NULL, &acpi_ac_state_fops, NULL, sbs); |
| 1126 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1127 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1128 | "acpi_sbs_generic_add_fs() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1129 | goto end; |
| 1130 | } |
| 1131 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1132 | printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n", |
| 1133 | ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1134 | ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line"); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1135 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1136 | end: |
| 1137 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1138 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static void acpi_ac_remove(struct acpi_sbs *sbs) |
| 1142 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1143 | |
| 1144 | if (sbs->ac_entry) { |
| 1145 | acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir); |
| 1146 | } |
| 1147 | } |
| 1148 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1149 | static void acpi_sbs_update_time_run(unsigned long data) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1150 | { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1151 | acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1152 | } |
| 1153 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1154 | static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1155 | { |
| 1156 | struct acpi_battery *battery; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1157 | int result = 0, cnt; |
| 1158 | int old_ac_present = -1; |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1159 | int old_present = -1; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1160 | int new_ac_present = -1; |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1161 | int new_present = -1; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1162 | int id_min = 0, id_max = MAX_SBS_BAT - 1; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1163 | char dir_name[32]; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1164 | int do_battery_init = 0, do_ac_init = 0; |
| 1165 | int old_remaining_capacity = 0; |
Adrian Bunk | bc90a01 | 2007-07-09 11:33:15 -0700 | [diff] [blame] | 1166 | int update_battery = 1; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1167 | int up_tm = update_time; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1168 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1169 | if (sbs_zombie(sbs)) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1170 | goto end; |
| 1171 | } |
| 1172 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1173 | if (id >= 0) { |
| 1174 | id_min = id_max = id; |
| 1175 | } |
| 1176 | |
| 1177 | if (data_type == DATA_TYPE_COMMON && up_tm > 0) { |
| 1178 | cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm); |
| 1179 | if (sbs->run_cnt % cnt != 0) { |
| 1180 | update_battery = 0; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | sbs->run_cnt++; |
| 1185 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1186 | if (!update_battery) { |
| 1187 | goto end; |
| 1188 | } |
| 1189 | |
| 1190 | old_ac_present = sbs->charger_present; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1191 | |
| 1192 | result = acpi_ac_get_present(sbs); |
| 1193 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1194 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1195 | "acpi_ac_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1198 | new_ac_present = sbs->charger_present; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1199 | |
| 1200 | do_ac_init = (old_ac_present != new_ac_present); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1201 | if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) { |
| 1202 | do_ac_init = 1; |
| 1203 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1204 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1205 | if (do_ac_init) { |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1206 | result = acpi_bus_generate_proc_event4(ACPI_AC_CLASS, |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1207 | ACPI_AC_DIR_NAME, |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1208 | ACPI_SBS_AC_NOTIFY_STATUS, |
| 1209 | new_ac_present); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1210 | if (result) { |
| 1211 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1212 | "acpi_bus_generate_event4() failed")); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1213 | } |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1214 | acpi_bus_generate_netlink_event(ACPI_AC_CLASS, ACPI_AC_DIR_NAME, |
| 1215 | ACPI_SBS_AC_NOTIFY_STATUS, |
| 1216 | new_ac_present); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | if (data_type == DATA_TYPE_COMMON) { |
| 1220 | if (!do_ac_init && !update_battery) { |
| 1221 | goto end; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1226 | goto end; |
| 1227 | } |
| 1228 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1229 | for (id = id_min; id <= id_max; id++) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1230 | battery = &sbs->battery[id]; |
| 1231 | if (battery->alive == 0) { |
| 1232 | continue; |
| 1233 | } |
| 1234 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1235 | old_remaining_capacity = battery->capacity_now; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1236 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1237 | old_present = battery->present; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1238 | |
| 1239 | result = acpi_battery_select(battery); |
| 1240 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1241 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1242 | "acpi_battery_select() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1243 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1244 | |
| 1245 | result = acpi_battery_get_present(battery); |
| 1246 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1247 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1248 | "acpi_battery_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1249 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1250 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1251 | new_present = battery->present; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1252 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1253 | do_battery_init = ((old_present != new_present) |
| 1254 | && new_present); |
| 1255 | if (!new_present) |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1256 | goto event; |
| 1257 | if (do_ac_init || do_battery_init) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1258 | result = acpi_battery_init(battery); |
| 1259 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1260 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1261 | "acpi_battery_init() " |
| 1262 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1263 | } |
| 1264 | } |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1265 | if (sbs_zombie(sbs)) { |
| 1266 | goto end; |
| 1267 | } |
| 1268 | |
| 1269 | if ((data_type == DATA_TYPE_COMMON |
| 1270 | || data_type == DATA_TYPE_INFO) |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1271 | && new_present) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1272 | result = acpi_battery_get_info(battery); |
| 1273 | if (result) { |
| 1274 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1275 | "acpi_battery_get_info() failed")); |
| 1276 | } |
| 1277 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1278 | if (data_type == DATA_TYPE_INFO) { |
| 1279 | continue; |
| 1280 | } |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1281 | if (sbs_zombie(sbs)) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1282 | goto end; |
| 1283 | } |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1284 | |
| 1285 | if ((data_type == DATA_TYPE_COMMON |
| 1286 | || data_type == DATA_TYPE_STATE) |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1287 | && new_present) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1288 | result = acpi_battery_get_state(battery); |
| 1289 | if (result) { |
| 1290 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1291 | "acpi_battery_get_state() failed")); |
| 1292 | } |
| 1293 | } |
| 1294 | if (data_type == DATA_TYPE_STATE) { |
| 1295 | goto event; |
| 1296 | } |
| 1297 | if (sbs_zombie(sbs)) { |
| 1298 | goto end; |
| 1299 | } |
| 1300 | |
| 1301 | if ((data_type == DATA_TYPE_COMMON |
| 1302 | || data_type == DATA_TYPE_ALARM) |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1303 | && new_present) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1304 | result = acpi_battery_get_alarm(battery); |
| 1305 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1306 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1307 | "acpi_battery_get_alarm() " |
| 1308 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1309 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1310 | } |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1311 | if (data_type == DATA_TYPE_ALARM) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1312 | continue; |
| 1313 | } |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1314 | if (sbs_zombie(sbs)) { |
| 1315 | goto end; |
| 1316 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1317 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1318 | event: |
| 1319 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1320 | if (old_present != new_present || do_ac_init || |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1321 | old_remaining_capacity != |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1322 | battery->capacity_now) { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1323 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1324 | result = acpi_bus_generate_proc_event4(ACPI_BATTERY_CLASS, |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1325 | dir_name, |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1326 | ACPI_SBS_BATTERY_NOTIFY_STATUS, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1327 | new_present); |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1328 | acpi_bus_generate_netlink_event(ACPI_BATTERY_CLASS, dir_name, |
| 1329 | ACPI_SBS_BATTERY_NOTIFY_STATUS, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1330 | new_present); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1331 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1332 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 8db85d4 | 2007-09-26 19:43:16 +0400 | [diff] [blame] | 1333 | "acpi_bus_generate_proc_event4() " |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1334 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1335 | } |
| 1336 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | end: |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1340 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1341 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1342 | } |
| 1343 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1344 | static void acpi_sbs_update_time(void *data) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1345 | { |
| 1346 | struct acpi_sbs *sbs = data; |
| 1347 | unsigned long delay = -1; |
| 1348 | int result; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1349 | unsigned int up_tm = update_time; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1350 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1351 | if (sbs_mutex_lock(sbs)) |
| 1352 | return; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1353 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1354 | result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1355 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1356 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1357 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1360 | if (sbs_zombie(sbs)) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1361 | goto end; |
| 1362 | } |
| 1363 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1364 | if (!up_tm) { |
| 1365 | if (timer_pending(&sbs->update_timer)) |
| 1366 | del_timer(&sbs->update_timer); |
| 1367 | } else { |
| 1368 | delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm); |
| 1369 | delay = jiffies + HZ * delay; |
| 1370 | if (timer_pending(&sbs->update_timer)) { |
| 1371 | mod_timer(&sbs->update_timer, delay); |
| 1372 | } else { |
| 1373 | sbs->update_timer.data = (unsigned long)data; |
| 1374 | sbs->update_timer.function = acpi_sbs_update_time_run; |
| 1375 | sbs->update_timer.expires = delay; |
| 1376 | add_timer(&sbs->update_timer); |
| 1377 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1378 | } |
| 1379 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1380 | end: |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1381 | |
| 1382 | sbs_mutex_unlock(sbs); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | static int acpi_sbs_add(struct acpi_device *device) |
| 1386 | { |
| 1387 | struct acpi_sbs *sbs = NULL; |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1388 | int result = 0, remove_result = 0; |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1389 | int id; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1390 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 1391 | sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1392 | if (!sbs) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1393 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed")); |
| 1394 | result = -ENOMEM; |
| 1395 | goto end; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1396 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1397 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1398 | mutex_init(&sbs->mutex); |
| 1399 | |
| 1400 | sbs_mutex_lock(sbs); |
| 1401 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1402 | sbs->device = device; |
Alexey Starikovskiy | 91087df | 2007-09-26 19:43:28 +0400 | [diff] [blame] | 1403 | sbs->hc = acpi_driver_data(device->parent); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1404 | |
| 1405 | strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME); |
| 1406 | strcpy(acpi_device_class(device), ACPI_SBS_CLASS); |
| 1407 | acpi_driver_data(device) = sbs; |
| 1408 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1409 | result = acpi_ac_add(sbs); |
| 1410 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1411 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1412 | goto end; |
| 1413 | } |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1414 | |
Alexey Starikovskiy | addad45 | 2007-06-23 16:24:48 -0400 | [diff] [blame] | 1415 | acpi_sbsm_get_info(sbs); |
| 1416 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1417 | if (!sbs->manager_present) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1418 | result = acpi_battery_add(sbs, 0); |
| 1419 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1420 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1421 | "acpi_battery_add() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1422 | goto end; |
| 1423 | } |
| 1424 | } else { |
| 1425 | for (id = 0; id < MAX_SBS_BAT; id++) { |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1426 | if ((sbs->batteries_supported & (1 << id))) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1427 | result = acpi_battery_add(sbs, id); |
| 1428 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1429 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1430 | "acpi_battery_add() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1431 | goto end; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1437 | init_timer(&sbs->update_timer); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1438 | result = acpi_check_update_proc(sbs); |
| 1439 | if (result) |
| 1440 | goto end; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1441 | |
| 1442 | end: |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1443 | |
| 1444 | sbs_mutex_unlock(sbs); |
| 1445 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1446 | if (result) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1447 | remove_result = acpi_sbs_remove(device, 0); |
| 1448 | if (remove_result) { |
| 1449 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1450 | "acpi_sbs_remove() failed")); |
| 1451 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1452 | } |
| 1453 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1454 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1457 | static int acpi_sbs_remove(struct acpi_device *device, int type) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1458 | { |
Len Brown | cece901 | 2006-12-16 01:04:27 -0500 | [diff] [blame] | 1459 | struct acpi_sbs *sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1460 | int id; |
| 1461 | |
Lebedev, Vladimir P | 963497c | 2006-09-05 19:49:13 +0400 | [diff] [blame] | 1462 | if (!device) { |
| 1463 | return -EINVAL; |
| 1464 | } |
| 1465 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1466 | sbs = acpi_driver_data(device); |
Lebedev, Vladimir P | 963497c | 2006-09-05 19:49:13 +0400 | [diff] [blame] | 1467 | if (!sbs) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1468 | return -EINVAL; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1471 | sbs_mutex_lock(sbs); |
| 1472 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1473 | sbs->zombie = 1; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1474 | del_timer_sync(&sbs->update_timer); |
| 1475 | acpi_os_wait_events_complete(NULL); |
| 1476 | del_timer_sync(&sbs->update_timer); |
| 1477 | |
| 1478 | for (id = 0; id < MAX_SBS_BAT; id++) { |
| 1479 | acpi_battery_remove(sbs, id); |
| 1480 | } |
| 1481 | |
| 1482 | acpi_ac_remove(sbs); |
| 1483 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1484 | sbs_mutex_unlock(sbs); |
| 1485 | |
| 1486 | mutex_destroy(&sbs->mutex); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1487 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1488 | kfree(sbs); |
| 1489 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1490 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1491 | } |
| 1492 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1493 | static void acpi_sbs_rmdirs(void) |
| 1494 | { |
| 1495 | if (acpi_ac_dir) { |
| 1496 | acpi_unlock_ac_dir(acpi_ac_dir); |
| 1497 | acpi_ac_dir = NULL; |
| 1498 | } |
| 1499 | if (acpi_battery_dir) { |
| 1500 | acpi_unlock_battery_dir(acpi_battery_dir); |
| 1501 | acpi_battery_dir = NULL; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | static int acpi_sbs_resume(struct acpi_device *device) |
| 1506 | { |
| 1507 | struct acpi_sbs *sbs; |
| 1508 | |
| 1509 | if (!device) |
| 1510 | return -EINVAL; |
| 1511 | |
| 1512 | sbs = device->driver_data; |
| 1513 | |
| 1514 | sbs->run_cnt = 0; |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1519 | static int __init acpi_sbs_init(void) |
| 1520 | { |
| 1521 | int result = 0; |
| 1522 | |
Len Brown | b20d2ae | 2006-08-15 23:21:37 -0400 | [diff] [blame] | 1523 | if (acpi_disabled) |
| 1524 | return -ENODEV; |
| 1525 | |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1526 | if (mode != DEF_CAPACITY_UNIT |
| 1527 | && mode != MAH_CAPACITY_UNIT |
| 1528 | && mode != MWH_CAPACITY_UNIT) { |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1529 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Alexey Starikovskiy | 89862e3 | 2007-09-26 19:43:35 +0400 | [diff] [blame^] | 1530 | "invalid mode = %d", mode)); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1531 | return -EINVAL; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | acpi_ac_dir = acpi_lock_ac_dir(); |
| 1535 | if (!acpi_ac_dir) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1536 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1537 | "acpi_lock_ac_dir() failed")); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1538 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | acpi_battery_dir = acpi_lock_battery_dir(); |
| 1542 | if (!acpi_battery_dir) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1543 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1544 | "acpi_lock_battery_dir() failed")); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1545 | acpi_sbs_rmdirs(); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1546 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | result = acpi_bus_register_driver(&acpi_sbs_driver); |
| 1550 | if (result < 0) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1551 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1552 | "acpi_bus_register_driver() failed")); |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1553 | acpi_sbs_rmdirs(); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1554 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1557 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | static void __exit acpi_sbs_exit(void) |
| 1561 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1562 | acpi_bus_unregister_driver(&acpi_sbs_driver); |
| 1563 | |
Vladimir Lebedev | 7220623 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1564 | acpi_sbs_rmdirs(); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1565 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1566 | return; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | module_init(acpi_sbs_init); |
| 1570 | module_exit(acpi_sbs_exit); |