sbs.c 34.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $)
 *
 *  Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>
#include <linux/acpi.h>
33
#include <linux/timer.h>
34
#include <linux/jiffies.h>
35 36
#include <linux/delay.h>

37 38
#include "sbshc.h"

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#define ACPI_SBS_COMPONENT		0x00080000
#define ACPI_SBS_CLASS			"sbs"
#define ACPI_AC_CLASS			"ac_adapter"
#define ACPI_BATTERY_CLASS		"battery"
#define ACPI_SBS_DEVICE_NAME		"Smart Battery System"
#define ACPI_SBS_FILE_INFO		"info"
#define ACPI_SBS_FILE_STATE		"state"
#define ACPI_SBS_FILE_ALARM		"alarm"
#define ACPI_BATTERY_DIR_NAME		"BAT%i"
#define ACPI_AC_DIR_NAME		"AC0"
#define ACPI_SBC_SMBUS_ADDR		0x9
#define ACPI_SBSM_SMBUS_ADDR		0xa
#define ACPI_SB_SMBUS_ADDR		0xb
#define ACPI_SBS_AC_NOTIFY_STATUS	0x80
#define ACPI_SBS_BATTERY_NOTIFY_STATUS	0x80
#define ACPI_SBS_BATTERY_NOTIFY_INFO	0x81

#define _COMPONENT			ACPI_SBS_COMPONENT

58
ACPI_MODULE_NAME("sbs");
59 60 61 62 63

MODULE_AUTHOR("Rich Townsend");
MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
MODULE_LICENSE("GPL");

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#define	DEF_CAPACITY_UNIT	3
#define	MAH_CAPACITY_UNIT	1
#define	MWH_CAPACITY_UNIT	2
#define	CAPACITY_UNIT		DEF_CAPACITY_UNIT

#define	REQUEST_UPDATE_MODE	1
#define	QUEUE_UPDATE_MODE	2

#define	DATA_TYPE_COMMON	0
#define	DATA_TYPE_INFO		1
#define	DATA_TYPE_STATE		2
#define	DATA_TYPE_ALARM		3
#define	DATA_TYPE_AC_STATE	4

extern struct proc_dir_entry *acpi_lock_ac_dir(void);
extern struct proc_dir_entry *acpi_lock_battery_dir(void);
extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);

#define	MAX_SBS_BAT			4
#define ACPI_SBS_BLOCK_MAX		32

86
#define	UPDATE_DELAY	10
87

88 89
/* 0 - every time, > 0 - by update_time */
static unsigned int update_time = 120;
90

91
static unsigned int mode = CAPACITY_UNIT;
92

93
module_param(update_time, uint, 0644);
94
module_param(mode, uint, 0444);
95 96 97

static int acpi_sbs_add(struct acpi_device *device);
static int acpi_sbs_remove(struct acpi_device *device, int type);
98
static int acpi_sbs_resume(struct acpi_device *device);
99

100
static const struct acpi_device_id sbs_device_ids[] = {
101
	{"ACPI0002", 0},
102 103 104 105
	{"", 0},
};
MODULE_DEVICE_TABLE(acpi, sbs_device_ids);

106
static struct acpi_driver acpi_sbs_driver = {
L
Len Brown 已提交
107
	.name = "sbs",
108
	.class = ACPI_SBS_CLASS,
109
	.ids = sbs_device_ids,
110 111 112
	.ops = {
		.add = acpi_sbs_add,
		.remove = acpi_sbs_remove,
113
		.resume = acpi_sbs_resume,
114 115 116 117 118
		},
};

struct acpi_battery {
	struct acpi_sbs *sbs;
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	struct proc_dir_entry *proc_entry;
	int vscale;
	int ipscale;
	char manufacturer_name[ACPI_SBS_BLOCK_MAX];
	char device_name[ACPI_SBS_BLOCK_MAX];
	char device_chemistry[ACPI_SBS_BLOCK_MAX];
	u16 full_charge_capacity;
	u16 design_capacity;
	u16 design_voltage;
	u16 serial_number;
	u16 voltage_now;
	s16 current_now;
	u16 capacity_now;
	u16 state;
	u16 alarm_capacity;
	u16 mode;
	u8 id;
	u8 alive:1;
	u8 init_state:1;
	u8 present:1;
139 140 141 142
};

struct acpi_sbs {
	struct acpi_device *device;
143
	struct acpi_smb_hc *hc;
144
	struct mutex mutex;
145 146 147 148
	struct proc_dir_entry *ac_entry;
	struct acpi_battery battery[MAX_SBS_BAT];
	int zombie;
	struct timer_list update_timer;
149 150
	int run_cnt;
	int update_proc_flg;
151 152 153
	u8 batteries_supported;
	u8 manager_present:1;
	u8 charger_present:1;
154 155
};

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
static void acpi_sbs_update_time(void *data);

static int sbs_zombie(struct acpi_sbs *sbs)
{
	return (sbs->zombie);
}

static int sbs_mutex_lock(struct acpi_sbs *sbs)
{
	if (sbs_zombie(sbs)) {
		return -ENODEV;
	}
	mutex_lock(&sbs->mutex);
	return 0;
}

static void sbs_mutex_unlock(struct acpi_sbs *sbs)
{
	mutex_unlock(&sbs->mutex);
}

