1 #include <linux/proc_fs.h>
2 #include <linux/seq_file.h>
3 #include <linux/suspend.h>
5 #include <asm/uaccess.h>
7 #include <acpi/acpi_bus.h>
8 #include <acpi/acpi_drivers.h>
11 #include <linux/mc146818rtc.h>
16 #define _COMPONENT ACPI_SYSTEM_COMPONENT
19 * this file provides support for:
25 ACPI_MODULE_NAME("sleep")
26 #ifdef CONFIG_ACPI_PROCFS
27 static int acpi_system_sleep_seq_show(struct seq_file *seq, void *offset)
31 ACPI_FUNCTION_TRACE("acpi_system_sleep_seq_show");
33 for (i = 0; i <= ACPI_STATE_S5; i++) {
34 if (sleep_states[i]) {
35 seq_printf(seq, "S%d ", i);
44 static int acpi_system_sleep_open_fs(struct inode *inode, struct file *file)
46 return single_open(file, acpi_system_sleep_seq_show, PDE(inode)->data);
50 acpi_system_write_sleep(struct file *file,
51 const char __user * buffer, size_t count, loff_t * ppos)
57 if (count > sizeof(str) - 1)
59 memset(str, 0, sizeof(str));
60 if (copy_from_user(str, buffer, count))
63 /* Check for S4 bios request */
64 if (!strcmp(str, "4b")) {
65 error = acpi_suspend(4);
68 state = simple_strtoul(str, NULL, 0);
69 #ifdef CONFIG_HIBERNATION
75 error = acpi_suspend(state);
77 return error ? error : count;
79 #endif /* CONFIG_ACPI_PROCFS */
81 #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86)
82 /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
84 #define HAVE_ACPI_LEGACY_ALARM
87 #ifdef HAVE_ACPI_LEGACY_ALARM
89 static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
92 u32 day, mo, yr, cent = 0;
93 unsigned char rtc_control = 0;
96 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
98 spin_lock_irqsave(&rtc_lock, flags);
100 sec = CMOS_READ(RTC_SECONDS_ALARM);
101 min = CMOS_READ(RTC_MINUTES_ALARM);
102 hr = CMOS_READ(RTC_HOURS_ALARM);
103 rtc_control = CMOS_READ(RTC_CONTROL);
105 /* If we ever get an FACP with proper values... */
106 if (acpi_gbl_FADT.day_alarm)
107 /* ACPI spec: only low 6 its should be cared */
108 day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
110 day = CMOS_READ(RTC_DAY_OF_MONTH);
111 if (acpi_gbl_FADT.month_alarm)
112 mo = CMOS_READ(acpi_gbl_FADT.month_alarm);
114 mo = CMOS_READ(RTC_MONTH);
115 if (acpi_gbl_FADT.century)
116 cent = CMOS_READ(acpi_gbl_FADT.century);
118 yr = CMOS_READ(RTC_YEAR);
120 spin_unlock_irqrestore(&rtc_lock, flags);
122 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
132 /* we're trusting the FADT (see above) */
133 if (!acpi_gbl_FADT.century)
134 /* If we're not trusting the FADT, we should at least make it
135 * right for _this_ century... ehm, what is _this_ century?
138 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
139 * which we can trust to determine the century correctly. Atom
140 * watch driver would be nice, too...
142 * if that has not happened, change for first release in 2050:
146 * yr += 2000; // current line of code
148 * if that has not happened either, please do on 2099/12/31:23:59:59
156 seq_printf(seq, "%4.4u-", yr);
157 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
158 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
159 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
160 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
161 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
166 static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
168 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
171 static int get_date_field(char **p, u32 * value)
174 char *string_end = NULL;
175 int result = -EINVAL;
178 * Try to find delimeter, only to insert null. The end of the
179 * string won't have one, but is still valid.
181 next = strpbrk(*p, "- :");
185 *value = simple_strtoul(*p, &string_end, 10);
187 /* Signal success if we got a good digit */
188 if (string_end != *p)
198 acpi_system_write_alarm(struct file *file,
199 const char __user * buffer, size_t count, loff_t * ppos)
202 char alarm_string[30] = { '\0' };
203 char *p = alarm_string;
204 u32 sec, min, hr, day, mo, yr;
206 unsigned char rtc_control = 0;
208 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
210 if (count > sizeof(alarm_string) - 1)
211 return_VALUE(-EINVAL);
213 if (copy_from_user(alarm_string, buffer, count))
214 return_VALUE(-EFAULT);
216 alarm_string[count] = '\0';
218 /* check for time adjustment */
219 if (alarm_string[0] == '+') {
224 if ((result = get_date_field(&p, &yr)))
226 if ((result = get_date_field(&p, &mo)))
228 if ((result = get_date_field(&p, &day)))
230 if ((result = get_date_field(&p, &hr)))
232 if ((result = get_date_field(&p, &min)))
234 if ((result = get_date_field(&p, &sec)))
258 spin_lock_irq(&rtc_lock);
260 rtc_control = CMOS_READ(RTC_CONTROL);
261 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
271 yr += CMOS_READ(RTC_YEAR);
272 mo += CMOS_READ(RTC_MONTH);
273 day += CMOS_READ(RTC_DAY_OF_MONTH);
274 hr += CMOS_READ(RTC_HOURS);
275 min += CMOS_READ(RTC_MINUTES);
276 sec += CMOS_READ(RTC_SECONDS);
279 spin_unlock_irq(&rtc_lock);
281 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
310 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
319 spin_lock_irq(&rtc_lock);
321 * Disable alarm interrupt before setting alarm timer or else
322 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
324 rtc_control &= ~RTC_AIE;
325 CMOS_WRITE(rtc_control, RTC_CONTROL);
326 CMOS_READ(RTC_INTR_FLAGS);
328 /* write the fields the rtc knows about */
329 CMOS_WRITE(hr, RTC_HOURS_ALARM);
330 CMOS_WRITE(min, RTC_MINUTES_ALARM);
331 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
334 * If the system supports an enhanced alarm it will have non-zero
335 * offsets into the CMOS RAM here -- which for some reason are pointing
336 * to the RTC area of memory.
338 if (acpi_gbl_FADT.day_alarm)
339 CMOS_WRITE(day, acpi_gbl_FADT.day_alarm);
340 if (acpi_gbl_FADT.month_alarm)
341 CMOS_WRITE(mo, acpi_gbl_FADT.month_alarm);
342 if (acpi_gbl_FADT.century)
343 CMOS_WRITE(yr / 100, acpi_gbl_FADT.century);
344 /* enable the rtc alarm interrupt */
345 rtc_control |= RTC_AIE;
346 CMOS_WRITE(rtc_control, RTC_CONTROL);
347 CMOS_READ(RTC_INTR_FLAGS);
349 spin_unlock_irq(&rtc_lock);
351 acpi_clear_event(ACPI_EVENT_RTC);
352 acpi_enable_event(ACPI_EVENT_RTC, 0);
358 return_VALUE(result ? result : count);
360 #endif /* HAVE_ACPI_LEGACY_ALARM */
362 extern struct list_head acpi_wakeup_device_list;
363 extern spinlock_t acpi_device_lock;
366 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
368 struct list_head *node, *next;
370 seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
372 spin_lock(&acpi_device_lock);
373 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
374 struct acpi_device *dev =
375 container_of(node, struct acpi_device, wakeup_list);
378 if (!dev->wakeup.flags.valid)
380 spin_unlock(&acpi_device_lock);
382 ldev = acpi_get_physical_device(dev->handle);
383 seq_printf(seq, "%s\t S%d\t%c%-8s ",
385 (u32) dev->wakeup.sleep_state,
386 dev->wakeup.flags.run_wake ? '*' : ' ',
387 dev->wakeup.state.enabled ? "enabled" : "disabled");
389 seq_printf(seq, "%s:%s",
390 ldev->bus ? ldev->bus->name : "no-bus",
392 seq_printf(seq, "\n");
395 spin_lock(&acpi_device_lock);
397 spin_unlock(&acpi_device_lock);
402 acpi_system_write_wakeup_device(struct file *file,
403 const char __user * buffer,
404 size_t count, loff_t * ppos)
406 struct list_head *node, *next;
410 struct acpi_device *found_dev = NULL;
415 if (copy_from_user(strbuf, buffer, len))
418 sscanf(strbuf, "%s", str);
420 spin_lock(&acpi_device_lock);
421 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
422 struct acpi_device *dev =
423 container_of(node, struct acpi_device, wakeup_list);
424 if (!dev->wakeup.flags.valid)
427 if (!strncmp(dev->pnp.bus_id, str, 4)) {
428 dev->wakeup.state.enabled =
429 dev->wakeup.state.enabled ? 0 : 1;
435 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
436 struct acpi_device *dev = container_of(node,
441 if ((dev != found_dev) &&
442 (dev->wakeup.gpe_number ==
443 found_dev->wakeup.gpe_number)
444 && (dev->wakeup.gpe_device ==
445 found_dev->wakeup.gpe_device)) {
447 "ACPI: '%s' and '%s' have the same GPE, "
448 "can't disable/enable one seperately\n",
449 dev->pnp.bus_id, found_dev->pnp.bus_id);
450 dev->wakeup.state.enabled =
451 found_dev->wakeup.state.enabled;
455 spin_unlock(&acpi_device_lock);
460 acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
462 return single_open(file, acpi_system_wakeup_device_seq_show,
466 static const struct file_operations acpi_system_wakeup_device_fops = {
467 .open = acpi_system_wakeup_device_open_fs,
469 .write = acpi_system_write_wakeup_device,
471 .release = single_release,
474 #ifdef CONFIG_ACPI_PROCFS
475 static const struct file_operations acpi_system_sleep_fops = {
476 .open = acpi_system_sleep_open_fs,
478 .write = acpi_system_write_sleep,
480 .release = single_release,
482 #endif /* CONFIG_ACPI_PROCFS */
484 #ifdef HAVE_ACPI_LEGACY_ALARM
485 static const struct file_operations acpi_system_alarm_fops = {
486 .open = acpi_system_alarm_open_fs,
488 .write = acpi_system_write_alarm,
490 .release = single_release,
493 static u32 rtc_handler(void *context)
495 acpi_clear_event(ACPI_EVENT_RTC);
496 acpi_disable_event(ACPI_EVENT_RTC, 0);
498 return ACPI_INTERRUPT_HANDLED;
500 #endif /* HAVE_ACPI_LEGACY_ALARM */
502 static int __init acpi_sleep_proc_init(void)
504 struct proc_dir_entry *entry = NULL;
509 #ifdef CONFIG_ACPI_PROCFS
512 create_proc_entry("sleep", S_IFREG | S_IRUGO | S_IWUSR,
515 entry->proc_fops = &acpi_system_sleep_fops;
516 #endif /* CONFIG_ACPI_PROCFS */
518 #ifdef HAVE_ACPI_LEGACY_ALARM
521 create_proc_entry("alarm", S_IFREG | S_IRUGO | S_IWUSR,
524 entry->proc_fops = &acpi_system_alarm_fops;
526 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
527 #endif /* HAVE_ACPI_LEGACY_ALARM */
529 /* 'wakeup device' [R/W] */
531 create_proc_entry("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
534 entry->proc_fops = &acpi_system_wakeup_device_fops;
539 late_initcall(acpi_sleep_proc_init);