battery.c 23.3 KB
Newer Older
L
Linus Torvalds 已提交
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 *  acpi_battery.c - ACPI Battery Driver ($Revision: 37 $)
 *
 *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
 *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  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/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <asm/uaccess.h>

#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF

#define ACPI_BATTERY_COMPONENT		0x00040000
#define ACPI_BATTERY_CLASS		"battery"
#define ACPI_BATTERY_DEVICE_NAME	"Battery"
#define ACPI_BATTERY_NOTIFY_STATUS	0x80
#define ACPI_BATTERY_NOTIFY_INFO	0x81
#define ACPI_BATTERY_UNITS_WATTS	"mW"
#define ACPI_BATTERY_UNITS_AMPS		"mA"

#define _COMPONENT		ACPI_BATTERY_COMPONENT
48 49 50 51 52 53 54

#define ACPI_BATTERY_UPDATE_TIME	0

#define ACPI_BATTERY_NONE_UPDATE	0
#define ACPI_BATTERY_EASY_UPDATE	1
#define ACPI_BATTERY_INIT_UPDATE	2

55
ACPI_MODULE_NAME("battery");
L
Linus Torvalds 已提交
56

57
MODULE_AUTHOR("Paul Diefenbaugh");
58
MODULE_DESCRIPTION("ACPI Battery Driver");
L
Linus Torvalds 已提交
59 60
MODULE_LICENSE("GPL");

61 62 63 64 65
static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME;

/* 0 - every time, > 0 - by update_time */
module_param(update_time, uint, 0644);

66 67 68
extern struct proc_dir_entry *acpi_lock_battery_dir(void);
extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);

L
Len Brown 已提交
69 70
static int acpi_battery_add(struct acpi_device *device);
static int acpi_battery_remove(struct acpi_device *device, int type);
71
static int acpi_battery_resume(struct acpi_device *device);
L
Linus Torvalds 已提交
72

73 74 75 76 77 78
static const struct acpi_device_id battery_device_ids[] = {
	{"PNP0C0A", 0},
	{"", 0},
};
MODULE_DEVICE_TABLE(acpi, battery_device_ids);

L
Linus Torvalds 已提交
79
static struct acpi_driver acpi_battery_driver = {
L
Len Brown 已提交
80
	.name = "battery",
L
Len Brown 已提交
81
	.class = ACPI_BATTERY_CLASS,
82
	.ids = battery_device_ids,
L
Len Brown 已提交
83 84
	.ops = {
		.add = acpi_battery_add,
85
		.resume = acpi_battery_resume,
L
Len Brown 已提交
86 87
		.remove = acpi_battery_remove,
		},
L
Linus Torvalds 已提交
88 89
};

90
enum acpi_battery_files {
91 92 93 94 95 96
	ACPI_BATTERY_INFO = 0,
	ACPI_BATTERY_STATE,
	ACPI_BATTERY_ALARM,
	ACPI_BATTERY_NUMFILES,
};

L
Linus Torvalds 已提交
97
struct acpi_battery {
V
Vladimir Lebedev 已提交
98
	struct acpi_device *device;
99
	struct mutex lock;
L
Len Brown 已提交
100
	unsigned long alarm;
101
	unsigned long update_time[ACPI_BATTERY_NUMFILES];
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	int state;
	int present_rate;
	int remaining_capacity;
	int present_voltage;
	int power_unit;
	int design_capacity;
	int last_full_capacity;
	int technology;
	int design_voltage;
	int design_capacity_warning;
	int design_capacity_low;
	int capacity_granularity_1;
	int capacity_granularity_2;
	char model_number[32];
	char serial_number[32];
	char type[32];
	char oem_info[32];
	u8 present_prev;
	u8 alarm_present;
	u8 init_update;
	u8 update[ACPI_BATTERY_NUMFILES];
L
Linus Torvalds 已提交
123 124
};

125
inline int acpi_battery_present(struct acpi_battery *battery)
126
{
127 128
	return battery->device->status.battery_present;
}
129

130 131
inline char *acpi_battery_power_units(struct acpi_battery *battery)
{
132
	if (battery->power_unit)
133 134 135
		return ACPI_BATTERY_UNITS_AMPS;
	else
		return ACPI_BATTERY_UNITS_WATTS;
136 137
}

