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 for (i = 0; i <= ACPI_STATE_S5; i++) {
32 if (sleep_states[i]) {
33 seq_printf(seq, "S%d ", i);
42 static int acpi_system_sleep_open_fs(struct inode *inode, struct file *file)
44 return single_open(file, acpi_system_sleep_seq_show, PDE(inode)->data);
48 acpi_system_write_sleep(struct file *file,
49 const char __user * buffer, size_t count, loff_t * ppos)
55 if (count > sizeof(str) - 1)
57 memset(str, 0, sizeof(str));
58 if (copy_from_user(str, buffer, count))
61 /* Check for S4 bios request */
62 if (!strcmp(str, "4b")) {
63 error = acpi_suspend(4);
66 state = simple_strtoul(str, NULL, 0);
67 #ifdef CONFIG_HIBERNATION
73 error = acpi_suspend(state);
75 return error ? error : count;
77 #endif /* CONFIG_ACPI_PROCFS */
79 #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86)
80 /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
82 #define HAVE_ACPI_LEGACY_ALARM
85 #ifdef HAVE_ACPI_LEGACY_ALARM
87 static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
90 u32 day, mo, yr, cent = 0;
91 unsigned char rtc_control = 0;
94 spin_lock_irqsave(&rtc_lock, flags);
96 sec = CMOS_READ(RTC_SECONDS_ALARM);
97 min = CMOS_READ(RTC_MINUTES_ALARM);
98 hr = CMOS_READ(RTC_HOURS_ALARM);
99 rtc_control = CMOS_READ(RTC_CONTROL);
101 /* If we ever get an FACP with proper values... */
102 if (acpi_gbl_FADT.day_alarm)
103 /* ACPI spec: only low 6 its should be cared */
104 day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
106 day = CMOS_READ(RTC_DAY_OF_MONTH);
107 if (acpi_gbl_FADT.month_alarm)
108 mo = CMOS_READ(acpi_gbl_FADT.month_alarm);
110 mo = CMOS_READ(RTC_MONTH);
111 if (acpi_gbl_FADT.century)
112 cent = CMOS_READ(acpi_gbl_FADT.century);
114 yr = CMOS_READ(RTC_YEAR);
116 spin_unlock_irqrestore(&rtc_lock, flags);
118 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
125 cent = bcd2bin(cent);
128 /* we're trusting the FADT (see above) */
129 if (!acpi_gbl_FADT.century)
130 /* If we're not trusting the FADT, we should at least make it
131 * right for _this_ century... ehm, what is _this_ century?
134 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
135 * which we can trust to determine the century correctly. Atom
136 * watch driver would be nice, too...
138 * if that has not happened, change for first release in 2050:
142 * yr += 2000; // current line of code
144 * if that has not happened either, please do on 2099/12/31:23:59:59
152 seq_printf(seq, "%4.4u-", yr);
153 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
154 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
155 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
156 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
157 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
162 static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
164 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
167 static int get_date_field(char **p, u32 * value)
170 char *string_end = NULL;
171 int result = -EINVAL;
174 * Try to find delimeter, only to insert null. The end of the
175 * string won't have one, but is still valid.
180 next = strpbrk(*p, "- :");
184 *value = simple_strtoul(*p, &string_end, 10);
186 /* Signal success if we got a good digit */
187 if (string_end != *p)
198 /* Read a possibly BCD register, always return binary */
199 static u32 cmos_bcd_read(int offset, int rtc_control)
201 u32 val = CMOS_READ(offset);
202 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
207 /* Write binary value into possibly BCD register */
208 static void cmos_bcd_write(u32 val, int offset, int rtc_control)
210 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
212 CMOS_WRITE(val, offset);
216 acpi_system_write_alarm(struct file *file,
217 const char __user * buffer, size_t count, loff_t * ppos)
220 char alarm_string[30] = { '\0' };
221 char *p = alarm_string;
222 u32 sec, min, hr, day, mo, yr;
224 unsigned char rtc_control = 0;
226 if (count > sizeof(alarm_string) - 1)
229 if (copy_from_user(alarm_string, buffer, count))
232 alarm_string[count] = '\0';
234 /* check for time adjustment */
235 if (alarm_string[0] == '+') {
240 if ((result = get_date_field(&p, &yr)))
242 if ((result = get_date_field(&p, &mo)))
244 if ((result = get_date_field(&p, &day)))
246 if ((result = get_date_field(&p, &hr)))
248 if ((result = get_date_field(&p, &min)))
250 if ((result = get_date_field(&p, &sec)))
253 spin_lock_irq(&rtc_lock);
255 rtc_control = CMOS_READ(RTC_CONTROL);
258 yr += cmos_bcd_read(RTC_YEAR, rtc_control);
259 mo += cmos_bcd_read(RTC_MONTH, rtc_control);
260 day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
261 hr += cmos_bcd_read(RTC_HOURS, rtc_control);
262 min += cmos_bcd_read(RTC_MINUTES, rtc_control);
263 sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
266 spin_unlock_irq(&rtc_lock);
289 spin_lock_irq(&rtc_lock);
291 * Disable alarm interrupt before setting alarm timer or else
292 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
294 rtc_control &= ~RTC_AIE;
295 CMOS_WRITE(rtc_control, RTC_CONTROL);
296 CMOS_READ(RTC_INTR_FLAGS);
298 /* write the fields the rtc knows about */
299 cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
300 cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
301 cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
304 * If the system supports an enhanced alarm it will have non-zero
305 * offsets into the CMOS RAM here -- which for some reason are pointing
306 * to the RTC area of memory.
308 if (acpi_gbl_FADT.day_alarm)
309 cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
310 if (acpi_gbl_FADT.month_alarm)
311 cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
312 if (acpi_gbl_FADT.century) {
314 yr += cmos_bcd_read(acpi_gbl_FADT.century, rtc_control) * 100;
315 cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
317 /* enable the rtc alarm interrupt */
318 rtc_control |= RTC_AIE;
319 CMOS_WRITE(rtc_control, RTC_CONTROL);
320 CMOS_READ(RTC_INTR_FLAGS);
322 spin_unlock_irq(&rtc_lock);
324 acpi_clear_event(ACPI_EVENT_RTC);
325 acpi_enable_event(ACPI_EVENT_RTC, 0);
331 return result ? result : count;
333 #endif /* HAVE_ACPI_LEGACY_ALARM */
335 extern struct list_head acpi_wakeup_device_list;
336 extern spinlock_t acpi_device_lock;
339 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
341 struct list_head *node, *next;
343 seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
345 spin_lock(&acpi_device_lock);
346 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
347 struct acpi_device *dev =
348 container_of(node, struct acpi_device, wakeup_list);
351 if (!dev->wakeup.flags.valid)
353 spin_unlock(&acpi_device_lock);
355 ldev = acpi_get_physical_device(dev->handle);
356 seq_printf(seq, "%s\t S%d\t%c%-8s ",
358 (u32) dev->wakeup.sleep_state,
359 dev->wakeup.flags.run_wake ? '*' : ' ',
360 dev->wakeup.state.enabled ? "enabled" : "disabled");
362 seq_printf(seq, "%s:%s",
363 ldev->bus ? ldev->bus->name : "no-bus",
365 seq_printf(seq, "\n");
368 spin_lock(&acpi_device_lock);
370 spin_unlock(&acpi_device_lock);
374 static void physical_device_enable_wakeup(struct acpi_device *adev)
376 struct device *dev = acpi_get_physical_device(adev->handle);
378 if (dev && device_can_wakeup(dev))
379 device_set_wakeup_enable(dev, adev->wakeup.state.enabled);
383 acpi_system_write_wakeup_device(struct file *file,
384 const char __user * buffer,
385 size_t count, loff_t * ppos)
387 struct list_head *node, *next;
391 struct acpi_device *found_dev = NULL;
396 if (copy_from_user(strbuf, buffer, len))
399 sscanf(strbuf, "%s", str);
401 spin_lock(&acpi_device_lock);
402 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
403 struct acpi_device *dev =
404 container_of(node, struct acpi_device, wakeup_list);
405 if (!dev->wakeup.flags.valid)
408 if (!strncmp(dev->pnp.bus_id, str, 4)) {
409 dev->wakeup.state.enabled =
410 dev->wakeup.state.enabled ? 0 : 1;
416 physical_device_enable_wakeup(found_dev);
417 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
418 struct acpi_device *dev = container_of(node,
423 if ((dev != found_dev) &&
424 (dev->wakeup.gpe_number ==
425 found_dev->wakeup.gpe_number)
426 && (dev->wakeup.gpe_device ==
427 found_dev->wakeup.gpe_device)) {
429 "ACPI: '%s' and '%s' have the same GPE, "
430 "can't disable/enable one seperately\n",
431 dev->pnp.bus_id, found_dev->pnp.bus_id);
432 dev->wakeup.state.enabled =
433 found_dev->wakeup.state.enabled;
434 physical_device_enable_wakeup(dev);
438 spin_unlock(&acpi_device_lock);
443 acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
445 return single_open(file, acpi_system_wakeup_device_seq_show,
449 static const struct file_operations acpi_system_wakeup_device_fops = {
450 .owner = THIS_MODULE,
451 .open = acpi_system_wakeup_device_open_fs,
453 .write = acpi_system_write_wakeup_device,
455 .release = single_release,
458 #ifdef CONFIG_ACPI_PROCFS
459 static const struct file_operations acpi_system_sleep_fops = {
460 .owner = THIS_MODULE,
461 .open = acpi_system_sleep_open_fs,
463 .write = acpi_system_write_sleep,
465 .release = single_release,
467 #endif /* CONFIG_ACPI_PROCFS */
469 #ifdef HAVE_ACPI_LEGACY_ALARM
470 static const struct file_operations acpi_system_alarm_fops = {
471 .owner = THIS_MODULE,
472 .open = acpi_system_alarm_open_fs,
474 .write = acpi_system_write_alarm,
476 .release = single_release,
479 static u32 rtc_handler(void *context)
481 acpi_clear_event(ACPI_EVENT_RTC);
482 acpi_disable_event(ACPI_EVENT_RTC, 0);
484 return ACPI_INTERRUPT_HANDLED;
486 #endif /* HAVE_ACPI_LEGACY_ALARM */
488 static int __init acpi_sleep_proc_init(void)
493 #ifdef CONFIG_ACPI_PROCFS
495 proc_create("sleep", S_IFREG | S_IRUGO | S_IWUSR,
496 acpi_root_dir, &acpi_system_sleep_fops);
497 #endif /* CONFIG_ACPI_PROCFS */
499 #ifdef HAVE_ACPI_LEGACY_ALARM
501 proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR,
502 acpi_root_dir, &acpi_system_alarm_fops);
504 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
506 * Disable the RTC event after installing RTC handler.
507 * Only when RTC alarm is set will it be enabled.
509 acpi_clear_event(ACPI_EVENT_RTC);
510 acpi_disable_event(ACPI_EVENT_RTC, 0);
511 #endif /* HAVE_ACPI_LEGACY_ALARM */
513 /* 'wakeup device' [R/W] */
514 proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
515 acpi_root_dir, &acpi_system_wakeup_device_fops);
520 late_initcall(acpi_sleep_proc_init);