178 179 180 181
/* --------------------------------------------------------------------------
                            Smart Battery System Management
   -------------------------------------------------------------------------- */

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
static int acpi_check_update_proc(struct acpi_sbs *sbs)
{
	acpi_status status = AE_OK;

	if (update_time == 0) {
		sbs->update_proc_flg = 0;
		return 0;
	}
	if (sbs->update_proc_flg == 0) {
		status = acpi_os_execute(OSL_GPE_HANDLER,
					 acpi_sbs_update_time, sbs);
		if (status != AE_OK) {
			ACPI_EXCEPTION((AE_INFO, status,
					"acpi_os_execute() failed"));
			return 1;
		}
		sbs->update_proc_flg = 1;
	}
	return 0;
}
202 203 204 205 206 207 208

static int acpi_battery_get_present(struct acpi_battery *battery)
{
	s16 state;
	int result = 0;
	int is_present = 0;

209 210
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
				    ACPI_SBSM_SMBUS_ADDR, 0x01, (u8 *)&state);
211
	if (result) {
212
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
213
				"acpi_smbus_read() failed"));
214 215 216 217
	}
	if (!result) {
		is_present = (state & 0x000f) & (1 << battery->id);
	}
218
	battery->present = is_present;
219

220
	return result;
221 222 223 224
}

static int acpi_battery_select(struct acpi_battery *battery)
{
225
	struct acpi_sbs *sbs = battery->sbs;
226 227 228 229
	int result = 0;
	s16 state;
	int foo;

230
	if (sbs->manager_present) {
231 232 233 234 235

		/* Take special care not to knobble other nibbles of
		 * state (aka selector_state), since
		 * it causes charging to halt on SBSELs */

236 237
		result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
					 ACPI_SBSM_SMBUS_ADDR, 0x01, (u8 *)&state);
238
		if (result) {
239
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
240
					"acpi_smbus_read() failed"));
241 242 243 244
			goto end;
		}

		foo = (state & 0x0fff) | (1 << (battery->id + 12));
245 246
		result = acpi_smbus_write(battery->sbs->hc, SMBUS_WRITE_WORD,
					  ACPI_SBSM_SMBUS_ADDR, 0x01, (u8 *)&foo, 2);
247
		if (result) {
248
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
249
					"acpi_smbus_write() failed"));
250 251 252 253 254
			goto end;
		}
	}

      end:
255
	return result;
256 257 258 259 260 261 262
}

static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
{
	int result = 0;
	s16 battery_system_info;

263 264
	result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBSM_SMBUS_ADDR, 0x04,
				    (u8 *)&battery_system_info);
265
	if (result) {
266
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
267
				"acpi_smbus_read() failed"));
268 269
		goto end;
	}
270
	sbs->manager_present = 1;
271 272 273

      end:

274
	return result;
275 276 277 278 279 280 281 282
}

static int acpi_battery_get_info(struct acpi_battery *battery)
{
	int result = 0;
	s16 battery_mode;
	s16 specification_info;

283 284
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x03,
				    (u8 *)&battery_mode);
285
	if (result) {
286
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
287
				"acpi_smbus_read() failed"));
288 289
		goto end;
	}
290
	battery->mode = (battery_mode & 0x8000) >> 15;
291

292
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x10,
293
				    (u8 *)&battery->full_charge_capacity);
294
	if (result) {
295
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
296
				"acpi_smbus_read() failed"));
297 298 299
		goto end;
	}

300
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x18,
301
				    (u8 *)&battery->design_capacity);
302 303

	if (result) {
304
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
305
				"acpi_smbus_read() failed"));
306 307 308
		goto end;
	}

309
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x19,
310
				    (u8 *)&battery->design_voltage);
311
	if (result) {
312
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
313
				"acpi_smbus_read() failed"));
314 315 316
		goto end;
	}

317 318
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x1a,
				    (u8 *)&specification_info);
319
	if (result) {
320
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
321
				"acpi_smbus_read() failed"));
322 323 324 325 326
		goto end;
	}

	switch ((specification_info & 0x0f00) >> 8) {
	case 1:
327
		battery->vscale = 10;
328 329
		break;
	case 2:
330
		battery->vscale = 100;
331 332
		break;
	case 3:
333
		battery->vscale = 1000;
334 335
		break;
	default:
336
		battery->vscale = 1;
337 338 339 340
	}

	switch ((specification_info & 0xf000) >> 12) {
	case 1:
341
		battery->ipscale = 10;
342 343
		break;
	case 2:
344
		battery->ipscale = 100;
345 346
		break;
	case 3:
347
		battery->ipscale = 1000;
348 349
		break;
	default:
350
		battery->ipscale = 1;
351 352
	}

353
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x1c,
354
				    (u8 *)&battery->serial_number);
355
	if (result) {
356
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
357
				"acpi_smbus_read() failed"));
358 359 360
		goto end;
	}

361
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_BLOCK, ACPI_SB_SMBUS_ADDR, 0x20,
362
				   (u8 *)battery->manufacturer_name);
363
	if (result) {
364 365
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_str() failed"));
366 367 368
		goto end;
	}

369
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_BLOCK, ACPI_SB_SMBUS_ADDR, 0x21,
370
				   (u8 *)battery->device_name);
371
	if (result) {
372 373
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_str() failed"));
374 375 376
		goto end;
	}