138
inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
139
{
140
	return battery->device->handle;
141 142
}

143 144 145 146
/* --------------------------------------------------------------------------
                               Battery Management
   -------------------------------------------------------------------------- */

147 148 149 150 151 152
static void acpi_battery_check_result(struct acpi_battery *battery, int result)
{
	if (!battery)
		return;

	if (result) {
153
		battery->init_update = 1;
154 155 156
	}
}

157 158 159 160
struct acpi_offsets {
	size_t offset;		/* offset inside struct acpi_sbs_battery */
	u8 mode;		/* int or string? */
};
161

162 163 164 165 166 167
static struct acpi_offsets state_offsets[] = {
	{offsetof(struct acpi_battery, state), 0},
	{offsetof(struct acpi_battery, present_rate), 0},
	{offsetof(struct acpi_battery, remaining_capacity), 0},
	{offsetof(struct acpi_battery, present_voltage), 0},
};
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
static struct acpi_offsets info_offsets[] = {
	{offsetof(struct acpi_battery, power_unit), 0},
	{offsetof(struct acpi_battery, design_capacity), 0},
	{offsetof(struct acpi_battery, last_full_capacity), 0},
	{offsetof(struct acpi_battery, technology), 0},
	{offsetof(struct acpi_battery, design_voltage), 0},
	{offsetof(struct acpi_battery, design_capacity_warning), 0},
	{offsetof(struct acpi_battery, design_capacity_low), 0},
	{offsetof(struct acpi_battery, capacity_granularity_1), 0},
	{offsetof(struct acpi_battery, capacity_granularity_2), 0},
	{offsetof(struct acpi_battery, model_number), 1},
	{offsetof(struct acpi_battery, serial_number), 1},
	{offsetof(struct acpi_battery, type), 1},
	{offsetof(struct acpi_battery, oem_info), 1},
};
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static int extract_package(struct acpi_battery *battery,
			   union acpi_object *package,
			   struct acpi_offsets *offsets, int num)
{
	int i, *x;
	union acpi_object *element;
	if (package->type != ACPI_TYPE_PACKAGE)
		return -EFAULT;
	for (i = 0; i < num; ++i) {
		if (package->package.count <= i)
			return -EFAULT;
		element = &package->package.elements[i];
		if (offsets[i].mode) {
			if (element->type != ACPI_TYPE_STRING &&
			    element->type != ACPI_TYPE_BUFFER)
				return -EFAULT;
			strncpy((u8 *)battery + offsets[i].offset,
				element->string.pointer, 32);
		} else {
			if (element->type != ACPI_TYPE_INTEGER)
				return -EFAULT;
			x = (int *)((u8 *)battery + offsets[i].offset);
			*x = element->integer.value;
		}
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	}
	return 0;
}

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

	result = acpi_bus_get_status(battery->device);
	if (result) {
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
		return -ENODEV;
	}
	return result;
}

static int acpi_battery_get_info(struct acpi_battery *battery)
L
Linus Torvalds 已提交
226
{
L
Len Brown 已提交
227 228 229
	int result = 0;
	acpi_status status = 0;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
L
Linus Torvalds 已提交
230

231
	battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
232 233
	if (!acpi_battery_present(battery))
		return 0;
234 235 236 237
	mutex_lock(&battery->lock);
	status = acpi_evaluate_object(acpi_battery_handle(battery), "_BIF",
				      NULL, &buffer);
	mutex_unlock(&battery->lock);
L
Linus Torvalds 已提交
238
	if (ACPI_FAILURE(status)) {
239
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
240
		return -ENODEV;
L
Linus Torvalds 已提交
241
	}
242 243
	result = extract_package(battery, buffer.pointer,
				 info_offsets, ARRAY_SIZE(info_offsets));
244
	kfree(buffer.pointer);
245
	return result;
L
Linus Torvalds 已提交
246 247
}

