1 /* $NetBSD: ctl.c,v 1.44 2019/05/08 14:44:42 isaki Exp $ */
2
3 /*
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss@NetBSD.org).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32
33 #ifndef lint
34 __RCSID("$NetBSD: ctl.c,v 1.44 2019/05/08 14:44:42 isaki Exp $");
35 #endif
36
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/audioio.h>
42
43 #include <err.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include <paths.h>
51
52 #include "libaudio.h"
53
54 static struct field *findfield(const char *name);
55 static void prfield(const struct field *p, const char *sep);
56 static void rdfield(struct field *p, char *q);
57 static void getinfo(int fd);
58 static void audioctl_write(int, int, char *[]);
59 __dead static void usage(void);
60
61 static audio_device_t adev;
62
63 static audio_info_t info;
64
65 static char encbuf[1000];
66
67 static int properties, fullduplex, rerror;
68
69 int verbose;
70
71 static struct field {
72 const char *name;
73 void *valp;
74 int format;
75 #define STRING 1
76 #define INT 2
77 #define UINT 3
78 #define P_R 4
79 #define ULONG 5 /* XXX obsolete now */
80 #define UCHAR 6
81 #define ENC 7
82 #define PROPS 8
83 #define XINT 9
84 #define FORMAT 10
85 char flags;
86 #define READONLY 1
87 #define ALIAS 2
88 #define SET 4
89 } fields[] = {
90 { "name", &adev.name, STRING, READONLY },
91 { "version", &adev.version, STRING, READONLY },
92 { "config", &adev.config, STRING, READONLY },
93 { "encodings", encbuf, STRING, READONLY },
94 { "properties", &properties, PROPS, READONLY },
95 { "full_duplex", &fullduplex, UINT, 0 },
96 { "fullduplex", &fullduplex, UINT, 0 },
97 { "blocksize", &info.blocksize, UINT, 0 },
98 { "hiwat", &info.hiwat, UINT, 0 },
99 { "lowat", &info.lowat, UINT, 0 },
100 { "monitor_gain", &info.monitor_gain, UINT, 0 },
101 { "mode", &info.mode, P_R, READONLY },
102 { "play", &info.play, FORMAT, ALIAS },
103 { "play.rate", &info.play.sample_rate, UINT, 0 },
104 { "play.sample_rate", &info.play.sample_rate, UINT, ALIAS },
105 { "play.channels", &info.play.channels, UINT, 0 },
106 { "play.precision", &info.play.precision, UINT, 0 },
107 { "play.encoding", &info.play.encoding, ENC, 0 },
108 { "play.gain", &info.play.gain, UINT, 0 },
109 { "play.balance", &info.play.balance, UCHAR, 0 },
110 { "play.port", &info.play.port, XINT, 0 },
111 { "play.avail_ports", &info.play.avail_ports, XINT, 0 },
112 { "play.seek", &info.play.seek, UINT, READONLY },
113 { "play.samples", &info.play.samples, UINT, READONLY },
114 { "play.eof", &info.play.eof, UINT, READONLY },
115 { "play.pause", &info.play.pause, UCHAR, 0 },
116 { "play.error", &info.play.error, UCHAR, READONLY },
117 { "play.waiting", &info.play.waiting, UCHAR, READONLY },
118 { "play.open", &info.play.open, UCHAR, READONLY },
119 { "play.active", &info.play.active, UCHAR, READONLY },
120 { "play.buffer_size", &info.play.buffer_size, UINT, 0 },
121 { "record", &info.record, FORMAT, ALIAS },
122 { "record.rate", &info.record.sample_rate,UINT, 0 },
123 { "record.sample_rate", &info.record.sample_rate,UINT, ALIAS },
124 { "record.channels", &info.record.channels, UINT, 0 },
125 { "record.precision", &info.record.precision, UINT, 0 },
126 { "record.encoding", &info.record.encoding, ENC, 0 },
127 { "record.gain", &info.record.gain, UINT, 0 },
128 { "record.balance", &info.record.balance, UCHAR, 0 },
129 { "record.port", &info.record.port, XINT, 0 },
130 { "record.avail_ports", &info.record.avail_ports,XINT, 0 },
131 { "record.seek", &info.record.seek, UINT, READONLY },
132 { "record.samples", &info.record.samples, UINT, READONLY },
133 { "record.eof", &info.record.eof, UINT, READONLY },
134 { "record.pause", &info.record.pause, UCHAR, 0 },
135 { "record.error", &info.record.error, UCHAR, READONLY },
136 { "record.waiting", &info.record.waiting, UCHAR, READONLY },
137 { "record.open", &info.record.open, UCHAR, READONLY },
138 { "record.active", &info.record.active, UCHAR, READONLY },
139 { "record.buffer_size", &info.record.buffer_size,UINT, 0 },
140 { "record.errors", &rerror, INT, READONLY },
141 { .name = NULL },
142 };
143
144 static const struct {
145 const char *name;
146 u_int prop;
147 } props[] = {
148 { "full_duplex", AUDIO_PROP_FULLDUPLEX },
149 { "mmap", AUDIO_PROP_MMAP },
150 { "independent", AUDIO_PROP_INDEPENDENT },
151 { .name = NULL },
152 };
153
154 static struct field *
findfield(const char * name)155 findfield(const char *name)
156 {
157 int i;
158 for (i = 0; fields[i].name; i++)
159 if (strcmp(fields[i].name, name) == 0)
160 return &fields[i];
161 return 0;
162 }
163
164 static void
prfield(const struct field * p,const char * sep)165 prfield(const struct field *p, const char *sep)
166 {
167 u_int v;
168 const char *cm, *encstr;
169 int i;
170
171 if (sep)
172 printf("%s%s", p->name, sep);
173 switch(p->format) {
174 case STRING:
175 printf("%s", (const char*)p->valp);
176 break;
177 case INT:
178 printf("%d", *(const int*)p->valp);
179 break;
180 case UINT:
181 printf("%u", *(const u_int*)p->valp);
182 break;
183 case XINT:
184 printf("0x%x", *(const u_int*)p->valp);
185 break;
186 case UCHAR:
187 printf("%u", *(const u_char*)p->valp);
188 break;
189 case ULONG:
190 printf("%lu", *(const u_long*)p->valp);
191 break;
192 case P_R:
193 v = *(const u_int*)p->valp;
194 cm = "";
195 if (v & AUMODE_PLAY) {
196 if (v & AUMODE_PLAY_ALL)
197 printf("play");
198 else
199 printf("playsync");
200 cm = ",";
201 }
202 if (v & AUMODE_RECORD)
203 printf("%srecord", cm);
204 break;
205 case ENC:
206 v = *(u_int*)p->valp;
207 encstr = audio_enc_from_val(v);
208 if (encstr)
209 printf("%s", encstr);
210 else
211 printf("%u", v);
212 break;
213 case PROPS:
214 v = *(u_int*)p->valp;
215 for (cm = "", i = 0; props[i].name; i++) {
216 if (v & props[i].prop) {
217 printf("%s%s", cm, props[i].name);
218 cm = ",";
219 }
220 }
221 break;
222 case FORMAT:
223 prfield(p + 1, 0);
224 printf(",");
225 prfield(p + 3, 0);
226 printf(",");
227 prfield(p + 4, 0);
228 printf(",");
229 prfield(p + 5, 0);
230 break;
231 default:
232 errx(1, "Invalid print format.");
233 }
234 }
235
236 static void
rdfield(struct field * p,char * q)237 rdfield(struct field *p, char *q)
238 {
239 int enc;
240 u_int u;
241 char *s;
242
243 switch(p->format) {
244 case UINT:
245 if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
246 errx(1, "Bad number: %s", q);
247 break;
248 case UCHAR:
249 if (sscanf(q, "%u", &u) != 1)
250 errx(1, "Bad number: %s", q);
251 else
252 *(u_char *)p->valp = u;
253 break;
254 case XINT:
255 if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
256 sscanf(q, "%x", (unsigned int *)p->valp) != 1)
257 errx(1, "Bad number: %s", q);
258 break;
259 case ENC:
260 enc = audio_enc_to_val(q);
261 if (enc >= 0)
262 *(u_int*)p->valp = enc;
263 else
264 errx(1, "Unknown encoding: %s", q);
265 break;
266 case FORMAT:
267 s = strsep(&q, ",");
268 if (s)
269 rdfield(p + 1, s);
270 s = strsep(&q, ",");
271 if (s)
272 rdfield(p + 3, s);
273 s = strsep(&q, ",");
274 if (s)
275 rdfield(p + 4, s);
276 s = strsep(&q, ",");
277 if (s)
278 rdfield(p + 5, s);
279 if (!s || q)
280 errx(1, "Bad format");
281 break;
282 default:
283 errx(1, "Invalid read format.");
284 }
285 p->flags |= SET;
286 }
287
288 static void
getinfo(int fd)289 getinfo(int fd)
290 {
291 int pos, i;
292
293 if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
294 err(1, "AUDIO_GETDEV");
295 for (pos = 0, i = 0; ; i++) {
296 audio_encoding_t enc;
297 enc.index = i;
298 if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
299 break;
300 if (pos >= (int)sizeof(encbuf)-1)
301 break;
302 if (pos)
303 encbuf[pos++] = ',';
304 if (pos >= (int)sizeof(encbuf)-1)
305 break;
306 pos += snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d%s",
307 enc.name, enc.precision,
308 enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
309 }
310 if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
311 err(1, "AUDIO_GETFD");
312 if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
313 err(1, "AUDIO_GETPROPS");
314 if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
315 err(1, "AUDIO_RERROR");
316 if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
317 err(1, "AUDIO_GETINFO");
318 }
319
320 static void
usage(void)321 usage(void)
322 {
323 const char *prog = getprogname();
324
325 fprintf(stderr, "Usage: %s [-d file] [-n] name ...\n", prog);
326 fprintf(stderr, "Usage: %s [-d file] [-n] -w name=value ...\n", prog);
327 fprintf(stderr, "Usage: %s [-d file] [-n] -a\n", prog);
328 exit(1);
329 }
330
331 int
main(int argc,char * argv[])332 main(int argc, char *argv[])
333 {
334 int fd, i, ch;
335 int aflag = 0, wflag = 0;
336 const char *deffile = _PATH_AUDIOCTL;
337 const char *file;
338 const char *sep = "=";
339
340 file = getenv("AUDIOCTLDEVICE");
341 if (file == NULL)
342 file = deffile;
343
344 while ((ch = getopt(argc, argv, "ad:f:nw")) != -1) {
345 switch(ch) {
346 case 'a':
347 aflag++;
348 break;
349 case 'w':
350 wflag++;
351 break;
352 case 'n':
353 sep = 0;
354 break;
355 case 'f': /* compatibility */
356 case 'd':
357 file = optarg;
358 break;
359 case '?':
360 default:
361 usage();
362 }
363 }
364 argc -= optind;
365 argv += optind;
366
367 fd = open(file, O_WRONLY);
368 if (fd < 0)
369 fd = open(file, O_RDONLY);
370 if (fd < 0 && file == deffile) {
371 file = _PATH_AUDIOCTL0;
372 fd = open(file, O_WRONLY);
373 if (fd < 0)
374 fd = open(file, O_RDONLY);
375 }
376
377 if (fd < 0)
378 err(1, "%s", file);
379
380 if (!wflag)
381 getinfo(fd);
382
383 if (argc == 0 && aflag && !wflag) {
384 for (i = 0; fields[i].name; i++) {
385 if (!(fields[i].flags & ALIAS)) {
386 prfield(&fields[i], sep);
387 printf("\n");
388 }
389 }
390 } else if (argc > 0 && !aflag) {
391 if (wflag) {
392 audioctl_write(fd, argc, argv);
393 if (sep) {
394 getinfo(fd);
395 for (i = 0; fields[i].name; i++) {
396 if (fields[i].flags & SET) {
397 printf("%s: -> ",
398 fields[i].name);
399 prfield(&fields[i], 0);
400 printf("\n");
401 }
402 }
403 }
404 } else {
405 struct field *p;
406 while (argc--) {
407 p = findfield(*argv);
408 if (p == 0) {
409 if (strchr(*argv, '='))
410 warnx("field %s does not exist "
411 "(use -w to set a "
412 "variable)", *argv);
413 else
414 warnx("field %s does not exist",
415 *argv);
416 } else {
417 prfield(p, sep);
418 printf("\n");
419 }
420 argv++;
421 }
422 }
423 } else
424 usage();
425 exit(0);
426 }
427
428 static void
audioctl_write(int fd,int argc,char * argv[])429 audioctl_write(int fd, int argc, char *argv[])
430 {
431 struct field *p;
432
433 AUDIO_INITINFO(&info);
434 while (argc--) {
435 char *q;
436
437 q = strchr(*argv, '=');
438 if (q) {
439 *q++ = 0;
440 p = findfield(*argv);
441 if (p == 0)
442 warnx("field `%s' does not exist", *argv);
443 else {
444 if (p->flags & READONLY)
445 warnx("`%s' is read only", *argv);
446 else {
447 rdfield(p, q);
448 if (p->valp == &fullduplex)
449 if (ioctl(fd, AUDIO_SETFD,
450 &fullduplex) < 0)
451 err(1, "set failed");
452 }
453 }
454 } else
455 warnx("No `=' in %s", *argv);
456 argv++;
457 }
458 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
459 err(1, "set failed");
460 }
461