battery.c 19.1 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  battery.c - ACPI Battery Driver (Revision: 2.0)
L
Linus Torvalds 已提交
3
 *
4 5
 *  Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
 *  Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
L
Linus Torvalds 已提交
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
 *  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>
32
#include <linux/jiffies.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#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 _COMPONENT		ACPI_BATTERY_COMPONENT
49

50
ACPI_MODULE_NAME("battery");
L
Linus Torvalds 已提交
51

52
MODULE_AUTHOR("Paul Diefenbaugh");
53
MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
54
MODULE_DESCRIPTION("ACPI Battery Driver");
L
Linus Torvalds 已提交
55 56
MODULE_LICENSE("GPL");

57 58 59
static unsigned int cache_time = 1000;
module_param(cache_time, uint, 0644);
MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
60

61 62 63
extern struct proc_dir_entry *acpi_lock_battery_dir(void);
extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);

64 65 66 67 68
static const struct acpi_device_id battery_device_ids[] = {
	{"PNP0C0A", 0},
	{"", 0},
};

69
MODULE_DEVICE_TABLE(acpi, battery_device_ids);
L
Linus Torvalds 已提交
70

71
enum acpi_battery_files {
72 73 74
	info_tag = 0,
	state_tag,
	alarm_tag,
75 76 77
	ACPI_BATTERY_NUMFILES,
};

L
Linus Torvalds 已提交
78
struct acpi_battery {
79
	struct mutex lock;
80 81
	struct acpi_device *device;
	unsigned long update_time;
82 83 84 85 86 87 88 89 90 91 92
	int present_rate;
	int remaining_capacity;
	int present_voltage;
	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;
93
	int alarm;
94 95 96 97
	char model_number[32];
	char serial_number[32];
	char type[32];
	char oem_info[32];
98 99
	int state;
	int power_unit;
100
	u8 alarm_present;
L
Linus Torvalds 已提交
101 102
};

103
inline int acpi_battery_present(struct acpi_battery *battery)
104
{
105 106
	return battery->device->status.battery_present;
}
107

108
inline char *acpi_battery_units(struct acpi_battery *battery)
109
{
110
	return (battery->power_unit)?"mA":"mW";
111 112
}

113 114 115
/* --------------------------------------------------------------------------
                               Battery Management
   -------------------------------------------------------------------------- */
116 117 118 119
struct acpi_offsets {
	size_t offset;		/* offset inside struct acpi_sbs_battery */
	u8 mode;		/* int or string? */
};
120

121 122 123 124 125 126
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},
};
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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},
};
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
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;
		}
168 169 170 171 172 173
	}
	return 0;
}

static int acpi_battery_get_status(struct acpi_battery *battery)
{
174
	if (acpi_bus_get_status(battery->device)) {
175 176 177
		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
		return -ENODEV;
	}
178
	return 0;
179 180 181
}

static int acpi_battery_get_info(struct acpi_battery *battery)
L
Linus Torvalds 已提交
182
{
183
	int result = -EFAULT;
L
Len Brown 已提交
184 185
	acpi_status status = 0;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
L
Linus Torvalds 已提交
186

187 188
	if (!acpi_battery_present(battery))
		return 0;
189
	mutex_lock(&battery->lock);
190
	status = acpi_evaluate_object(battery->device->handle, "_BIF",
191 192
				      NULL, &buffer);
	mutex_unlock(&battery->lock);
193

L
Linus Torvalds 已提交
194
	if (ACPI_FAILURE(status)) {
195
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
196
		return -ENODEV;
L
Linus Torvalds 已提交
197
	}
198

199 200
	result = extract_package(battery, buffer.pointer,
				 info_offsets, ARRAY_SIZE(info_offsets));
201
	kfree(buffer.pointer);
202
	return result;
L
Linus Torvalds 已提交
203 204
}

205
static int acpi_battery_get_state(struct acpi_battery *battery)
L
Linus Torvalds 已提交
206
{
L
Len Brown 已提交
207 208 209
	int result = 0;
	acpi_status status = 0;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
L
Linus Torvalds 已提交
210

211 212
	if (!acpi_battery_present(battery))
		return 0;
L
Linus Torvalds 已提交
213

214 215 216 217 218
	if (battery->update_time &&
	    time_before(jiffies, battery->update_time +
			msecs_to_jiffies(cache_time)))
		return 0;

219
	mutex_lock(&battery->lock);
220
	status = acpi_evaluate_object(battery->device->handle, "_BST",
221 222
				      NULL, &buffer);
	mutex_unlock(&battery->lock);
223

L
Linus Torvalds 已提交
224
	if (ACPI_FAILURE(status)) {
225
		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
226
		return -ENODEV;
L
Linus Torvalds 已提交
227
	}
228

229 230
	result = extract_package(battery, buffer.pointer,
				 state_offsets, ARRAY_SIZE(state_offsets));
231
	battery->update_time = jiffies;
232
	kfree(buffer.pointer);
233 234
	return result;
}
L
Linus Torvalds 已提交
235

