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