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 u32 cmos_bcd_read(int offset, int rtc_control);
89 static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
92 u32 day, mo, yr, cent = 0;
94 unsigned char rtc_control = 0;
97 spin_lock_irqsave(&rtc_lock, flags);
99 rtc_control = CMOS_READ(RTC_CONTROL);
100 sec = cmos_bcd_read(RTC_SECONDS_ALARM, rtc_control);
101 min = cmos_bcd_read(RTC_MINUTES_ALARM, rtc_control);
102 hr = cmos_bcd_read(RTC_HOURS_ALARM, rtc_control);
104 /* If we ever get an FACP with proper values... */
105 if (acpi_gbl_FADT.day_alarm) {
106 /* ACPI spec: only low 6 its should be cared */
107 day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
108 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
111 day = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
112 if (acpi_gbl_FADT.month_alarm)
113 mo = cmos_bcd_read(acpi_gbl_FADT.month_alarm, rtc_control);
115 mo = cmos_bcd_read(RTC_MONTH, rtc_control);
116 today = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
118 if (acpi_gbl_FADT.century)
119 cent = cmos_bcd_read(acpi_gbl_FADT.century, rtc_control);
121 yr = cmos_bcd_read(RTC_YEAR, rtc_control);
123 spin_unlock_irqrestore(&rtc_lock, flags);
125 /* we're trusting the FADT (see above) */
126 if (!acpi_gbl_FADT.century)
127 /* If we're not trusting the FADT, we should at least make it
128 * right for _this_ century... ehm, what is _this_ century?
131 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
132 * which we can trust to determine the century correctly. Atom
133 * watch driver would be nice, too...
135 * if that has not happened, change for first release in 2050:
139 * yr += 2000; // current line of code
141 * if that has not happened either, please do on 2099/12/31:23:59:59
150 * Show correct dates for alarms up to a month into the future.
151 * This solves issues for nearly all situations with the common
152 * 30-day alarm clocks in PC hardware.
163 seq_printf(seq, "%4.4u-", yr);
164 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
165 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
166 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
167 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
168 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
173 static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
175 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
178 static int get_date_field(char **p, u32 * value)
181 char *string_end = NULL;
182 int result = -EINVAL;
185 * Try to find delimeter, only to insert null. The end of the
186 * string won't have one, but is still valid.
191 next = strpbrk(*p, "- :");
195 *value = simple_strtoul(*p, &string_end, 10);
197 /* Signal success if we got a good digit */
198 if (string_end != *p)
209 /* Read a possibly BCD register, always return binary */
210 static u32 cmos_bcd_read(int offset, int rtc_control)
212 u32 val = CMOS_READ(offset);
213 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
218 /* Write binary value into possibly BCD register */
219 static void cmos_bcd_write(u32 val, int offset, int rtc_control)
221 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
223 CMOS_WRITE(val, offset);
227 acpi_system_write_alarm(struct file *file,
228 const char __user * buffer, size_t count, loff_t * ppos)
231 char alarm_string[30] = { '\0' };
232 char *p = alarm_string;
233 u32 sec, min, hr, day, mo, yr;
235 unsigned char rtc_control = 0;
237 if (count > sizeof(alarm_string) - 1)
240 if (copy_from_user(alarm_string, buffer, count))
243 alarm_string[count] = '\0';
245 /* check for time adjustment */
246 if (alarm_string[0] == '+') {
251 if ((result = get_date_field(&p, &yr)))
253 if ((result = get_date_field(&p, &mo)))
255 if ((result = get_date_field(&p, &day)))
257 if ((result = get_date_field(&p, &hr)))
259 if ((result = get_date_field(&p, &min)))
261 if ((result = get_date_field(&p, &sec)))
264 spin_lock_irq(&rtc_lock);
266 rtc_control = CMOS_READ(RTC_CONTROL);
269 yr += cmos_bcd_read(RTC_YEAR, rtc_control);
270 mo += cmos_bcd_read(RTC_MONTH, rtc_control);
271 day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
272 hr += cmos_bcd_read(RTC_HOURS, rtc_control);
273 min += cmos_bcd_read(RTC_MINUTES, rtc_control);
274 sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
277 spin_unlock_irq(&rtc_lock);
300 spin_lock_irq(&rtc_lock);
302 * Disable alarm interrupt before setting alarm timer or else
303 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
305 rtc_control &= ~RTC_AIE;
306 CMOS_WRITE(rtc_control, RTC_CONTROL);
307 CMOS_READ(RTC_INTR_FLAGS);
309 /* write the fields the rtc knows about */
310 cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
311 cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
312 cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
315 * If the system supports an enhanced alarm it will have non-zero
316 * offsets into the CMOS RAM here -- which for some reason are pointing
317 * to the RTC area of memory.
319 if (acpi_gbl_FADT.day_alarm)
320 cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
321 if (acpi_gbl_FADT.month_alarm)
322 cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
323 if (acpi_gbl_FADT.century) {
325 yr += cmos_bcd_read(acpi_gbl_FADT.century, rtc_control) * 100;
326 cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
328 /* enable the rtc alarm interrupt */
329 rtc_control |= RTC_AIE;
330 CMOS_WRITE(rtc_control, RTC_CONTROL);
331 CMOS_READ(RTC_INTR_FLAGS);
333 spin_unlock_irq(&rtc_lock);
335 acpi_clear_event(ACPI_EVENT_RTC);
336 acpi_enable_event(ACPI_EVENT_RTC, 0);
342 return result ? result : count;
344 #endif /* HAVE_ACPI_LEGACY_ALARM */
346 extern struct list_head acpi_wakeup_device_list;
347 extern spinlock_t acpi_device_lock;
350 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
352 struct list_head *node, *next;
354 seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
356 spin_lock(&acpi_device_lock);
357 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
358 struct acpi_device *dev =
359 container_of(node, struct acpi_device, wakeup_list);
362 if (!dev->wakeup.flags.valid)
364 spin_unlock(&acpi_device_lock);
366 ldev = acpi_get_physical_device(dev->handle);
367 seq_printf(seq, "%s\t S%d\t%c%-8s ",
369 (u32) dev->wakeup.sleep_state,
370 dev->wakeup.flags.run_wake ? '*' : ' ',
371 dev->wakeup.state.enabled ? "enabled" : "disabled");
373 seq_printf(seq, "%s:%s",
374 ldev->bus ? ldev->bus->name : "no-bus",
376 seq_printf(seq, "\n");
379 spin_lock(&acpi_device_lock);
381 spin_unlock(&acpi_device_lock);
385 static void physical_device_enable_wakeup(struct acpi_device *adev)
387 struct device *dev = acpi_get_physical_device(adev->handle);
389 if (dev && device_can_wakeup(dev))
390 device_set_wakeup_enable(dev, adev->wakeup.state.enabled);
394 acpi_system_write_wakeup_device(struct file *file,
395 const char __user * buffer,
396 size_t count, loff_t * ppos)
398 struct list_head *node, *next;
402 struct acpi_device *found_dev = NULL;
407 if (copy_from_user(strbuf, buffer, len))
410 sscanf(strbuf, "%s", str);
412 spin_lock(&acpi_device_lock);
413 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
414 struct acpi_device *dev =
415 container_of(node, struct acpi_device, wakeup_list);
416 if (!dev->wakeup.flags.valid)
419 if (!strncmp(dev->pnp.bus_id, str, 4)) {
420 dev->wakeup.state.enabled =
421 dev->wakeup.state.enabled ? 0 : 1;
427 physical_device_enable_wakeup(found_dev);
428 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
429 struct acpi_device *dev = container_of(node,
434 if ((dev != found_dev) &&
435 (dev->wakeup.gpe_number ==
436 found_dev->wakeup.gpe_number)
437 && (dev->wakeup.gpe_device ==
438 found_dev->wakeup.gpe_device)) {
440 "ACPI: '%s' and '%s' have the same GPE, "
441 "can't disable/enable one seperately\n",
442 dev->pnp.bus_id, found_dev->pnp.bus_id);
443 dev->wakeup.state.enabled =
444 found_dev->wakeup.state.enabled;
445 physical_device_enable_wakeup(dev);
449 spin_unlock(&acpi_device_lock);
454 acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
456 return single_open(file, acpi_system_wakeup_device_seq_show,
460 static const struct file_operations acpi_system_wakeup_device_fops = {
461 .owner = THIS_MODULE,
462 .open = acpi_system_wakeup_device_open_fs,
464 .write = acpi_system_write_wakeup_device,
466 .release = single_release,
469 #ifdef CONFIG_ACPI_PROCFS
470 static const struct file_operations acpi_system_sleep_fops = {
471 .owner = THIS_MODULE,
472 .open = acpi_system_sleep_open_fs,
474 .write = acpi_system_write_sleep,
476 .release = single_release,
478 #endif /* CONFIG_ACPI_PROCFS */
480 #ifdef HAVE_ACPI_LEGACY_ALARM
481 static const struct file_operations acpi_system_alarm_fops = {
482 .owner = THIS_MODULE,
483 .open = acpi_system_alarm_open_fs,
485 .write = acpi_system_write_alarm,
487 .release = single_release,
490 static u32 rtc_handler(void *context)
492 acpi_clear_event(ACPI_EVENT_RTC);
493 acpi_disable_event(ACPI_EVENT_RTC, 0);
495 return ACPI_INTERRUPT_HANDLED;
497 #endif /* HAVE_ACPI_LEGACY_ALARM */
499 int __init acpi_sleep_proc_init(void)
501 #ifdef CONFIG_ACPI_PROCFS
503 proc_create("sleep", S_IFREG | S_IRUGO | S_IWUSR,
504 acpi_root_dir, &acpi_system_sleep_fops);
505 #endif /* CONFIG_ACPI_PROCFS */
507 #ifdef HAVE_ACPI_LEGACY_ALARM
509 proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR,
510 acpi_root_dir, &acpi_system_alarm_fops);
512 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
514 * Disable the RTC event after installing RTC handler.
515 * Only when RTC alarm is set will it be enabled.
517 acpi_clear_event(ACPI_EVENT_RTC);
518 acpi_disable_event(ACPI_EVENT_RTC, 0);
519 #endif /* HAVE_ACPI_LEGACY_ALARM */
521 /* 'wakeup device' [R/W] */
522 proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
523 acpi_root_dir, &acpi_system_wakeup_device_fops);