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