248
static int acpi_battery_get_state(struct acpi_battery *battery)
L
Linus Torvalds 已提交
249
{
L
Len Brown 已提交
250 251 252
	int result = 0;
	acpi_status status = 0;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
L
Linus Torvalds 已提交
253

254
	battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
L
Linus Torvalds 已提交
255

256 257
	if (!acpi_battery_present(battery))
		return 0;
L
Linus Torvalds 已提交
258

259 260 261 262
	mutex_lock(&battery->lock);
	status = acpi_evaluate_object(acpi_battery_handle(battery), "_BST",
				      NULL, &buffer);
	mutex_unlock(&battery->lock);
263

L
Linus Torvalds 已提交
264
	if (ACPI_FAILURE(status)) {
265
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
266
		return -ENODEV;
L
Linus Torvalds 已提交
267
	}
268 269
	result = extract_package(battery, buffer.pointer,
				 state_offsets, ARRAY_SIZE(state_offsets));
270
	kfree(buffer.pointer);
271 272
	return result;
}
L
Linus Torvalds 已提交
273

274 275
static int acpi_battery_get_alarm(struct acpi_battery *battery)
{
276
	battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
L
Linus Torvalds 已提交
277

278
	return 0;
L
Linus Torvalds 已提交
279 280
}

V
Vladimir Lebedev 已提交
281 282
static int acpi_battery_set_alarm(struct acpi_battery *battery,
				  unsigned long alarm)
L
Linus Torvalds 已提交
283
{
L
Len Brown 已提交
284 285 286
	acpi_status status = 0;
	union acpi_object arg0 = { ACPI_TYPE_INTEGER };
	struct acpi_object_list arg_list = { 1, &arg0 };
L
Linus Torvalds 已提交
287

288
	battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
L
Linus Torvalds 已提交
289

290 291
	if (!acpi_battery_present(battery))
		return -ENODEV;
L
Linus Torvalds 已提交
292

293
	if (!battery->alarm_present)
294
		return -ENODEV;
L
Linus Torvalds 已提交
295 296 297

	arg0.integer.value = alarm;

298 299 300 301
	mutex_lock(&battery->lock);
	status = acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
				      &arg_list, NULL);
	mutex_unlock(&battery->lock);
L
Linus Torvalds 已提交
302
	if (ACPI_FAILURE(status))
303
		return -ENODEV;
L
Linus Torvalds 已提交
304 305 306 307 308

	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));

	battery->alarm = alarm;

309
	return 0;
L
Linus Torvalds 已提交
310 311
}

312
static int acpi_battery_init_alarm(struct acpi_battery *battery)
L
Linus Torvalds 已提交
313
{
L
Len Brown 已提交
314 315 316
	int result = 0;
	acpi_status status = AE_OK;
	acpi_handle handle = NULL;
317
	unsigned long alarm = battery->alarm;
L
Len Brown 已提交
318

319
	/* See if alarms are supported, and if so, set default */
L
Linus Torvalds 已提交
320

321 322
	status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
	if (ACPI_SUCCESS(status)) {
323 324 325
		battery->alarm_present = 1;
		if (!alarm) {
			alarm = battery->design_capacity_warning;
326 327 328 329 330
		}
		result = acpi_battery_set_alarm(battery, alarm);
		if (result)
			goto end;
	} else {
331
		battery->alarm_present = 0;
332
	}
L
Linus Torvalds 已提交
333

334
      end:
L
Linus Torvalds 已提交
335

336 337
	return result;
}
L
Linus Torvalds 已提交
338

339 340 341
static int acpi_battery_init_update(struct acpi_battery *battery)
{
	int result = 0;
L
Linus Torvalds 已提交
342

343 344 345
	result = acpi_battery_get_status(battery);
	if (result)
		return result;
L
Linus Torvalds 已提交
346

347
	battery->present_prev = acpi_battery_present(battery);
L
Linus Torvalds 已提交
348

349 350 351 352 353
	if (acpi_battery_present(battery)) {
		result = acpi_battery_get_info(battery);
		if (result)
			return result;
		result = acpi_battery_get_state(battery);
L
Linus Torvalds 已提交
354
		if (result)
355
			return result;
L
Linus Torvalds 已提交
356

357 358 359 360 361 362 363
		acpi_battery_init_alarm(battery);
	}

	return result;
}

static int acpi_battery_update(struct acpi_battery *battery,
V
Vladimir Lebedev 已提交
364
			       int update, int *update_result_ptr)
