1 /*-
2  * Copyright (c) 2001 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: stable/10/usr.sbin/digictl/digictl.c 95258 2002-04-22 13:44:47Z des $
27  */
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sysexits.h>
35 #include <sys/digiio.h>
36 #include <sys/ioctl.h>
37 #include <unistd.h>
38 
39 enum aflag { NO_AFLAG, ALTPIN_DISABLE, ALTPIN_ENABLE, ALTPIN_QUERY };
40 
41 static int
usage(const char * prog)42 usage(const char *prog)
43 {
44 	fprintf(stderr, "usage: %s -a disable|enable|query device\n", prog);
45 	fprintf(stderr, "       %s [-d debug] [-ir] ctrl-device...\n", prog);
46 	return (EX_USAGE);
47 }
48 
49 int
main(int argc,char ** argv)50 main(int argc, char **argv)
51 {
52 	char namedata[256], *name = namedata;
53 	const char *prog;
54 	enum digi_model model;
55 	int altpin, ch, debug, fd, i, res;
56 	int dflag, iflag, rflag;
57 	enum aflag aflag;
58 
59 	if ((prog = strrchr(argv[0], '/')) == NULL)
60 		prog = argv[0];
61 	else
62 		prog++;
63 
64 	aflag = NO_AFLAG;
65 	dflag = iflag = rflag = 0;
66 	while ((ch = getopt(argc, argv, "a:d:ir")) != -1)
67 		switch (ch) {
68 		case 'a':
69 			if (strcasecmp(optarg, "disable") == 0)
70 				aflag = ALTPIN_DISABLE;
71 			else if (strcasecmp(optarg, "enable") == 0)
72 				aflag = ALTPIN_ENABLE;
73 			else if (strcasecmp(optarg, "query") == 0)
74 				aflag = ALTPIN_QUERY;
75 			else
76 				return (usage(prog));
77 			break;
78 
79 		case 'd':
80 			dflag = 1;
81 			debug = atoi(optarg);
82 			break;
83 
84 		case 'i':
85 			iflag = 1;
86 			break;
87 
88 		case 'r':
89 			rflag = 1;
90 			break;
91 
92 		default:
93 			return (usage(prog));
94 		}
95 
96 	if ((dflag || iflag || rflag) && aflag != NO_AFLAG)
97 		return (usage(prog));
98 
99 	if (argc <= optind)
100 		return (usage(prog));
101 
102 	altpin = (aflag == ALTPIN_ENABLE);
103 	res = 0;
104 
105 	for (i = optind; i < argc; i++) {
106 		if ((fd = open(argv[i], O_RDONLY)) == -1) {
107 			fprintf(stderr, "%s: %s: open: %s\n", prog, argv[i],
108 			    strerror(errno));
109 			res++;
110 			continue;
111 		}
112 
113 		switch (aflag) {
114 		case NO_AFLAG:
115 			break;
116 
117 		case ALTPIN_ENABLE:
118 		case ALTPIN_DISABLE:
119 			if (ioctl(fd, DIGIIO_SETALTPIN, &altpin) != 0) {
120 				fprintf(stderr, "%s: %s: set altpin: %s\n",
121 				    prog, argv[i], strerror(errno));
122 				res++;
123 			}
124 			break;
125 
126 		case ALTPIN_QUERY:
127 			if (ioctl(fd, DIGIIO_GETALTPIN, &altpin) != 0) {
128 				fprintf(stderr, "%s: %s: get altpin: %s\n",
129 				    prog, argv[i], strerror(errno));
130 				res++;
131 			} else {
132 				if (argc > optind + 1)
133 					printf("%s: ", argv[i]);
134 				puts(altpin ? "enabled" : "disabled");
135 			}
136 			break;
137 		}
138 
139 		if (dflag && ioctl(fd, DIGIIO_DEBUG, &debug) != 0) {
140 			fprintf(stderr, "%s: %s: debug: %s\n",
141 			    prog, argv[i], strerror(errno));
142 			res++;
143 		}
144 
145 		if (iflag) {
146 			if (ioctl(fd, DIGIIO_MODEL, &model) != 0) {
147 				fprintf(stderr, "%s: %s: model: %s\n",
148 				    prog, argv[i], strerror(errno));
149 				res++;
150 			} else if (ioctl(fd, DIGIIO_IDENT, &name) != 0) {
151 				fprintf(stderr, "%s: %s: ident: %s\n",
152 				    prog, argv[i], strerror(errno));
153 				res++;
154 			} else {
155 				if (argc > optind + 1)
156 					printf("%s: ", argv[i]);
157 				printf("%s (type %d)\n", name, (int)model);
158 			}
159 		}
160 
161 		if (rflag && ioctl(fd, DIGIIO_REINIT) != 0) {
162 			fprintf(stderr, "%s: %s: reinit: %s\n",
163 			    prog, argv[i], strerror(errno));
164 			res++;
165 		}
166 
167 		close(fd);
168 	}
169 
170 	return (res);
171 }
172