summaryrefslogtreecommitdiffstats
path: root/drivers/acpi/power.c
blob: d24392895c20963e3330211b5aa52cd12720998b (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *  power.c - Overall power driver. Also handles AC adapter device.
 *
 *  Copyright (C) 2000 Andrew Grover
 *
 *  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/types.h>
#include <linux/proc_fs.h>
#include "acpi.h"
#include "driver.h"

#define _COMPONENT	OS_DEPENDENT
	MODULE_NAME	("power")

int acpi_cmbatt_init(void);
int acpi_cmbatt_terminate(void);

/* ACPI-specific defines */
#define ACPI_AC_ADAPTER_HID	"ACPI0003"

static int ac_count = 0;
static ACPI_HANDLE ac_handle = 0;

/*
 * We found a device with the correct HID
 */
static ACPI_STATUS
acpi_found_ac_adapter(ACPI_HANDLE handle, u32 level, void *ctx, void **value)
{
	ACPI_DEVICE_INFO	info;

	if (ac_count > 0) {
		printk(KERN_ERR "AC Adapter: more than one!\n");
		return (AE_OK);
	}

	if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
		printk(KERN_ERR "AC Adapter: Could not get AC Adapter object info\n");
		return (AE_OK);
	}

	if (!(info.valid & ACPI_VALID_STA)) {
		printk(KERN_ERR "AC Adapter: Battery _STA invalid\n");
		return AE_OK;
	}

	printk(KERN_INFO "AC Adapter: found\n");

	ac_handle = handle;

	ac_count++;

	return AE_OK;
}

static int
proc_read_ac_adapter_status(char *page, char **start, off_t off,
				int count, int *eof, void *data)
{
	ACPI_OBJECT obj;
	ACPI_BUFFER buf;

	char *p = page;
	int len;

	buf.length = sizeof(obj);
	buf.pointer = &obj;
	if (!ACPI_SUCCESS(acpi_evaluate_object(ac_handle, "_PSR", NULL, &buf))
		|| obj.type != ACPI_TYPE_INTEGER) {
		p += sprintf(p, "Could not read AC status\n");
		goto end;
	}

	if (obj.integer.value)
		p += sprintf(p, "on-line\n");
	else
		p += sprintf(p, "off-line\n");

end:
	len = (p - page);
	if (len <= off+count) *eof = 1;
	*start = page + off;
	len -= off;
	if (len>count) len = count;
	if (len<0) len = 0;
	return len;
}

int
acpi_power_init(void)
{
	acpi_get_devices(ACPI_AC_ADAPTER_HID, 
			acpi_found_ac_adapter,
			NULL,
			NULL);

	if (!proc_mkdir("power", NULL))
		return 0;

	if (ac_handle) {
		create_proc_read_entry("power/ac", 0, NULL,
			proc_read_ac_adapter_status, NULL);
	}

	acpi_cmbatt_init();

	return 0;
}

int
acpi_power_terminate(void)
{
	acpi_cmbatt_terminate();

	if (ac_handle) {
		remove_proc_entry("power/ac", NULL);
	}

	remove_proc_entry("power", NULL);

	return 0;
}