blob: 59640d9a0acceefe1ee8a4e6d9495edb4f5c5431 [file] [log] [blame]
Rich Townsend3f86b832006-07-01 11:36:54 -04001/*
2 * acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $)
3 *
4 * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/kernel.h>
29#include <linux/proc_fs.h>
30#include <linux/seq_file.h>
31#include <asm/uaccess.h>
32#include <linux/acpi.h>
33#include <linux/i2c.h>
34#include <linux/delay.h>
35
36#include "i2c_ec.h"
37
38#define DEF_CAPACITY_UNIT 3
39#define MAH_CAPACITY_UNIT 1
40#define MWH_CAPACITY_UNIT 2
41#define CAPACITY_UNIT DEF_CAPACITY_UNIT
42
43#define REQUEST_UPDATE_MODE 1
44#define QUEUE_UPDATE_MODE 2
45
46#define DATA_TYPE_COMMON 0
47#define DATA_TYPE_INFO 1
48#define DATA_TYPE_STATE 2
49#define DATA_TYPE_ALARM 3
50#define DATA_TYPE_AC_STATE 4
51
52extern struct proc_dir_entry *acpi_lock_ac_dir(void);
53extern struct proc_dir_entry *acpi_lock_battery_dir(void);
54extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
55extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
56
57#define ACPI_SBS_COMPONENT 0x00080000
58#define ACPI_SBS_CLASS "sbs"
59#define ACPI_AC_CLASS "ac_adapter"
60#define ACPI_BATTERY_CLASS "battery"
61#define ACPI_SBS_HID "ACPI0002"
Rich Townsend3f86b832006-07-01 11:36:54 -040062#define ACPI_SBS_DEVICE_NAME "Smart Battery System"
63#define ACPI_SBS_FILE_INFO "info"
64#define ACPI_SBS_FILE_STATE "state"
65#define ACPI_SBS_FILE_ALARM "alarm"
66#define ACPI_BATTERY_DIR_NAME "BAT%i"
67#define ACPI_AC_DIR_NAME "AC0"
68#define ACPI_SBC_SMBUS_ADDR 0x9
69#define ACPI_SBSM_SMBUS_ADDR 0xa
70#define ACPI_SB_SMBUS_ADDR 0xb
71#define ACPI_SBS_AC_NOTIFY_STATUS 0x80
72#define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80
73#define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81
74
75#define _COMPONENT ACPI_SBS_COMPONENT
76
77#define MAX_SBS_BAT 4
78#define MAX_SMBUS_ERR 1
79
Len Brownf52fd662007-02-12 22:42:12 -050080ACPI_MODULE_NAME("sbs");
Rich Townsend3f86b832006-07-01 11:36:54 -040081
82MODULE_AUTHOR("Rich Townsend");
83MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
84MODULE_LICENSE("GPL");
85
86static struct semaphore sbs_sem;
87
88#define UPDATE_MODE QUEUE_UPDATE_MODE
89/* REQUEST_UPDATE_MODE QUEUE_UPDATE_MODE */
90#define UPDATE_INFO_MODE 0
91#define UPDATE_TIME 60
92#define UPDATE_TIME2 0
93
94static int capacity_mode = CAPACITY_UNIT;
95static int update_mode = UPDATE_MODE;
96static int update_info_mode = UPDATE_INFO_MODE;
97static int update_time = UPDATE_TIME;
98static int update_time2 = UPDATE_TIME2;
99
Lebedev, Vladimir P3cd5b872006-09-05 19:59:22 +0400100module_param(capacity_mode, int, 0);
101module_param(update_mode, int, 0);
102module_param(update_info_mode, int, 0);
103module_param(update_time, int, 0);
104module_param(update_time2, int, 0);
Rich Townsend3f86b832006-07-01 11:36:54 -0400105
106static int acpi_sbs_add(struct acpi_device *device);
107static int acpi_sbs_remove(struct acpi_device *device, int type);
108static void acpi_battery_smbus_err_handler(struct acpi_ec_smbus *smbus);
109static void acpi_sbs_update_queue(void *data);
110
111static struct acpi_driver acpi_sbs_driver = {
Len Brownc2b67052007-02-12 23:33:40 -0500112 .name = "sbs",
Rich Townsend3f86b832006-07-01 11:36:54 -0400113 .class = ACPI_SBS_CLASS,
114 .ids = ACPI_SBS_HID,
115 .ops = {
116 .add = acpi_sbs_add,
117 .remove = acpi_sbs_remove,
118 },
119};
120
121struct acpi_battery_info {
122 int capacity_mode;
123 s16 full_charge_capacity;
124 s16 design_capacity;
125 s16 design_voltage;
126 int vscale;
127 int ipscale;
128 s16 serial_number;
129 char manufacturer_name[I2C_SMBUS_BLOCK_MAX + 3];
130 char device_name[I2C_SMBUS_BLOCK_MAX + 3];
131 char device_chemistry[I2C_SMBUS_BLOCK_MAX + 3];
132};
133
134struct acpi_battery_state {
135 s16 voltage;
136 s16 amperage;
137 s16 remaining_capacity;
138 s16 average_time_to_empty;
139 s16 average_time_to_full;
140 s16 battery_status;
141};
142
143struct acpi_battery_alarm {
144 s16 remaining_capacity;
145};
146
147struct acpi_battery {
148 int alive;
149 int battery_present;
150 int id;
151 int init_state;
152 struct acpi_sbs *sbs;
153 struct acpi_battery_info info;
154 struct acpi_battery_state state;
155 struct acpi_battery_alarm alarm;
156 struct proc_dir_entry *battery_entry;
157};
158
159struct acpi_sbs {
160 acpi_handle handle;
161 struct acpi_device *device;
162 struct acpi_ec_smbus *smbus;
163 int sbsm_present;
164 int sbsm_batteries_supported;
165 int ac_present;
166 struct proc_dir_entry *ac_entry;
167 struct acpi_battery battery[MAX_SBS_BAT];
168 int update_info_mode;
169 int zombie;
170 int update_time;
171 int update_time2;
172 struct timer_list update_timer;
173};
174
175static void acpi_update_delay(struct acpi_sbs *sbs);
176static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type);
177
178/* --------------------------------------------------------------------------
179 SMBus Communication
180 -------------------------------------------------------------------------- */
181
182static void acpi_battery_smbus_err_handler(struct acpi_ec_smbus *smbus)
183{
184 union i2c_smbus_data data;
185 int result = 0;
186 char *err_str;
187 int err_number;
188
Rich Townsend3f86b832006-07-01 11:36:54 -0400189 data.word = 0;
190
191 result = smbus->adapter.algo->
192 smbus_xfer(&smbus->adapter,
193 ACPI_SB_SMBUS_ADDR,
194 0, I2C_SMBUS_READ, 0x16, I2C_SMBUS_BLOCK_DATA, &data);
195
196 err_number = (data.word & 0x000f);
197
198 switch (data.word & 0x000f) {
199 case 0x0000:
200 err_str = "unexpected bus error";
201 break;
202 case 0x0001:
203 err_str = "busy";
204 break;
205 case 0x0002:
206 err_str = "reserved command";
207 break;
208 case 0x0003:
209 err_str = "unsupported command";
210 break;
211 case 0x0004:
212 err_str = "access denied";
213 break;
214 case 0x0005:
215 err_str = "overflow/underflow";
216 break;
217 case 0x0006:
218 err_str = "bad size";
219 break;
220 case 0x0007:
221 err_str = "unknown error";
222 break;
223 default:
224 err_str = "unrecognized error";
225 }
226 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
227 "%s: ret %i, err %i\n", err_str, result, err_number));
228}
229
230static int
231acpi_sbs_smbus_read_word(struct acpi_ec_smbus *smbus, int addr, int func,
232 u16 * word,
233 void (*err_handler) (struct acpi_ec_smbus * smbus))
234{
235 union i2c_smbus_data data;
236 int result = 0;
237 int i;
238
Rich Townsend3f86b832006-07-01 11:36:54 -0400239 if (err_handler == NULL) {
240 err_handler = acpi_battery_smbus_err_handler;
241 }
242
243 for (i = 0; i < MAX_SMBUS_ERR; i++) {
244 result =
245 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
246 I2C_SMBUS_READ, func,
247 I2C_SMBUS_WORD_DATA, &data);
248 if (result) {
249 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
250 "try %i: smbus->adapter.algo->smbus_xfer() failed\n",
251 i));
252 if (err_handler) {
253 err_handler(smbus);
254 }
255 } else {
256 *word = data.word;
257 break;
258 }
259 }
260
Len Brown635227e2006-07-01 16:48:23 -0400261 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400262}
263
264static int
265acpi_sbs_smbus_read_str(struct acpi_ec_smbus *smbus, int addr, int func,
266 char *str,
267 void (*err_handler) (struct acpi_ec_smbus * smbus))
268{
269 union i2c_smbus_data data;
270 int result = 0;
271 int i;
272
Rich Townsend3f86b832006-07-01 11:36:54 -0400273 if (err_handler == NULL) {
274 err_handler = acpi_battery_smbus_err_handler;
275 }
276
277 for (i = 0; i < MAX_SMBUS_ERR; i++) {
278 result =
279 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
280 I2C_SMBUS_READ, func,
281 I2C_SMBUS_BLOCK_DATA,
282 &data);
283 if (result) {
284 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
285 "try %i: smbus->adapter.algo->smbus_xfer() failed\n",
286 i));
287 if (err_handler) {
288 err_handler(smbus);
289 }
290 } else {
291 strncpy(str, (const char *)data.block + 1,
292 data.block[0]);
293 str[data.block[0]] = 0;
294 break;
295 }
296 }
297
Len Brown635227e2006-07-01 16:48:23 -0400298 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400299}
300
301static int
302acpi_sbs_smbus_write_word(struct acpi_ec_smbus *smbus, int addr, int func,
303 int word,
304 void (*err_handler) (struct acpi_ec_smbus * smbus))
305{
306 union i2c_smbus_data data;
307 int result = 0;
308 int i;
309
Rich Townsend3f86b832006-07-01 11:36:54 -0400310 if (err_handler == NULL) {
311 err_handler = acpi_battery_smbus_err_handler;
312 }
313
314 data.word = word;
315
316 for (i = 0; i < MAX_SMBUS_ERR; i++) {
317 result =
318 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
319 I2C_SMBUS_WRITE, func,
320 I2C_SMBUS_WORD_DATA, &data);
321 if (result) {
322 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
323 "try %i: smbus->adapter.algo"
324 "->smbus_xfer() failed\n", i));
325 if (err_handler) {
326 err_handler(smbus);
327 }
328 } else {
329 break;
330 }
331 }
332
Len Brown635227e2006-07-01 16:48:23 -0400333 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400334}
335
336/* --------------------------------------------------------------------------
337 Smart Battery System Management
338 -------------------------------------------------------------------------- */
339
340/* Smart Battery */
341
342static int acpi_sbs_generate_event(struct acpi_device *device,
343 int event, int state, char *bid, char *class)
344{
345 char bid_saved[5];
346 char class_saved[20];
347 int result = 0;
348
Rich Townsend3f86b832006-07-01 11:36:54 -0400349 strcpy(bid_saved, acpi_device_bid(device));
350 strcpy(class_saved, acpi_device_class(device));
351
352 strcpy(acpi_device_bid(device), bid);
353 strcpy(acpi_device_class(device), class);
354
355 result = acpi_bus_generate_event(device, event, state);
356
357 strcpy(acpi_device_bid(device), bid_saved);
358 strcpy(acpi_device_class(device), class_saved);
359
Len Brown635227e2006-07-01 16:48:23 -0400360 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400361}
362
363static int acpi_battery_get_present(struct acpi_battery *battery)
364{
365 s16 state;
366 int result = 0;
367 int is_present = 0;
368
Rich Townsend3f86b832006-07-01 11:36:54 -0400369 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
370 ACPI_SBSM_SMBUS_ADDR, 0x01,
371 &state, NULL);
372 if (result) {
373 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
374 "acpi_sbs_smbus_read_word() failed"));
375 }
376 if (!result) {
377 is_present = (state & 0x000f) & (1 << battery->id);
378 }
379 battery->battery_present = is_present;
380
Len Brown635227e2006-07-01 16:48:23 -0400381 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400382}
383
384static int acpi_battery_is_present(struct acpi_battery *battery)
385{
386 return (battery->battery_present);
387}
388
389static int acpi_ac_is_present(struct acpi_sbs *sbs)
390{
391 return (sbs->ac_present);
392}
393
394static int acpi_battery_select(struct acpi_battery *battery)
395{
396 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
397 int result = 0;
398 s16 state;
399 int foo;
400
Rich Townsend3f86b832006-07-01 11:36:54 -0400401 if (battery->sbs->sbsm_present) {
402
403 /* Take special care not to knobble other nibbles of
404 * state (aka selector_state), since
405 * it causes charging to halt on SBSELs */
406
407 result =
408 acpi_sbs_smbus_read_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x01,
409 &state, NULL);
410 if (result) {
411 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
412 "acpi_sbs_smbus_read_word() failed\n"));
413 goto end;
414 }
415
416 foo = (state & 0x0fff) | (1 << (battery->id + 12));
417 result =
418 acpi_sbs_smbus_write_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x01,
419 foo, NULL);
420 if (result) {
421 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
422 "acpi_sbs_smbus_write_word() failed\n"));
423 goto end;
424 }
425 }
426
427 end:
Len Brown635227e2006-07-01 16:48:23 -0400428 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400429}
430
431static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
432{
433 struct acpi_ec_smbus *smbus = sbs->smbus;
434 int result = 0;
435 s16 battery_system_info;
436
Rich Townsend3f86b832006-07-01 11:36:54 -0400437 result = acpi_sbs_smbus_read_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x04,
438 &battery_system_info, NULL);
439 if (result) {
440 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
441 "acpi_sbs_smbus_read_word() failed\n"));
442 goto end;
443 }
444
445 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
446
447 end:
448
Len Brown635227e2006-07-01 16:48:23 -0400449 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400450}
451
452static int acpi_battery_get_info(struct acpi_battery *battery)
453{
454 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
455 int result = 0;
456 s16 battery_mode;
457 s16 specification_info;
458
Rich Townsend3f86b832006-07-01 11:36:54 -0400459 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x03,
460 &battery_mode,
461 &acpi_battery_smbus_err_handler);
462 if (result) {
463 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
464 "acpi_sbs_smbus_read_word() failed\n"));
465 goto end;
466 }
467 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
468
469 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x10,
470 &battery->info.full_charge_capacity,
471 &acpi_battery_smbus_err_handler);
472 if (result) {
473 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
474 "acpi_sbs_smbus_read_word() failed\n"));
475 goto end;
476 }
477
478 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x18,
479 &battery->info.design_capacity,
480 &acpi_battery_smbus_err_handler);
481
482 if (result) {
483 goto end;
484 }
485
486 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x19,
487 &battery->info.design_voltage,
488 &acpi_battery_smbus_err_handler);
489 if (result) {
490 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
491 "acpi_sbs_smbus_read_word() failed\n"));
492 goto end;
493 }
494
495 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x1a,
496 &specification_info,
497 &acpi_battery_smbus_err_handler);
498 if (result) {
499 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
500 "acpi_sbs_smbus_read_word() failed\n"));
501 goto end;
502 }
503
504 switch ((specification_info & 0x0f00) >> 8) {
505 case 1:
506 battery->info.vscale = 10;
507 break;
508 case 2:
509 battery->info.vscale = 100;
510 break;
511 case 3:
512 battery->info.vscale = 1000;
513 break;
514 default:
515 battery->info.vscale = 1;
516 }
517
518 switch ((specification_info & 0xf000) >> 12) {
519 case 1:
520 battery->info.ipscale = 10;
521 break;
522 case 2:
523 battery->info.ipscale = 100;
524 break;
525 case 3:
526 battery->info.ipscale = 1000;
527 break;
528 default:
529 battery->info.ipscale = 1;
530 }
531
532 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x1c,
533 &battery->info.serial_number,
534 &acpi_battery_smbus_err_handler);
535 if (result) {
536 goto end;
537 }
538
539 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x20,
540 battery->info.manufacturer_name,
541 &acpi_battery_smbus_err_handler);
542 if (result) {
543 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
544 "acpi_sbs_smbus_read_str() failed\n"));
545 goto end;
546 }
547
548 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x21,
549 battery->info.device_name,
550 &acpi_battery_smbus_err_handler);
551 if (result) {
552 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
553 "acpi_sbs_smbus_read_str() failed\n"));
554 goto end;
555 }
556
557 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x22,
558 battery->info.device_chemistry,
559 &acpi_battery_smbus_err_handler);
560 if (result) {
561 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
562 "acpi_sbs_smbus_read_str() failed\n"));
563 goto end;
564 }
565
566 end:
Len Brown635227e2006-07-01 16:48:23 -0400567 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400568}
569
570static void acpi_update_delay(struct acpi_sbs *sbs)
571{
Rich Townsend3f86b832006-07-01 11:36:54 -0400572 if (sbs->zombie) {
573 return;
574 }
575 if (sbs->update_time2 > 0) {
576 msleep(sbs->update_time2 * 1000);
577 }
578}
579
580static int acpi_battery_get_state(struct acpi_battery *battery)
581{
582 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
583 int result = 0;
584
Rich Townsend3f86b832006-07-01 11:36:54 -0400585 acpi_update_delay(battery->sbs);
586 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x09,
587 &battery->state.voltage,
588 &acpi_battery_smbus_err_handler);
589 if (result) {
590 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
591 "acpi_sbs_smbus_read_word() failed\n"));
592 goto end;
593 }
594
595 acpi_update_delay(battery->sbs);
596 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x0a,
597 &battery->state.amperage,
598 &acpi_battery_smbus_err_handler);
599 if (result) {
600 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
601 "acpi_sbs_smbus_read_word() failed\n"));
602 goto end;
603 }
604
605 acpi_update_delay(battery->sbs);
606 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x0f,
607 &battery->state.remaining_capacity,
608 &acpi_battery_smbus_err_handler);
609 if (result) {
610 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
611 "acpi_sbs_smbus_read_word() failed\n"));
612 goto end;
613 }
614
615 acpi_update_delay(battery->sbs);
616 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x12,
617 &battery->state.average_time_to_empty,
618 &acpi_battery_smbus_err_handler);
619 if (result) {
620 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
621 "acpi_sbs_smbus_read_word() failed\n"));
622 goto end;
623 }
624
625 acpi_update_delay(battery->sbs);
626 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x13,
627 &battery->state.average_time_to_full,
628 &acpi_battery_smbus_err_handler);
629 if (result) {
630 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
631 "acpi_sbs_smbus_read_word() failed\n"));
632 goto end;
633 }
634
635 acpi_update_delay(battery->sbs);
636 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x16,
637 &battery->state.battery_status,
638 &acpi_battery_smbus_err_handler);
639 if (result) {
640 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
641 "acpi_sbs_smbus_read_word() failed\n"));
642 goto end;
643 }
644
645 acpi_update_delay(battery->sbs);
646
647 end:
Len Brown635227e2006-07-01 16:48:23 -0400648 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400649}
650
651static int acpi_battery_get_alarm(struct acpi_battery *battery)
652{
653 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
654 int result = 0;
655
Rich Townsend3f86b832006-07-01 11:36:54 -0400656 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
657 &battery->alarm.remaining_capacity,
658 &acpi_battery_smbus_err_handler);
659 if (result) {
660 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
661 "acpi_sbs_smbus_read_word() failed\n"));
662 goto end;
663 }
664
665 acpi_update_delay(battery->sbs);
666
667 end:
668
Len Brown635227e2006-07-01 16:48:23 -0400669 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400670}
671
672static int acpi_battery_set_alarm(struct acpi_battery *battery,
673 unsigned long alarm)
674{
675 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
676 int result = 0;
677 s16 battery_mode;
678 int foo;
679
Rich Townsend3f86b832006-07-01 11:36:54 -0400680 result = acpi_battery_select(battery);
681 if (result) {
682 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
683 "acpi_battery_select() failed\n"));
684 goto end;
685 }
686
687 /* If necessary, enable the alarm */
688
689 if (alarm > 0) {
690 result =
691 acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x03,
692 &battery_mode,
693 &acpi_battery_smbus_err_handler);
694 if (result) {
695 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
696 "acpi_sbs_smbus_read_word() failed\n"));
697 goto end;
698 }
699
700 result =
701 acpi_sbs_smbus_write_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
702 battery_mode & 0xbfff,
703 &acpi_battery_smbus_err_handler);
704 if (result) {
705 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
706 "acpi_sbs_smbus_write_word() failed\n"));
707 goto end;
708 }
709 }
710
711 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
712 result = acpi_sbs_smbus_write_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
713 foo,
714 &acpi_battery_smbus_err_handler);
715 if (result) {
716 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
717 "acpi_sbs_smbus_write_word() failed\n"));
718 goto end;
719 }
720
721 end:
722
Len Brown635227e2006-07-01 16:48:23 -0400723 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400724}
725
726static int acpi_battery_set_mode(struct acpi_battery *battery)
727{
728 int result = 0;
729 s16 battery_mode;
730
Rich Townsend3f86b832006-07-01 11:36:54 -0400731 if (capacity_mode == DEF_CAPACITY_UNIT) {
732 goto end;
733 }
734
735 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
736 ACPI_SB_SMBUS_ADDR, 0x03,
737 &battery_mode, NULL);
738 if (result) {
739 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
740 "acpi_sbs_smbus_read_word() failed\n"));
741 goto end;
742 }
743
744 if (capacity_mode == MAH_CAPACITY_UNIT) {
745 battery_mode &= 0x7fff;
746 } else {
747 battery_mode |= 0x8000;
748 }
749 result = acpi_sbs_smbus_write_word(battery->sbs->smbus,
750 ACPI_SB_SMBUS_ADDR, 0x03,
751 battery_mode, NULL);
752 if (result) {
753 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
754 "acpi_sbs_smbus_write_word() failed\n"));
755 goto end;
756 }
757
758 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
759 ACPI_SB_SMBUS_ADDR, 0x03,
760 &battery_mode, NULL);
761 if (result) {
762 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
763 "acpi_sbs_smbus_read_word() failed\n"));
764 goto end;
765 }
766
767 end:
Len Brown635227e2006-07-01 16:48:23 -0400768 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400769}
770
771static int acpi_battery_init(struct acpi_battery *battery)
772{
773 int result = 0;
774
Rich Townsend3f86b832006-07-01 11:36:54 -0400775 result = acpi_battery_select(battery);
776 if (result) {
777 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
778 "acpi_battery_init() failed\n"));
779 goto end;
780 }
781
782 result = acpi_battery_set_mode(battery);
783 if (result) {
784 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
785 "acpi_battery_set_mode() failed\n"));
786 goto end;
787 }
788
789 result = acpi_battery_get_info(battery);
790 if (result) {
791 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
792 "acpi_battery_get_info() failed\n"));
793 goto end;
794 }
795
796 result = acpi_battery_get_state(battery);
797 if (result) {
798 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
799 "acpi_battery_get_state() failed\n"));
800 goto end;
801 }
802
803 result = acpi_battery_get_alarm(battery);
804 if (result) {
805 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
806 "acpi_battery_get_alarm() failed\n"));
807 goto end;
808 }
809
810 end:
Len Brown635227e2006-07-01 16:48:23 -0400811 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400812}
813
814static int acpi_ac_get_present(struct acpi_sbs *sbs)
815{
816 struct acpi_ec_smbus *smbus = sbs->smbus;
817 int result = 0;
818 s16 charger_status;
819
Rich Townsend3f86b832006-07-01 11:36:54 -0400820 result = acpi_sbs_smbus_read_word(smbus, ACPI_SBC_SMBUS_ADDR, 0x13,
821 &charger_status, NULL);
822
823 if (result) {
824 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
825 "acpi_sbs_smbus_read_word() failed\n"));
826 goto end;
827 }
828
829 sbs->ac_present = (charger_status & 0x8000) >> 15;
830
831 end:
832
Len Brown635227e2006-07-01 16:48:23 -0400833 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400834}
835
836/* --------------------------------------------------------------------------
837 FS Interface (/proc/acpi)
838 -------------------------------------------------------------------------- */
839
840/* Generic Routines */
841
842static int
843acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
844 struct proc_dir_entry *parent_dir,
845 char *dir_name,
846 struct file_operations *info_fops,
847 struct file_operations *state_fops,
848 struct file_operations *alarm_fops, void *data)
849{
850 struct proc_dir_entry *entry = NULL;
851
Rich Townsend3f86b832006-07-01 11:36:54 -0400852 if (!*dir) {
853 *dir = proc_mkdir(dir_name, parent_dir);
854 if (!*dir) {
855 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
856 "proc_mkdir() failed\n"));
Len Brown635227e2006-07-01 16:48:23 -0400857 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400858 }
859 (*dir)->owner = THIS_MODULE;
860 }
861
862 /* 'info' [R] */
863 if (info_fops) {
864 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
865 if (!entry) {
866 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
867 "create_proc_entry() failed\n"));
868 } else {
869 entry->proc_fops = info_fops;
870 entry->data = data;
871 entry->owner = THIS_MODULE;
872 }
873 }
874
875 /* 'state' [R] */
876 if (state_fops) {
877 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
878 if (!entry) {
879 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
880 "create_proc_entry() failed\n"));
881 } else {
882 entry->proc_fops = state_fops;
883 entry->data = data;
884 entry->owner = THIS_MODULE;
885 }
886 }
887
888 /* 'alarm' [R/W] */
889 if (alarm_fops) {
890 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
891 if (!entry) {
892 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
893 "create_proc_entry() failed\n"));
894 } else {
895 entry->proc_fops = alarm_fops;
896 entry->data = data;
897 entry->owner = THIS_MODULE;
898 }
899 }
900
Len Brown635227e2006-07-01 16:48:23 -0400901 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -0400902}
903
904static void
905acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
906 struct proc_dir_entry *parent_dir)
907{
Rich Townsend3f86b832006-07-01 11:36:54 -0400908
909 if (*dir) {
910 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
911 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
912 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
913 remove_proc_entry((*dir)->name, parent_dir);
914 *dir = NULL;
915 }
916
917}
918
919/* Smart Battery Interface */
920
921static struct proc_dir_entry *acpi_battery_dir = NULL;
922
923static int acpi_battery_read_info(struct seq_file *seq, void *offset)
924{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200925 struct acpi_battery *battery = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -0400926 int cscale;
927 int result = 0;
928
Rich Townsend3f86b832006-07-01 11:36:54 -0400929 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -0400930 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -0400931 }
932
933 down(&sbs_sem);
934
935 if (update_mode == REQUEST_UPDATE_MODE) {
936 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_INFO);
937 if (result) {
938 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
939 "acpi_sbs_update_run() failed\n"));
940 }
941 }
942
943 if (acpi_battery_is_present(battery)) {
944 seq_printf(seq, "present: yes\n");
945 } else {
946 seq_printf(seq, "present: no\n");
947 goto end;
948 }
949
950 if (battery->info.capacity_mode) {
951 cscale = battery->info.vscale * battery->info.ipscale;
952 } else {
953 cscale = battery->info.ipscale;
954 }
955 seq_printf(seq, "design capacity: %i%s",
956 battery->info.design_capacity * cscale,
957 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
958
959 seq_printf(seq, "last full capacity: %i%s",
960 battery->info.full_charge_capacity * cscale,
961 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
962
963 seq_printf(seq, "battery technology: rechargeable\n");
964
965 seq_printf(seq, "design voltage: %i mV\n",
966 battery->info.design_voltage * battery->info.vscale);
967
968 seq_printf(seq, "design capacity warning: unknown\n");
969 seq_printf(seq, "design capacity low: unknown\n");
970 seq_printf(seq, "capacity granularity 1: unknown\n");
971 seq_printf(seq, "capacity granularity 2: unknown\n");
972
973 seq_printf(seq, "model number: %s\n",
974 battery->info.device_name);
975
976 seq_printf(seq, "serial number: %i\n",
977 battery->info.serial_number);
978
979 seq_printf(seq, "battery type: %s\n",
980 battery->info.device_chemistry);
981
982 seq_printf(seq, "OEM info: %s\n",
983 battery->info.manufacturer_name);
984
985 end:
986
987 up(&sbs_sem);
988
Len Brown635227e2006-07-01 16:48:23 -0400989 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -0400990}
991
992static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
993{
994 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
995}
996
997static int acpi_battery_read_state(struct seq_file *seq, void *offset)
998{
999 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1000 int result = 0;
1001 int cscale;
1002 int foo;
1003
Rich Townsend3f86b832006-07-01 11:36:54 -04001004 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001005 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001006 }
1007
1008 down(&sbs_sem);
1009
1010 if (update_mode == REQUEST_UPDATE_MODE) {
1011 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_STATE);
1012 if (result) {
1013 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1014 "acpi_sbs_update_run() failed\n"));
1015 }
1016 }
1017
1018 if (acpi_battery_is_present(battery)) {
1019 seq_printf(seq, "present: yes\n");
1020 } else {
1021 seq_printf(seq, "present: no\n");
1022 goto end;
1023 }
1024
1025 if (battery->info.capacity_mode) {
1026 cscale = battery->info.vscale * battery->info.ipscale;
1027 } else {
1028 cscale = battery->info.ipscale;
1029 }
1030
1031 if (battery->state.battery_status & 0x0010) {
1032 seq_printf(seq, "capacity state: critical\n");
1033 } else {
1034 seq_printf(seq, "capacity state: ok\n");
1035 }
Vladimir Lebedeve6d0f562007-02-10 01:51:13 -05001036
1037 foo = (s16) battery->state.amperage * battery->info.ipscale;
1038 if (battery->info.capacity_mode) {
1039 foo = foo * battery->info.design_voltage / 1000;
1040 }
Rich Townsend3f86b832006-07-01 11:36:54 -04001041 if (battery->state.amperage < 0) {
1042 seq_printf(seq, "charging state: discharging\n");
Vladimir Lebedeve6d0f562007-02-10 01:51:13 -05001043 seq_printf(seq, "present rate: %d %s\n",
1044 -foo, battery->info.capacity_mode ? "mW" : "mA");
Rich Townsend3f86b832006-07-01 11:36:54 -04001045 } else if (battery->state.amperage > 0) {
1046 seq_printf(seq, "charging state: charging\n");
Vladimir Lebedeve6d0f562007-02-10 01:51:13 -05001047 seq_printf(seq, "present rate: %d %s\n",
1048 foo, battery->info.capacity_mode ? "mW" : "mA");
Rich Townsend3f86b832006-07-01 11:36:54 -04001049 } else {
1050 seq_printf(seq, "charging state: charged\n");
1051 seq_printf(seq, "present rate: 0 %s\n",
1052 battery->info.capacity_mode ? "mW" : "mA");
1053 }
1054
1055 seq_printf(seq, "remaining capacity: %i%s",
1056 battery->state.remaining_capacity * cscale,
1057 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1058
1059 seq_printf(seq, "present voltage: %i mV\n",
1060 battery->state.voltage * battery->info.vscale);
1061
1062 end:
1063
1064 up(&sbs_sem);
1065
Len Brown635227e2006-07-01 16:48:23 -04001066 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001067}
1068
1069static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1070{
1071 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1072}
1073
1074static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1075{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001076 struct acpi_battery *battery = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -04001077 int result = 0;
1078 int cscale;
1079
Rich Townsend3f86b832006-07-01 11:36:54 -04001080 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001081 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001082 }
1083
1084 down(&sbs_sem);
1085
1086 if (update_mode == REQUEST_UPDATE_MODE) {
1087 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM);
1088 if (result) {
1089 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1090 "acpi_sbs_update_run() failed\n"));
1091 }
1092 }
1093
1094 if (!acpi_battery_is_present(battery)) {
1095 seq_printf(seq, "present: no\n");
1096 goto end;
1097 }
1098
1099 if (battery->info.capacity_mode) {
1100 cscale = battery->info.vscale * battery->info.ipscale;
1101 } else {
1102 cscale = battery->info.ipscale;
1103 }
1104
1105 seq_printf(seq, "alarm: ");
1106 if (battery->alarm.remaining_capacity) {
1107 seq_printf(seq, "%i%s",
1108 battery->alarm.remaining_capacity * cscale,
1109 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1110 } else {
1111 seq_printf(seq, "disabled\n");
1112 }
1113
1114 end:
1115
1116 up(&sbs_sem);
1117
Len Brown635227e2006-07-01 16:48:23 -04001118 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001119}
1120
1121static ssize_t
1122acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1123 size_t count, loff_t * ppos)
1124{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001125 struct seq_file *seq = file->private_data;
1126 struct acpi_battery *battery = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -04001127 char alarm_string[12] = { '\0' };
1128 int result, old_alarm, new_alarm;
1129
Rich Townsend3f86b832006-07-01 11:36:54 -04001130 if (battery->sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001131 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001132 }
1133
1134 down(&sbs_sem);
1135
1136 if (!acpi_battery_is_present(battery)) {
1137 result = -ENODEV;
1138 goto end;
1139 }
1140
1141 if (count > sizeof(alarm_string) - 1) {
1142 result = -EINVAL;
1143 goto end;
1144 }
1145
1146 if (copy_from_user(alarm_string, buffer, count)) {
1147 result = -EFAULT;
1148 goto end;
1149 }
1150
1151 alarm_string[count] = 0;
1152
1153 old_alarm = battery->alarm.remaining_capacity;
1154 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1155
1156 result = acpi_battery_set_alarm(battery, new_alarm);
1157 if (result) {
1158 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1159 "acpi_battery_set_alarm() failed\n"));
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001160 acpi_battery_set_alarm(battery, old_alarm);
Rich Townsend3f86b832006-07-01 11:36:54 -04001161 goto end;
1162 }
1163 result = acpi_battery_get_alarm(battery);
1164 if (result) {
1165 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1166 "acpi_battery_get_alarm() failed\n"));
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001167 acpi_battery_set_alarm(battery, old_alarm);
Rich Townsend3f86b832006-07-01 11:36:54 -04001168 goto end;
1169 }
1170
1171 end:
1172 up(&sbs_sem);
1173
1174 if (result) {
Len Brown635227e2006-07-01 16:48:23 -04001175 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001176 } else {
Len Brown635227e2006-07-01 16:48:23 -04001177 return count;
Rich Townsend3f86b832006-07-01 11:36:54 -04001178 }
1179}
1180
1181static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1182{
1183 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1184}
1185
1186static struct file_operations acpi_battery_info_fops = {
1187 .open = acpi_battery_info_open_fs,
1188 .read = seq_read,
1189 .llseek = seq_lseek,
1190 .release = single_release,
1191 .owner = THIS_MODULE,
1192};
1193
1194static struct file_operations acpi_battery_state_fops = {
1195 .open = acpi_battery_state_open_fs,
1196 .read = seq_read,
1197 .llseek = seq_lseek,
1198 .release = single_release,
1199 .owner = THIS_MODULE,
1200};
1201
1202static struct file_operations acpi_battery_alarm_fops = {
1203 .open = acpi_battery_alarm_open_fs,
1204 .read = seq_read,
1205 .write = acpi_battery_write_alarm,
1206 .llseek = seq_lseek,
1207 .release = single_release,
1208 .owner = THIS_MODULE,
1209};
1210
1211/* Legacy AC Adapter Interface */
1212
1213static struct proc_dir_entry *acpi_ac_dir = NULL;
1214
1215static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1216{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001217 struct acpi_sbs *sbs = seq->private;
Rich Townsend3f86b832006-07-01 11:36:54 -04001218 int result;
1219
Rich Townsend3f86b832006-07-01 11:36:54 -04001220 if (sbs->zombie) {
Len Brown635227e2006-07-01 16:48:23 -04001221 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001222 }
1223
1224 down(&sbs_sem);
1225
1226 if (update_mode == REQUEST_UPDATE_MODE) {
1227 result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE);
1228 if (result) {
1229 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1230 "acpi_sbs_update_run() failed\n"));
1231 }
1232 }
1233
1234 seq_printf(seq, "state: %s\n",
1235 sbs->ac_present ? "on-line" : "off-line");
1236
1237 up(&sbs_sem);
1238
Len Brown635227e2006-07-01 16:48:23 -04001239 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -04001240}
1241
1242static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1243{
1244 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1245}
1246
1247static struct file_operations acpi_ac_state_fops = {
1248 .open = acpi_ac_state_open_fs,
1249 .read = seq_read,
1250 .llseek = seq_lseek,
1251 .release = single_release,
1252 .owner = THIS_MODULE,
1253};
1254
1255/* --------------------------------------------------------------------------
1256 Driver Interface
1257 -------------------------------------------------------------------------- */
1258
1259/* Smart Battery */
1260
1261static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1262{
1263 int is_present;
1264 int result;
1265 char dir_name[32];
1266 struct acpi_battery *battery;
1267
Rich Townsend3f86b832006-07-01 11:36:54 -04001268 battery = &sbs->battery[id];
1269
1270 battery->alive = 0;
1271
1272 battery->init_state = 0;
1273 battery->id = id;
1274 battery->sbs = sbs;
1275
1276 result = acpi_battery_select(battery);
1277 if (result) {
1278 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1279 "acpi_battery_select() failed\n"));
1280 goto end;
1281 }
1282
1283 result = acpi_battery_get_present(battery);
1284 if (result) {
1285 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1286 "acpi_battery_get_present() failed\n"));
1287 goto end;
1288 }
1289
1290 is_present = acpi_battery_is_present(battery);
1291
1292 if (is_present) {
1293 result = acpi_battery_init(battery);
1294 if (result) {
1295 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1296 "acpi_battery_init() failed\n"));
1297 goto end;
1298 }
1299 battery->init_state = 1;
1300 }
1301
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001302 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
Rich Townsend3f86b832006-07-01 11:36:54 -04001303
1304 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1305 acpi_battery_dir,
1306 dir_name,
1307 &acpi_battery_info_fops,
1308 &acpi_battery_state_fops,
1309 &acpi_battery_alarm_fops, battery);
1310 if (result) {
1311 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1312 "acpi_sbs_generic_add_fs() failed\n"));
1313 goto end;
1314 }
1315 battery->alive = 1;
1316
1317 end:
Len Brown635227e2006-07-01 16:48:23 -04001318 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001319}
1320
1321static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1322{
Rich Townsend3f86b832006-07-01 11:36:54 -04001323
1324 if (sbs->battery[id].battery_entry) {
1325 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1326 acpi_battery_dir);
1327 }
1328}
1329
1330static int acpi_ac_add(struct acpi_sbs *sbs)
1331{
1332 int result;
1333
Rich Townsend3f86b832006-07-01 11:36:54 -04001334 result = acpi_ac_get_present(sbs);
1335 if (result) {
1336 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1337 "acpi_ac_get_present() failed\n"));
1338 goto end;
1339 }
1340
1341 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1342 acpi_ac_dir,
1343 ACPI_AC_DIR_NAME,
1344 NULL, &acpi_ac_state_fops, NULL, sbs);
1345 if (result) {
1346 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1347 "acpi_sbs_generic_add_fs() failed\n"));
1348 goto end;
1349 }
1350
1351 end:
1352
Len Brown635227e2006-07-01 16:48:23 -04001353 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001354}
1355
1356static void acpi_ac_remove(struct acpi_sbs *sbs)
1357{
Rich Townsend3f86b832006-07-01 11:36:54 -04001358
1359 if (sbs->ac_entry) {
1360 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1361 }
1362}
1363
1364static void acpi_sbs_update_queue_run(unsigned long data)
1365{
Rich Townsend3f86b832006-07-01 11:36:54 -04001366 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data);
1367}
1368
1369static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type)
1370{
1371 struct acpi_battery *battery;
1372 int result = 0;
1373 int old_ac_present;
1374 int old_battery_present;
1375 int new_ac_present;
1376 int new_battery_present;
1377 int id;
1378 char dir_name[32];
1379 int do_battery_init, do_ac_init;
1380 s16 old_remaining_capacity;
1381
Rich Townsend3f86b832006-07-01 11:36:54 -04001382 if (sbs->zombie) {
1383 goto end;
1384 }
1385
1386 old_ac_present = acpi_ac_is_present(sbs);
1387
1388 result = acpi_ac_get_present(sbs);
1389 if (result) {
1390 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1391 "acpi_ac_get_present() failed\n"));
1392 }
1393
1394 new_ac_present = acpi_ac_is_present(sbs);
1395
1396 do_ac_init = (old_ac_present != new_ac_present);
1397
1398 if (data_type == DATA_TYPE_AC_STATE) {
1399 goto end;
1400 }
1401
1402 for (id = 0; id < MAX_SBS_BAT; id++) {
1403 battery = &sbs->battery[id];
1404 if (battery->alive == 0) {
1405 continue;
1406 }
1407
1408 old_remaining_capacity = battery->state.remaining_capacity;
1409
1410 old_battery_present = acpi_battery_is_present(battery);
1411
1412 result = acpi_battery_select(battery);
1413 if (result) {
1414 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1415 "acpi_battery_select() failed\n"));
1416 }
1417 if (sbs->zombie) {
1418 goto end;
1419 }
1420
1421 result = acpi_battery_get_present(battery);
1422 if (result) {
1423 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1424 "acpi_battery_get_present() failed\n"));
1425 }
1426 if (sbs->zombie) {
1427 goto end;
1428 }
1429
1430 new_battery_present = acpi_battery_is_present(battery);
1431
1432 do_battery_init = ((old_battery_present != new_battery_present)
1433 && new_battery_present);
1434
1435 if (sbs->zombie) {
1436 goto end;
1437 }
1438 if (do_ac_init || do_battery_init ||
1439 update_info_mode || sbs->update_info_mode) {
1440 if (sbs->update_info_mode) {
1441 sbs->update_info_mode = 0;
1442 } else {
1443 sbs->update_info_mode = 1;
1444 }
1445 result = acpi_battery_init(battery);
1446 if (result) {
1447 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1448 "acpi_battery_init() "
1449 "failed\n"));
1450 }
1451 }
1452 if (data_type == DATA_TYPE_INFO) {
1453 continue;
1454 }
1455
1456 if (sbs->zombie) {
1457 goto end;
1458 }
1459 if (new_battery_present) {
1460 result = acpi_battery_get_alarm(battery);
1461 if (result) {
1462 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1463 "acpi_battery_get_alarm() "
1464 "failed\n"));
1465 }
1466 if (data_type == DATA_TYPE_ALARM) {
1467 continue;
1468 }
1469
1470 result = acpi_battery_get_state(battery);
1471 if (result) {
1472 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1473 "acpi_battery_get_state() "
1474 "failed\n"));
1475 }
1476 }
1477 if (sbs->zombie) {
1478 goto end;
1479 }
1480 if (data_type != DATA_TYPE_COMMON) {
1481 continue;
1482 }
1483
1484 if (old_battery_present != new_battery_present) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001485 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
Rich Townsend3f86b832006-07-01 11:36:54 -04001486 result = acpi_sbs_generate_event(sbs->device,
1487 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1488 new_battery_present,
1489 dir_name,
1490 ACPI_BATTERY_CLASS);
1491 if (result) {
1492 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1493 "acpi_sbs_generate_event() "
1494 "failed\n"));
1495 }
1496 }
1497 if (old_remaining_capacity != battery->state.remaining_capacity) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001498 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
Rich Townsend3f86b832006-07-01 11:36:54 -04001499 result = acpi_sbs_generate_event(sbs->device,
1500 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1501 new_battery_present,
1502 dir_name,
1503 ACPI_BATTERY_CLASS);
1504 if (result) {
1505 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1506 "acpi_sbs_generate_event() failed\n"));
1507 }
1508 }
1509
1510 }
1511 if (sbs->zombie) {
1512 goto end;
1513 }
1514 if (data_type != DATA_TYPE_COMMON) {
1515 goto end;
1516 }
1517
1518 if (old_ac_present != new_ac_present) {
1519 result = acpi_sbs_generate_event(sbs->device,
1520 ACPI_SBS_AC_NOTIFY_STATUS,
1521 new_ac_present,
1522 ACPI_AC_DIR_NAME,
1523 ACPI_AC_CLASS);
1524 if (result) {
1525 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1526 "acpi_sbs_generate_event() failed\n"));
1527 }
1528 }
1529
1530 end:
Len Brown635227e2006-07-01 16:48:23 -04001531 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001532}
1533
1534static void acpi_sbs_update_queue(void *data)
1535{
1536 struct acpi_sbs *sbs = data;
1537 unsigned long delay = -1;
1538 int result;
1539
Rich Townsend3f86b832006-07-01 11:36:54 -04001540 if (sbs->zombie) {
1541 goto end;
1542 }
1543
1544 result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON);
1545 if (result) {
1546 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1547 "acpi_sbs_update_run() failed\n"));
1548 }
1549
1550 if (sbs->zombie) {
1551 goto end;
1552 }
1553
1554 if (update_mode == REQUEST_UPDATE_MODE) {
1555 goto end;
1556 }
1557
1558 delay = jiffies + HZ * update_time;
1559 sbs->update_timer.data = (unsigned long)data;
1560 sbs->update_timer.function = acpi_sbs_update_queue_run;
1561 sbs->update_timer.expires = delay;
1562 add_timer(&sbs->update_timer);
1563 end:
1564 ;
1565}
1566
1567static int acpi_sbs_add(struct acpi_device *device)
1568{
1569 struct acpi_sbs *sbs = NULL;
1570 struct acpi_ec_hc *ec_hc = NULL;
1571 int result, remove_result = 0;
1572 unsigned long sbs_obj;
1573 int id, cnt;
1574 acpi_status status = AE_OK;
1575
Burman Yan36bcbec2006-12-19 12:56:11 -08001576 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
Rich Townsend3f86b832006-07-01 11:36:54 -04001577 if (!sbs) {
1578 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "kmalloc() failed\n"));
Len Brown635227e2006-07-01 16:48:23 -04001579 return -ENOMEM;
Rich Townsend3f86b832006-07-01 11:36:54 -04001580 }
Rich Townsend3f86b832006-07-01 11:36:54 -04001581
1582 cnt = 0;
1583 while (cnt < 10) {
1584 cnt++;
1585 ec_hc = acpi_get_ec_hc(device);
1586 if (ec_hc) {
1587 break;
1588 }
1589 msleep(1000);
1590 }
1591
1592 if (!ec_hc) {
1593 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1594 "acpi_get_ec_hc() failed: "
1595 "NO driver found for EC HC SMBus\n"));
1596 result = -ENODEV;
1597 goto end;
1598 }
1599
1600 sbs->device = device;
1601 sbs->smbus = ec_hc->smbus;
1602
1603 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1604 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1605 acpi_driver_data(device) = sbs;
1606
1607 sbs->update_time = 0;
1608 sbs->update_time2 = 0;
1609
1610 result = acpi_ac_add(sbs);
1611 if (result) {
1612 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_ac_add() failed\n"));
1613 goto end;
1614 }
1615 result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1616 if (ACPI_FAILURE(result)) {
1617 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1618 "acpi_evaluate_integer() failed\n"));
1619 result = -EIO;
1620 goto end;
1621 }
1622
1623 if (sbs_obj > 0) {
1624 result = acpi_sbsm_get_info(sbs);
1625 if (result) {
1626 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1627 "acpi_sbsm_get_info() failed\n"));
1628 goto end;
1629 }
1630 sbs->sbsm_present = 1;
1631 }
1632 if (sbs->sbsm_present == 0) {
1633 result = acpi_battery_add(sbs, 0);
1634 if (result) {
1635 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1636 "acpi_battery_add() failed\n"));
1637 goto end;
1638 }
1639 } else {
1640 for (id = 0; id < MAX_SBS_BAT; id++) {
1641 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1642 result = acpi_battery_add(sbs, id);
1643 if (result) {
1644 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1645 "acpi_battery_add() "
1646 "failed\n"));
1647 goto end;
1648 }
1649 }
1650 }
1651 }
1652
1653 sbs->handle = device->handle;
1654
1655 init_timer(&sbs->update_timer);
1656 if (update_mode == QUEUE_UPDATE_MODE) {
1657 status = acpi_os_execute(OSL_GPE_HANDLER,
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001658 acpi_sbs_update_queue, sbs);
Rich Townsend3f86b832006-07-01 11:36:54 -04001659 if (status != AE_OK) {
1660 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1661 "acpi_os_execute() failed\n"));
1662 }
1663 }
1664 sbs->update_time = update_time;
1665 sbs->update_time2 = update_time2;
1666
1667 printk(KERN_INFO PREFIX "%s [%s]\n",
1668 acpi_device_name(device), acpi_device_bid(device));
1669
1670 end:
1671 if (result) {
1672 remove_result = acpi_sbs_remove(device, 0);
1673 if (remove_result) {
1674 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1675 "acpi_sbs_remove() failed\n"));
1676 }
1677 }
1678
Len Brown635227e2006-07-01 16:48:23 -04001679 return result;
Rich Townsend3f86b832006-07-01 11:36:54 -04001680}
1681
1682int acpi_sbs_remove(struct acpi_device *device, int type)
1683{
Len Browncece9012006-12-16 01:04:27 -05001684 struct acpi_sbs *sbs;
Rich Townsend3f86b832006-07-01 11:36:54 -04001685 int id;
1686
Lebedev, Vladimir P963497c2006-09-05 19:49:13 +04001687 if (!device) {
1688 return -EINVAL;
1689 }
1690
1691 sbs = (struct acpi_sbs *)acpi_driver_data(device);
1692
1693 if (!sbs) {
Len Brown635227e2006-07-01 16:48:23 -04001694 return -EINVAL;
Rich Townsend3f86b832006-07-01 11:36:54 -04001695 }
1696
1697 sbs->zombie = 1;
1698 sbs->update_time = 0;
1699 sbs->update_time2 = 0;
1700 del_timer_sync(&sbs->update_timer);
1701 acpi_os_wait_events_complete(NULL);
1702 del_timer_sync(&sbs->update_timer);
1703
1704 for (id = 0; id < MAX_SBS_BAT; id++) {
1705 acpi_battery_remove(sbs, id);
1706 }
1707
1708 acpi_ac_remove(sbs);
1709
1710 kfree(sbs);
1711
Len Brown635227e2006-07-01 16:48:23 -04001712 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -04001713}
1714
1715static int __init acpi_sbs_init(void)
1716{
1717 int result = 0;
1718
Len Brownb20d2ae2006-08-15 23:21:37 -04001719 if (acpi_disabled)
1720 return -ENODEV;
1721
Rich Townsend3f86b832006-07-01 11:36:54 -04001722 init_MUTEX(&sbs_sem);
1723
1724 if (capacity_mode != DEF_CAPACITY_UNIT
1725 && capacity_mode != MAH_CAPACITY_UNIT
1726 && capacity_mode != MWH_CAPACITY_UNIT) {
1727 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_sbs_init: "
1728 "invalid capacity_mode = %d\n",
1729 capacity_mode));
Len Brown635227e2006-07-01 16:48:23 -04001730 return -EINVAL;
Rich Townsend3f86b832006-07-01 11:36:54 -04001731 }
1732
1733 acpi_ac_dir = acpi_lock_ac_dir();
1734 if (!acpi_ac_dir) {
1735 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1736 "acpi_lock_ac_dir() failed\n"));
Len Brown635227e2006-07-01 16:48:23 -04001737 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001738 }
1739
1740 acpi_battery_dir = acpi_lock_battery_dir();
1741 if (!acpi_battery_dir) {
1742 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1743 "acpi_lock_battery_dir() failed\n"));
Len Brown635227e2006-07-01 16:48:23 -04001744 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001745 }
1746
1747 result = acpi_bus_register_driver(&acpi_sbs_driver);
1748 if (result < 0) {
1749 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1750 "acpi_bus_register_driver() failed\n"));
Len Brown635227e2006-07-01 16:48:23 -04001751 return -ENODEV;
Rich Townsend3f86b832006-07-01 11:36:54 -04001752 }
1753
Len Brown635227e2006-07-01 16:48:23 -04001754 return 0;
Rich Townsend3f86b832006-07-01 11:36:54 -04001755}
1756
1757static void __exit acpi_sbs_exit(void)
1758{
Rich Townsend3f86b832006-07-01 11:36:54 -04001759
1760 acpi_bus_unregister_driver(&acpi_sbs_driver);
1761
1762 acpi_unlock_ac_dir(acpi_ac_dir);
1763 acpi_ac_dir = NULL;
1764 acpi_unlock_battery_dir(acpi_battery_dir);
1765 acpi_battery_dir = NULL;
1766
Len Brown635227e2006-07-01 16:48:23 -04001767 return;
Rich Townsend3f86b832006-07-01 11:36:54 -04001768}
1769
1770module_init(acpi_sbs_init);
1771module_exit(acpi_sbs_exit);