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