1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 1989-2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * This file contains routines to read and write the audio device state. 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <errno.h> 34*7c478bd9Sstevel@tonic-gate #include <stropts.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/file.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #include <libaudio_impl.h> 41*7c478bd9Sstevel@tonic-gate #include <audio_errno.h> 42*7c478bd9Sstevel@tonic-gate #include <audio_hdr.h> 43*7c478bd9Sstevel@tonic-gate #include <audio_device.h> 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate /* 47*7c478bd9Sstevel@tonic-gate * Get device information structure. 48*7c478bd9Sstevel@tonic-gate */ 49*7c478bd9Sstevel@tonic-gate int 50*7c478bd9Sstevel@tonic-gate audio_getinfo(int fd, Audio_info *ip) 51*7c478bd9Sstevel@tonic-gate { 52*7c478bd9Sstevel@tonic-gate if (ioctl(fd, AUDIO_GETINFO, (char *)ip) < 0) { 53*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR); 54*7c478bd9Sstevel@tonic-gate } else { 55*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS); 56*7c478bd9Sstevel@tonic-gate } 57*7c478bd9Sstevel@tonic-gate } 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate /* 60*7c478bd9Sstevel@tonic-gate * Set device information structure. 61*7c478bd9Sstevel@tonic-gate * The calling routine should use AUDIO_INITINFO prior to setting new values. 62*7c478bd9Sstevel@tonic-gate */ 63*7c478bd9Sstevel@tonic-gate int 64*7c478bd9Sstevel@tonic-gate audio_setinfo(int fd, Audio_info *ip) 65*7c478bd9Sstevel@tonic-gate { 66*7c478bd9Sstevel@tonic-gate if (ioctl(fd, AUDIO_SETINFO, (char *)ip) < 0) { 67*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR); 68*7c478bd9Sstevel@tonic-gate } else { 69*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS); 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate } 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* 74*7c478bd9Sstevel@tonic-gate * Return an Audio_hdr corresponding to the encoding configuration 75*7c478bd9Sstevel@tonic-gate * of the audio device on 'fd'. 76*7c478bd9Sstevel@tonic-gate */ 77*7c478bd9Sstevel@tonic-gate int 78*7c478bd9Sstevel@tonic-gate audio__setplayhdr(int fd, Audio_hdr *hdrp, unsigned int which) 79*7c478bd9Sstevel@tonic-gate { 80*7c478bd9Sstevel@tonic-gate Audio_hdr thdr; 81*7c478bd9Sstevel@tonic-gate Audio_info info; 82*7c478bd9Sstevel@tonic-gate struct audio_prinfo *prinfo; 83*7c478bd9Sstevel@tonic-gate int err; 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate if (which & AUDIO__PLAY) 86*7c478bd9Sstevel@tonic-gate prinfo = &info.play; 87*7c478bd9Sstevel@tonic-gate else if (which & AUDIO__RECORD) 88*7c478bd9Sstevel@tonic-gate prinfo = &info.record; 89*7c478bd9Sstevel@tonic-gate else 90*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG); 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate if (which & AUDIO__SET) { 93*7c478bd9Sstevel@tonic-gate thdr = *hdrp; /* save original hdr */ 94*7c478bd9Sstevel@tonic-gate AUDIO_INITINFO(&info); 95*7c478bd9Sstevel@tonic-gate prinfo->sample_rate = hdrp->sample_rate; 96*7c478bd9Sstevel@tonic-gate prinfo->channels = hdrp->channels; 97*7c478bd9Sstevel@tonic-gate prinfo->encoding = hdrp->encoding; 98*7c478bd9Sstevel@tonic-gate prinfo->precision = hdrp->bytes_per_unit * 8; 99*7c478bd9Sstevel@tonic-gate err = audio_setinfo(fd, &info); 100*7c478bd9Sstevel@tonic-gate } else { 101*7c478bd9Sstevel@tonic-gate err = audio_getinfo(fd, &info); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate /* Decode back into the header structure */ 105*7c478bd9Sstevel@tonic-gate /* since the I_SMSG is set, upon completion of updating */ 106*7c478bd9Sstevel@tonic-gate /* the format, the driver sends the msg so the */ 107*7c478bd9Sstevel@tonic-gate /* system call is interrupted. For now, just ignore this */ 108*7c478bd9Sstevel@tonic-gate if ((err == AUDIO_SUCCESS) || (errno == EINTR)) { 109*7c478bd9Sstevel@tonic-gate hdrp->sample_rate = prinfo->sample_rate; 110*7c478bd9Sstevel@tonic-gate hdrp->channels = prinfo->channels; 111*7c478bd9Sstevel@tonic-gate hdrp->data_size = AUDIO_UNKNOWN_SIZE; 112*7c478bd9Sstevel@tonic-gate hdrp->encoding = prinfo->encoding; 113*7c478bd9Sstevel@tonic-gate switch (hdrp->encoding) { 114*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ULAW: 115*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_ALAW: 116*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR: 117*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_LINEAR8: 118*7c478bd9Sstevel@tonic-gate case AUDIO_ENCODING_FLOAT: 119*7c478bd9Sstevel@tonic-gate hdrp->bytes_per_unit = prinfo->precision / 8; 120*7c478bd9Sstevel@tonic-gate hdrp->samples_per_unit = 1; 121*7c478bd9Sstevel@tonic-gate break; 122*7c478bd9Sstevel@tonic-gate default: 123*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_ENCODING); 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate if (which & AUDIO__SET) { 126*7c478bd9Sstevel@tonic-gate /* Check to see if *all* changes took effect */ 127*7c478bd9Sstevel@tonic-gate if (audio_cmp_hdr(hdrp, &thdr) != 0) 128*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT); 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate return (err); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /* 136*7c478bd9Sstevel@tonic-gate * Attempt to configure the audio device to match a particular encoding. 137*7c478bd9Sstevel@tonic-gate */ 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate /* 140*7c478bd9Sstevel@tonic-gate * Set and/or set individual state values. 141*7c478bd9Sstevel@tonic-gate * This routine is generally invoked by using the audio_set_*() 142*7c478bd9Sstevel@tonic-gate * and audio_get_*() macros. 143*7c478bd9Sstevel@tonic-gate * The 'valp' argument is always a pointer to an unsigned int. 144*7c478bd9Sstevel@tonic-gate * Conversions to/from (unsigned char) flags are taken care of. 145*7c478bd9Sstevel@tonic-gate */ 146*7c478bd9Sstevel@tonic-gate int 147*7c478bd9Sstevel@tonic-gate audio__setval(int fd, unsigned int *valp, unsigned int which) 148*7c478bd9Sstevel@tonic-gate { 149*7c478bd9Sstevel@tonic-gate Audio_info info; 150*7c478bd9Sstevel@tonic-gate struct audio_prinfo *prinfo; 151*7c478bd9Sstevel@tonic-gate int err; 152*7c478bd9Sstevel@tonic-gate unsigned *up; 153*7c478bd9Sstevel@tonic-gate unsigned char *cp; 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate /* Set a pointer to the value of interest */ 156*7c478bd9Sstevel@tonic-gate if (which & AUDIO__PLAY) 157*7c478bd9Sstevel@tonic-gate prinfo = &info.play; 158*7c478bd9Sstevel@tonic-gate else if (which & AUDIO__RECORD) 159*7c478bd9Sstevel@tonic-gate prinfo = &info.record; 160*7c478bd9Sstevel@tonic-gate else if ((which & AUDIO__SETVAL_MASK) != AUDIO__MONGAIN) 161*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG); 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate up = NULL; 164*7c478bd9Sstevel@tonic-gate switch (which & AUDIO__SETVAL_MASK) { 165*7c478bd9Sstevel@tonic-gate case AUDIO__PORT: 166*7c478bd9Sstevel@tonic-gate up = &prinfo->port; break; 167*7c478bd9Sstevel@tonic-gate case AUDIO__SAMPLES: 168*7c478bd9Sstevel@tonic-gate up = &prinfo->samples; break; 169*7c478bd9Sstevel@tonic-gate case AUDIO__ERROR: 170*7c478bd9Sstevel@tonic-gate cp = &prinfo->error; break; 171*7c478bd9Sstevel@tonic-gate case AUDIO__EOF: 172*7c478bd9Sstevel@tonic-gate up = &prinfo->eof; break; 173*7c478bd9Sstevel@tonic-gate case AUDIO__OPEN: 174*7c478bd9Sstevel@tonic-gate cp = &prinfo->open; break; 175*7c478bd9Sstevel@tonic-gate case AUDIO__ACTIVE: 176*7c478bd9Sstevel@tonic-gate cp = &prinfo->active; break; 177*7c478bd9Sstevel@tonic-gate case AUDIO__WAITING: 178*7c478bd9Sstevel@tonic-gate cp = &prinfo->waiting; break; 179*7c478bd9Sstevel@tonic-gate case AUDIO__GAIN: 180*7c478bd9Sstevel@tonic-gate up = &prinfo->gain; break; 181*7c478bd9Sstevel@tonic-gate case AUDIO__MONGAIN: 182*7c478bd9Sstevel@tonic-gate up = &info.monitor_gain; break; 183*7c478bd9Sstevel@tonic-gate case AUDIO__BALANCE: 184*7c478bd9Sstevel@tonic-gate cp = &prinfo->balance; break; 185*7c478bd9Sstevel@tonic-gate default: 186*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG); 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate if (which & AUDIO__SET) { 190*7c478bd9Sstevel@tonic-gate /* Init so that only the value of interest is changed */ 191*7c478bd9Sstevel@tonic-gate AUDIO_INITINFO(&info); 192*7c478bd9Sstevel@tonic-gate if (up != NULL) { 193*7c478bd9Sstevel@tonic-gate *up = *valp; 194*7c478bd9Sstevel@tonic-gate } else { 195*7c478bd9Sstevel@tonic-gate *cp = (unsigned char) *valp; 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate err = audio_setinfo(fd, &info); 198*7c478bd9Sstevel@tonic-gate } else { 199*7c478bd9Sstevel@tonic-gate err = audio_getinfo(fd, &info); 200*7c478bd9Sstevel@tonic-gate } 201*7c478bd9Sstevel@tonic-gate if (err == AUDIO_SUCCESS) { 202*7c478bd9Sstevel@tonic-gate if (up != NULL) 203*7c478bd9Sstevel@tonic-gate *valp = *up; 204*7c478bd9Sstevel@tonic-gate else 205*7c478bd9Sstevel@tonic-gate *valp = (unsigned)*cp; 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate return (err); 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate 210*7c478bd9Sstevel@tonic-gate /* 211*7c478bd9Sstevel@tonic-gate * Get/set gain value. 212*7c478bd9Sstevel@tonic-gate * NOTE: legal values are floating-point double 0. - 1. 213*7c478bd9Sstevel@tonic-gate */ 214*7c478bd9Sstevel@tonic-gate int 215*7c478bd9Sstevel@tonic-gate audio__setgain(int fd, double *valp, unsigned int which) 216*7c478bd9Sstevel@tonic-gate { 217*7c478bd9Sstevel@tonic-gate int err; 218*7c478bd9Sstevel@tonic-gate unsigned x; 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate if (which & AUDIO__SET) { 221*7c478bd9Sstevel@tonic-gate if ((*valp < 0.) || (*valp > 1.)) 222*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG); 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate /* Map value into legal range */ 225*7c478bd9Sstevel@tonic-gate x = ((unsigned)(*valp * (AUDIO_MAX_GAIN - AUDIO_MIN_GAIN))) + 226*7c478bd9Sstevel@tonic-gate AUDIO_MIN_GAIN; 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate /* Get or set the new value */ 230*7c478bd9Sstevel@tonic-gate err = audio__setval(fd, &x, which); 231*7c478bd9Sstevel@tonic-gate if (err == AUDIO_SUCCESS) { 232*7c478bd9Sstevel@tonic-gate /* Map value back to double */ 233*7c478bd9Sstevel@tonic-gate *valp = ((double)(x - AUDIO_MIN_GAIN) / 234*7c478bd9Sstevel@tonic-gate (AUDIO_MAX_GAIN - AUDIO_MIN_GAIN)); 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate return (err); 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate /* 240*7c478bd9Sstevel@tonic-gate * Set Pause/resume flags. 241*7c478bd9Sstevel@tonic-gate * Can set play and record individually or together. 242*7c478bd9Sstevel@tonic-gate */ 243*7c478bd9Sstevel@tonic-gate int 244*7c478bd9Sstevel@tonic-gate audio__setpause(int fd, unsigned int which) 245*7c478bd9Sstevel@tonic-gate { 246*7c478bd9Sstevel@tonic-gate Audio_info info; 247*7c478bd9Sstevel@tonic-gate int err; 248*7c478bd9Sstevel@tonic-gate unsigned char x; 249*7c478bd9Sstevel@tonic-gate 250*7c478bd9Sstevel@tonic-gate AUDIO_INITINFO(&info); 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate if ((which & AUDIO__SETVAL_MASK) == AUDIO__PAUSE) { 253*7c478bd9Sstevel@tonic-gate x = TRUE; 254*7c478bd9Sstevel@tonic-gate } else if ((which & AUDIO__SETVAL_MASK) == AUDIO__RESUME) { 255*7c478bd9Sstevel@tonic-gate x = FALSE; 256*7c478bd9Sstevel@tonic-gate } else { 257*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_BADARG); 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate if (which & AUDIO__PLAY) { 261*7c478bd9Sstevel@tonic-gate info.play.pause = x; 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate if (which & AUDIO__RECORD) { 264*7c478bd9Sstevel@tonic-gate info.record.pause = x; 265*7c478bd9Sstevel@tonic-gate } 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate /* Set the new value */ 268*7c478bd9Sstevel@tonic-gate err = audio_setinfo(fd, &info); 269*7c478bd9Sstevel@tonic-gate 270*7c478bd9Sstevel@tonic-gate /* Check to see if this took effect */ 271*7c478bd9Sstevel@tonic-gate if (err == AUDIO_SUCCESS) { 272*7c478bd9Sstevel@tonic-gate if (((which & AUDIO__PLAY) && (info.play.pause != x)) || 273*7c478bd9Sstevel@tonic-gate ((which & AUDIO__RECORD) && (info.record.pause != x))) 274*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_NOEFFECT); 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate return (err); 277*7c478bd9Sstevel@tonic-gate } 278*7c478bd9Sstevel@tonic-gate 279*7c478bd9Sstevel@tonic-gate 280*7c478bd9Sstevel@tonic-gate /* 281*7c478bd9Sstevel@tonic-gate * Flush play and/or record buffers. 282*7c478bd9Sstevel@tonic-gate */ 283*7c478bd9Sstevel@tonic-gate int 284*7c478bd9Sstevel@tonic-gate audio__flush(int fd, unsigned int which) 285*7c478bd9Sstevel@tonic-gate { 286*7c478bd9Sstevel@tonic-gate int flag; 287*7c478bd9Sstevel@tonic-gate 288*7c478bd9Sstevel@tonic-gate flag = (which & AUDIO__PLAY) ? FLUSHW : 0; 289*7c478bd9Sstevel@tonic-gate flag |= (which & AUDIO__RECORD) ? FLUSHR : 0; 290*7c478bd9Sstevel@tonic-gate 291*7c478bd9Sstevel@tonic-gate return ((ioctl(fd, I_FLUSH, flag) < 0) ? 292*7c478bd9Sstevel@tonic-gate AUDIO_UNIXERROR : AUDIO_SUCCESS); 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate /* 297*7c478bd9Sstevel@tonic-gate * Wait synchronously for output buffers to finish playing. 298*7c478bd9Sstevel@tonic-gate * If 'sig' is TRUE, signals may interrupt the drain. 299*7c478bd9Sstevel@tonic-gate */ 300*7c478bd9Sstevel@tonic-gate int 301*7c478bd9Sstevel@tonic-gate audio_drain(int fd, int sig) 302*7c478bd9Sstevel@tonic-gate { 303*7c478bd9Sstevel@tonic-gate while (ioctl(fd, AUDIO_DRAIN, 0) < 0) { 304*7c478bd9Sstevel@tonic-gate if (errno != EINTR) { 305*7c478bd9Sstevel@tonic-gate return (AUDIO_UNIXERROR); 306*7c478bd9Sstevel@tonic-gate } 307*7c478bd9Sstevel@tonic-gate 308*7c478bd9Sstevel@tonic-gate if (sig) { 309*7c478bd9Sstevel@tonic-gate return (AUDIO_ERR_INTERRUPTED); 310*7c478bd9Sstevel@tonic-gate } 311*7c478bd9Sstevel@tonic-gate } 312*7c478bd9Sstevel@tonic-gate return (AUDIO_SUCCESS); 313*7c478bd9Sstevel@tonic-gate } 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate /* 316*7c478bd9Sstevel@tonic-gate * Write an EOF marker to the output audio stream. 317*7c478bd9Sstevel@tonic-gate */ 318*7c478bd9Sstevel@tonic-gate int 319*7c478bd9Sstevel@tonic-gate audio_play_eof(int fd) 320*7c478bd9Sstevel@tonic-gate { 321*7c478bd9Sstevel@tonic-gate return (write(fd, (char *)NULL, 0) < 0 ? 322*7c478bd9Sstevel@tonic-gate AUDIO_UNIXERROR : AUDIO_SUCCESS); 323*7c478bd9Sstevel@tonic-gate } 324