blob: 395664528dd1b103b2ff25d8857cf9349281b57b [file] [log] [blame]
Rich Townsend3f86b832006-07-01 11:36:54 -04001/*
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 Lebedev6d157022007-03-19 17:45:50 +030033#include <linux/timer.h>
Rich Townsend3f86b832006-07-01 11:36:54 -040034#include <linux/delay.h>
35
Rich Townsend3f86b832006-07-01 11:36:54 -040036#define ACPI_SBS_COMPONENT 0x00080000
37#define ACPI_SBS_CLASS "sbs"
38#define ACPI_AC_CLASS "ac_adapter"
39#define ACPI_BATTERY_CLASS "battery"
40#define ACPI_SBS_HID "ACPI0002"
Rich Townsend3f86b832006-07-01 11:36:54 -040041#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 Brownf52fd662007-02-12 22:42:12 -050056ACPI_MODULE_NAME("sbs");
Rich Townsend3f86b832006-07-01 11:36:54 -040057
58MODULE_AUTHOR("Rich Townsend");
59MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
60MODULE_LICENSE("GPL");
61
Vladimir Lebedev6d157022007-03-19 17:45:50 +030062#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
98extern struct proc_dir_entry *acpi_lock_ac_dir(void);
99extern struct proc_dir_entry *acpi_lock_battery_dir(void);
100extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
101extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
102
103#define MAX_SBS_BAT 4
104#define ACPI_SBS_BLOCK_MAX 32
105
106#define ACPI_SBS_SMBUS_READ 1
107#define ACPI_SBS_SMBUS_WRITE 2
108
109#define ACPI_SBS_WORD_DATA 1
110#define ACPI_SBS_BLOCK_DATA 2
111
Rich Townsend3f86b832006-07-01 11:36:54 -0400112static struct semaphore sbs_sem;
113
114#define UPDATE_MODE QUEUE_UPDATE_MODE
115/* REQUEST_UPDATE_MODE QUEUE_UPDATE_MODE */
116#define UPDATE_INFO_MODE 0
117#define UPDATE_TIME 60
118#define UPDATE_TIME2 0
119
120static int capacity_mode = CAPACITY_UNIT;
121static int update_mode = UPDATE_MODE;
122static int update_info_mode = UPDATE_INFO_MODE;
123static int update_time = UPDATE_TIME;
124static int update_time2 = UPDATE_TIME2;
125
Lebedev, Vladimir P3cd5b872006-09-05 19:59:22 +0400126module_param(capacity_mode, int, 0);
127module_param(update_mode, int, 0);
128module_param(update_info_mode, int, 0);
129module_param(update_time, int, 0);
130module_param(update_time2, int, 0);
Rich Townsend3f86b832006-07-01 11:36:54 -0400131
132static int acpi_sbs_add(struct acpi_device *device);
133static int acpi_sbs_remove(struct acpi_device *device, int type);
Rich Townsend3f86b832006-07-01 11:36:54 -0400134static void acpi_sbs_update_queue(void *data);
135
136static struct acpi_driver acpi_sbs_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500137 .name = "sbs",
Rich Townsend3f86b832006-07-01 11:36:54 -0400138 .class = ACPI_SBS_CLASS,
139 .ids = ACPI_SBS_HID,
140 .ops = {
141 .add = acpi_sbs_add,
142 .remove = acpi_sbs_remove,
143 },
144};
145
146struct acpi_battery_info {
147 int capacity_mode;
148 s16 full_charge_capacity;
149 s16 design_capacity;
150 s16 design_voltage;
151 int vscale;
152 int ipscale;
153 s16 serial_number;
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300154 char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
155 char device_name[ACPI_SBS_BLOCK_MAX + 3];
156 char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
Rich Townsend3f86b832006-07-01 11:36:54 -0400157};
158
159struct acpi_battery_state {
160 s16 voltage;
161 s16 amperage;
162 s16 remaining_capacity;
163 s16 average_time_to_empty;
164 s16 average_time_to_full;
165 s16 battery_status;
166};
167
168struct acpi_battery_alarm {
169 s16 remaining_capacity;
170};
171
172struct acpi_battery {
173 int alive;
174 int battery_present;
175 int id;
176 int init_state;
177 struct acpi_sbs *sbs;
178 struct acpi_battery_info info;
179 struct acpi_battery_state state;
180 struct acpi_battery_alarm alarm;
181 struct proc_dir_entry *battery_entry;
182};
183
184struct acpi_sbs {
185 acpi_handle handle;
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300186 int base;
Rich Townsend3f86b832006-07-01 11:36:54 -0400187 struct acpi_device *device;
Rich Townsend3f86b832006-07-01 11:36:54 -0400188 int sbsm_present;
189 int sbsm_batteries_supported;
190 int ac_present;
191 struct proc_dir_entry *ac_entry;
192 struct acpi_battery battery[MAX_SBS_BAT];
193 int update_info_mode;
194 int zombie;
195 int update_time;
196 int update_time2;
197 struct timer_list update_timer;
198};
199
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300200union sbs_rw_data {
201 u16 word;
202 u8 block[ACPI_SBS_BLOCK_MAX + 2];
203};
204
205static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
206 char read_write, u8 command, int size,
207 union sbs_rw_data *data);
Rich Townsend3f86b832006-07-01 11:36:54 -0400208static void acpi_update_delay(struct acpi_sbs *sbs);
209static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type);
210
211/* --------------------------------------------------------------------------
212 SMBus Communication
213 -------------------------------------------------------------------------- */
214
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300215static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
Rich Townsend3f86b832006-07-01 11:36:54 -0400216{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300217 u8 val;
218 int err;
Rich Townsend3f86b832006-07-01 11:36:54 -0400219
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300220 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 Townsend3f86b832006-07-01 11:36:54 -0400227
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300228static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
229{
230 int err;
Rich Townsend3f86b832006-07-01 11:36:54 -0400231
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300232 err = ec_write(sbs->base + address, data);
233 return (err);
234}
Rich Townsend3f86b832006-07-01 11:36:54 -0400235
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300236static int
237acpi_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 Townsend3f86b832006-07-01 11:36:54 -0400260 break;
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300261 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 Townsend3f86b832006-07-01 11:36:54 -0400271 break;
272 default:
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300273 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
Vladimir Lebedev68451182007-03-19 17:45:50 +0300274 "unsupported transaction %d", size));
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300275 return (-1);
Rich Townsend3f86b832006-07-01 11:36:54 -0400276 }
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300277
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 Lebedev68451182007-03-19 17:45:50 +0300294 "transaction %d error", size));
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300295 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 Lebedev68451182007-03-19 17:45:50 +0300321 "unsupported transaction %d", size));
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300322 return (-1);
323 }
324
325 return (0);
Rich Townsend3f86b832006-07-01 11:36:54 -0400326}
327
328static int
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300329acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
Rich Townsend3f86b832006-07-01 11:36:54 -0400330{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300331 union sbs_rw_data data;
Rich Townsend3f86b832006-07-01 11:36:54 -0400332 int result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400333
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300334 result = acpi_ec_sbs_access(sbs, addr,
335 ACPI_SBS_SMBUS_READ, func,
336 ACPI_SBS_WORD_DATA, &data);
337 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300338 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
339 "acpi_ec_sbs_access() failed"));
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300340 } else {
341 *word = data.word;
Rich Townsend3f86b832006-07-01 11:36:54 -0400342 }
343
Len Brown635227e2006-07-01 16:48:23 -0400344 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400345}
346
347static int
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300348acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
Rich Townsend3f86b832006-07-01 11:36:54 -0400349{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300350 union sbs_rw_data data;
Rich Townsend3f86b832006-07-01 11:36:54 -0400351 int result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400352
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300353 result = acpi_ec_sbs_access(sbs, addr,
354 ACPI_SBS_SMBUS_READ, func,
355 ACPI_SBS_BLOCK_DATA, &data);
356 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300357 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
358 "acpi_ec_sbs_access() failed"));
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300359 } else {
360 strncpy(str, (const char *)data.block + 1, data.block[0]);
361 str[data.block[0]] = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400362 }
363
Len Brown635227e2006-07-01 16:48:23 -0400364 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400365}
366
367static int
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300368acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
Rich Townsend3f86b832006-07-01 11:36:54 -0400369{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300370 union sbs_rw_data data;
Rich Townsend3f86b832006-07-01 11:36:54 -0400371 int result = 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400372
373 data.word = word;
374
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300375 result = acpi_ec_sbs_access(sbs, addr,
376 ACPI_SBS_SMBUS_WRITE, func,
377 ACPI_SBS_WORD_DATA, &data);
378 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300379 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
380 "acpi_ec_sbs_access() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400381 }
382
Len Brown635227e2006-07-01 16:48:23 -0400383 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400384}
385
386/* --------------------------------------------------------------------------
387 Smart Battery System Management
388 -------------------------------------------------------------------------- */
389
390/* Smart Battery */
391
392static int acpi_sbs_generate_event(struct acpi_device *device,
393 int event, int state, char *bid, char *class)
394{
395 char bid_saved[5];
396 char class_saved[20];
397 int result = 0;
398
Rich Townsend3f86b832006-07-01 11:36:54 -0400399 strcpy(bid_saved, acpi_device_bid(device));
400 strcpy(class_saved, acpi_device_class(device));
401
402 strcpy(acpi_device_bid(device), bid);
403 strcpy(acpi_device_class(device), class);
404
405 result = acpi_bus_generate_event(device, event, state);
406
407 strcpy(acpi_device_bid(device), bid_saved);
408 strcpy(acpi_device_class(device), class_saved);
409
Len Brown635227e2006-07-01 16:48:23 -0400410 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400411}
412
413static int acpi_battery_get_present(struct acpi_battery *battery)
414{
415 s16 state;
416 int result = 0;
417 int is_present = 0;
418
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300419 result = acpi_sbs_read_word(battery->sbs,
420 ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
Rich Townsend3f86b832006-07-01 11:36:54 -0400421 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300422 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
423 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400424 }
425 if (!result) {
426 is_present = (state & 0x000f) & (1 << battery->id);
427 }
428 battery->battery_present = is_present;
429
Len Brown635227e2006-07-01 16:48:23 -0400430 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400431}
432
433static int acpi_battery_is_present(struct acpi_battery *battery)
434{
435 return (battery->battery_present);
436}
437
438static int acpi_ac_is_present(struct acpi_sbs *sbs)
439{
440 return (sbs->ac_present);
441}
442
443static int acpi_battery_select(struct acpi_battery *battery)
444{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300445 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400446 int result = 0;
447 s16 state;
448 int foo;
449
Rich Townsend3f86b832006-07-01 11:36:54 -0400450 if (battery->sbs->sbsm_present) {
451
452 /* Take special care not to knobble other nibbles of
453 * state (aka selector_state), since
454 * it causes charging to halt on SBSELs */
455
456 result =
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300457 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
Rich Townsend3f86b832006-07-01 11:36:54 -0400458 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300459 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
460 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400461 goto end;
462 }
463
464 foo = (state & 0x0fff) | (1 << (battery->id + 12));
465 result =
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300466 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
Rich Townsend3f86b832006-07-01 11:36:54 -0400467 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300468 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
469 "acpi_sbs_write_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400470 goto end;
471 }
472 }
473
474 end:
Len Brown635227e2006-07-01 16:48:23 -0400475 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400476}
477
478static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
479{
Rich Townsend3f86b832006-07-01 11:36:54 -0400480 int result = 0;
481 s16 battery_system_info;
482
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300483 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
484 &battery_system_info);
Rich Townsend3f86b832006-07-01 11:36:54 -0400485 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300486 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
487 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400488 goto end;
489 }
490
491 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
492
493 end:
494
Len Brown635227e2006-07-01 16:48:23 -0400495 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400496}
497
498static int acpi_battery_get_info(struct acpi_battery *battery)
499{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300500 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400501 int result = 0;
502 s16 battery_mode;
503 s16 specification_info;
504
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300505 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
506 &battery_mode);
Rich Townsend3f86b832006-07-01 11:36:54 -0400507 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300508 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
509 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400510 goto end;
511 }
512 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
513
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300514 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
515 &battery->info.full_charge_capacity);
Rich Townsend3f86b832006-07-01 11:36:54 -0400516 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300517 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
518 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400519 goto end;
520 }
521
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300522 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
523 &battery->info.design_capacity);
Rich Townsend3f86b832006-07-01 11:36:54 -0400524
525 if (result) {
526 goto end;
527 }
528
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300529 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
530 &battery->info.design_voltage);
Rich Townsend3f86b832006-07-01 11:36:54 -0400531 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300532 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
533 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400534 goto end;
535 }
536
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300537 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
538 &specification_info);
Rich Townsend3f86b832006-07-01 11:36:54 -0400539 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300540 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
541 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400542 goto end;
543 }
544
545 switch ((specification_info & 0x0f00) >> 8) {
546 case 1:
547 battery->info.vscale = 10;
548 break;
549 case 2:
550 battery->info.vscale = 100;
551 break;
552 case 3:
553 battery->info.vscale = 1000;
554 break;
555 default:
556 battery->info.vscale = 1;
557 }
558
559 switch ((specification_info & 0xf000) >> 12) {
560 case 1:
561 battery->info.ipscale = 10;
562 break;
563 case 2:
564 battery->info.ipscale = 100;
565 break;
566 case 3:
567 battery->info.ipscale = 1000;
568 break;
569 default:
570 battery->info.ipscale = 1;
571 }
572
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300573 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
574 &battery->info.serial_number);
Rich Townsend3f86b832006-07-01 11:36:54 -0400575 if (result) {
576 goto end;
577 }
578
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300579 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
580 battery->info.manufacturer_name);
Rich Townsend3f86b832006-07-01 11:36:54 -0400581 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300582 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
583 "acpi_sbs_read_str() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400584 goto end;
585 }
586
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300587 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
588 battery->info.device_name);
Rich Townsend3f86b832006-07-01 11:36:54 -0400589 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300590 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
591 "acpi_sbs_read_str() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400592 goto end;
593 }
594
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300595 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
596 battery->info.device_chemistry);
Rich Townsend3f86b832006-07-01 11:36:54 -0400597 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300598 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
599 "acpi_sbs_read_str() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400600 goto end;
601 }
602
603 end:
Len Brown635227e2006-07-01 16:48:23 -0400604 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400605}
606
607static void acpi_update_delay(struct acpi_sbs *sbs)
608{
Rich Townsend3f86b832006-07-01 11:36:54 -0400609 if (sbs->zombie) {
610 return;
611 }
612 if (sbs->update_time2 > 0) {
613 msleep(sbs->update_time2 * 1000);
614 }
615}
616
617static int acpi_battery_get_state(struct acpi_battery *battery)
618{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300619 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400620 int result = 0;
621
Rich Townsend3f86b832006-07-01 11:36:54 -0400622 acpi_update_delay(battery->sbs);
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300623 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
624 &battery->state.voltage);
Rich Townsend3f86b832006-07-01 11:36:54 -0400625 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300626 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
627 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400628 goto end;
629 }
630
631 acpi_update_delay(battery->sbs);
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300632 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
633 &battery->state.amperage);
Rich Townsend3f86b832006-07-01 11:36:54 -0400634 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300635 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
636 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400637 goto end;
638 }
639
640 acpi_update_delay(battery->sbs);
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300641 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
642 &battery->state.remaining_capacity);
Rich Townsend3f86b832006-07-01 11:36:54 -0400643 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300644 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
645 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400646 goto end;
647 }
648
649 acpi_update_delay(battery->sbs);
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300650 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x12,
651 &battery->state.average_time_to_empty);
Rich Townsend3f86b832006-07-01 11:36:54 -0400652 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300653 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
654 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400655 goto end;
656 }
657
658 acpi_update_delay(battery->sbs);
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300659 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x13,
660 &battery->state.average_time_to_full);
Rich Townsend3f86b832006-07-01 11:36:54 -0400661 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300662 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
663 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400664 goto end;
665 }
666
667 acpi_update_delay(battery->sbs);
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300668 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
669 &battery->state.battery_status);
Rich Townsend3f86b832006-07-01 11:36:54 -0400670 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300671 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
672 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400673 goto end;
674 }
675
676 acpi_update_delay(battery->sbs);
677
678 end:
Len Brown635227e2006-07-01 16:48:23 -0400679 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400680}
681
682static int acpi_battery_get_alarm(struct acpi_battery *battery)
683{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300684 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400685 int result = 0;
686
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300687 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
688 &battery->alarm.remaining_capacity);
Rich Townsend3f86b832006-07-01 11:36:54 -0400689 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300690 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
691 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400692 goto end;
693 }
694
695 acpi_update_delay(battery->sbs);
696
697 end:
698
Len Brown635227e2006-07-01 16:48:23 -0400699 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400700}
701
702static int acpi_battery_set_alarm(struct acpi_battery *battery,
703 unsigned long alarm)
704{
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300705 struct acpi_sbs *sbs = battery->sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -0400706 int result = 0;
707 s16 battery_mode;
708 int foo;
709
Rich Townsend3f86b832006-07-01 11:36:54 -0400710 result = acpi_battery_select(battery);
711 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300712 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
713 "acpi_battery_select() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400714 goto end;
715 }
716
717 /* If necessary, enable the alarm */
718
719 if (alarm > 0) {
720 result =
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300721 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
722 &battery_mode);
Rich Townsend3f86b832006-07-01 11:36:54 -0400723 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300724 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
725 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400726 goto end;
727 }
728
729 result =
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300730 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
731 battery_mode & 0xbfff);
Rich Townsend3f86b832006-07-01 11:36:54 -0400732 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300733 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
734 "acpi_sbs_write_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400735 goto end;
736 }
737 }
738
739 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300740 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
Rich Townsend3f86b832006-07-01 11:36:54 -0400741 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300742 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
743 "acpi_sbs_write_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400744 goto end;
745 }
746
747 end:
748
Len Brown635227e2006-07-01 16:48:23 -0400749 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400750}
751
752static int acpi_battery_set_mode(struct acpi_battery *battery)
753{
754 int result = 0;
755 s16 battery_mode;
756
Rich Townsend3f86b832006-07-01 11:36:54 -0400757 if (capacity_mode == DEF_CAPACITY_UNIT) {
758 goto end;
759 }
760
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300761 result = acpi_sbs_read_word(battery->sbs,
762 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
Rich Townsend3f86b832006-07-01 11:36:54 -0400763 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300764 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
765 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400766 goto end;
767 }
768
769 if (capacity_mode == MAH_CAPACITY_UNIT) {
770 battery_mode &= 0x7fff;
771 } else {
772 battery_mode |= 0x8000;
773 }
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300774 result = acpi_sbs_write_word(battery->sbs,
775 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
Rich Townsend3f86b832006-07-01 11:36:54 -0400776 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300777 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
778 "acpi_sbs_write_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400779 goto end;
780 }
781
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300782 result = acpi_sbs_read_word(battery->sbs,
783 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
Rich Townsend3f86b832006-07-01 11:36:54 -0400784 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300785 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
786 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400787 goto end;
788 }
789
790 end:
Len Brown635227e2006-07-01 16:48:23 -0400791 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400792}
793
794static int acpi_battery_init(struct acpi_battery *battery)
795{
796 int result = 0;
797
Rich Townsend3f86b832006-07-01 11:36:54 -0400798 result = acpi_battery_select(battery);
799 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300800 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
801 "acpi_battery_init() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400802 goto end;
803 }
804
805 result = acpi_battery_set_mode(battery);
806 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300807 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
808 "acpi_battery_set_mode() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400809 goto end;
810 }
811
812 result = acpi_battery_get_info(battery);
813 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300814 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
815 "acpi_battery_get_info() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400816 goto end;
817 }
818
819 result = acpi_battery_get_state(battery);
820 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300821 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
822 "acpi_battery_get_state() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400823 goto end;
824 }
825
826 result = acpi_battery_get_alarm(battery);
827 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300828 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
829 "acpi_battery_get_alarm() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400830 goto end;
831 }
832
833 end:
Len Brown635227e2006-07-01 16:48:23 -0400834 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400835}
836
837static int acpi_ac_get_present(struct acpi_sbs *sbs)
838{
Rich Townsend3f86b832006-07-01 11:36:54 -0400839 int result = 0;
840 s16 charger_status;
841
Vladimir Lebedev6d157022007-03-19 17:45:50 +0300842 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
843 &charger_status);
Rich Townsend3f86b832006-07-01 11:36:54 -0400844
845 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300846 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
847 "acpi_sbs_read_word() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400848 goto end;
849 }
850
851 sbs->ac_present = (charger_status & 0x8000) >> 15;
852
853 end:
854
Len Brown635227e2006-07-01 16:48:23 -0400855 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400856}
857
858/* --------------------------------------------------------------------------
859 FS Interface (/proc/acpi)
860 -------------------------------------------------------------------------- */
861
862/* Generic Routines */
863
864static int
865acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
866 struct proc_dir_entry *parent_dir,
867 char *dir_name,
868 struct file_operations *info_fops,
869 struct file_operations *state_fops,
870 struct file_operations *alarm_fops, void *data)
871{
872 struct proc_dir_entry *entry = NULL;
873
Rich Townsend3f86b832006-07-01 11:36:54 -0400874 if (!*dir) {
875 *dir = proc_mkdir(dir_name, parent_dir);
876 if (!*dir) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300877 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
878 "proc_mkdir() failed"));
Len Brown635227e2006-07-01 16:48:23 -0400879 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400880 }
881 (*dir)->owner = THIS_MODULE;
882 }
883
884 /* 'info' [R] */
885 if (info_fops) {
886 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
887 if (!entry) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300888 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
889 "create_proc_entry() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400890 } else {
891 entry->proc_fops = info_fops;
892 entry->data = data;
893 entry->owner = THIS_MODULE;
894 }
895 }
896
897 /* 'state' [R] */
898 if (state_fops) {
899 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
900 if (!entry) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300901 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
902 "create_proc_entry() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400903 } else {
904 entry->proc_fops = state_fops;
905 entry->data = data;
906 entry->owner = THIS_MODULE;
907 }
908 }
909
910 /* 'alarm' [R/W] */
911 if (alarm_fops) {
912 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
913 if (!entry) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300914 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
915 "create_proc_entry() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400916 } else {
917 entry->proc_fops = alarm_fops;
918 entry->data = data;
919 entry->owner = THIS_MODULE;
920 }
921 }
922
Len Brown635227e2006-07-01 16:48:23 -0400923 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400924}
925
926static void
927acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
928 struct proc_dir_entry *parent_dir)
929{
Rich Townsend3f86b832006-07-01 11:36:54 -0400930
931 if (*dir) {
932 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
933 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
934 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
935 remove_proc_entry((*dir)->name, parent_dir);
936 *dir = NULL;
937 }
938
939}
940
941/* Smart Battery Interface */
942
943static struct proc_dir_entry *acpi_battery_dir = NULL;
944
945static int acpi_battery_read_info(struct seq_file *seq, void *offset)
946{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200947 struct acpi_battery *battery = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -0400948 int cscale;
949 int result = 0;
950
Rich Townsend3f86b832006-07-01 11:36:54 -0400951 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -0400952 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400953 }
954
955 down(&sbs_sem);
956
957 if (update_mode == REQUEST_UPDATE_MODE) {
958 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_INFO);
959 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +0300960 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
961 "acpi_sbs_update_run() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -0400962 }
963 }
964
965 if (acpi_battery_is_present(battery)) {
966 seq_printf(seq, "present: yes\n");
967 } else {
968 seq_printf(seq, "present: no\n");
969 goto end;
970 }
971
972 if (battery->info.capacity_mode) {
973 cscale = battery->info.vscale * battery->info.ipscale;
974 } else {
975 cscale = battery->info.ipscale;
976 }
977 seq_printf(seq, "design capacity: %i%s",
978 battery->info.design_capacity * cscale,
979 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
980
981 seq_printf(seq, "last full capacity: %i%s",
982 battery->info.full_charge_capacity * cscale,
983 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
984
985 seq_printf(seq, "battery technology: rechargeable\n");
986
987 seq_printf(seq, "design voltage: %i mV\n",
988 battery->info.design_voltage * battery->info.vscale);
989
990 seq_printf(seq, "design capacity warning: unknown\n");
991 seq_printf(seq, "design capacity low: unknown\n");
992 seq_printf(seq, "capacity granularity 1: unknown\n");
993 seq_printf(seq, "capacity granularity 2: unknown\n");
994
995 seq_printf(seq, "model number: %s\n",
996 battery->info.device_name);
997
998 seq_printf(seq, "serial number: %i\n",
999 battery->info.serial_number);
1000
1001 seq_printf(seq, "battery type: %s\n",
1002 battery->info.device_chemistry);
1003
1004 seq_printf(seq, "OEM info: %s\n",
1005 battery->info.manufacturer_name);
1006
1007 end:
1008
1009 up(&sbs_sem);
1010
Len Brown635227e2006-07-01 16:48:23 -04001011 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001012}
1013
1014static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1015{
1016 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1017}
1018
1019static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1020{
1021 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1022 int result = 0;
1023 int cscale;
1024 int foo;
1025
Rich Townsend3f86b832006-07-01 11:36:54 -04001026 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001027 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001028 }
1029
1030 down(&sbs_sem);
1031
1032 if (update_mode == REQUEST_UPDATE_MODE) {
1033 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_STATE);
1034 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001035 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1036 "acpi_sbs_update_run() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001037 }
1038 }
1039
1040 if (acpi_battery_is_present(battery)) {
1041 seq_printf(seq, "present: yes\n");
1042 } else {
1043 seq_printf(seq, "present: no\n");
1044 goto end;
1045 }
1046
1047 if (battery->info.capacity_mode) {
1048 cscale = battery->info.vscale * battery->info.ipscale;
1049 } else {
1050 cscale = battery->info.ipscale;
1051 }
1052
1053 if (battery->state.battery_status & 0x0010) {
1054 seq_printf(seq, "capacity state: critical\n");
1055 } else {
1056 seq_printf(seq, "capacity state: ok\n");
1057 }
Vladimir Lebedeve6d0f562007-02-10 01:51:13 -05001058
1059 foo = (s16) battery->state.amperage * battery->info.ipscale;
1060 if (battery->info.capacity_mode) {
1061 foo = foo * battery->info.design_voltage / 1000;
1062 }
Rich Townsend3f86b832006-07-01 11:36:54 -04001063 if (battery->state.amperage < 0) {
1064 seq_printf(seq, "charging state: discharging\n");
Vladimir Lebedeve6d0f562007-02-10 01:51:13 -05001065 seq_printf(seq, "present rate: %d %s\n",
1066 -foo, battery->info.capacity_mode ? "mW" : "mA");
Rich Townsend3f86b832006-07-01 11:36:54 -04001067 } else if (battery->state.amperage > 0) {
1068 seq_printf(seq, "charging state: charging\n");
Vladimir Lebedeve6d0f562007-02-10 01:51:13 -05001069 seq_printf(seq, "present rate: %d %s\n",
1070 foo, battery->info.capacity_mode ? "mW" : "mA");
Rich Townsend3f86b832006-07-01 11:36:54 -04001071 } else {
1072 seq_printf(seq, "charging state: charged\n");
1073 seq_printf(seq, "present rate: 0 %s\n",
1074 battery->info.capacity_mode ? "mW" : "mA");
1075 }
1076
1077 seq_printf(seq, "remaining capacity: %i%s",
1078 battery->state.remaining_capacity * cscale,
1079 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1080
1081 seq_printf(seq, "present voltage: %i mV\n",
1082 battery->state.voltage * battery->info.vscale);
1083
1084 end:
1085
1086 up(&sbs_sem);
1087
Len Brown635227e2006-07-01 16:48:23 -04001088 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001089}
1090
1091static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1092{
1093 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1094}
1095
1096static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1097{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001098 struct acpi_battery *battery = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -04001099 int result = 0;
1100 int cscale;
1101
Rich Townsend3f86b832006-07-01 11:36:54 -04001102 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001103 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001104 }
1105
1106 down(&sbs_sem);
1107
1108 if (update_mode == REQUEST_UPDATE_MODE) {
1109 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM);
1110 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001111 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1112 "acpi_sbs_update_run() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001113 }
1114 }
1115
1116 if (!acpi_battery_is_present(battery)) {
1117 seq_printf(seq, "present: no\n");
1118 goto end;
1119 }
1120
1121 if (battery->info.capacity_mode) {
1122 cscale = battery->info.vscale * battery->info.ipscale;
1123 } else {
1124 cscale = battery->info.ipscale;
1125 }
1126
1127 seq_printf(seq, "alarm: ");
1128 if (battery->alarm.remaining_capacity) {
1129 seq_printf(seq, "%i%s",
1130 battery->alarm.remaining_capacity * cscale,
1131 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1132 } else {
1133 seq_printf(seq, "disabled\n");
1134 }
1135
1136 end:
1137
1138 up(&sbs_sem);
1139
Len Brown635227e2006-07-01 16:48:23 -04001140 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001141}
1142
1143static ssize_t
1144acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1145 size_t count, loff_t * ppos)
1146{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001147 struct seq_file *seq = file->private_data;
1148 struct acpi_battery *battery = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -04001149 char alarm_string[12] = { '\0' };
1150 int result, old_alarm, new_alarm;
1151
Rich Townsend3f86b832006-07-01 11:36:54 -04001152 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001153 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001154 }
1155
1156 down(&sbs_sem);
1157
1158 if (!acpi_battery_is_present(battery)) {
1159 result = -ENODEV;
1160 goto end;
1161 }
1162
1163 if (count > sizeof(alarm_string) - 1) {
1164 result = -EINVAL;
1165 goto end;
1166 }
1167
1168 if (copy_from_user(alarm_string, buffer, count)) {
1169 result = -EFAULT;
1170 goto end;
1171 }
1172
1173 alarm_string[count] = 0;
1174
1175 old_alarm = battery->alarm.remaining_capacity;
1176 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1177
1178 result = acpi_battery_set_alarm(battery, new_alarm);
1179 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001180 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1181 "acpi_battery_set_alarm() failed"));
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001182 acpi_battery_set_alarm(battery, old_alarm);
Rich Townsend3f86b832006-07-01 11:36:54 -04001183 goto end;
1184 }
1185 result = acpi_battery_get_alarm(battery);
1186 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001187 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1188 "acpi_battery_get_alarm() failed"));
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001189 acpi_battery_set_alarm(battery, old_alarm);
Rich Townsend3f86b832006-07-01 11:36:54 -04001190 goto end;
1191 }
1192
1193 end:
1194 up(&sbs_sem);
1195
1196 if (result) {
Len Brown635227e2006-07-01 16:48:23 -04001197 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001198 } else {
Len Brown635227e2006-07-01 16:48:23 -04001199 return count;
Rich Townsend3f86b832006-07-01 11:36:54 -04001200 }
1201}
1202
1203static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1204{
1205 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1206}
1207
1208static struct file_operations acpi_battery_info_fops = {
1209 .open = acpi_battery_info_open_fs,
1210 .read = seq_read,
1211 .llseek = seq_lseek,
1212 .release = single_release,
1213 .owner = THIS_MODULE,
1214};
1215
1216static struct file_operations acpi_battery_state_fops = {
1217 .open = acpi_battery_state_open_fs,
1218 .read = seq_read,
1219 .llseek = seq_lseek,
1220 .release = single_release,
1221 .owner = THIS_MODULE,
1222};
1223
1224static struct file_operations acpi_battery_alarm_fops = {
1225 .open = acpi_battery_alarm_open_fs,
1226 .read = seq_read,
1227 .write = acpi_battery_write_alarm,
1228 .llseek = seq_lseek,
1229 .release = single_release,
1230 .owner = THIS_MODULE,
1231};
1232
1233/* Legacy AC Adapter Interface */
1234
1235static struct proc_dir_entry *acpi_ac_dir = NULL;
1236
1237static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1238{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001239 struct acpi_sbs *sbs = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -04001240 int result;
1241
Rich Townsend3f86b832006-07-01 11:36:54 -04001242 if (sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001243 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001244 }
1245
1246 down(&sbs_sem);
1247
1248 if (update_mode == REQUEST_UPDATE_MODE) {
1249 result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE);
1250 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001251 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1252 "acpi_sbs_update_run() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001253 }
1254 }
1255
1256 seq_printf(seq, "state: %s\n",
1257 sbs->ac_present ? "on-line" : "off-line");
1258
1259 up(&sbs_sem);
1260
Len Brown635227e2006-07-01 16:48:23 -04001261 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -04001262}
1263
1264static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1265{
1266 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1267}
1268
1269static struct file_operations acpi_ac_state_fops = {
1270 .open = acpi_ac_state_open_fs,
1271 .read = seq_read,
1272 .llseek = seq_lseek,
1273 .release = single_release,
1274 .owner = THIS_MODULE,
1275};
1276
1277/* --------------------------------------------------------------------------
1278 Driver Interface
1279 -------------------------------------------------------------------------- */
1280
1281/* Smart Battery */
1282
1283static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1284{
1285 int is_present;
1286 int result;
1287 char dir_name[32];
1288 struct acpi_battery *battery;
1289
Rich Townsend3f86b832006-07-01 11:36:54 -04001290 battery = &sbs->battery[id];
1291
1292 battery->alive = 0;
1293
1294 battery->init_state = 0;
1295 battery->id = id;
1296 battery->sbs = sbs;
1297
1298 result = acpi_battery_select(battery);
1299 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001300 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1301 "acpi_battery_select() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001302 goto end;
1303 }
1304
1305 result = acpi_battery_get_present(battery);
1306 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001307 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1308 "acpi_battery_get_present() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001309 goto end;
1310 }
1311
1312 is_present = acpi_battery_is_present(battery);
1313
1314 if (is_present) {
1315 result = acpi_battery_init(battery);
1316 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001317 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1318 "acpi_battery_init() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001319 goto end;
1320 }
1321 battery->init_state = 1;
1322 }
1323
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001324 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
Rich Townsend3f86b832006-07-01 11:36:54 -04001325
1326 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1327 acpi_battery_dir,
1328 dir_name,
1329 &acpi_battery_info_fops,
1330 &acpi_battery_state_fops,
1331 &acpi_battery_alarm_fops, battery);
1332 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001333 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1334 "acpi_sbs_generic_add_fs() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001335 goto end;
1336 }
1337 battery->alive = 1;
1338
1339 end:
Len Brown635227e2006-07-01 16:48:23 -04001340 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001341}
1342
1343static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1344{
Rich Townsend3f86b832006-07-01 11:36:54 -04001345
1346 if (sbs->battery[id].battery_entry) {
1347 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1348 acpi_battery_dir);
1349 }
1350}
1351
1352static int acpi_ac_add(struct acpi_sbs *sbs)
1353{
1354 int result;
1355
Rich Townsend3f86b832006-07-01 11:36:54 -04001356 result = acpi_ac_get_present(sbs);
1357 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001358 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1359 "acpi_ac_get_present() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001360 goto end;
1361 }
1362
1363 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1364 acpi_ac_dir,
1365 ACPI_AC_DIR_NAME,
1366 NULL, &acpi_ac_state_fops, NULL, sbs);
1367 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001368 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1369 "acpi_sbs_generic_add_fs() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001370 goto end;
1371 }
1372
1373 end:
1374
Len Brown635227e2006-07-01 16:48:23 -04001375 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001376}
1377
1378static void acpi_ac_remove(struct acpi_sbs *sbs)
1379{
Rich Townsend3f86b832006-07-01 11:36:54 -04001380
1381 if (sbs->ac_entry) {
1382 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1383 }
1384}
1385
1386static void acpi_sbs_update_queue_run(unsigned long data)
1387{
Rich Townsend3f86b832006-07-01 11:36:54 -04001388 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data);
1389}
1390
1391static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type)
1392{
1393 struct acpi_battery *battery;
1394 int result = 0;
1395 int old_ac_present;
1396 int old_battery_present;
1397 int new_ac_present;
1398 int new_battery_present;
1399 int id;
1400 char dir_name[32];
1401 int do_battery_init, do_ac_init;
1402 s16 old_remaining_capacity;
1403
Rich Townsend3f86b832006-07-01 11:36:54 -04001404 if (sbs->zombie) {
1405 goto end;
1406 }
1407
1408 old_ac_present = acpi_ac_is_present(sbs);
1409
1410 result = acpi_ac_get_present(sbs);
1411 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001412 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1413 "acpi_ac_get_present() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001414 }
1415
1416 new_ac_present = acpi_ac_is_present(sbs);
1417
1418 do_ac_init = (old_ac_present != new_ac_present);
1419
1420 if (data_type == DATA_TYPE_AC_STATE) {
1421 goto end;
1422 }
1423
1424 for (id = 0; id < MAX_SBS_BAT; id++) {
1425 battery = &sbs->battery[id];
1426 if (battery->alive == 0) {
1427 continue;
1428 }
1429
1430 old_remaining_capacity = battery->state.remaining_capacity;
1431
1432 old_battery_present = acpi_battery_is_present(battery);
1433
1434 result = acpi_battery_select(battery);
1435 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001436 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1437 "acpi_battery_select() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001438 }
1439 if (sbs->zombie) {
1440 goto end;
1441 }
1442
1443 result = acpi_battery_get_present(battery);
1444 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001445 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1446 "acpi_battery_get_present() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001447 }
1448 if (sbs->zombie) {
1449 goto end;
1450 }
1451
1452 new_battery_present = acpi_battery_is_present(battery);
1453
1454 do_battery_init = ((old_battery_present != new_battery_present)
1455 && new_battery_present);
1456
1457 if (sbs->zombie) {
1458 goto end;
1459 }
1460 if (do_ac_init || do_battery_init ||
1461 update_info_mode || sbs->update_info_mode) {
1462 if (sbs->update_info_mode) {
1463 sbs->update_info_mode = 0;
1464 } else {
1465 sbs->update_info_mode = 1;
1466 }
1467 result = acpi_battery_init(battery);
1468 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001469 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1470 "acpi_battery_init() "
1471 "failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001472 }
1473 }
1474 if (data_type == DATA_TYPE_INFO) {
1475 continue;
1476 }
1477
1478 if (sbs->zombie) {
1479 goto end;
1480 }
1481 if (new_battery_present) {
1482 result = acpi_battery_get_alarm(battery);
1483 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001484 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1485 "acpi_battery_get_alarm() "
1486 "failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001487 }
1488 if (data_type == DATA_TYPE_ALARM) {
1489 continue;
1490 }
1491
1492 result = acpi_battery_get_state(battery);
1493 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001494 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1495 "acpi_battery_get_state() "
1496 "failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001497 }
1498 }
1499 if (sbs->zombie) {
1500 goto end;
1501 }
1502 if (data_type != DATA_TYPE_COMMON) {
1503 continue;
1504 }
1505
1506 if (old_battery_present != new_battery_present) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001507 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
Rich Townsend3f86b832006-07-01 11:36:54 -04001508 result = acpi_sbs_generate_event(sbs->device,
1509 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1510 new_battery_present,
1511 dir_name,
1512 ACPI_BATTERY_CLASS);
1513 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001514 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1515 "acpi_sbs_generate_event() "
1516 "failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001517 }
1518 }
1519 if (old_remaining_capacity != battery->state.remaining_capacity) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001520 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
Rich Townsend3f86b832006-07-01 11:36:54 -04001521 result = acpi_sbs_generate_event(sbs->device,
1522 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1523 new_battery_present,
1524 dir_name,
1525 ACPI_BATTERY_CLASS);
1526 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001527 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1528 "acpi_sbs_generate_event() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001529 }
1530 }
1531
1532 }
1533 if (sbs->zombie) {
1534 goto end;
1535 }
1536 if (data_type != DATA_TYPE_COMMON) {
1537 goto end;
1538 }
1539
1540 if (old_ac_present != new_ac_present) {
1541 result = acpi_sbs_generate_event(sbs->device,
1542 ACPI_SBS_AC_NOTIFY_STATUS,
1543 new_ac_present,
1544 ACPI_AC_DIR_NAME,
1545 ACPI_AC_CLASS);
1546 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001547 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1548 "acpi_sbs_generate_event() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001549 }
1550 }
1551
1552 end:
Len Brown635227e2006-07-01 16:48:23 -04001553 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001554}
1555
1556static void acpi_sbs_update_queue(void *data)
1557{
1558 struct acpi_sbs *sbs = data;
1559 unsigned long delay = -1;
1560 int result;
1561
Rich Townsend3f86b832006-07-01 11:36:54 -04001562 if (sbs->zombie) {
1563 goto end;
1564 }
1565
1566 result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON);
1567 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001568 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1569 "acpi_sbs_update_run() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001570 }
1571
1572 if (sbs->zombie) {
1573 goto end;
1574 }
1575
1576 if (update_mode == REQUEST_UPDATE_MODE) {
1577 goto end;
1578 }
1579
1580 delay = jiffies + HZ * update_time;
1581 sbs->update_timer.data = (unsigned long)data;
1582 sbs->update_timer.function = acpi_sbs_update_queue_run;
1583 sbs->update_timer.expires = delay;
1584 add_timer(&sbs->update_timer);
1585 end:
1586 ;
1587}
1588
1589static int acpi_sbs_add(struct acpi_device *device)
1590{
1591 struct acpi_sbs *sbs = NULL;
Vladimir Lebedev6d157022007-03-19 17:45:50 +03001592 int result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001593 unsigned long sbs_obj;
Vladimir Lebedev6d157022007-03-19 17:45:50 +03001594 int id;
Rich Townsend3f86b832006-07-01 11:36:54 -04001595 acpi_status status = AE_OK;
Vladimir Lebedev6d157022007-03-19 17:45:50 +03001596 unsigned long val;
1597
1598 status =
1599 acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val);
1600 if (ACPI_FAILURE(status)) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001601 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
Vladimir Lebedev6d157022007-03-19 17:45:50 +03001602 return -EIO;
1603 }
Rich Townsend3f86b832006-07-01 11:36:54 -04001604
Burman Yan36bcbec2006-12-19 12:56:11 -08001605 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
Rich Townsend3f86b832006-07-01 11:36:54 -04001606 if (!sbs) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001607 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kmalloc() failed"));
Len Brown635227e2006-07-01 16:48:23 -04001608 return -ENOMEM;
Rich Townsend3f86b832006-07-01 11:36:54 -04001609 }
Vladimir Lebedev6d157022007-03-19 17:45:50 +03001610 sbs->base = (val & 0xff00ull) >> 8;
Rich Townsend3f86b832006-07-01 11:36:54 -04001611
1612 sbs->device = device;
Rich Townsend3f86b832006-07-01 11:36:54 -04001613
1614 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1615 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1616 acpi_driver_data(device) = sbs;
1617
1618 sbs->update_time = 0;
1619 sbs->update_time2 = 0;
1620
1621 result = acpi_ac_add(sbs);
1622 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001623 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001624 goto end;
1625 }
1626 result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1627 if (ACPI_FAILURE(result)) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001628 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1629 "acpi_evaluate_integer() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001630 result = -EIO;
1631 goto end;
1632 }
1633
1634 if (sbs_obj > 0) {
1635 result = acpi_sbsm_get_info(sbs);
1636 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001637 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1638 "acpi_sbsm_get_info() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001639 goto end;
1640 }
1641 sbs->sbsm_present = 1;
1642 }
1643 if (sbs->sbsm_present == 0) {
1644 result = acpi_battery_add(sbs, 0);
1645 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001646 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1647 "acpi_battery_add() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001648 goto end;
1649 }
1650 } else {
1651 for (id = 0; id < MAX_SBS_BAT; id++) {
1652 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1653 result = acpi_battery_add(sbs, id);
1654 if (result) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001655 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1656 "acpi_battery_add() "
1657 "failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001658 goto end;
1659 }
1660 }
1661 }
1662 }
1663
1664 sbs->handle = device->handle;
1665
1666 init_timer(&sbs->update_timer);
1667 if (update_mode == QUEUE_UPDATE_MODE) {
1668 status = acpi_os_execute(OSL_GPE_HANDLER,
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001669 acpi_sbs_update_queue, sbs);
Rich Townsend3f86b832006-07-01 11:36:54 -04001670 if (status != AE_OK) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001671 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1672 "acpi_os_execute() failed"));
Rich Townsend3f86b832006-07-01 11:36:54 -04001673 }
1674 }
1675 sbs->update_time = update_time;
1676 sbs->update_time2 = update_time2;
1677
1678 printk(KERN_INFO PREFIX "%s [%s]\n",
1679 acpi_device_name(device), acpi_device_bid(device));
1680
1681 end:
1682 if (result) {
Vladimir Lebedev6d157022007-03-19 17:45:50 +03001683 acpi_sbs_remove(device, 0);
Rich Townsend3f86b832006-07-01 11:36:54 -04001684 }
1685
Len Brown635227e2006-07-01 16:48:23 -04001686 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001687}
1688
1689int acpi_sbs_remove(struct acpi_device *device, int type)
1690{
Len Browncece9012006-12-16 01:04:27 -05001691 struct acpi_sbs *sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -04001692 int id;
1693
Lebedev, Vladimir P963497c2006-09-05 19:49:13 +04001694 if (!device) {
1695 return -EINVAL;
1696 }
1697
1698 sbs = (struct acpi_sbs *)acpi_driver_data(device);
1699
1700 if (!sbs) {
Len Brown635227e2006-07-01 16:48:23 -04001701 return -EINVAL;
Rich Townsend3f86b832006-07-01 11:36:54 -04001702 }
1703
1704 sbs->zombie = 1;
1705 sbs->update_time = 0;
1706 sbs->update_time2 = 0;
1707 del_timer_sync(&sbs->update_timer);
1708 acpi_os_wait_events_complete(NULL);
1709 del_timer_sync(&sbs->update_timer);
1710
1711 for (id = 0; id < MAX_SBS_BAT; id++) {
1712 acpi_battery_remove(sbs, id);
1713 }
1714
1715 acpi_ac_remove(sbs);
1716
Vladimir Lebedev6d157022007-03-19 17:45:50 +03001717 acpi_driver_data(device) = NULL;
1718
Rich Townsend3f86b832006-07-01 11:36:54 -04001719 kfree(sbs);
1720
Len Brown635227e2006-07-01 16:48:23 -04001721 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -04001722}
1723
1724static int __init acpi_sbs_init(void)
1725{
1726 int result = 0;
1727
Len Brownb20d2ae2006-08-15 23:21:37 -04001728 if (acpi_disabled)
1729 return -ENODEV;
1730
Rich Townsend3f86b832006-07-01 11:36:54 -04001731 init_MUTEX(&sbs_sem);
1732
1733 if (capacity_mode != DEF_CAPACITY_UNIT
1734 && capacity_mode != MAH_CAPACITY_UNIT
1735 && capacity_mode != MWH_CAPACITY_UNIT) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001736 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_sbs_init: "
1737 "invalid capacity_mode = %d", capacity_mode));
Len Brown635227e2006-07-01 16:48:23 -04001738 return -EINVAL;
Rich Townsend3f86b832006-07-01 11:36:54 -04001739 }
1740
1741 acpi_ac_dir = acpi_lock_ac_dir();
1742 if (!acpi_ac_dir) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001743 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1744 "acpi_lock_ac_dir() failed"));
Len Brown635227e2006-07-01 16:48:23 -04001745 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001746 }
1747
1748 acpi_battery_dir = acpi_lock_battery_dir();
1749 if (!acpi_battery_dir) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001750 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1751 "acpi_lock_battery_dir() failed"));
Len Brown635227e2006-07-01 16:48:23 -04001752 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001753 }
1754
1755 result = acpi_bus_register_driver(&acpi_sbs_driver);
1756 if (result < 0) {
Vladimir Lebedev68451182007-03-19 17:45:50 +03001757 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1758 "acpi_bus_register_driver() failed"));
Len Brown635227e2006-07-01 16:48:23 -04001759 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001760 }
1761
Len Brown635227e2006-07-01 16:48:23 -04001762 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -04001763}
1764
1765static void __exit acpi_sbs_exit(void)
1766{
Rich Townsend3f86b832006-07-01 11:36:54 -04001767
1768 acpi_bus_unregister_driver(&acpi_sbs_driver);
1769
1770 acpi_unlock_ac_dir(acpi_ac_dir);
1771 acpi_ac_dir = NULL;
1772 acpi_unlock_battery_dir(acpi_battery_dir);
1773 acpi_battery_dir = NULL;
1774
Len Brown635227e2006-07-01 16:48:23 -04001775 return;
Rich Townsend3f86b832006-07-01 11:36:54 -04001776}
1777
1778module_init(acpi_sbs_init);
1779module_exit(acpi_sbs_exit);