Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/proc_fs.h> |
| 2 | #include <linux/seq_file.h> |
Paul Gortmaker | 214f2c9 | 2011-10-26 16:22:14 -0400 | [diff] [blame] | 3 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/suspend.h> |
| 5 | #include <linux/bcd.h> |
| 6 | #include <asm/uaccess.h> |
| 7 | |
| 8 | #include <acpi/acpi_bus.h> |
| 9 | #include <acpi/acpi_drivers.h> |
| 10 | |
| 11 | #ifdef CONFIG_X86 |
| 12 | #include <linux/mc146818rtc.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "sleep.h" |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #define _COMPONENT ACPI_SYSTEM_COMPONENT |
Len Brown | 43532c8 | 2007-07-24 02:16:50 -0400 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * this file provides support for: |
Len Brown | 43532c8 | 2007-07-24 02:16:50 -0400 | [diff] [blame] | 21 | * /proc/acpi/alarm |
| 22 | * /proc/acpi/wakeup |
| 23 | */ |
| 24 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 25 | ACPI_MODULE_NAME("sleep") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Len Brown | 673d5b4 | 2007-07-28 03:33:16 -0400 | [diff] [blame] | 27 | #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86) |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 28 | /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */ |
| 29 | #else |
| 30 | #define HAVE_ACPI_LEGACY_ALARM |
| 31 | #endif |
| 32 | |
| 33 | #ifdef HAVE_ACPI_LEGACY_ALARM |
| 34 | |
Len Brown | 2602a67 | 2009-01-09 02:10:16 -0500 | [diff] [blame] | 35 | static u32 cmos_bcd_read(int offset, int rtc_control); |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset) |
| 38 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 39 | u32 sec, min, hr; |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 40 | u32 day, mo, yr, cent = 0; |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 41 | u32 today = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 42 | unsigned char rtc_control = 0; |
| 43 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | spin_lock_irqsave(&rtc_lock, flags); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | rtc_control = CMOS_READ(RTC_CONTROL); |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 48 | sec = cmos_bcd_read(RTC_SECONDS_ALARM, rtc_control); |
| 49 | min = cmos_bcd_read(RTC_MINUTES_ALARM, rtc_control); |
| 50 | hr = cmos_bcd_read(RTC_HOURS_ALARM, rtc_control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* If we ever get an FACP with proper values... */ |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 53 | if (acpi_gbl_FADT.day_alarm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* ACPI spec: only low 6 its should be cared */ |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 55 | day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F; |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 56 | if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
| 57 | day = bcd2bin(day); |
| 58 | } else |
| 59 | day = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control); |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 60 | if (acpi_gbl_FADT.month_alarm) |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 61 | mo = cmos_bcd_read(acpi_gbl_FADT.month_alarm, rtc_control); |
| 62 | else { |
| 63 | mo = cmos_bcd_read(RTC_MONTH, rtc_control); |
| 64 | today = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control); |
| 65 | } |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 66 | if (acpi_gbl_FADT.century) |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 67 | cent = cmos_bcd_read(acpi_gbl_FADT.century, rtc_control); |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 68 | |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 69 | yr = cmos_bcd_read(RTC_YEAR, rtc_control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 72 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 73 | /* we're trusting the FADT (see above) */ |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 74 | if (!acpi_gbl_FADT.century) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 75 | /* If we're not trusting the FADT, we should at least make it |
| 76 | * right for _this_ century... ehm, what is _this_ century? |
| 77 | * |
| 78 | * TBD: |
| 79 | * ASAP: find piece of code in the kernel, e.g. star tracker driver, |
| 80 | * which we can trust to determine the century correctly. Atom |
| 81 | * watch driver would be nice, too... |
| 82 | * |
| 83 | * if that has not happened, change for first release in 2050: |
| 84 | * if (yr<50) |
| 85 | * yr += 2100; |
| 86 | * else |
| 87 | * yr += 2000; // current line of code |
| 88 | * |
| 89 | * if that has not happened either, please do on 2099/12/31:23:59:59 |
| 90 | * s/2000/2100 |
| 91 | * |
| 92 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | yr += 2000; |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 94 | else |
| 95 | yr += cent * 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Mark Lord | 48452e5 | 2008-12-09 10:46:30 -0500 | [diff] [blame] | 97 | /* |
| 98 | * Show correct dates for alarms up to a month into the future. |
| 99 | * This solves issues for nearly all situations with the common |
| 100 | * 30-day alarm clocks in PC hardware. |
| 101 | */ |
| 102 | if (day < today) { |
| 103 | if (mo < 12) { |
| 104 | mo += 1; |
| 105 | } else { |
| 106 | mo = 1; |
| 107 | yr += 1; |
| 108 | } |
| 109 | } |
| 110 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 111 | seq_printf(seq, "%4.4u-", yr); |
| 112 | (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo); |
| 113 | (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day); |
| 114 | (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr); |
| 115 | (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file) |
| 122 | { |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 123 | return single_open(file, acpi_system_alarm_seq_show, PDE_DATA(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 126 | static int get_date_field(char **p, u32 * value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 128 | char *next = NULL; |
| 129 | char *string_end = NULL; |
| 130 | int result = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Try to find delimeter, only to insert null. The end of the |
| 134 | * string won't have one, but is still valid. |
| 135 | */ |
Signed-off by Yi Yang | 975c302 | 2007-12-27 21:50:42 -0500 | [diff] [blame] | 136 | if (*p == NULL) |
| 137 | return result; |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | next = strpbrk(*p, "- :"); |
| 140 | if (next) |
| 141 | *next++ = '\0'; |
| 142 | |
| 143 | *value = simple_strtoul(*p, &string_end, 10); |
| 144 | |
| 145 | /* Signal success if we got a good digit */ |
| 146 | if (string_end != *p) |
| 147 | result = 0; |
| 148 | |
| 149 | if (next) |
| 150 | *p = next; |
Signed-off by Yi Yang | 975c302 | 2007-12-27 21:50:42 -0500 | [diff] [blame] | 151 | else |
| 152 | *p = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | return result; |
| 155 | } |
| 156 | |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 157 | /* Read a possibly BCD register, always return binary */ |
| 158 | static u32 cmos_bcd_read(int offset, int rtc_control) |
| 159 | { |
| 160 | u32 val = CMOS_READ(offset); |
| 161 | if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
Adrian Bunk | ab527d7 | 2008-10-18 20:28:39 -0700 | [diff] [blame] | 162 | val = bcd2bin(val); |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 163 | return val; |
| 164 | } |
| 165 | |
| 166 | /* Write binary value into possibly BCD register */ |
| 167 | static void cmos_bcd_write(u32 val, int offset, int rtc_control) |
| 168 | { |
| 169 | if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
Adrian Bunk | ab527d7 | 2008-10-18 20:28:39 -0700 | [diff] [blame] | 170 | val = bin2bcd(val); |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 171 | CMOS_WRITE(val, offset); |
| 172 | } |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 175 | acpi_system_write_alarm(struct file *file, |
| 176 | const char __user * buffer, size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 178 | int result = 0; |
| 179 | char alarm_string[30] = { '\0' }; |
| 180 | char *p = alarm_string; |
| 181 | u32 sec, min, hr, day, mo, yr; |
| 182 | int adjust = 0; |
| 183 | unsigned char rtc_control = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (count > sizeof(alarm_string) - 1) |
Lin Ming | 95d9a7a | 2008-12-16 16:49:26 +0800 | [diff] [blame] | 186 | return -EINVAL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (copy_from_user(alarm_string, buffer, count)) |
Lin Ming | 95d9a7a | 2008-12-16 16:49:26 +0800 | [diff] [blame] | 189 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | alarm_string[count] = '\0'; |
| 192 | |
| 193 | /* check for time adjustment */ |
| 194 | if (alarm_string[0] == '+') { |
| 195 | p++; |
| 196 | adjust = 1; |
| 197 | } |
| 198 | |
| 199 | if ((result = get_date_field(&p, &yr))) |
| 200 | goto end; |
| 201 | if ((result = get_date_field(&p, &mo))) |
| 202 | goto end; |
| 203 | if ((result = get_date_field(&p, &day))) |
| 204 | goto end; |
| 205 | if ((result = get_date_field(&p, &hr))) |
| 206 | goto end; |
| 207 | if ((result = get_date_field(&p, &min))) |
| 208 | goto end; |
| 209 | if ((result = get_date_field(&p, &sec))) |
| 210 | goto end; |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | spin_lock_irq(&rtc_lock); |
| 213 | |
| 214 | rtc_control = CMOS_READ(RTC_CONTROL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | if (adjust) { |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 217 | yr += cmos_bcd_read(RTC_YEAR, rtc_control); |
| 218 | mo += cmos_bcd_read(RTC_MONTH, rtc_control); |
| 219 | day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control); |
| 220 | hr += cmos_bcd_read(RTC_HOURS, rtc_control); |
| 221 | min += cmos_bcd_read(RTC_MINUTES, rtc_control); |
| 222 | sec += cmos_bcd_read(RTC_SECONDS, rtc_control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | spin_unlock_irq(&rtc_lock); |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (sec > 59) { |
Yi Yang | 0879802 | 2007-12-27 22:04:26 -0500 | [diff] [blame] | 228 | min += sec/60; |
| 229 | sec = sec%60; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | if (min > 59) { |
Yi Yang | 0879802 | 2007-12-27 22:04:26 -0500 | [diff] [blame] | 232 | hr += min/60; |
| 233 | min = min%60; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | if (hr > 23) { |
Yi Yang | 0879802 | 2007-12-27 22:04:26 -0500 | [diff] [blame] | 236 | day += hr/24; |
| 237 | hr = hr%24; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | if (day > 31) { |
Yi Yang | 0879802 | 2007-12-27 22:04:26 -0500 | [diff] [blame] | 240 | mo += day/32; |
| 241 | day = day%32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | if (mo > 12) { |
Yi Yang | 0879802 | 2007-12-27 22:04:26 -0500 | [diff] [blame] | 244 | yr += mo/13; |
| 245 | mo = mo%13; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | spin_lock_irq(&rtc_lock); |
| 249 | /* |
| 250 | * Disable alarm interrupt before setting alarm timer or else |
| 251 | * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs |
| 252 | */ |
| 253 | rtc_control &= ~RTC_AIE; |
| 254 | CMOS_WRITE(rtc_control, RTC_CONTROL); |
| 255 | CMOS_READ(RTC_INTR_FLAGS); |
| 256 | |
| 257 | /* write the fields the rtc knows about */ |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 258 | cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control); |
| 259 | cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control); |
| 260 | cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | /* |
| 263 | * If the system supports an enhanced alarm it will have non-zero |
| 264 | * offsets into the CMOS RAM here -- which for some reason are pointing |
| 265 | * to the RTC area of memory. |
| 266 | */ |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 267 | if (acpi_gbl_FADT.day_alarm) |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 268 | cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control); |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 269 | if (acpi_gbl_FADT.month_alarm) |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 270 | cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control); |
Huacai Chen | cce3ce8 | 2008-07-04 09:59:31 -0700 | [diff] [blame] | 271 | if (acpi_gbl_FADT.century) { |
| 272 | if (adjust) |
| 273 | yr += cmos_bcd_read(acpi_gbl_FADT.century, rtc_control) * 100; |
Linus Torvalds | c67c36e | 2007-10-17 23:18:32 -0400 | [diff] [blame] | 274 | cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control); |
Huacai Chen | cce3ce8 | 2008-07-04 09:59:31 -0700 | [diff] [blame] | 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* enable the rtc alarm interrupt */ |
| 277 | rtc_control |= RTC_AIE; |
| 278 | CMOS_WRITE(rtc_control, RTC_CONTROL); |
| 279 | CMOS_READ(RTC_INTR_FLAGS); |
| 280 | |
| 281 | spin_unlock_irq(&rtc_lock); |
| 282 | |
| 283 | acpi_clear_event(ACPI_EVENT_RTC); |
| 284 | acpi_enable_event(ACPI_EVENT_RTC, 0); |
| 285 | |
| 286 | *ppos += count; |
| 287 | |
| 288 | result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 289 | end: |
Lin Ming | 95d9a7a | 2008-12-16 16:49:26 +0800 | [diff] [blame] | 290 | return result ? result : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
Len Brown | fd35094 | 2007-05-09 23:34:35 -0400 | [diff] [blame] | 292 | #endif /* HAVE_ACPI_LEGACY_ALARM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | static int |
| 295 | acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset) |
| 296 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 297 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
David Brownell | 8aa5559 | 2007-04-25 15:20:10 -0400 | [diff] [blame] | 299 | seq_printf(seq, "Device\tS-state\t Status Sysfs node\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 301 | mutex_lock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 303 | struct acpi_device *dev = |
| 304 | container_of(node, struct acpi_device, wakeup_list); |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 305 | struct acpi_device_physical_node *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | if (!dev->wakeup.flags.valid) |
| 308 | continue; |
David Brownell | 8aa5559 | 2007-04-25 15:20:10 -0400 | [diff] [blame] | 309 | |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 310 | seq_printf(seq, "%s\t S%d\t", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 311 | dev->pnp.bus_id, |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 312 | (u32) dev->wakeup.sleep_state); |
David Brownell | 8aa5559 | 2007-04-25 15:20:10 -0400 | [diff] [blame] | 313 | |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 314 | mutex_lock(&dev->physical_node_lock); |
| 315 | |
Andreas Fleig | 65ab96f | 2013-01-27 14:17:55 +0000 | [diff] [blame] | 316 | if (!dev->physical_node_count) { |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 317 | seq_printf(seq, "%c%-8s\n", |
Andreas Fleig | 65ab96f | 2013-01-27 14:17:55 +0000 | [diff] [blame] | 318 | dev->wakeup.flags.run_wake ? '*' : ' ', |
| 319 | device_may_wakeup(&dev->dev) ? |
| 320 | "enabled" : "disabled"); |
| 321 | } else { |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 322 | struct device *ldev; |
| 323 | list_for_each_entry(entry, &dev->physical_node_list, |
| 324 | node) { |
| 325 | ldev = get_device(entry->dev); |
| 326 | if (!ldev) |
| 327 | continue; |
| 328 | |
| 329 | if (&entry->node != |
| 330 | dev->physical_node_list.next) |
| 331 | seq_printf(seq, "\t\t"); |
| 332 | |
| 333 | seq_printf(seq, "%c%-8s %s:%s\n", |
| 334 | dev->wakeup.flags.run_wake ? '*' : ' ', |
| 335 | (device_may_wakeup(&dev->dev) || |
| 336 | (ldev && device_may_wakeup(ldev))) ? |
| 337 | "enabled" : "disabled", |
| 338 | ldev->bus ? ldev->bus->name : |
| 339 | "no-bus", dev_name(ldev)); |
| 340 | put_device(ldev); |
| 341 | } |
| 342 | } |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 343 | |
| 344 | mutex_unlock(&dev->physical_node_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 346 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 350 | static void physical_device_enable_wakeup(struct acpi_device *adev) |
| 351 | { |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 352 | struct acpi_device_physical_node *entry; |
Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 353 | |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 354 | mutex_lock(&adev->physical_node_lock); |
| 355 | |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 356 | list_for_each_entry(entry, |
| 357 | &adev->physical_node_list, node) |
| 358 | if (entry->dev && device_can_wakeup(entry->dev)) { |
| 359 | bool enable = !device_may_wakeup(entry->dev); |
| 360 | device_set_wakeup_enable(entry->dev, enable); |
| 361 | } |
Rafael J. Wysocki | 623cf33 | 2013-08-06 02:26:22 +0200 | [diff] [blame] | 362 | |
| 363 | mutex_unlock(&adev->physical_node_lock); |
Rafael J. Wysocki | 76acae0 | 2008-10-03 15:23:49 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 367 | acpi_system_write_wakeup_device(struct file *file, |
| 368 | const char __user * buffer, |
| 369 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 371 | struct list_head *node, *next; |
| 372 | char strbuf[5]; |
| 373 | char str[5] = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Cyril Roelandt | 05bce79 | 2012-11-22 23:20:31 +0100 | [diff] [blame] | 375 | if (count > 4) |
| 376 | count = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Cyril Roelandt | 05bce79 | 2012-11-22 23:20:31 +0100 | [diff] [blame] | 378 | if (copy_from_user(strbuf, buffer, count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | return -EFAULT; |
Cyril Roelandt | 05bce79 | 2012-11-22 23:20:31 +0100 | [diff] [blame] | 380 | strbuf[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | sscanf(strbuf, "%s", str); |
| 382 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 383 | mutex_lock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 385 | struct acpi_device *dev = |
| 386 | container_of(node, struct acpi_device, wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (!dev->wakeup.flags.valid) |
| 388 | continue; |
| 389 | |
| 390 | if (!strncmp(dev->pnp.bus_id, str, 4)) { |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 391 | if (device_can_wakeup(&dev->dev)) { |
| 392 | bool enable = !device_may_wakeup(&dev->dev); |
| 393 | device_set_wakeup_enable(&dev->dev, enable); |
| 394 | } else { |
| 395 | physical_device_enable_wakeup(dev); |
| 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | } |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 400 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | return count; |
| 402 | } |
| 403 | |
| 404 | static int |
| 405 | acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file) |
| 406 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 407 | return single_open(file, acpi_system_wakeup_device_seq_show, |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 408 | PDE_DATA(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 411 | static const struct file_operations acpi_system_wakeup_device_fops = { |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 412 | .owner = THIS_MODULE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 413 | .open = acpi_system_wakeup_device_open_fs, |
| 414 | .read = seq_read, |
| 415 | .write = acpi_system_write_wakeup_device, |
| 416 | .llseek = seq_lseek, |
| 417 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | }; |
| 419 | |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 420 | #ifdef HAVE_ACPI_LEGACY_ALARM |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 421 | static const struct file_operations acpi_system_alarm_fops = { |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 422 | .owner = THIS_MODULE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 423 | .open = acpi_system_alarm_open_fs, |
| 424 | .read = seq_read, |
| 425 | .write = acpi_system_write_alarm, |
| 426 | .llseek = seq_lseek, |
| 427 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | }; |
| 429 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 430 | static u32 rtc_handler(void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | acpi_clear_event(ACPI_EVENT_RTC); |
| 433 | acpi_disable_event(ACPI_EVENT_RTC, 0); |
| 434 | |
| 435 | return ACPI_INTERRUPT_HANDLED; |
| 436 | } |
Len Brown | fd35094 | 2007-05-09 23:34:35 -0400 | [diff] [blame] | 437 | #endif /* HAVE_ACPI_LEGACY_ALARM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Bjorn Helgaas | 9cee43e | 2009-03-24 16:50:14 -0600 | [diff] [blame] | 439 | int __init acpi_sleep_proc_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 441 | #ifdef HAVE_ACPI_LEGACY_ALARM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | /* 'alarm' [R/W] */ |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 443 | proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR, |
| 444 | acpi_root_dir, &acpi_system_alarm_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 446 | acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL); |
Zhao Yakui | e1094bf | 2008-05-14 11:32:59 +0800 | [diff] [blame] | 447 | /* |
| 448 | * Disable the RTC event after installing RTC handler. |
| 449 | * Only when RTC alarm is set will it be enabled. |
| 450 | */ |
| 451 | acpi_clear_event(ACPI_EVENT_RTC); |
| 452 | acpi_disable_event(ACPI_EVENT_RTC, 0); |
Len Brown | fd35094 | 2007-05-09 23:34:35 -0400 | [diff] [blame] | 453 | #endif /* HAVE_ACPI_LEGACY_ALARM */ |
David Brownell | f5f72b4 | 2007-05-08 00:34:02 -0700 | [diff] [blame] | 454 | |
Pavel Machek | c65ade4 | 2005-08-05 00:37:45 -0400 | [diff] [blame] | 455 | /* 'wakeup device' [R/W] */ |
Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 456 | proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR, |
| 457 | acpi_root_dir, &acpi_system_wakeup_device_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return 0; |
| 460 | } |