Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * acpi_system.c - ACPI System Driver ($Revision: 63 $) |
| 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/proc_fs.h> |
| 27 | #include <linux/seq_file.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | |
| 31 | #include <acpi/acpi_drivers.h> |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #define _COMPONENT ACPI_SYSTEM_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 34 | ACPI_MODULE_NAME("system"); |
Zhang Rui | 5bb730f | 2007-01-29 11:02:42 +0800 | [diff] [blame] | 35 | #ifdef MODULE_PARAM_PREFIX |
| 36 | #undef MODULE_PARAM_PREFIX |
| 37 | #endif |
| 38 | #define MODULE_PARAM_PREFIX "acpi." |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define ACPI_SYSTEM_CLASS "system" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define ACPI_SYSTEM_DEVICE_NAME "System" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Zhang Rui | 5bb730f | 2007-01-29 11:02:42 +0800 | [diff] [blame] | 43 | /* |
| 44 | * Make ACPICA version work as module param |
| 45 | */ |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 46 | static int param_get_acpica_version(char *buffer, struct kernel_param *kp) |
| 47 | { |
Zhang Rui | 5bb730f | 2007-01-29 11:02:42 +0800 | [diff] [blame] | 48 | int result; |
| 49 | |
| 50 | result = sprintf(buffer, "%x", ACPI_CA_VERSION); |
| 51 | |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* -------------------------------------------------------------------------- |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 58 | FS Interface (/sys) |
| 59 | -------------------------------------------------------------------------- */ |
| 60 | static LIST_HEAD(acpi_table_attr_list); |
Greg Kroah-Hartman | a77aa28 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 61 | static struct kobject *tables_kobj; |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 62 | |
| 63 | struct acpi_table_attr { |
| 64 | struct bin_attribute attr; |
| 65 | char name[8]; |
| 66 | int instance; |
| 67 | struct list_head node; |
| 68 | }; |
| 69 | |
| 70 | static ssize_t acpi_table_show(struct kobject *kobj, |
| 71 | struct bin_attribute *bin_attr, char *buf, |
| 72 | loff_t offset, size_t count) |
| 73 | { |
| 74 | struct acpi_table_attr *table_attr = |
| 75 | container_of(bin_attr, struct acpi_table_attr, attr); |
| 76 | struct acpi_table_header *table_header = NULL; |
| 77 | acpi_status status; |
| 78 | ssize_t ret_count = count; |
| 79 | |
| 80 | status = |
| 81 | acpi_get_table(table_attr->name, table_attr->instance, |
| 82 | &table_header); |
| 83 | if (ACPI_FAILURE(status)) |
| 84 | return -ENODEV; |
| 85 | |
| 86 | if (offset >= table_header->length) { |
| 87 | ret_count = 0; |
| 88 | goto end; |
| 89 | } |
| 90 | |
| 91 | if (offset + ret_count > table_header->length) |
| 92 | ret_count = table_header->length - offset; |
| 93 | |
| 94 | memcpy(buf, ((char *)table_header) + offset, ret_count); |
| 95 | |
| 96 | end: |
| 97 | return ret_count; |
| 98 | } |
| 99 | |
| 100 | static void acpi_table_attr_init(struct acpi_table_attr *table_attr, |
| 101 | struct acpi_table_header *table_header) |
| 102 | { |
| 103 | struct acpi_table_header *header = NULL; |
| 104 | struct acpi_table_attr *attr = NULL; |
| 105 | |
| 106 | memcpy(table_attr->name, table_header->signature, ACPI_NAME_SIZE); |
| 107 | |
| 108 | list_for_each_entry(attr, &acpi_table_attr_list, node) { |
| 109 | if (!memcmp(table_header->signature, attr->name, |
| 110 | ACPI_NAME_SIZE)) |
| 111 | if (table_attr->instance < attr->instance) |
| 112 | table_attr->instance = attr->instance; |
| 113 | } |
| 114 | table_attr->instance++; |
| 115 | |
| 116 | if (table_attr->instance > 1 || (table_attr->instance == 1 && |
| 117 | !acpi_get_table(table_header-> |
| 118 | signature, 2, |
| 119 | &header))) |
| 120 | sprintf(table_attr->name + 4, "%d", table_attr->instance); |
| 121 | |
| 122 | table_attr->attr.size = 0; |
| 123 | table_attr->attr.read = acpi_table_show; |
| 124 | table_attr->attr.attr.name = table_attr->name; |
| 125 | table_attr->attr.attr.mode = 0444; |
| 126 | table_attr->attr.attr.owner = THIS_MODULE; |
| 127 | |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | static int acpi_system_sysfs_init(void) |
| 132 | { |
| 133 | struct acpi_table_attr *table_attr; |
| 134 | struct acpi_table_header *table_header = NULL; |
| 135 | int table_index = 0; |
| 136 | int result; |
| 137 | |
Greg Kroah-Hartman | a77aa28 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 138 | tables_kobj = kobject_create_and_add("tables", acpi_kobj); |
| 139 | if (!tables_kobj) |
| 140 | return -ENOMEM; |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 141 | |
| 142 | do { |
| 143 | result = acpi_get_table_by_index(table_index, &table_header); |
| 144 | if (!result) { |
| 145 | table_index++; |
| 146 | table_attr = NULL; |
| 147 | table_attr = |
| 148 | kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL); |
| 149 | if (!table_attr) |
| 150 | return -ENOMEM; |
| 151 | |
| 152 | acpi_table_attr_init(table_attr, table_header); |
| 153 | result = |
Greg Kroah-Hartman | a77aa28 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 154 | sysfs_create_bin_file(tables_kobj, |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 155 | &table_attr->attr); |
| 156 | if (result) { |
| 157 | kfree(table_attr); |
| 158 | return result; |
| 159 | } else |
| 160 | list_add_tail(&table_attr->node, |
| 161 | &acpi_table_attr_list); |
| 162 | } |
| 163 | } while (!result); |
Greg Kroah-Hartman | a77aa28 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 164 | kobject_uevent(tables_kobj, KOBJ_ADD); |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* -------------------------------------------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | FS Interface (/proc) |
| 171 | -------------------------------------------------------------------------- */ |
Zhang Rui | 5bb730f | 2007-01-29 11:02:42 +0800 | [diff] [blame] | 172 | #ifdef CONFIG_ACPI_PROCFS |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 173 | #define ACPI_SYSTEM_FILE_INFO "info" |
| 174 | #define ACPI_SYSTEM_FILE_EVENT "event" |
| 175 | #define ACPI_SYSTEM_FILE_DSDT "dsdt" |
| 176 | #define ACPI_SYSTEM_FILE_FADT "fadt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 178 | static int acpi_system_read_info(struct seq_file *seq, void *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | seq_printf(seq, "version: %x\n", ACPI_CA_VERSION); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 182 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static int acpi_system_info_open_fs(struct inode *inode, struct file *file) |
| 186 | { |
| 187 | return single_open(file, acpi_system_read_info, PDE(inode)->data); |
| 188 | } |
| 189 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 190 | static const struct file_operations acpi_system_info_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 191 | .open = acpi_system_info_open_fs, |
| 192 | .read = seq_read, |
| 193 | .llseek = seq_lseek, |
| 194 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 197 | static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t, |
| 198 | loff_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 200 | static const struct file_operations acpi_system_dsdt_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 201 | .read = acpi_system_read_dsdt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 205 | acpi_system_read_dsdt(struct file *file, |
| 206 | char __user * buffer, size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 208 | acpi_status status = AE_OK; |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 209 | struct acpi_table_header *dsdt = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 210 | ssize_t res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 212 | status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 214 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 216 | res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 218 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 221 | static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t, |
| 222 | loff_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 224 | static const struct file_operations acpi_system_fadt_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 225 | .read = acpi_system_read_fadt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 229 | acpi_system_read_fadt(struct file *file, |
| 230 | char __user * buffer, size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 232 | acpi_status status = AE_OK; |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 233 | struct acpi_table_header *fadt = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 234 | ssize_t res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 236 | status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 238 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 240 | res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 242 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 245 | static int acpi_system_procfs_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 247 | struct proc_dir_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | int error = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 249 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | /* 'info' [R] */ |
| 252 | name = ACPI_SYSTEM_FILE_INFO; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 253 | entry = create_proc_entry(name, S_IRUGO, acpi_root_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | if (!entry) |
| 255 | goto Error; |
| 256 | else { |
| 257 | entry->proc_fops = &acpi_system_info_ops; |
| 258 | } |
| 259 | |
| 260 | /* 'dsdt' [R] */ |
| 261 | name = ACPI_SYSTEM_FILE_DSDT; |
| 262 | entry = create_proc_entry(name, S_IRUSR, acpi_root_dir); |
| 263 | if (entry) |
| 264 | entry->proc_fops = &acpi_system_dsdt_ops; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 265 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | goto Error; |
| 267 | |
| 268 | /* 'fadt' [R] */ |
| 269 | name = ACPI_SYSTEM_FILE_FADT; |
| 270 | entry = create_proc_entry(name, S_IRUSR, acpi_root_dir); |
| 271 | if (entry) |
| 272 | entry->proc_fops = &acpi_system_fadt_ops; |
| 273 | else |
| 274 | goto Error; |
| 275 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 276 | Done: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 277 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 279 | Error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir); |
| 281 | remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir); |
| 282 | remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir); |
| 283 | |
| 284 | error = -EFAULT; |
| 285 | goto Done; |
| 286 | } |
Zhang Rui | d4c5f04 | 2007-06-14 17:43:07 +0800 | [diff] [blame] | 287 | #else |
| 288 | static int acpi_system_procfs_init(void) |
| 289 | { |
| 290 | return 0; |
| 291 | } |
| 292 | #endif |
| 293 | |
| 294 | static int __init acpi_system_init(void) |
| 295 | { |
| 296 | int result = 0; |
| 297 | |
| 298 | if (acpi_disabled) |
| 299 | return 0; |
| 300 | |
| 301 | result = acpi_system_procfs_init(); |
| 302 | if (result) |
| 303 | return result; |
| 304 | |
| 305 | result = acpi_system_sysfs_init(); |
| 306 | |
| 307 | return result; |
| 308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | subsys_initcall(acpi_system_init); |