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
17 ACPI_MODULE_NAME("sleep")
18 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
19 static int acpi_system_sleep_seq_show(struct seq_file *seq, void *offset)
23 ACPI_FUNCTION_TRACE("acpi_system_sleep_seq_show");
25 for (i = 0; i <= ACPI_STATE_S5; i++) {
26 if (sleep_states[i]) {
27 seq_printf(seq, "S%d ", i);
36 static int acpi_system_sleep_open_fs(struct inode *inode, struct file *file)
38 return single_open(file, acpi_system_sleep_seq_show, PDE(inode)->data);
42 acpi_system_write_sleep(struct file *file,
43 const char __user * buffer, size_t count, loff_t * ppos)
49 if (count > sizeof(str) - 1)
51 memset(str, 0, sizeof(str));
52 if (copy_from_user(str, buffer, count))
55 /* Check for S4 bios request */
56 if (!strcmp(str, "4b")) {
57 error = acpi_suspend(4);
60 state = simple_strtoul(str, NULL, 0);
61 #ifdef CONFIG_SOFTWARE_SUSPEND
63 error = pm_suspend(PM_SUSPEND_DISK);
67 error = acpi_suspend(state);
69 return error ? error : count;
71 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
73 static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
76 u32 day, mo, yr, cent = 0;
77 unsigned char rtc_control = 0;
80 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
82 spin_lock_irqsave(&rtc_lock, flags);
84 sec = CMOS_READ(RTC_SECONDS_ALARM);
85 min = CMOS_READ(RTC_MINUTES_ALARM);
86 hr = CMOS_READ(RTC_HOURS_ALARM);
87 rtc_control = CMOS_READ(RTC_CONTROL);
89 /* If we ever get an FACP with proper values... */
90 if (acpi_gbl_FADT.day_alarm)
91 /* ACPI spec: only low 6 its should be cared */
92 day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
94 day = CMOS_READ(RTC_DAY_OF_MONTH);
95 if (acpi_gbl_FADT.month_alarm)
96 mo = CMOS_READ(acpi_gbl_FADT.month_alarm);
98 mo = CMOS_READ(RTC_MONTH);
99 if (acpi_gbl_FADT.century)
100 cent = CMOS_READ(acpi_gbl_FADT.century);
102 yr = CMOS_READ(RTC_YEAR);
104 spin_unlock_irqrestore(&rtc_lock, flags);
106 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
116 /* we're trusting the FADT (see above) */
117 if (!acpi_gbl_FADT.century)
118 /* If we're not trusting the FADT, we should at least make it
119 * right for _this_ century... ehm, what is _this_ century?
122 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
123 * which we can trust to determine the century correctly. Atom
124 * watch driver would be nice, too...
126 * if that has not happened, change for first release in 2050:
130 * yr += 2000; // current line of code
132 * if that has not happened either, please do on 2099/12/31:23:59:59
140 seq_printf(seq, "%4.4u-", yr);
141 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
142 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
143 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
144 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
145 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
150 static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
152 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
155 static int get_date_field(char **p, u32 * value)
158 char *string_end = NULL;
159 int result = -EINVAL;
162 * Try to find delimeter, only to insert null. The end of the
163 * string won't have one, but is still valid.
165 next = strpbrk(*p, "- :");
169 *value = simple_strtoul(*p, &string_end, 10);
171 /* Signal success if we got a good digit */
172 if (string_end != *p)
182 acpi_system_write_alarm(struct file *file,
183 const char __user * buffer, size_t count, loff_t * ppos)
186 char alarm_string[30] = { '\0' };
187 char *p = alarm_string;
188 u32 sec, min, hr, day, mo, yr;
190 unsigned char rtc_control = 0;
192 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
194 if (count > sizeof(alarm_string) - 1)
195 return_VALUE(-EINVAL);
197 if (copy_from_user(alarm_string, buffer, count))
198 return_VALUE(-EFAULT);
200 alarm_string[count] = '\0';
202 /* check for time adjustment */
203 if (alarm_string[0] == '+') {
208 if ((result = get_date_field(&p, &yr)))
210 if ((result = get_date_field(&p, &mo)))
212 if ((result = get_date_field(&p, &day)))
214 if ((result = get_date_field(&p, &hr)))
216 if ((result = get_date_field(&p, &min)))
218 if ((result = get_date_field(&p, &sec)))
242 spin_lock_irq(&rtc_lock);
244 rtc_control = CMOS_READ(RTC_CONTROL);
245 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
255 yr += CMOS_READ(RTC_YEAR);
256 mo += CMOS_READ(RTC_MONTH);
257 day += CMOS_READ(RTC_DAY_OF_MONTH);
258 hr += CMOS_READ(RTC_HOURS);
259 min += CMOS_READ(RTC_MINUTES);
260 sec += CMOS_READ(RTC_SECONDS);
263 spin_unlock_irq(&rtc_lock);
265 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
294 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
303 spin_lock_irq(&rtc_lock);
305 * Disable alarm interrupt before setting alarm timer or else
306 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
308 rtc_control &= ~RTC_AIE;
309 CMOS_WRITE(rtc_control, RTC_CONTROL);
310 CMOS_READ(RTC_INTR_FLAGS);
312 /* write the fields the rtc knows about */
313 CMOS_WRITE(hr, RTC_HOURS_ALARM);
314 CMOS_WRITE(min, RTC_MINUTES_ALARM);
315 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
318 * If the system supports an enhanced alarm it will have non-zero
319 * offsets into the CMOS RAM here -- which for some reason are pointing
320 * to the RTC area of memory.
322 if (acpi_gbl_FADT.day_alarm)
323 CMOS_WRITE(day, acpi_gbl_FADT.day_alarm);
324 if (acpi_gbl_FADT.month_alarm)
325 CMOS_WRITE(mo, acpi_gbl_FADT.month_alarm);
326 if (acpi_gbl_FADT.century)
327 CMOS_WRITE(yr / 100, acpi_gbl_FADT.century);
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_VALUE(result ? result : count);
345 extern struct list_head acpi_wakeup_device_list;
346 extern spinlock_t acpi_device_lock;
349 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
351 struct list_head *node, *next;
353 seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
355 spin_lock(&acpi_device_lock);
356 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
357 struct acpi_device *dev =
358 container_of(node, struct acpi_device, wakeup_list);
361 if (!dev->wakeup.flags.valid)
363 spin_unlock(&acpi_device_lock);
365 ldev = acpi_get_physical_device(dev->handle);
366 seq_printf(seq, "%s\t S%d\t%c%-8s ",
368 (u32) dev->wakeup.sleep_state,
369 dev->wakeup.flags.run_wake ? '*' : ' ',
370 dev->wakeup.state.enabled ? "enabled" : "disabled");
372 seq_printf(seq, "%s:%s",
373 ldev->bus ? ldev->bus->name : "no-bus",
375 seq_printf(seq, "\n");
378 spin_lock(&acpi_device_lock);
380 spin_unlock(&acpi_device_lock);
385 acpi_system_write_wakeup_device(struct file *file,
386 const char __user * buffer,
387 size_t count, loff_t * ppos)
389 struct list_head *node, *next;
393 struct acpi_device *found_dev = NULL;
398 if (copy_from_user(strbuf, buffer, len))
401 sscanf(strbuf, "%s", str);
403 spin_lock(&acpi_device_lock);
404 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
405 struct acpi_device *dev =
406 container_of(node, struct acpi_device, wakeup_list);
407 if (!dev->wakeup.flags.valid)
410 if (!strncmp(dev->pnp.bus_id, str, 4)) {
411 dev->wakeup.state.enabled =
412 dev->wakeup.state.enabled ? 0 : 1;
418 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
419 struct acpi_device *dev = container_of(node,
424 if ((dev != found_dev) &&
425 (dev->wakeup.gpe_number ==
426 found_dev->wakeup.gpe_number)
427 && (dev->wakeup.gpe_device ==
428 found_dev->wakeup.gpe_device)) {
430 "ACPI: '%s' and '%s' have the same GPE, "
431 "can't disable/enable one seperately\n",
432 dev->pnp.bus_id, found_dev->pnp.bus_id);
433 dev->wakeup.state.enabled =
434 found_dev->wakeup.state.enabled;
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 .open = acpi_system_wakeup_device_open_fs,
452 .write = acpi_system_write_wakeup_device,
454 .release = single_release,
457 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
458 static const struct file_operations acpi_system_sleep_fops = {
459 .open = acpi_system_sleep_open_fs,
461 .write = acpi_system_write_sleep,
463 .release = single_release,
465 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
467 static const struct file_operations acpi_system_alarm_fops = {
468 .open = acpi_system_alarm_open_fs,
470 .write = acpi_system_write_alarm,
472 .release = single_release,
475 static u32 rtc_handler(void *context)
477 acpi_clear_event(ACPI_EVENT_RTC);
478 acpi_disable_event(ACPI_EVENT_RTC, 0);
480 return ACPI_INTERRUPT_HANDLED;
483 static int acpi_sleep_proc_init(void)
485 struct proc_dir_entry *entry = NULL;
490 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
493 create_proc_entry("sleep", S_IFREG | S_IRUGO | S_IWUSR,
496 entry->proc_fops = &acpi_system_sleep_fops;
501 create_proc_entry("alarm", S_IFREG | S_IRUGO | S_IWUSR,
504 entry->proc_fops = &acpi_system_alarm_fops;
506 /* 'wakeup device' [R/W] */
508 create_proc_entry("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
511 entry->proc_fops = &acpi_system_wakeup_device_fops;
513 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
517 late_initcall(acpi_sleep_proc_init);