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 static u32 cmos_bcd_read(int offset, int rtc_control);
89 #ifdef HAVE_ACPI_LEGACY_ALARM
91 static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
94 u32 day, mo, yr, cent = 0;
96 unsigned char rtc_control = 0;
99 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
101 spin_lock_irqsave(&rtc_lock, flags);
103 rtc_control = CMOS_READ(RTC_CONTROL);
104 sec = cmos_bcd_read(RTC_SECONDS_ALARM, rtc_control);
105 min = cmos_bcd_read(RTC_MINUTES_ALARM, rtc_control);
106 hr = cmos_bcd_read(RTC_HOURS_ALARM, rtc_control);
108 /* If we ever get an FACP with proper values... */
109 if (acpi_gbl_FADT.day_alarm) {
110 /* ACPI spec: only low 6 its should be cared */
111 day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
112 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
115 day = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
116 if (acpi_gbl_FADT.month_alarm)
117 mo = cmos_bcd_read(acpi_gbl_FADT.month_alarm, rtc_control);
119 mo = cmos_bcd_read(RTC_MONTH, rtc_control);
120 today = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
122 if (acpi_gbl_FADT.century)
123 cent = cmos_bcd_read(acpi_gbl_FADT.century, rtc_control);
125 yr = cmos_bcd_read(RTC_YEAR, rtc_control);
127 spin_unlock_irqrestore(&rtc_lock, flags);
129 /* we're trusting the FADT (see above) */
130 if (!acpi_gbl_FADT.century)
131 /* If we're not trusting the FADT, we should at least make it
132 * right for _this_ century... ehm, what is _this_ century?
135 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
136 * which we can trust to determine the century correctly. Atom
137 * watch driver would be nice, too...
139 * if that has not happened, change for first release in 2050:
143 * yr += 2000; // current line of code
145 * if that has not happened either, please do on 2099/12/31:23:59:59
154 * Show correct dates for alarms up to a month into the future.
155 * This solves issues for nearly all situations with the common
156 * 30-day alarm clocks in PC hardware.
167 seq_printf(seq, "%4.4u-", yr);
168 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
169 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
170 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
171 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
172 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
177 static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
179 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
182 static int get_date_field(char **p, u32 * value)
185 char *string_end = NULL;
186 int result = -EINVAL;
189 * Try to find delimeter, only to insert null. The end of the
190 * string won't have one, but is still valid.
195 next = strpbrk(*p, "- :");
199 *value = simple_strtoul(*p, &string_end, 10);
201 /* Signal success if we got a good digit */
202 if (string_end != *p)
213 /* Read a possibly BCD register, always return binary */
214 static u32 cmos_bcd_read(int offset, int rtc_control)
216 u32 val = CMOS_READ(offset);
217 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
222 /* Write binary value into possibly BCD register */
223 static void cmos_bcd_write(u32 val, int offset, int rtc_control)
225 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
227 CMOS_WRITE(val, offset);
231 acpi_system_write_alarm(struct file *file,
232 const char __user * buffer, size_t count, loff_t * ppos)
235 char alarm_string[30] = { '\0' };
236 char *p = alarm_string;
237 u32 sec, min, hr, day, mo, yr;
239 unsigned char rtc_control = 0;
241 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
243 if (count > sizeof(alarm_string) - 1)
244 return_VALUE(-EINVAL);
246 if (copy_from_user(alarm_string, buffer, count))
247 return_VALUE(-EFAULT);
249 alarm_string[count] = '\0';
251 /* check for time adjustment */
252 if (alarm_string[0] == '+') {
257 if ((result = get_date_field(&p, &yr)))
259 if ((result = get_date_field(&p, &mo)))
261 if ((result = get_date_field(&p, &day)))
263 if ((result = get_date_field(&p, &hr)))
265 if ((result = get_date_field(&p, &min)))
267 if ((result = get_date_field(&p, &sec)))
270 spin_lock_irq(&rtc_lock);
272 rtc_control = CMOS_READ(RTC_CONTROL);
275 yr += cmos_bcd_read(RTC_YEAR, rtc_control);
276 mo += cmos_bcd_read(RTC_MONTH, rtc_control);
277 day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
278 hr += cmos_bcd_read(RTC_HOURS, rtc_control);
279 min += cmos_bcd_read(RTC_MINUTES, rtc_control);
280 sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
283 spin_unlock_irq(&rtc_lock);
306 spin_lock_irq(&rtc_lock);
308 * Disable alarm interrupt before setting alarm timer or else
309 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
311 rtc_control &= ~RTC_AIE;
312 CMOS_WRITE(rtc_control, RTC_CONTROL);
313 CMOS_READ(RTC_INTR_FLAGS);
315 /* write the fields the rtc knows about */
316 cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
317 cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
318 cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
321 * If the system supports an enhanced alarm it will have non-zero
322 * offsets into the CMOS RAM here -- which for some reason are pointing
323 * to the RTC area of memory.
325 if (acpi_gbl_FADT.day_alarm)
326 cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
327 if (acpi_gbl_FADT.month_alarm)
328 cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
329 if (acpi_gbl_FADT.century) {
331 yr += cmos_bcd_read(acpi_gbl_FADT.century, rtc_control) * 100;
332 cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
334 /* enable the rtc alarm interrupt */
335 rtc_control |= RTC_AIE;
336 CMOS_WRITE(rtc_control, RTC_CONTROL);
337 CMOS_READ(RTC_INTR_FLAGS);
339 spin_unlock_irq(&rtc_lock);
341 acpi_clear_event(ACPI_EVENT_RTC);
342 acpi_enable_event(ACPI_EVENT_RTC, 0);
348 return_VALUE(result ? result : count);
350 #endif /* HAVE_ACPI_LEGACY_ALARM */
352 extern struct list_head acpi_wakeup_device_list;
353 extern spinlock_t acpi_device_lock;
356 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
358 struct list_head *node, *next;
360 seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
362 spin_lock(&acpi_device_lock);
363 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
364 struct acpi_device *dev =
365 container_of(node, struct acpi_device, wakeup_list);
368 if (!dev->wakeup.flags.valid)
370 spin_unlock(&acpi_device_lock);
372 ldev = acpi_get_physical_device(dev->handle);
373 seq_printf(seq, "%s\t S%d\t%c%-8s ",
375 (u32) dev->wakeup.sleep_state,
376 dev->wakeup.flags.run_wake ? '*' : ' ',
377 dev->wakeup.state.enabled ? "enabled" : "disabled");
379 seq_printf(seq, "%s:%s",
380 ldev->bus ? ldev->bus->name : "no-bus",
382 seq_printf(seq, "\n");
385 spin_lock(&acpi_device_lock);
387 spin_unlock(&acpi_device_lock);
391 static void physical_device_enable_wakeup(struct acpi_device *adev)
393 struct device *dev = acpi_get_physical_device(adev->handle);
395 if (dev && device_can_wakeup(dev))
396 device_set_wakeup_enable(dev, adev->wakeup.state.enabled);
400 acpi_system_write_wakeup_device(struct file *file,
401 const char __user * buffer,
402 size_t count, loff_t * ppos)
404 struct list_head *node, *next;
408 struct acpi_device *found_dev = NULL;
413 if (copy_from_user(strbuf, buffer, len))
416 sscanf(strbuf, "%s", str);
418 spin_lock(&acpi_device_lock);
419 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
420 struct acpi_device *dev =
421 container_of(node, struct acpi_device, wakeup_list);
422 if (!dev->wakeup.flags.valid)
425 if (!strncmp(dev->pnp.bus_id, str, 4)) {
426 dev->wakeup.state.enabled =
427 dev->wakeup.state.enabled ? 0 : 1;
433 physical_device_enable_wakeup(found_dev);
434 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
435 struct acpi_device *dev = container_of(node,
440 if ((dev != found_dev) &&
441 (dev->wakeup.gpe_number ==
442 found_dev->wakeup.gpe_number)
443 && (dev->wakeup.gpe_device ==
444 found_dev->wakeup.gpe_device)) {
446 "ACPI: '%s' and '%s' have the same GPE, "
447 "can't disable/enable one seperately\n",
448 dev->pnp.bus_id, found_dev->pnp.bus_id);
449 dev->wakeup.state.enabled =
450 found_dev->wakeup.state.enabled;
451 physical_device_enable_wakeup(dev);
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 .owner = THIS_MODULE,
468 .open = acpi_system_wakeup_device_open_fs,
470 .write = acpi_system_write_wakeup_device,
472 .release = single_release,
475 #ifdef CONFIG_ACPI_PROCFS
476 static const struct file_operations acpi_system_sleep_fops = {
477 .owner = THIS_MODULE,
478 .open = acpi_system_sleep_open_fs,
480 .write = acpi_system_write_sleep,
482 .release = single_release,
484 #endif /* CONFIG_ACPI_PROCFS */
486 #ifdef HAVE_ACPI_LEGACY_ALARM
487 static const struct file_operations acpi_system_alarm_fops = {
488 .owner = THIS_MODULE,
489 .open = acpi_system_alarm_open_fs,
491 .write = acpi_system_write_alarm,
493 .release = single_release,
496 static u32 rtc_handler(void *context)
498 acpi_clear_event(ACPI_EVENT_RTC);
499 acpi_disable_event(ACPI_EVENT_RTC, 0);
501 return ACPI_INTERRUPT_HANDLED;
503 #endif /* HAVE_ACPI_LEGACY_ALARM */
505 static int __init acpi_sleep_proc_init(void)
510 #ifdef CONFIG_ACPI_PROCFS
512 proc_create("sleep", S_IFREG | S_IRUGO | S_IWUSR,
513 acpi_root_dir, &acpi_system_sleep_fops);
514 #endif /* CONFIG_ACPI_PROCFS */
516 #ifdef HAVE_ACPI_LEGACY_ALARM
518 proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR,
519 acpi_root_dir, &acpi_system_alarm_fops);
521 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
523 * Disable the RTC event after installing RTC handler.
524 * Only when RTC alarm is set will it be enabled.
526 acpi_clear_event(ACPI_EVENT_RTC);
527 acpi_disable_event(ACPI_EVENT_RTC, 0);
528 #endif /* HAVE_ACPI_LEGACY_ALARM */
530 /* 'wakeup device' [R/W] */
531 proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
532 acpi_root_dir, &acpi_system_wakeup_device_fops);
537 late_initcall(acpi_sleep_proc_init);