236
static int acpi_battery_set_alarm(struct acpi_battery *battery)
L
Linus Torvalds 已提交
237
{
L
Len Brown 已提交
238
	acpi_status status = 0;
239
	union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
L
Len Brown 已提交
240
	struct acpi_object_list arg_list = { 1, &arg0 };
L
Linus Torvalds 已提交
241

242
	if (!acpi_battery_present(battery)|| !battery->alarm_present)
243
		return -ENODEV;
L
Linus Torvalds 已提交
244

245
	arg0.integer.value = battery->alarm;
L
Linus Torvalds 已提交
246

247
	mutex_lock(&battery->lock);
248 249
	status = acpi_evaluate_object(battery->device->handle, "_BTP",
				 &arg_list, NULL);
250
	mutex_unlock(&battery->lock);
251

L
Linus Torvalds 已提交
252
	if (ACPI_FAILURE(status))
253
		return -ENODEV;
L
Linus Torvalds 已提交
254

255
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
256
	return 0;
L
Linus Torvalds 已提交
257 258
}

259
static int acpi_battery_init_alarm(struct acpi_battery *battery)
L
Linus Torvalds 已提交
260
{
L
Len Brown 已提交
261 262 263
	acpi_status status = AE_OK;
	acpi_handle handle = NULL;

264
	/* See if alarms are supported, and if so, set default */
265 266
	status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
	if (ACPI_FAILURE(status)) {
267
		battery->alarm_present = 0;
268
		return 0;
269
	}
270 271 272
	battery->alarm_present = 1;
	if (!battery->alarm)
		battery->alarm = battery->design_capacity_warning;
273
	return acpi_battery_set_alarm(battery);
274
}
L
Linus Torvalds 已提交
275

276
static int acpi_battery_update(struct acpi_battery *battery)
277
{
278 279 280
	int saved_present = acpi_battery_present(battery);
	int result = acpi_battery_get_status(battery);
	if (result || !acpi_battery_present(battery))
281
		return result;
282 283 284
	if (saved_present != acpi_battery_present(battery) ||
	    !battery->update_time) {
		battery->update_time = 0;
285 286 287 288 289
		result = acpi_battery_get_info(battery);
		if (result)
			return result;
		acpi_battery_init_alarm(battery);
	}
290
	return acpi_battery_get_state(battery);
291 292
}

L
Linus Torvalds 已提交
293 294 295 296
/* --------------------------------------------------------------------------
                              FS Interface (/proc)
   -------------------------------------------------------------------------- */

L
Len Brown 已提交
297
static struct proc_dir_entry *acpi_battery_dir;
298

