2 * acpi_system.c - ACPI System Driver ($Revision: 63 $)
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/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <asm/uaccess.h>
32 #include <acpi/acpi_drivers.h>
34 #define _COMPONENT ACPI_SYSTEM_COMPONENT
35 ACPI_MODULE_NAME("system");
37 #define ACPI_SYSTEM_CLASS "system"
38 #define ACPI_SYSTEM_DEVICE_NAME "System"
43 * Make ACPICA version work as module param
45 static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
49 result = sprintf(buffer, "%x", ACPI_CA_VERSION);
54 module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
56 /* --------------------------------------------------------------------------
58 -------------------------------------------------------------------------- */
59 static LIST_HEAD(acpi_table_attr_list);
60 static struct kobject *tables_kobj;
61 static struct kobject *dynamic_tables_kobj;
63 struct acpi_table_attr {
64 struct bin_attribute attr;
67 struct list_head node;
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)
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;
78 char name[ACPI_NAME_SIZE];
80 if (strncmp(table_attr->name, "NULL", 4))
81 memcpy(name, table_attr->name, ACPI_NAME_SIZE);
83 memcpy(name, "\0\0\0\0", 4);
86 acpi_get_table(name, table_attr->instance,
88 if (ACPI_FAILURE(status))
91 return memory_read_from_buffer(buf, count, &offset,
92 table_header, table_header->length);
95 static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
96 struct acpi_table_header *table_header)
98 struct acpi_table_header *header = NULL;
99 struct acpi_table_attr *attr = NULL;
101 if (table_header->signature[0] != '\0')
102 memcpy(table_attr->name, table_header->signature,
105 memcpy(table_attr->name, "NULL", 4);
107 list_for_each_entry(attr, &acpi_table_attr_list, node) {
108 if (!memcmp(table_attr->name, attr->name, ACPI_NAME_SIZE))
109 if (table_attr->instance < attr->instance)
110 table_attr->instance = attr->instance;
112 table_attr->instance++;
114 if (table_attr->instance > 1 || (table_attr->instance == 1 &&
116 (table_header->signature, 2, &header)))
117 sprintf(table_attr->name + ACPI_NAME_SIZE, "%d",
118 table_attr->instance);
120 table_attr->attr.size = 0;
121 table_attr->attr.read = acpi_table_show;
122 table_attr->attr.attr.name = table_attr->name;
123 table_attr->attr.attr.mode = 0444;
129 acpi_sysfs_table_handler(u32 event, void *table, void *context)
131 struct acpi_table_attr *table_attr;
134 case ACPI_TABLE_EVENT_LOAD:
136 kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
140 acpi_table_attr_init(table_attr, table);
141 if (sysfs_create_bin_file(dynamic_tables_kobj,
142 &table_attr->attr)) {
146 list_add_tail(&table_attr->node,
147 &acpi_table_attr_list);
149 case ACPI_TABLE_EVENT_UNLOAD:
151 * we do not need to do anything right now
152 * because the table is not deleted from the
153 * global table list when unloading it.
157 return AE_BAD_PARAMETER;
162 static int acpi_system_sysfs_init(void)
164 struct acpi_table_attr *table_attr;
165 struct acpi_table_header *table_header = NULL;
169 tables_kobj = kobject_create_and_add("tables", acpi_kobj);
173 dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
174 if (!dynamic_tables_kobj)
175 goto err_dynamic_tables;
178 result = acpi_get_table_by_index(table_index, &table_header);
183 kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
187 acpi_table_attr_init(table_attr, table_header);
189 sysfs_create_bin_file(tables_kobj,
195 list_add_tail(&table_attr->node,
196 &acpi_table_attr_list);
199 kobject_uevent(tables_kobj, KOBJ_ADD);
200 kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
201 result = acpi_install_table_handler(acpi_sysfs_table_handler, NULL);
203 return result == AE_OK ? 0 : -EINVAL;
205 kobject_put(tables_kobj);
211 * Detailed ACPI IRQ counters in /sys/firmware/acpi/interrupts/
212 * See Documentation/ABI/testing/sysfs-firmware-acpi
216 #define COUNT_SCI 1 /* acpi_irq_handled */
217 #define COUNT_ERROR 2 /* other */
218 #define NUM_COUNTERS_EXTRA 3
220 struct event_counter {
225 static struct event_counter *all_counters;
227 static u32 num_counters;
228 static struct attribute **all_attrs;
229 static u32 acpi_gpe_count;
231 static struct attribute_group interrupt_stats_attr_group = {
232 .name = "interrupts",
234 static struct kobj_attribute *counter_attrs;
236 static void delete_gpe_attr_array(void)
238 struct event_counter *tmp = all_counters;
246 for (i = 0; i < num_gpes; i++)
247 kfree(counter_attrs[i].attr.name);
249 kfree(counter_attrs);
256 void acpi_os_gpe_count(u32 gpe_number)
263 if (gpe_number < num_gpes)
264 all_counters[gpe_number].count++;
266 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
272 void acpi_os_fixed_event_count(u32 event_number)
277 if (event_number < ACPI_NUM_FIXED_EVENTS)
278 all_counters[num_gpes + event_number].count++;
280 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
286 static int get_status(u32 index, acpi_event_status *status, acpi_handle *handle)
290 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
293 if (index < num_gpes) {
294 result = acpi_get_gpe_device(index, handle);
296 ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
297 "Invalid GPE 0x%x\n", index));
300 result = acpi_get_gpe_status(*handle, index,
301 ACPI_NOT_ISR, status);
302 } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
303 result = acpi_get_event_status(index - num_gpes, status);
309 static ssize_t counter_show(struct kobject *kobj,
310 struct kobj_attribute *attr, char *buf)
312 int index = attr - counter_attrs;
315 acpi_event_status status;
318 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
320 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
323 size = sprintf(buf, "%8d", all_counters[index].count);
325 /* "gpe_all" or "sci" */
326 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
329 result = get_status(index, &status, &handle);
333 if (!(status & ACPI_EVENT_FLAG_HANDLE))
334 size += sprintf(buf + size, " invalid");
335 else if (status & ACPI_EVENT_FLAG_ENABLED)
336 size += sprintf(buf + size, " enabled");
337 else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
338 size += sprintf(buf + size, " wake_enabled");
340 size += sprintf(buf + size, " disabled");
343 size += sprintf(buf + size, "\n");
344 return result ? result : size;
348 * counter_set() sets the specified counter.
349 * setting the total "sci" file to any value clears all counters.
350 * enable/disable/clear a gpe/fixed event in user space.
352 static ssize_t counter_set(struct kobject *kobj,
353 struct kobj_attribute *attr, const char *buf, size_t size)
355 int index = attr - counter_attrs;
356 acpi_event_status status;
360 if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
362 for (i = 0; i < num_counters; ++i)
363 all_counters[i].count = 0;
365 acpi_irq_handled = 0;
369 /* show the event status for both GPEs and Fixed Events */
370 result = get_status(index, &status, &handle);
374 if (!(status & ACPI_EVENT_FLAG_HANDLE)) {
375 printk(KERN_WARNING PREFIX
376 "Can not change Invalid GPE/Fixed Event status\n");
380 if (index < num_gpes) {
381 if (!strcmp(buf, "disable\n") &&
382 (status & ACPI_EVENT_FLAG_ENABLED))
383 result = acpi_disable_gpe(handle, index);
384 else if (!strcmp(buf, "enable\n") &&
385 !(status & ACPI_EVENT_FLAG_ENABLED))
386 result = acpi_enable_gpe(handle, index);
387 else if (!strcmp(buf, "clear\n") &&
388 (status & ACPI_EVENT_FLAG_SET))
389 result = acpi_clear_gpe(handle, index, ACPI_NOT_ISR);
391 all_counters[index].count = strtoul(buf, NULL, 0);
392 } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
393 int event = index - num_gpes;
394 if (!strcmp(buf, "disable\n") &&
395 (status & ACPI_EVENT_FLAG_ENABLED))
396 result = acpi_disable_event(event, ACPI_NOT_ISR);
397 else if (!strcmp(buf, "enable\n") &&
398 !(status & ACPI_EVENT_FLAG_ENABLED))
399 result = acpi_enable_event(event, ACPI_NOT_ISR);
400 else if (!strcmp(buf, "clear\n") &&
401 (status & ACPI_EVENT_FLAG_SET))
402 result = acpi_clear_event(event);
404 all_counters[index].count = strtoul(buf, NULL, 0);
406 all_counters[index].count = strtoul(buf, NULL, 0);
408 if (ACPI_FAILURE(result))
411 return result ? result : size;
414 void acpi_irq_stats_init(void)
421 num_gpes = acpi_current_gpe_count;
422 num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
424 all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
426 if (all_attrs == NULL)
429 all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
431 if (all_counters == NULL)
434 counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
436 if (counter_attrs == NULL)
439 for (i = 0; i < num_counters; ++i) {
444 sprintf(buffer, "gpe%02X", i);
445 else if (i == num_gpes + ACPI_EVENT_PMTIMER)
446 sprintf(buffer, "ff_pmtimer");
447 else if (i == num_gpes + ACPI_EVENT_GLOBAL)
448 sprintf(buffer, "ff_gbl_lock");
449 else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
450 sprintf(buffer, "ff_pwr_btn");
451 else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
452 sprintf(buffer, "ff_slp_btn");
453 else if (i == num_gpes + ACPI_EVENT_RTC)
454 sprintf(buffer, "ff_rt_clk");
455 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
456 sprintf(buffer, "gpe_all");
457 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
458 sprintf(buffer, "sci");
459 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
460 sprintf(buffer, "error");
462 sprintf(buffer, "bug%02X", i);
464 name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
467 strncpy(name, buffer, strlen(buffer) + 1);
469 counter_attrs[i].attr.name = name;
470 counter_attrs[i].attr.mode = 0644;
471 counter_attrs[i].show = counter_show;
472 counter_attrs[i].store = counter_set;
474 all_attrs[i] = &counter_attrs[i].attr;
477 interrupt_stats_attr_group.attrs = all_attrs;
478 if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
482 delete_gpe_attr_array();
486 static void __exit interrupt_stats_exit(void)
488 sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
490 delete_gpe_attr_array();
495 /* --------------------------------------------------------------------------
497 -------------------------------------------------------------------------- */
498 #ifdef CONFIG_ACPI_PROCFS
499 #define ACPI_SYSTEM_FILE_INFO "info"
500 #define ACPI_SYSTEM_FILE_EVENT "event"
501 #define ACPI_SYSTEM_FILE_DSDT "dsdt"
502 #define ACPI_SYSTEM_FILE_FADT "fadt"
504 static int acpi_system_read_info(struct seq_file *seq, void *offset)
507 seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
511 static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
513 return single_open(file, acpi_system_read_info, PDE(inode)->data);
516 static const struct file_operations acpi_system_info_ops = {
517 .owner = THIS_MODULE,
518 .open = acpi_system_info_open_fs,
521 .release = single_release,
524 static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
527 static const struct file_operations acpi_system_dsdt_ops = {
528 .owner = THIS_MODULE,
529 .read = acpi_system_read_dsdt,
533 acpi_system_read_dsdt(struct file *file,
534 char __user * buffer, size_t count, loff_t * ppos)
536 acpi_status status = AE_OK;
537 struct acpi_table_header *dsdt = NULL;
540 status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
541 if (ACPI_FAILURE(status))
544 res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
549 static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
552 static const struct file_operations acpi_system_fadt_ops = {
553 .owner = THIS_MODULE,
554 .read = acpi_system_read_fadt,
558 acpi_system_read_fadt(struct file *file,
559 char __user * buffer, size_t count, loff_t * ppos)
561 acpi_status status = AE_OK;
562 struct acpi_table_header *fadt = NULL;
565 status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
566 if (ACPI_FAILURE(status))
569 res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
574 static int acpi_system_procfs_init(void)
576 struct proc_dir_entry *entry;
580 entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
581 &acpi_system_info_ops);
586 entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
587 &acpi_system_dsdt_ops);
592 entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
593 &acpi_system_fadt_ops);
601 remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
602 remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
603 remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
609 static int acpi_system_procfs_init(void)
615 int __init acpi_system_init(void)
619 result = acpi_system_procfs_init();
623 result = acpi_system_sysfs_init();