365 366 367
{
	int result = 0;
	int update_result = ACPI_BATTERY_NONE_UPDATE;
L
Linus Torvalds 已提交
368

369 370 371
	if (!acpi_battery_present(battery)) {
		update = 1;
	}
L
Linus Torvalds 已提交
372

373
	if (battery->init_update) {
374 375
		result = acpi_battery_init_update(battery);
		if (result)
376
			goto end;
377 378 379 380
		update_result = ACPI_BATTERY_INIT_UPDATE;
	} else if (update) {
		result = acpi_battery_get_status(battery);
		if (result)
381
			goto end;
382 383
		if ((!battery->present_prev & acpi_battery_present(battery))
		    || (battery->present_prev & !acpi_battery_present(battery))) {
384 385
			result = acpi_battery_init_update(battery);
			if (result)
386
				goto end;
387 388 389
			update_result = ACPI_BATTERY_INIT_UPDATE;
		} else {
			update_result = ACPI_BATTERY_EASY_UPDATE;
L
Linus Torvalds 已提交
390 391 392
		}
	}

393
      end:
L
Linus Torvalds 已提交
394

395
	battery->init_update = (result != 0);
L
Linus Torvalds 已提交
396

397
	*update_result_ptr = update_result;
L
Linus Torvalds 已提交
398

399
	return result;
L
Linus Torvalds 已提交
400 401
}

402
static void acpi_battery_notify_update(struct acpi_battery *battery)
403
{
404 405
	acpi_battery_get_status(battery);

406
	if (battery->init_update) {
407 408 409
		return;
	}

410
	if ((!battery->present_prev &
411
	     acpi_battery_present(battery)) ||
412
	    (battery->present_prev &
413
	     !acpi_battery_present(battery))) {
414
		battery->init_update = 1;
415
	} else {
416 417 418
		battery->update[ACPI_BATTERY_INFO] = 1;
		battery->update[ACPI_BATTERY_STATE] = 1;
		battery->update[ACPI_BATTERY_ALARM] = 1;
419 420 421
	}
}

L
Linus Torvalds 已提交
422 423 424 425
/* --------------------------------------------------------------------------
                              FS Interface (/proc)
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
426
static struct proc_dir_entry *acpi_battery_dir;
427

428
static int acpi_battery_print_info(struct seq_file *seq, int result)
L
Linus Torvalds 已提交
429
{
430
	struct acpi_battery *battery = seq->private;
L
Len Brown 已提交
431
	char *units = "?";
L
Linus Torvalds 已提交
432

433
	if (result)
L
Linus Torvalds 已提交
434 435
		goto end;

436
	if (acpi_battery_present(battery))
L
Linus Torvalds 已提交
437 438 439 440 441 442
		seq_printf(seq, "present:                 yes\n");
	else {
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

443 444 445
	/* Battery Units */

	units = acpi_battery_power_units(battery);
L
Len Brown 已提交
446

447
	if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
448 449 450
		seq_printf(seq, "design capacity:         unknown\n");
	else
		seq_printf(seq, "design capacity:         %d %sh\n",
451
			   (u32) battery->design_capacity, units);
L
Linus Torvalds 已提交
452

453
	if (battery->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
454 455 456
		seq_printf(seq, "last full capacity:      unknown\n");
	else
		seq_printf(seq, "last full capacity:      %d %sh\n",
457
			   (u32) battery->last_full_capacity, units);
L
Linus Torvalds 已提交
458

459
	switch ((u32) battery->technology) {
L
Linus Torvalds 已提交
460 461 462 463 464 465 466 467 468 469 470
	case 0:
		seq_printf(seq, "battery technology:      non-rechargeable\n");
		break;
	case 1:
		seq_printf(seq, "battery technology:      rechargeable\n");
		break;
	default:
		seq_printf(seq, "battery technology:      unknown\n");
		break;
	}

471
	if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
472 473 474
		seq_printf(seq, "design voltage:          unknown\n");
	else
		seq_printf(seq, "design voltage:          %d mV\n",
475
			   (u32) battery->design_voltage);
L
Linus Torvalds 已提交
476
	seq_printf(seq, "design capacity warning: %d %sh\n",
477
		   (u32) battery->design_capacity_warning, units);
L
Linus Torvalds 已提交
478
	seq_printf(seq, "design capacity low:     %d %sh\n",
479
		   (u32) battery->design_capacity_low, units);
L
Linus Torvalds 已提交
480
	seq_printf(seq, "capacity granularity 1:  %d %sh\n",
481
		   (u32) battery->capacity_granularity_1, units);
L
Linus Torvalds 已提交
482
	seq_printf(seq, "capacity granularity 2:  %d %sh\n",
483 484 485 486 487
		   (u32) battery->capacity_granularity_2, units);
	seq_printf(seq, "model number:            %s\n", battery->model_number);
	seq_printf(seq, "serial number:           %s\n", battery->serial_number);
	seq_printf(seq, "battery type:            %s\n", battery->type);
	seq_printf(seq, "OEM info:                %s\n", battery->oem_info);
L
Len Brown 已提交
488 489

      end:
L
Linus Torvalds 已提交
490

491 492 493 494 495 496
	if (result)
		seq_printf(seq, "ERROR: Unable to read battery info\n");

	return result;
}

