2 * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
36 #define ACPI_AC_COMPONENT 0x00020000
37 #define ACPI_AC_CLASS "ac_adapter"
38 #define ACPI_AC_HID "ACPI0003"
39 #define ACPI_AC_DRIVER_NAME "ACPI AC Adapter Driver"
40 #define ACPI_AC_DEVICE_NAME "AC Adapter"
41 #define ACPI_AC_FILE_STATE "state"
42 #define ACPI_AC_NOTIFY_STATUS 0x80
43 #define ACPI_AC_STATUS_OFFLINE 0x00
44 #define ACPI_AC_STATUS_ONLINE 0x01
45 #define ACPI_AC_STATUS_UNKNOWN 0xFF
47 #define _COMPONENT ACPI_AC_COMPONENT
48 ACPI_MODULE_NAME ("acpi_ac")
50 MODULE_AUTHOR("Paul Diefenbaugh");
51 MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME
);
52 MODULE_LICENSE("GPL");
54 int acpi_ac_add (struct acpi_device
*device
);
55 int acpi_ac_remove (struct acpi_device
*device
, int type
);
56 static int acpi_ac_open_fs(struct inode
*inode
, struct file
*file
);
58 static struct acpi_driver acpi_ac_driver
= {
59 .name
= ACPI_AC_DRIVER_NAME
,
60 .class = ACPI_AC_CLASS
,
64 .remove
= acpi_ac_remove
,
73 static struct file_operations acpi_ac_fops
= {
74 .open
= acpi_ac_open_fs
,
77 .release
= single_release
,
80 /* --------------------------------------------------------------------------
82 -------------------------------------------------------------------------- */
88 acpi_status status
= AE_OK
;
90 ACPI_FUNCTION_TRACE("acpi_ac_get_state");
93 return_VALUE(-EINVAL
);
95 status
= acpi_evaluate_integer(ac
->handle
, "_PSR", NULL
, &ac
->state
);
96 if (ACPI_FAILURE(status
)) {
97 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
98 "Error reading AC Adapter state\n"));
99 ac
->state
= ACPI_AC_STATUS_UNKNOWN
;
100 return_VALUE(-ENODEV
);
107 /* --------------------------------------------------------------------------
109 -------------------------------------------------------------------------- */
111 struct proc_dir_entry
*acpi_ac_dir
= NULL
;
113 int acpi_ac_seq_show(struct seq_file
*seq
, void *offset
)
115 struct acpi_ac
*ac
= (struct acpi_ac
*) seq
->private;
117 ACPI_FUNCTION_TRACE("acpi_ac_seq_show");
122 if (acpi_ac_get_state(ac
)) {
123 seq_puts(seq
, "ERROR: Unable to read AC Adapter state\n");
127 seq_puts(seq
, "state: ");
129 case ACPI_AC_STATUS_OFFLINE
:
130 seq_puts(seq
, "off-line\n");
132 case ACPI_AC_STATUS_ONLINE
:
133 seq_puts(seq
, "on-line\n");
136 seq_puts(seq
, "unknown\n");
143 static int acpi_ac_open_fs(struct inode
*inode
, struct file
*file
)
145 return single_open(file
, acpi_ac_seq_show
, PDE(inode
)->data
);
150 struct acpi_device
*device
)
152 struct proc_dir_entry
*entry
= NULL
;
154 ACPI_FUNCTION_TRACE("acpi_ac_add_fs");
156 if (!acpi_device_dir(device
)) {
157 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
159 if (!acpi_device_dir(device
))
160 return_VALUE(-ENODEV
);
164 entry
= create_proc_entry(ACPI_AC_FILE_STATE
,
165 S_IRUGO
, acpi_device_dir(device
));
167 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
168 "Unable to create '%s' fs entry\n",
169 ACPI_AC_FILE_STATE
));
171 entry
->proc_fops
= &acpi_ac_fops
;
172 entry
->data
= acpi_driver_data(device
);
181 struct acpi_device
*device
)
183 ACPI_FUNCTION_TRACE("acpi_ac_remove_fs");
185 if (acpi_device_dir(device
)) {
186 remove_proc_entry(acpi_device_bid(device
), acpi_ac_dir
);
187 acpi_device_dir(device
) = NULL
;
194 /* --------------------------------------------------------------------------
196 -------------------------------------------------------------------------- */
204 struct acpi_ac
*ac
= (struct acpi_ac
*) data
;
205 struct acpi_device
*device
= NULL
;
207 ACPI_FUNCTION_TRACE("acpi_ac_notify");
212 if (acpi_bus_get_device(ac
->handle
, &device
))
216 case ACPI_AC_NOTIFY_STATUS
:
217 acpi_ac_get_state(ac
);
218 acpi_bus_generate_event(device
, event
, (u32
) ac
->state
);
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
222 "Unsupported event [0x%x]\n", event
));
232 struct acpi_device
*device
)
235 acpi_status status
= AE_OK
;
236 struct acpi_ac
*ac
= NULL
;
238 ACPI_FUNCTION_TRACE("acpi_ac_add");
241 return_VALUE(-EINVAL
);
243 ac
= kmalloc(sizeof(struct acpi_ac
), GFP_KERNEL
);
245 return_VALUE(-ENOMEM
);
246 memset(ac
, 0, sizeof(struct acpi_ac
));
248 ac
->handle
= device
->handle
;
249 sprintf(acpi_device_name(device
), "%s", ACPI_AC_DEVICE_NAME
);
250 sprintf(acpi_device_class(device
), "%s", ACPI_AC_CLASS
);
251 acpi_driver_data(device
) = ac
;
253 result
= acpi_ac_get_state(ac
);
257 result
= acpi_ac_add_fs(device
);
261 status
= acpi_install_notify_handler(ac
->handle
,
262 ACPI_DEVICE_NOTIFY
, acpi_ac_notify
, ac
);
263 if (ACPI_FAILURE(status
)) {
264 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
265 "Error installing notify handler\n"));
270 printk(KERN_INFO PREFIX
"%s [%s] (%s)\n",
271 acpi_device_name(device
), acpi_device_bid(device
),
272 ac
->state
?"on-line":"off-line");
276 acpi_ac_remove_fs(device
);
280 return_VALUE(result
);
286 struct acpi_device
*device
,
289 acpi_status status
= AE_OK
;
290 struct acpi_ac
*ac
= NULL
;
292 ACPI_FUNCTION_TRACE("acpi_ac_remove");
294 if (!device
|| !acpi_driver_data(device
))
295 return_VALUE(-EINVAL
);
297 ac
= (struct acpi_ac
*) acpi_driver_data(device
);
299 status
= acpi_remove_notify_handler(ac
->handle
,
300 ACPI_DEVICE_NOTIFY
, acpi_ac_notify
);
301 if (ACPI_FAILURE(status
))
302 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
303 "Error removing notify handler\n"));
305 acpi_ac_remove_fs(device
);
318 ACPI_FUNCTION_TRACE("acpi_ac_init");
320 acpi_ac_dir
= proc_mkdir(ACPI_AC_CLASS
, acpi_root_dir
);
322 return_VALUE(-ENODEV
);
324 result
= acpi_bus_register_driver(&acpi_ac_driver
);
326 remove_proc_entry(ACPI_AC_CLASS
, acpi_root_dir
);
327 return_VALUE(-ENODEV
);
337 ACPI_FUNCTION_TRACE("acpi_ac_exit");
339 acpi_bus_unregister_driver(&acpi_ac_driver
);
341 remove_proc_entry(ACPI_AC_CLASS
, acpi_root_dir
);
347 module_init(acpi_ac_init
);
348 module_exit(acpi_ac_exit
);