1 #include <linux/proc_fs.h>
2 #include <linux/seq_file.h>
3 #include <linux/export.h>
4 #include <linux/suspend.h>
6 #include <asm/uaccess.h>
8 #include <acpi/acpi_bus.h>
9 #include <acpi/acpi_drivers.h>
12 #include <linux/mc146818rtc.h>
17 #define _COMPONENT ACPI_SYSTEM_COMPONENT
20 * this file provides support for:
25 ACPI_MODULE_NAME("sleep")
27 #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86)
28 /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
30 #define HAVE_ACPI_LEGACY_ALARM
33 #ifdef HAVE_ACPI_LEGACY_ALARM
35 static u32 cmos_bcd_read(int offset, int rtc_control);
37 static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
40 u32 day, mo, yr, cent = 0;
42 unsigned char rtc_control = 0;
45 spin_lock_irqsave(&rtc_lock, flags);
47 rtc_control = CMOS_READ(RTC_CONTROL);
48 sec = cmos_bcd_read(RTC_SECONDS_ALARM, rtc_control);
49 min = cmos_bcd_read(RTC_MINUTES_ALARM, rtc_control);
50 hr = cmos_bcd_read(RTC_HOURS_ALARM, rtc_control);
52 /* If we ever get an FACP with proper values... */
53 if (acpi_gbl_FADT.day_alarm) {
54 /* ACPI spec: only low 6 its should be cared */
55 day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
56 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
59 day = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
60 if (acpi_gbl_FADT.month_alarm)
61 mo = cmos_bcd_read(acpi_gbl_FADT.month_alarm, rtc_control);
63 mo = cmos_bcd_read(RTC_MONTH, rtc_control);
64 today = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
66 if (acpi_gbl_FADT.century)
67 cent = cmos_bcd_read(acpi_gbl_FADT.century, rtc_control);
69 yr = cmos_bcd_read(RTC_YEAR, rtc_control);
71 spin_unlock_irqrestore(&rtc_lock, flags);
73 /* we're trusting the FADT (see above) */
74 if (!acpi_gbl_FADT.century)
75 /* If we're not trusting the FADT, we should at least make it
76 * right for _this_ century... ehm, what is _this_ century?
79 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
80 * which we can trust to determine the century correctly. Atom
81 * watch driver would be nice, too...
83 * if that has not happened, change for first release in 2050:
87 * yr += 2000; // current line of code
89 * if that has not happened either, please do on 2099/12/31:23:59:59
98 * Show correct dates for alarms up to a month into the future.
99 * This solves issues for nearly all situations with the common
100 * 30-day alarm clocks in PC hardware.
111 seq_printf(seq, "%4.4u-", yr);
112 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
113 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
114 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
115 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
116 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
121 static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
123 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
126 static int get_date_field(char **p, u32 * value)
129 char *string_end = NULL;
130 int result = -EINVAL;
133 * Try to find delimeter, only to insert null. The end of the
134 * string won't have one, but is still valid.
139 next = strpbrk(*p, "- :");
143 *value = simple_strtoul(*p, &string_end, 10);
145 /* Signal success if we got a good digit */
146 if (string_end != *p)
157 /* Read a possibly BCD register, always return binary */
158 static u32 cmos_bcd_read(int offset, int rtc_control)
160 u32 val = CMOS_READ(offset);
161 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
166 /* Write binary value into possibly BCD register */
167 static void cmos_bcd_write(u32 val, int offset, int rtc_control)
169 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
171 CMOS_WRITE(val, offset);
175 acpi_system_write_alarm(struct file *file,
176 const char __user * buffer, size_t count, loff_t * ppos)
179 char alarm_string[30] = { '\0' };
180 char *p = alarm_string;
181 u32 sec, min, hr, day, mo, yr;
183 unsigned char rtc_control = 0;
185 if (count > sizeof(alarm_string) - 1)
188 if (copy_from_user(alarm_string, buffer, count))
191 alarm_string[count] = '\0';
193 /* check for time adjustment */
194 if (alarm_string[0] == '+') {
199 if ((result = get_date_field(&p, &yr)))
201 if ((result = get_date_field(&p, &mo)))
203 if ((result = get_date_field(&p, &day)))
205 if ((result = get_date_field(&p, &hr)))
207 if ((result = get_date_field(&p, &min)))
209 if ((result = get_date_field(&p, &sec)))
212 spin_lock_irq(&rtc_lock);
214 rtc_control = CMOS_READ(RTC_CONTROL);
217 yr += cmos_bcd_read(RTC_YEAR, rtc_control);
218 mo += cmos_bcd_read(RTC_MONTH, rtc_control);
219 day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
220 hr += cmos_bcd_read(RTC_HOURS, rtc_control);
221 min += cmos_bcd_read(RTC_MINUTES, rtc_control);
222 sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
225 spin_unlock_irq(&rtc_lock);
248 spin_lock_irq(&rtc_lock);
250 * Disable alarm interrupt before setting alarm timer or else
251 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
253 rtc_control &= ~RTC_AIE;
254 CMOS_WRITE(rtc_control, RTC_CONTROL);
255 CMOS_READ(RTC_INTR_FLAGS);
257 /* write the fields the rtc knows about */
258 cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
259 cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
260 cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
263 * If the system supports an enhanced alarm it will have non-zero
264 * offsets into the CMOS RAM here -- which for some reason are pointing
265 * to the RTC area of memory.
267 if (acpi_gbl_FADT.day_alarm)
268 cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
269 if (acpi_gbl_FADT.month_alarm)
270 cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
271 if (acpi_gbl_FADT.century) {
273 yr += cmos_bcd_read(acpi_gbl_FADT.century, rtc_control) * 100;
274 cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
276 /* enable the rtc alarm interrupt */
277 rtc_control |= RTC_AIE;
278 CMOS_WRITE(rtc_control, RTC_CONTROL);
279 CMOS_READ(RTC_INTR_FLAGS);
281 spin_unlock_irq(&rtc_lock);
283 acpi_clear_event(ACPI_EVENT_RTC);
284 acpi_enable_event(ACPI_EVENT_RTC, 0);
290 return result ? result : count;
292 #endif /* HAVE_ACPI_LEGACY_ALARM */
295 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
297 struct list_head *node, *next;
299 seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
301 mutex_lock(&acpi_device_lock);
302 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
303 struct acpi_device *dev =
304 container_of(node, struct acpi_device, wakeup_list);
307 if (!dev->wakeup.flags.valid)
310 ldev = acpi_get_physical_device(dev->handle);
311 seq_printf(seq, "%s\t S%d\t%c%-8s ",
313 (u32) dev->wakeup.sleep_state,
314 dev->wakeup.flags.run_wake ? '*' : ' ',
315 (device_may_wakeup(&dev->dev)
316 || (ldev && device_may_wakeup(ldev))) ?
317 "enabled" : "disabled");
319 seq_printf(seq, "%s:%s",
320 ldev->bus ? ldev->bus->name : "no-bus",
322 seq_printf(seq, "\n");
326 mutex_unlock(&acpi_device_lock);
330 static void physical_device_enable_wakeup(struct acpi_device *adev)
332 struct device *dev = acpi_get_physical_device(adev->handle);
334 if (dev && device_can_wakeup(dev)) {
335 bool enable = !device_may_wakeup(dev);
336 device_set_wakeup_enable(dev, enable);
341 acpi_system_write_wakeup_device(struct file *file,
342 const char __user * buffer,
343 size_t count, loff_t * ppos)
345 struct list_head *node, *next;
348 unsigned int len = count;
355 if (copy_from_user(strbuf, buffer, len))
358 sscanf(strbuf, "%s", str);
360 mutex_lock(&acpi_device_lock);
361 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
362 struct acpi_device *dev =
363 container_of(node, struct acpi_device, wakeup_list);
364 if (!dev->wakeup.flags.valid)
367 if (!strncmp(dev->pnp.bus_id, str, 4)) {
368 if (device_can_wakeup(&dev->dev)) {
369 bool enable = !device_may_wakeup(&dev->dev);
370 device_set_wakeup_enable(&dev->dev, enable);
372 physical_device_enable_wakeup(dev);
377 mutex_unlock(&acpi_device_lock);
382 acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
384 return single_open(file, acpi_system_wakeup_device_seq_show,
388 static const struct file_operations acpi_system_wakeup_device_fops = {
389 .owner = THIS_MODULE,
390 .open = acpi_system_wakeup_device_open_fs,
392 .write = acpi_system_write_wakeup_device,
394 .release = single_release,
397 #ifdef HAVE_ACPI_LEGACY_ALARM
398 static const struct file_operations acpi_system_alarm_fops = {
399 .owner = THIS_MODULE,
400 .open = acpi_system_alarm_open_fs,
402 .write = acpi_system_write_alarm,
404 .release = single_release,
407 static u32 rtc_handler(void *context)
409 acpi_clear_event(ACPI_EVENT_RTC);
410 acpi_disable_event(ACPI_EVENT_RTC, 0);
412 return ACPI_INTERRUPT_HANDLED;
414 #endif /* HAVE_ACPI_LEGACY_ALARM */
416 int __init acpi_sleep_proc_init(void)
418 #ifdef HAVE_ACPI_LEGACY_ALARM
420 proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR,
421 acpi_root_dir, &acpi_system_alarm_fops);
423 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
425 * Disable the RTC event after installing RTC handler.
426 * Only when RTC alarm is set will it be enabled.
428 acpi_clear_event(ACPI_EVENT_RTC);
429 acpi_disable_event(ACPI_EVENT_RTC, 0);
430 #endif /* HAVE_ACPI_LEGACY_ALARM */
432 /* 'wakeup device' [R/W] */
433 proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
434 acpi_root_dir, &acpi_system_wakeup_device_fops);