proc.c 12.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/suspend.h>
#include <linux/bcd.h>
#include <asm/uaccess.h>

#include <acpi/acpi_bus.h>
#include <acpi/acpi_drivers.h>

#ifdef CONFIG_X86
#include <linux/mc146818rtc.h>
#endif

#include "sleep.h"

#define _COMPONENT		ACPI_SYSTEM_COMPONENT
17 18 19 20 21 22 23 24

/*
 * this file provides support for:
 * /proc/acpi/sleep
 * /proc/acpi/alarm
 * /proc/acpi/wakeup
 */

L
Len Brown 已提交
25
ACPI_MODULE_NAME("sleep")
26
#ifdef	CONFIG_ACPI_PROCFS
L
Linus Torvalds 已提交
27 28
static int acpi_system_sleep_seq_show(struct seq_file *seq, void *offset)
{
L
Len Brown 已提交
29
	int i;
L
Linus Torvalds 已提交
30 31 32 33 34

	ACPI_FUNCTION_TRACE("acpi_system_sleep_seq_show");

	for (i = 0; i <= ACPI_STATE_S5; i++) {
		if (sleep_states[i]) {
L
Len Brown 已提交
35
			seq_printf(seq, "S%d ", i);
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
		}
	}

	seq_puts(seq, "\n");

	return 0;
}

static int acpi_system_sleep_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_system_sleep_seq_show, PDE(inode)->data);
}

static ssize_t
L
Len Brown 已提交
50 51
acpi_system_write_sleep(struct file *file,
			const char __user * buffer, size_t count, loff_t * ppos)
L
Linus Torvalds 已提交
52
{
L
Len Brown 已提交
53 54 55
	char str[12];
	u32 state = 0;
	int error = 0;
L
Linus Torvalds 已提交
56 57 58

	if (count > sizeof(str) - 1)
		goto Done;
L
Len Brown 已提交
59
	memset(str, 0, sizeof(str));
L
Linus Torvalds 已提交
60 61 62 63
	if (copy_from_user(str, buffer, count))
		return -EFAULT;

	/* Check for S4 bios request */
L
Len Brown 已提交
64
	if (!strcmp(str, "4b")) {
L
Linus Torvalds 已提交
65 66 67 68
		error = acpi_suspend(4);
		goto Done;
	}
	state = simple_strtoul(str, NULL, 0);
69
#ifdef CONFIG_HIBERNATION
L
Linus Torvalds 已提交
70
	if (state == 4) {
71
		error = hibernate();
L
Linus Torvalds 已提交
72 73 74 75
		goto Done;
	}
#endif
	error = acpi_suspend(state);
L
Len Brown 已提交
76
      Done:
L
Linus Torvalds 已提交
77 78
	return error ? error : count;
}
79
#endif				/* CONFIG_ACPI_PROCFS */
L
Linus Torvalds 已提交
80

L
Len Brown 已提交
81
#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86)
D
David Brownell 已提交
82 83 84 85 86 87 88
/* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
#else
#define	HAVE_ACPI_LEGACY_ALARM
#endif

#ifdef	HAVE_ACPI_LEGACY_ALARM

L
Linus Torvalds 已提交
89 90
static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
{
L
Len Brown 已提交
91
	u32 sec, min, hr;
92
	u32 day, mo, yr, cent = 0;
L
Len Brown 已提交
93 94
	unsigned char rtc_control = 0;
	unsigned long flags;
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105

	ACPI_FUNCTION_TRACE("acpi_system_alarm_seq_show");

	spin_lock_irqsave(&rtc_lock, flags);

	sec = CMOS_READ(RTC_SECONDS_ALARM);
	min = CMOS_READ(RTC_MINUTES_ALARM);
	hr = CMOS_READ(RTC_HOURS_ALARM);
	rtc_control = CMOS_READ(RTC_CONTROL);

	/* If we ever get an FACP with proper values... */
106
	if (acpi_gbl_FADT.day_alarm)
L
Linus Torvalds 已提交
107
		/* ACPI spec: only low 6 its should be cared */
