Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 | 16 | #define _COMPONENT ACPI_SYSTEM_COMPONENT |
43532c8a LB |
17 | |
18 | /* | |
19 | * this file provides support for: | |
43532c8a LB |
20 | * /proc/acpi/alarm |
21 | * /proc/acpi/wakeup | |
22 | */ | |
23 | ||
4be44fcd | 24 | ACPI_MODULE_NAME("sleep") |
1da177e4 | 25 | |
673d5b43 | 26 | #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86) |
f5f72b46 DB |
27 | /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */ |
28 | #else | |
29 | #define HAVE_ACPI_LEGACY_ALARM | |
30 | #endif | |
31 | ||
32 | #ifdef HAVE_ACPI_LEGACY_ALARM | |
33 | ||
2602a671 LB |
34 | static u32 cmos_bcd_read(int offset, int rtc_control); |
35 | ||
1da177e4 LT |
36 | static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset) |
37 | { | |
4be44fcd | 38 | u32 sec, min, hr; |
ad71860a | 39 | u32 day, mo, yr, cent = 0; |
48452e5f | 40 | u32 today = 0; |
4be44fcd LB |
41 | unsigned char rtc_control = 0; |
42 | unsigned long flags; | |
1da177e4 | 43 | |
1da177e4 LT |
44 | spin_lock_irqsave(&rtc_lock, flags); |
45 | ||
1da177e4 | 46 | rtc_control = CMOS_READ(RTC_CONTROL); |
48452e5f ML |
47 | sec = cmos_bcd_read(RTC_SECONDS_ALARM, rtc_control); |
48 | min = cmos_bcd_read(RTC_MINUTES_ALARM, rtc_control); | |
49 | hr = cmos_bcd_read(RTC_HOURS_ALARM, rtc_control); | |
1da177e4 LT |
50 | |
51 | /* If we ever get an FACP with proper values... */ | |
48452e5f | 52 | if (acpi_gbl_FADT.day_alarm) { |
1da177e4 | 53 | /* ACPI spec: only low 6 its should be cared */ |
ad71860a | 54 | day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F; |
48452e5f ML |
55 | if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
56 | day = bcd2bin(day); | |
57 | } else | |
58 | day = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control); | |
ad71860a | 59 | if (acpi_gbl_FADT.month_alarm) |
48452e5f ML |
60 | mo = cmos_bcd_read(acpi_gbl_FADT.month_alarm, rtc_control); |
61 | else { | |
62 | mo = cmos_bcd_read(RTC_MONTH, rtc_control); | |
63 | today = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control); | |
64 | } | |
ad71860a | 65 | if (acpi_gbl_FADT.century) |
48452e5f | 66 | cent = cmos_bcd_read(acpi_gbl_FADT.century, rtc_control); |
ad71860a | 67 | |
48452e5f | 68 | yr = cmos_bcd_read(RTC_YEAR, rtc_control); |
1da177e4 LT |
69 | |
70 | spin_unlock_irqrestore(&rtc_lock, flags); | |
71 | ||
4be44fcd | 72 | /* we're trusting the FADT (see above) */ |
ad71860a | 73 | if (!acpi_gbl_FADT.century) |
4be44fcd LB |
74 | /* If we're not trusting the FADT, we should at least make it |
75 | * right for _this_ century... ehm, what is _this_ century? | |
76 | * | |
77 | * TBD: | |
78 | * ASAP: find piece of code in the kernel, e.g. star tracker driver, | |
79 | * which we can trust to determine the century correctly. Atom | |
80 | * watch driver would be nice, too... | |
81 | * | |
82 | * if that has not happened, change for first release in 2050: | |
83 | * if (yr<50) | |
84 | * yr += 2100; | |
85 | * else | |
86 | * yr += 2000; // current line of code | |
87 | * | |
88 | * if that has not happened either, please do on 2099/12/31:23:59:59 | |
89 | * s/2000/2100 | |
90 | * | |
91 | */ | |
1da177e4 | 92 | yr += 2000; |
ad71860a AS |
93 | else |
94 | yr += cent * 100; | |
1da177e4 | 95 | |
48452e5f ML |
96 | /* |
97 | * Show correct dates for alarms up to a month into the future. | |
98 | * This solves issues for nearly all situations with the common | |
99 | * 30-day alarm clocks in PC hardware. | |
100 | */ | |
101 | if (day < today) { | |
102 | if (mo < 12) { | |
103 | mo += 1; | |
104 | } else { | |
105 | mo = 1; | |
106 | yr += 1; | |
107 | } | |
108 | } | |
109 | ||
4be44fcd LB |
110 | seq_printf(seq, "%4.4u-", yr); |
111 | (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo); | |
112 | (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day); | |
113 | (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr); | |
114 | (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min); | |
1da177e4 LT |
115 | (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec); |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file) | |
121 | { | |
122 | return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data); | |
123 | } | |
124 | ||
4be44fcd | 125 | static int get_date_field(char **p, u32 * value) |
1da177e4 | 126 | { |
4be44fcd LB |
127 | char *next = NULL; |
128 | char *string_end = NULL; | |
129 | int result = -EINVAL; | |
1da177e4 LT |
130 | |
131 | /* | |
132 | * Try to find delimeter, only to insert null. The end of the | |
133 | * string won't have one, but is still valid. | |
134 | */ | |
975c3025 SYY |
135 | if (*p == NULL) |
136 | return result; | |
137 | ||
1da177e4 LT |
138 | next = strpbrk(*p, "- :"); |
139 | if (next) | |
140 | *next++ = '\0'; | |
141 | ||
142 | *value = simple_strtoul(*p, &string_end, 10); | |
143 | ||
144 | /* Signal success if we got a good digit */ | |
145 | if (string_end != *p) | |
146 | result = 0; | |
147 | ||
148 | if (next) | |
149 | *p = next; | |
975c3025 SYY |
150 | else |
151 | *p = NULL; | |
1da177e4 LT |
152 | |
153 | return result; | |
154 | } | |
155 | ||
c67c36e4 LT |
156 | /* Read a possibly BCD register, always return binary */ |
157 | static u32 cmos_bcd_read(int offset, int rtc_control) | |
158 | { | |
159 | u32 val = CMOS_READ(offset); | |
160 | if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) | |
ab527d75 | 161 | val = bcd2bin(val); |
c67c36e4 LT |
162 | return val; |
163 | } | |
164 | ||
165 | /* Write binary value into possibly BCD register */ | |
166 | static void cmos_bcd_write(u32 val, int offset, int rtc_control) | |
167 | { | |
168 | if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) | |
ab527d75 | 169 | val = bin2bcd(val); |
c67c36e4 LT |
170 | CMOS_WRITE(val, offset); |
171 | } | |
172 | ||
1da177e4 | 173 | static ssize_t |
4be44fcd LB |
174 | acpi_system_write_alarm(struct file *file, |
175 | const char __user * buffer, size_t count, loff_t * ppos) | |
1da177e4 | 176 | { |
4be44fcd LB |
177 | int result = 0; |
178 | char alarm_string[30] = { '\0' }; | |
179 | char *p = alarm_string; | |
180 | u32 sec, min, hr, day, mo, yr; | |
181 | int adjust = 0; | |
182 | unsigned char rtc_control = 0; | |
1da177e4 | 183 | |
1da177e4 | 184 | if (count > sizeof(alarm_string) - 1) |
95d9a7a8 | 185 | return -EINVAL; |
4be44fcd | 186 | |
1da177e4 | 187 | if (copy_from_user(alarm_string, buffer, count)) |
95d9a7a8 | 188 | return -EFAULT; |
1da177e4 LT |
189 | |
190 | alarm_string[count] = '\0'; | |
191 | ||
192 | /* check for time adjustment */ | |
193 | if (alarm_string[0] == '+') { | |
194 | p++; | |
195 | adjust = 1; | |
196 | } | |
197 | ||
198 | if ((result = get_date_field(&p, &yr))) | |
199 | goto end; | |
200 | if ((result = get_date_field(&p, &mo))) | |
201 | goto end; | |
202 | if ((result = get_date_field(&p, &day))) | |
203 | goto end; | |
204 | if ((result = get_date_field(&p, &hr))) | |
205 | goto end; | |
206 | if ((result = get_date_field(&p, &min))) | |
207 | goto end; | |
208 | if ((result = get_date_field(&p, &sec))) | |
209 | goto end; | |
210 | ||
1da177e4 LT |
211 | spin_lock_irq(&rtc_lock); |
212 | ||
213 | rtc_control = CMOS_READ(RTC_CONTROL); | |
1da177e4 LT |
214 | |
215 | if (adjust) { | |
c67c36e4 LT |
216 | yr += cmos_bcd_read(RTC_YEAR, rtc_control); |
217 | mo += cmos_bcd_read(RTC_MONTH, rtc_control); | |
218 | day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control); | |
219 | hr += cmos_bcd_read(RTC_HOURS, rtc_control); | |
220 | min += cmos_bcd_read(RTC_MINUTES, rtc_control); | |
221 | sec += cmos_bcd_read(RTC_SECONDS, rtc_control); | |
1da177e4 LT |
222 | } |
223 | ||
224 | spin_unlock_irq(&rtc_lock); | |
225 | ||
1da177e4 | 226 | if (sec > 59) { |
08798029 YY |
227 | min += sec/60; |
228 | sec = sec%60; | |
1da177e4 LT |
229 | } |
230 | if (min > 59) { | |
08798029 YY |
231 | hr += min/60; |
232 | min = min%60; | |
1da177e4 LT |
233 | } |
234 | if (hr > 23) { | |
08798029 YY |
235 | day += hr/24; |
236 | hr = hr%24; | |
1da177e4 LT |
237 | } |
238 | if (day > 31) { | |
08798029 YY |
239 | mo += day/32; |
240 | day = day%32; | |
1da177e4 LT |
241 | } |
242 | if (mo > 12) { | |
08798029 YY |
243 | yr += mo/13; |
244 | mo = mo%13; | |
1da177e4 | 245 | } |
1da177e4 LT |
246 | |
247 | spin_lock_irq(&rtc_lock); | |
248 | /* | |
249 | * Disable alarm interrupt before setting alarm timer or else | |
250 | * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs | |
251 | */ | |
252 | rtc_control &= ~RTC_AIE; | |
253 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
254 | CMOS_READ(RTC_INTR_FLAGS); | |
255 | ||
256 | /* write the fields the rtc knows about */ | |
c67c36e4 LT |
257 | cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control); |
258 | cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control); | |
259 | cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control); | |
1da177e4 LT |
260 | |
261 | /* | |
262 | * If the system supports an enhanced alarm it will have non-zero | |
263 | * offsets into the CMOS RAM here -- which for some reason are pointing | |
264 | * to the RTC area of memory. | |
265 | */ | |
ad71860a | 266 | if (acpi_gbl_FADT.day_alarm) |
c67c36e4 | 267 | cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control); |
ad71860a | 268 | if (acpi_gbl_FADT.month_alarm) |
c67c36e4 | 269 | cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control); |
cce3ce89 HC |
270 | if (acpi_gbl_FADT.century) { |
271 | if (adjust) | |
272 | yr += cmos_bcd_read(acpi_gbl_FADT.century, rtc_control) * 100; | |
c67c36e4 | 273 | cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control); |
cce3ce89 | 274 | } |
1da177e4 LT |
275 | /* enable the rtc alarm interrupt */ |
276 | rtc_control |= RTC_AIE; | |
277 | CMOS_WRITE(rtc_control, RTC_CONTROL); | |
278 | CMOS_READ(RTC_INTR_FLAGS); | |
279 | ||
280 | spin_unlock_irq(&rtc_lock); | |
281 | ||
282 | acpi_clear_event(ACPI_EVENT_RTC); | |
283 | acpi_enable_event(ACPI_EVENT_RTC, 0); | |
284 | ||
285 | *ppos += count; | |
286 | ||
287 | result = 0; | |
4be44fcd | 288 | end: |
95d9a7a8 | 289 | return result ? result : count; |
1da177e4 | 290 | } |
fd350943 | 291 | #endif /* HAVE_ACPI_LEGACY_ALARM */ |
1da177e4 | 292 | |
1da177e4 LT |
293 | static int |
294 | acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset) | |
295 | { | |
4be44fcd | 296 | struct list_head *node, *next; |
1da177e4 | 297 | |
8aa55591 | 298 | seq_printf(seq, "Device\tS-state\t Status Sysfs node\n"); |
1da177e4 | 299 | |
9090589d | 300 | mutex_lock(&acpi_device_lock); |
1da177e4 | 301 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
4be44fcd LB |
302 | struct acpi_device *dev = |
303 | container_of(node, struct acpi_device, wakeup_list); | |
8aa55591 | 304 | struct device *ldev; |
1da177e4 LT |
305 | |
306 | if (!dev->wakeup.flags.valid) | |
307 | continue; | |
8aa55591 DB |
308 | |
309 | ldev = acpi_get_physical_device(dev->handle); | |
310 | seq_printf(seq, "%s\t S%d\t%c%-8s ", | |
4be44fcd LB |
311 | dev->pnp.bus_id, |
312 | (u32) dev->wakeup.sleep_state, | |
8aa55591 | 313 | dev->wakeup.flags.run_wake ? '*' : ' ', |
c65ade4d | 314 | dev->wakeup.state.enabled ? "enabled" : "disabled"); |
8aa55591 DB |
315 | if (ldev) |
316 | seq_printf(seq, "%s:%s", | |
77fb61a0 | 317 | ldev->bus ? ldev->bus->name : "no-bus", |
0794469d | 318 | dev_name(ldev)); |
8aa55591 DB |
319 | seq_printf(seq, "\n"); |
320 | put_device(ldev); | |
321 | ||
1da177e4 | 322 | } |
9090589d | 323 | mutex_unlock(&acpi_device_lock); |
1da177e4 LT |
324 | return 0; |
325 | } | |
326 | ||
76acae04 RW |
327 | static void physical_device_enable_wakeup(struct acpi_device *adev) |
328 | { | |
329 | struct device *dev = acpi_get_physical_device(adev->handle); | |
330 | ||
331 | if (dev && device_can_wakeup(dev)) | |
332 | device_set_wakeup_enable(dev, adev->wakeup.state.enabled); | |
333 | } | |
334 | ||
1da177e4 | 335 | static ssize_t |
4be44fcd LB |
336 | acpi_system_write_wakeup_device(struct file *file, |
337 | const char __user * buffer, | |
338 | size_t count, loff_t * ppos) | |
1da177e4 | 339 | { |
4be44fcd LB |
340 | struct list_head *node, *next; |
341 | char strbuf[5]; | |
342 | char str[5] = ""; | |
52a2b11c | 343 | unsigned int len = count; |
1da177e4 | 344 | |
4be44fcd LB |
345 | if (len > 4) |
346 | len = 4; | |
d9f65018 AV |
347 | if (len < 0) |
348 | return -EFAULT; | |
1da177e4 LT |
349 | |
350 | if (copy_from_user(strbuf, buffer, len)) | |
351 | return -EFAULT; | |
352 | strbuf[len] = '\0'; | |
353 | sscanf(strbuf, "%s", str); | |
354 | ||
9090589d | 355 | mutex_lock(&acpi_device_lock); |
1da177e4 | 356 | list_for_each_safe(node, next, &acpi_wakeup_device_list) { |
4be44fcd LB |
357 | struct acpi_device *dev = |
358 | container_of(node, struct acpi_device, wakeup_list); | |
1da177e4 LT |
359 | if (!dev->wakeup.flags.valid) |
360 | continue; | |
361 | ||
362 | if (!strncmp(dev->pnp.bus_id, str, 4)) { | |
4be44fcd LB |
363 | dev->wakeup.state.enabled = |
364 | dev->wakeup.state.enabled ? 0 : 1; | |
b014f4f1 | 365 | physical_device_enable_wakeup(dev); |
1da177e4 LT |
366 | break; |
367 | } | |
368 | } | |
9090589d | 369 | mutex_unlock(&acpi_device_lock); |
1da177e4 LT |
370 | return count; |
371 | } | |
372 | ||
373 | static int | |
374 | acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file) | |
375 | { | |
4be44fcd LB |
376 | return single_open(file, acpi_system_wakeup_device_seq_show, |
377 | PDE(inode)->data); | |
1da177e4 LT |
378 | } |
379 | ||
d7508032 | 380 | static const struct file_operations acpi_system_wakeup_device_fops = { |
cf7acfab | 381 | .owner = THIS_MODULE, |
4be44fcd LB |
382 | .open = acpi_system_wakeup_device_open_fs, |
383 | .read = seq_read, | |
384 | .write = acpi_system_write_wakeup_device, | |
385 | .llseek = seq_lseek, | |
386 | .release = single_release, | |
1da177e4 LT |
387 | }; |
388 | ||
f5f72b46 | 389 | #ifdef HAVE_ACPI_LEGACY_ALARM |
d7508032 | 390 | static const struct file_operations acpi_system_alarm_fops = { |
cf7acfab | 391 | .owner = THIS_MODULE, |
4be44fcd LB |
392 | .open = acpi_system_alarm_open_fs, |
393 | .read = seq_read, | |
394 | .write = acpi_system_write_alarm, | |
395 | .llseek = seq_lseek, | |
396 | .release = single_release, | |
1da177e4 LT |
397 | }; |
398 | ||
4be44fcd | 399 | static u32 rtc_handler(void *context) |
1da177e4 LT |
400 | { |
401 | acpi_clear_event(ACPI_EVENT_RTC); | |
402 | acpi_disable_event(ACPI_EVENT_RTC, 0); | |
403 | ||
404 | return ACPI_INTERRUPT_HANDLED; | |
405 | } | |
fd350943 | 406 | #endif /* HAVE_ACPI_LEGACY_ALARM */ |
1da177e4 | 407 | |
9cee43e0 | 408 | int __init acpi_sleep_proc_init(void) |
1da177e4 | 409 | { |
f5f72b46 | 410 | #ifdef HAVE_ACPI_LEGACY_ALARM |
1da177e4 | 411 | /* 'alarm' [R/W] */ |
cf7acfab DL |
412 | proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR, |
413 | acpi_root_dir, &acpi_system_alarm_fops); | |
1da177e4 | 414 | |
f5f72b46 | 415 | acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL); |
e1094bfa ZY |
416 | /* |
417 | * Disable the RTC event after installing RTC handler. | |
418 | * Only when RTC alarm is set will it be enabled. | |
419 | */ | |
420 | acpi_clear_event(ACPI_EVENT_RTC); | |
421 | acpi_disable_event(ACPI_EVENT_RTC, 0); | |
fd350943 | 422 | #endif /* HAVE_ACPI_LEGACY_ALARM */ |
f5f72b46 | 423 | |
c65ade4d | 424 | /* 'wakeup device' [R/W] */ |
cf7acfab DL |
425 | proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR, |
426 | acpi_root_dir, &acpi_system_wakeup_device_fops); | |
1da177e4 | 427 | |
1da177e4 LT |
428 | return 0; |
429 | } |