497
static int acpi_battery_print_state(struct seq_file *seq, int result)
L
Linus Torvalds 已提交
498
{
499
	struct acpi_battery *battery = seq->private;
L
Len Brown 已提交
500
	char *units = "?";
L
Linus Torvalds 已提交
501

502
	if (result)
L
Linus Torvalds 已提交
503 504
		goto end;

505
	if (acpi_battery_present(battery))
L
Linus Torvalds 已提交
506 507 508 509 510 511
		seq_printf(seq, "present:                 yes\n");
	else {
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

512 513 514 515
	/* Battery Units */

	units = acpi_battery_power_units(battery);

516
	if (!(battery->state & 0x04))
L
Linus Torvalds 已提交
517 518 519 520
		seq_printf(seq, "capacity state:          ok\n");
	else
		seq_printf(seq, "capacity state:          critical\n");

521
	if ((battery->state & 0x01) && (battery->state & 0x02)) {
L
Len Brown 已提交
522 523
		seq_printf(seq,
			   "charging state:          charging/discharging\n");
524
	} else if (battery->state & 0x01)
L
Linus Torvalds 已提交
525
		seq_printf(seq, "charging state:          discharging\n");
526
	else if (battery->state & 0x02)
L
Linus Torvalds 已提交
527 528 529 530 531
		seq_printf(seq, "charging state:          charging\n");
	else {
		seq_printf(seq, "charging state:          charged\n");
	}

532
	if (battery->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
533 534 535
		seq_printf(seq, "present rate:            unknown\n");
	else
		seq_printf(seq, "present rate:            %d %s\n",
536
			   (u32) battery->present_rate, units);
L
Linus Torvalds 已提交
537

538
	if (battery->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
539 540 541
		seq_printf(seq, "remaining capacity:      unknown\n");
	else
		seq_printf(seq, "remaining capacity:      %d %sh\n",
542
			   (u32) battery->remaining_capacity, units);
L
Linus Torvalds 已提交
543

544
	if (battery->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
545 546 547
		seq_printf(seq, "present voltage:         unknown\n");
	else
		seq_printf(seq, "present voltage:         %d mV\n",
548
			   (u32) battery->present_voltage);
L
Linus Torvalds 已提交
549

L
Len Brown 已提交
550
      end:
L
Linus Torvalds 已提交
551

552 553 554 555 556 557 558
	if (result) {
		seq_printf(seq, "ERROR: Unable to read battery state\n");
	}

	return result;
}

559
static int acpi_battery_print_alarm(struct seq_file *seq, int result)
L
Linus Torvalds 已提交
560
{
561
	struct acpi_battery *battery = seq->private;
L
Len Brown 已提交
562
	char *units = "?";
L
Linus Torvalds 已提交
563

564
	if (result)
L
Linus Torvalds 已提交
565 566
		goto end;

567
	if (!acpi_battery_present(battery)) {
L
Linus Torvalds 已提交
568 569 570 571 572
		seq_printf(seq, "present:                 no\n");
		goto end;
	}

	/* Battery Units */
L
Len Brown 已提交
573

574
	units = acpi_battery_power_units(battery);
L
Linus Torvalds 已提交
575 576 577 578 579

	seq_printf(seq, "alarm:                   ");
	if (!battery->alarm)
		seq_printf(seq, "unsupported\n");
	else
580
		seq_printf(seq, "%lu %sh\n", battery->alarm, units);
L
Linus Torvalds 已提交
581

L
Len Brown 已提交
582
      end:
583 584 585 586 587 588 589

	if (result)
		seq_printf(seq, "ERROR: Unable to read battery alarm\n");

	return result;
}

L
Linus Torvalds 已提交
590
static ssize_t
L
Len Brown 已提交
591 592 593
acpi_battery_write_alarm(struct file *file,
			 const char __user * buffer,
			 size_t count, loff_t * ppos)
L
Linus Torvalds 已提交
594
{
L
Len Brown 已提交
595 596
	int result = 0;
	char alarm_string[12] = { '\0' };
597 598
	struct seq_file *m = file->private_data;
	struct acpi_battery *battery = m->private;
599
	int update_result = ACPI_BATTERY_NONE_UPDATE;
L
Linus Torvalds 已提交
600 601

	if (!battery || (count > sizeof(alarm_string) - 1))
602
		return -EINVAL;
L
Linus Torvalds 已提交
603

604 605 606 607 608
	result = acpi_battery_update(battery, 1, &update_result);
	if (result) {
		result = -ENODEV;
		goto end;
	}
L
Linus Torvalds 已提交
609

610 611 612 613 614 615 616 617 618
	if (!acpi_battery_present(battery)) {
		result = -ENODEV;
		goto end;
	}

	if (copy_from_user(alarm_string, buffer, count)) {
		result = -EFAULT;
		goto end;
	}
L
Len Brown 已提交
619

L
Linus Torvalds 已提交
620 621
	alarm_string[count] = '\0';

L
Len Brown 已提交
622 623
	result = acpi_battery_set_alarm(battery,
					simple_strtoul(alarm_string, NULL, 0));
L
Linus Torvalds 已提交
624
	if (result)
625 626 627 628 629
		goto end;

      end:

	acpi_battery_check_result(battery, result);
L
Linus Torvalds 已提交
630

631
	if (!result)
632
		result = count;
633 634 635 636 637 638 639 640 641 642 643 644 645 646
	return result;
}

typedef int(*print_func)(struct seq_file *seq, int result);
typedef int(*get_func)(struct acpi_battery *battery);

static struct acpi_read_mux {
	print_func print;
	get_func get;
} acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
	{.get = acpi_battery_get_info, .print = acpi_battery_print_info},
	{.get = acpi_battery_get_state, .print = acpi_battery_print_state},
	{.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
};
647

648 649 650 651 652 653 654 655
static int acpi_battery_read(int fid, struct seq_file *seq)
{
	struct acpi_battery *battery = seq->private;
	int result = 0;
	int update_result = ACPI_BATTERY_NONE_UPDATE;
	int update = 0;

	update = (get_seconds() - battery->update_time[fid] >= update_time);
656
	update = (update | battery->update[fid]);
657 658 659 660 661 662 663 664 665 666 667 668 669 670

	result = acpi_battery_update(battery, update, &update_result);
	if (result)
		goto end;

	if (update_result == ACPI_BATTERY_EASY_UPDATE) {
		result = acpi_read_funcs[fid].get(battery);
		if (result)
			goto end;
	}

      end:
	result = acpi_read_funcs[fid].print(seq, result);
	acpi_battery_check_result(battery, result);
671
	battery->update[fid] = result;
672
	return result;
L
Linus Torvalds 已提交
673 674
}

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
static int acpi_battery_read_info(struct seq_file *seq, void *offset)
{
	return acpi_battery_read(ACPI_BATTERY_INFO, seq);
}

static int acpi_battery_read_state(struct seq_file *seq, void *offset)
{
	return acpi_battery_read(ACPI_BATTERY_STATE, seq);
}

static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
{
	return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
}

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_state_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_state, PDE(inode)->data);
}

L
Linus Torvalds 已提交
700 701 702 703 704
static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
}

705 706 707 708 709 710 711 712 713
static struct battery_file {
	struct file_operations ops;
	mode_t mode;
	char *name;
} acpi_battery_file[] = {
	{
	.name = "info",
	.mode = S_IRUGO,
	.ops = {
L
Len Brown 已提交
714 715 716 717
	.open = acpi_battery_info_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
718
	.owner = THIS_MODULE,
719 720 721 722 723 724
	},
	},
	{
	.name = "state",
	.mode = S_IRUGO,
	.ops = {
L
Len Brown 已提交
725 726 727 728
	.open = acpi_battery_state_open_fs,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
729
	.owner = THIS_MODULE,
730 731 732 733 734 735
	},
	},
	{
	.name = "alarm",
	.mode = S_IFREG | S_IRUGO | S_IWUSR,
	.ops = {
L
Len Brown 已提交
736 737 738 739 740
	.open = acpi_battery_alarm_open_fs,
	.read = seq_read,
	.write = acpi_battery_write_alarm,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
741
	.owner = THIS_MODULE,
742 743
	},
	},
L
Linus Torvalds 已提交
744 745
};

L
Len Brown 已提交
746
static int acpi_battery_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
747
{
L
Len Brown 已提交
748
	struct proc_dir_entry *entry = NULL;
749
	int i;
L
Linus Torvalds 已提交
750 751 752

	if (!acpi_device_dir(device)) {
		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
L
Len Brown 已提交
753
						     acpi_battery_dir);
L
Linus Torvalds 已提交
754
		if (!acpi_device_dir(device))
755
			return -ENODEV;
L
Linus Torvalds 已提交
756 757 758
		acpi_device_dir(device)->owner = THIS_MODULE;
	}

759 760 761 762 763 764 765 766 767 768
	for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
		entry = create_proc_entry(acpi_battery_file[i].name,
				  acpi_battery_file[i].mode, acpi_device_dir(device));
		if (!entry)
			return -ENODEV;
		else {
			entry->proc_fops = &acpi_battery_file[i].ops;
			entry->data = acpi_driver_data(device);
			entry->owner = THIS_MODULE;
		}
L
Linus Torvalds 已提交
769 770
	}