377
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_BLOCK, ACPI_SB_SMBUS_ADDR, 0x22,
378
				   (u8 *)battery->device_chemistry);
379
	if (result) {
380 381
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_read_str() failed"));
382 383 384 385
		goto end;
	}

      end:
386
	return result;
387 388 389 390 391 392
}

static int acpi_battery_get_state(struct acpi_battery *battery)
{
	int result = 0;

393
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x09,
394
				    (u8 *)&battery->voltage_now);
395
	if (result) {
396
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
397
				"acpi_smbus_read() failed"));
398 399 400
		goto end;
	}

401
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x0a,
402
				    (u8 *)&battery->current_now);
403
	if (result) {
404
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
405
				"acpi_smbus_read() failed"));
406 407 408
		goto end;
	}

409
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x0f,
410
				    (u8 *)&battery->capacity_now);
411
	if (result) {
412
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
413
				"acpi_smbus_read() failed"));
414 415 416
		goto end;
	}

417
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x16,
418
				    (u8 *)&battery->state);
419
	if (result) {
420
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
421
				"acpi_smbus_read() failed"));
422 423 424 425
		goto end;
	}

      end:
426
	return result;
427 428 429 430 431 432
}

static int acpi_battery_get_alarm(struct acpi_battery *battery)
{
	int result = 0;

433
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x01,
434
				    (u8 *)&battery->alarm_capacity);
435
	if (result) {
436
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
437
				"acpi_smbus_read() failed"));
438 439 440 441 442
		goto end;
	}

      end:

443
	return result;
444 445 446 447 448 449 450 451 452 453 454
}

static int acpi_battery_set_alarm(struct acpi_battery *battery,
				  unsigned long alarm)
{
	int result = 0;
	s16 battery_mode;
	int foo;

	result = acpi_battery_select(battery);
	if (result) {
455 456
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_select() failed"));
457 458 459 460 461 462 463
		goto end;
	}

	/* If necessary, enable the alarm */

	if (alarm > 0) {
		result =
464 465
		    acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x03,
				       (u8 *)&battery_mode);
466
		if (result) {
467
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
468
					"acpi_smbus_read() failed"));
469 470 471
			goto end;
		}

472
		battery_mode &= 0xbfff;
473
		result =
474 475
		    acpi_smbus_write(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x01,
					(u8 *)&battery_mode, 2);
476
		if (result) {
477
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
478
					"acpi_smbus_write() failed"));
479 480 481 482
			goto end;
		}
	}

483 484 485
	foo = alarm / (battery->mode ? 10 : 1);
	result = acpi_smbus_write(battery->sbs->hc, SMBUS_READ_WORD, ACPI_SB_SMBUS_ADDR, 0x01,
				  (u8 *)&foo, 2);
486
	if (result) {
487
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
488
				"acpi_smbus_write() failed"));
489 490 491 492 493
		goto end;
	}

      end:

494
	return result;
495 496 497 498 499 500 501
}

static int acpi_battery_set_mode(struct acpi_battery *battery)
{
	int result = 0;
	s16 battery_mode;

502
	if (mode == DEF_CAPACITY_UNIT) {
503 504 505
		goto end;
	}

506
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
507
				 ACPI_SB_SMBUS_ADDR, 0x03, (u8 *)&battery_mode);
508
	if (result) {
509
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
510
				"acpi_smbus_read() failed"));
511 512 513
		goto end;
	}

514
	if (mode == MAH_CAPACITY_UNIT) {
515 516 517 518
		battery_mode &= 0x7fff;
	} else {
		battery_mode |= 0x8000;
	}
519
	result = acpi_smbus_write(battery->sbs->hc, SMBUS_READ_WORD,
520
				  ACPI_SB_SMBUS_ADDR, 0x03, (u8 *)&battery_mode, 2);
521
	if (result) {
522
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
523
				"acpi_smbus_write() failed"));
524 525 526
		goto end;
	}

527
	result = acpi_smbus_read(battery->sbs->hc, SMBUS_READ_WORD,
528
				 ACPI_SB_SMBUS_ADDR, 0x03, (u8 *)&battery_mode);
529
	if (result) {
530
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
531
				"acpi_smbus_read() failed"));
532 533 534 535
		goto end;
	}

      end:
536
	return result;
537 538 539 540 541 542 543 544
}

static int acpi_battery_init(struct acpi_battery *battery)
{
	int result = 0;

	result = acpi_battery_select(battery);
	if (result) {
545
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
546
				"acpi_battery_select() failed"));
547 548 549 550 551
		goto end;
	}

	result = acpi_battery_set_mode(battery);
	if (result) {
552 553
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_set_mode() failed"));
554 555 556 557 558
		goto end;
	}

	result = acpi_battery_get_info(battery);
	if (result) {
559 560
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_info() failed"));
561 562 563 564 565
		goto end;
	}

	result = acpi_battery_get_state(battery);
	if (result) {
566 567
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_state() failed"));
568 569 570 571 572
		goto end;
	}

	result = acpi_battery_get_alarm(battery);
	if (result) {
573 574
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_alarm() failed"));
575 576 577 578
		goto end;
	}

      end:
579
	return result;
580 581 582 583 584 585 586
}

