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