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