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