static int acpi_ac_get_present(struct acpi_sbs *sbs)
{
	int result = 0;
	s16 charger_status;

587
	result = acpi_smbus_read(sbs->hc, SMBUS_READ_WORD, ACPI_SBC_SMBUS_ADDR, 0x13,
588
				 (u8 *)&charger_status);
589 590

	if (result) {
591
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
592
				"acpi_smbus_read() failed"));
593 594 595
		goto end;
	}

596
	sbs->charger_present = (charger_status & 0x8000) >> 15;
597 598 599

      end:

600
	return result;
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
}

/* --------------------------------------------------------------------------
                              FS Interface (/proc/acpi)
   -------------------------------------------------------------------------- */

/* Generic Routines */

static int
acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
			struct proc_dir_entry *parent_dir,
			char *dir_name,
			struct file_operations *info_fops,
			struct file_operations *state_fops,
			struct file_operations *alarm_fops, void *data)
{
	struct proc_dir_entry *entry = NULL;

	if (!*dir) {
		*dir = proc_mkdir(dir_name, parent_dir);
		if (!*dir) {
622 623
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"proc_mkdir() failed"));
624
			return -ENODEV;
625 626 627 628 629 630 631 632
		}
		(*dir)->owner = THIS_MODULE;
	}

	/* 'info' [R] */
	if (info_fops) {
		entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
		if (!entry) {
633 634
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"create_proc_entry() failed"));
635 636 637 638 639 640 641 642 643 644 645
		} else {
			entry->proc_fops = info_fops;
			entry->data = data;
			entry->owner = THIS_MODULE;
		}
	}

	/* 'state' [R] */
	if (state_fops) {
		entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
		if (!entry) {
646 647
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"create_proc_entry() failed"));
648 649 650 651 652 653 654 655 656 657 658
		} else {
			entry->proc_fops = state_fops;
			entry->data = data;
			entry->owner = THIS_MODULE;
		}
	}

	/* 'alarm' [R/W] */
	if (alarm_fops) {
		entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
		if (!entry) {
659 660
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"create_proc_entry() failed"));
661 662 663 664 665 666 667
		} else {
			entry->proc_fops = alarm_fops;
			entry->data = data;
			entry->owner = THIS_MODULE;
		}
	}

668
	return 0;
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
}

static void
acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
			   struct proc_dir_entry *parent_dir)
{

	if (*dir) {
		remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
		remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
		remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
		remove_proc_entry((*dir)->name, parent_dir);
		*dir = NULL;
	}

}

/* Smart Battery Interface */

static struct proc_dir_entry *acpi_battery_dir = NULL;

static int acpi_battery_read_info(struct seq_file *seq, void *offset)
{
692
	struct acpi_battery *battery = seq->private;
693
	struct acpi_sbs *sbs = battery->sbs;
694 695 696
	int cscale;
	int result = 0;

697
	if (sbs_mutex_lock(sbs)) {
698
		return -ENODEV;
699 700
	}

701 702 703
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
704

705 706
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
707
		if (result) {
708 709
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_update_run() failed"));
710 711 712
		}
	}

713
	if (battery->present) {
714 715 716 717 718 719
		seq_printf(seq, "present:                 yes\n");
	} else {
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

720 721
	if (battery->mode) {
		cscale = battery->vscale * battery->ipscale;
722
	} else {
723
		cscale = battery->ipscale;
724
	}
725
	seq_printf(seq, "design capacity:         %i%s\n",
726 727
		   battery->design_capacity * cscale,
		   battery->mode ? "0 mWh" : " mAh");
728

729
	seq_printf(seq, "last full capacity:      %i%s\n",
730 731
		   battery->full_charge_capacity * cscale,
		   battery->mode ? "0 mWh" : " mAh");
732 733 734 735

	seq_printf(seq, "battery technology:      rechargeable\n");

	seq_printf(seq, "design voltage:          %i mV\n",
736
		   battery->design_voltage * battery->vscale);
737 738 739 740 741 742 743

	seq_printf(seq, "design capacity warning: unknown\n");
	seq_printf(seq, "design capacity low:     unknown\n");
	seq_printf(seq, "capacity granularity 1:  unknown\n");
	seq_printf(seq, "capacity granularity 2:  unknown\n");

	seq_printf(seq, "model number:            %s\n",
744
		   battery->device_name);
745 746

	seq_printf(seq, "serial number:           %i\n",
747
		   battery->serial_number);
748 749

	seq_printf(seq, "battery type:            %s\n",
750
		   battery->device_chemistry);
751 752

	seq_printf(seq, "OEM info:                %s\n",
753
		   battery->manufacturer_name);
754 755 756

      end:

757
	sbs_mutex_unlock(sbs);
758

759
	return result;
760 761 762 763 764 765 766 767 768
}

static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_info, PDE(inode)->data);
}

static int acpi_battery_read_state(struct seq_file *seq, void *offset)
{
769 770
	struct acpi_battery *battery = seq->private;
	struct acpi_sbs *sbs = battery->sbs;
771 772 773 774
	int result = 0;
	int cscale;
	int foo;

775
	if (sbs_mutex_lock(sbs)) {
776
		return -ENODEV;
777 778
	}

779 780 781
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
782

783 784
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
785
		if (result) {
786 787
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_update_run() failed"));
788 789 790
		}
	}