771
	return 0;
L
Linus Torvalds 已提交
772 773
}

L
Len Brown 已提交
774
static int acpi_battery_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
775
{
776
	int i;
L
Linus Torvalds 已提交
777
	if (acpi_device_dir(device)) {
778 779
		for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
			remove_proc_entry(acpi_battery_file[i].name,
L
Linus Torvalds 已提交
780
				  acpi_device_dir(device));
781
		}
L
Linus Torvalds 已提交
782 783 784 785
		remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
		acpi_device_dir(device) = NULL;
	}

786
	return 0;
L
Linus Torvalds 已提交
787 788 789 790 791 792
}

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

L
Len Brown 已提交
793
static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
L
Linus Torvalds 已提交
794
{
795
	struct acpi_battery *battery = data;
L
Len Brown 已提交
796
	struct acpi_device *device = NULL;
L
Linus Torvalds 已提交
797 798

	if (!battery)
799
		return;
L
Linus Torvalds 已提交
800

801
	device = battery->device;
L
Linus Torvalds 已提交
802 803 804 805

	switch (event) {
	case ACPI_BATTERY_NOTIFY_STATUS:
	case ACPI_BATTERY_NOTIFY_INFO:
806 807
	case ACPI_NOTIFY_BUS_CHECK:
	case ACPI_NOTIFY_DEVICE_CHECK:
808 809
		device = battery->device;
		acpi_battery_notify_update(battery);
810
		acpi_bus_generate_proc_event(device, event,
V
Vladimir Lebedev 已提交
811
					acpi_battery_present(battery));
812 813 814
		acpi_bus_generate_netlink_event(device->pnp.device_class,
						  device->dev.bus_id, event,
						  acpi_battery_present(battery));
L
Linus Torvalds 已提交
815 816 817
		break;
	default:
		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
L
Len Brown 已提交
818
				  "Unsupported event [0x%x]\n", event));
