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