791
	if (battery->present) {
792 793 794 795 796 797
		seq_printf(seq, "present:                 yes\n");
	} else {
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

798 799
	if (battery->mode) {
		cscale = battery->vscale * battery->ipscale;
800
	} else {
801
		cscale = battery->ipscale;
802 803
	}

804
	if (battery->state & 0x0010) {
805 806 807 808
		seq_printf(seq, "capacity state:          critical\n");
	} else {
		seq_printf(seq, "capacity state:          ok\n");
	}
V
Vladimir Lebedev 已提交
809

810 811 812
	foo = (s16) battery->current_now * battery->ipscale;
	if (battery->mode) {
		foo = foo * battery->design_voltage / 1000;
V
Vladimir Lebedev 已提交
813
	}
814
	if (battery->current_now < 0) {
815
		seq_printf(seq, "charging state:          discharging\n");
V
Vladimir Lebedev 已提交
816
		seq_printf(seq, "present rate:            %d %s\n",
817 818
			   -foo, battery->mode ? "mW" : "mA");
	} else if (battery->current_now > 0) {
819
		seq_printf(seq, "charging state:          charging\n");
V
Vladimir Lebedev 已提交
820
		seq_printf(seq, "present rate:            %d %s\n",
821
			   foo, battery->mode ? "mW" : "mA");
822 823 824
	} else {
		seq_printf(seq, "charging state:          charged\n");
		seq_printf(seq, "present rate:            0 %s\n",
825
			   battery->mode ? "mW" : "mA");
826 827
	}

828
	seq_printf(seq, "remaining capacity:      %i%s\n",
829 830
		   battery->capacity_now * cscale,
		   battery->mode ? "0 mWh" : " mAh");
831 832

	seq_printf(seq, "present voltage:         %i mV\n",
833
		   battery->voltage_now * battery->vscale);
834 835 836

      end:

837
	sbs_mutex_unlock(sbs);
838

839
	return result;
840 841 842 843 844 845 846 847 848
}

static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_state, PDE(inode)->data);
}

static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
{
849
	struct acpi_battery *battery = seq->private;
850
	struct acpi_sbs *sbs = battery->sbs;
851 852 853
	int result = 0;
	int cscale;

854
	if (sbs_mutex_lock(sbs)) {
855
		return -ENODEV;
856 857
	}

858 859 860
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
861

862 863
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
864
		if (result) {
865 866
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_update_run() failed"));
867 868 869
		}
	}

870
	if (!battery->present) {
871 872 873 874
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

875 876
	if (battery->mode) {
		cscale = battery->vscale * battery->ipscale;
877
	} else {
878
		cscale = battery->ipscale;
879 880 881
	}

	seq_printf(seq, "alarm:                   ");
882
	if (battery->alarm_capacity) {
883
		seq_printf(seq, "%i%s\n",
884 885
			   battery->alarm_capacity * cscale,
			   battery->mode ? "0 mWh" : " mAh");
886 887 888 889 890 891
	} else {
		seq_printf(seq, "disabled\n");
	}

      end:

892
	sbs_mutex_unlock(sbs);
893

894
	return result;
895 896 897 898 899 900
}

static ssize_t
acpi_battery_write_alarm(struct file *file, const char __user * buffer,
			 size_t count, loff_t * ppos)
{
901 902
	struct seq_file *seq = file->private_data;
	struct acpi_battery *battery = seq->private;
903
	struct acpi_sbs *sbs = battery->sbs;
904 905 906
	char alarm_string[12] = { '\0' };
	int result, old_alarm, new_alarm;

907
	if (sbs_mutex_lock(sbs)) {
908
		return -ENODEV;
909 910
	}

911 912 913
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
914

915
	if (!battery->present) {
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
		result = -ENODEV;
		goto end;
	}

	if (count > sizeof(alarm_string) - 1) {
		result = -EINVAL;
		goto end;
	}

	if (copy_from_user(alarm_string, buffer, count)) {
		result = -EFAULT;
		goto end;
	}

	alarm_string[count] = 0;

932
	old_alarm = battery->alarm_capacity;
933 934 935 936
	new_alarm = simple_strtoul(alarm_string, NULL, 0);

	result = acpi_battery_set_alarm(battery, new_alarm);
	if (result) {
937 938
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_set_alarm() failed"));
939
		acpi_battery_set_alarm(battery, old_alarm);
940 941 942 943
		goto end;
	}
	result = acpi_battery_get_alarm(battery);
	if (result) {
944 945
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_alarm() failed"));
946
		acpi_battery_set_alarm(battery, old_alarm);
947 948 949 950
		goto end;
	}

      end:
951
	sbs_mutex_unlock(sbs);
952 953

	if (result) {
954
		return result;
955
	} else {
956
		return count;
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
	}
}

static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
}

