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
19 * this file provides support for:
24 ACPI_MODULE_NAME("sleep")
26 #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86)
27 /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
29 #define HAVE_ACPI_LEGACY_ALARM
32 #ifdef HAVE_ACPI_LEGACY_ALARM
34 static u32
cmos_bcd_read(int offset
, int rtc_control
);
36 static int acpi_system_alarm_seq_show(struct seq_file
*seq
, void *offset
)
39 u32 day
, mo
, yr
, cent
= 0;
41 unsigned char rtc_control
= 0;
44 spin_lock_irqsave(&rtc_lock
, flags
);
46 rtc_control
= CMOS_READ(RTC_CONTROL
);
47 sec
= cmos_bcd_read(RTC_SECONDS_ALARM
, rtc_control
);
48 min
= cmos_bcd_read(RTC_MINUTES_ALARM
, rtc_control
);
49 hr
= cmos_bcd_read(RTC_HOURS_ALARM
, rtc_control
);
51 /* If we ever get an FACP with proper values... */
52 if (acpi_gbl_FADT
.day_alarm
) {
53 /* ACPI spec: only low 6 its should be cared */
54 day
= CMOS_READ(acpi_gbl_FADT
.day_alarm
) & 0x3F;
55 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
58 day
= cmos_bcd_read(RTC_DAY_OF_MONTH
, rtc_control
);
59 if (acpi_gbl_FADT
.month_alarm
)
60 mo
= cmos_bcd_read(acpi_gbl_FADT
.month_alarm
, rtc_control
);
62 mo
= cmos_bcd_read(RTC_MONTH
, rtc_control
);
63 today
= cmos_bcd_read(RTC_DAY_OF_MONTH
, rtc_control
);
65 if (acpi_gbl_FADT
.century
)
66 cent
= cmos_bcd_read(acpi_gbl_FADT
.century
, rtc_control
);
68 yr
= cmos_bcd_read(RTC_YEAR
, rtc_control
);
70 spin_unlock_irqrestore(&rtc_lock
, flags
);
72 /* we're trusting the FADT (see above) */
73 if (!acpi_gbl_FADT
.century
)
74 /* If we're not trusting the FADT, we should at least make it
75 * right for _this_ century... ehm, what is _this_ century?
78 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
79 * which we can trust to determine the century correctly. Atom
80 * watch driver would be nice, too...
82 * if that has not happened, change for first release in 2050:
86 * yr += 2000; // current line of code
88 * if that has not happened either, please do on 2099/12/31:23:59:59
97 * Show correct dates for alarms up to a month into the future.
98 * This solves issues for nearly all situations with the common
99 * 30-day alarm clocks in PC hardware.
110 seq_printf(seq
, "%4.4u-", yr
);
111 (mo
> 12) ? seq_puts(seq
, "**-") : seq_printf(seq
, "%2.2u-", mo
);
112 (day
> 31) ? seq_puts(seq
, "** ") : seq_printf(seq
, "%2.2u ", day
);
113 (hr
> 23) ? seq_puts(seq
, "**:") : seq_printf(seq
, "%2.2u:", hr
);
114 (min
> 59) ? seq_puts(seq
, "**:") : seq_printf(seq
, "%2.2u:", min
);
115 (sec
> 59) ? seq_puts(seq
, "**\n") : seq_printf(seq
, "%2.2u\n", sec
);
120 static int acpi_system_alarm_open_fs(struct inode
*inode
, struct file
*file
)
122 return single_open(file
, acpi_system_alarm_seq_show
, PDE(inode
)->data
);
125 static int get_date_field(char **p
, u32
* value
)
128 char *string_end
= NULL
;
129 int result
= -EINVAL
;
132 * Try to find delimeter, only to insert null. The end of the
133 * string won't have one, but is still valid.
138 next
= strpbrk(*p
, "- :");
142 *value
= simple_strtoul(*p
, &string_end
, 10);
144 /* Signal success if we got a good digit */
145 if (string_end
!= *p
)
156 /* Read a possibly BCD register, always return binary */
157 static u32
cmos_bcd_read(int offset
, int rtc_control
)
159 u32 val
= CMOS_READ(offset
);
160 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
165 /* Write binary value into possibly BCD register */
166 static void cmos_bcd_write(u32 val
, int offset
, int rtc_control
)
168 if (!(rtc_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
170 CMOS_WRITE(val
, offset
);
174 acpi_system_write_alarm(struct file
*file
,
175 const char __user
* buffer
, size_t count
, loff_t
* ppos
)
178 char alarm_string
[30] = { '\0' };
179 char *p
= alarm_string
;
180 u32 sec
, min
, hr
, day
, mo
, yr
;
182 unsigned char rtc_control
= 0;
184 if (count
> sizeof(alarm_string
) - 1)
187 if (copy_from_user(alarm_string
, buffer
, count
))
190 alarm_string
[count
] = '\0';
192 /* check for time adjustment */
193 if (alarm_string
[0] == '+') {
198 if ((result
= get_date_field(&p
, &yr
)))
200 if ((result
= get_date_field(&p
, &mo
)))
202 if ((result
= get_date_field(&p
, &day
)))
204 if ((result
= get_date_field(&p
, &hr
)))
206 if ((result
= get_date_field(&p
, &min
)))
208 if ((result
= get_date_field(&p
, &sec
)))
211 spin_lock_irq(&rtc_lock
);
213 rtc_control
= CMOS_READ(RTC_CONTROL
);
216 yr
+= cmos_bcd_read(RTC_YEAR
, rtc_control
);
217 mo
+= cmos_bcd_read(RTC_MONTH
, rtc_control
);
218 day
+= cmos_bcd_read(RTC_DAY_OF_MONTH
, rtc_control
);
219 hr
+= cmos_bcd_read(RTC_HOURS
, rtc_control
);
220 min
+= cmos_bcd_read(RTC_MINUTES
, rtc_control
);
221 sec
+= cmos_bcd_read(RTC_SECONDS
, rtc_control
);
224 spin_unlock_irq(&rtc_lock
);
247 spin_lock_irq(&rtc_lock
);
249 * Disable alarm interrupt before setting alarm timer or else
250 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
252 rtc_control
&= ~RTC_AIE
;
253 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
254 CMOS_READ(RTC_INTR_FLAGS
);
256 /* write the fields the rtc knows about */
257 cmos_bcd_write(hr
, RTC_HOURS_ALARM
, rtc_control
);
258 cmos_bcd_write(min
, RTC_MINUTES_ALARM
, rtc_control
);
259 cmos_bcd_write(sec
, RTC_SECONDS_ALARM
, rtc_control
);
262 * If the system supports an enhanced alarm it will have non-zero
263 * offsets into the CMOS RAM here -- which for some reason are pointing
264 * to the RTC area of memory.
266 if (acpi_gbl_FADT
.day_alarm
)
267 cmos_bcd_write(day
, acpi_gbl_FADT
.day_alarm
, rtc_control
);
268 if (acpi_gbl_FADT
.month_alarm
)
269 cmos_bcd_write(mo
, acpi_gbl_FADT
.month_alarm
, rtc_control
);
270 if (acpi_gbl_FADT
.century
) {
272 yr
+= cmos_bcd_read(acpi_gbl_FADT
.century
, rtc_control
) * 100;
273 cmos_bcd_write(yr
/ 100, acpi_gbl_FADT
.century
, rtc_control
);
275 /* enable the rtc alarm interrupt */
276 rtc_control
|= RTC_AIE
;
277 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
278 CMOS_READ(RTC_INTR_FLAGS
);
280 spin_unlock_irq(&rtc_lock
);
282 acpi_clear_event(ACPI_EVENT_RTC
);
283 acpi_enable_event(ACPI_EVENT_RTC
, 0);
289 return result
? result
: count
;
291 #endif /* HAVE_ACPI_LEGACY_ALARM */
294 acpi_system_wakeup_device_seq_show(struct seq_file
*seq
, void *offset
)
296 struct list_head
*node
, *next
;
298 seq_printf(seq
, "Device\tS-state\t Status Sysfs node\n");
300 mutex_lock(&acpi_device_lock
);
301 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
302 struct acpi_device
*dev
=
303 container_of(node
, struct acpi_device
, wakeup_list
);
306 if (!dev
->wakeup
.flags
.valid
)
309 ldev
= acpi_get_physical_device(dev
->handle
);
310 seq_printf(seq
, "%s\t S%d\t%c%-8s ",
312 (u32
) dev
->wakeup
.sleep_state
,
313 dev
->wakeup
.flags
.run_wake
? '*' : ' ',
314 dev
->wakeup
.state
.enabled
? "enabled" : "disabled");
316 seq_printf(seq
, "%s:%s",
317 ldev
->bus
? ldev
->bus
->name
: "no-bus",
319 seq_printf(seq
, "\n");
323 mutex_unlock(&acpi_device_lock
);
327 static void physical_device_enable_wakeup(struct acpi_device
*adev
)
329 struct device
*dev
= acpi_get_physical_device(adev
->handle
);
331 if (dev
&& device_can_wakeup(dev
))
332 device_set_wakeup_enable(dev
, adev
->wakeup
.state
.enabled
);
336 acpi_system_write_wakeup_device(struct file
*file
,
337 const char __user
* buffer
,
338 size_t count
, loff_t
* ppos
)
340 struct list_head
*node
, *next
;
343 unsigned int len
= count
;
344 struct acpi_device
*found_dev
= NULL
;
351 if (copy_from_user(strbuf
, buffer
, len
))
354 sscanf(strbuf
, "%s", str
);
356 mutex_lock(&acpi_device_lock
);
357 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
358 struct acpi_device
*dev
=
359 container_of(node
, struct acpi_device
, wakeup_list
);
360 if (!dev
->wakeup
.flags
.valid
)
363 if (!strncmp(dev
->pnp
.bus_id
, str
, 4)) {
364 dev
->wakeup
.state
.enabled
=
365 dev
->wakeup
.state
.enabled
? 0 : 1;
371 physical_device_enable_wakeup(found_dev
);
372 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
373 struct acpi_device
*dev
= container_of(node
,
378 if ((dev
!= found_dev
) &&
379 (dev
->wakeup
.gpe_number
==
380 found_dev
->wakeup
.gpe_number
)
381 && (dev
->wakeup
.gpe_device
==
382 found_dev
->wakeup
.gpe_device
)) {
384 "ACPI: '%s' and '%s' have the same GPE, "
385 "can't disable/enable one separately\n",
386 dev
->pnp
.bus_id
, found_dev
->pnp
.bus_id
);
387 dev
->wakeup
.state
.enabled
=
388 found_dev
->wakeup
.state
.enabled
;
389 physical_device_enable_wakeup(dev
);
393 mutex_unlock(&acpi_device_lock
);
398 acpi_system_wakeup_device_open_fs(struct inode
*inode
, struct file
*file
)
400 return single_open(file
, acpi_system_wakeup_device_seq_show
,
404 static const struct file_operations acpi_system_wakeup_device_fops
= {
405 .owner
= THIS_MODULE
,
406 .open
= acpi_system_wakeup_device_open_fs
,
408 .write
= acpi_system_write_wakeup_device
,
410 .release
= single_release
,
413 #ifdef HAVE_ACPI_LEGACY_ALARM
414 static const struct file_operations acpi_system_alarm_fops
= {
415 .owner
= THIS_MODULE
,
416 .open
= acpi_system_alarm_open_fs
,
418 .write
= acpi_system_write_alarm
,
420 .release
= single_release
,
423 static u32
rtc_handler(void *context
)
425 acpi_clear_event(ACPI_EVENT_RTC
);
426 acpi_disable_event(ACPI_EVENT_RTC
, 0);
428 return ACPI_INTERRUPT_HANDLED
;
430 #endif /* HAVE_ACPI_LEGACY_ALARM */
432 int __init
acpi_sleep_proc_init(void)
434 #ifdef HAVE_ACPI_LEGACY_ALARM
436 proc_create("alarm", S_IFREG
| S_IRUGO
| S_IWUSR
,
437 acpi_root_dir
, &acpi_system_alarm_fops
);
439 acpi_install_fixed_event_handler(ACPI_EVENT_RTC
, rtc_handler
, NULL
);
441 * Disable the RTC event after installing RTC handler.
442 * Only when RTC alarm is set will it be enabled.
444 acpi_clear_event(ACPI_EVENT_RTC
);
445 acpi_disable_event(ACPI_EVENT_RTC
, 0);
446 #endif /* HAVE_ACPI_LEGACY_ALARM */
448 /* 'wakeup device' [R/W] */
449 proc_create("wakeup", S_IFREG
| S_IRUGO
| S_IWUSR
,
450 acpi_root_dir
, &acpi_system_wakeup_device_fops
);