299
static int acpi_battery_print_info(struct seq_file *seq, int result)
L
Linus Torvalds 已提交
300
{
301
	struct acpi_battery *battery = seq->private;
L
Linus Torvalds 已提交
302

303
	if (result)
L
Linus Torvalds 已提交
304 305
		goto end;

306 307 308
	seq_printf(seq, "present:                 %s\n",
		   acpi_battery_present(battery)?"yes":"no");
	if (!acpi_battery_present(battery))
L
Linus Torvalds 已提交
309
		goto end;
310
	if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
311 312 313
		seq_printf(seq, "design capacity:         unknown\n");
	else
		seq_printf(seq, "design capacity:         %d %sh\n",
314 315
			   battery->design_capacity,
			   acpi_battery_units(battery));
L
Linus Torvalds 已提交
316

317
	if (battery->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
318 319 320
		seq_printf(seq, "last full capacity:      unknown\n");
	else
		seq_printf(seq, "last full capacity:      %d %sh\n",
321 322 323 324 325
			   battery->last_full_capacity,
			   acpi_battery_units(battery));

	seq_printf(seq, "battery technology:      %srechargeable\n",
		   (!battery->technology)?"non-":"");
L
Linus Torvalds 已提交
326

327
	if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
328 329 330
		seq_printf(seq, "design voltage:          unknown\n");
	else
		seq_printf(seq, "design voltage:          %d mV\n",
331
			   battery->design_voltage);
L
Linus Torvalds 已提交
332
	seq_printf(seq, "design capacity warning: %d %sh\n",
333 334
		   battery->design_capacity_warning,
		   acpi_battery_units(battery));
L
Linus Torvalds 已提交
335
	seq_printf(seq, "design capacity low:     %d %sh\n",
336 337
		   battery->design_capacity_low,
		   acpi_battery_units(battery));
L
Linus Torvalds 已提交
338
	seq_printf(seq, "capacity granularity 1:  %d %sh\n",
339 340
		   battery->capacity_granularity_1,
		   acpi_battery_units(battery));
L
Linus Torvalds 已提交
341
	seq_printf(seq, "capacity granularity 2:  %d %sh\n",
342 343
		   battery->capacity_granularity_2,
		   acpi_battery_units(battery));
344 345 346 347
	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 已提交
348
      end:
349 350 351 352 353
	if (result)
		seq_printf(seq, "ERROR: Unable to read battery info\n");
	return result;
}

354
static int acpi_battery_print_state(struct seq_file *seq, int result)
L
Linus Torvalds 已提交
355
{
356
	struct acpi_battery *battery = seq->private;
L
Linus Torvalds 已提交
357

358
	if (result)
L
Linus Torvalds 已提交
359 360
		goto end;

361 362 363
	seq_printf(seq, "present:                 %s\n",
		   acpi_battery_present(battery)?"yes":"no");
	if (!acpi_battery_present(battery))
L
Linus Torvalds 已提交
364
		goto end;
365

366 367 368
	seq_printf(seq, "capacity state:          %s\n",
			(battery->state & 0x04)?"critical":"ok");
	if ((battery->state & 0x01) && (battery->state & 0x02))
L
Len Brown 已提交
369 370
		seq_printf(seq,
			   "charging state:          charging/discharging\n");
371
	else if (battery->state & 0x01)
L
Linus Torvalds 已提交
372
		seq_printf(seq, "charging state:          discharging\n");
373
	else if (battery->state & 0x02)
L
Linus Torvalds 已提交
374
		seq_printf(seq, "charging state:          charging\n");
375
	else
L
Linus Torvalds 已提交
376 377
		seq_printf(seq, "charging state:          charged\n");

378
	if (battery->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
379 380 381
		seq_printf(seq, "present rate:            unknown\n");
	else
		seq_printf(seq, "present rate:            %d %s\n",
382
			   battery->present_rate, acpi_battery_units(battery));
L
Linus Torvalds 已提交
383

384
	if (battery->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
385 386 387
		seq_printf(seq, "remaining capacity:      unknown\n");
	else
		seq_printf(seq, "remaining capacity:      %d %sh\n",
388
			   battery->remaining_capacity, acpi_battery_units(battery));
389
	if (battery->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
L
Linus Torvalds 已提交
390 391 392
		seq_printf(seq, "present voltage:         unknown\n");
	else
		seq_printf(seq, "present voltage:         %d mV\n",
393
			   battery->present_voltage);
L
Len Brown 已提交
394
      end:
395
	if (result)
396 397 398 399 400
		seq_printf(seq, "ERROR: Unable to read battery state\n");

	return result;
}

401
static int acpi_battery_print_alarm(struct seq_file *seq, int result)
L
Linus Torvalds 已提交
402
{
403
	struct acpi_battery *battery = seq->private;
L
Linus Torvalds 已提交
404

405
	if (result)
L
Linus Torvalds 已提交
406 407
		goto end;

408
	if (!acpi_battery_present(battery)) {
L
Linus Torvalds 已提交
409 410 411 412 413 414 415
		seq_printf(seq, "present:                 no\n");
		goto end;
	}
	seq_printf(seq, "alarm:                   ");
	if (!battery->alarm)
		seq_printf(seq, "unsupported\n");
	else
416 417
		seq_printf(seq, "%u %sh\n", battery->alarm,
				acpi_battery_units(battery));
L
Len Brown 已提交
418
      end:
419 420 421 422 423
	if (result)
		seq_printf(seq, "ERROR: Unable to read battery alarm\n");
	return result;
}

424 425 426
static ssize_t acpi_battery_write_alarm(struct file *file,
					const char __user * buffer,
					size_t count, loff_t * ppos)
L
Linus Torvalds 已提交
427
{
L
Len Brown 已提交
428 429
	int result = 0;
	char alarm_string[12] = { '\0' };
430 431
	struct seq_file *m = file->private_data;
	struct acpi_battery *battery = m->private;
L
Linus Torvalds 已提交
432 433

	if (!battery || (count > sizeof(alarm_string) - 1))
434
		return -EINVAL;
435 436 437 438 439 440 441 442 443 444 445 446
	if (result) {
		result = -ENODEV;
		goto end;
	}
	if (!acpi_battery_present(battery)) {
		result = -ENODEV;
		goto end;
	}
	if (copy_from_user(alarm_string, buffer, count)) {
		result = -EFAULT;
		goto end;
	}
L
Linus Torvalds 已提交
447
	alarm_string[count] = '\0';
448
	battery->alarm = simple_strtol(alarm_string, NULL, 0);
449
	result = acpi_battery_set_alarm(battery);
450 451
      end:
	if (!result)
452
		return count;
453 454 455 456
	return result;
}

typedef int(*print_func)(struct seq_file *seq, int result);
457 458 459 460 461

static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
	acpi_battery_print_info,
	acpi_battery_print_state,
	acpi_battery_print_alarm,
462
};
463

464 465 466
static int acpi_battery_read(int fid, struct seq_file *seq)
{
	struct acpi_battery *battery = seq->private;
467
	int result = acpi_battery_update(battery);
468
	return acpi_print_funcs[fid](seq, result);
469 470
}

471 472 473 474 475 476 477 478
#define DECLARE_FILE_FUNCTIONS(_name) \
static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
{ \
	return acpi_battery_read(_name##_tag, seq); \
} \
static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
{ \
	return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
479 480
}

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
DECLARE_FILE_FUNCTIONS(info);
DECLARE_FILE_FUNCTIONS(state);
DECLARE_FILE_FUNCTIONS(alarm);

#undef DECLARE_FILE_FUNCTIONS

#define FILE_DESCRIPTION_RO(_name) \
	{ \
	.name = __stringify(_name), \
	.mode = S_IRUGO, \
	.ops = { \
		.open = acpi_battery_##_name##_open_fs, \
		.read = seq_read, \
		.llseek = seq_lseek, \
		.release = single_release, \
		.owner = THIS_MODULE, \
		}, \
	}
499

500 501 502 503 504 505 506 507 508 509 510 511 512
#define FILE_DESCRIPTION_RW(_name) \
	{ \
	.name = __stringify(_name), \
	.mode = S_IFREG | S_IRUGO | S_IWUSR, \
	.ops = { \
		.open = acpi_battery_##_name##_open_fs, \
		.read = seq_read, \
		.llseek = seq_lseek, \
		.write = acpi_battery_write_##_name, \
		.release = single_release, \
		.owner = THIS_MODULE, \
		}, \
	}
L
Linus Torvalds 已提交
513

514 515 516 517 518
static struct battery_file {
	struct file_operations ops;
	mode_t mode;
	char *name;
} acpi_battery_file[] = {
519 520 521
	FILE_DESCRIPTION_RO(info),
	FILE_DESCRIPTION_RO(state),
	FILE_DESCRIPTION_RW(alarm),
L
Linus Torvalds 已提交
522 523
};

524 525 526
#undef FILE_DESCRIPTION_RO
#undef FILE_DESCRIPTION_RW

L
Len Brown 已提交
527
static int acpi_battery_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
528
{
L
Len Brown 已提交
529
	struct proc_dir_entry *entry = NULL;
530
	int i;
L
Linus Torvalds 已提交
531 532 533

	if (!acpi_device_dir(device)) {
		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
L
Len Brown 已提交
534
						     acpi_battery_dir);
L
Linus Torvalds 已提交
535
		if (!acpi_device_dir(device))
536
			return -ENODEV;
L
Linus Torvalds 已提交
537 538 539
		acpi_device_dir(device)->owner = THIS_MODULE;
	}

540 541 542 543 544 545 546 547 548 549
	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 已提交
550
	}
551
	return 0;
L
Linus Torvalds 已提交
552 553
}

554
static void acpi_battery_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
555
{
556
	int i;
557 558 559 560
	if (!acpi_device_dir(device))
		return;
	for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
		remove_proc_entry(acpi_battery_file[i].name,
L
Linus Torvalds 已提交
561 562
				  acpi_device_dir(device));

563 564
	remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
	acpi_device_dir(device) = NULL;
L
Linus Torvalds 已提交
565 566 567 568 569 570
}

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

L
Len Brown 已提交
571
static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
L
Linus Torvalds 已提交
572
{
573
	struct acpi_battery *battery = data;
574
	struct acpi_device *device;
L
Linus Torvalds 已提交
575
	if (!battery)
576
		return;
577
	device = battery->device;
578 579 580 581 582
	acpi_battery_update(battery);
	acpi_bus_generate_proc_event(device, event,
				     acpi_battery_present(battery));
	acpi_bus_generate_netlink_event(device->pnp.device_class,
					device->dev.bus_id, event,
V
Vladimir Lebedev 已提交
583
					acpi_battery_present(battery));
L
Linus Torvalds 已提交
584 585
}

L
Len Brown 已提交
586
static int acpi_battery_add(struct acpi_device *device)
L
Linus Torvalds 已提交
587
{
L
Len Brown 已提交
588 589 590
	int result = 0;
	acpi_status status = 0;
	struct acpi_battery *battery = NULL;
L
Linus Torvalds 已提交
591
	if (!device)
592
		return -EINVAL;
593
	battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
L
Linus Torvalds 已提交
594
	if (!battery)
595
		return -ENOMEM;
596
	battery->device = device;
L
Linus Torvalds 已提交
597 598 599
	strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
	acpi_driver_data(device) = battery;
600
	mutex_init(&battery->lock);
601
	acpi_battery_update(battery);
L
Linus Torvalds 已提交
602 603 604
	result = acpi_battery_add_fs(device);
	if (result)
		goto end;
605
	status = acpi_install_notify_handler(device->handle,
606
					     ACPI_ALL_NOTIFY,
L
Len Brown 已提交
607
					     acpi_battery_notify, battery);
L
Linus Torvalds 已提交
608
	if (ACPI_FAILURE(status)) {
609
		ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
L
Linus Torvalds 已提交
610 611 612 613
		result = -ENODEV;
		goto end;
	}
	printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
L
Len Brown 已提交
614 615 616
	       ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
	       device->status.battery_present ? "present" : "absent");
      end:
L
Linus Torvalds 已提交
617 618 619 620
	if (result) {
		acpi_battery_remove_fs(device);
		kfree(battery);
	}
621
	return result;
L
Linus Torvalds 已提交
622 623
}

L
Len Brown 已提交
624
static int acpi_battery_remove(struct acpi_device *device, int type)
L
Linus Torvalds 已提交
625
{
L
Len Brown 已提交
626 627
	acpi_status status = 0;
	struct acpi_battery *battery = NULL;
L
Linus Torvalds 已提交
628 629

	if (!device || !acpi_driver_data(device))
630
		return -EINVAL;
631
	battery = acpi_driver_data(device);
632
	status = acpi_remove_notify_handler(device->handle,
633
					    ACPI_ALL_NOTIFY,
L
Len Brown 已提交
634
					    acpi_battery_notify);
L
Linus Torvalds 已提交
635
	acpi_battery_remove_fs(device);
636
	mutex_destroy(&battery->lock);
L
Linus Torvalds 已提交
637
	kfree(battery);
638
	return 0;
L
Linus Torvalds 已提交
639 640
}

641
/* this is needed to learn about changes made in suspended state */
642
static int acpi_battery_resume(struct acpi_device *device)
643 644 645 646
{
	struct acpi_battery *battery;
	if (!device)
		return -EINVAL;
647 648
	battery = acpi_driver_data(device);
	battery->update_time = 0;
649
	return 0;
650 651
}

652 653 654 655 656 657 658 659 660 661 662
static struct acpi_driver acpi_battery_driver = {
	.name = "battery",
	.class = ACPI_BATTERY_CLASS,
	.ids = battery_device_ids,
	.ops = {
		.add = acpi_battery_add,
		.resume = acpi_battery_resume,
		.remove = acpi_battery_remove,
		},
};

L
Len Brown 已提交
663
static int __init acpi_battery_init(void)
L
Linus Torvalds 已提交
664
{
P
Pavel Machek 已提交
665 666
	if (acpi_disabled)
		return -ENODEV;
667
	acpi_battery_dir = acpi_lock_battery_dir();
L
Linus Torvalds 已提交
668
	if (!acpi_battery_dir)
669
		return -ENODEV;
670
	if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
671
		acpi_unlock_battery_dir(acpi_battery_dir);
672
		return -ENODEV;
L
Linus Torvalds 已提交
673
	}
674
	return 0;
L
Linus Torvalds 已提交
675 676
}

L
Len Brown 已提交
677
static void __exit acpi_battery_exit(void)
L
Linus Torvalds 已提交
678 679
{
	acpi_bus_unregister_driver(&acpi_battery_driver);
680
	acpi_unlock_battery_dir(acpi_battery_dir);
L
Linus Torvalds 已提交
681 682 683 684
}

module_init(acpi_battery_init);
module_exit(acpi_battery_exit);