static struct file_operations acpi_battery_info_fops = {
	.open = acpi_battery_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static struct file_operations acpi_battery_state_fops = {
	.open = acpi_battery_state_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

static struct file_operations acpi_battery_alarm_fops = {
	.open = acpi_battery_alarm_open_fs,
	.read = seq_read,
	.write = acpi_battery_write_alarm,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

/* Legacy AC Adapter Interface */

static struct proc_dir_entry *acpi_ac_dir = NULL;

static int acpi_ac_read_state(struct seq_file *seq, void *offset)
{
996
	struct acpi_sbs *sbs = seq->private;
997 998
	int result;

999
	if (sbs_mutex_lock(sbs)) {
1000
		return -ENODEV;
1001 1002
	}

1003 1004
	if (update_time == 0) {
		result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1005
		if (result) {
1006 1007
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_update_run() failed"));
1008 1009 1010 1011
		}
	}

	seq_printf(seq, "state:                   %s\n",
1012
		   sbs->charger_present ? "on-line" : "off-line");
1013

1014
	sbs_mutex_unlock(sbs);
1015

1016
	return 0;
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
}

static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_ac_read_state, PDE(inode)->data);
}

static struct file_operations acpi_ac_state_fops = {
	.open = acpi_ac_state_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
	.owner = THIS_MODULE,
};

/* --------------------------------------------------------------------------
                                 Driver Interface
   -------------------------------------------------------------------------- */

/* Smart Battery */

static int acpi_battery_add(struct acpi_sbs *sbs, int id)
{
	int is_present;
	int result;
	char dir_name[32];
	struct acpi_battery *battery;

	battery = &sbs->battery[id];

	battery->alive = 0;

	battery->init_state = 0;
	battery->id = id;
	battery->sbs = sbs;

	result = acpi_battery_select(battery);
	if (result) {
1055 1056
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_select() failed"));
1057 1058 1059 1060 1061
		goto end;
	}

	result = acpi_battery_get_present(battery);
	if (result) {
1062 1063
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_battery_get_present() failed"));
1064 1065 1066
		goto end;
	}

1067
	is_present = battery->present;
1068 1069 1070 1071

	if (is_present) {
		result = acpi_battery_init(battery);
		if (result) {
1072 1073
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_init() failed"));
1074 1075 1076 1077 1078
			goto end;
		}
		battery->init_state = 1;
	}

1079
	sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1080

1081
	result = acpi_sbs_generic_add_fs(&battery->proc_entry,
1082 1083 1084 1085 1086 1087
					 acpi_battery_dir,
					 dir_name,
					 &acpi_battery_info_fops,
					 &acpi_battery_state_fops,
					 &acpi_battery_alarm_fops, battery);
	if (result) {
1088 1089
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_generic_add_fs() failed"));
1090 1091 1092 1093
		goto end;
	}
	battery->alive = 1;

1094 1095
	printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
	       ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1096
	       sbs->battery->present ? "present" : "absent");
1097

1098
      end:
1099
	return result;
1100 1101 1102 1103 1104
}

static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
{

1105 1106
	if (sbs->battery[id].proc_entry) {
		acpi_sbs_generic_remove_fs(&(sbs->battery[id].proc_entry),
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
					   acpi_battery_dir);
	}
}

static int acpi_ac_add(struct acpi_sbs *sbs)
{
	int result;

	result = acpi_ac_get_present(sbs);
	if (result) {
1117 1118
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_ac_get_present() failed"));
1119 1120 1121 1122 1123 1124 1125 1126
		goto end;
	}

	result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
					 acpi_ac_dir,
					 ACPI_AC_DIR_NAME,
					 NULL, &acpi_ac_state_fops, NULL, sbs);
	if (result) {
1127 1128
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_generic_add_fs() failed"));
1129 1130 1131
		goto end;
	}

1132 1133
	printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
	       ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1134
	       ACPI_AC_DIR_NAME, sbs->charger_present ? "on-line" : "off-line");
1135

1136 1137
      end:

1138
	return result;
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
}

static void acpi_ac_remove(struct acpi_sbs *sbs)
{

	if (sbs->ac_entry) {
		acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
	}
}

1149
static void acpi_sbs_update_time_run(unsigned long data)
1150
{
1151
	acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1152 1153
}

1154
static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1155 1156
{
	struct acpi_battery *battery;
1157 1158
	int result = 0, cnt;
	int old_ac_present = -1;
1159
	int old_present = -1;
1160
	int new_ac_present = -1;
1161
	int new_present = -1;
1162
	int id_min = 0, id_max = MAX_SBS_BAT - 1;
1163
	char dir_name[32];
1164 1165
	int do_battery_init = 0, do_ac_init = 0;
	int old_remaining_capacity = 0;
A
Adrian Bunk 已提交
1166
	int update_battery = 1;
1167 1168 1169 1170 1171
	int up_tm = update_time;

	if (sbs_zombie(sbs)) {
		goto end;
	}
1172

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	if (id >= 0) {
		id_min = id_max = id;
	}

	if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
		cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
		if (sbs->run_cnt % cnt != 0) {
			update_battery = 0;
		}
	}

	sbs->run_cnt++;

1186 1187 1188 1189 1190
	if (!update_battery) {
		goto end;
	}

	old_ac_present = sbs->charger_present;
1191 1192 1193

	result = acpi_ac_get_present(sbs);
	if (result) {
1194 1195
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_ac_get_present() failed"));
1196 1197
	}

1198
	new_ac_present = sbs->charger_present;
1199 1200

	do_ac_init = (old_ac_present != new_ac_present);
1201 1202 1203
	if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
		do_ac_init = 1;
	}
1204

1205
	if (do_ac_init) {
1206
		result = acpi_bus_generate_proc_event4(ACPI_AC_CLASS,
1207
						 ACPI_AC_DIR_NAME,
1208 1209
						 ACPI_SBS_AC_NOTIFY_STATUS,
						 new_ac_present);
1210 1211
		if (result) {
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1212
					"acpi_bus_generate_event4() failed"));
