ACPI / sysfs: Update method tracing facility.
[cascardo/linux.git] / drivers / acpi / sysfs.c
1 /*
2  * sysfs.c - ACPI sysfs interface to userspace.
3  */
4
5 #include <linux/init.h>
6 #include <linux/kernel.h>
7 #include <linux/moduleparam.h>
8 #include <linux/acpi.h>
9
10 #include "internal.h"
11
12 #define _COMPONENT              ACPI_SYSTEM_COMPONENT
13 ACPI_MODULE_NAME("sysfs");
14
15 #ifdef CONFIG_ACPI_DEBUG
16 /*
17  * ACPI debug sysfs I/F, including:
18  * /sys/modules/acpi/parameters/debug_layer
19  * /sys/modules/acpi/parameters/debug_level
20  * /sys/modules/acpi/parameters/trace_method_name
21  * /sys/modules/acpi/parameters/trace_state
22  * /sys/modules/acpi/parameters/trace_debug_layer
23  * /sys/modules/acpi/parameters/trace_debug_level
24  */
25
26 struct acpi_dlayer {
27         const char *name;
28         unsigned long value;
29 };
30 struct acpi_dlevel {
31         const char *name;
32         unsigned long value;
33 };
34 #define ACPI_DEBUG_INIT(v)      { .name = #v, .value = v }
35
36 static const struct acpi_dlayer acpi_debug_layers[] = {
37         ACPI_DEBUG_INIT(ACPI_UTILITIES),
38         ACPI_DEBUG_INIT(ACPI_HARDWARE),
39         ACPI_DEBUG_INIT(ACPI_EVENTS),
40         ACPI_DEBUG_INIT(ACPI_TABLES),
41         ACPI_DEBUG_INIT(ACPI_NAMESPACE),
42         ACPI_DEBUG_INIT(ACPI_PARSER),
43         ACPI_DEBUG_INIT(ACPI_DISPATCHER),
44         ACPI_DEBUG_INIT(ACPI_EXECUTER),
45         ACPI_DEBUG_INIT(ACPI_RESOURCES),
46         ACPI_DEBUG_INIT(ACPI_CA_DEBUGGER),
47         ACPI_DEBUG_INIT(ACPI_OS_SERVICES),
48         ACPI_DEBUG_INIT(ACPI_CA_DISASSEMBLER),
49         ACPI_DEBUG_INIT(ACPI_COMPILER),
50         ACPI_DEBUG_INIT(ACPI_TOOLS),
51
52         ACPI_DEBUG_INIT(ACPI_BUS_COMPONENT),
53         ACPI_DEBUG_INIT(ACPI_AC_COMPONENT),
54         ACPI_DEBUG_INIT(ACPI_BATTERY_COMPONENT),
55         ACPI_DEBUG_INIT(ACPI_BUTTON_COMPONENT),
56         ACPI_DEBUG_INIT(ACPI_SBS_COMPONENT),
57         ACPI_DEBUG_INIT(ACPI_FAN_COMPONENT),
58         ACPI_DEBUG_INIT(ACPI_PCI_COMPONENT),
59         ACPI_DEBUG_INIT(ACPI_POWER_COMPONENT),
60         ACPI_DEBUG_INIT(ACPI_CONTAINER_COMPONENT),
61         ACPI_DEBUG_INIT(ACPI_SYSTEM_COMPONENT),
62         ACPI_DEBUG_INIT(ACPI_THERMAL_COMPONENT),
63         ACPI_DEBUG_INIT(ACPI_MEMORY_DEVICE_COMPONENT),
64         ACPI_DEBUG_INIT(ACPI_VIDEO_COMPONENT),
65         ACPI_DEBUG_INIT(ACPI_PROCESSOR_COMPONENT),
66 };
67
68 static const struct acpi_dlevel acpi_debug_levels[] = {
69         ACPI_DEBUG_INIT(ACPI_LV_INIT),
70         ACPI_DEBUG_INIT(ACPI_LV_DEBUG_OBJECT),
71         ACPI_DEBUG_INIT(ACPI_LV_INFO),
72         ACPI_DEBUG_INIT(ACPI_LV_REPAIR),
73         ACPI_DEBUG_INIT(ACPI_LV_TRACE_POINT),
74
75         ACPI_DEBUG_INIT(ACPI_LV_INIT_NAMES),
76         ACPI_DEBUG_INIT(ACPI_LV_PARSE),
77         ACPI_DEBUG_INIT(ACPI_LV_LOAD),
78         ACPI_DEBUG_INIT(ACPI_LV_DISPATCH),
79         ACPI_DEBUG_INIT(ACPI_LV_EXEC),
80         ACPI_DEBUG_INIT(ACPI_LV_NAMES),
81         ACPI_DEBUG_INIT(ACPI_LV_OPREGION),
82         ACPI_DEBUG_INIT(ACPI_LV_BFIELD),
83         ACPI_DEBUG_INIT(ACPI_LV_TABLES),
84         ACPI_DEBUG_INIT(ACPI_LV_VALUES),
85         ACPI_DEBUG_INIT(ACPI_LV_OBJECTS),
86         ACPI_DEBUG_INIT(ACPI_LV_RESOURCES),
87         ACPI_DEBUG_INIT(ACPI_LV_USER_REQUESTS),
88         ACPI_DEBUG_INIT(ACPI_LV_PACKAGE),
89
90         ACPI_DEBUG_INIT(ACPI_LV_ALLOCATIONS),
91         ACPI_DEBUG_INIT(ACPI_LV_FUNCTIONS),
92         ACPI_DEBUG_INIT(ACPI_LV_OPTIMIZATIONS),
93
94         ACPI_DEBUG_INIT(ACPI_LV_MUTEX),
95         ACPI_DEBUG_INIT(ACPI_LV_THREADS),
96         ACPI_DEBUG_INIT(ACPI_LV_IO),
97         ACPI_DEBUG_INIT(ACPI_LV_INTERRUPTS),
98
99         ACPI_DEBUG_INIT(ACPI_LV_AML_DISASSEMBLE),
100         ACPI_DEBUG_INIT(ACPI_LV_VERBOSE_INFO),
101         ACPI_DEBUG_INIT(ACPI_LV_FULL_TABLES),
102         ACPI_DEBUG_INIT(ACPI_LV_EVENTS),
103 };
104
105 static int param_get_debug_layer(char *buffer, const struct kernel_param *kp)
106 {
107         int result = 0;
108         int i;
109
110         result = sprintf(buffer, "%-25s\tHex        SET\n", "Description");
111
112         for (i = 0; i < ARRAY_SIZE(acpi_debug_layers); i++) {
113                 result += sprintf(buffer + result, "%-25s\t0x%08lX [%c]\n",
114                                   acpi_debug_layers[i].name,
115                                   acpi_debug_layers[i].value,
116                                   (acpi_dbg_layer & acpi_debug_layers[i].value)
117                                   ? '*' : ' ');
118         }
119         result +=
120             sprintf(buffer + result, "%-25s\t0x%08X [%c]\n", "ACPI_ALL_DRIVERS",
121                     ACPI_ALL_DRIVERS,
122                     (acpi_dbg_layer & ACPI_ALL_DRIVERS) ==
123                     ACPI_ALL_DRIVERS ? '*' : (acpi_dbg_layer & ACPI_ALL_DRIVERS)
124                     == 0 ? ' ' : '-');
125         result +=
126             sprintf(buffer + result,
127                     "--\ndebug_layer = 0x%08X ( * = enabled)\n",
128                     acpi_dbg_layer);
129
130         return result;
131 }
132
133 static int param_get_debug_level(char *buffer, const struct kernel_param *kp)
134 {
135         int result = 0;
136         int i;
137
138         result = sprintf(buffer, "%-25s\tHex        SET\n", "Description");
139
140         for (i = 0; i < ARRAY_SIZE(acpi_debug_levels); i++) {
141                 result += sprintf(buffer + result, "%-25s\t0x%08lX [%c]\n",
142                                   acpi_debug_levels[i].name,
143                                   acpi_debug_levels[i].value,
144                                   (acpi_dbg_level & acpi_debug_levels[i].value)
145                                   ? '*' : ' ');
146         }
147         result +=
148             sprintf(buffer + result, "--\ndebug_level = 0x%08X (* = enabled)\n",
149                     acpi_dbg_level);
150
151         return result;
152 }
153
154 static const struct kernel_param_ops param_ops_debug_layer = {
155         .set = param_set_uint,
156         .get = param_get_debug_layer,
157 };
158
159 static const struct kernel_param_ops param_ops_debug_level = {
160         .set = param_set_uint,
161         .get = param_get_debug_level,
162 };
163
164 module_param_cb(debug_layer, &param_ops_debug_layer, &acpi_dbg_layer, 0644);
165 module_param_cb(debug_level, &param_ops_debug_level, &acpi_dbg_level, 0644);
166
167 static char* trace_method_name;
168 static bool trace_method_kmalloced;
169
170 int param_set_trace_method_name(const char *val, const struct kernel_param *kp)
171 {
172         u32 saved_flags = 0;
173
174         if (strlen(val) > 1024) {
175                 pr_err("%s: string parameter too long\n", kp->name);
176                 return -ENOSPC;
177         }
178
179         /*
180          * It's not safe to update acpi_gbl_trace_method_name without
181          * having the tracer stopped, so we save the original tracer
182          * state and disable it.
183          */
184         saved_flags = acpi_gbl_trace_flags;
185         (void)acpi_debug_trace(NULL,
186                                acpi_gbl_trace_dbg_level,
187                                acpi_gbl_trace_dbg_layer,
188                                0);
189
190         if (trace_method_kmalloced)
191                 kfree(trace_method_name);
192         trace_method_name = NULL;
193         trace_method_kmalloced = false;
194
195         /* This is a hack.  We can't kmalloc in early boot. */
196         if (slab_is_available()) {
197                 trace_method_name = kstrdup(val, GFP_KERNEL);
198                 if (!trace_method_name)
199                         return -ENOMEM;
200                 trace_method_kmalloced = true;
201         } else
202                 trace_method_name = (char *)val;
203
204         /* Restore the original tracer state */
205         (void)acpi_debug_trace(trace_method_name,
206                                acpi_gbl_trace_dbg_level,
207                                acpi_gbl_trace_dbg_layer,
208                                saved_flags);
209
210         return 0;
211 }
212
213 static int param_get_trace_method_name(char *buffer, const struct kernel_param *kp)
214 {
215         return scnprintf(buffer, PAGE_SIZE, "%s", acpi_gbl_trace_method_name);
216 }
217
218 static const struct kernel_param_ops param_ops_trace_method = {
219         .set = param_set_trace_method_name,
220         .get = param_get_trace_method_name,
221 };
222
223 static const struct kernel_param_ops param_ops_trace_attrib = {
224         .set = param_set_uint,
225         .get = param_get_uint,
226 };
227
228 module_param_cb(trace_method_name, &param_ops_trace_method, &trace_method_name, 0644);
229 module_param_cb(trace_debug_layer, &param_ops_trace_attrib, &acpi_gbl_trace_dbg_layer, 0644);
230 module_param_cb(trace_debug_level, &param_ops_trace_attrib, &acpi_gbl_trace_dbg_level, 0644);
231
232 static int param_set_trace_state(const char *val, struct kernel_param *kp)
233 {
234         acpi_status status;
235         const char *method = trace_method_name;
236         u32 flags = 0;
237
238 /* So "xxx-once" comparison should go prior than "xxx" comparison */
239 #define acpi_compare_param(val, key)    \
240         strncmp((val), (key), sizeof(key) - 1)
241
242         if (!acpi_compare_param(val, "enable")) {
243                 method = NULL;
244                 flags = ACPI_TRACE_ENABLED;
245         } else if (!acpi_compare_param(val, "disable"))
246                 method = NULL;
247         else if (!acpi_compare_param(val, "method-once"))
248                 flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT;
249         else if (!acpi_compare_param(val, "method"))
250                 flags = ACPI_TRACE_ENABLED;
251         else if (!acpi_compare_param(val, "opcode-once"))
252                 flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT | ACPI_TRACE_OPCODE;
253         else if (!acpi_compare_param(val, "opcode"))
254                 flags = ACPI_TRACE_ENABLED | ACPI_TRACE_OPCODE;
255         else
256                 return -EINVAL;
257
258         status = acpi_debug_trace(method,
259                                   acpi_gbl_trace_dbg_level,
260                                   acpi_gbl_trace_dbg_layer,
261                                   flags);
262         if (ACPI_FAILURE(status))
263                 return -EBUSY;
264
265         return 0;
266 }
267
268 static int param_get_trace_state(char *buffer, struct kernel_param *kp)
269 {
270         if (!(acpi_gbl_trace_flags & ACPI_TRACE_ENABLED))
271                 return sprintf(buffer, "disable");
272         else {
273                 if (acpi_gbl_trace_method_name) {
274                         if (acpi_gbl_trace_flags & ACPI_TRACE_ONESHOT)
275                                 return sprintf(buffer, "method-once");
276                         else
277                                 return sprintf(buffer, "method");
278                 } else
279                         return sprintf(buffer, "enable");
280         }
281         return 0;
282 }
283
284 module_param_call(trace_state, param_set_trace_state, param_get_trace_state,
285                   NULL, 0644);
286 #endif /* CONFIG_ACPI_DEBUG */
287
288
289 /* /sys/modules/acpi/parameters/aml_debug_output */
290
291 module_param_named(aml_debug_output, acpi_gbl_enable_aml_debug_object,
292                    byte, 0644);
293 MODULE_PARM_DESC(aml_debug_output,
294                  "To enable/disable the ACPI Debug Object output.");
295
296 /* /sys/module/acpi/parameters/acpica_version */
297 static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
298 {
299         int result;
300
301         result = sprintf(buffer, "%x", ACPI_CA_VERSION);
302
303         return result;
304 }
305
306 module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
307
308 /*
309  * ACPI table sysfs I/F:
310  * /sys/firmware/acpi/tables/
311  * /sys/firmware/acpi/tables/dynamic/
312  */
313
314 static LIST_HEAD(acpi_table_attr_list);
315 static struct kobject *tables_kobj;
316 static struct kobject *dynamic_tables_kobj;
317 static struct kobject *hotplug_kobj;
318
319 struct acpi_table_attr {
320         struct bin_attribute attr;
321         char name[8];
322         int instance;
323         struct list_head node;
324 };
325
326 static ssize_t acpi_table_show(struct file *filp, struct kobject *kobj,
327                                struct bin_attribute *bin_attr, char *buf,
328                                loff_t offset, size_t count)
329 {
330         struct acpi_table_attr *table_attr =
331             container_of(bin_attr, struct acpi_table_attr, attr);
332         struct acpi_table_header *table_header = NULL;
333         acpi_status status;
334         char name[ACPI_NAME_SIZE];
335
336         if (strncmp(table_attr->name, "NULL", 4))
337                 memcpy(name, table_attr->name, ACPI_NAME_SIZE);
338         else
339                 memcpy(name, "\0\0\0\0", 4);
340
341         status = acpi_get_table(name, table_attr->instance, &table_header);
342         if (ACPI_FAILURE(status))
343                 return -ENODEV;
344
345         return memory_read_from_buffer(buf, count, &offset,
346                                        table_header, table_header->length);
347 }
348
349 static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
350                                  struct acpi_table_header *table_header)
351 {
352         struct acpi_table_header *header = NULL;
353         struct acpi_table_attr *attr = NULL;
354
355         sysfs_attr_init(&table_attr->attr.attr);
356         if (table_header->signature[0] != '\0')
357                 memcpy(table_attr->name, table_header->signature,
358                        ACPI_NAME_SIZE);
359         else
360                 memcpy(table_attr->name, "NULL", 4);
361
362         list_for_each_entry(attr, &acpi_table_attr_list, node) {
363                 if (!memcmp(table_attr->name, attr->name, ACPI_NAME_SIZE))
364                         if (table_attr->instance < attr->instance)
365                                 table_attr->instance = attr->instance;
366         }
367         table_attr->instance++;
368
369         if (table_attr->instance > 1 || (table_attr->instance == 1 &&
370                                          !acpi_get_table
371                                          (table_header->signature, 2, &header)))
372                 sprintf(table_attr->name + ACPI_NAME_SIZE, "%d",
373                         table_attr->instance);
374
375         table_attr->attr.size = table_header->length;
376         table_attr->attr.read = acpi_table_show;
377         table_attr->attr.attr.name = table_attr->name;
378         table_attr->attr.attr.mode = 0400;
379
380         return;
381 }
382
383 static acpi_status
384 acpi_sysfs_table_handler(u32 event, void *table, void *context)
385 {
386         struct acpi_table_attr *table_attr;
387
388         switch (event) {
389         case ACPI_TABLE_EVENT_LOAD:
390                 table_attr =
391                     kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
392                 if (!table_attr)
393                         return AE_NO_MEMORY;
394
395                 acpi_table_attr_init(table_attr, table);
396                 if (sysfs_create_bin_file(dynamic_tables_kobj,
397                                           &table_attr->attr)) {
398                         kfree(table_attr);
399                         return AE_ERROR;
400                 } else
401                         list_add_tail(&table_attr->node, &acpi_table_attr_list);
402                 break;
403         case ACPI_TABLE_EVENT_UNLOAD:
404                 /*
405                  * we do not need to do anything right now
406                  * because the table is not deleted from the
407                  * global table list when unloading it.
408                  */
409                 break;
410         default:
411                 return AE_BAD_PARAMETER;
412         }
413         return AE_OK;
414 }
415
416 static int acpi_tables_sysfs_init(void)
417 {
418         struct acpi_table_attr *table_attr;
419         struct acpi_table_header *table_header = NULL;
420         int table_index;
421         acpi_status status;
422         int ret;
423
424         tables_kobj = kobject_create_and_add("tables", acpi_kobj);
425         if (!tables_kobj)
426                 goto err;
427
428         dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
429         if (!dynamic_tables_kobj)
430                 goto err_dynamic_tables;
431
432         for (table_index = 0;; table_index++) {
433                 status = acpi_get_table_by_index(table_index, &table_header);
434
435                 if (status == AE_BAD_PARAMETER)
436                         break;
437
438                 if (ACPI_FAILURE(status))
439                         continue;
440
441                 table_attr = NULL;
442                 table_attr = kzalloc(sizeof(*table_attr), GFP_KERNEL);
443                 if (!table_attr)
444                         return -ENOMEM;
445
446                 acpi_table_attr_init(table_attr, table_header);
447                 ret = sysfs_create_bin_file(tables_kobj, &table_attr->attr);
448                 if (ret) {
449                         kfree(table_attr);
450                         return ret;
451                 }
452                 list_add_tail(&table_attr->node, &acpi_table_attr_list);
453         }
454
455         kobject_uevent(tables_kobj, KOBJ_ADD);
456         kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
457         status = acpi_install_table_handler(acpi_sysfs_table_handler, NULL);
458
459         return ACPI_FAILURE(status) ? -EINVAL : 0;
460 err_dynamic_tables:
461         kobject_put(tables_kobj);
462 err:
463         return -ENOMEM;
464 }
465
466 /*
467  * Detailed ACPI IRQ counters:
468  * /sys/firmware/acpi/interrupts/
469  */
470
471 u32 acpi_irq_handled;
472 u32 acpi_irq_not_handled;
473
474 #define COUNT_GPE 0
475 #define COUNT_SCI 1             /* acpi_irq_handled */
476 #define COUNT_SCI_NOT 2         /* acpi_irq_not_handled */
477 #define COUNT_ERROR 3           /* other */
478 #define NUM_COUNTERS_EXTRA 4
479
480 struct event_counter {
481         u32 count;
482         u32 flags;
483 };
484
485 static struct event_counter *all_counters;
486 static u32 num_gpes;
487 static u32 num_counters;
488 static struct attribute **all_attrs;
489 static u32 acpi_gpe_count;
490
491 static struct attribute_group interrupt_stats_attr_group = {
492         .name = "interrupts",
493 };
494
495 static struct kobj_attribute *counter_attrs;
496
497 static void delete_gpe_attr_array(void)
498 {
499         struct event_counter *tmp = all_counters;
500
501         all_counters = NULL;
502         kfree(tmp);
503
504         if (counter_attrs) {
505                 int i;
506
507                 for (i = 0; i < num_gpes; i++)
508                         kfree(counter_attrs[i].attr.name);
509
510                 kfree(counter_attrs);
511         }
512         kfree(all_attrs);
513
514         return;
515 }
516
517 static void gpe_count(u32 gpe_number)
518 {
519         acpi_gpe_count++;
520
521         if (!all_counters)
522                 return;
523
524         if (gpe_number < num_gpes)
525                 all_counters[gpe_number].count++;
526         else
527                 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
528                              COUNT_ERROR].count++;
529
530         return;
531 }
532
533 static void fixed_event_count(u32 event_number)
534 {
535         if (!all_counters)
536                 return;
537
538         if (event_number < ACPI_NUM_FIXED_EVENTS)
539                 all_counters[num_gpes + event_number].count++;
540         else
541                 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
542                              COUNT_ERROR].count++;
543
544         return;
545 }
546
547 static void acpi_global_event_handler(u32 event_type, acpi_handle device,
548         u32 event_number, void *context)
549 {
550         if (event_type == ACPI_EVENT_TYPE_GPE)
551                 gpe_count(event_number);
552
553         if (event_type == ACPI_EVENT_TYPE_FIXED)
554                 fixed_event_count(event_number);
555 }
556
557 static int get_status(u32 index, acpi_event_status *status,
558                       acpi_handle *handle)
559 {
560         int result = 0;
561
562         if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
563                 goto end;
564
565         if (index < num_gpes) {
566                 result = acpi_get_gpe_device(index, handle);
567                 if (result) {
568                         ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
569                                         "Invalid GPE 0x%x", index));
570                         goto end;
571                 }
572                 result = acpi_get_gpe_status(*handle, index, status);
573         } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
574                 result = acpi_get_event_status(index - num_gpes, status);
575
576 end:
577         return result;
578 }
579
580 static ssize_t counter_show(struct kobject *kobj,
581                             struct kobj_attribute *attr, char *buf)
582 {
583         int index = attr - counter_attrs;
584         int size;
585         acpi_handle handle;
586         acpi_event_status status;
587         int result = 0;
588
589         all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
590             acpi_irq_handled;
591         all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count =
592             acpi_irq_not_handled;
593         all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
594             acpi_gpe_count;
595         size = sprintf(buf, "%8u", all_counters[index].count);
596
597         /* "gpe_all" or "sci" */
598         if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
599                 goto end;
600
601         result = get_status(index, &status, &handle);
602         if (result)
603                 goto end;
604
605         if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER))
606                 size += sprintf(buf + size, "   invalid");
607         else if (status & ACPI_EVENT_FLAG_ENABLED)
608                 size += sprintf(buf + size, "   enabled");
609         else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
610                 size += sprintf(buf + size, "   wake_enabled");
611         else
612                 size += sprintf(buf + size, "   disabled");
613
614 end:
615         size += sprintf(buf + size, "\n");
616         return result ? result : size;
617 }
618
619 /*
620  * counter_set() sets the specified counter.
621  * setting the total "sci" file to any value clears all counters.
622  * enable/disable/clear a gpe/fixed event in user space.
623  */
624 static ssize_t counter_set(struct kobject *kobj,
625                            struct kobj_attribute *attr, const char *buf,
626                            size_t size)
627 {
628         int index = attr - counter_attrs;
629         acpi_event_status status;
630         acpi_handle handle;
631         int result = 0;
632         unsigned long tmp;
633
634         if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
635                 int i;
636                 for (i = 0; i < num_counters; ++i)
637                         all_counters[i].count = 0;
638                 acpi_gpe_count = 0;
639                 acpi_irq_handled = 0;
640                 acpi_irq_not_handled = 0;
641                 goto end;
642         }
643
644         /* show the event status for both GPEs and Fixed Events */
645         result = get_status(index, &status, &handle);
646         if (result)
647                 goto end;
648
649         if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER)) {
650                 printk(KERN_WARNING PREFIX
651                        "Can not change Invalid GPE/Fixed Event status\n");
652                 return -EINVAL;
653         }
654
655         if (index < num_gpes) {
656                 if (!strcmp(buf, "disable\n") &&
657                     (status & ACPI_EVENT_FLAG_ENABLED))
658                         result = acpi_disable_gpe(handle, index);
659                 else if (!strcmp(buf, "enable\n") &&
660                          !(status & ACPI_EVENT_FLAG_ENABLED))
661                         result = acpi_enable_gpe(handle, index);
662                 else if (!strcmp(buf, "clear\n") &&
663                          (status & ACPI_EVENT_FLAG_SET))
664                         result = acpi_clear_gpe(handle, index);
665                 else if (!kstrtoul(buf, 0, &tmp))
666                         all_counters[index].count = tmp;
667                 else
668                         result = -EINVAL;
669         } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
670                 int event = index - num_gpes;
671                 if (!strcmp(buf, "disable\n") &&
672                     (status & ACPI_EVENT_FLAG_ENABLED))
673                         result = acpi_disable_event(event, ACPI_NOT_ISR);
674                 else if (!strcmp(buf, "enable\n") &&
675                          !(status & ACPI_EVENT_FLAG_ENABLED))
676                         result = acpi_enable_event(event, ACPI_NOT_ISR);
677                 else if (!strcmp(buf, "clear\n") &&
678                          (status & ACPI_EVENT_FLAG_SET))
679                         result = acpi_clear_event(event);
680                 else if (!kstrtoul(buf, 0, &tmp))
681                         all_counters[index].count = tmp;
682                 else
683                         result = -EINVAL;
684         } else
685                 all_counters[index].count = strtoul(buf, NULL, 0);
686
687         if (ACPI_FAILURE(result))
688                 result = -EINVAL;
689 end:
690         return result ? result : size;
691 }
692
693 void acpi_irq_stats_init(void)
694 {
695         acpi_status status;
696         int i;
697
698         if (all_counters)
699                 return;
700
701         num_gpes = acpi_current_gpe_count;
702         num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
703
704         all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
705                             GFP_KERNEL);
706         if (all_attrs == NULL)
707                 return;
708
709         all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
710                                GFP_KERNEL);
711         if (all_counters == NULL)
712                 goto fail;
713
714         status = acpi_install_global_event_handler(acpi_global_event_handler, NULL);
715         if (ACPI_FAILURE(status))
716                 goto fail;
717
718         counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
719                                 GFP_KERNEL);
720         if (counter_attrs == NULL)
721                 goto fail;
722
723         for (i = 0; i < num_counters; ++i) {
724                 char buffer[12];
725                 char *name;
726
727                 if (i < num_gpes)
728                         sprintf(buffer, "gpe%02X", i);
729                 else if (i == num_gpes + ACPI_EVENT_PMTIMER)
730                         sprintf(buffer, "ff_pmtimer");
731                 else if (i == num_gpes + ACPI_EVENT_GLOBAL)
732                         sprintf(buffer, "ff_gbl_lock");
733                 else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
734                         sprintf(buffer, "ff_pwr_btn");
735                 else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
736                         sprintf(buffer, "ff_slp_btn");
737                 else if (i == num_gpes + ACPI_EVENT_RTC)
738                         sprintf(buffer, "ff_rt_clk");
739                 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
740                         sprintf(buffer, "gpe_all");
741                 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
742                         sprintf(buffer, "sci");
743                 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
744                         sprintf(buffer, "sci_not");
745                 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
746                         sprintf(buffer, "error");
747                 else
748                         sprintf(buffer, "bug%02X", i);
749
750                 name = kstrdup(buffer, GFP_KERNEL);
751                 if (name == NULL)
752                         goto fail;
753
754                 sysfs_attr_init(&counter_attrs[i].attr);
755                 counter_attrs[i].attr.name = name;
756                 counter_attrs[i].attr.mode = 0644;
757                 counter_attrs[i].show = counter_show;
758                 counter_attrs[i].store = counter_set;
759
760                 all_attrs[i] = &counter_attrs[i].attr;
761         }
762
763         interrupt_stats_attr_group.attrs = all_attrs;
764         if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
765                 return;
766
767 fail:
768         delete_gpe_attr_array();
769         return;
770 }
771
772 static void __exit interrupt_stats_exit(void)
773 {
774         sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
775
776         delete_gpe_attr_array();
777
778         return;
779 }
780
781 static ssize_t
782 acpi_show_profile(struct device *dev, struct device_attribute *attr,
783                   char *buf)
784 {
785         return sprintf(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
786 }
787
788 static const struct device_attribute pm_profile_attr =
789         __ATTR(pm_profile, S_IRUGO, acpi_show_profile, NULL);
790
791 static ssize_t hotplug_enabled_show(struct kobject *kobj,
792                                     struct kobj_attribute *attr, char *buf)
793 {
794         struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
795
796         return sprintf(buf, "%d\n", hotplug->enabled);
797 }
798
799 static ssize_t hotplug_enabled_store(struct kobject *kobj,
800                                      struct kobj_attribute *attr,
801                                      const char *buf, size_t size)
802 {
803         struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
804         unsigned int val;
805
806         if (kstrtouint(buf, 10, &val) || val > 1)
807                 return -EINVAL;
808
809         acpi_scan_hotplug_enabled(hotplug, val);
810         return size;
811 }
812
813 static struct kobj_attribute hotplug_enabled_attr =
814         __ATTR(enabled, S_IRUGO | S_IWUSR, hotplug_enabled_show,
815                 hotplug_enabled_store);
816
817 static struct attribute *hotplug_profile_attrs[] = {
818         &hotplug_enabled_attr.attr,
819         NULL
820 };
821
822 static struct kobj_type acpi_hotplug_profile_ktype = {
823         .sysfs_ops = &kobj_sysfs_ops,
824         .default_attrs = hotplug_profile_attrs,
825 };
826
827 void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
828                                     const char *name)
829 {
830         int error;
831
832         if (!hotplug_kobj)
833                 goto err_out;
834
835         error = kobject_init_and_add(&hotplug->kobj,
836                 &acpi_hotplug_profile_ktype, hotplug_kobj, "%s", name);
837         if (error)
838                 goto err_out;
839
840         kobject_uevent(&hotplug->kobj, KOBJ_ADD);
841         return;
842
843  err_out:
844         pr_err(PREFIX "Unable to add hotplug profile '%s'\n", name);
845 }
846
847 static ssize_t force_remove_show(struct kobject *kobj,
848                                  struct kobj_attribute *attr, char *buf)
849 {
850         return sprintf(buf, "%d\n", !!acpi_force_hot_remove);
851 }
852
853 static ssize_t force_remove_store(struct kobject *kobj,
854                                   struct kobj_attribute *attr,
855                                   const char *buf, size_t size)
856 {
857         bool val;
858         int ret;
859
860         ret = strtobool(buf, &val);
861         if (ret < 0)
862                 return ret;
863
864         lock_device_hotplug();
865         acpi_force_hot_remove = val;
866         unlock_device_hotplug();
867         return size;
868 }
869
870 static const struct kobj_attribute force_remove_attr =
871         __ATTR(force_remove, S_IRUGO | S_IWUSR, force_remove_show,
872                force_remove_store);
873
874 int __init acpi_sysfs_init(void)
875 {
876         int result;
877
878         result = acpi_tables_sysfs_init();
879         if (result)
880                 return result;
881
882         hotplug_kobj = kobject_create_and_add("hotplug", acpi_kobj);
883         result = sysfs_create_file(hotplug_kobj, &force_remove_attr.attr);
884         if (result)
885                 return result;
886
887         result = sysfs_create_file(acpi_kobj, &pm_profile_attr.attr);
888         return result;
889 }