Power: Changes the permission to read only for sysfs file /sys/kernel/wakeup_reasons...
[firefly-linux-kernel-4.4.55.git] / kernel / power / wakeup_reason.c
1 /*
2  * kernel/power/wakeup_reason.c
3  *
4  * Logs the reasons which caused the kernel to resume from
5  * the suspend mode.
6  *
7  * Copyright (C) 2014 Google, Inc.
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/wakeup_reason.h>
19 #include <linux/kernel.h>
20 #include <linux/irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/kobject.h>
24 #include <linux/sysfs.h>
25 #include <linux/init.h>
26 #include <linux/spinlock.h>
27 #include <linux/notifier.h>
28 #include <linux/suspend.h>
29
30
31 #define MAX_WAKEUP_REASON_IRQS 32
32 static int irq_list[MAX_WAKEUP_REASON_IRQS];
33 static int irqcount;
34 static struct kobject *wakeup_reason;
35 static spinlock_t resume_reason_lock;
36
37 static ssize_t last_resume_reason_show(struct kobject *kobj, struct kobj_attribute *attr,
38                 char *buf)
39 {
40         int irq_no, buf_offset = 0;
41         struct irq_desc *desc;
42         spin_lock(&resume_reason_lock);
43         for (irq_no = 0; irq_no < irqcount; irq_no++) {
44                 desc = irq_to_desc(irq_list[irq_no]);
45                 if (desc && desc->action && desc->action->name)
46                         buf_offset += sprintf(buf + buf_offset, "%d %s\n",
47                                         irq_list[irq_no], desc->action->name);
48                 else
49                         buf_offset += sprintf(buf + buf_offset, "%d\n",
50                                         irq_list[irq_no]);
51         }
52         spin_unlock(&resume_reason_lock);
53         return buf_offset;
54 }
55
56 static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason);
57
58 static struct attribute *attrs[] = {
59         &resume_reason.attr,
60         NULL,
61 };
62 static struct attribute_group attr_group = {
63         .attrs = attrs,
64 };
65
66 /*
67  * logs all the wake up reasons to the kernel
68  * stores the irqs to expose them to the userspace via sysfs
69  */
70 void log_wakeup_reason(int irq)
71 {
72         struct irq_desc *desc;
73         desc = irq_to_desc(irq);
74         if (desc && desc->action && desc->action->name)
75                 printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq,
76                                 desc->action->name);
77         else
78                 printk(KERN_INFO "Resume caused by IRQ %d\n", irq);
79
80         spin_lock(&resume_reason_lock);
81         if (irqcount == MAX_WAKEUP_REASON_IRQS) {
82                 spin_unlock(&resume_reason_lock);
83                 printk(KERN_WARNING "Resume caused by more than %d IRQs\n",
84                                 MAX_WAKEUP_REASON_IRQS);
85                 return;
86         }
87
88         irq_list[irqcount++] = irq;
89         spin_unlock(&resume_reason_lock);
90 }
91
92 /* Detects a suspend and clears all the previous wake up reasons*/
93 static int wakeup_reason_pm_event(struct notifier_block *notifier,
94                 unsigned long pm_event, void *unused)
95 {
96         switch (pm_event) {
97         case PM_SUSPEND_PREPARE:
98                 spin_lock(&resume_reason_lock);
99                 irqcount = 0;
100                 spin_unlock(&resume_reason_lock);
101                 break;
102         default:
103                 break;
104         }
105         return NOTIFY_DONE;
106 }
107
108 static struct notifier_block wakeup_reason_pm_notifier_block = {
109         .notifier_call = wakeup_reason_pm_event,
110 };
111
112 /* Initializes the sysfs parameter
113  * registers the pm_event notifier
114  */
115 int __init wakeup_reason_init(void)
116 {
117         int retval;
118         spin_lock_init(&resume_reason_lock);
119         retval = register_pm_notifier(&wakeup_reason_pm_notifier_block);
120         if (retval)
121                 printk(KERN_WARNING "[%s] failed to register PM notifier %d\n",
122                                 __func__, retval);
123
124         wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj);
125         if (!wakeup_reason) {
126                 printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n",
127                                 __func__);
128                 return 1;
129         }
130         retval = sysfs_create_group(wakeup_reason, &attr_group);
131         if (retval) {
132                 kobject_put(wakeup_reason);
133                 printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n",
134                                 __func__, retval);
135         }
136         return 0;
137 }
138
139 late_initcall(wakeup_reason_init);