acpiconf.c revision 118127
165283Siwasaki/*-
265283Siwasaki * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
365283Siwasaki * All rights reserved.
465283Siwasaki *
565283Siwasaki * Redistribution and use in source and binary forms, with or without
665283Siwasaki * modification, are permitted provided that the following conditions
765283Siwasaki * are met:
865283Siwasaki * 1. Redistributions of source code must retain the above copyright
965283Siwasaki *    notice, this list of conditions and the following disclaimer.
1065283Siwasaki * 2. Redistributions in binary form must reproduce the above copyright
1165283Siwasaki *    notice, this list of conditions and the following disclaimer in the
1265283Siwasaki *    documentation and/or other materials provided with the distribution.
1365283Siwasaki *
1465283Siwasaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1565283Siwasaki * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1665283Siwasaki * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1765283Siwasaki * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1865283Siwasaki * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1965283Siwasaki * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2065283Siwasaki * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2165283Siwasaki * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2265283Siwasaki * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2365283Siwasaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2465283Siwasaki * SUCH DAMAGE.
2565283Siwasaki *
2665283Siwasaki *	$Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $
2765283Siwasaki *	$FreeBSD: head/usr.sbin/acpi/acpiconf/acpiconf.c 118127 2003-07-28 16:22:45Z njl $
2865283Siwasaki */
2965283Siwasaki
3065283Siwasaki#include <sys/param.h>
3165283Siwasaki
3265283Siwasaki#include <err.h>
3365283Siwasaki#include <fcntl.h>
3465283Siwasaki#include <stdio.h>
3566490Smsmith#include <sys/ioctl.h>
3687121Scjc#include <sysexits.h>
3765283Siwasaki#include <unistd.h>
3865283Siwasaki
3968475Siwasaki#include <dev/acpica/acpiio.h>
4066490Smsmith
41114246Snjl#include <contrib/dev/acpica/acpi.h>
4268475Siwasaki
4365283Siwasaki#define ACPIDEV	"/dev/acpi"
4465283Siwasaki
4565283Siwasakistatic int
4665283Siwasakiacpi_enable_disable(int enable)
4765283Siwasaki{
4865283Siwasaki	int	fd;
4965283Siwasaki
5065283Siwasaki	fd  = open(ACPIDEV, O_RDWR);
5165283Siwasaki	if (fd == -1) {
5287121Scjc		err(EX_OSFILE, ACPIDEV);
5365283Siwasaki	}
5465283Siwasaki	if (ioctl(fd, enable, NULL) == -1) {
5587121Scjc		if (enable == ACPIIO_ENABLE)
5687121Scjc			err(EX_IOERR, "enable failed");
5787121Scjc		else
5887121Scjc			err(EX_IOERR, "disable failed");
5965283Siwasaki	}
6065283Siwasaki	close(fd);
6165283Siwasaki
6265283Siwasaki	return (0);
6365283Siwasaki}
6465283Siwasaki
6565283Siwasakistatic int
6665283Siwasakiacpi_sleep(int sleep_type)
6765283Siwasaki{
6865283Siwasaki	int	fd;
6965283Siwasaki
7065283Siwasaki	fd  = open(ACPIDEV, O_RDWR);
7165283Siwasaki	if (fd == -1) {
7287121Scjc		err(EX_OSFILE, ACPIDEV);
7365283Siwasaki	}
7465283Siwasaki	if (ioctl(fd, ACPIIO_SETSLPSTATE, &sleep_type) == -1) {
7587121Scjc		err(EX_IOERR, "sleep type (%d) failed", sleep_type);
7665283Siwasaki	}
7765283Siwasaki	close(fd);
7865283Siwasaki
7965283Siwasaki	return (0);
8065283Siwasaki}
8165283Siwasaki
8268475Siwasakistatic void
8368475Siwasakiusage(const char* prog)
8468475Siwasaki{
8568475Siwasaki	printf("usage: %s [-deh] [-s [1|2|3|4|4b|5]]\n", prog);
8668475Siwasaki	exit(0);
8768475Siwasaki}
8868475Siwasaki
8965283Siwasakiint
9065283Siwasakimain(int argc, char *argv[])
9165283Siwasaki{
9268475Siwasaki	char	c, *prog;
9365283Siwasaki	int	sleep_type;
9465283Siwasaki
9568475Siwasaki	prog = argv[0];
9665283Siwasaki	sleep_type = -1;
9768475Siwasaki	while ((c = getopt(argc, argv, "dehs:")) != -1) {
9865283Siwasaki		switch (c) {
9968475Siwasaki		case 'd':
10068475Siwasaki			acpi_enable_disable(ACPIIO_DISABLE);
10168475Siwasaki			break;
10268475Siwasaki
10365283Siwasaki		case 'e':
10465283Siwasaki			acpi_enable_disable(ACPIIO_ENABLE);
10565283Siwasaki			break;
10665283Siwasaki
10768475Siwasaki		case 'h':
10868475Siwasaki			usage(prog);
10965283Siwasaki			break;
11065283Siwasaki
11165283Siwasaki		case 's':
112118127Snjl			if (optarg[0] == 'S')
113118127Snjl				sleep_type = optarg[1] - '0';
114118127Snjl			else
115118127Snjl				sleep_type = optarg[0] - '0';
11687121Scjc			if (sleep_type < 0 || sleep_type > 5)
11787121Scjc				errx(EX_USAGE, "invalid sleep type (%d)",
11887121Scjc				    sleep_type);
11965283Siwasaki			break;
12065283Siwasaki		default:
12165283Siwasaki			argc -= optind;
12265283Siwasaki			argv += optind;
12365283Siwasaki		}
12465283Siwasaki	}
12565283Siwasaki
12665283Siwasaki	if (sleep_type != -1) {
12765283Siwasaki		sleep(1);	/* wait 1 sec. for key-release event */
12865283Siwasaki		acpi_sleep(sleep_type);
12965283Siwasaki	}
13065283Siwasaki	return (0);
13165283Siwasaki}
132