108
		day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
L
Linus Torvalds 已提交
109
	else
L
Len Brown 已提交
110
		day = CMOS_READ(RTC_DAY_OF_MONTH);
111 112
	if (acpi_gbl_FADT.month_alarm)
		mo = CMOS_READ(acpi_gbl_FADT.month_alarm);
L
Linus Torvalds 已提交
113 114
	else
		mo = CMOS_READ(RTC_MONTH);
115 116 117 118
	if (acpi_gbl_FADT.century)
		cent = CMOS_READ(acpi_gbl_FADT.century);

	yr = CMOS_READ(RTC_YEAR);
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128

	spin_unlock_irqrestore(&rtc_lock, flags);

	if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
		BCD_TO_BIN(sec);
		BCD_TO_BIN(min);
		BCD_TO_BIN(hr);
		BCD_TO_BIN(day);
		BCD_TO_BIN(mo);
		BCD_TO_BIN(yr);
129
		BCD_TO_BIN(cent);
L
Linus Torvalds 已提交
130 131
	}

L
Len Brown 已提交
132
	/* we're trusting the FADT (see above) */
133
	if (!acpi_gbl_FADT.century)
L
Len Brown 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
		/* If we're not trusting the FADT, we should at least make it
		 * right for _this_ century... ehm, what is _this_ century?
		 *
		 * TBD:
		 *  ASAP: find piece of code in the kernel, e.g. star tracker driver,
		 *        which we can trust to determine the century correctly. Atom
		 *        watch driver would be nice, too...
		 *
		 *  if that has not happened, change for first release in 2050:
		 *        if (yr<50)
		 *                yr += 2100;
		 *        else
		 *                yr += 2000;   // current line of code
		 *
		 *  if that has not happened either, please do on 2099/12/31:23:59:59
		 *        s/2000/2100
		 *
		 */
L
Linus Torvalds 已提交
152
		yr += 2000;
153 154
	else
		yr += cent * 100;
L
Linus Torvalds 已提交
155

