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
);
28 if (i
== ACPI_STATE_S4
&& acpi_gbl_FACS
->S4bios_f
)
29 seq_printf(seq
, "S4bios ");
38 static int acpi_system_sleep_open_fs(struct inode
*inode
, struct file
*file
)
40 return single_open(file
, acpi_system_sleep_seq_show
, PDE(inode
)->data
);
44 acpi_system_write_sleep(struct file
*file
,
45 const char __user
* buffer
, size_t count
, loff_t
* ppos
)
51 if (count
> sizeof(str
) - 1)
53 memset(str
, 0, sizeof(str
));
54 if (copy_from_user(str
, buffer
, count
))
57 /* Check for S4 bios request */
58 if (!strcmp(str
, "4b")) {
59 error
= acpi_suspend(4);
62 state
= simple_strtoul(str
, NULL
, 0);
63 #ifdef CONFIG_SOFTWARE_SUSPEND
65 error
= software_suspend();
69 error
= acpi_suspend(state
);
71 return error
? error
: count
;
73 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
75 static int acpi_system_alarm_seq_show(struct seq_file
*seq
, void *offset
)
79 unsigned char rtc_control
= 0;
82 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
84 spin_lock_irqsave(&rtc_lock
, flags
);
86 sec
= CMOS_READ(RTC_SECONDS_ALARM
);
87 min
= CMOS_READ(RTC_MINUTES_ALARM
);
88 hr
= CMOS_READ(RTC_HOURS_ALARM
);
89 rtc_control
= CMOS_READ(RTC_CONTROL
);
91 /* If we ever get an FACP with proper values... */
92 if (acpi_gbl_FADT
->day_alrm
)
93 /* ACPI spec: only low 6 its should be cared */
94 day
= CMOS_READ(acpi_gbl_FADT
->day_alrm
) & 0x3F;
96 day
= CMOS_READ(RTC_DAY_OF_MONTH
);
97 if (acpi_gbl_FADT
->mon_alrm
)
98 mo
= CMOS_READ(acpi_gbl_FADT
->mon_alrm
);
100 mo
= CMOS_READ(RTC_MONTH
);
101 if (acpi_gbl_FADT
->century
)
102 yr
= CMOS_READ(acpi_gbl_FADT
->century
) * 100 +
105 yr
= CMOS_READ(RTC_YEAR
);
107 spin_unlock_irqrestore(&rtc_lock
, flags
);
109 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
118 /* we're trusting the FADT (see above) */
119 if (!acpi_gbl_FADT
->century
)
120 /* If we're not trusting the FADT, we should at least make it
121 * right for _this_ century... ehm, what is _this_ century?
124 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
125 * which we can trust to determine the century correctly. Atom
126 * watch driver would be nice, too...
128 * if that has not happened, change for first release in 2050:
132 * yr += 2000; // current line of code
134 * 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_alrm
)
323 CMOS_WRITE(day
, acpi_gbl_FADT
->day_alrm
);
324 if (acpi_gbl_FADT
->mon_alrm
)
325 CMOS_WRITE(mo
, acpi_gbl_FADT
->mon_alrm
);
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 Sleep state Status\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
);
360 if (!dev
->wakeup
.flags
.valid
)
362 spin_unlock(&acpi_device_lock
);
363 seq_printf(seq
, "%4s %4d %s%8s\n",
365 (u32
) dev
->wakeup
.sleep_state
,
366 dev
->wakeup
.flags
.run_wake
? "*" : "",
367 dev
->wakeup
.state
.enabled
? "enabled" : "disabled");
368 spin_lock(&acpi_device_lock
);
370 spin_unlock(&acpi_device_lock
);
375 acpi_system_write_wakeup_device(struct file
*file
,
376 const char __user
* buffer
,
377 size_t count
, loff_t
* ppos
)
379 struct list_head
*node
, *next
;
383 struct acpi_device
*found_dev
= NULL
;
388 if (copy_from_user(strbuf
, buffer
, len
))
391 sscanf(strbuf
, "%s", str
);
393 spin_lock(&acpi_device_lock
);
394 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
395 struct acpi_device
*dev
=
396 container_of(node
, struct acpi_device
, wakeup_list
);
397 if (!dev
->wakeup
.flags
.valid
)
400 if (!strncmp(dev
->pnp
.bus_id
, str
, 4)) {
401 dev
->wakeup
.state
.enabled
=
402 dev
->wakeup
.state
.enabled
? 0 : 1;
408 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
409 struct acpi_device
*dev
= container_of(node
,
414 if ((dev
!= found_dev
) &&
415 (dev
->wakeup
.gpe_number
==
416 found_dev
->wakeup
.gpe_number
)
417 && (dev
->wakeup
.gpe_device
==
418 found_dev
->wakeup
.gpe_device
)) {
420 "ACPI: '%s' and '%s' have the same GPE, "
421 "can't disable/enable one seperately\n",
422 dev
->pnp
.bus_id
, found_dev
->pnp
.bus_id
);
423 dev
->wakeup
.state
.enabled
=
424 found_dev
->wakeup
.state
.enabled
;
428 spin_unlock(&acpi_device_lock
);
433 acpi_system_wakeup_device_open_fs(struct inode
*inode
, struct file
*file
)
435 return single_open(file
, acpi_system_wakeup_device_seq_show
,
439 static struct file_operations acpi_system_wakeup_device_fops
= {
440 .open
= acpi_system_wakeup_device_open_fs
,
442 .write
= acpi_system_write_wakeup_device
,
444 .release
= single_release
,
447 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
448 static struct file_operations acpi_system_sleep_fops
= {
449 .open
= acpi_system_sleep_open_fs
,
451 .write
= acpi_system_write_sleep
,
453 .release
= single_release
,
455 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
457 static struct file_operations acpi_system_alarm_fops
= {
458 .open
= acpi_system_alarm_open_fs
,
460 .write
= acpi_system_write_alarm
,
462 .release
= single_release
,
465 static u32
rtc_handler(void *context
)
467 acpi_clear_event(ACPI_EVENT_RTC
);
468 acpi_disable_event(ACPI_EVENT_RTC
, 0);
470 return ACPI_INTERRUPT_HANDLED
;
473 static int acpi_sleep_proc_init(void)
475 struct proc_dir_entry
*entry
= NULL
;
480 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
483 create_proc_entry("sleep", S_IFREG
| S_IRUGO
| S_IWUSR
,
486 entry
->proc_fops
= &acpi_system_sleep_fops
;
491 create_proc_entry("alarm", S_IFREG
| S_IRUGO
| S_IWUSR
,
494 entry
->proc_fops
= &acpi_system_alarm_fops
;
496 /* 'wakeup device' [R/W] */
498 create_proc_entry("wakeup", S_IFREG
| S_IRUGO
| S_IWUSR
,
501 entry
->proc_fops
= &acpi_system_wakeup_device_fops
;
503 acpi_install_fixed_event_handler(ACPI_EVENT_RTC
, rtc_handler
, NULL
);
507 late_initcall(acpi_sleep_proc_init
);