ACPI: simplify module_param namespace
[safe/jmp/linux-2.6] / drivers / acpi / system.c
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 <linux/string.h>
30 #include <asm/uaccess.h>
31
32 #include <acpi/acpi_drivers.h>
33
34 #define _COMPONENT              ACPI_SYSTEM_COMPONENT
35 ACPI_MODULE_NAME("system");
36
37 #define ACPI_SYSTEM_CLASS               "system"
38 #define ACPI_SYSTEM_DEVICE_NAME         "System"
39
40 u32 acpi_irq_handled;
41
42 /*
43  * Make ACPICA version work as module param
44  */
45 static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
46 {
47         int result;
48
49         result = sprintf(buffer, "%x", ACPI_CA_VERSION);
50
51         return result;
52 }
53
54 module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
55
56 /* --------------------------------------------------------------------------
57                               FS Interface (/sys)
58    -------------------------------------------------------------------------- */
59 static LIST_HEAD(acpi_table_attr_list);
60 static struct kobject *tables_kobj;
61
62 struct acpi_table_attr {
63         struct bin_attribute attr;
64         char name[8];
65         int instance;
66         struct list_head node;
67 };
68
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)
72 {
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;
76         acpi_status status;
77         char name[ACPI_NAME_SIZE];
78
79         if (strncmp(table_attr->name, "NULL", 4))
80                 memcpy(name, table_attr->name, ACPI_NAME_SIZE);
81         else
82                 memcpy(name, "\0\0\0\0", 4);
83
84         status =
85             acpi_get_table(name, table_attr->instance,
86                            &table_header);
87         if (ACPI_FAILURE(status))
88                 return -ENODEV;
89
90         return memory_read_from_buffer(buf, count, &offset,
91                                         table_header, table_header->length);
92 }
93
94 static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
95                                  struct acpi_table_header *table_header)
96 {
97         struct acpi_table_header *header = NULL;
98         struct acpi_table_attr *attr = NULL;
99
100         if (table_header->signature[0] != '\0')
101                 memcpy(table_attr->name, table_header->signature,
102                         ACPI_NAME_SIZE);
103         else
104                 memcpy(table_attr->name, "NULL", 4);
105
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;
110         }
111         table_attr->instance++;
112
113         if (table_attr->instance > 1 || (table_attr->instance == 1 &&
114                                         !acpi_get_table
115                                         (table_header->signature, 2, &header)))
116                 sprintf(table_attr->name + ACPI_NAME_SIZE, "%d",
117                         table_attr->instance);
118
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;
123
124         return;
125 }
126
127 static int acpi_system_sysfs_init(void)
128 {
129         struct acpi_table_attr *table_attr;
130         struct acpi_table_header *table_header = NULL;
131         int table_index = 0;
132         int result;
133
134         tables_kobj = kobject_create_and_add("tables", acpi_kobj);
135         if (!tables_kobj)
136                 return -ENOMEM;
137
138         do {
139                 result = acpi_get_table_by_index(table_index, &table_header);
140                 if (!result) {
141                         table_index++;
142                         table_attr = NULL;
143                         table_attr =
144                             kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
145                         if (!table_attr)
146                                 return -ENOMEM;
147
148                         acpi_table_attr_init(table_attr, table_header);
149                         result =
150                             sysfs_create_bin_file(tables_kobj,
151                                                   &table_attr->attr);
152                         if (result) {
153                                 kfree(table_attr);
154                                 return result;
155                         } else
156                                 list_add_tail(&table_attr->node,
157                                               &acpi_table_attr_list);
158                 }
159         } while (!result);
160         kobject_uevent(tables_kobj, KOBJ_ADD);
161
162         return 0;
163 }
164
165 /*
166  * Detailed ACPI IRQ counters in /sys/firmware/acpi/interrupts/
167  * See Documentation/ABI/testing/sysfs-firmware-acpi
168  */
169
170 #define COUNT_GPE 0
171 #define COUNT_SCI 1     /* acpi_irq_handled */
172 #define COUNT_ERROR 2   /* other */
173 #define NUM_COUNTERS_EXTRA 3
174
175 struct event_counter {
176         u32 count;
177         u32 flags;
178 };
179
180 static struct event_counter *all_counters;
181 static u32 num_gpes;
182 static u32 num_counters;
183 static struct attribute **all_attrs;
184 static u32 acpi_gpe_count;
185
186 static struct attribute_group interrupt_stats_attr_group = {
187         .name = "interrupts",
188 };
189 static struct kobj_attribute *counter_attrs;
190
191 static void delete_gpe_attr_array(void)
192 {
193         struct event_counter *tmp = all_counters;
194
195         all_counters = NULL;
196         kfree(tmp);
197
198         if (counter_attrs) {
199                 int i;
200
201                 for (i = 0; i < num_gpes; i++)
202                         kfree(counter_attrs[i].attr.name);
203
204                 kfree(counter_attrs);
205         }
206         kfree(all_attrs);
207
208         return;
209 }
210
211 void acpi_os_gpe_count(u32 gpe_number)
212 {
213         acpi_gpe_count++;
214
215         if (!all_counters)
216                 return;
217
218         if (gpe_number < num_gpes)
219                 all_counters[gpe_number].count++;
220         else
221                 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
222                                         count++;
223
224         return;
225 }
226
227 void acpi_os_fixed_event_count(u32 event_number)
228 {
229         if (!all_counters)
230                 return;
231
232         if (event_number < ACPI_NUM_FIXED_EVENTS)
233                 all_counters[num_gpes + event_number].count++;
234         else
235                 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
236                                 count++;
237
238         return;
239 }
240
241 static int get_status(u32 index, acpi_event_status *status, acpi_handle *handle)
242 {
243         int result = 0;
244
245         if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
246                 goto end;
247
248         if (index < num_gpes) {
249                 result = acpi_get_gpe_device(index, handle);
250                 if (result) {
251                         ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
252                                 "Invalid GPE 0x%x\n", index));
253                         goto end;
254                 }
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);
259
260 end:
261         return result;
262 }
263
264 static ssize_t counter_show(struct kobject *kobj,
265         struct kobj_attribute *attr, char *buf)
266 {
267         int index = attr - counter_attrs;
268         int size;
269         acpi_handle handle;
270         acpi_event_status status;
271         int result = 0;
272
273         all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
274                 acpi_irq_handled;
275         all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
276                 acpi_gpe_count;
277
278         size = sprintf(buf, "%8d", all_counters[index].count);
279
280         /* "gpe_all" or "sci" */
281         if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
282                 goto end;
283
284         result = get_status(index, &status, &handle);
285         if (result)
286                 goto end;
287
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");
294         else
295                 size += sprintf(buf + size, "   disabled");
296
297 end:
298         size += sprintf(buf + size, "\n");
299         return result ? result : size;
300 }
301
302 /*
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.
306  */
307 static ssize_t counter_set(struct kobject *kobj,
308         struct kobj_attribute *attr, const char *buf, size_t size)
309 {
310         int index = attr - counter_attrs;
311         acpi_event_status status;
312         acpi_handle handle;
313         int result = 0;
314
315         if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
316                 int i;
317                 for (i = 0; i < num_counters; ++i)
318                         all_counters[i].count = 0;
319                 acpi_gpe_count = 0;
320                 acpi_irq_handled = 0;
321                 goto end;
322         }
323
324         /* show the event status for both GPEs and Fixed Events */
325         result = get_status(index, &status, &handle);
326         if (result)
327                 goto end;
328
329         if (!(status & ACPI_EVENT_FLAG_HANDLE)) {
330                 printk(KERN_WARNING PREFIX
331                         "Can not change Invalid GPE/Fixed Event status\n");
332                 return -EINVAL;
333         }
334
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);
345                 else
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);
358                 else
359                         all_counters[index].count = strtoul(buf, NULL, 0);
360         } else
361                 all_counters[index].count = strtoul(buf, NULL, 0);
362
363         if (ACPI_FAILURE(result))
364                 result = -EINVAL;
365 end:
366         return result ? result : size;
367 }
368
369 void acpi_irq_stats_init(void)
370 {
371         int i;
372
373         if (all_counters)
374                 return;
375
376         num_gpes = acpi_current_gpe_count;
377         num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
378
379         all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
380                         GFP_KERNEL);
381         if (all_attrs == NULL)
382                 return;
383
384         all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
385                                 GFP_KERNEL);
386         if (all_counters == NULL)
387                 goto fail;
388
389         counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
390                         GFP_KERNEL);
391         if (counter_attrs == NULL)
392                 goto fail;
393
394         for (i = 0; i < num_counters; ++i) {
395                 char buffer[12];
396                 char *name;
397
398                 if (i < num_gpes)
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");
416                 else
417                         sprintf(buffer, "bug%02X", i);
418
419                 name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
420                 if (name == NULL)
421                         goto fail;
422                 strncpy(name, buffer, strlen(buffer) + 1);
423
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;
428
429                 all_attrs[i] = &counter_attrs[i].attr;
430         }
431
432         interrupt_stats_attr_group.attrs = all_attrs;
433         if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
434                 return;
435
436 fail:
437         delete_gpe_attr_array();
438         return;
439 }
440
441 static void __exit interrupt_stats_exit(void)
442 {
443         sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
444
445         delete_gpe_attr_array();
446
447         return;
448 }
449
450 /* --------------------------------------------------------------------------
451                               FS Interface (/proc)
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"
458
459 static int acpi_system_read_info(struct seq_file *seq, void *offset)
460 {
461
462         seq_printf(seq, "version:                 %x\n", ACPI_CA_VERSION);
463         return 0;
464 }
465
466 static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
467 {
468         return single_open(file, acpi_system_read_info, PDE(inode)->data);
469 }
470
471 static const struct file_operations acpi_system_info_ops = {
472         .owner = THIS_MODULE,
473         .open = acpi_system_info_open_fs,
474         .read = seq_read,
475         .llseek = seq_lseek,
476         .release = single_release,
477 };
478
479 static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
480                                      loff_t *);
481
482 static const struct file_operations acpi_system_dsdt_ops = {
483         .owner = THIS_MODULE,
484         .read = acpi_system_read_dsdt,
485 };
486
487 static ssize_t
488 acpi_system_read_dsdt(struct file *file,
489                       char __user * buffer, size_t count, loff_t * ppos)
490 {
491         acpi_status status = AE_OK;
492         struct acpi_table_header *dsdt = NULL;
493         ssize_t res;
494
495         status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
496         if (ACPI_FAILURE(status))
497                 return -ENODEV;
498
499         res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
500
501         return res;
502 }
503
504 static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
505                                      loff_t *);
506
507 static const struct file_operations acpi_system_fadt_ops = {
508         .owner = THIS_MODULE,
509         .read = acpi_system_read_fadt,
510 };
511
512 static ssize_t
513 acpi_system_read_fadt(struct file *file,
514                       char __user * buffer, size_t count, loff_t * ppos)
515 {
516         acpi_status status = AE_OK;
517         struct acpi_table_header *fadt = NULL;
518         ssize_t res;
519
520         status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
521         if (ACPI_FAILURE(status))
522                 return -ENODEV;
523
524         res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
525
526         return res;
527 }
528
529 static int acpi_system_procfs_init(void)
530 {
531         struct proc_dir_entry *entry;
532         int error = 0;
533
534         /* 'info' [R] */
535         entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
536                             &acpi_system_info_ops);
537         if (!entry)
538                 goto Error;
539
540         /* 'dsdt' [R] */
541         entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
542                             &acpi_system_dsdt_ops);
543         if (!entry)
544                 goto Error;
545
546         /* 'fadt' [R] */
547         entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
548                             &acpi_system_fadt_ops);
549         if (!entry)
550                 goto Error;
551
552       Done:
553         return error;
554
555       Error:
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);
559
560         error = -EFAULT;
561         goto Done;
562 }
563 #else
564 static int acpi_system_procfs_init(void)
565 {
566         return 0;
567 }
568 #endif
569
570 static int __init acpi_system_init(void)
571 {
572         int result = 0;
573
574         if (acpi_disabled)
575                 return 0;
576
577         result = acpi_system_procfs_init();
578         if (result)
579                 return result;
580
581         result = acpi_system_sysfs_init();
582
583         return result;
584 }
585
586 subsys_initcall(acpi_system_init);