1213
		}
1214 1215 1216
		acpi_bus_generate_netlink_event(ACPI_AC_CLASS, ACPI_AC_DIR_NAME,
						ACPI_SBS_AC_NOTIFY_STATUS,
						new_ac_present);
1217 1218 1219 1220 1221 1222 1223 1224 1225
	}

	if (data_type == DATA_TYPE_COMMON) {
		if (!do_ac_init && !update_battery) {
			goto end;
		}
	}

	if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1226 1227 1228
		goto end;
	}

1229
	for (id = id_min; id <= id_max; id++) {
1230 1231 1232 1233 1234
		battery = &sbs->battery[id];
		if (battery->alive == 0) {
			continue;
		}

1235
		old_remaining_capacity = battery->capacity_now;
1236

1237
		old_present = battery->present;
1238 1239 1240

		result = acpi_battery_select(battery);
		if (result) {
1241 1242
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_select() failed"));
1243 1244 1245 1246
		}

		result = acpi_battery_get_present(battery);
		if (result) {
1247 1248
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_get_present() failed"));
1249 1250
		}

1251
		new_present = battery->present;
1252

1253 1254 1255
		do_battery_init = ((old_present != new_present)
				   && new_present);
		if (!new_present)
1256 1257
			goto event;
		if (do_ac_init || do_battery_init) {
1258 1259
			result = acpi_battery_init(battery);
			if (result) {
1260 1261 1262
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
						"acpi_battery_init() "
						"failed"));
1263 1264
			}
		}
1265
		if (sbs_zombie(sbs)) {
1266 1267
			goto end;
		}
1268 1269 1270

		if ((data_type == DATA_TYPE_COMMON
		     || data_type == DATA_TYPE_INFO)
1271
		    && new_present) {
1272
			result = acpi_battery_get_info(battery);
1273
			if (result) {
1274
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1275
						"acpi_battery_get_info() failed"));
1276
			}
1277 1278 1279 1280 1281 1282 1283
		}
		if (data_type == DATA_TYPE_INFO) {
			continue;
		}
		if (sbs_zombie(sbs)) {
			goto end;
		}
1284

1285 1286
		if ((data_type == DATA_TYPE_COMMON
		     || data_type == DATA_TYPE_STATE)
1287
		    && new_present) {
1288 1289
			result = acpi_battery_get_state(battery);
			if (result) {
1290
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1291
						"acpi_battery_get_state() failed"));
1292 1293
			}
		}
1294 1295
		if (data_type == DATA_TYPE_STATE) {
			goto event;
1296
		}
1297 1298
		if (sbs_zombie(sbs)) {
			goto end;
1299 1300
		}

1301 1302
		if ((data_type == DATA_TYPE_COMMON
		     || data_type == DATA_TYPE_ALARM)
1303
		    && new_present) {
1304
			result = acpi_battery_get_alarm(battery);
1305
			if (result) {
1306
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1307
						"acpi_battery_get_alarm() "
1308
						"failed"));
1309 1310
			}
		}
1311 1312 1313 1314 1315 1316 1317 1318 1319
		if (data_type == DATA_TYPE_ALARM) {
			continue;
		}
		if (sbs_zombie(sbs)) {
			goto end;
		}

	      event:

1320
		if (old_present != new_present || do_ac_init ||
1321
		    old_remaining_capacity !=
1322
		    battery->capacity_now) {
1323
			sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1324
			result = acpi_bus_generate_proc_event4(ACPI_BATTERY_CLASS,
1325
							 dir_name,
1326
							 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1327
							 new_present);
1328 1329
			acpi_bus_generate_netlink_event(ACPI_BATTERY_CLASS, dir_name,
							ACPI_SBS_BATTERY_NOTIFY_STATUS,
1330
							new_present);
1331
			if (result) {
1332
				ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1333
						"acpi_bus_generate_proc_event4() "
1334
						"failed"));
1335 1336 1337 1338 1339
			}
		}
	}

      end:
1340

1341
	return result;
1342 1343
}

1344
static void acpi_sbs_update_time(void *data)
1345 1346 1347 1348
{
	struct acpi_sbs *sbs = data;
	unsigned long delay = -1;
	int result;
1349
	unsigned int up_tm = update_time;
1350

1351 1352
	if (sbs_mutex_lock(sbs))
		return;
1353

1354
	result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1355
	if (result) {
1356 1357
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_sbs_update_run() failed"));
1358 1359
	}

1360
	if (sbs_zombie(sbs)) {
1361 1362 1363
		goto end;
	}

1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	if (!up_tm) {
		if (timer_pending(&sbs->update_timer))
			del_timer(&sbs->update_timer);
	} else {
		delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
		delay = jiffies + HZ * delay;
		if (timer_pending(&sbs->update_timer)) {
			mod_timer(&sbs->update_timer, delay);
		} else {
			sbs->update_timer.data = (unsigned long)data;
			sbs->update_timer.function = acpi_sbs_update_time_run;
			sbs->update_timer.expires = delay;
			add_timer(&sbs->update_timer);
		}
1378 1379 1380
	}

      end:
1381 1382

	sbs_mutex_unlock(sbs);
1383 1384 1385 1386 1387
}

