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> |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 34 | #include <linux/delay.h> |
| 35 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 36 | #define ACPI_SBS_COMPONENT 0x00080000 |
| 37 | #define ACPI_SBS_CLASS "sbs" |
| 38 | #define ACPI_AC_CLASS "ac_adapter" |
| 39 | #define ACPI_BATTERY_CLASS "battery" |
| 40 | #define ACPI_SBS_HID "ACPI0002" |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 41 | #define ACPI_SBS_DEVICE_NAME "Smart Battery System" |
| 42 | #define ACPI_SBS_FILE_INFO "info" |
| 43 | #define ACPI_SBS_FILE_STATE "state" |
| 44 | #define ACPI_SBS_FILE_ALARM "alarm" |
| 45 | #define ACPI_BATTERY_DIR_NAME "BAT%i" |
| 46 | #define ACPI_AC_DIR_NAME "AC0" |
| 47 | #define ACPI_SBC_SMBUS_ADDR 0x9 |
| 48 | #define ACPI_SBSM_SMBUS_ADDR 0xa |
| 49 | #define ACPI_SB_SMBUS_ADDR 0xb |
| 50 | #define ACPI_SBS_AC_NOTIFY_STATUS 0x80 |
| 51 | #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80 |
| 52 | #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81 |
| 53 | |
| 54 | #define _COMPONENT ACPI_SBS_COMPONENT |
| 55 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 56 | ACPI_MODULE_NAME("sbs"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 57 | |
| 58 | MODULE_AUTHOR("Rich Townsend"); |
| 59 | MODULE_DESCRIPTION("Smart Battery System ACPI interface driver"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 62 | #define xmsleep(t) msleep(t) |
| 63 | |
| 64 | #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */ |
| 65 | |
| 66 | #define ACPI_EC_SMB_STS 0x01 /* status */ |
| 67 | #define ACPI_EC_SMB_ADDR 0x02 /* address */ |
| 68 | #define ACPI_EC_SMB_CMD 0x03 /* command */ |
| 69 | #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */ |
| 70 | #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */ |
| 71 | |
| 72 | #define ACPI_EC_SMB_STS_DONE 0x80 |
| 73 | #define ACPI_EC_SMB_STS_STATUS 0x1f |
| 74 | |
| 75 | #define ACPI_EC_SMB_PRTCL_WRITE 0x00 |
| 76 | #define ACPI_EC_SMB_PRTCL_READ 0x01 |
| 77 | #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08 |
| 78 | #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a |
| 79 | |
| 80 | #define ACPI_EC_SMB_TRANSACTION_SLEEP 1 |
| 81 | #define ACPI_EC_SMB_ACCESS_SLEEP1 1 |
| 82 | #define ACPI_EC_SMB_ACCESS_SLEEP2 10 |
| 83 | |
| 84 | #define DEF_CAPACITY_UNIT 3 |
| 85 | #define MAH_CAPACITY_UNIT 1 |
| 86 | #define MWH_CAPACITY_UNIT 2 |
| 87 | #define CAPACITY_UNIT DEF_CAPACITY_UNIT |
| 88 | |
| 89 | #define REQUEST_UPDATE_MODE 1 |
| 90 | #define QUEUE_UPDATE_MODE 2 |
| 91 | |
| 92 | #define DATA_TYPE_COMMON 0 |
| 93 | #define DATA_TYPE_INFO 1 |
| 94 | #define DATA_TYPE_STATE 2 |
| 95 | #define DATA_TYPE_ALARM 3 |
| 96 | #define DATA_TYPE_AC_STATE 4 |
| 97 | |
| 98 | extern struct proc_dir_entry *acpi_lock_ac_dir(void); |
| 99 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
| 100 | extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); |
| 101 | extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); |
| 102 | |
| 103 | #define MAX_SBS_BAT 4 |
| 104 | #define ACPI_SBS_BLOCK_MAX 32 |
| 105 | |
| 106 | #define ACPI_SBS_SMBUS_READ 1 |
| 107 | #define ACPI_SBS_SMBUS_WRITE 2 |
| 108 | |
| 109 | #define ACPI_SBS_WORD_DATA 1 |
| 110 | #define ACPI_SBS_BLOCK_DATA 2 |
| 111 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 112 | static struct semaphore sbs_sem; |
| 113 | |
| 114 | #define UPDATE_MODE QUEUE_UPDATE_MODE |
| 115 | /* REQUEST_UPDATE_MODE QUEUE_UPDATE_MODE */ |
| 116 | #define UPDATE_INFO_MODE 0 |
| 117 | #define UPDATE_TIME 60 |
| 118 | #define UPDATE_TIME2 0 |
| 119 | |
| 120 | static int capacity_mode = CAPACITY_UNIT; |
| 121 | static int update_mode = UPDATE_MODE; |
| 122 | static int update_info_mode = UPDATE_INFO_MODE; |
| 123 | static int update_time = UPDATE_TIME; |
| 124 | static int update_time2 = UPDATE_TIME2; |
| 125 | |
Lebedev, Vladimir P | 3cd5b87 | 2006-09-05 19:59:22 +0400 | [diff] [blame] | 126 | module_param(capacity_mode, int, 0); |
| 127 | module_param(update_mode, int, 0); |
| 128 | module_param(update_info_mode, int, 0); |
| 129 | module_param(update_time, int, 0); |
| 130 | module_param(update_time2, int, 0); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 131 | |
| 132 | static int acpi_sbs_add(struct acpi_device *device); |
| 133 | static int acpi_sbs_remove(struct acpi_device *device, int type); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 134 | static void acpi_sbs_update_queue(void *data); |
| 135 | |
| 136 | static struct acpi_driver acpi_sbs_driver = { |
Len Brown | c2b6705b | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 137 | .name = "sbs", |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 138 | .class = ACPI_SBS_CLASS, |
| 139 | .ids = ACPI_SBS_HID, |
| 140 | .ops = { |
| 141 | .add = acpi_sbs_add, |
| 142 | .remove = acpi_sbs_remove, |
| 143 | }, |
| 144 | }; |
| 145 | |
| 146 | struct acpi_battery_info { |
| 147 | int capacity_mode; |
| 148 | s16 full_charge_capacity; |
| 149 | s16 design_capacity; |
| 150 | s16 design_voltage; |
| 151 | int vscale; |
| 152 | int ipscale; |
| 153 | s16 serial_number; |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 154 | char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3]; |
| 155 | char device_name[ACPI_SBS_BLOCK_MAX + 3]; |
| 156 | char device_chemistry[ACPI_SBS_BLOCK_MAX + 3]; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | struct acpi_battery_state { |
| 160 | s16 voltage; |
| 161 | s16 amperage; |
| 162 | s16 remaining_capacity; |
| 163 | s16 average_time_to_empty; |
| 164 | s16 average_time_to_full; |
| 165 | s16 battery_status; |
| 166 | }; |
| 167 | |
| 168 | struct acpi_battery_alarm { |
| 169 | s16 remaining_capacity; |
| 170 | }; |
| 171 | |
| 172 | struct acpi_battery { |
| 173 | int alive; |
| 174 | int battery_present; |
| 175 | int id; |
| 176 | int init_state; |
| 177 | struct acpi_sbs *sbs; |
| 178 | struct acpi_battery_info info; |
| 179 | struct acpi_battery_state state; |
| 180 | struct acpi_battery_alarm alarm; |
| 181 | struct proc_dir_entry *battery_entry; |
| 182 | }; |
| 183 | |
| 184 | struct acpi_sbs { |
| 185 | acpi_handle handle; |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 186 | int base; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 187 | struct acpi_device *device; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 188 | int sbsm_present; |
| 189 | int sbsm_batteries_supported; |
| 190 | int ac_present; |
| 191 | struct proc_dir_entry *ac_entry; |
| 192 | struct acpi_battery battery[MAX_SBS_BAT]; |
| 193 | int update_info_mode; |
| 194 | int zombie; |
| 195 | int update_time; |
| 196 | int update_time2; |
| 197 | struct timer_list update_timer; |
| 198 | }; |
| 199 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 200 | union sbs_rw_data { |
| 201 | u16 word; |
| 202 | u8 block[ACPI_SBS_BLOCK_MAX + 2]; |
| 203 | }; |
| 204 | |
| 205 | static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr, |
| 206 | char read_write, u8 command, int size, |
| 207 | union sbs_rw_data *data); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 208 | static void acpi_update_delay(struct acpi_sbs *sbs); |
| 209 | static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type); |
| 210 | |
| 211 | /* -------------------------------------------------------------------------- |
| 212 | SMBus Communication |
| 213 | -------------------------------------------------------------------------- */ |
| 214 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 215 | static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 216 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 217 | u8 val; |
| 218 | int err; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 219 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 220 | err = ec_read(sbs->base + address, &val); |
| 221 | if (!err) { |
| 222 | *data = val; |
| 223 | } |
| 224 | xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP); |
| 225 | return (err); |
| 226 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 227 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 228 | static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data) |
| 229 | { |
| 230 | int err; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 231 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 232 | err = ec_write(sbs->base + address, data); |
| 233 | return (err); |
| 234 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 235 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 236 | static int |
| 237 | acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr, |
| 238 | char read_write, u8 command, int size, |
| 239 | union sbs_rw_data *data) |
| 240 | { |
| 241 | unsigned char protocol, len = 0, temp[2] = { 0, 0 }; |
| 242 | int i; |
| 243 | |
| 244 | if (read_write == ACPI_SBS_SMBUS_READ) { |
| 245 | protocol = ACPI_EC_SMB_PRTCL_READ; |
| 246 | } else { |
| 247 | protocol = ACPI_EC_SMB_PRTCL_WRITE; |
| 248 | } |
| 249 | |
| 250 | switch (size) { |
| 251 | |
| 252 | case ACPI_SBS_WORD_DATA: |
| 253 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command); |
| 254 | if (read_write == ACPI_SBS_SMBUS_WRITE) { |
| 255 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word); |
| 256 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1, |
| 257 | data->word >> 8); |
| 258 | } |
| 259 | protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 260 | break; |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 261 | case ACPI_SBS_BLOCK_DATA: |
| 262 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command); |
| 263 | if (read_write == ACPI_SBS_SMBUS_WRITE) { |
| 264 | len = min_t(u8, data->block[0], 32); |
| 265 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len); |
| 266 | for (i = 0; i < len; i++) |
| 267 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i, |
| 268 | data->block[i + 1]); |
| 269 | } |
| 270 | protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 271 | break; |
| 272 | default: |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 273 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 274 | "unsupported transaction %d", size)); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 275 | return (-1); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 276 | } |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 277 | |
| 278 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1); |
| 279 | acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol); |
| 280 | |
| 281 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); |
| 282 | |
| 283 | if (~temp[0] & ACPI_EC_SMB_STS_DONE) { |
| 284 | xmsleep(ACPI_EC_SMB_ACCESS_SLEEP1); |
| 285 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); |
| 286 | } |
| 287 | if (~temp[0] & ACPI_EC_SMB_STS_DONE) { |
| 288 | xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2); |
| 289 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp); |
| 290 | } |
| 291 | if ((~temp[0] & ACPI_EC_SMB_STS_DONE) |
| 292 | || (temp[0] & ACPI_EC_SMB_STS_STATUS)) { |
| 293 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 294 | "transaction %d error", size)); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 295 | return (-1); |
| 296 | } |
| 297 | |
| 298 | if (read_write == ACPI_SBS_SMBUS_WRITE) { |
| 299 | return (0); |
| 300 | } |
| 301 | |
| 302 | switch (size) { |
| 303 | |
| 304 | case ACPI_SBS_WORD_DATA: |
| 305 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp); |
| 306 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1); |
| 307 | data->word = (temp[1] << 8) | temp[0]; |
| 308 | break; |
| 309 | |
| 310 | case ACPI_SBS_BLOCK_DATA: |
| 311 | len = 0; |
| 312 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len); |
| 313 | len = min_t(u8, len, 32); |
| 314 | for (i = 0; i < len; i++) |
| 315 | acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i, |
| 316 | data->block + i + 1); |
| 317 | data->block[0] = len; |
| 318 | break; |
| 319 | default: |
| 320 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 321 | "unsupported transaction %d", size)); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 322 | return (-1); |
| 323 | } |
| 324 | |
| 325 | return (0); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static int |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 329 | acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 330 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 331 | union sbs_rw_data data; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 332 | int result = 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 333 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 334 | result = acpi_ec_sbs_access(sbs, addr, |
| 335 | ACPI_SBS_SMBUS_READ, func, |
| 336 | ACPI_SBS_WORD_DATA, &data); |
| 337 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 338 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 339 | "acpi_ec_sbs_access() failed")); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 340 | } else { |
| 341 | *word = data.word; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 344 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static int |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 348 | acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 349 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 350 | union sbs_rw_data data; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 351 | int result = 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 352 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 353 | result = acpi_ec_sbs_access(sbs, addr, |
| 354 | ACPI_SBS_SMBUS_READ, func, |
| 355 | ACPI_SBS_BLOCK_DATA, &data); |
| 356 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 357 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 358 | "acpi_ec_sbs_access() failed")); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 359 | } else { |
| 360 | strncpy(str, (const char *)data.block + 1, data.block[0]); |
| 361 | str[data.block[0]] = 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 364 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static int |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 368 | acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word) |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 369 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 370 | union sbs_rw_data data; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 371 | int result = 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 372 | |
| 373 | data.word = word; |
| 374 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 375 | result = acpi_ec_sbs_access(sbs, addr, |
| 376 | ACPI_SBS_SMBUS_WRITE, func, |
| 377 | ACPI_SBS_WORD_DATA, &data); |
| 378 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 379 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 380 | "acpi_ec_sbs_access() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 381 | } |
| 382 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 383 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* -------------------------------------------------------------------------- |
| 387 | Smart Battery System Management |
| 388 | -------------------------------------------------------------------------- */ |
| 389 | |
| 390 | /* Smart Battery */ |
| 391 | |
| 392 | static int acpi_sbs_generate_event(struct acpi_device *device, |
| 393 | int event, int state, char *bid, char *class) |
| 394 | { |
| 395 | char bid_saved[5]; |
| 396 | char class_saved[20]; |
| 397 | int result = 0; |
| 398 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 399 | strcpy(bid_saved, acpi_device_bid(device)); |
| 400 | strcpy(class_saved, acpi_device_class(device)); |
| 401 | |
| 402 | strcpy(acpi_device_bid(device), bid); |
| 403 | strcpy(acpi_device_class(device), class); |
| 404 | |
| 405 | result = acpi_bus_generate_event(device, event, state); |
| 406 | |
| 407 | strcpy(acpi_device_bid(device), bid_saved); |
| 408 | strcpy(acpi_device_class(device), class_saved); |
| 409 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 410 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static int acpi_battery_get_present(struct acpi_battery *battery) |
| 414 | { |
| 415 | s16 state; |
| 416 | int result = 0; |
| 417 | int is_present = 0; |
| 418 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 419 | result = acpi_sbs_read_word(battery->sbs, |
| 420 | ACPI_SBSM_SMBUS_ADDR, 0x01, &state); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 421 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 422 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 423 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 424 | } |
| 425 | if (!result) { |
| 426 | is_present = (state & 0x000f) & (1 << battery->id); |
| 427 | } |
| 428 | battery->battery_present = is_present; |
| 429 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 430 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int acpi_battery_is_present(struct acpi_battery *battery) |
| 434 | { |
| 435 | return (battery->battery_present); |
| 436 | } |
| 437 | |
| 438 | static int acpi_ac_is_present(struct acpi_sbs *sbs) |
| 439 | { |
| 440 | return (sbs->ac_present); |
| 441 | } |
| 442 | |
| 443 | static int acpi_battery_select(struct acpi_battery *battery) |
| 444 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 445 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 446 | int result = 0; |
| 447 | s16 state; |
| 448 | int foo; |
| 449 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 450 | if (battery->sbs->sbsm_present) { |
| 451 | |
| 452 | /* Take special care not to knobble other nibbles of |
| 453 | * state (aka selector_state), since |
| 454 | * it causes charging to halt on SBSELs */ |
| 455 | |
| 456 | result = |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 457 | acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 458 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 459 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 460 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 461 | goto end; |
| 462 | } |
| 463 | |
| 464 | foo = (state & 0x0fff) | (1 << (battery->id + 12)); |
| 465 | result = |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 466 | acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 467 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 468 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 469 | "acpi_sbs_write_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 470 | goto end; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 475 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static int acpi_sbsm_get_info(struct acpi_sbs *sbs) |
| 479 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 480 | int result = 0; |
| 481 | s16 battery_system_info; |
| 482 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 483 | result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04, |
| 484 | &battery_system_info); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 485 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 486 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 487 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 488 | goto end; |
| 489 | } |
| 490 | |
| 491 | sbs->sbsm_batteries_supported = battery_system_info & 0x000f; |
| 492 | |
| 493 | end: |
| 494 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 495 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static int acpi_battery_get_info(struct acpi_battery *battery) |
| 499 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 500 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 501 | int result = 0; |
| 502 | s16 battery_mode; |
| 503 | s16 specification_info; |
| 504 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 505 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03, |
| 506 | &battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 507 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 508 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 509 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 510 | goto end; |
| 511 | } |
| 512 | battery->info.capacity_mode = (battery_mode & 0x8000) >> 15; |
| 513 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 514 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10, |
| 515 | &battery->info.full_charge_capacity); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 516 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 517 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 518 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 519 | goto end; |
| 520 | } |
| 521 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 522 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18, |
| 523 | &battery->info.design_capacity); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 524 | |
| 525 | if (result) { |
| 526 | goto end; |
| 527 | } |
| 528 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 529 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19, |
| 530 | &battery->info.design_voltage); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 531 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 532 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 533 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 534 | goto end; |
| 535 | } |
| 536 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 537 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a, |
| 538 | &specification_info); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 539 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 540 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 541 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 542 | goto end; |
| 543 | } |
| 544 | |
| 545 | switch ((specification_info & 0x0f00) >> 8) { |
| 546 | case 1: |
| 547 | battery->info.vscale = 10; |
| 548 | break; |
| 549 | case 2: |
| 550 | battery->info.vscale = 100; |
| 551 | break; |
| 552 | case 3: |
| 553 | battery->info.vscale = 1000; |
| 554 | break; |
| 555 | default: |
| 556 | battery->info.vscale = 1; |
| 557 | } |
| 558 | |
| 559 | switch ((specification_info & 0xf000) >> 12) { |
| 560 | case 1: |
| 561 | battery->info.ipscale = 10; |
| 562 | break; |
| 563 | case 2: |
| 564 | battery->info.ipscale = 100; |
| 565 | break; |
| 566 | case 3: |
| 567 | battery->info.ipscale = 1000; |
| 568 | break; |
| 569 | default: |
| 570 | battery->info.ipscale = 1; |
| 571 | } |
| 572 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 573 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c, |
| 574 | &battery->info.serial_number); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 575 | if (result) { |
| 576 | goto end; |
| 577 | } |
| 578 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 579 | result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20, |
| 580 | battery->info.manufacturer_name); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 581 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 582 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 583 | "acpi_sbs_read_str() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 584 | goto end; |
| 585 | } |
| 586 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 587 | result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21, |
| 588 | battery->info.device_name); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 589 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 590 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 591 | "acpi_sbs_read_str() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 592 | goto end; |
| 593 | } |
| 594 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 595 | result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22, |
| 596 | battery->info.device_chemistry); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 597 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 598 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 599 | "acpi_sbs_read_str() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 600 | goto end; |
| 601 | } |
| 602 | |
| 603 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 604 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void acpi_update_delay(struct acpi_sbs *sbs) |
| 608 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 609 | if (sbs->zombie) { |
| 610 | return; |
| 611 | } |
| 612 | if (sbs->update_time2 > 0) { |
| 613 | msleep(sbs->update_time2 * 1000); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | static int acpi_battery_get_state(struct acpi_battery *battery) |
| 618 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 619 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 620 | int result = 0; |
| 621 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 622 | acpi_update_delay(battery->sbs); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 623 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09, |
| 624 | &battery->state.voltage); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 625 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 626 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 627 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 628 | goto end; |
| 629 | } |
| 630 | |
| 631 | acpi_update_delay(battery->sbs); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 632 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a, |
| 633 | &battery->state.amperage); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 634 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 635 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 636 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 637 | goto end; |
| 638 | } |
| 639 | |
| 640 | acpi_update_delay(battery->sbs); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 641 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f, |
| 642 | &battery->state.remaining_capacity); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 643 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 644 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 645 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 646 | goto end; |
| 647 | } |
| 648 | |
| 649 | acpi_update_delay(battery->sbs); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 650 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x12, |
| 651 | &battery->state.average_time_to_empty); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 652 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 653 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 654 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 655 | goto end; |
| 656 | } |
| 657 | |
| 658 | acpi_update_delay(battery->sbs); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 659 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x13, |
| 660 | &battery->state.average_time_to_full); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 661 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 662 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 663 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 664 | goto end; |
| 665 | } |
| 666 | |
| 667 | acpi_update_delay(battery->sbs); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 668 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16, |
| 669 | &battery->state.battery_status); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 670 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 671 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 672 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 673 | goto end; |
| 674 | } |
| 675 | |
| 676 | acpi_update_delay(battery->sbs); |
| 677 | |
| 678 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 679 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static int acpi_battery_get_alarm(struct acpi_battery *battery) |
| 683 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 684 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 685 | int result = 0; |
| 686 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 687 | result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, |
| 688 | &battery->alarm.remaining_capacity); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 689 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 690 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 691 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 692 | goto end; |
| 693 | } |
| 694 | |
| 695 | acpi_update_delay(battery->sbs); |
| 696 | |
| 697 | end: |
| 698 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 699 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static int acpi_battery_set_alarm(struct acpi_battery *battery, |
| 703 | unsigned long alarm) |
| 704 | { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 705 | struct acpi_sbs *sbs = battery->sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 706 | int result = 0; |
| 707 | s16 battery_mode; |
| 708 | int foo; |
| 709 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 710 | result = acpi_battery_select(battery); |
| 711 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 712 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 713 | "acpi_battery_select() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 714 | goto end; |
| 715 | } |
| 716 | |
| 717 | /* If necessary, enable the alarm */ |
| 718 | |
| 719 | if (alarm > 0) { |
| 720 | result = |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 721 | acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03, |
| 722 | &battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 723 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 724 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 725 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 726 | goto end; |
| 727 | } |
| 728 | |
| 729 | result = |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 730 | acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, |
| 731 | battery_mode & 0xbfff); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 732 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 733 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 734 | "acpi_sbs_write_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 735 | goto end; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | foo = alarm / (battery->info.capacity_mode ? 10 : 1); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 740 | result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 741 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 742 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 743 | "acpi_sbs_write_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 744 | goto end; |
| 745 | } |
| 746 | |
| 747 | end: |
| 748 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 749 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static int acpi_battery_set_mode(struct acpi_battery *battery) |
| 753 | { |
| 754 | int result = 0; |
| 755 | s16 battery_mode; |
| 756 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 757 | if (capacity_mode == DEF_CAPACITY_UNIT) { |
| 758 | goto end; |
| 759 | } |
| 760 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 761 | result = acpi_sbs_read_word(battery->sbs, |
| 762 | ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 763 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 764 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 765 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 766 | goto end; |
| 767 | } |
| 768 | |
| 769 | if (capacity_mode == MAH_CAPACITY_UNIT) { |
| 770 | battery_mode &= 0x7fff; |
| 771 | } else { |
| 772 | battery_mode |= 0x8000; |
| 773 | } |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 774 | result = acpi_sbs_write_word(battery->sbs, |
| 775 | ACPI_SB_SMBUS_ADDR, 0x03, battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 776 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 777 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 778 | "acpi_sbs_write_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 779 | goto end; |
| 780 | } |
| 781 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 782 | result = acpi_sbs_read_word(battery->sbs, |
| 783 | ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 784 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 785 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 786 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 787 | goto end; |
| 788 | } |
| 789 | |
| 790 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 791 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static int acpi_battery_init(struct acpi_battery *battery) |
| 795 | { |
| 796 | int result = 0; |
| 797 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 798 | result = acpi_battery_select(battery); |
| 799 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 800 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 801 | "acpi_battery_init() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 802 | goto end; |
| 803 | } |
| 804 | |
| 805 | result = acpi_battery_set_mode(battery); |
| 806 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 807 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 808 | "acpi_battery_set_mode() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 809 | goto end; |
| 810 | } |
| 811 | |
| 812 | result = acpi_battery_get_info(battery); |
| 813 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 814 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 815 | "acpi_battery_get_info() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 816 | goto end; |
| 817 | } |
| 818 | |
| 819 | result = acpi_battery_get_state(battery); |
| 820 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 821 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 822 | "acpi_battery_get_state() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 823 | goto end; |
| 824 | } |
| 825 | |
| 826 | result = acpi_battery_get_alarm(battery); |
| 827 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 828 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 829 | "acpi_battery_get_alarm() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 830 | goto end; |
| 831 | } |
| 832 | |
| 833 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 834 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static int acpi_ac_get_present(struct acpi_sbs *sbs) |
| 838 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 839 | int result = 0; |
| 840 | s16 charger_status; |
| 841 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 842 | result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13, |
| 843 | &charger_status); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 844 | |
| 845 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 846 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 847 | "acpi_sbs_read_word() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 848 | goto end; |
| 849 | } |
| 850 | |
| 851 | sbs->ac_present = (charger_status & 0x8000) >> 15; |
| 852 | |
| 853 | end: |
| 854 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 855 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* -------------------------------------------------------------------------- |
| 859 | FS Interface (/proc/acpi) |
| 860 | -------------------------------------------------------------------------- */ |
| 861 | |
| 862 | /* Generic Routines */ |
| 863 | |
| 864 | static int |
| 865 | acpi_sbs_generic_add_fs(struct proc_dir_entry **dir, |
| 866 | struct proc_dir_entry *parent_dir, |
| 867 | char *dir_name, |
| 868 | struct file_operations *info_fops, |
| 869 | struct file_operations *state_fops, |
| 870 | struct file_operations *alarm_fops, void *data) |
| 871 | { |
| 872 | struct proc_dir_entry *entry = NULL; |
| 873 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 874 | if (!*dir) { |
| 875 | *dir = proc_mkdir(dir_name, parent_dir); |
| 876 | if (!*dir) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 877 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 878 | "proc_mkdir() failed")); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 879 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 880 | } |
| 881 | (*dir)->owner = THIS_MODULE; |
| 882 | } |
| 883 | |
| 884 | /* 'info' [R] */ |
| 885 | if (info_fops) { |
| 886 | entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir); |
| 887 | if (!entry) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 888 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 889 | "create_proc_entry() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 890 | } else { |
| 891 | entry->proc_fops = info_fops; |
| 892 | entry->data = data; |
| 893 | entry->owner = THIS_MODULE; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* 'state' [R] */ |
| 898 | if (state_fops) { |
| 899 | entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir); |
| 900 | if (!entry) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 901 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 902 | "create_proc_entry() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 903 | } else { |
| 904 | entry->proc_fops = state_fops; |
| 905 | entry->data = data; |
| 906 | entry->owner = THIS_MODULE; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /* 'alarm' [R/W] */ |
| 911 | if (alarm_fops) { |
| 912 | entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir); |
| 913 | if (!entry) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 914 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 915 | "create_proc_entry() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 916 | } else { |
| 917 | entry->proc_fops = alarm_fops; |
| 918 | entry->data = data; |
| 919 | entry->owner = THIS_MODULE; |
| 920 | } |
| 921 | } |
| 922 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 923 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static void |
| 927 | acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir, |
| 928 | struct proc_dir_entry *parent_dir) |
| 929 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 930 | |
| 931 | if (*dir) { |
| 932 | remove_proc_entry(ACPI_SBS_FILE_INFO, *dir); |
| 933 | remove_proc_entry(ACPI_SBS_FILE_STATE, *dir); |
| 934 | remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir); |
| 935 | remove_proc_entry((*dir)->name, parent_dir); |
| 936 | *dir = NULL; |
| 937 | } |
| 938 | |
| 939 | } |
| 940 | |
| 941 | /* Smart Battery Interface */ |
| 942 | |
| 943 | static struct proc_dir_entry *acpi_battery_dir = NULL; |
| 944 | |
| 945 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) |
| 946 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 947 | struct acpi_battery *battery = seq->private; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 948 | int cscale; |
| 949 | int result = 0; |
| 950 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 951 | if (battery->sbs->zombie) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 952 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | down(&sbs_sem); |
| 956 | |
| 957 | if (update_mode == REQUEST_UPDATE_MODE) { |
| 958 | result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_INFO); |
| 959 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 960 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 961 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
| 965 | if (acpi_battery_is_present(battery)) { |
| 966 | seq_printf(seq, "present: yes\n"); |
| 967 | } else { |
| 968 | seq_printf(seq, "present: no\n"); |
| 969 | goto end; |
| 970 | } |
| 971 | |
| 972 | if (battery->info.capacity_mode) { |
| 973 | cscale = battery->info.vscale * battery->info.ipscale; |
| 974 | } else { |
| 975 | cscale = battery->info.ipscale; |
| 976 | } |
| 977 | seq_printf(seq, "design capacity: %i%s", |
| 978 | battery->info.design_capacity * cscale, |
| 979 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); |
| 980 | |
| 981 | seq_printf(seq, "last full capacity: %i%s", |
| 982 | battery->info.full_charge_capacity * cscale, |
| 983 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); |
| 984 | |
| 985 | seq_printf(seq, "battery technology: rechargeable\n"); |
| 986 | |
| 987 | seq_printf(seq, "design voltage: %i mV\n", |
| 988 | battery->info.design_voltage * battery->info.vscale); |
| 989 | |
| 990 | seq_printf(seq, "design capacity warning: unknown\n"); |
| 991 | seq_printf(seq, "design capacity low: unknown\n"); |
| 992 | seq_printf(seq, "capacity granularity 1: unknown\n"); |
| 993 | seq_printf(seq, "capacity granularity 2: unknown\n"); |
| 994 | |
| 995 | seq_printf(seq, "model number: %s\n", |
| 996 | battery->info.device_name); |
| 997 | |
| 998 | seq_printf(seq, "serial number: %i\n", |
| 999 | battery->info.serial_number); |
| 1000 | |
| 1001 | seq_printf(seq, "battery type: %s\n", |
| 1002 | battery->info.device_chemistry); |
| 1003 | |
| 1004 | seq_printf(seq, "OEM info: %s\n", |
| 1005 | battery->info.manufacturer_name); |
| 1006 | |
| 1007 | end: |
| 1008 | |
| 1009 | up(&sbs_sem); |
| 1010 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1011 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) |
| 1015 | { |
| 1016 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); |
| 1017 | } |
| 1018 | |
| 1019 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) |
| 1020 | { |
| 1021 | struct acpi_battery *battery = (struct acpi_battery *)seq->private; |
| 1022 | int result = 0; |
| 1023 | int cscale; |
| 1024 | int foo; |
| 1025 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1026 | if (battery->sbs->zombie) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1027 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | down(&sbs_sem); |
| 1031 | |
| 1032 | if (update_mode == REQUEST_UPDATE_MODE) { |
| 1033 | result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_STATE); |
| 1034 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1035 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1036 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | if (acpi_battery_is_present(battery)) { |
| 1041 | seq_printf(seq, "present: yes\n"); |
| 1042 | } else { |
| 1043 | seq_printf(seq, "present: no\n"); |
| 1044 | goto end; |
| 1045 | } |
| 1046 | |
| 1047 | if (battery->info.capacity_mode) { |
| 1048 | cscale = battery->info.vscale * battery->info.ipscale; |
| 1049 | } else { |
| 1050 | cscale = battery->info.ipscale; |
| 1051 | } |
| 1052 | |
| 1053 | if (battery->state.battery_status & 0x0010) { |
| 1054 | seq_printf(seq, "capacity state: critical\n"); |
| 1055 | } else { |
| 1056 | seq_printf(seq, "capacity state: ok\n"); |
| 1057 | } |
Vladimir Lebedev | e6d0f56 | 2007-02-10 01:51:13 -0500 | [diff] [blame] | 1058 | |
| 1059 | foo = (s16) battery->state.amperage * battery->info.ipscale; |
| 1060 | if (battery->info.capacity_mode) { |
| 1061 | foo = foo * battery->info.design_voltage / 1000; |
| 1062 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1063 | if (battery->state.amperage < 0) { |
| 1064 | seq_printf(seq, "charging state: discharging\n"); |
Vladimir Lebedev | e6d0f56 | 2007-02-10 01:51:13 -0500 | [diff] [blame] | 1065 | seq_printf(seq, "present rate: %d %s\n", |
| 1066 | -foo, battery->info.capacity_mode ? "mW" : "mA"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1067 | } else if (battery->state.amperage > 0) { |
| 1068 | seq_printf(seq, "charging state: charging\n"); |
Vladimir Lebedev | e6d0f56 | 2007-02-10 01:51:13 -0500 | [diff] [blame] | 1069 | seq_printf(seq, "present rate: %d %s\n", |
| 1070 | foo, battery->info.capacity_mode ? "mW" : "mA"); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1071 | } else { |
| 1072 | seq_printf(seq, "charging state: charged\n"); |
| 1073 | seq_printf(seq, "present rate: 0 %s\n", |
| 1074 | battery->info.capacity_mode ? "mW" : "mA"); |
| 1075 | } |
| 1076 | |
| 1077 | seq_printf(seq, "remaining capacity: %i%s", |
| 1078 | battery->state.remaining_capacity * cscale, |
| 1079 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); |
| 1080 | |
| 1081 | seq_printf(seq, "present voltage: %i mV\n", |
| 1082 | battery->state.voltage * battery->info.vscale); |
| 1083 | |
| 1084 | end: |
| 1085 | |
| 1086 | up(&sbs_sem); |
| 1087 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1088 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) |
| 1092 | { |
| 1093 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); |
| 1094 | } |
| 1095 | |
| 1096 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) |
| 1097 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1098 | struct acpi_battery *battery = seq->private; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1099 | int result = 0; |
| 1100 | int cscale; |
| 1101 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1102 | if (battery->sbs->zombie) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1103 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | down(&sbs_sem); |
| 1107 | |
| 1108 | if (update_mode == REQUEST_UPDATE_MODE) { |
| 1109 | result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM); |
| 1110 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1111 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1112 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | if (!acpi_battery_is_present(battery)) { |
| 1117 | seq_printf(seq, "present: no\n"); |
| 1118 | goto end; |
| 1119 | } |
| 1120 | |
| 1121 | if (battery->info.capacity_mode) { |
| 1122 | cscale = battery->info.vscale * battery->info.ipscale; |
| 1123 | } else { |
| 1124 | cscale = battery->info.ipscale; |
| 1125 | } |
| 1126 | |
| 1127 | seq_printf(seq, "alarm: "); |
| 1128 | if (battery->alarm.remaining_capacity) { |
| 1129 | seq_printf(seq, "%i%s", |
| 1130 | battery->alarm.remaining_capacity * cscale, |
| 1131 | battery->info.capacity_mode ? "0 mWh\n" : " mAh\n"); |
| 1132 | } else { |
| 1133 | seq_printf(seq, "disabled\n"); |
| 1134 | } |
| 1135 | |
| 1136 | end: |
| 1137 | |
| 1138 | up(&sbs_sem); |
| 1139 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1140 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static ssize_t |
| 1144 | acpi_battery_write_alarm(struct file *file, const char __user * buffer, |
| 1145 | size_t count, loff_t * ppos) |
| 1146 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1147 | struct seq_file *seq = file->private_data; |
| 1148 | struct acpi_battery *battery = seq->private; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1149 | char alarm_string[12] = { '\0' }; |
| 1150 | int result, old_alarm, new_alarm; |
| 1151 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1152 | if (battery->sbs->zombie) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1153 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | down(&sbs_sem); |
| 1157 | |
| 1158 | if (!acpi_battery_is_present(battery)) { |
| 1159 | result = -ENODEV; |
| 1160 | goto end; |
| 1161 | } |
| 1162 | |
| 1163 | if (count > sizeof(alarm_string) - 1) { |
| 1164 | result = -EINVAL; |
| 1165 | goto end; |
| 1166 | } |
| 1167 | |
| 1168 | if (copy_from_user(alarm_string, buffer, count)) { |
| 1169 | result = -EFAULT; |
| 1170 | goto end; |
| 1171 | } |
| 1172 | |
| 1173 | alarm_string[count] = 0; |
| 1174 | |
| 1175 | old_alarm = battery->alarm.remaining_capacity; |
| 1176 | new_alarm = simple_strtoul(alarm_string, NULL, 0); |
| 1177 | |
| 1178 | result = acpi_battery_set_alarm(battery, new_alarm); |
| 1179 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1180 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1181 | "acpi_battery_set_alarm() failed")); |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1182 | acpi_battery_set_alarm(battery, old_alarm); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1183 | goto end; |
| 1184 | } |
| 1185 | result = acpi_battery_get_alarm(battery); |
| 1186 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1187 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1188 | "acpi_battery_get_alarm() failed")); |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1189 | acpi_battery_set_alarm(battery, old_alarm); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1190 | goto end; |
| 1191 | } |
| 1192 | |
| 1193 | end: |
| 1194 | up(&sbs_sem); |
| 1195 | |
| 1196 | if (result) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1197 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1198 | } else { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1199 | return count; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) |
| 1204 | { |
| 1205 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); |
| 1206 | } |
| 1207 | |
| 1208 | static struct file_operations acpi_battery_info_fops = { |
| 1209 | .open = acpi_battery_info_open_fs, |
| 1210 | .read = seq_read, |
| 1211 | .llseek = seq_lseek, |
| 1212 | .release = single_release, |
| 1213 | .owner = THIS_MODULE, |
| 1214 | }; |
| 1215 | |
| 1216 | static struct file_operations acpi_battery_state_fops = { |
| 1217 | .open = acpi_battery_state_open_fs, |
| 1218 | .read = seq_read, |
| 1219 | .llseek = seq_lseek, |
| 1220 | .release = single_release, |
| 1221 | .owner = THIS_MODULE, |
| 1222 | }; |
| 1223 | |
| 1224 | static struct file_operations acpi_battery_alarm_fops = { |
| 1225 | .open = acpi_battery_alarm_open_fs, |
| 1226 | .read = seq_read, |
| 1227 | .write = acpi_battery_write_alarm, |
| 1228 | .llseek = seq_lseek, |
| 1229 | .release = single_release, |
| 1230 | .owner = THIS_MODULE, |
| 1231 | }; |
| 1232 | |
| 1233 | /* Legacy AC Adapter Interface */ |
| 1234 | |
| 1235 | static struct proc_dir_entry *acpi_ac_dir = NULL; |
| 1236 | |
| 1237 | static int acpi_ac_read_state(struct seq_file *seq, void *offset) |
| 1238 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1239 | struct acpi_sbs *sbs = seq->private; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1240 | int result; |
| 1241 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1242 | if (sbs->zombie) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1243 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | down(&sbs_sem); |
| 1247 | |
| 1248 | if (update_mode == REQUEST_UPDATE_MODE) { |
| 1249 | result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE); |
| 1250 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1251 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1252 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | seq_printf(seq, "state: %s\n", |
| 1257 | sbs->ac_present ? "on-line" : "off-line"); |
| 1258 | |
| 1259 | up(&sbs_sem); |
| 1260 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1261 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | static int acpi_ac_state_open_fs(struct inode *inode, struct file *file) |
| 1265 | { |
| 1266 | return single_open(file, acpi_ac_read_state, PDE(inode)->data); |
| 1267 | } |
| 1268 | |
| 1269 | static struct file_operations acpi_ac_state_fops = { |
| 1270 | .open = acpi_ac_state_open_fs, |
| 1271 | .read = seq_read, |
| 1272 | .llseek = seq_lseek, |
| 1273 | .release = single_release, |
| 1274 | .owner = THIS_MODULE, |
| 1275 | }; |
| 1276 | |
| 1277 | /* -------------------------------------------------------------------------- |
| 1278 | Driver Interface |
| 1279 | -------------------------------------------------------------------------- */ |
| 1280 | |
| 1281 | /* Smart Battery */ |
| 1282 | |
| 1283 | static int acpi_battery_add(struct acpi_sbs *sbs, int id) |
| 1284 | { |
| 1285 | int is_present; |
| 1286 | int result; |
| 1287 | char dir_name[32]; |
| 1288 | struct acpi_battery *battery; |
| 1289 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1290 | battery = &sbs->battery[id]; |
| 1291 | |
| 1292 | battery->alive = 0; |
| 1293 | |
| 1294 | battery->init_state = 0; |
| 1295 | battery->id = id; |
| 1296 | battery->sbs = sbs; |
| 1297 | |
| 1298 | result = acpi_battery_select(battery); |
| 1299 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1300 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1301 | "acpi_battery_select() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1302 | goto end; |
| 1303 | } |
| 1304 | |
| 1305 | result = acpi_battery_get_present(battery); |
| 1306 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1307 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1308 | "acpi_battery_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1309 | goto end; |
| 1310 | } |
| 1311 | |
| 1312 | is_present = acpi_battery_is_present(battery); |
| 1313 | |
| 1314 | if (is_present) { |
| 1315 | result = acpi_battery_init(battery); |
| 1316 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1317 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1318 | "acpi_battery_init() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1319 | goto end; |
| 1320 | } |
| 1321 | battery->init_state = 1; |
| 1322 | } |
| 1323 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1324 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1325 | |
| 1326 | result = acpi_sbs_generic_add_fs(&battery->battery_entry, |
| 1327 | acpi_battery_dir, |
| 1328 | dir_name, |
| 1329 | &acpi_battery_info_fops, |
| 1330 | &acpi_battery_state_fops, |
| 1331 | &acpi_battery_alarm_fops, battery); |
| 1332 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1333 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1334 | "acpi_sbs_generic_add_fs() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1335 | goto end; |
| 1336 | } |
| 1337 | battery->alive = 1; |
| 1338 | |
| 1339 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1340 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | static void acpi_battery_remove(struct acpi_sbs *sbs, int id) |
| 1344 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1345 | |
| 1346 | if (sbs->battery[id].battery_entry) { |
| 1347 | acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry), |
| 1348 | acpi_battery_dir); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | static int acpi_ac_add(struct acpi_sbs *sbs) |
| 1353 | { |
| 1354 | int result; |
| 1355 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1356 | result = acpi_ac_get_present(sbs); |
| 1357 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1358 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1359 | "acpi_ac_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1360 | goto end; |
| 1361 | } |
| 1362 | |
| 1363 | result = acpi_sbs_generic_add_fs(&sbs->ac_entry, |
| 1364 | acpi_ac_dir, |
| 1365 | ACPI_AC_DIR_NAME, |
| 1366 | NULL, &acpi_ac_state_fops, NULL, sbs); |
| 1367 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1368 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1369 | "acpi_sbs_generic_add_fs() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1370 | goto end; |
| 1371 | } |
| 1372 | |
| 1373 | end: |
| 1374 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1375 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | static void acpi_ac_remove(struct acpi_sbs *sbs) |
| 1379 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1380 | |
| 1381 | if (sbs->ac_entry) { |
| 1382 | acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | static void acpi_sbs_update_queue_run(unsigned long data) |
| 1387 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1388 | acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data); |
| 1389 | } |
| 1390 | |
| 1391 | static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type) |
| 1392 | { |
| 1393 | struct acpi_battery *battery; |
| 1394 | int result = 0; |
| 1395 | int old_ac_present; |
| 1396 | int old_battery_present; |
| 1397 | int new_ac_present; |
| 1398 | int new_battery_present; |
| 1399 | int id; |
| 1400 | char dir_name[32]; |
| 1401 | int do_battery_init, do_ac_init; |
| 1402 | s16 old_remaining_capacity; |
| 1403 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1404 | if (sbs->zombie) { |
| 1405 | goto end; |
| 1406 | } |
| 1407 | |
| 1408 | old_ac_present = acpi_ac_is_present(sbs); |
| 1409 | |
| 1410 | result = acpi_ac_get_present(sbs); |
| 1411 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1412 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1413 | "acpi_ac_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | new_ac_present = acpi_ac_is_present(sbs); |
| 1417 | |
| 1418 | do_ac_init = (old_ac_present != new_ac_present); |
| 1419 | |
| 1420 | if (data_type == DATA_TYPE_AC_STATE) { |
| 1421 | goto end; |
| 1422 | } |
| 1423 | |
| 1424 | for (id = 0; id < MAX_SBS_BAT; id++) { |
| 1425 | battery = &sbs->battery[id]; |
| 1426 | if (battery->alive == 0) { |
| 1427 | continue; |
| 1428 | } |
| 1429 | |
| 1430 | old_remaining_capacity = battery->state.remaining_capacity; |
| 1431 | |
| 1432 | old_battery_present = acpi_battery_is_present(battery); |
| 1433 | |
| 1434 | result = acpi_battery_select(battery); |
| 1435 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1436 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1437 | "acpi_battery_select() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1438 | } |
| 1439 | if (sbs->zombie) { |
| 1440 | goto end; |
| 1441 | } |
| 1442 | |
| 1443 | result = acpi_battery_get_present(battery); |
| 1444 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1445 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1446 | "acpi_battery_get_present() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1447 | } |
| 1448 | if (sbs->zombie) { |
| 1449 | goto end; |
| 1450 | } |
| 1451 | |
| 1452 | new_battery_present = acpi_battery_is_present(battery); |
| 1453 | |
| 1454 | do_battery_init = ((old_battery_present != new_battery_present) |
| 1455 | && new_battery_present); |
| 1456 | |
| 1457 | if (sbs->zombie) { |
| 1458 | goto end; |
| 1459 | } |
| 1460 | if (do_ac_init || do_battery_init || |
| 1461 | update_info_mode || sbs->update_info_mode) { |
| 1462 | if (sbs->update_info_mode) { |
| 1463 | sbs->update_info_mode = 0; |
| 1464 | } else { |
| 1465 | sbs->update_info_mode = 1; |
| 1466 | } |
| 1467 | result = acpi_battery_init(battery); |
| 1468 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1469 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1470 | "acpi_battery_init() " |
| 1471 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | if (data_type == DATA_TYPE_INFO) { |
| 1475 | continue; |
| 1476 | } |
| 1477 | |
| 1478 | if (sbs->zombie) { |
| 1479 | goto end; |
| 1480 | } |
| 1481 | if (new_battery_present) { |
| 1482 | result = acpi_battery_get_alarm(battery); |
| 1483 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1484 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1485 | "acpi_battery_get_alarm() " |
| 1486 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1487 | } |
| 1488 | if (data_type == DATA_TYPE_ALARM) { |
| 1489 | continue; |
| 1490 | } |
| 1491 | |
| 1492 | result = acpi_battery_get_state(battery); |
| 1493 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1494 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1495 | "acpi_battery_get_state() " |
| 1496 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | if (sbs->zombie) { |
| 1500 | goto end; |
| 1501 | } |
| 1502 | if (data_type != DATA_TYPE_COMMON) { |
| 1503 | continue; |
| 1504 | } |
| 1505 | |
| 1506 | if (old_battery_present != new_battery_present) { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1507 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1508 | result = acpi_sbs_generate_event(sbs->device, |
| 1509 | ACPI_SBS_BATTERY_NOTIFY_STATUS, |
| 1510 | new_battery_present, |
| 1511 | dir_name, |
| 1512 | ACPI_BATTERY_CLASS); |
| 1513 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1514 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1515 | "acpi_sbs_generate_event() " |
| 1516 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | if (old_remaining_capacity != battery->state.remaining_capacity) { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1520 | sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1521 | result = acpi_sbs_generate_event(sbs->device, |
| 1522 | ACPI_SBS_BATTERY_NOTIFY_STATUS, |
| 1523 | new_battery_present, |
| 1524 | dir_name, |
| 1525 | ACPI_BATTERY_CLASS); |
| 1526 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1527 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1528 | "acpi_sbs_generate_event() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | } |
| 1533 | if (sbs->zombie) { |
| 1534 | goto end; |
| 1535 | } |
| 1536 | if (data_type != DATA_TYPE_COMMON) { |
| 1537 | goto end; |
| 1538 | } |
| 1539 | |
| 1540 | if (old_ac_present != new_ac_present) { |
| 1541 | result = acpi_sbs_generate_event(sbs->device, |
| 1542 | ACPI_SBS_AC_NOTIFY_STATUS, |
| 1543 | new_ac_present, |
| 1544 | ACPI_AC_DIR_NAME, |
| 1545 | ACPI_AC_CLASS); |
| 1546 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1547 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1548 | "acpi_sbs_generate_event() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | end: |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1553 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | static void acpi_sbs_update_queue(void *data) |
| 1557 | { |
| 1558 | struct acpi_sbs *sbs = data; |
| 1559 | unsigned long delay = -1; |
| 1560 | int result; |
| 1561 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1562 | if (sbs->zombie) { |
| 1563 | goto end; |
| 1564 | } |
| 1565 | |
| 1566 | result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON); |
| 1567 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1568 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1569 | "acpi_sbs_update_run() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | if (sbs->zombie) { |
| 1573 | goto end; |
| 1574 | } |
| 1575 | |
| 1576 | if (update_mode == REQUEST_UPDATE_MODE) { |
| 1577 | goto end; |
| 1578 | } |
| 1579 | |
| 1580 | delay = jiffies + HZ * update_time; |
| 1581 | sbs->update_timer.data = (unsigned long)data; |
| 1582 | sbs->update_timer.function = acpi_sbs_update_queue_run; |
| 1583 | sbs->update_timer.expires = delay; |
| 1584 | add_timer(&sbs->update_timer); |
| 1585 | end: |
| 1586 | ; |
| 1587 | } |
| 1588 | |
| 1589 | static int acpi_sbs_add(struct acpi_device *device) |
| 1590 | { |
| 1591 | struct acpi_sbs *sbs = NULL; |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1592 | int result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1593 | unsigned long sbs_obj; |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1594 | int id; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1595 | acpi_status status = AE_OK; |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1596 | unsigned long val; |
| 1597 | |
| 1598 | status = |
| 1599 | acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val); |
| 1600 | if (ACPI_FAILURE(status)) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1601 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC")); |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1602 | return -EIO; |
| 1603 | } |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1604 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 1605 | sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1606 | if (!sbs) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1607 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kmalloc() failed")); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1608 | return -ENOMEM; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1609 | } |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1610 | sbs->base = (val & 0xff00ull) >> 8; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1611 | |
| 1612 | sbs->device = device; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1613 | |
| 1614 | strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME); |
| 1615 | strcpy(acpi_device_class(device), ACPI_SBS_CLASS); |
| 1616 | acpi_driver_data(device) = sbs; |
| 1617 | |
| 1618 | sbs->update_time = 0; |
| 1619 | sbs->update_time2 = 0; |
| 1620 | |
| 1621 | result = acpi_ac_add(sbs); |
| 1622 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1623 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1624 | goto end; |
| 1625 | } |
| 1626 | result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj); |
| 1627 | if (ACPI_FAILURE(result)) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1628 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1629 | "acpi_evaluate_integer() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1630 | result = -EIO; |
| 1631 | goto end; |
| 1632 | } |
| 1633 | |
| 1634 | if (sbs_obj > 0) { |
| 1635 | result = acpi_sbsm_get_info(sbs); |
| 1636 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1637 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1638 | "acpi_sbsm_get_info() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1639 | goto end; |
| 1640 | } |
| 1641 | sbs->sbsm_present = 1; |
| 1642 | } |
| 1643 | if (sbs->sbsm_present == 0) { |
| 1644 | result = acpi_battery_add(sbs, 0); |
| 1645 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1646 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1647 | "acpi_battery_add() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1648 | goto end; |
| 1649 | } |
| 1650 | } else { |
| 1651 | for (id = 0; id < MAX_SBS_BAT; id++) { |
| 1652 | if ((sbs->sbsm_batteries_supported & (1 << id))) { |
| 1653 | result = acpi_battery_add(sbs, id); |
| 1654 | if (result) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1655 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1656 | "acpi_battery_add() " |
| 1657 | "failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1658 | goto end; |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | sbs->handle = device->handle; |
| 1665 | |
| 1666 | init_timer(&sbs->update_timer); |
| 1667 | if (update_mode == QUEUE_UPDATE_MODE) { |
| 1668 | status = acpi_os_execute(OSL_GPE_HANDLER, |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1669 | acpi_sbs_update_queue, sbs); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1670 | if (status != AE_OK) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1671 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1672 | "acpi_os_execute() failed")); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | sbs->update_time = update_time; |
| 1676 | sbs->update_time2 = update_time2; |
| 1677 | |
| 1678 | printk(KERN_INFO PREFIX "%s [%s]\n", |
| 1679 | acpi_device_name(device), acpi_device_bid(device)); |
| 1680 | |
| 1681 | end: |
| 1682 | if (result) { |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1683 | acpi_sbs_remove(device, 0); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1684 | } |
| 1685 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1686 | return result; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | int acpi_sbs_remove(struct acpi_device *device, int type) |
| 1690 | { |
Len Brown | cece901 | 2006-12-16 01:04:27 -0500 | [diff] [blame] | 1691 | struct acpi_sbs *sbs; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1692 | int id; |
| 1693 | |
Lebedev, Vladimir P | 963497c | 2006-09-05 19:49:13 +0400 | [diff] [blame] | 1694 | if (!device) { |
| 1695 | return -EINVAL; |
| 1696 | } |
| 1697 | |
| 1698 | sbs = (struct acpi_sbs *)acpi_driver_data(device); |
| 1699 | |
| 1700 | if (!sbs) { |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1701 | return -EINVAL; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | sbs->zombie = 1; |
| 1705 | sbs->update_time = 0; |
| 1706 | sbs->update_time2 = 0; |
| 1707 | del_timer_sync(&sbs->update_timer); |
| 1708 | acpi_os_wait_events_complete(NULL); |
| 1709 | del_timer_sync(&sbs->update_timer); |
| 1710 | |
| 1711 | for (id = 0; id < MAX_SBS_BAT; id++) { |
| 1712 | acpi_battery_remove(sbs, id); |
| 1713 | } |
| 1714 | |
| 1715 | acpi_ac_remove(sbs); |
| 1716 | |
Vladimir Lebedev | 6d15702 | 2007-03-19 17:45:50 +0300 | [diff] [blame] | 1717 | acpi_driver_data(device) = NULL; |
| 1718 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1719 | kfree(sbs); |
| 1720 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1721 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | static int __init acpi_sbs_init(void) |
| 1725 | { |
| 1726 | int result = 0; |
| 1727 | |
Len Brown | b20d2ae | 2006-08-15 23:21:37 -0400 | [diff] [blame] | 1728 | if (acpi_disabled) |
| 1729 | return -ENODEV; |
| 1730 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1731 | init_MUTEX(&sbs_sem); |
| 1732 | |
| 1733 | if (capacity_mode != DEF_CAPACITY_UNIT |
| 1734 | && capacity_mode != MAH_CAPACITY_UNIT |
| 1735 | && capacity_mode != MWH_CAPACITY_UNIT) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1736 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_sbs_init: " |
| 1737 | "invalid capacity_mode = %d", capacity_mode)); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1738 | return -EINVAL; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | acpi_ac_dir = acpi_lock_ac_dir(); |
| 1742 | if (!acpi_ac_dir) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1743 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1744 | "acpi_lock_ac_dir() failed")); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1745 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | acpi_battery_dir = acpi_lock_battery_dir(); |
| 1749 | if (!acpi_battery_dir) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1750 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1751 | "acpi_lock_battery_dir() failed")); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1752 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | result = acpi_bus_register_driver(&acpi_sbs_driver); |
| 1756 | if (result < 0) { |
Vladimir Lebedev | 6845118 | 2007-03-19 17:45:50 +0300 | [diff] [blame^] | 1757 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, |
| 1758 | "acpi_bus_register_driver() failed")); |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1759 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1760 | } |
| 1761 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1762 | return 0; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | static void __exit acpi_sbs_exit(void) |
| 1766 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1767 | |
| 1768 | acpi_bus_unregister_driver(&acpi_sbs_driver); |
| 1769 | |
| 1770 | acpi_unlock_ac_dir(acpi_ac_dir); |
| 1771 | acpi_ac_dir = NULL; |
| 1772 | acpi_unlock_battery_dir(acpi_battery_dir); |
| 1773 | acpi_battery_dir = NULL; |
| 1774 | |
Len Brown | 635227e | 2006-07-01 16:48:23 -0400 | [diff] [blame] | 1775 | return; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | module_init(acpi_sbs_init); |
| 1779 | module_exit(acpi_sbs_exit); |