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
67 error
= acpi_suspend(state
);
69 return error
? error
: count
;
71 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
73 #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
74 /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
76 #define HAVE_ACPI_LEGACY_ALARM
79 #ifdef HAVE_ACPI_LEGACY_ALARM
81 static int acpi_system_alarm_seq_show(struct seq_file
*seq
, void *offset
)
84 u32 day
, mo
, yr
, cent
= 0;
85 unsigned char rtc_control
= 0;
88 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
90 spin_lock_irqsave(&rtc_lock
, flags
);
92 sec
= CMOS_READ(RTC_SECONDS_ALARM
);
93 min
= CMOS_READ(RTC_MINUTES_ALARM
);
94 hr
= CMOS_READ(RTC_HOURS_ALARM
);
95 rtc_control
= CMOS_READ(RTC_CONTROL
);
97 /* If we ever get an FACP with proper values... */
98 if (acpi_gbl_FADT
.day_alarm
)
99 /* ACPI spec: only low 6 its should be cared */
100 day
= CMOS_READ(acpi_gbl_FADT
.day_alarm
) & 0x3F;
102 day
= CMOS_READ(RTC_DAY_OF_MONTH
);
103 if (acpi_gbl_FADT
.month_alarm
)
104 mo
= CMOS_READ(acpi_gbl_FADT
.month_alarm
);
106 mo
= CMOS_READ(RTC_MONTH
);
107 if (acpi_gbl_FADT
.century
)
108 cent
= CMOS_READ(acpi_gbl_FADT
.century
);
110 yr
= CMOS_READ(RTC_YEAR
);
112 spin_unlock_irqrestore(&rtc_lock
, flags
);
114 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
124 /* we're trusting the FADT (see above) */
125 if (!acpi_gbl_FADT
.century
)
126 /* If we're not trusting the FADT, we should at least make it
127 * right for _this_ century... ehm, what is _this_ century?
130 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
131 * which we can trust to determine the century correctly. Atom
132 * watch driver would be nice, too...
134 * if that has not happened, change for first release in 2050:
138 * yr += 2000; // current line of code
140 * if that has not happened either, please do on 2099/12/31:23:59:59
148 seq_printf(seq
, "%4.4u-", yr
);
149 (mo
> 12) ? seq_puts(seq
, "**-") : seq_printf(seq
, "%2.2u-", mo
);
150 (day
> 31) ? seq_puts(seq
, "** ") : seq_printf(seq
, "%2.2u ", day
);
151 (hr
> 23) ? seq_puts(seq
, "**:") : seq_printf(seq
, "%2.2u:", hr
);
152 (min
> 59) ? seq_puts(seq
, "**:") : seq_printf(seq
, "%2.2u:", min
);
153 (sec
> 59) ? seq_puts(seq
, "**\n") : seq_printf(seq
, "%2.2u\n", sec
);
158 static int acpi_system_alarm_open_fs(struct inode
*inode
, struct file
*file
)
160 return single_open(file
, acpi_system_alarm_seq_show
, PDE(inode
)->data
);
163 static int get_date_field(char **p
, u32
* value
)
166 char *string_end
= NULL
;
167 int result
= -EINVAL
;
170 * Try to find delimeter, only to insert null. The end of the
171 * string won't have one, but is still valid.
173 next
= strpbrk(*p
, "- :");
177 *value
= simple_strtoul(*p
, &string_end
, 10);
179 /* Signal success if we got a good digit */
180 if (string_end
!= *p
)
190 acpi_system_write_alarm(struct file
*file
,
191 const char __user
* buffer
, size_t count
, loff_t
* ppos
)
194 char alarm_string
[30] = { '\0' };
195 char *p
= alarm_string
;
196 u32 sec
, min
, hr
, day
, mo
, yr
;
198 unsigned char rtc_control
= 0;
200 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
202 if (count
> sizeof(alarm_string
) - 1)
203 return_VALUE(-EINVAL
);
205 if (copy_from_user(alarm_string
, buffer
, count
))
206 return_VALUE(-EFAULT
);
208 alarm_string
[count
] = '\0';
210 /* check for time adjustment */
211 if (alarm_string
[0] == '+') {
216 if ((result
= get_date_field(&p
, &yr
)))
218 if ((result
= get_date_field(&p
, &mo
)))
220 if ((result
= get_date_field(&p
, &day
)))
222 if ((result
= get_date_field(&p
, &hr
)))
224 if ((result
= get_date_field(&p
, &min
)))
226 if ((result
= get_date_field(&p
, &sec
)))
250 spin_lock_irq(&rtc_lock
);
252 rtc_control
= CMOS_READ(RTC_CONTROL
);
253 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
263 yr
+= CMOS_READ(RTC_YEAR
);
264 mo
+= CMOS_READ(RTC_MONTH
);
265 day
+= CMOS_READ(RTC_DAY_OF_MONTH
);
266 hr
+= CMOS_READ(RTC_HOURS
);
267 min
+= CMOS_READ(RTC_MINUTES
);
268 sec
+= CMOS_READ(RTC_SECONDS
);
271 spin_unlock_irq(&rtc_lock
);
273 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
302 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
311 spin_lock_irq(&rtc_lock
);
313 * Disable alarm interrupt before setting alarm timer or else
314 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
316 rtc_control
&= ~RTC_AIE
;
317 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
318 CMOS_READ(RTC_INTR_FLAGS
);
320 /* write the fields the rtc knows about */
321 CMOS_WRITE(hr
, RTC_HOURS_ALARM
);
322 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
323 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
326 * If the system supports an enhanced alarm it will have non-zero
327 * offsets into the CMOS RAM here -- which for some reason are pointing
328 * to the RTC area of memory.
330 if (acpi_gbl_FADT
.day_alarm
)
331 CMOS_WRITE(day
, acpi_gbl_FADT
.day_alarm
);
332 if (acpi_gbl_FADT
.month_alarm
)
333 CMOS_WRITE(mo
, acpi_gbl_FADT
.month_alarm
);
334 if (acpi_gbl_FADT
.century
)
335 CMOS_WRITE(yr
/ 100, acpi_gbl_FADT
.century
);
336 /* enable the rtc alarm interrupt */
337 rtc_control
|= RTC_AIE
;
338 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
339 CMOS_READ(RTC_INTR_FLAGS
);
341 spin_unlock_irq(&rtc_lock
);
343 acpi_clear_event(ACPI_EVENT_RTC
);
344 acpi_enable_event(ACPI_EVENT_RTC
, 0);
350 return_VALUE(result
? result
: count
);
352 #endif /* HAVE_ACPI_LEGACY_ALARM */
354 extern struct list_head acpi_wakeup_device_list
;
355 extern spinlock_t acpi_device_lock
;
358 acpi_system_wakeup_device_seq_show(struct seq_file
*seq
, void *offset
)
360 struct list_head
*node
, *next
;
362 seq_printf(seq
, "Device\tS-state\t Status Sysfs node\n");
364 spin_lock(&acpi_device_lock
);
365 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
366 struct acpi_device
*dev
=
367 container_of(node
, struct acpi_device
, wakeup_list
);
370 if (!dev
->wakeup
.flags
.valid
)
372 spin_unlock(&acpi_device_lock
);
374 ldev
= acpi_get_physical_device(dev
->handle
);
375 seq_printf(seq
, "%s\t S%d\t%c%-8s ",
377 (u32
) dev
->wakeup
.sleep_state
,
378 dev
->wakeup
.flags
.run_wake
? '*' : ' ',
379 dev
->wakeup
.state
.enabled
? "enabled" : "disabled");
381 seq_printf(seq
, "%s:%s",
382 ldev
->bus
? ldev
->bus
->name
: "no-bus",
384 seq_printf(seq
, "\n");
387 spin_lock(&acpi_device_lock
);
389 spin_unlock(&acpi_device_lock
);
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 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
428 struct acpi_device
*dev
= container_of(node
,
433 if ((dev
!= found_dev
) &&
434 (dev
->wakeup
.gpe_number
==
435 found_dev
->wakeup
.gpe_number
)
436 && (dev
->wakeup
.gpe_device
==
437 found_dev
->wakeup
.gpe_device
)) {
439 "ACPI: '%s' and '%s' have the same GPE, "
440 "can't disable/enable one seperately\n",
441 dev
->pnp
.bus_id
, found_dev
->pnp
.bus_id
);
442 dev
->wakeup
.state
.enabled
=
443 found_dev
->wakeup
.state
.enabled
;
447 spin_unlock(&acpi_device_lock
);
452 acpi_system_wakeup_device_open_fs(struct inode
*inode
, struct file
*file
)
454 return single_open(file
, acpi_system_wakeup_device_seq_show
,
458 static const struct file_operations acpi_system_wakeup_device_fops
= {
459 .open
= acpi_system_wakeup_device_open_fs
,
461 .write
= acpi_system_write_wakeup_device
,
463 .release
= single_release
,
466 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
467 static const struct file_operations acpi_system_sleep_fops
= {
468 .open
= acpi_system_sleep_open_fs
,
470 .write
= acpi_system_write_sleep
,
472 .release
= single_release
,
474 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
476 #ifdef HAVE_ACPI_LEGACY_ALARM
477 static const struct file_operations acpi_system_alarm_fops
= {
478 .open
= acpi_system_alarm_open_fs
,
480 .write
= acpi_system_write_alarm
,
482 .release
= single_release
,
485 static u32
rtc_handler(void *context
)
487 acpi_clear_event(ACPI_EVENT_RTC
);
488 acpi_disable_event(ACPI_EVENT_RTC
, 0);
490 return ACPI_INTERRUPT_HANDLED
;
492 #endif /* HAVE_ACPI_LEGACY_ALARM */
494 static int __init
acpi_sleep_proc_init(void)
496 struct proc_dir_entry
*entry
= NULL
;
501 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
504 create_proc_entry("sleep", S_IFREG
| S_IRUGO
| S_IWUSR
,
507 entry
->proc_fops
= &acpi_system_sleep_fops
;
510 #ifdef HAVE_ACPI_LEGACY_ALARM
513 create_proc_entry("alarm", S_IFREG
| S_IRUGO
| S_IWUSR
,
516 entry
->proc_fops
= &acpi_system_alarm_fops
;
518 acpi_install_fixed_event_handler(ACPI_EVENT_RTC
, rtc_handler
, NULL
);
519 #endif /* HAVE_ACPI_LEGACY_ALARM */
521 /* 'wakeup device' [R/W] */
523 create_proc_entry("wakeup", S_IFREG
| S_IRUGO
| S_IWUSR
,
526 entry
->proc_fops
= &acpi_system_wakeup_device_fops
;
531 late_initcall(acpi_sleep_proc_init
);