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 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
17 #define ACPI_SYSTEM_FILE_SLEEP "sleep"
20 #define ACPI_SYSTEM_FILE_ALARM "alarm"
21 #define ACPI_SYSTEM_FILE_WAKEUP_DEVICE "wakeup"
23 #define _COMPONENT ACPI_SYSTEM_COMPONENT
24 ACPI_MODULE_NAME ("sleep")
26 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
28 static int acpi_system_sleep_seq_show(struct seq_file *seq, void *offset)
32 ACPI_FUNCTION_TRACE("acpi_system_sleep_seq_show");
34 for (i = 0; i <= ACPI_STATE_S5; i++) {
35 if (sleep_states[i]) {
36 seq_printf(seq,"S%d ", i);
37 if (i == ACPI_STATE_S4 && acpi_gbl_FACS->S4bios_f)
38 seq_printf(seq, "S4bios ");
47 static int acpi_system_sleep_open_fs(struct inode *inode, struct file *file)
49 return single_open(file, acpi_system_sleep_seq_show, PDE(inode)->data);
53 acpi_system_write_sleep (
55 const char __user *buffer,
63 if (count > sizeof(str) - 1)
65 memset(str,0,sizeof(str));
66 if (copy_from_user(str, buffer, count))
69 /* Check for S4 bios request */
70 if (!strcmp(str,"4b")) {
71 error = acpi_suspend(4);
74 state = simple_strtoul(str, NULL, 0);
75 #ifdef CONFIG_SOFTWARE_SUSPEND
77 error = software_suspend();
81 error = acpi_suspend(state);
83 return error ? error : count;
85 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
87 static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
91 unsigned char rtc_control = 0;
94 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
96 spin_lock_irqsave(&rtc_lock, flags);
98 sec = CMOS_READ(RTC_SECONDS_ALARM);
99 min = CMOS_READ(RTC_MINUTES_ALARM);
100 hr = CMOS_READ(RTC_HOURS_ALARM);
101 rtc_control = CMOS_READ(RTC_CONTROL);
103 /* If we ever get an FACP with proper values... */
104 if (acpi_gbl_FADT->day_alrm)
105 /* ACPI spec: only low 6 its should be cared */
106 day = CMOS_READ(acpi_gbl_FADT->day_alrm) & 0x3F;
108 day = CMOS_READ(RTC_DAY_OF_MONTH);
109 if (acpi_gbl_FADT->mon_alrm)
110 mo = CMOS_READ(acpi_gbl_FADT->mon_alrm);
112 mo = CMOS_READ(RTC_MONTH);
113 if (acpi_gbl_FADT->century)
114 yr = CMOS_READ(acpi_gbl_FADT->century) * 100 + CMOS_READ(RTC_YEAR);
116 yr = CMOS_READ(RTC_YEAR);
118 spin_unlock_irqrestore(&rtc_lock, flags);
120 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
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
151 seq_printf(seq,"%4.4u-", yr);
152 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
153 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
154 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
155 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
156 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
161 static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
163 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
173 char *string_end = NULL;
174 int result = -EINVAL;
177 * Try to find delimeter, only to insert null. The end of the
178 * 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 acpi_system_write_alarm (
200 const char __user *buffer,
205 char alarm_string[30] = {'\0'};
206 char *p = alarm_string;
207 u32 sec, min, hr, day, mo, yr;
209 unsigned char rtc_control = 0;
211 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
213 if (count > sizeof(alarm_string) - 1)
214 return_VALUE(-EINVAL);
216 if (copy_from_user(alarm_string, buffer, count))
217 return_VALUE(-EFAULT);
219 alarm_string[count] = '\0';
221 /* check for time adjustment */
222 if (alarm_string[0] == '+') {
227 if ((result = get_date_field(&p, &yr)))
229 if ((result = get_date_field(&p, &mo)))
231 if ((result = get_date_field(&p, &day)))
233 if ((result = get_date_field(&p, &hr)))
235 if ((result = get_date_field(&p, &min)))
237 if ((result = get_date_field(&p, &sec)))
261 spin_lock_irq(&rtc_lock);
263 rtc_control = CMOS_READ(RTC_CONTROL);
264 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
274 yr += CMOS_READ(RTC_YEAR);
275 mo += CMOS_READ(RTC_MONTH);
276 day += CMOS_READ(RTC_DAY_OF_MONTH);
277 hr += CMOS_READ(RTC_HOURS);
278 min += CMOS_READ(RTC_MINUTES);
279 sec += CMOS_READ(RTC_SECONDS);
282 spin_unlock_irq(&rtc_lock);
284 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
313 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
322 spin_lock_irq(&rtc_lock);
324 * Disable alarm interrupt before setting alarm timer or else
325 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
327 rtc_control &= ~RTC_AIE;
328 CMOS_WRITE(rtc_control, RTC_CONTROL);
329 CMOS_READ(RTC_INTR_FLAGS);
331 /* write the fields the rtc knows about */
332 CMOS_WRITE(hr, RTC_HOURS_ALARM);
333 CMOS_WRITE(min, RTC_MINUTES_ALARM);
334 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
337 * If the system supports an enhanced alarm it will have non-zero
338 * offsets into the CMOS RAM here -- which for some reason are pointing
339 * to the RTC area of memory.
341 if (acpi_gbl_FADT->day_alrm)
342 CMOS_WRITE(day, acpi_gbl_FADT->day_alrm);
343 if (acpi_gbl_FADT->mon_alrm)
344 CMOS_WRITE(mo, acpi_gbl_FADT->mon_alrm);
345 if (acpi_gbl_FADT->century)
346 CMOS_WRITE(yr/100, acpi_gbl_FADT->century);
347 /* enable the rtc alarm interrupt */
348 rtc_control |= RTC_AIE;
349 CMOS_WRITE(rtc_control, RTC_CONTROL);
350 CMOS_READ(RTC_INTR_FLAGS);
352 spin_unlock_irq(&rtc_lock);
354 acpi_clear_event(ACPI_EVENT_RTC);
355 acpi_enable_event(ACPI_EVENT_RTC, 0);
361 return_VALUE(result ? result : count);
364 extern struct list_head acpi_wakeup_device_list;
365 extern spinlock_t acpi_device_lock;
368 acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
370 struct list_head * node, * next;
372 seq_printf(seq, "Device Sleep state Status\n");
374 spin_lock(&acpi_device_lock);
375 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
376 struct acpi_device * dev = container_of(node, struct acpi_device, wakeup_list);
378 if (!dev->wakeup.flags.valid)
380 spin_unlock(&acpi_device_lock);
381 if (dev->wakeup.flags.run_wake)
382 seq_printf(seq, "%4s %4d %8s\n",
383 dev->pnp.bus_id, (u32) dev->wakeup.sleep_state,
384 dev->wakeup.state.enabled ? "*enabled" : "*disabled");
386 seq_printf(seq, "%4s %4d %8s\n",
387 dev->pnp.bus_id, (u32) dev->wakeup.sleep_state,
388 dev->wakeup.state.enabled ? "enabled" : "disabled");
389 spin_lock(&acpi_device_lock);
391 spin_unlock(&acpi_device_lock);
396 acpi_system_write_wakeup_device (
398 const char __user *buffer,
402 struct list_head * node, * next;
406 struct acpi_device *found_dev = NULL;
408 if (len > 4) len = 4;
410 if (copy_from_user(strbuf, buffer, len))
413 sscanf(strbuf, "%s", str);
415 spin_lock(&acpi_device_lock);
416 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
417 struct acpi_device * dev = container_of(node, struct acpi_device, wakeup_list);
418 if (!dev->wakeup.flags.valid)
421 if (!strncmp(dev->pnp.bus_id, str, 4)) {
422 dev->wakeup.state.enabled = dev->wakeup.state.enabled ? 0:1;
428 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
429 struct acpi_device * dev = container_of(node,
430 struct acpi_device, wakeup_list);
432 if ((dev != found_dev) &&
433 (dev->wakeup.gpe_number == found_dev->wakeup.gpe_number) &&
434 (dev->wakeup.gpe_device == found_dev->wakeup.gpe_device)) {
435 printk(KERN_WARNING "ACPI: '%s' and '%s' have the same GPE, "
436 "can't disable/enable one seperately\n",
437 dev->pnp.bus_id, found_dev->pnp.bus_id);
438 dev->wakeup.state.enabled = found_dev->wakeup.state.enabled;
442 spin_unlock(&acpi_device_lock);
447 acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
449 return single_open(file, acpi_system_wakeup_device_seq_show, PDE(inode)->data);
452 static struct file_operations acpi_system_wakeup_device_fops = {
453 .open = acpi_system_wakeup_device_open_fs,
455 .write = acpi_system_write_wakeup_device,
457 .release = single_release,
460 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
461 static struct file_operations acpi_system_sleep_fops = {
462 .open = acpi_system_sleep_open_fs,
464 .write = acpi_system_write_sleep,
466 .release = single_release,
468 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
470 static struct file_operations acpi_system_alarm_fops = {
471 .open = acpi_system_alarm_open_fs,
473 .write = acpi_system_write_alarm,
475 .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;
487 static int acpi_sleep_proc_init(void)
489 struct proc_dir_entry *entry = NULL;
494 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
496 entry = create_proc_entry(ACPI_SYSTEM_FILE_SLEEP,
497 S_IFREG|S_IRUGO|S_IWUSR, acpi_root_dir);
499 entry->proc_fops = &acpi_system_sleep_fops;
503 entry = create_proc_entry(ACPI_SYSTEM_FILE_ALARM,
504 S_IFREG|S_IRUGO|S_IWUSR, acpi_root_dir);
506 entry->proc_fops = &acpi_system_alarm_fops;
508 /* 'wakeup device' [R/W]*/
509 entry = create_proc_entry(ACPI_SYSTEM_FILE_WAKEUP_DEVICE,
510 S_IFREG|S_IRUGO|S_IWUSR, acpi_root_dir);
512 entry->proc_fops = &acpi_system_wakeup_device_fops;
514 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
518 late_initcall(acpi_sleep_proc_init);