ctl.c revision 1.19 1 /* $NetBSD: ctl.c,v 1.19 1998/08/21 19:45:37 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Author: Lennart Augustsson
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <fcntl.h>
41 #include <err.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/ioctl.h>
47 #include <sys/audioio.h>
48
49 #define AUDIOCTL "/dev/audioctl0"
50 #define OLD_AUDIOCTL "/dev/audioctl"
51
52 struct field *findfield __P((char *name));
53 void prfield __P((struct field *p, char *sep));
54 void rdfield __P((struct field *p, char *q));
55 void getinfo __P((int fd));
56 void usage __P((void));
57 int main __P((int argc, char **argv));
58
59 FILE *out = stdout;
60
61 char *prog;
62
63 audio_device_t adev;
64
65 audio_info_t info;
66
67 char encbuf[1000];
68
69 int properties, fullduplex, rerror;
70
71 struct field {
72 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 { 0 }
142 };
143
144 struct {
145 char *ename;
146 int eno;
147 } encs[] = {
148 { AudioEmulaw, AUDIO_ENCODING_ULAW },
149 { "ulaw", AUDIO_ENCODING_ULAW },
150 { AudioEalaw, AUDIO_ENCODING_ALAW },
151 { AudioEslinear, AUDIO_ENCODING_SLINEAR },
152 { "linear", AUDIO_ENCODING_SLINEAR },
153 { AudioEulinear, AUDIO_ENCODING_ULINEAR },
154 { AudioEadpcm, AUDIO_ENCODING_ADPCM },
155 { "ADPCM", AUDIO_ENCODING_ADPCM },
156 { AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE },
157 { "linear_le", AUDIO_ENCODING_SLINEAR_LE },
158 { AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE },
159 { AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE },
160 { "linear_be", AUDIO_ENCODING_SLINEAR_BE },
161 { AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE },
162 { AudioEmpeg_l1_stream, AUDIO_ENCODING_MPEG_L1_STREAM },
163 { AudioEmpeg_l1_packets,AUDIO_ENCODING_MPEG_L1_PACKETS },
164 { AudioEmpeg_l1_system, AUDIO_ENCODING_MPEG_L1_SYSTEM },
165 { AudioEmpeg_l2_stream, AUDIO_ENCODING_MPEG_L2_STREAM },
166 { AudioEmpeg_l2_packets,AUDIO_ENCODING_MPEG_L2_PACKETS },
167 { AudioEmpeg_l2_system, AUDIO_ENCODING_MPEG_L2_SYSTEM },
168 { 0 }
169 };
170
171 static struct {
172 char *name;
173 u_int prop;
174 } props[] = {
175 { "full_duplex", AUDIO_PROP_FULLDUPLEX },
176 { "mmap", AUDIO_PROP_MMAP },
177 { "independent", AUDIO_PROP_INDEPENDENT },
178 { 0 }
179 };
180
181 struct field *
182 findfield(name)
183 char *name;
184 {
185 int i;
186 for(i = 0; fields[i].name; i++)
187 if (strcmp(fields[i].name, name) == 0)
188 return &fields[i];
189 return 0;
190 }
191
192 void
193 prfield(p, sep)
194 struct field *p;
195 char *sep;
196 {
197 u_int v;
198 char *cm;
199 int i;
200
201 if (sep)
202 fprintf(out, "%s%s", p->name, sep);
203 switch(p->format) {
204 case STRING:
205 fprintf(out, "%s", (char*)p->valp);
206 break;
207 case INT:
208 fprintf(out, "%d", *(int*)p->valp);
209 break;
210 case UINT:
211 fprintf(out, "%u", *(u_int*)p->valp);
212 break;
213 case XINT:
214 fprintf(out, "0x%x", *(u_int*)p->valp);
215 break;
216 case UCHAR:
217 fprintf(out, "%u", *(u_char*)p->valp);
218 break;
219 case ULONG:
220 fprintf(out, "%lu", *(u_long*)p->valp);
221 break;
222 case P_R:
223 v = *(u_int*)p->valp;
224 cm = "";
225 if (v & AUMODE_PLAY) {
226 if (v & AUMODE_PLAY_ALL)
227 fprintf(out, "play");
228 else
229 fprintf(out, "playsync");
230 cm = ",";
231 }
232 if (v & AUMODE_RECORD)
233 fprintf(out, "%srecord", cm);
234 break;
235 case ENC:
236 v = *(u_int*)p->valp;
237 for(i = 0; encs[i].ename; i++)
238 if (encs[i].eno == v)
239 break;
240 if (encs[i].ename)
241 fprintf(out, "%s", encs[i].ename);
242 else
243 fprintf(out, "%u", v);
244 break;
245 case PROPS:
246 v = *(u_int*)p->valp;
247 for (cm = "", i = 0; props[i].name; i++) {
248 if (v & props[i].prop) {
249 fprintf(out, "%s%s", cm, props[i].name);
250 cm = ",";
251 }
252 }
253 break;
254 case FORMAT:
255 prfield(p + 1, 0);
256 fprintf(out, ",");
257 prfield(p + 3, 0);
258 fprintf(out, ",");
259 prfield(p + 4, 0);
260 fprintf(out, ",");
261 prfield(p + 5, 0);
262 break;
263 default:
264 errx(1, "Invalid print format.");
265 }
266 }
267
268 void
269 rdfield(p, q)
270 struct field *p;
271 char *q;
272 {
273 int i;
274 u_int u;
275 char *s;
276
277 switch(p->format) {
278 case UINT:
279 if (sscanf(q, "%u", (unsigned int *)p->valp) != 1)
280 errx(1, "Bad number: %s", q);
281 break;
282 case UCHAR:
283 if (sscanf(q, "%u", &u) != 1)
284 errx(1, "Bad number: %s", q);
285 else
286 *(u_char *)p->valp = u;
287 break;
288 case XINT:
289 if (sscanf(q, "0x%x", (unsigned int *)p->valp) != 1 &&
290 sscanf(q, "%x", (unsigned int *)p->valp) != 1)
291 errx(1, "Bad number: %s", q);
292 break;
293 case ENC:
294 for(i = 0; encs[i].ename; i++)
295 if (strcmp(encs[i].ename, q) == 0)
296 break;
297 if (encs[i].ename)
298 *(u_int*)p->valp = encs[i].eno;
299 else
300 errx(1, "Unknown encoding: %s", q);
301 break;
302 case FORMAT:
303 s = strsep(&q, ",");
304 if (s)
305 rdfield(p + 1, s);
306 s = strsep(&q, ",");
307 if (s)
308 rdfield(p + 3, s);
309 s = strsep(&q, ",");
310 if (s)
311 rdfield(p + 4, s);
312 s = strsep(&q, ",");
313 if (s)
314 rdfield(p + 5, s);
315 if (!s || q)
316 errx(1, "Bad format");
317 break;
318 default:
319 errx(1, "Invalid read format.");
320 }
321 p->flags |= SET;
322 }
323
324 void
325 getinfo(fd)
326 int fd;
327 {
328 int pos, i;
329
330 if (ioctl(fd, AUDIO_GETDEV, &adev) < 0)
331 err(1, "AUDIO_GETDEV");
332 for (pos = 0, i = 0; ; i++) {
333 audio_encoding_t enc;
334 enc.index = i;
335 if (ioctl(fd, AUDIO_GETENC, &enc) < 0)
336 break;
337 if (pos >= sizeof(encbuf)-1)
338 break;
339 if (pos)
340 encbuf[pos++] = ',';
341 if (pos >= sizeof(encbuf)-1)
342 break;
343 pos += snprintf(encbuf+pos, sizeof(encbuf)-pos, "%s:%d%s",
344 enc.name, enc.precision,
345 enc.flags & AUDIO_ENCODINGFLAG_EMULATED ? "*" : "");
346 }
347 if (ioctl(fd, AUDIO_GETFD, &fullduplex) < 0)
348 err(1, "AUDIO_GETFD");
349 if (ioctl(fd, AUDIO_GETPROPS, &properties) < 0)
350 err(1, "AUDIO_GETPROPS");
351 if (ioctl(fd, AUDIO_RERROR, &rerror) < 0)
352 err(1, "AUDIO_RERROR");
353 if (ioctl(fd, AUDIO_GETINFO, &info) < 0)
354 err(1, "AUDIO_GETINFO");
355 }
356
357 void
358 usage()
359 {
360 fprintf(out, "%s [-f file] [-n] name ...\n", prog);
361 fprintf(out, "%s [-f file] [-n] -w name=value ...\n", prog);
362 fprintf(out, "%s [-f file] [-n] -a\n", prog);
363 exit(1);
364 }
365
366 int
367 main(argc, argv)
368 int argc;
369 char **argv;
370 {
371 int fd, i, ch;
372 int aflag = 0, wflag = 0;
373 struct stat dstat, ostat;
374 const char *file;
375 char *sep = "=";
376
377 file = getenv("AUDIOCTLDEVICE");
378 if (file == 0)
379 file = AUDIOCTL;
380
381 prog = *argv;
382
383 while ((ch = getopt(argc, argv, "af:nw")) != -1) {
384 switch(ch) {
385 case 'a':
386 aflag++;
387 break;
388 case 'w':
389 wflag++;
390 break;
391 case 'n':
392 sep = 0;
393 break;
394 case 'f':
395 file = optarg;
396 break;
397 case '?':
398 default:
399 usage();
400 }
401 }
402 argc -= optind;
403 argv += optind;
404
405 fd = open(file, O_WRONLY);
406 if (fd < 0)
407 fd = open(file, O_RDONLY);
408 #ifdef OLD_AUDIOCTL
409 /* Allow the non-unit device to be used. */
410 if (file == AUDIOCTL) {
411 file = OLD_AUDIOCTL;
412 fd = open(file, O_WRONLY);
413 if (fd < 0)
414 fd = open(file, O_RDONLY);
415 }
416 #endif
417 if (fd < 0)
418 err(1, "%s", file);
419
420 /* Check if stdout is the same device as the audio device. */
421 if (fstat(fd, &dstat) < 0)
422 err(1, "fstat au");
423 if (fstat(STDOUT_FILENO, &ostat) < 0)
424 err(1, "fstat stdout");
425 if (S_ISCHR(dstat.st_mode) && S_ISCHR(ostat.st_mode) &&
426 major(dstat.st_dev) == major(ostat.st_dev) &&
427 minor(dstat.st_dev) == minor(ostat.st_dev))
428 /* We can't write to stdout so use stderr */
429 out = stderr;
430
431 if (!wflag)
432 getinfo(fd);
433
434 if (argc == 0 && aflag && !wflag) {
435 for(i = 0; fields[i].name; i++) {
436 if (!(fields[i].flags & ALIAS)) {
437 prfield(&fields[i], sep);
438 fprintf(out, "\n");
439 }
440 }
441 } else if (argc > 0 && !aflag) {
442 struct field *p;
443 if (wflag) {
444 AUDIO_INITINFO(&info);
445 while(argc--) {
446 char *q;
447
448 q = strchr(*argv, '=');
449 if (q) {
450 *q++ = 0;
451 p = findfield(*argv);
452 if (p == 0)
453 warnx("field `%s' does not exist", *argv);
454 else {
455 if (p->flags & READONLY)
456 warnx("`%s' is read only", *argv);
457 else {
458 rdfield(p, q);
459 if (p->valp == &fullduplex)
460 if (ioctl(fd, AUDIO_SETFD, &fullduplex) < 0)
461 err(1, "set failed");
462 }
463 }
464 } else
465 warnx("No `=' in %s", *argv);
466 argv++;
467 }
468 if (ioctl(fd, AUDIO_SETINFO, &info) < 0)
469 err(1, "set failed");
470 if (sep) {
471 getinfo(fd);
472 for(i = 0; fields[i].name; i++) {
473 if (fields[i].flags & SET) {
474 fprintf(out, "%s: -> ", fields[i].name);
475 prfield(&fields[i], 0);
476 fprintf(out, "\n");
477 }
478 }
479 }
480 } else {
481 while(argc--) {
482 p = findfield(*argv);
483 if (p == 0) {
484 if (strchr(*argv, '='))
485 warnx("field %s does not exist (use -w to set a variable)", *argv);
486 else
487 warnx("field %s does not exist", *argv);
488 } else {
489 prfield(p, sep);
490 fprintf(out, "\n");
491 }
492 argv++;
493 }
494 }
495 } else
496 usage();
497 exit(0);
498 }
499