L
Linus Torvalds 已提交
819 820 821
		break;
	}

822
	return;
L
Linus Torvalds 已提交
823 824
}

L
Len Brown 已提交
825
static int acpi_battery_add(struct acpi_device *device)
L
Linus Torvalds 已提交
826
{
L
Len Brown 已提交
827 828 829
	int result = 0;
	acpi_status status = 0;
	struct acpi_battery *battery = NULL;
L
Linus Torvalds 已提交
830 831

	if (!device)
832
		return -EINVAL;
L
Linus Torvalds 已提交
833

834
	battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
L
Linus Torvalds 已提交
835
	if (!battery)
836
		return -ENOMEM;
L
Linus Torvalds 已提交
837

838
	battery->device = device;
L
Linus Torvalds 已提交
839 840 841
	strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
	acpi_driver_data(device) = battery;
842
	mutex_init(&battery->lock);
843
	result = acpi_battery_get_status(battery);
L
Linus Torvalds 已提交
844 845 846
	if (result)
		goto end;

847
	battery->init_update = 1;
848

L
Linus Torvalds 已提交
849 850 851 852
	result = acpi_battery_add_fs(device);
	if (result)
		goto end;

853
	status = acpi_install_notify_handler(device->handle,
854
					     ACPI_ALL_NOTIFY,
L
Len Brown 已提交
855
					     acpi_battery_notify, battery);
L
Linus Torvalds 已提交
856
	if (ACPI_FAILURE(status)) {
857
		ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
L
Linus Torvalds 已提交
858 859 860 861 862
		result = -ENODEV;
		goto end;
	}

	printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
L
Len Brown 已提交
863 864 865 866
	       ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
	       device->status.battery_present ? "present" : "absent");

      end:
