Merge git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / sleep / proc.c
CommitLineData
1da177e4
LT
1#include <linux/proc_fs.h>
2#include <linux/seq_file.h>
3#include <linux/suspend.h>
4#include <linux/bcd.h>
5#include <asm/uaccess.h>
6
7#include <acpi/acpi_bus.h>
8#include <acpi/acpi_drivers.h>
9
10#ifdef CONFIG_X86
11#include <linux/mc146818rtc.h>
12#endif
13
14#include "sleep.h"
15
1da177e4 16#define _COMPONENT ACPI_SYSTEM_COMPONENT
43532c8a
LB
17
18/*
19 * this file provides support for:
20 * /proc/acpi/sleep
21 * /proc/acpi/alarm
22 * /proc/acpi/wakeup
23 */
24
4be44fcd 25ACPI_MODULE_NAME("sleep")
134c2171 26#ifdef CONFIG_ACPI_PROCFS
1da177e4
LT
27static int acpi_system_sleep_seq_show(struct seq_file *seq, void *offset)
28{
4be44fcd 29 int i;
1da177e4
LT
30
31 ACPI_FUNCTION_TRACE("acpi_system_sleep_seq_show");
32
33 for (i = 0; i <= ACPI_STATE_S5; i++) {
34 if (sleep_states[i]) {
4be44fcd 35 seq_printf(seq, "S%d ", i);
1da177e4
LT
36 }
37 }
38
39 seq_puts(seq, "\n");
40
41 return 0;
42}
43
44static int acpi_system_sleep_open_fs(struct inode *inode, struct file *file)
45{
46 return single_open(file, acpi_system_sleep_seq_show, PDE(inode)->data);
47}
48
49static ssize_t
4be44fcd
LB
50acpi_system_write_sleep(struct file *file,
51 const char __user * buffer, size_t count, loff_t * ppos)
1da177e4 52{
4be44fcd
LB
53 char str[12];
54 u32 state = 0;
55 int error = 0;
1da177e4
LT
56
57 if (count > sizeof(str) - 1)
58 goto Done;
4be44fcd 59 memset(str, 0, sizeof(str));
1da177e4
LT
60 if (copy_from_user(str, buffer, count))
61 return -EFAULT;
62
63 /* Check for S4 bios request */
4be44fcd 64 if (!strcmp(str, "4b")) {
1da177e4
LT
65 error = acpi_suspend(4);
66 goto Done;
67 }
68 state = simple_strtoul(str, NULL, 0);
b0cb1a19 69#ifdef CONFIG_HIBERNATION
1da177e4 70 if (state == 4) {
a3d25c27 71 error = hibernate();
1da177e4
LT
72 goto Done;
73 }
74#endif
75 error = acpi_suspend(state);
4be44fcd 76 Done:
1da177e4
LT
77 return error ? error : count;
78}
134c2171 79#endif /* CONFIG_ACPI_PROCFS */
1da177e4 80
673d5b43 81#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86)
f5f72b46
DB
82/* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
83#else
84#define HAVE_ACPI_LEGACY_ALARM
85#endif
86
87#ifdef HAVE_ACPI_LEGACY_ALARM
88
1da177e4
LT
89static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
90{
4be44fcd 91 u32 sec, min, hr;
ad71860a 92 u32 day, mo, yr, cent = 0;
4be44fcd
LB
93 unsigned char rtc_control = 0;
94 unsigned long flags;
1da177e4
LT
95
96 ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");
97
98 spin_lock_irqsave(&rtc_lock, flags);
99
100 sec = CMOS_READ(RTC_SECONDS_ALARM);
101 min = CMOS_READ(RTC_MINUTES_ALARM);
102 hr = CMOS_READ(RTC_HOURS_ALARM);
103 rtc_control = CMOS_READ(RTC_CONTROL);
104
105 /* If we ever get an FACP with proper values... */
ad71860a 106 if (acpi_gbl_FADT.day_alarm)
1da177e4 107 /* ACPI spec: only low 6 its should be cared */
ad71860a 108 day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
1da177e4 109 else
4be44fcd 110 day = CMOS_READ(RTC_DAY_OF_MONTH);
ad71860a
AS
111 if (acpi_gbl_FADT.month_alarm)
112 mo = CMOS_READ(acpi_gbl_FADT.month_alarm);
1da177e4
LT
113 else
114 mo = CMOS_READ(RTC_MONTH);
ad71860a
AS
115 if (acpi_gbl_FADT.century)
116 cent = CMOS_READ(acpi_gbl_FADT.century);
117
118 yr = CMOS_READ(RTC_YEAR);
1da177e4
LT
119
120 spin_unlock_irqrestore(&rtc_lock, flags);
121
122 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
123 BCD_TO_BIN(sec);
124 BCD_TO_BIN(min);
125 BCD_TO_BIN(hr);
126 BCD_TO_BIN(day);
127 BCD_TO_BIN(mo);
128 BCD_TO_BIN(yr);
ad71860a 129 BCD_TO_BIN(cent);
1da177e4
LT
130 }
131
4be44fcd 132 /* we're trusting the FADT (see above) */
ad71860a 133 if (!acpi_gbl_FADT.century)
4be44fcd
LB
134 /* If we're not trusting the FADT, we should at least make it
135 * right for _this_ century... ehm, what is _this_ century?
136 *
137 * TBD:
138 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
139 * which we can trust to determine the century correctly. Atom
140 * watch driver would be nice, too...
141 *
142 * if that has not happened, change for first release in 2050:
143 * if (yr<50)
144 * yr += 2100;
145 * else
146 * yr += 2000; // current line of code
147 *
148 * if that has not happened either, please do on 2099/12/31:23:59:59
149 * s/2000/2100
150 *
151 */
1da177e4 152 yr += 2000;
ad71860a
AS
153 else
154 yr += cent * 100;
1da177e4 155
4be44fcd
LB
156 seq_printf(seq, "%4.4u-", yr);
157 (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
158 (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
159 (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
160 (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
1da177e4
LT
161 (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
162
163 return 0;
164}
165
166static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
167{
168 return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
169}
170
4be44fcd 171static int get_date_field(char **p, u32 * value)
1da177e4 172{
4be44fcd
LB
173 char *next = NULL;
174 char *string_end = NULL;
175 int result = -EINVAL;
1da177e4
LT
176
177 /*
178 * Try to find delimeter, only to insert null. The end of the
179 * string won't have one, but is still valid.
180 */
975c3025
SYY
181 if (*p == NULL)
182 return result;
183
1da177e4
LT
184 next = strpbrk(*p, "- :");
185 if (next)
186 *next++ = '\0';
187
188 *value = simple_strtoul(*p, &string_end, 10);
189
190 /* Signal success if we got a good digit */
191 if (string_end != *p)
192 result = 0;
193
194 if (next)
195 *p = next;
975c3025
SYY
196 else
197 *p = NULL;
1da177e4
LT
198
199 return result;
200}
201
c67c36e4
LT
202/* Read a possibly BCD register, always return binary */
203static u32 cmos_bcd_read(int offset, int rtc_control)
204{
205 u32 val = CMOS_READ(offset);
206 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
207 BCD_TO_BIN(val);
208 return val;
209}
210
211/* Write binary value into possibly BCD register */
212static void cmos_bcd_write(u32 val, int offset, int rtc_control)
213{
214 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
215 BIN_TO_BCD(val);
216 CMOS_WRITE(val, offset);
217}
218
1da177e4 219static ssize_t
4be44fcd
LB
220acpi_system_write_alarm(struct file *file,
221 const char __user * buffer, size_t count, loff_t * ppos)
1da177e4 222{
4be44fcd
LB
223 int result = 0;
224 char alarm_string[30] = { '\0' };
225 char *p = alarm_string;
226 u32 sec, min, hr, day, mo, yr;
227 int adjust = 0;
228 unsigned char rtc_control = 0;
1da177e4
LT
229
230 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
231
232 if (count > sizeof(alarm_string) - 1)
233 return_VALUE(-EINVAL);
4be44fcd 234
1da177e4
LT
235 if (copy_from_user(alarm_string, buffer, count))
236 return_VALUE(-EFAULT);
237
238 alarm_string[count] = '\0';
239
240 /* check for time adjustment */
241 if (alarm_string[0] == '+') {
242 p++;
243 adjust = 1;
244 }
245
246 if ((result = get_date_field(&p, &yr)))
247 goto end;
248 if ((result = get_date_field(&p, &mo)))
249 goto end;
250 if ((result = get_date_field(&p, &day)))
251 goto end;
252 if ((result = get_date_field(&p, &hr)))
253 goto end;
254 if ((result = get_date_field(&p, &min)))
255 goto end;
256 if ((result = get_date_field(&p, &sec)))
257 goto end;
258
1da177e4
LT
259 spin_lock_irq(&rtc_lock);
260
261 rtc_control = CMOS_READ(RTC_CONTROL);
1da177e4
LT
262
263 if (adjust) {
c67c36e4
LT
264 yr += cmos_bcd_read(RTC_YEAR, rtc_control);
265 mo += cmos_bcd_read(RTC_MONTH, rtc_control);
266 day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
267 hr += cmos_bcd_read(RTC_HOURS, rtc_control);
268 min += cmos_bcd_read(RTC_MINUTES, rtc_control);
269 sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
1da177e4
LT
270 }
271
272 spin_unlock_irq(&rtc_lock);
273
1da177e4 274 if (sec > 59) {
08798029
YY
275 min += sec/60;
276 sec = sec%60;
1da177e4
LT
277 }
278 if (min > 59) {
08798029
YY
279 hr += min/60;
280 min = min%60;
1da177e4
LT
281 }
282 if (hr > 23) {
08798029
YY
283 day += hr/24;
284 hr = hr%24;
1da177e4
LT
285 }
286 if (day > 31) {
08798029
YY
287 mo += day/32;
288 day = day%32;
1da177e4
LT
289 }
290 if (mo > 12) {
08798029
YY
291 yr += mo/13;
292 mo = mo%13;
1da177e4 293 }
1da177e4
LT
294
295 spin_lock_irq(&rtc_lock);
296 /*
297 * Disable alarm interrupt before setting alarm timer or else
298 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
299 */
300 rtc_control &= ~RTC_AIE;
301 CMOS_WRITE(rtc_control, RTC_CONTROL);
302 CMOS_READ(RTC_INTR_FLAGS);
303
304 /* write the fields the rtc knows about */
c67c36e4
LT
305 cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
306 cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
307 cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
1da177e4
LT
308
309 /*
310 * If the system supports an enhanced alarm it will have non-zero
311 * offsets into the CMOS RAM here -- which for some reason are pointing
312 * to the RTC area of memory.
313 */
ad71860a 314 if (acpi_gbl_FADT.day_alarm)
c67c36e4 315 cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
ad71860a 316 if (acpi_gbl_FADT.month_alarm)
c67c36e4 317 cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
ad71860a 318 if (acpi_gbl_FADT.century)
c67c36e4 319 cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
1da177e4
LT
320 /* enable the rtc alarm interrupt */
321 rtc_control |= RTC_AIE;
322 CMOS_WRITE(rtc_control, RTC_CONTROL);
323 CMOS_READ(RTC_INTR_FLAGS);
324
325 spin_unlock_irq(&rtc_lock);
326
327 acpi_clear_event(ACPI_EVENT_RTC);
328 acpi_enable_event(ACPI_EVENT_RTC, 0);
329
330 *ppos += count;
331
332 result = 0;
4be44fcd 333 end:
1da177e4
LT
334 return_VALUE(result ? result : count);
335}
fd350943 336#endif /* HAVE_ACPI_LEGACY_ALARM */
1da177e4 337
4be44fcd 338extern struct list_head acpi_wakeup_device_list;
1da177e4
LT
339extern spinlock_t acpi_device_lock;
340
341static int
342acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
343{
4be44fcd 344 struct list_head *node, *next;
1da177e4 345
8aa55591 346 seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
1da177e4
LT
347
348 spin_lock(&acpi_device_lock);
349 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
4be44fcd
LB
350 struct acpi_device *dev =
351 container_of(node, struct acpi_device, wakeup_list);
8aa55591 352 struct device *ldev;
1da177e4
LT
353
354 if (!dev->wakeup.flags.valid)
355 continue;
356 spin_unlock(&acpi_device_lock);
8aa55591
DB
357
358 ldev = acpi_get_physical_device(dev->handle);
359 seq_printf(seq, "%s\t S%d\t%c%-8s ",
4be44fcd
LB
360 dev->pnp.bus_id,
361 (u32) dev->wakeup.sleep_state,
8aa55591 362 dev->wakeup.flags.run_wake ? '*' : ' ',
c65ade4d 363 dev->wakeup.state.enabled ? "enabled" : "disabled");
8aa55591
DB
364 if (ldev)
365 seq_printf(seq, "%s:%s",
fd350943
LB
366 ldev->bus ? ldev->bus->name : "no-bus",
367 ldev->bus_id);
8aa55591
DB
368 seq_printf(seq, "\n");
369 put_device(ldev);
370
1da177e4
LT
371 spin_lock(&acpi_device_lock);
372 }
373 spin_unlock(&acpi_device_lock);
374 return 0;
375}
376
377static ssize_t
4be44fcd
LB
378acpi_system_write_wakeup_device(struct file *file,
379 const char __user * buffer,
380 size_t count, loff_t * ppos)
1da177e4 381{
4be44fcd
LB
382 struct list_head *node, *next;
383 char strbuf[5];
384 char str[5] = "";
385 int len = count;
1da177e4
LT
386 struct acpi_device *found_dev = NULL;
387
4be44fcd
LB
388 if (len > 4)
389 len = 4;
1da177e4
LT
390
391 if (copy_from_user(strbuf, buffer, len))
392 return -EFAULT;
393 strbuf[len] = '\0';
394 sscanf(strbuf, "%s", str);
395
396 spin_lock(&acpi_device_lock);
397 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
4be44fcd
LB
398 struct acpi_device *dev =
399 container_of(node, struct acpi_device, wakeup_list);
1da177e4
LT
400 if (!dev->wakeup.flags.valid)
401 continue;
402
403 if (!strncmp(dev->pnp.bus_id, str, 4)) {
4be44fcd
LB
404 dev->wakeup.state.enabled =
405 dev->wakeup.state.enabled ? 0 : 1;
1da177e4
LT
406 found_dev = dev;
407 break;
408 }
409 }
410 if (found_dev) {
411 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
4be44fcd
LB
412 struct acpi_device *dev = container_of(node,
413 struct
414 acpi_device,
415 wakeup_list);
1da177e4
LT
416
417 if ((dev != found_dev) &&
4be44fcd
LB
418 (dev->wakeup.gpe_number ==
419 found_dev->wakeup.gpe_number)
420 && (dev->wakeup.gpe_device ==
421 found_dev->wakeup.gpe_device)) {
422 printk(KERN_WARNING
423 "ACPI: '%s' and '%s' have the same GPE, "
424 "can't disable/enable one seperately\n",
425 dev->pnp.bus_id, found_dev->pnp.bus_id);
426 dev->wakeup.state.enabled =
427 found_dev->wakeup.state.enabled;
1da177e4
LT
428 }
429 }
430 }
431 spin_unlock(&acpi_device_lock);
432 return count;
433}
434
435static int
436acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
437{
4be44fcd
LB
438 return single_open(file, acpi_system_wakeup_device_seq_show,
439 PDE(inode)->data);
1da177e4
LT
440}
441
d7508032 442static const struct file_operations acpi_system_wakeup_device_fops = {
cf7acfab 443 .owner = THIS_MODULE,
4be44fcd
LB
444 .open = acpi_system_wakeup_device_open_fs,
445 .read = seq_read,
446 .write = acpi_system_write_wakeup_device,
447 .llseek = seq_lseek,
448 .release = single_release,
1da177e4
LT
449};
450
134c2171 451#ifdef CONFIG_ACPI_PROCFS
d7508032 452static const struct file_operations acpi_system_sleep_fops = {
cf7acfab 453 .owner = THIS_MODULE,
4be44fcd
LB
454 .open = acpi_system_sleep_open_fs,
455 .read = seq_read,
456 .write = acpi_system_write_sleep,
457 .llseek = seq_lseek,
458 .release = single_release,
1da177e4 459};
134c2171 460#endif /* CONFIG_ACPI_PROCFS */
1da177e4 461
f5f72b46 462#ifdef HAVE_ACPI_LEGACY_ALARM
d7508032 463static const struct file_operations acpi_system_alarm_fops = {
cf7acfab 464 .owner = THIS_MODULE,
4be44fcd
LB
465 .open = acpi_system_alarm_open_fs,
466 .read = seq_read,
467 .write = acpi_system_write_alarm,
468 .llseek = seq_lseek,
469 .release = single_release,
1da177e4
LT
470};
471
4be44fcd 472static u32 rtc_handler(void *context)
1da177e4
LT
473{
474 acpi_clear_event(ACPI_EVENT_RTC);
475 acpi_disable_event(ACPI_EVENT_RTC, 0);
476
477 return ACPI_INTERRUPT_HANDLED;
478}
fd350943 479#endif /* HAVE_ACPI_LEGACY_ALARM */
1da177e4 480
f5f72b46 481static int __init acpi_sleep_proc_init(void)
1da177e4 482{
1da177e4
LT
483 if (acpi_disabled)
484 return 0;
4be44fcd 485
134c2171 486#ifdef CONFIG_ACPI_PROCFS
c65ade4d 487 /* 'sleep' [R/W] */
cf7acfab
DL
488 proc_create("sleep", S_IFREG | S_IRUGO | S_IWUSR,
489 acpi_root_dir, &acpi_system_sleep_fops);
43532c8a 490#endif /* CONFIG_ACPI_PROCFS */
1da177e4 491
f5f72b46 492#ifdef HAVE_ACPI_LEGACY_ALARM
1da177e4 493 /* 'alarm' [R/W] */
cf7acfab
DL
494 proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR,
495 acpi_root_dir, &acpi_system_alarm_fops);
1da177e4 496
f5f72b46 497 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
fd350943 498#endif /* HAVE_ACPI_LEGACY_ALARM */
f5f72b46 499
c65ade4d 500 /* 'wakeup device' [R/W] */
cf7acfab
DL
501 proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
502 acpi_root_dir, &acpi_system_wakeup_device_fops);
1da177e4 503
1da177e4
LT
504 return 0;
505}
506
507late_initcall(acpi_sleep_proc_init);