176195Sbrian/*-
276195Sbrian * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
376195Sbrian * All rights reserved.
476195Sbrian *
576195Sbrian * Redistribution and use in source and binary forms, with or without
676195Sbrian * modification, are permitted provided that the following conditions
776195Sbrian * are met:
876195Sbrian * 1. Redistributions of source code must retain the above copyright
976195Sbrian *    notice, this list of conditions and the following disclaimer.
1076195Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1176195Sbrian *    notice, this list of conditions and the following disclaimer in the
1276195Sbrian *    documentation and/or other materials provided with the distribution.
1376195Sbrian *
1476195Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1576195Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1676195Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1776195Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1876195Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1976195Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2076195Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2176195Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2276195Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2376195Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2476195Sbrian * SUCH DAMAGE.
2576195Sbrian *
2676195Sbrian * $FreeBSD$
2776195Sbrian */
2876195Sbrian
2976195Sbrian#include <errno.h>
3076195Sbrian#include <fcntl.h>
3176195Sbrian#include <stdio.h>
3276195Sbrian#include <stdlib.h>
3376195Sbrian#include <string.h>
3476195Sbrian#include <sysexits.h>
3576848Sbrian#include <sys/digiio.h>
3676195Sbrian#include <sys/ioctl.h>
3776195Sbrian#include <unistd.h>
3876195Sbrian
3978497Sbrianenum aflag { NO_AFLAG, ALTPIN_DISABLE, ALTPIN_ENABLE, ALTPIN_QUERY };
4078497Sbrian
4176195Sbrianstatic int
4276195Sbrianusage(const char *prog)
4376195Sbrian{
4495258Sdes	fprintf(stderr, "usage: %s -a disable|enable|query device\n", prog);
4578497Sbrian	fprintf(stderr, "       %s [-d debug] [-ir] ctrl-device...\n", prog);
4676195Sbrian	return (EX_USAGE);
4776195Sbrian}
4876195Sbrian
4976195Sbrianint
5076195Sbrianmain(int argc, char **argv)
5176195Sbrian{
5276195Sbrian	char namedata[256], *name = namedata;
5376195Sbrian	const char *prog;
5476707Sbrian	enum digi_model model;
5578497Sbrian	int altpin, ch, debug, fd, i, res;
5676195Sbrian	int dflag, iflag, rflag;
5778497Sbrian	enum aflag aflag;
5876195Sbrian
5976195Sbrian	if ((prog = strrchr(argv[0], '/')) == NULL)
6076195Sbrian		prog = argv[0];
6176195Sbrian	else
6276195Sbrian		prog++;
6376195Sbrian
6478497Sbrian	aflag = NO_AFLAG;
6576195Sbrian	dflag = iflag = rflag = 0;
6678497Sbrian	while ((ch = getopt(argc, argv, "a:d:ir")) != -1)
6776195Sbrian		switch (ch) {
6878497Sbrian		case 'a':
6978497Sbrian			if (strcasecmp(optarg, "disable") == 0)
7078497Sbrian				aflag = ALTPIN_DISABLE;
7178497Sbrian			else if (strcasecmp(optarg, "enable") == 0)
7278497Sbrian				aflag = ALTPIN_ENABLE;
7378497Sbrian			else if (strcasecmp(optarg, "query") == 0)
7478497Sbrian				aflag = ALTPIN_QUERY;
7578497Sbrian			else
7678497Sbrian				return (usage(prog));
7778497Sbrian			break;
7878497Sbrian
7976195Sbrian		case 'd':
8076195Sbrian			dflag = 1;
8176195Sbrian			debug = atoi(optarg);
8276195Sbrian			break;
8378497Sbrian
8476195Sbrian		case 'i':
8576195Sbrian			iflag = 1;
8676195Sbrian			break;
8778497Sbrian
8876195Sbrian		case 'r':
8976195Sbrian			rflag = 1;
9076195Sbrian			break;
9178497Sbrian
9276195Sbrian		default:
9378497Sbrian			return (usage(prog));
9476195Sbrian		}
9576195Sbrian
9678497Sbrian	if ((dflag || iflag || rflag) && aflag != NO_AFLAG)
9778497Sbrian		return (usage(prog));
9876195Sbrian
9978497Sbrian	if (argc <= optind)
10078497Sbrian		return (usage(prog));
10178497Sbrian
10278497Sbrian	altpin = (aflag == ALTPIN_ENABLE);
10376195Sbrian	res = 0;
10478497Sbrian
10576195Sbrian	for (i = optind; i < argc; i++) {
10676195Sbrian		if ((fd = open(argv[i], O_RDONLY)) == -1) {
10776195Sbrian			fprintf(stderr, "%s: %s: open: %s\n", prog, argv[i],
10876195Sbrian			    strerror(errno));
10976195Sbrian			res++;
11076195Sbrian			continue;
11176195Sbrian		}
11276195Sbrian
11378497Sbrian		switch (aflag) {
11478497Sbrian		case NO_AFLAG:
11578497Sbrian			break;
11678497Sbrian
11778497Sbrian		case ALTPIN_ENABLE:
11878497Sbrian		case ALTPIN_DISABLE:
11978497Sbrian			if (ioctl(fd, DIGIIO_SETALTPIN, &altpin) != 0) {
12078497Sbrian				fprintf(stderr, "%s: %s: set altpin: %s\n",
12178497Sbrian				    prog, argv[i], strerror(errno));
12278497Sbrian				res++;
12378497Sbrian			}
12478497Sbrian			break;
12578497Sbrian
12678497Sbrian		case ALTPIN_QUERY:
12778497Sbrian			if (ioctl(fd, DIGIIO_GETALTPIN, &altpin) != 0) {
12878497Sbrian				fprintf(stderr, "%s: %s: get altpin: %s\n",
12978497Sbrian				    prog, argv[i], strerror(errno));
13078497Sbrian				res++;
13178497Sbrian			} else {
13278497Sbrian				if (argc > optind + 1)
13378497Sbrian					printf("%s: ", argv[i]);
13478497Sbrian				puts(altpin ? "enabled" : "disabled");
13578497Sbrian			}
13678497Sbrian			break;
13778497Sbrian		}
13878497Sbrian
13976195Sbrian		if (dflag && ioctl(fd, DIGIIO_DEBUG, &debug) != 0) {
14076195Sbrian			fprintf(stderr, "%s: %s: debug: %s\n",
14176195Sbrian			    prog, argv[i], strerror(errno));
14276195Sbrian			res++;
14376195Sbrian		}
14476195Sbrian
14576195Sbrian		if (iflag) {
14676195Sbrian			if (ioctl(fd, DIGIIO_MODEL, &model) != 0) {
14776195Sbrian				fprintf(stderr, "%s: %s: model: %s\n",
14876195Sbrian				    prog, argv[i], strerror(errno));
14976195Sbrian				res++;
15076195Sbrian			} else if (ioctl(fd, DIGIIO_IDENT, &name) != 0) {
15176195Sbrian				fprintf(stderr, "%s: %s: ident: %s\n",
15276195Sbrian				    prog, argv[i], strerror(errno));
15376195Sbrian				res++;
15478497Sbrian			} else {
15578497Sbrian				if (argc > optind + 1)
15678497Sbrian					printf("%s: ", argv[i]);
15778497Sbrian				printf("%s (type %d)\n", name, (int)model);
15878497Sbrian			}
15976195Sbrian		}
16076195Sbrian
16176195Sbrian		if (rflag && ioctl(fd, DIGIIO_REINIT) != 0) {
16276195Sbrian			fprintf(stderr, "%s: %s: reinit: %s\n",
16376195Sbrian			    prog, argv[i], strerror(errno));
16476195Sbrian			res++;
16576195Sbrian		}
16676195Sbrian
16776195Sbrian		close(fd);
16876195Sbrian	}
16976195Sbrian
17076195Sbrian	return (res);
17176195Sbrian}
172