blob: 09a603f3523ec148df72d54d629ef4712fdbe8c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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 Torvalds1da177e2005-04-16 15:20:36 -070016#define _COMPONENT ACPI_SYSTEM_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040017ACPI_MODULE_NAME("sleep")
Len Brownebb6e1a62005-04-14 23:12:56 -040018#ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static int acpi_system_sleep_seq_show(struct seq_file *seq, void *offset)
20{
Len Brown4be44fc2005-08-05 00:44:28 -040021 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23 ACPI_FUNCTION_TRACE("acpi_system_sleep_seq_show");
24
25 for (i = 0; i <= ACPI_STATE_S5; i++) {
26 if (sleep_states[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -040027 seq_printf(seq, "S%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 if (i == ACPI_STATE_S4 && acpi_gbl_FACS->S4bios_f)
29 seq_printf(seq, "S4bios ");
30 }
31 }
32
33 seq_puts(seq, "\n");
34
35 return 0;
36}
37
38static int acpi_system_sleep_open_fs(struct inode *inode, struct file *file)
39{
40 return single_open(file, acpi_system_sleep_seq_show, PDE(inode)->data);
41}
42
43static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -040044acpi_system_write_sleep(struct file *file,
45 const char __user * buffer, size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Len Brown4be44fc2005-08-05 00:44:28 -040047 char str[12];
48 u32 state = 0;
49 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 if (count > sizeof(str) - 1)
52 goto Done;
Len Brown4be44fc2005-08-05 00:44:28 -040053 memset(str, 0, sizeof(str));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (copy_from_user(str, buffer, count))
55 return -EFAULT;
56
57 /* Check for S4 bios request */
Len Brown4be44fc2005-08-05 00:44:28 -040058 if (!strcmp(str, "4b")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 error = acpi_suspend(4);
60 goto Done;
61 }
62 state = simple_strtoul(str, NULL, 0);
63#ifdef CONFIG_SOFTWARE_SUSPEND
64 if (state == 4) {
65 error = software_suspend();
66 goto Done;
67 }
68#endif
69 error = acpi_suspend(state);
Len Brown4be44fc2005-08-05 00:44:28 -040070 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return error ? error : count;
72}
Len Brown4be44fc2005-08-05 00:44:28 -040073#endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
76{
Len Brown4be44fc2005-08-05 00:44:28 -040077 u32 sec, min, hr;
78 u32 day, mo, yr;
79 unsigned char rtc_control = 0;
80 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
83
84 spin_lock_irqsave(&rtc_lock, flags);
85
86 sec = CMOS_READ(RTC_SECONDS_ALARM);
87 min = CMOS_READ(RTC_MINUTES_ALARM);
88 hr = CMOS_READ(RTC_HOURS_ALARM);
89 rtc_control = CMOS_READ(RTC_CONTROL);
90
91 /* If we ever get an FACP with proper values... */
92 if (acpi_gbl_FADT->day_alrm)
93 /* ACPI spec: only low 6 its should be cared */
94 day = CMOS_READ(acpi_gbl_FADT->day_alrm) & 0x3F;
95 else
Len Brown4be44fc2005-08-05 00:44:28 -040096 day = CMOS_READ(RTC_DAY_OF_MONTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (acpi_gbl_FADT->mon_alrm)
98 mo = CMOS_READ(acpi_gbl_FADT->mon_alrm);
99 else
100 mo = CMOS_READ(RTC_MONTH);
101 if (acpi_gbl_FADT->century)
Len Brown4be44fc2005-08-05 00:44:28 -0400102 yr = CMOS_READ(acpi_gbl_FADT->century) * 100 +
103 CMOS_READ(RTC_YEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 else
105 yr = CMOS_READ(RTC_YEAR);
106
107 spin_unlock_irqrestore(&rtc_lock, flags);
108
109 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
110 BCD_TO_BIN(sec);
111 BCD_TO_BIN(min);
112 BCD_TO_BIN(hr);
113 BCD_TO_BIN(day);
114 BCD_TO_BIN(mo);
115 BCD_TO_BIN(yr);
116 }
117
Len Brown4be44fc2005-08-05 00:44:28 -0400118 /* we're trusting the FADT (see above) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!acpi_gbl_FADT->century)
Len Brown4be44fc2005-08-05 00:44:28 -0400120 /* If we're not trusting the FADT, we should at least make it
121 * right for _this_ century... ehm, what is _this_ century?
122 *
123 * TBD:
124 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
125 * which we can trust to determine the century correctly. Atom
126 * watch driver would be nice, too...
127 *
128 * if that has not happened, change for first release in 2050:
129 * if (yr<50)
130 * yr += 2100;
131 * else
132 * yr += 2000; // current line of code
133 *
134 * if that has not happened either, please do on 2099/12/31:23:59:59
135 * s/2000/2100
136 *
137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 yr += 2000;
139
Len Brown4be44fc2005-08-05 00:44:28 -0400140 seq_printf(seq, "%4.4u-", yr);
141 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
142 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
143 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
144 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
146
147 return 0;
148}
149
150static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
151{
152 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
153}
154
Len Brown4be44fc2005-08-05 00:44:28 -0400155static int get_date_field(char **p, u32 * value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Len Brown4be44fc2005-08-05 00:44:28 -0400157 char *next = NULL;
158 char *string_end = NULL;
159 int result = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /*
162 * Try to find delimeter, only to insert null. The end of the
163 * string won't have one, but is still valid.
164 */
165 next = strpbrk(*p, "- :");
166 if (next)
167 *next++ = '\0';
168
169 *value = simple_strtoul(*p, &string_end, 10);
170
171 /* Signal success if we got a good digit */
172 if (string_end != *p)
173 result = 0;
174
175 if (next)
176 *p = next;
177
178 return result;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400182acpi_system_write_alarm(struct file *file,
183 const char __user * buffer, size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Len Brown4be44fc2005-08-05 00:44:28 -0400185 int result = 0;
186 char alarm_string[30] = { '\0' };
187 char *p = alarm_string;
188 u32 sec, min, hr, day, mo, yr;
189 int adjust = 0;
190 unsigned char rtc_control = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
193
194 if (count > sizeof(alarm_string) - 1)
195 return_VALUE(-EINVAL);
Len Brown4be44fc2005-08-05 00:44:28 -0400196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (copy_from_user(alarm_string, buffer, count))
198 return_VALUE(-EFAULT);
199
200 alarm_string[count] = '\0';
201
202 /* check for time adjustment */
203 if (alarm_string[0] == '+') {
204 p++;
205 adjust = 1;
206 }
207
208 if ((result = get_date_field(&p, &yr)))
209 goto end;
210 if ((result = get_date_field(&p, &mo)))
211 goto end;
212 if ((result = get_date_field(&p, &day)))
213 goto end;
214 if ((result = get_date_field(&p, &hr)))
215 goto end;
216 if ((result = get_date_field(&p, &min)))
217 goto end;
218 if ((result = get_date_field(&p, &sec)))
219 goto end;
220
221 if (sec > 59) {
222 min += 1;
223 sec -= 60;
224 }
225 if (min > 59) {
226 hr += 1;
227 min -= 60;
228 }
229 if (hr > 23) {
230 day += 1;
231 hr -= 24;
232 }
233 if (day > 31) {
234 mo += 1;
235 day -= 31;
236 }
237 if (mo > 12) {
238 yr += 1;
239 mo -= 12;
240 }
241
242 spin_lock_irq(&rtc_lock);
243
244 rtc_control = CMOS_READ(RTC_CONTROL);
245 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
246 BIN_TO_BCD(yr);
247 BIN_TO_BCD(mo);
248 BIN_TO_BCD(day);
249 BIN_TO_BCD(hr);
250 BIN_TO_BCD(min);
251 BIN_TO_BCD(sec);
252 }
253
254 if (adjust) {
Len Brown4be44fc2005-08-05 00:44:28 -0400255 yr += CMOS_READ(RTC_YEAR);
256 mo += CMOS_READ(RTC_MONTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 day += CMOS_READ(RTC_DAY_OF_MONTH);
Len Brown4be44fc2005-08-05 00:44:28 -0400258 hr += CMOS_READ(RTC_HOURS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 min += CMOS_READ(RTC_MINUTES);
260 sec += CMOS_READ(RTC_SECONDS);
261 }
262
263 spin_unlock_irq(&rtc_lock);
264
265 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
266 BCD_TO_BIN(yr);
267 BCD_TO_BIN(mo);
268 BCD_TO_BIN(day);
269 BCD_TO_BIN(hr);
270 BCD_TO_BIN(min);
271 BCD_TO_BIN(sec);
272 }
273
274 if (sec > 59) {
275 min++;
276 sec -= 60;
277 }
278 if (min > 59) {
279 hr++;
280 min -= 60;
281 }
282 if (hr > 23) {
283 day++;
284 hr -= 24;
285 }
286 if (day > 31) {
287 mo++;
288 day -= 31;
289 }
290 if (mo > 12) {
291 yr++;
292 mo -= 12;
293 }
294 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
295 BIN_TO_BCD(yr);
296 BIN_TO_BCD(mo);
297 BIN_TO_BCD(day);
298 BIN_TO_BCD(hr);
299 BIN_TO_BCD(min);
300 BIN_TO_BCD(sec);
301 }
302
303 spin_lock_irq(&rtc_lock);
304 /*
305 * Disable alarm interrupt before setting alarm timer or else
306 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
307 */
308 rtc_control &= ~RTC_AIE;
309 CMOS_WRITE(rtc_control, RTC_CONTROL);
310 CMOS_READ(RTC_INTR_FLAGS);
311
312 /* write the fields the rtc knows about */
313 CMOS_WRITE(hr, RTC_HOURS_ALARM);
314 CMOS_WRITE(min, RTC_MINUTES_ALARM);
315 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
316
317 /*
318 * If the system supports an enhanced alarm it will have non-zero
319 * offsets into the CMOS RAM here -- which for some reason are pointing
320 * to the RTC area of memory.
321 */
322 if (acpi_gbl_FADT->day_alrm)
323 CMOS_WRITE(day, acpi_gbl_FADT->day_alrm);
324 if (acpi_gbl_FADT->mon_alrm)
325 CMOS_WRITE(mo, acpi_gbl_FADT->mon_alrm);
326 if (acpi_gbl_FADT->century)
Len Brown4be44fc2005-08-05 00:44:28 -0400327 CMOS_WRITE(yr / 100, acpi_gbl_FADT->century);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* 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 Brown4be44fc2005-08-05 00:44:28 -0400341 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return_VALUE(result ? result : count);
343}
344
Len Brown4be44fc2005-08-05 00:44:28 -0400345extern struct list_head acpi_wakeup_device_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346extern spinlock_t acpi_device_lock;
347
348static int
349acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
350{
Len Brown4be44fc2005-08-05 00:44:28 -0400351 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 seq_printf(seq, "Device Sleep state Status\n");
354
355 spin_lock(&acpi_device_lock);
356 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400357 struct acpi_device *dev =
358 container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!dev->wakeup.flags.valid)
361 continue;
362 spin_unlock(&acpi_device_lock);
Pavel Machekc65ade42005-08-05 00:37:45 -0400363 seq_printf(seq, "%4s %4d %s%8s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400364 dev->pnp.bus_id,
365 (u32) dev->wakeup.sleep_state,
Pavel Machekc65ade42005-08-05 00:37:45 -0400366 dev->wakeup.flags.run_wake ? "*" : "",
367 dev->wakeup.state.enabled ? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_lock(&acpi_device_lock);
369 }
370 spin_unlock(&acpi_device_lock);
371 return 0;
372}
373
374static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400375acpi_system_write_wakeup_device(struct file *file,
376 const char __user * buffer,
377 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Len Brown4be44fc2005-08-05 00:44:28 -0400379 struct list_head *node, *next;
380 char strbuf[5];
381 char str[5] = "";
382 int len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct acpi_device *found_dev = NULL;
384
Len Brown4be44fc2005-08-05 00:44:28 -0400385 if (len > 4)
386 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (copy_from_user(strbuf, buffer, len))
389 return -EFAULT;
390 strbuf[len] = '\0';
391 sscanf(strbuf, "%s", str);
392
393 spin_lock(&acpi_device_lock);
394 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400395 struct acpi_device *dev =
396 container_of(node, struct acpi_device, wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!dev->wakeup.flags.valid)
398 continue;
399
400 if (!strncmp(dev->pnp.bus_id, str, 4)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400401 dev->wakeup.state.enabled =
402 dev->wakeup.state.enabled ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 found_dev = dev;
404 break;
405 }
406 }
407 if (found_dev) {
408 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400409 struct acpi_device *dev = container_of(node,
410 struct
411 acpi_device,
412 wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if ((dev != found_dev) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400415 (dev->wakeup.gpe_number ==
416 found_dev->wakeup.gpe_number)
417 && (dev->wakeup.gpe_device ==
418 found_dev->wakeup.gpe_device)) {
419 printk(KERN_WARNING
420 "ACPI: '%s' and '%s' have the same GPE, "
421 "can't disable/enable one seperately\n",
422 dev->pnp.bus_id, found_dev->pnp.bus_id);
423 dev->wakeup.state.enabled =
424 found_dev->wakeup.state.enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 }
427 }
428 spin_unlock(&acpi_device_lock);
429 return count;
430}
431
432static int
433acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
434{
Len Brown4be44fc2005-08-05 00:44:28 -0400435 return single_open(file, acpi_system_wakeup_device_seq_show,
436 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439static struct file_operations acpi_system_wakeup_device_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400440 .open = acpi_system_wakeup_device_open_fs,
441 .read = seq_read,
442 .write = acpi_system_write_wakeup_device,
443 .llseek = seq_lseek,
444 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
Len Brownebb6e1a62005-04-14 23:12:56 -0400447#ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static struct file_operations acpi_system_sleep_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400449 .open = acpi_system_sleep_open_fs,
450 .read = seq_read,
451 .write = acpi_system_write_sleep,
452 .llseek = seq_lseek,
453 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
Len Brown4be44fc2005-08-05 00:44:28 -0400455#endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457static struct file_operations acpi_system_alarm_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400458 .open = acpi_system_alarm_open_fs,
459 .read = seq_read,
460 .write = acpi_system_write_alarm,
461 .llseek = seq_lseek,
462 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463};
464
Len Brown4be44fc2005-08-05 00:44:28 -0400465static u32 rtc_handler(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 acpi_clear_event(ACPI_EVENT_RTC);
468 acpi_disable_event(ACPI_EVENT_RTC, 0);
469
470 return ACPI_INTERRUPT_HANDLED;
471}
472
473static int acpi_sleep_proc_init(void)
474{
Pavel Machekc65ade42005-08-05 00:37:45 -0400475 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (acpi_disabled)
478 return 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400479
Len Brownebb6e1a62005-04-14 23:12:56 -0400480#ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
Pavel Machekc65ade42005-08-05 00:37:45 -0400481 /* 'sleep' [R/W] */
Len Brown4be44fc2005-08-05 00:44:28 -0400482 entry =
483 create_proc_entry("sleep", S_IFREG | S_IRUGO | S_IWUSR,
484 acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (entry)
486 entry->proc_fops = &acpi_system_sleep_fops;
Len Brownebb6e1a62005-04-14 23:12:56 -0400487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* 'alarm' [R/W] */
Len Brown4be44fc2005-08-05 00:44:28 -0400490 entry =
491 create_proc_entry("alarm", S_IFREG | S_IRUGO | S_IWUSR,
492 acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (entry)
494 entry->proc_fops = &acpi_system_alarm_fops;
495
Pavel Machekc65ade42005-08-05 00:37:45 -0400496 /* 'wakeup device' [R/W] */
Len Brown4be44fc2005-08-05 00:44:28 -0400497 entry =
498 create_proc_entry("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
499 acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (entry)
501 entry->proc_fops = &acpi_system_wakeup_device_fops;
502
503 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
504 return 0;
505}
506
507late_initcall(acpi_sleep_proc_init);