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