]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/acpi/ac.c
ACPI: autoload modules - Create ACPI alias interface
[mirror_ubuntu-bionic-kernel.git] / drivers / acpi / ac.c
CommitLineData
1da177e4
LT
1/*
2 * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
32#include <acpi/acpi_bus.h>
33#include <acpi/acpi_drivers.h>
34
1da177e4
LT
35#define ACPI_AC_COMPONENT 0x00020000
36#define ACPI_AC_CLASS "ac_adapter"
37#define ACPI_AC_HID "ACPI0003"
1da177e4
LT
38#define ACPI_AC_DEVICE_NAME "AC Adapter"
39#define ACPI_AC_FILE_STATE "state"
40#define ACPI_AC_NOTIFY_STATUS 0x80
41#define ACPI_AC_STATUS_OFFLINE 0x00
42#define ACPI_AC_STATUS_ONLINE 0x01
43#define ACPI_AC_STATUS_UNKNOWN 0xFF
44
45#define _COMPONENT ACPI_AC_COMPONENT
f52fd66d 46ACPI_MODULE_NAME("ac");
1da177e4 47
f52fd66d 48MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 49MODULE_DESCRIPTION("ACPI AC Adapter Driver");
1da177e4
LT
50MODULE_LICENSE("GPL");
51
3f86b832
RT
52extern struct proc_dir_entry *acpi_lock_ac_dir(void);
53extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
54
4be44fcd
LB
55static int acpi_ac_add(struct acpi_device *device);
56static int acpi_ac_remove(struct acpi_device *device, int type);
1da177e4
LT
57static int acpi_ac_open_fs(struct inode *inode, struct file *file);
58
59static struct acpi_driver acpi_ac_driver = {
c2b6705b 60 .name = "ac",
4be44fcd
LB
61 .class = ACPI_AC_CLASS,
62 .ids = ACPI_AC_HID,
63 .ops = {
64 .add = acpi_ac_add,
65 .remove = acpi_ac_remove,
66 },
1da177e4
LT
67};
68
69struct acpi_ac {
af96179a 70 struct acpi_device * device;
4be44fcd 71 unsigned long state;
1da177e4
LT
72};
73
d7508032 74static const struct file_operations acpi_ac_fops = {
4be44fcd
LB
75 .open = acpi_ac_open_fs,
76 .read = seq_read,
77 .llseek = seq_lseek,
78 .release = single_release,
1da177e4
LT
79};
80
81/* --------------------------------------------------------------------------
82 AC Adapter Management
83 -------------------------------------------------------------------------- */
84
4be44fcd 85static int acpi_ac_get_state(struct acpi_ac *ac)
1da177e4 86{
4be44fcd 87 acpi_status status = AE_OK;
1da177e4 88
1da177e4
LT
89
90 if (!ac)
d550d98d 91 return -EINVAL;
1da177e4 92
a6ba5ebe 93 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
1da177e4 94 if (ACPI_FAILURE(status)) {
a6fc6720 95 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
1da177e4 96 ac->state = ACPI_AC_STATUS_UNKNOWN;
d550d98d 97 return -ENODEV;
1da177e4 98 }
4be44fcd 99
d550d98d 100 return 0;
1da177e4
LT
101}
102
1da177e4
LT
103/* --------------------------------------------------------------------------
104 FS Interface (/proc)
105 -------------------------------------------------------------------------- */
106
4be44fcd 107static struct proc_dir_entry *acpi_ac_dir;
1da177e4
LT
108
109static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
110{
50dd0969 111 struct acpi_ac *ac = seq->private;
1da177e4 112
1da177e4
LT
113
114 if (!ac)
d550d98d 115 return 0;
1da177e4
LT
116
117 if (acpi_ac_get_state(ac)) {
118 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
d550d98d 119 return 0;
1da177e4
LT
120 }
121
122 seq_puts(seq, "state: ");
123 switch (ac->state) {
124 case ACPI_AC_STATUS_OFFLINE:
125 seq_puts(seq, "off-line\n");
126 break;
127 case ACPI_AC_STATUS_ONLINE:
128 seq_puts(seq, "on-line\n");
129 break;
130 default:
131 seq_puts(seq, "unknown\n");
132 break;
133 }
134
d550d98d 135 return 0;
1da177e4 136}
4be44fcd 137
1da177e4
LT
138static int acpi_ac_open_fs(struct inode *inode, struct file *file)
139{
140 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
141}
142
4be44fcd 143static int acpi_ac_add_fs(struct acpi_device *device)
1da177e4 144{
4be44fcd 145 struct proc_dir_entry *entry = NULL;
1da177e4 146
1da177e4
LT
147
148 if (!acpi_device_dir(device)) {
149 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 150 acpi_ac_dir);
1da177e4 151 if (!acpi_device_dir(device))
d550d98d 152 return -ENODEV;
1da177e4
LT
153 acpi_device_dir(device)->owner = THIS_MODULE;
154 }
155
156 /* 'state' [R] */
157 entry = create_proc_entry(ACPI_AC_FILE_STATE,
4be44fcd 158 S_IRUGO, acpi_device_dir(device));
1da177e4 159 if (!entry)
d550d98d 160 return -ENODEV;
1da177e4
LT
161 else {
162 entry->proc_fops = &acpi_ac_fops;
163 entry->data = acpi_driver_data(device);
164 entry->owner = THIS_MODULE;
165 }
166
d550d98d 167 return 0;
1da177e4
LT
168}
169
4be44fcd 170static int acpi_ac_remove_fs(struct acpi_device *device)
1da177e4 171{
1da177e4
LT
172
173 if (acpi_device_dir(device)) {
4be44fcd 174 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
1da177e4
LT
175
176 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
177 acpi_device_dir(device) = NULL;
178 }
179
d550d98d 180 return 0;
1da177e4
LT
181}
182
1da177e4
LT
183/* --------------------------------------------------------------------------
184 Driver Model
185 -------------------------------------------------------------------------- */
186
4be44fcd 187static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
1da177e4 188{
50dd0969 189 struct acpi_ac *ac = data;
4be44fcd 190 struct acpi_device *device = NULL;
1da177e4 191
1da177e4
LT
192
193 if (!ac)
d550d98d 194 return;
1da177e4 195
af96179a 196 device = ac->device;
1da177e4
LT
197 switch (event) {
198 case ACPI_AC_NOTIFY_STATUS:
03d78252
CL
199 case ACPI_NOTIFY_BUS_CHECK:
200 case ACPI_NOTIFY_DEVICE_CHECK:
1da177e4
LT
201 acpi_ac_get_state(ac);
202 acpi_bus_generate_event(device, event, (u32) ac->state);
203 break;
204 default:
205 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 206 "Unsupported event [0x%x]\n", event));
1da177e4
LT
207 break;
208 }
209
d550d98d 210 return;
1da177e4
LT
211}
212
4be44fcd 213static int acpi_ac_add(struct acpi_device *device)
1da177e4 214{
4be44fcd
LB
215 int result = 0;
216 acpi_status status = AE_OK;
217 struct acpi_ac *ac = NULL;
1da177e4 218
1da177e4
LT
219
220 if (!device)
d550d98d 221 return -EINVAL;
1da177e4 222
36bcbec7 223 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
1da177e4 224 if (!ac)
d550d98d 225 return -ENOMEM;
1da177e4 226
af96179a 227 ac->device = device;
1da177e4
LT
228 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
229 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
230 acpi_driver_data(device) = ac;
231
232 result = acpi_ac_get_state(ac);
233 if (result)
234 goto end;
235
236 result = acpi_ac_add_fs(device);
237 if (result)
238 goto end;
239
a6ba5ebe 240 status = acpi_install_notify_handler(device->handle,
03d78252 241 ACPI_ALL_NOTIFY, acpi_ac_notify,
4be44fcd 242 ac);
1da177e4 243 if (ACPI_FAILURE(status)) {
1da177e4
LT
244 result = -ENODEV;
245 goto end;
246 }
247
4be44fcd
LB
248 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
249 acpi_device_name(device), acpi_device_bid(device),
250 ac->state ? "on-line" : "off-line");
1da177e4 251
4be44fcd 252 end:
1da177e4
LT
253 if (result) {
254 acpi_ac_remove_fs(device);
255 kfree(ac);
256 }
257
d550d98d 258 return result;
1da177e4
LT
259}
260
4be44fcd 261static int acpi_ac_remove(struct acpi_device *device, int type)
1da177e4 262{
4be44fcd
LB
263 acpi_status status = AE_OK;
264 struct acpi_ac *ac = NULL;
1da177e4 265
1da177e4
LT
266
267 if (!device || !acpi_driver_data(device))
d550d98d 268 return -EINVAL;
1da177e4 269
50dd0969 270 ac = acpi_driver_data(device);
1da177e4 271
a6ba5ebe 272 status = acpi_remove_notify_handler(device->handle,
03d78252 273 ACPI_ALL_NOTIFY, acpi_ac_notify);
1da177e4
LT
274
275 acpi_ac_remove_fs(device);
276
277 kfree(ac);
278
d550d98d 279 return 0;
1da177e4
LT
280}
281
4be44fcd 282static int __init acpi_ac_init(void)
1da177e4 283{
3f86b832 284 int result;
1da177e4 285
4d8316d5
PM
286 if (acpi_disabled)
287 return -ENODEV;
1da177e4 288
3f86b832 289 acpi_ac_dir = acpi_lock_ac_dir();
1da177e4 290 if (!acpi_ac_dir)
d550d98d 291 return -ENODEV;
1da177e4
LT
292
293 result = acpi_bus_register_driver(&acpi_ac_driver);
294 if (result < 0) {
3f86b832 295 acpi_unlock_ac_dir(acpi_ac_dir);
d550d98d 296 return -ENODEV;
1da177e4
LT
297 }
298
d550d98d 299 return 0;
1da177e4
LT
300}
301
4be44fcd 302static void __exit acpi_ac_exit(void)
1da177e4 303{
1da177e4
LT
304
305 acpi_bus_unregister_driver(&acpi_ac_driver);
306
3f86b832 307 acpi_unlock_ac_dir(acpi_ac_dir);
1da177e4 308
d550d98d 309 return;
1da177e4
LT
310}
311
1da177e4
LT
312module_init(acpi_ac_init);
313module_exit(acpi_ac_exit);