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;
62 struct acpi_table_attr {
63 struct bin_attribute attr;
66 struct list_head node;
69 static ssize_t acpi_table_show(struct kobject *kobj,
70 struct bin_attribute *bin_attr, char *buf,
71 loff_t offset, size_t count)
73 struct acpi_table_attr *table_attr =
74 container_of(bin_attr, struct acpi_table_attr, attr);
75 struct acpi_table_header *table_header = NULL;
77 char name[ACPI_NAME_SIZE];
79 if (strncmp(table_attr->name, "NULL", 4))
80 memcpy(name, table_attr->name, ACPI_NAME_SIZE);
82 memcpy(name, "\0\0\0\0", 4);
85 acpi_get_table(name, table_attr->instance,
87 if (ACPI_FAILURE(status))
90 return memory_read_from_buffer(buf, count, &offset,
91 table_header, table_header->length);
94 static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
95 struct acpi_table_header *table_header)
97 struct acpi_table_header *header = NULL;
98 struct acpi_table_attr *attr = NULL;
100 if (table_header->signature[0] != '\0')
101 memcpy(table_attr->name, table_header->signature,
104 memcpy(table_attr->name, "NULL", 4);
106 list_for_each_entry(attr, &acpi_table_attr_list, node) {
107 if (!memcmp(table_attr->name, attr->name, ACPI_NAME_SIZE))
108 if (table_attr->instance < attr->instance)
109 table_attr->instance = attr->instance;
111 table_attr->instance++;
113 if (table_attr->instance > 1 || (table_attr->instance == 1 &&
115 (table_header->signature, 2, &header)))
116 sprintf(table_attr->name + ACPI_NAME_SIZE, "%d",
117 table_attr->instance);
119 table_attr->attr.size = 0;
120 table_attr->attr.read = acpi_table_show;
121 table_attr->attr.attr.name = table_attr->name;
122 table_attr->attr.attr.mode = 0444;
127 static int acpi_system_sysfs_init(void)
129 struct acpi_table_attr *table_attr;
130 struct acpi_table_header *table_header = NULL;
134 tables_kobj = kobject_create_and_add("tables", acpi_kobj);
139 result = acpi_get_table_by_index(table_index, &table_header);
144 kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
148 acpi_table_attr_init(table_attr, table_header);
150 sysfs_create_bin_file(tables_kobj,
156 list_add_tail(&table_attr->node,
157 &acpi_table_attr_list);
160 kobject_uevent(tables_kobj, KOBJ_ADD);
166 * Detailed ACPI IRQ counters in /sys/firmware/acpi/interrupts/
167 * See Documentation/ABI/testing/sysfs-firmware-acpi
171 #define COUNT_SCI 1 /* acpi_irq_handled */
172 #define COUNT_ERROR 2 /* other */
173 #define NUM_COUNTERS_EXTRA 3
175 struct event_counter {
180 static struct event_counter *all_counters;
182 static u32 num_counters;
183 static struct attribute **all_attrs;
184 static u32 acpi_gpe_count;
186 static struct attribute_group interrupt_stats_attr_group = {
187 .name = "interrupts",
189 static struct kobj_attribute *counter_attrs;
191 static void delete_gpe_attr_array(void)
193 struct event_counter *tmp = all_counters;
201 for (i = 0; i < num_gpes; i++)
202 kfree(counter_attrs[i].attr.name);
204 kfree(counter_attrs);
211 void acpi_os_gpe_count(u32 gpe_number)
218 if (gpe_number < num_gpes)
219 all_counters[gpe_number].count++;
221 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
227 void acpi_os_fixed_event_count(u32 event_number)
232 if (event_number < ACPI_NUM_FIXED_EVENTS)
233 all_counters[num_gpes + event_number].count++;
235 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
241 static int get_status(u32 index, acpi_event_status *status, acpi_handle *handle)
245 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
248 if (index < num_gpes) {
249 result = acpi_get_gpe_device(index, handle);
251 ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
252 "Invalid GPE 0x%x\n", index));
255 result = acpi_get_gpe_status(*handle, index,
256 ACPI_NOT_ISR, status);
257 } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
258 result = acpi_get_event_status(index - num_gpes, status);
264 static ssize_t counter_show(struct kobject *kobj,
265 struct kobj_attribute *attr, char *buf)
267 int index = attr - counter_attrs;
270 acpi_event_status status;
273 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
275 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
278 size = sprintf(buf, "%8d", all_counters[index].count);
280 /* "gpe_all" or "sci" */
281 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
284 result = get_status(index, &status, &handle);
288 if (!(status & ACPI_EVENT_FLAG_HANDLE))
289 size += sprintf(buf + size, " invalid");
290 else if (status & ACPI_EVENT_FLAG_ENABLED)
291 size += sprintf(buf + size, " enabled");
292 else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
293 size += sprintf(buf + size, " wake_enabled");
295 size += sprintf(buf + size, " disabled");
298 size += sprintf(buf + size, "\n");
299 return result ? result : size;
303 * counter_set() sets the specified counter.
304 * setting the total "sci" file to any value clears all counters.
305 * enable/disable/clear a gpe/fixed event in user space.
307 static ssize_t counter_set(struct kobject *kobj,
308 struct kobj_attribute *attr, const char *buf, size_t size)
310 int index = attr - counter_attrs;
311 acpi_event_status status;
315 if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
317 for (i = 0; i < num_counters; ++i)
318 all_counters[i].count = 0;
320 acpi_irq_handled = 0;
324 /* show the event status for both GPEs and Fixed Events */
325 result = get_status(index, &status, &handle);
329 if (!(status & ACPI_EVENT_FLAG_HANDLE)) {
330 printk(KERN_WARNING PREFIX
331 "Can not change Invalid GPE/Fixed Event status\n");
335 if (index < num_gpes) {
336 if (!strcmp(buf, "disable\n") &&
337 (status & ACPI_EVENT_FLAG_ENABLED))
338 result = acpi_disable_gpe(handle, index);
339 else if (!strcmp(buf, "enable\n") &&
340 !(status & ACPI_EVENT_FLAG_ENABLED))
341 result = acpi_enable_gpe(handle, index);
342 else if (!strcmp(buf, "clear\n") &&
343 (status & ACPI_EVENT_FLAG_SET))
344 result = acpi_clear_gpe(handle, index, ACPI_NOT_ISR);
346 all_counters[index].count = strtoul(buf, NULL, 0);
347 } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
348 int event = index - num_gpes;
349 if (!strcmp(buf, "disable\n") &&
350 (status & ACPI_EVENT_FLAG_ENABLED))
351 result = acpi_disable_event(event, ACPI_NOT_ISR);
352 else if (!strcmp(buf, "enable\n") &&
353 !(status & ACPI_EVENT_FLAG_ENABLED))
354 result = acpi_enable_event(event, ACPI_NOT_ISR);
355 else if (!strcmp(buf, "clear\n") &&
356 (status & ACPI_EVENT_FLAG_SET))
357 result = acpi_clear_event(event);
359 all_counters[index].count = strtoul(buf, NULL, 0);
361 all_counters[index].count = strtoul(buf, NULL, 0);
363 if (ACPI_FAILURE(result))
366 return result ? result : size;
369 void acpi_irq_stats_init(void)
376 num_gpes = acpi_current_gpe_count;
377 num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
379 all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
381 if (all_attrs == NULL)
384 all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
386 if (all_counters == NULL)
389 counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
391 if (counter_attrs == NULL)
394 for (i = 0; i < num_counters; ++i) {
399 sprintf(buffer, "gpe%02X", i);
400 else if (i == num_gpes + ACPI_EVENT_PMTIMER)
401 sprintf(buffer, "ff_pmtimer");
402 else if (i == num_gpes + ACPI_EVENT_GLOBAL)
403 sprintf(buffer, "ff_gbl_lock");
404 else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
405 sprintf(buffer, "ff_pwr_btn");
406 else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
407 sprintf(buffer, "ff_slp_btn");
408 else if (i == num_gpes + ACPI_EVENT_RTC)
409 sprintf(buffer, "ff_rt_clk");
410 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
411 sprintf(buffer, "gpe_all");
412 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
413 sprintf(buffer, "sci");
414 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
415 sprintf(buffer, "error");
417 sprintf(buffer, "bug%02X", i);
419 name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
422 strncpy(name, buffer, strlen(buffer) + 1);
424 counter_attrs[i].attr.name = name;
425 counter_attrs[i].attr.mode = 0644;
426 counter_attrs[i].show = counter_show;
427 counter_attrs[i].store = counter_set;
429 all_attrs[i] = &counter_attrs[i].attr;
432 interrupt_stats_attr_group.attrs = all_attrs;
433 if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
437 delete_gpe_attr_array();
441 static void __exit interrupt_stats_exit(void)
443 sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
445 delete_gpe_attr_array();
450 /* --------------------------------------------------------------------------
452 -------------------------------------------------------------------------- */
453 #ifdef CONFIG_ACPI_PROCFS
454 #define ACPI_SYSTEM_FILE_INFO "info"
455 #define ACPI_SYSTEM_FILE_EVENT "event"
456 #define ACPI_SYSTEM_FILE_DSDT "dsdt"
457 #define ACPI_SYSTEM_FILE_FADT "fadt"
459 static int acpi_system_read_info(struct seq_file *seq, void *offset)
462 seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
466 static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
468 return single_open(file, acpi_system_read_info, PDE(inode)->data);
471 static const struct file_operations acpi_system_info_ops = {
472 .owner = THIS_MODULE,
473 .open = acpi_system_info_open_fs,
476 .release = single_release,
479 static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
482 static const struct file_operations acpi_system_dsdt_ops = {
483 .owner = THIS_MODULE,
484 .read = acpi_system_read_dsdt,
488 acpi_system_read_dsdt(struct file *file,
489 char __user * buffer, size_t count, loff_t * ppos)
491 acpi_status status = AE_OK;
492 struct acpi_table_header *dsdt = NULL;
495 status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
496 if (ACPI_FAILURE(status))
499 res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
504 static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
507 static const struct file_operations acpi_system_fadt_ops = {
508 .owner = THIS_MODULE,
509 .read = acpi_system_read_fadt,
513 acpi_system_read_fadt(struct file *file,
514 char __user * buffer, size_t count, loff_t * ppos)
516 acpi_status status = AE_OK;
517 struct acpi_table_header *fadt = NULL;
520 status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
521 if (ACPI_FAILURE(status))
524 res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
529 static int acpi_system_procfs_init(void)
531 struct proc_dir_entry *entry;
535 entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
536 &acpi_system_info_ops);
541 entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
542 &acpi_system_dsdt_ops);
547 entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
548 &acpi_system_fadt_ops);
556 remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
557 remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
558 remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
564 static int acpi_system_procfs_init(void)
570 static int __init acpi_system_init(void)
577 result = acpi_system_procfs_init();
581 result = acpi_system_sysfs_init();
586 subsys_initcall(acpi_system_init);