]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/acpi/ac.c
ACPI: fix acpi_driver.name usage
[net-next-2.6.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"
38#define ACPI_AC_DRIVER_NAME "ACPI AC Adapter Driver"
39#define ACPI_AC_DEVICE_NAME "AC Adapter"
40#define ACPI_AC_FILE_STATE "state"
41#define ACPI_AC_NOTIFY_STATUS 0x80
42#define ACPI_AC_STATUS_OFFLINE 0x00
43#define ACPI_AC_STATUS_ONLINE 0x01
44#define ACPI_AC_STATUS_UNKNOWN 0xFF
45
46#define _COMPONENT ACPI_AC_COMPONENT
f52fd66d 47ACPI_MODULE_NAME("ac");
1da177e4 48
f52fd66d 49MODULE_AUTHOR("Paul Diefenbaugh");
1da177e4
LT
50MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME);
51MODULE_LICENSE("GPL");
52
3f86b832
RT
53extern struct proc_dir_entry *acpi_lock_ac_dir(void);
54extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
55
4be44fcd
LB
56static int acpi_ac_add(struct acpi_device *device);
57static int acpi_ac_remove(struct acpi_device *device, int type);
1da177e4
LT
58static int acpi_ac_open_fs(struct inode *inode, struct file *file);
59
60static struct acpi_driver acpi_ac_driver = {
c2b6705b 61 .name = "ac",
4be44fcd
LB
62 .class = ACPI_AC_CLASS,
63 .ids = ACPI_AC_HID,
64 .ops = {
65 .add = acpi_ac_add,
66 .remove = acpi_ac_remove,
67 },
1da177e4
LT
68};
69
70struct acpi_ac {
af96179a 71 struct acpi_device * device;
4be44fcd 72 unsigned long state;
1da177e4
LT
73};
74
d7508032 75static const struct file_operations acpi_ac_fops = {
4be44fcd
LB
76 .open = acpi_ac_open_fs,
77 .read = seq_read,
78 .llseek = seq_lseek,
79 .release = single_release,
1da177e4
LT
80};
81
82/* --------------------------------------------------------------------------
83 AC Adapter Management
84 -------------------------------------------------------------------------- */
85
4be44fcd 86static int acpi_ac_get_state(struct acpi_ac *ac)
1da177e4 87{
4be44fcd 88 acpi_status status = AE_OK;
1da177e4 89
1da177e4
LT
90
91 if (!ac)
d550d98d 92 return -EINVAL;
1da177e4 93
a6ba5ebe 94 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
1da177e4 95 if (ACPI_FAILURE(status)) {
a6fc6720 96 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
1da177e4 97 ac->state = ACPI_AC_STATUS_UNKNOWN;
d550d98d 98 return -ENODEV;
1da177e4 99 }
4be44fcd 100
d550d98d 101 return 0;
1da177e4
LT
102}
103
1da177e4
LT
104/* --------------------------------------------------------------------------
105 FS Interface (/proc)
106 -------------------------------------------------------------------------- */
107
4be44fcd 108static struct proc_dir_entry *acpi_ac_dir;
1da177e4
LT
109
110static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
111{
50dd0969 112 struct acpi_ac *ac = seq->private;
1da177e4 113
1da177e4
LT
114
115 if (!ac)
d550d98d 116 return 0;
1da177e4
LT
117
118 if (acpi_ac_get_state(ac)) {
119 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
d550d98d 120 return 0;
1da177e4
LT
121 }
122
123 seq_puts(seq, "state: ");
124 switch (ac->state) {
125 case ACPI_AC_STATUS_OFFLINE:
126 seq_puts(seq, "off-line\n");
127 break;
128 case ACPI_AC_STATUS_ONLINE:
129 seq_puts(seq, "on-line\n");
130 break;
131 default:
132 seq_puts(seq, "unknown\n");
133 break;
134 }
135
d550d98d 136 return 0;
1da177e4 137}
4be44fcd 138
1da177e4
LT
139static int acpi_ac_open_fs(struct inode *inode, struct file *file)
140{
141 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
142}
143
4be44fcd 144static int acpi_ac_add_fs(struct acpi_device *device)
1da177e4 145{
4be44fcd 146 struct proc_dir_entry *entry = NULL;
1da177e4 147
1da177e4
LT
148
149 if (!acpi_device_dir(device)) {
150 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 151 acpi_ac_dir);
1da177e4 152 if (!acpi_device_dir(device))
d550d98d 153 return -ENODEV;
1da177e4
LT
154 acpi_device_dir(device)->owner = THIS_MODULE;
155 }
156
157 /* 'state' [R] */
158 entry = create_proc_entry(ACPI_AC_FILE_STATE,
4be44fcd 159 S_IRUGO, acpi_device_dir(device));
1da177e4 160 if (!entry)
d550d98d 161 return -ENODEV;
1da177e4
LT
162 else {
163 entry->proc_fops = &acpi_ac_fops;
164 entry->data = acpi_driver_data(device);
165 entry->owner = THIS_MODULE;
166 }
167
d550d98d 168 return 0;
1da177e4
LT
169}
170
4be44fcd 171static int acpi_ac_remove_fs(struct acpi_device *device)
1da177e4 172{
1da177e4
LT
173
174 if (acpi_device_dir(device)) {
4be44fcd 175 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
1da177e4
LT
176
177 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
178 acpi_device_dir(device) = NULL;
179 }
180
d550d98d 181 return 0;
1da177e4
LT
182}
183
1da177e4
LT
184/* --------------------------------------------------------------------------
185 Driver Model
186 -------------------------------------------------------------------------- */
187
4be44fcd 188static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
1da177e4 189{
50dd0969 190 struct acpi_ac *ac = data;
4be44fcd 191 struct acpi_device *device = NULL;
1da177e4 192
1da177e4
LT
193
194 if (!ac)
d550d98d 195 return;
1da177e4 196
af96179a 197 device = ac->device;
1da177e4
LT
198 switch (event) {
199 case ACPI_AC_NOTIFY_STATUS:
03d78252
CL
200 case ACPI_NOTIFY_BUS_CHECK:
201 case ACPI_NOTIFY_DEVICE_CHECK:
1da177e4
LT
202 acpi_ac_get_state(ac);
203 acpi_bus_generate_event(device, event, (u32) ac->state);
204 break;
205 default:
206 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 207 "Unsupported event [0x%x]\n", event));
1da177e4
LT
208 break;
209 }
210
d550d98d 211 return;
1da177e4
LT
212}
213
4be44fcd 214static int acpi_ac_add(struct acpi_device *device)
1da177e4 215{
4be44fcd
LB
216 int result = 0;
217 acpi_status status = AE_OK;
218 struct acpi_ac *ac = NULL;
1da177e4 219
1da177e4
LT
220
221 if (!device)
d550d98d 222 return -EINVAL;
1da177e4 223
36bcbec7 224 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
1da177e4 225 if (!ac)
d550d98d 226 return -ENOMEM;
1da177e4 227
af96179a 228 ac->device = device;
1da177e4
LT
229 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
230 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
231 acpi_driver_data(device) = ac;
232
233 result = acpi_ac_get_state(ac);
234 if (result)
235 goto end;
236
237 result = acpi_ac_add_fs(device);
238 if (result)
239 goto end;
240
a6ba5ebe 241 status = acpi_install_notify_handler(device->handle,
03d78252 242 ACPI_ALL_NOTIFY, acpi_ac_notify,
4be44fcd 243 ac);
1da177e4 244 if (ACPI_FAILURE(status)) {
1da177e4
LT
245 result = -ENODEV;
246 goto end;
247 }
248
4be44fcd
LB
249 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
250 acpi_device_name(device), acpi_device_bid(device),
251 ac->state ? "on-line" : "off-line");
1da177e4 252
4be44fcd 253 end:
1da177e4
LT
254 if (result) {
255 acpi_ac_remove_fs(device);
256 kfree(ac);
257 }
258
d550d98d 259 return result;
1da177e4
LT
260}
261
4be44fcd 262static int acpi_ac_remove(struct acpi_device *device, int type)
1da177e4 263{
4be44fcd
LB
264 acpi_status status = AE_OK;
265 struct acpi_ac *ac = NULL;
1da177e4 266
1da177e4
LT
267
268 if (!device || !acpi_driver_data(device))
d550d98d 269 return -EINVAL;
1da177e4 270
50dd0969 271 ac = acpi_driver_data(device);
1da177e4 272
a6ba5ebe 273 status = acpi_remove_notify_handler(device->handle,
03d78252 274 ACPI_ALL_NOTIFY, acpi_ac_notify);
1da177e4
LT
275
276 acpi_ac_remove_fs(device);
277
278 kfree(ac);
279
d550d98d 280 return 0;
1da177e4
LT
281}
282
4be44fcd 283static int __init acpi_ac_init(void)
1da177e4 284{
3f86b832 285 int result;
1da177e4 286
4d8316d5
PM
287 if (acpi_disabled)
288 return -ENODEV;
1da177e4 289
3f86b832 290 acpi_ac_dir = acpi_lock_ac_dir();
1da177e4 291 if (!acpi_ac_dir)
d550d98d 292 return -ENODEV;
1da177e4
LT
293
294 result = acpi_bus_register_driver(&acpi_ac_driver);
295 if (result < 0) {
3f86b832 296 acpi_unlock_ac_dir(acpi_ac_dir);
d550d98d 297 return -ENODEV;
1da177e4
LT
298 }
299
d550d98d 300 return 0;
1da177e4
LT
301}
302
4be44fcd 303static void __exit acpi_ac_exit(void)
1da177e4 304{
1da177e4
LT
305
306 acpi_bus_unregister_driver(&acpi_ac_driver);
307
3f86b832 308 acpi_unlock_ac_dir(acpi_ac_dir);
1da177e4 309
d550d98d 310 return;
1da177e4
LT
311}
312
1da177e4
LT
313module_init(acpi_ac_init);
314module_exit(acpi_ac_exit);