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