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