867

L
Linus Torvalds 已提交
868 869 870 871 872
	if (result) {
		acpi_battery_remove_fs(device);
		kfree(battery);
	}

873
	return result;
L
Linus Torvalds 已提交
874 875
}

L
Len Brown 已提交
876
static int acpi_battery_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
877
{
L
Len Brown 已提交
878 879
	acpi_status status = 0;
	struct acpi_battery *battery = NULL;
L
Linus Torvalds 已提交
880 881

	if (!device || !acpi_driver_data(device))
882
		return -EINVAL;
L
Linus Torvalds 已提交
883

884
	battery = acpi_driver_data(device);
L
Linus Torvalds 已提交
885

886
	status = acpi_remove_notify_handler(device->handle,
887
					    ACPI_ALL_NOTIFY,
L
Len Brown 已提交
888
					    acpi_battery_notify);
L
Linus Torvalds 已提交
889 890

	acpi_battery_remove_fs(device);
891
	mutex_destroy(&battery->lock);
L
Linus Torvalds 已提交
892 893
	kfree(battery);

894
	return 0;
L
Linus Torvalds 已提交
895 896
}

897
/* this is needed to learn about changes made in suspended state */
898
static int acpi_battery_resume(struct acpi_device *device)
899 900 901 902 903 904 905
{
	struct acpi_battery *battery;

	if (!device)
		return -EINVAL;

	battery = device->driver_data;
906

907
	battery->init_update = 1;
908 909

	return 0;
910 911
}

L
Len Brown 已提交
912
static int __init acpi_battery_init(void)
L
Linus Torvalds 已提交
913
{
914
	int result;
L
Linus Torvalds 已提交
915

P
Pavel Machek 已提交
916 917 918
	if (acpi_disabled)
		return -ENODEV;

919
	acpi_battery_dir = acpi_lock_battery_dir();
L
Linus Torvalds 已提交
920
	if (!acpi_battery_dir)
921
		return -ENODEV;
L
Linus Torvalds 已提交
922 923 924

	result = acpi_bus_register_driver(&acpi_battery_driver);
	if (result < 0) {
925
		acpi_unlock_battery_dir(acpi_battery_dir);
926
		return -ENODEV;
L
Linus Torvalds 已提交
927 928
	}

929
	return 0;
L
Linus Torvalds 已提交
930 931
}

L
Len Brown 已提交
932
static void __exit acpi_battery_exit(void)
L
Linus Torvalds 已提交
933 934 935
{
	acpi_bus_unregister_driver(&acpi_battery_driver);

936
	acpi_unlock_battery_dir(acpi_battery_dir);
L
Linus Torvalds 已提交
937

938
	return;
L
Linus Torvalds 已提交
939 940 941 942
}

module_init(acpi_battery_init);
module_exit(acpi_battery_exit);