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