static int acpi_sbs_add(struct acpi_device *device)
{
	struct acpi_sbs *sbs = NULL;
1388
	int result = 0, remove_result = 0;
1389
	int id;
1390

1391
	sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1392
	if (!sbs) {
1393 1394 1395
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
		result = -ENOMEM;
		goto end;
1396 1397
	}

1398 1399 1400 1401
	mutex_init(&sbs->mutex);

	sbs_mutex_lock(sbs);

1402
	sbs->device = device;
1403
	sbs->hc = acpi_driver_data(device->parent);
1404 1405 1406 1407 1408 1409 1410

	strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
	acpi_driver_data(device) = sbs;

	result = acpi_ac_add(sbs);
	if (result) {
1411
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1412 1413
		goto end;
	}
1414

1415 1416
	acpi_sbsm_get_info(sbs);

1417
	if (!sbs->manager_present) {
1418 1419
		result = acpi_battery_add(sbs, 0);
		if (result) {
1420 1421
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_battery_add() failed"));
1422 1423 1424 1425
			goto end;
		}
	} else {
		for (id = 0; id < MAX_SBS_BAT; id++) {
1426
			if ((sbs->batteries_supported & (1 << id))) {
1427 1428
				result = acpi_battery_add(sbs, id);
				if (result) {
1429
					ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1430
							"acpi_battery_add() failed"));
1431 1432 1433 1434 1435 1436 1437
					goto end;
				}
			}
		}
	}

	init_timer(&sbs->update_timer);
1438 1439 1440
	result = acpi_check_update_proc(sbs);
	if (result)
		goto end;
1441 1442

      end:
1443 1444 1445

	sbs_mutex_unlock(sbs);

1446
	if (result) {
1447 1448 1449 1450 1451
		remove_result = acpi_sbs_remove(device, 0);
		if (remove_result) {
			ACPI_EXCEPTION((AE_INFO, AE_ERROR,
					"acpi_sbs_remove() failed"));
		}
1452 1453
	}

1454
	return result;
1455 1456
}

1457
static int acpi_sbs_remove(struct acpi_device *device, int type)
1458
{
L
Len Brown 已提交
1459
	struct acpi_sbs *sbs;
1460 1461
	int id;

1462 1463 1464 1465
	if (!device) {
		return -EINVAL;
	}

1466
	sbs = acpi_driver_data(device);
1467
	if (!sbs) {
1468
		return -EINVAL;
1469 1470
	}

1471 1472
	sbs_mutex_lock(sbs);

1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	sbs->zombie = 1;
	del_timer_sync(&sbs->update_timer);
	acpi_os_wait_events_complete(NULL);
	del_timer_sync(&sbs->update_timer);

	for (id = 0; id < MAX_SBS_BAT; id++) {
		acpi_battery_remove(sbs, id);
	}

	acpi_ac_remove(sbs);

1484 1485 1486
	sbs_mutex_unlock(sbs);

	mutex_destroy(&sbs->mutex);
1487

1488 1489
	kfree(sbs);

1490
	return 0;
1491 1492
}

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
static void acpi_sbs_rmdirs(void)
{
	if (acpi_ac_dir) {
		acpi_unlock_ac_dir(acpi_ac_dir);
		acpi_ac_dir = NULL;
	}
	if (acpi_battery_dir) {
		acpi_unlock_battery_dir(acpi_battery_dir);
		acpi_battery_dir = NULL;
	}
}

static int acpi_sbs_resume(struct acpi_device *device)
{
	struct acpi_sbs *sbs;

	if (!device)
		return -EINVAL;

	sbs = device->driver_data;

	sbs->run_cnt = 0;

	return 0;
}

1519 1520 1521 1522
static int __init acpi_sbs_init(void)
{
	int result = 0;

1523 1524 1525
	if (acpi_disabled)
		return -ENODEV;

1526 1527 1528
	if (mode != DEF_CAPACITY_UNIT
	    && mode != MAH_CAPACITY_UNIT
	    && mode != MWH_CAPACITY_UNIT) {
1529
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1530
				"invalid mode = %d", mode));
1531
		return -EINVAL;
1532 1533 1534 1535
	}

	acpi_ac_dir = acpi_lock_ac_dir();
	if (!acpi_ac_dir) {
1536 1537
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_lock_ac_dir() failed"));
1538
		return -ENODEV;
1539 1540 1541 1542
	}

	acpi_battery_dir = acpi_lock_battery_dir();
	if (!acpi_battery_dir) {
1543 1544
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_lock_battery_dir() failed"));
1545
		acpi_sbs_rmdirs();
1546
		return -ENODEV;
1547 1548 1549 1550
	}

	result = acpi_bus_register_driver(&acpi_sbs_driver);
	if (result < 0) {
1551 1552
		ACPI_EXCEPTION((AE_INFO, AE_ERROR,
				"acpi_bus_register_driver() failed"));
1553
		acpi_sbs_rmdirs();
1554
		return -ENODEV;
1555 1556
	}

1557
	return 0;
1558 1559 1560 1561 1562 1563
}

static void __exit acpi_sbs_exit(void)
{
	acpi_bus_unregister_driver(&acpi_sbs_driver);

1564
	acpi_sbs_rmdirs();
1565

1566
	return;
1567 1568 1569 1570
}

module_init(acpi_sbs_init);
module_exit(acpi_sbs_exit);