Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $) |
| 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 <asm/uaccess.h> |
| 33 | |
| 34 | #include <acpi/acpi_bus.h> |
| 35 | #include <acpi/acpi_drivers.h> |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #define ACPI_FAN_COMPONENT 0x00200000 |
| 38 | #define ACPI_FAN_CLASS "fan" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define ACPI_FAN_DRIVER_NAME "ACPI Fan Driver" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define ACPI_FAN_FILE_STATE "state" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | #define _COMPONENT ACPI_FAN_COMPONENT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 43 | ACPI_MODULE_NAME("acpi_fan") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 45 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | MODULE_DESCRIPTION(ACPI_FAN_DRIVER_NAME); |
| 47 | MODULE_LICENSE("GPL"); |
| 48 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 49 | static int acpi_fan_add(struct acpi_device *device); |
| 50 | static int acpi_fan_remove(struct acpi_device *device, int type); |
Konstantin Karasyov | 0feabb0 | 2006-05-08 00:00:00 -0400 | [diff] [blame] | 51 | static int acpi_fan_suspend(struct acpi_device *device, int state); |
| 52 | static int acpi_fan_resume(struct acpi_device *device, int state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | static struct acpi_driver acpi_fan_driver = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 55 | .name = ACPI_FAN_DRIVER_NAME, |
| 56 | .class = ACPI_FAN_CLASS, |
| 57 | .ids = "PNP0C0B", |
| 58 | .ops = { |
| 59 | .add = acpi_fan_add, |
| 60 | .remove = acpi_fan_remove, |
Konstantin Karasyov | 0feabb0 | 2006-05-08 00:00:00 -0400 | [diff] [blame] | 61 | .suspend = acpi_fan_suspend, |
| 62 | .resume = acpi_fan_resume, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 63 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct acpi_fan { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 67 | acpi_handle handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* -------------------------------------------------------------------------- |
| 71 | FS Interface (/proc) |
| 72 | -------------------------------------------------------------------------- */ |
| 73 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 74 | static struct proc_dir_entry *acpi_fan_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | static int acpi_fan_read_state(struct seq_file *seq, void *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 78 | struct acpi_fan *fan = seq->private; |
| 79 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | ACPI_FUNCTION_TRACE("acpi_fan_read_state"); |
| 82 | |
| 83 | if (fan) { |
| 84 | if (acpi_bus_get_power(fan->handle, &state)) |
| 85 | seq_printf(seq, "status: ERROR\n"); |
| 86 | else |
| 87 | seq_printf(seq, "status: %s\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 88 | !state ? "on" : "off"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | return_VALUE(0); |
| 91 | } |
| 92 | |
| 93 | static int acpi_fan_state_open_fs(struct inode *inode, struct file *file) |
| 94 | { |
| 95 | return single_open(file, acpi_fan_read_state, PDE(inode)->data); |
| 96 | } |
| 97 | |
| 98 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 99 | acpi_fan_write_state(struct file *file, const char __user * buffer, |
| 100 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 102 | int result = 0; |
| 103 | struct seq_file *m = (struct seq_file *)file->private_data; |
| 104 | struct acpi_fan *fan = (struct acpi_fan *)m->private; |
| 105 | char state_string[12] = { '\0' }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | ACPI_FUNCTION_TRACE("acpi_fan_write_state"); |
| 108 | |
| 109 | if (!fan || (count > sizeof(state_string) - 1)) |
| 110 | return_VALUE(-EINVAL); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | if (copy_from_user(state_string, buffer, count)) |
| 113 | return_VALUE(-EFAULT); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | state_string[count] = '\0'; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 116 | |
| 117 | result = acpi_bus_set_power(fan->handle, |
| 118 | simple_strtoul(state_string, NULL, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | if (result) |
| 120 | return_VALUE(result); |
| 121 | |
| 122 | return_VALUE(count); |
| 123 | } |
| 124 | |
| 125 | static struct file_operations acpi_fan_state_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 126 | .open = acpi_fan_state_open_fs, |
| 127 | .read = seq_read, |
| 128 | .write = acpi_fan_write_state, |
| 129 | .llseek = seq_lseek, |
| 130 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .owner = THIS_MODULE, |
| 132 | }; |
| 133 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 134 | static int acpi_fan_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 136 | struct proc_dir_entry *entry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | ACPI_FUNCTION_TRACE("acpi_fan_add_fs"); |
| 139 | |
| 140 | if (!device) |
| 141 | return_VALUE(-EINVAL); |
| 142 | |
| 143 | if (!acpi_device_dir(device)) { |
| 144 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 145 | acpi_fan_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (!acpi_device_dir(device)) |
| 147 | return_VALUE(-ENODEV); |
| 148 | acpi_device_dir(device)->owner = THIS_MODULE; |
| 149 | } |
| 150 | |
| 151 | /* 'status' [R/W] */ |
| 152 | entry = create_proc_entry(ACPI_FAN_FILE_STATE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 153 | S_IFREG | S_IRUGO | S_IWUSR, |
| 154 | acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (!entry) |
| 156 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 157 | "Unable to create '%s' fs entry\n", |
| 158 | ACPI_FAN_FILE_STATE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | else { |
| 160 | entry->proc_fops = &acpi_fan_state_ops; |
| 161 | entry->data = acpi_driver_data(device); |
| 162 | entry->owner = THIS_MODULE; |
| 163 | } |
| 164 | |
| 165 | return_VALUE(0); |
| 166 | } |
| 167 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 168 | static int acpi_fan_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
| 170 | ACPI_FUNCTION_TRACE("acpi_fan_remove_fs"); |
| 171 | |
| 172 | if (acpi_device_dir(device)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 173 | remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | remove_proc_entry(acpi_device_bid(device), acpi_fan_dir); |
| 175 | acpi_device_dir(device) = NULL; |
| 176 | } |
| 177 | |
| 178 | return_VALUE(0); |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* -------------------------------------------------------------------------- |
| 182 | Driver Interface |
| 183 | -------------------------------------------------------------------------- */ |
| 184 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 185 | static int acpi_fan_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 187 | int result = 0; |
| 188 | struct acpi_fan *fan = NULL; |
| 189 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | ACPI_FUNCTION_TRACE("acpi_fan_add"); |
| 192 | |
| 193 | if (!device) |
| 194 | return_VALUE(-EINVAL); |
| 195 | |
| 196 | fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL); |
| 197 | if (!fan) |
| 198 | return_VALUE(-ENOMEM); |
| 199 | memset(fan, 0, sizeof(struct acpi_fan)); |
| 200 | |
| 201 | fan->handle = device->handle; |
Pavel Machek | c65ade4 | 2005-08-05 00:37:45 -0400 | [diff] [blame] | 202 | strcpy(acpi_device_name(device), "Fan"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | strcpy(acpi_device_class(device), ACPI_FAN_CLASS); |
| 204 | acpi_driver_data(device) = fan; |
| 205 | |
| 206 | result = acpi_bus_get_power(fan->handle, &state); |
| 207 | if (result) { |
| 208 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 209 | "Error reading power state\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | goto end; |
| 211 | } |
| 212 | |
Konstantin Karasyov | 0feabb0 | 2006-05-08 00:00:00 -0400 | [diff] [blame] | 213 | device->flags.force_power_state = 1; |
| 214 | acpi_bus_set_power(device->handle, state); |
| 215 | device->flags.force_power_state = 0; |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | result = acpi_fan_add_fs(device); |
| 218 | if (result) |
| 219 | goto end; |
| 220 | |
| 221 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 222 | acpi_device_name(device), acpi_device_bid(device), |
| 223 | !device->power.state ? "on" : "off"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 225 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (result) |
| 227 | kfree(fan); |
| 228 | |
| 229 | return_VALUE(result); |
| 230 | } |
| 231 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 232 | static int acpi_fan_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Pavel Machek | c65ade4 | 2005-08-05 00:37:45 -0400 | [diff] [blame] | 234 | struct acpi_fan *fan = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | ACPI_FUNCTION_TRACE("acpi_fan_remove"); |
| 237 | |
| 238 | if (!device || !acpi_driver_data(device)) |
| 239 | return_VALUE(-EINVAL); |
| 240 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 241 | fan = (struct acpi_fan *)acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | acpi_fan_remove_fs(device); |
| 244 | |
| 245 | kfree(fan); |
| 246 | |
| 247 | return_VALUE(0); |
| 248 | } |
| 249 | |
Konstantin Karasyov | 0feabb0 | 2006-05-08 00:00:00 -0400 | [diff] [blame] | 250 | static int acpi_fan_suspend(struct acpi_device *device, int state) |
| 251 | { |
| 252 | if (!device) |
| 253 | return -EINVAL; |
| 254 | |
| 255 | acpi_bus_set_power(device->handle, ACPI_STATE_D0); |
| 256 | |
| 257 | return AE_OK; |
| 258 | } |
| 259 | |
| 260 | static int acpi_fan_resume(struct acpi_device *device, int state) |
| 261 | { |
| 262 | int result = 0; |
| 263 | int power_state = 0; |
| 264 | |
| 265 | if (!device) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | result = acpi_bus_get_power(device->handle, &power_state); |
| 269 | if (result) { |
| 270 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
| 271 | "Error reading fan power state\n")); |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | device->flags.force_power_state = 1; |
| 276 | acpi_bus_set_power(device->handle, power_state); |
| 277 | device->flags.force_power_state = 0; |
| 278 | |
| 279 | return result; |
| 280 | } |
| 281 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 282 | static int __init acpi_fan_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Pavel Machek | c65ade4 | 2005-08-05 00:37:45 -0400 | [diff] [blame] | 284 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | ACPI_FUNCTION_TRACE("acpi_fan_init"); |
| 287 | |
| 288 | acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir); |
| 289 | if (!acpi_fan_dir) |
| 290 | return_VALUE(-ENODEV); |
| 291 | acpi_fan_dir->owner = THIS_MODULE; |
| 292 | |
| 293 | result = acpi_bus_register_driver(&acpi_fan_driver); |
| 294 | if (result < 0) { |
| 295 | remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir); |
| 296 | return_VALUE(-ENODEV); |
| 297 | } |
| 298 | |
| 299 | return_VALUE(0); |
| 300 | } |
| 301 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 302 | static void __exit acpi_fan_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
| 304 | ACPI_FUNCTION_TRACE("acpi_fan_exit"); |
| 305 | |
| 306 | acpi_bus_unregister_driver(&acpi_fan_driver); |
| 307 | |
| 308 | remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir); |
| 309 | |
| 310 | return_VOID; |
| 311 | } |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | module_init(acpi_fan_init); |
| 314 | module_exit(acpi_fan_exit); |