L
Len Brown 已提交
156 157 158 159 160
	seq_printf(seq, "%4.4u-", yr);
	(mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
	(day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
	(hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
	(min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170
	(sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);

	return 0;
}

static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
{
	return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
}

L
Len Brown 已提交
171
static int get_date_field(char **p, u32 * value)
L
Linus Torvalds 已提交
172
{
L
Len Brown 已提交
173 174 175
	char *next = NULL;
	char *string_end = NULL;
	int result = -EINVAL;
L
Linus Torvalds 已提交
176 177 178 179 180

	/*
	 * Try to find delimeter, only to insert null.  The end of the
	 * string won't have one, but is still valid.
	 */
181 182 183
	if (*p == NULL)
		return result;

L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193 194 195
	next = strpbrk(*p, "- :");
	if (next)
		*next++ = '\0';

	*value = simple_strtoul(*p, &string_end, 10);

	/* Signal success if we got a good digit */
	if (string_end != *p)
		result = 0;

	if (next)
		*p = next;
196 197
	else
		*p = NULL;
L
Linus Torvalds 已提交
198 199 200 201

	return result;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/* Read a possibly BCD register, always return binary */
static u32 cmos_bcd_read(int offset, int rtc_control)
{
	u32 val = CMOS_READ(offset);
	if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
		BCD_TO_BIN(val);
	return val;
}

/* Write binary value into possibly BCD register */
static void cmos_bcd_write(u32 val, int offset, int rtc_control)
{
	if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
		BIN_TO_BCD(val);
	CMOS_WRITE(val, offset);
}

L
Linus Torvalds 已提交
219
static ssize_t
L
Len Brown 已提交
220 221
acpi_system_write_alarm(struct file *file,
			const char __user * buffer, size_t count, loff_t * ppos)
L
Linus Torvalds 已提交
222
{
L
Len Brown 已提交
223 224 225 226 227 228
	int result = 0;
	char alarm_string[30] = { '\0' };
	char *p = alarm_string;
	u32 sec, min, hr, day, mo, yr;
	int adjust = 0;
	unsigned char rtc_control = 0;
L
Linus Torvalds 已提交
229 230 231 232 233

	ACPI_FUNCTION_TRACE("acpi_system_write_alarm");

	if (count > sizeof(alarm_string) - 1)
		return_VALUE(-EINVAL);
L
Len Brown 已提交
234

L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	if (copy_from_user(alarm_string, buffer, count))
		return_VALUE(-EFAULT);

	alarm_string[count] = '\0';

	/* check for time adjustment */
	if (alarm_string[0] == '+') {
		p++;
		adjust = 1;
	}

	if ((result = get_date_field(&p, &yr)))
		goto end;
	if ((result = get_date_field(&p, &mo)))
		goto end;
	if ((result = get_date_field(&p, &day)))
		goto end;
	if ((result = get_date_field(&p, &hr)))
		goto end;
	if ((result = get_date_field(&p, &min)))
		goto end;
	if ((result = get_date_field(&p, &sec)))
		goto end;

	if (sec > 59) {
		min += 1;
		sec -= 60;
	}
	if (min > 59) {
		hr += 1;
		min -= 60;
	}
	if (hr > 23) {
		day += 1;
		hr -= 24;
	}
	if (day > 31) {
		mo += 1;
		day -= 31;
	}
	if (mo > 12) {
		yr += 1;
		mo -= 12;
	}

	spin_lock_irq(&rtc_lock);

	rtc_control = CMOS_READ(RTC_CONTROL);

	if (adjust) {
285 286 287 288 289 290
		yr += cmos_bcd_read(RTC_YEAR, rtc_control);
		mo += cmos_bcd_read(RTC_MONTH, rtc_control);
		day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
		hr += cmos_bcd_read(RTC_HOURS, rtc_control);
		min += cmos_bcd_read(RTC_MINUTES, rtc_control);
		sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	}

	spin_unlock_irq(&rtc_lock);

	if (sec > 59) {
		min++;
		sec -= 60;
	}
	if (min > 59) {
		hr++;
		min -= 60;
	}
	if (hr > 23) {
		day++;
		hr -= 24;
	}
	if (day > 31) {
		mo++;
		day -= 31;
	}
	if (mo > 12) {
		yr++;
		mo -= 12;
	}

	spin_lock_irq(&rtc_lock);
	/*
	 * Disable alarm interrupt before setting alarm timer or else
	 * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
	 */
	rtc_control &= ~RTC_AIE;
	CMOS_WRITE(rtc_control, RTC_CONTROL);
	CMOS_READ(RTC_INTR_FLAGS);

	/* write the fields the rtc knows about */
326 327 328
	cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
	cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
	cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
L
Linus Torvalds 已提交
329 330 331 332 333 334

	/*
	 * If the system supports an enhanced alarm it will have non-zero
	 * offsets into the CMOS RAM here -- which for some reason are pointing
	 * to the RTC area of memory.
	 */
335
	if (acpi_gbl_FADT.day_alarm)
336
		cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
337
	if (acpi_gbl_FADT.month_alarm)
338
		cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
339
	if (acpi_gbl_FADT.century)
340
		cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
L
Linus Torvalds 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353
	/* enable the rtc alarm interrupt */
	rtc_control |= RTC_AIE;
	CMOS_WRITE(rtc_control, RTC_CONTROL);
	CMOS_READ(RTC_INTR_FLAGS);

	spin_unlock_irq(&rtc_lock);

	acpi_clear_event(ACPI_EVENT_RTC);
	acpi_enable_event(ACPI_EVENT_RTC, 0);

	*ppos += count;

	result = 0;
L
Len Brown 已提交
354
      end:
L
Linus Torvalds 已提交
355 356
	return_VALUE(result ? result : count);
}
L
Len Brown 已提交
357
#endif				/* HAVE_ACPI_LEGACY_ALARM */
L
Linus Torvalds 已提交
358

L
Len Brown 已提交
359
extern struct list_head acpi_wakeup_device_list;
L
Linus Torvalds 已提交
360 361 362 363 364
extern spinlock_t acpi_device_lock;

static int
acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
{
L
Len Brown 已提交
365
	struct list_head *node, *next;
L
Linus Torvalds 已提交
366

367
	seq_printf(seq, "Device\tS-state\t  Status   Sysfs node\n");
L
Linus Torvalds 已提交
368 369 370

	spin_lock(&acpi_device_lock);
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
L
Len Brown 已提交
371 372
		struct acpi_device *dev =
		    container_of(node, struct acpi_device, wakeup_list);
373
		struct device *ldev;
L
Linus Torvalds 已提交
374 375 376 377

		if (!dev->wakeup.flags.valid)
			continue;
		spin_unlock(&acpi_device_lock);
378 379 380

		ldev = acpi_get_physical_device(dev->handle);
		seq_printf(seq, "%s\t  S%d\t%c%-8s  ",
L
Len Brown 已提交
381 382
			   dev->pnp.bus_id,
			   (u32) dev->wakeup.sleep_state,
383
			   dev->wakeup.flags.run_wake ? '*' : ' ',
P
Pavel Machek 已提交
384
			   dev->wakeup.state.enabled ? "enabled" : "disabled");
385 386
		if (ldev)
			seq_printf(seq, "%s:%s",
L
Len Brown 已提交
387 388
				   ldev->bus ? ldev->bus->name : "no-bus",
				   ldev->bus_id);
389 390 391
		seq_printf(seq, "\n");
		put_device(ldev);

L
Linus Torvalds 已提交
392 393 394 395 396 397 398
		spin_lock(&acpi_device_lock);
	}
	spin_unlock(&acpi_device_lock);
	return 0;
}

static ssize_t
L
Len Brown 已提交
399 400 401
acpi_system_write_wakeup_device(struct file *file,
				const char __user * buffer,
				size_t count, loff_t * ppos)
L
Linus Torvalds 已提交
402
{
L
Len Brown 已提交
403 404 405 406
	struct list_head *node, *next;
	char strbuf[5];
	char str[5] = "";
	int len = count;
L
Linus Torvalds 已提交
407 408
	struct acpi_device *found_dev = NULL;

L
Len Brown 已提交
409 410
	if (len > 4)
		len = 4;
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418

	if (copy_from_user(strbuf, buffer, len))
		return -EFAULT;
	strbuf[len] = '\0';
	sscanf(strbuf, "%s", str);

	spin_lock(&acpi_device_lock);
	list_for_each_safe(node, next, &acpi_wakeup_device_list) {
L
Len Brown 已提交
419 420
		struct acpi_device *dev =
		    container_of(node, struct acpi_device, wakeup_list);
L
Linus Torvalds 已提交
421 422 423 424
		if (!dev->wakeup.flags.valid)
			continue;

		if (!strncmp(dev->pnp.bus_id, str, 4)) {
L
Len Brown 已提交
425 426
			dev->wakeup.state.enabled =
			    dev->wakeup.state.enabled ? 0 : 1;
L
Linus Torvalds 已提交
427 428 429 430 431 432
			found_dev = dev;
			break;
		}
	}
	if (found_dev) {
		list_for_each_safe(node, next, &acpi_wakeup_device_list) {
L
Len Brown 已提交
433 434 435 436
			struct acpi_device *dev = container_of(node,
							       struct
							       acpi_device,
							       wakeup_list);
L
Linus Torvalds 已提交
437 438

			if ((dev != found_dev) &&
L
Len Brown 已提交
439 440 441 442 443 444 445 446 447 448
			    (dev->wakeup.gpe_number ==
			     found_dev->wakeup.gpe_number)
			    && (dev->wakeup.gpe_device ==
				found_dev->wakeup.gpe_device)) {
				printk(KERN_WARNING
				       "ACPI: '%s' and '%s' have the same GPE, "
				       "can't disable/enable one seperately\n",
				       dev->pnp.bus_id, found_dev->pnp.bus_id);
				dev->wakeup.state.enabled =
				    found_dev->wakeup.state.enabled;
L
Linus Torvalds 已提交
449 450 451 452 453 454 455 456 457 458
			}
		}
	}
	spin_unlock(&acpi_device_lock);
	return count;
}

static int
acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
{
L
Len Brown 已提交
459 460
	return single_open(file, acpi_system_wakeup_device_seq_show,
			   PDE(inode)->data);
L
Linus Torvalds 已提交
461 462
}

463
static const struct file_operations acpi_system_wakeup_device_fops = {
L
Len Brown 已提交
464 465 466 467 468
	.open = acpi_system_wakeup_device_open_fs,
	.read = seq_read,
	.write = acpi_system_write_wakeup_device,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
469 470
};

471
#ifdef	CONFIG_ACPI_PROCFS
472
static const struct file_operations acpi_system_sleep_fops = {
L
Len Brown 已提交
473 474 475 476 477
	.open = acpi_system_sleep_open_fs,
	.read = seq_read,
	.write = acpi_system_write_sleep,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
478
};
479
#endif				/* CONFIG_ACPI_PROCFS */
L
Linus Torvalds 已提交
480

D
David Brownell 已提交
481
#ifdef	HAVE_ACPI_LEGACY_ALARM
482
static const struct file_operations acpi_system_alarm_fops = {
L
Len Brown 已提交
483 484 485 486 487
	.open = acpi_system_alarm_open_fs,
	.read = seq_read,
	.write = acpi_system_write_alarm,
	.llseek = seq_lseek,
	.release = single_release,
L
Linus Torvalds 已提交
488 489
};

L
Len Brown 已提交
490
static u32 rtc_handler(void *context)
L
Linus Torvalds 已提交
491 492 493 494 495 496
{
	acpi_clear_event(ACPI_EVENT_RTC);
	acpi_disable_event(ACPI_EVENT_RTC, 0);

	return ACPI_INTERRUPT_HANDLED;
}
L
Len Brown 已提交
497
#endif				/* HAVE_ACPI_LEGACY_ALARM */
L
Linus Torvalds 已提交
498

D
David Brownell 已提交
499
static int __init acpi_sleep_proc_init(void)
L
Linus Torvalds 已提交
500
{
P
Pavel Machek 已提交
501
	struct proc_dir_entry *entry = NULL;
L
Linus Torvalds 已提交
502 503 504

	if (acpi_disabled)
		return 0;
L
Len Brown 已提交
505

506
#ifdef	CONFIG_ACPI_PROCFS
P
Pavel Machek 已提交
507
	/* 'sleep' [R/W] */
L
Len Brown 已提交
508 509 510
	entry =
	    create_proc_entry("sleep", S_IFREG | S_IRUGO | S_IWUSR,
			      acpi_root_dir);
L
Linus Torvalds 已提交
511 512
	if (entry)
		entry->proc_fops = &acpi_system_sleep_fops;
513
#endif				/* CONFIG_ACPI_PROCFS */
L
Linus Torvalds 已提交
514

D
David Brownell 已提交
515
#ifdef	HAVE_ACPI_LEGACY_ALARM
L
Linus Torvalds 已提交
516
	/* 'alarm' [R/W] */
L
Len Brown 已提交
517 518 519
	entry =
	    create_proc_entry("alarm", S_IFREG | S_IRUGO | S_IWUSR,
			      acpi_root_dir);
L
Linus Torvalds 已提交
520 521 522
	if (entry)
		entry->proc_fops = &acpi_system_alarm_fops;

D
David Brownell 已提交
523
	acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
L
Len Brown 已提交
524
#endif				/* HAVE_ACPI_LEGACY_ALARM */
D
David Brownell 已提交
525

P
Pavel Machek 已提交
526
	/* 'wakeup device' [R/W] */
L
Len Brown 已提交
527 528 529
	entry =
	    create_proc_entry("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
			      acpi_root_dir);
L
Linus Torvalds 已提交
530 531 532 533 534 535 536
	if (entry)
		entry->proc_fops = &acpi_system_wakeup_device_fops;

	return 0;
}

late_initcall(acpi_sleep_proc_init);