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