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
= software_suspend();
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
)
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_alrm
)
91 /* ACPI spec: only low 6 its should be cared */
92 day
= CMOS_READ(acpi_gbl_FADT
->day_alrm
) & 0x3F;
94 day
= CMOS_READ(RTC_DAY_OF_MONTH
);
95 if (acpi_gbl_FADT
->mon_alrm
)
96 mo
= CMOS_READ(acpi_gbl_FADT
->mon_alrm
);
98 mo
= CMOS_READ(RTC_MONTH
);
99 if (acpi_gbl_FADT
->century
)
100 yr
= CMOS_READ(acpi_gbl_FADT
->century
) * 100 +
103 yr
= CMOS_READ(RTC_YEAR
);
105 spin_unlock_irqrestore(&rtc_lock
, flags
);
107 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
138 seq_printf(seq
, "%4.4u-", yr
);
139 (mo
> 12) ? seq_puts(seq
, "**-") : seq_printf(seq
, "%2.2u-", mo
);
140 (day
> 31) ? seq_puts(seq
, "** ") : seq_printf(seq
, "%2.2u ", day
);
141 (hr
> 23) ? seq_puts(seq
, "**:") : seq_printf(seq
, "%2.2u:", hr
);
142 (min
> 59) ? seq_puts(seq
, "**:") : seq_printf(seq
, "%2.2u:", min
);
143 (sec
> 59) ? seq_puts(seq
, "**\n") : seq_printf(seq
, "%2.2u\n", sec
);
148 static int acpi_system_alarm_open_fs(struct inode
*inode
, struct file
*file
)
150 return single_open(file
, acpi_system_alarm_seq_show
, PDE(inode
)->data
);
153 static int get_date_field(char **p
, u32
* value
)
156 char *string_end
= NULL
;
157 int result
= -EINVAL
;
160 * Try to find delimeter, only to insert null. The end of the
161 * string won't have one, but is still valid.
163 next
= strpbrk(*p
, "- :");
167 *value
= simple_strtoul(*p
, &string_end
, 10);
169 /* Signal success if we got a good digit */
170 if (string_end
!= *p
)
180 acpi_system_write_alarm(struct file
*file
,
181 const char __user
* buffer
, size_t count
, loff_t
* ppos
)
184 char alarm_string
[30] = { '\0' };
185 char *p
= alarm_string
;
186 u32 sec
, min
, hr
, day
, mo
, yr
;
188 unsigned char rtc_control
= 0;
190 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
192 if (count
> sizeof(alarm_string
) - 1)
193 return_VALUE(-EINVAL
);
195 if (copy_from_user(alarm_string
, buffer
, count
))
196 return_VALUE(-EFAULT
);
198 alarm_string
[count
] = '\0';
200 /* check for time adjustment */
201 if (alarm_string
[0] == '+') {
206 if ((result
= get_date_field(&p
, &yr
)))
208 if ((result
= get_date_field(&p
, &mo
)))
210 if ((result
= get_date_field(&p
, &day
)))
212 if ((result
= get_date_field(&p
, &hr
)))
214 if ((result
= get_date_field(&p
, &min
)))
216 if ((result
= get_date_field(&p
, &sec
)))
240 spin_lock_irq(&rtc_lock
);
242 rtc_control
= CMOS_READ(RTC_CONTROL
);
243 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
253 yr
+= CMOS_READ(RTC_YEAR
);
254 mo
+= CMOS_READ(RTC_MONTH
);
255 day
+= CMOS_READ(RTC_DAY_OF_MONTH
);
256 hr
+= CMOS_READ(RTC_HOURS
);
257 min
+= CMOS_READ(RTC_MINUTES
);
258 sec
+= CMOS_READ(RTC_SECONDS
);
261 spin_unlock_irq(&rtc_lock
);
263 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
292 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
301 spin_lock_irq(&rtc_lock
);
303 * Disable alarm interrupt before setting alarm timer or else
304 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
306 rtc_control
&= ~RTC_AIE
;
307 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
308 CMOS_READ(RTC_INTR_FLAGS
);
310 /* write the fields the rtc knows about */
311 CMOS_WRITE(hr
, RTC_HOURS_ALARM
);
312 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
313 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
316 * If the system supports an enhanced alarm it will have non-zero
317 * offsets into the CMOS RAM here -- which for some reason are pointing
318 * to the RTC area of memory.
320 if (acpi_gbl_FADT
->day_alrm
)
321 CMOS_WRITE(day
, acpi_gbl_FADT
->day_alrm
);
322 if (acpi_gbl_FADT
->mon_alrm
)
323 CMOS_WRITE(mo
, acpi_gbl_FADT
->mon_alrm
);
324 if (acpi_gbl_FADT
->century
)
325 CMOS_WRITE(yr
/ 100, acpi_gbl_FADT
->century
);
326 /* enable the rtc alarm interrupt */
327 rtc_control
|= RTC_AIE
;
328 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
329 CMOS_READ(RTC_INTR_FLAGS
);
331 spin_unlock_irq(&rtc_lock
);
333 acpi_clear_event(ACPI_EVENT_RTC
);
334 acpi_enable_event(ACPI_EVENT_RTC
, 0);
340 return_VALUE(result
? result
: count
);
343 extern struct list_head acpi_wakeup_device_list
;
344 extern spinlock_t acpi_device_lock
;
347 acpi_system_wakeup_device_seq_show(struct seq_file
*seq
, void *offset
)
349 struct list_head
*node
, *next
;
351 seq_printf(seq
, "Device Sleep state Status\n");
353 spin_lock(&acpi_device_lock
);
354 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
355 struct acpi_device
*dev
=
356 container_of(node
, struct acpi_device
, wakeup_list
);
358 if (!dev
->wakeup
.flags
.valid
)
360 spin_unlock(&acpi_device_lock
);
361 seq_printf(seq
, "%4s %4d %s%8s\n",
363 (u32
) dev
->wakeup
.sleep_state
,
364 dev
->wakeup
.flags
.run_wake
? "*" : "",
365 dev
->wakeup
.state
.enabled
? "enabled" : "disabled");
366 spin_lock(&acpi_device_lock
);
368 spin_unlock(&acpi_device_lock
);
373 acpi_system_write_wakeup_device(struct file
*file
,
374 const char __user
* buffer
,
375 size_t count
, loff_t
* ppos
)
377 struct list_head
*node
, *next
;
381 struct acpi_device
*found_dev
= NULL
;
386 if (copy_from_user(strbuf
, buffer
, len
))
389 sscanf(strbuf
, "%s", str
);
391 spin_lock(&acpi_device_lock
);
392 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
393 struct acpi_device
*dev
=
394 container_of(node
, struct acpi_device
, wakeup_list
);
395 if (!dev
->wakeup
.flags
.valid
)
398 if (!strncmp(dev
->pnp
.bus_id
, str
, 4)) {
399 dev
->wakeup
.state
.enabled
=
400 dev
->wakeup
.state
.enabled
? 0 : 1;
406 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
407 struct acpi_device
*dev
= container_of(node
,
412 if ((dev
!= found_dev
) &&
413 (dev
->wakeup
.gpe_number
==
414 found_dev
->wakeup
.gpe_number
)
415 && (dev
->wakeup
.gpe_device
==
416 found_dev
->wakeup
.gpe_device
)) {
418 "ACPI: '%s' and '%s' have the same GPE, "
419 "can't disable/enable one seperately\n",
420 dev
->pnp
.bus_id
, found_dev
->pnp
.bus_id
);
421 dev
->wakeup
.state
.enabled
=
422 found_dev
->wakeup
.state
.enabled
;
426 spin_unlock(&acpi_device_lock
);
431 acpi_system_wakeup_device_open_fs(struct inode
*inode
, struct file
*file
)
433 return single_open(file
, acpi_system_wakeup_device_seq_show
,
437 static const struct file_operations acpi_system_wakeup_device_fops
= {
438 .open
= acpi_system_wakeup_device_open_fs
,
440 .write
= acpi_system_write_wakeup_device
,
442 .release
= single_release
,
445 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
446 static const struct file_operations acpi_system_sleep_fops
= {
447 .open
= acpi_system_sleep_open_fs
,
449 .write
= acpi_system_write_sleep
,
451 .release
= single_release
,
453 #endif /* CONFIG_ACPI_SLEEP_PROC_SLEEP */
455 static const struct file_operations acpi_system_alarm_fops
= {
456 .open
= acpi_system_alarm_open_fs
,
458 .write
= acpi_system_write_alarm
,
460 .release
= single_release
,
463 static u32
rtc_handler(void *context
)
465 acpi_clear_event(ACPI_EVENT_RTC
);
466 acpi_disable_event(ACPI_EVENT_RTC
, 0);
468 return ACPI_INTERRUPT_HANDLED
;
471 static int acpi_sleep_proc_init(void)
473 struct proc_dir_entry
*entry
= NULL
;
478 #ifdef CONFIG_ACPI_SLEEP_PROC_SLEEP
481 create_proc_entry("sleep", S_IFREG
| S_IRUGO
| S_IWUSR
,
484 entry
->proc_fops
= &acpi_system_sleep_fops
;
489 create_proc_entry("alarm", S_IFREG
| S_IRUGO
| S_IWUSR
,
492 entry
->proc_fops
= &acpi_system_alarm_fops
;
494 /* 'wakeup device' [R/W] */
496 create_proc_entry("wakeup", S_IFREG
| S_IRUGO
| S_IWUSR
,
499 entry
->proc_fops
= &acpi_system_wakeup_device_fops
;
501 acpi_install_fixed_event_handler(ACPI_EVENT_RTC
, rtc_handler
, NULL
);
505 late_initcall(acpi_sleep_proc_init
);