1 /* 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 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$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/kernel.h> 32 33 #include <dev/pcm/sound.h> 34 35 static int getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch); 36 37 static pcm_channel * 38 allocchn(snddev_info *d, int direction) 39 { 40 pcm_channel *chns = (direction == PCMDIR_PLAY)? d->play : d->rec; 41 int i, cnt = (direction == PCMDIR_PLAY)? d->playcount : d->reccount; 42 for (i = 0; i < cnt; i++) { 43 if (!(chns[i].flags & CHN_F_BUSY)) { 44 chns[i].flags |= CHN_F_BUSY; 45 return &chns[i]; 46 } 47 } 48 return NULL; 49 } 50 51 static int 52 getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch) 53 { 54 if ((d->flags & SD_F_PRIO_SET) == SD_F_PRIO_SET) 55 panic("read and write both prioritised"); 56 if (d->flags & SD_F_SIMPLEX) { 57 *rdch = (d->flags & SD_F_PRIO_RD)? d->arec[chan] : &d->fakechan; 58 *wrch = (d->flags & SD_F_PRIO_WR)? d->aplay[chan] : &d->fakechan; 59 } else { 60 *rdch = d->arec[chan]; 61 *wrch = d->aplay[chan]; 62 } 63 return 0; 64 } 65 66 static void 67 setchns(snddev_info *d, int chan) 68 { 69 if ((d->flags & SD_F_PRIO_SET) == SD_F_PRIO_SET) 70 panic("read and write both prioritised"); 71 d->flags |= SD_F_DIR_SET; 72 if (d->flags & SD_F_EVILSB16) { 73 if ((d->flags & SD_F_PRIO_RD) && (d->aplay[chan])) { 74 pcm_channel *tmp; 75 tmp = d->arec[chan]; 76 d->arec[chan] = d->aplay[chan]; 77 d->aplay[chan] = tmp; 78 } 79 if (d->aplay[chan]) chn_setdir(d->aplay[chan], PCMDIR_PLAY); 80 if (d->arec[chan]) chn_setdir(d->arec[chan], PCMDIR_REC); 81 } 82 } 83 84 int 85 dsp_open(snddev_info *d, int chan, int oflags, int devtype) 86 { 87 pcm_channel *rdch = NULL, *wrch = NULL; 88 u_int32_t fmt; 89 90 if (chan >= d->chancount) return ENODEV; 91 if (d->aplay[chan] || d->arec[chan]) return EBUSY; 92 if (oflags & FREAD) { 93 rdch = allocchn(d, PCMDIR_REC); 94 if (!rdch) return EBUSY; 95 } 96 if (oflags & FWRITE) { 97 wrch = allocchn(d, PCMDIR_PLAY); 98 if (!wrch) { 99 if (rdch) rdch->flags &= ~CHN_F_BUSY; 100 return EBUSY; 101 } 102 } 103 d->aplay[chan] = wrch; 104 d->arec[chan] = rdch; 105 switch (devtype) { 106 case SND_DEV_DSP16: 107 fmt = AFMT_S16_LE; 108 break; 109 110 case SND_DEV_DSP: 111 fmt = AFMT_U8; 112 break; 113 114 case SND_DEV_AUDIO: 115 fmt = AFMT_MU_LAW; 116 break; 117 118 default: 119 return ENXIO; 120 } 121 122 if (rdch) { 123 chn_reset(rdch); 124 if (oflags & O_NONBLOCK) rdch->flags |= CHN_F_NBIO; 125 rdch->volume = (100 << 8) | 100; 126 rdch->format = fmt; 127 rdch->speed = DSP_DEFAULT_SPEED; 128 rdch->blocksize = 2048; 129 } 130 if (wrch) { 131 chn_reset(wrch); 132 if (oflags & O_NONBLOCK) wrch->flags |= CHN_F_NBIO; 133 wrch->volume = (100 << 8) | 100; 134 wrch->format = fmt; 135 wrch->speed = DSP_DEFAULT_SPEED; 136 wrch->blocksize = 2048; 137 } 138 return 0; 139 } 140 141 int 142 dsp_close(snddev_info *d, int chan, int devtype) 143 { 144 pcm_channel *rdch, *wrch; 145 146 d->flags &= ~SD_F_TRANSIENT; 147 rdch = d->arec[chan]; 148 wrch = d->aplay[chan]; 149 150 if (rdch) { 151 chn_abort(rdch); 152 rdch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED); 153 } 154 if (wrch) wrch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED); 155 d->aplay[chan] = NULL; 156 d->arec[chan] = NULL; 157 return 0; 158 } 159 160 int 161 dsp_read(snddev_info *d, int chan, struct uio *buf, int flag) 162 { 163 pcm_channel *rdch, *wrch; 164 165 if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_RD; 166 if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan); 167 getchns(d, chan, &rdch, &wrch); 168 if (!rdch || !(rdch->flags & CHN_F_BUSY)) 169 panic("dsp_read: non%s channel", rdch? "busy" : "existant"); 170 if (rdch->flags & CHN_F_MAPPED) return EINVAL; 171 if (!(rdch->flags & CHN_F_RUNNING)) rdch->flags |= CHN_F_RUNNING; 172 return chn_read(rdch, buf); 173 } 174 175 int 176 dsp_write(snddev_info *d, int chan, struct uio *buf, int flag) 177 { 178 pcm_channel *rdch, *wrch; 179 180 if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_WR; 181 if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan); 182 getchns(d, chan, &rdch, &wrch); 183 if (!wrch || !(wrch->flags & CHN_F_BUSY)) 184 panic("dsp_write: non%s channel", wrch? "busy" : "existant"); 185 if (wrch->flags & CHN_F_MAPPED) return EINVAL; 186 if (!(wrch->flags & CHN_F_RUNNING)) wrch->flags |= CHN_F_RUNNING; 187 return chn_write(wrch, buf); 188 } 189 190 int 191 dsp_ioctl(snddev_info *d, int chan, u_long cmd, caddr_t arg) 192 { 193 int ret = 0, *arg_i = (int *)arg; 194 u_long s; 195 pcm_channel *wrch = NULL, *rdch = NULL; 196 197 getchns(d, chan, &rdch, &wrch); 198 199 /* 200 * all routines are called with int. blocked. Make sure that 201 * ints are re-enabled when calling slow or blocking functions! 202 */ 203 s = spltty(); 204 switch(cmd) { 205 206 /* 207 * we start with the new ioctl interface. 208 */ 209 case AIONWRITE: /* how many bytes can write ? */ 210 if (wrch && wrch->buffer.dl) chn_dmaupdate(wrch); 211 *arg_i = wrch? wrch->buffer.fl : 0; 212 break; 213 214 case AIOSSIZE: /* set the current blocksize */ 215 { 216 struct snd_size *p = (struct snd_size *)arg; 217 splx(s); 218 if (wrch) chn_setblocksize(wrch, p->play_size); 219 if (rdch) chn_setblocksize(rdch, p->rec_size); 220 } 221 /* FALLTHROUGH */ 222 case AIOGSIZE: /* get the current blocksize */ 223 { 224 struct snd_size *p = (struct snd_size *)arg; 225 if (wrch) p->play_size = wrch->blocksize; 226 if (rdch) p->rec_size = rdch->blocksize; 227 } 228 break; 229 230 case AIOSFMT: 231 { 232 snd_chan_param *p = (snd_chan_param *)arg; 233 splx(s); 234 if (wrch) { 235 chn_setformat(wrch, p->play_format); 236 chn_setspeed(wrch, p->play_rate); 237 } 238 if (rdch) { 239 chn_setformat(rdch, p->rec_format); 240 chn_setspeed(rdch, p->rec_rate); 241 } 242 } 243 /* FALLTHROUGH */ 244 245 case AIOGFMT: 246 { 247 snd_chan_param *p = (snd_chan_param *)arg; 248 p->play_rate = wrch? wrch->speed : 0; 249 p->rec_rate = rdch? rdch->speed : 0; 250 p->play_format = wrch? wrch->format : 0; 251 p->rec_format = rdch? rdch->format : 0; 252 } 253 break; 254 255 case AIOGCAP: /* get capabilities */ 256 { 257 snd_capabilities *p = (snd_capabilities *)arg; 258 pcmchan_caps *pcaps = NULL, *rcaps = NULL; 259 if (rdch) rcaps = chn_getcaps(rdch); 260 if (wrch) pcaps = chn_getcaps(wrch); 261 p->rate_min = max(rcaps? rcaps->minspeed : 0, 262 pcaps? pcaps->minspeed : 0); 263 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000, 264 pcaps? pcaps->maxspeed : 1000000); 265 p->bufsize = min(rdch? rdch->buffer.bufsize : 1000000, 266 wrch? wrch->buffer.bufsize : 1000000); 267 /* XXX bad on sb16 */ 268 p->formats = (rcaps? rcaps->formats : 0xffffffff) & 269 (pcaps? pcaps->formats : 0xffffffff); 270 p->mixers = 1; /* default: one mixer */ 271 p->inputs = d->mixer.devs; 272 p->left = p->right = 100; 273 } 274 break; 275 276 case AIOSTOP: 277 if (*arg_i == AIOSYNC_PLAY && wrch) *arg_i = chn_abort(wrch); 278 else if (*arg_i == AIOSYNC_CAPTURE && rdch) *arg_i = chn_abort(rdch); 279 else { 280 splx(s); 281 printf("AIOSTOP: bad channel 0x%x\n", *arg_i); 282 *arg_i = 0; 283 } 284 break; 285 286 case AIOSYNC: 287 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n", 288 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos); 289 break; 290 /* 291 * here follow the standard ioctls (filio.h etc.) 292 */ 293 case FIONREAD: /* get # bytes to read */ 294 if (rdch && rdch->buffer.dl) chn_dmaupdate(rdch); 295 *arg_i = rdch? rdch->buffer.rl : 0; 296 break; 297 298 case FIOASYNC: /*set/clear async i/o */ 299 DEB( printf("FIOASYNC\n") ; ) 300 break; 301 302 case SNDCTL_DSP_NONBLOCK: 303 case FIONBIO: /* set/clear non-blocking i/o */ 304 if (rdch) rdch->flags &= ~CHN_F_NBIO; 305 if (wrch) wrch->flags &= ~CHN_F_NBIO; 306 if (*arg_i) { 307 if (rdch) rdch->flags |= CHN_F_NBIO; 308 if (wrch) wrch->flags |= CHN_F_NBIO; 309 } 310 break; 311 312 /* 313 * Finally, here is the linux-compatible ioctl interface 314 */ 315 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int) 316 case THE_REAL_SNDCTL_DSP_GETBLKSIZE: 317 case SNDCTL_DSP_GETBLKSIZE: 318 *arg_i = wrch? wrch->blocksize : 0; /* XXX rdch? */ 319 break ; 320 321 case SNDCTL_DSP_SETBLKSIZE: 322 splx(s); 323 if (wrch) chn_setblocksize(wrch, *arg_i); 324 if (rdch) chn_setblocksize(rdch, *arg_i); 325 break; 326 327 case SNDCTL_DSP_RESET: 328 DEB(printf("dsp reset\n")); 329 if (wrch) chn_abort(wrch); 330 if (rdch) chn_abort(rdch); 331 break; 332 333 case SNDCTL_DSP_SYNC: 334 printf("dsp sync\n"); 335 splx(s); 336 if (wrch) chn_sync(wrch, wrch->buffer.bufsize - 4); 337 break; 338 339 case SNDCTL_DSP_SPEED: 340 splx(s); 341 if (wrch) chn_setspeed(wrch, *arg_i); 342 if (rdch) chn_setspeed(rdch, *arg_i); 343 /* fallthru */ 344 345 case SOUND_PCM_READ_RATE: 346 *arg_i = wrch? wrch->speed : rdch->speed; 347 break; 348 349 case SNDCTL_DSP_STEREO: 350 splx(s); 351 if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | 352 ((*arg_i)? AFMT_STEREO : 0)); 353 if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | 354 ((*arg_i)? AFMT_STEREO : 0)); 355 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 1 : 0; 356 break; 357 358 case SOUND_PCM_WRITE_CHANNELS: 359 splx(s); 360 if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | 361 ((*arg_i == 2)? AFMT_STEREO : 0)); 362 if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | 363 ((*arg_i == 2)? AFMT_STEREO : 0)); 364 /* fallthru */ 365 366 case SOUND_PCM_READ_CHANNELS: 367 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1; 368 break; 369 370 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */ 371 *arg_i = wrch? chn_getcaps(wrch)->formats : chn_getcaps(rdch)->formats; 372 break ; 373 374 case SNDCTL_DSP_SETFMT: /* sets _one_ format */ 375 splx(s); 376 if (wrch) chn_setformat(wrch, *arg_i); 377 if (rdch) chn_setformat(rdch, *arg_i); 378 *arg_i = wrch? wrch->format : rdch->format; 379 break; 380 381 case SNDCTL_DSP_SUBDIVIDE: 382 /* XXX watch out, this is RW! */ 383 DEB(printf("SNDCTL_DSP_SUBDIVIDE unimplemented\n");) 384 break; 385 386 case SNDCTL_DSP_SETFRAGMENT: 387 /* XXX watch out, this is RW! */ 388 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg)); 389 { 390 int bytes = 1 << min(*arg_i & 0xffff, 16); 391 int count = (*arg_i >> 16) & 0xffff; 392 pcm_channel *c = wrch? wrch : rdch; 393 splx(s); 394 if (rdch) chn_setblocksize(rdch, bytes); 395 if (wrch) chn_setblocksize(wrch, bytes); 396 397 /* eg: 4dwave can only interrupt at buffer midpoint, so 398 * it will force blocksize == bufsize/2 399 */ 400 count = c->buffer.bufsize / c->blocksize; 401 bytes = ffs(c->blocksize) - 1; 402 *arg_i = (count << 16) | bytes; 403 } 404 break; 405 406 case SNDCTL_DSP_GETISPACE: 407 /* return space available in the input queue */ 408 { 409 audio_buf_info *a = (audio_buf_info *)arg; 410 if (rdch) { 411 snd_dbuf *b = &rdch->buffer; 412 if (b->dl) chn_dmaupdate(rdch); 413 a->bytes = b->fl; 414 a->fragments = 1; 415 a->fragstotal = b->bufsize / rdch->blocksize; 416 a->fragsize = rdch->blocksize; 417 } 418 } 419 break; 420 421 case SNDCTL_DSP_GETOSPACE: 422 /* return space available in the output queue */ 423 { 424 audio_buf_info *a = (audio_buf_info *)arg; 425 if (wrch) { 426 snd_dbuf *b = &wrch->buffer; 427 if (b->dl) chn_dmaupdate(wrch); 428 a->bytes = b->fl; 429 a->fragments = 1; 430 a->fragstotal = b->bufsize / wrch->blocksize; 431 a->fragsize = wrch->blocksize; 432 } 433 } 434 break; 435 436 case SNDCTL_DSP_GETIPTR: 437 { 438 count_info *a = (count_info *)arg; 439 if (rdch) { 440 snd_dbuf *b = &rdch->buffer; 441 if (b->dl) chn_dmaupdate(rdch); 442 a->bytes = b->total; 443 a->blocks = (b->total - b->prev_total) / rdch->blocksize; 444 a->ptr = b->fp; 445 b->prev_total += a->blocks * rdch->blocksize; 446 } else ret = EINVAL; 447 } 448 break; 449 450 case SNDCTL_DSP_GETOPTR: 451 { 452 count_info *a = (count_info *)arg; 453 if (wrch) { 454 snd_dbuf *b = &wrch->buffer; 455 if (b->dl) chn_dmaupdate(wrch); 456 a->bytes = b->total; 457 a->blocks = (b->total - b->prev_total) / wrch->blocksize; 458 a->ptr = b->rp; 459 b->prev_total += a->blocks * wrch->blocksize; 460 } else ret = EINVAL; 461 } 462 break; 463 464 case SNDCTL_DSP_GETCAPS: 465 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER; 466 if (rdch && wrch && !(d->flags & SD_F_SIMPLEX)) 467 *arg_i |= DSP_CAP_DUPLEX; 468 break; 469 470 case SOUND_PCM_READ_BITS: 471 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_16BIT)? 16 : 8; 472 break; 473 474 case SNDCTL_DSP_SETTRIGGER: 475 if (rdch) { 476 rdch->flags &= ~CHN_F_TRIGGERED; 477 if (*arg_i & PCM_ENABLE_INPUT) 478 rdch->flags |= CHN_F_TRIGGERED; 479 chn_intr(rdch); 480 } 481 if (wrch) { 482 wrch->flags &= ~CHN_F_TRIGGERED; 483 if (*arg_i & PCM_ENABLE_OUTPUT) 484 wrch->flags |= CHN_F_TRIGGERED; 485 chn_intr(wrch); 486 } 487 break; 488 489 case SNDCTL_DSP_GETTRIGGER: 490 *arg_i = 0; 491 if (wrch && wrch->flags & CHN_F_TRIGGERED) 492 *arg_i |= PCM_ENABLE_OUTPUT; 493 if (rdch && rdch->flags & CHN_F_TRIGGERED) 494 *arg_i |= PCM_ENABLE_INPUT; 495 break; 496 497 case SNDCTL_DSP_MAPINBUF: 498 case SNDCTL_DSP_MAPOUTBUF: 499 case SNDCTL_DSP_SETSYNCRO: 500 /* undocumented */ 501 502 case SNDCTL_DSP_POST: 503 case SOUND_PCM_WRITE_FILTER: 504 case SOUND_PCM_READ_FILTER: 505 /* dunno what these do, don't sound important */ 506 default: 507 DEB(printf("default ioctl snd%d fn 0x%08x fail\n", unit, cmd)); 508 ret = EINVAL; 509 break; 510 } 511 splx(s); 512 return ret; 513 } 514 515 int 516 dsp_poll(snddev_info *d, int chan, int events, struct proc *p) 517 { 518 int ret = 0, e; 519 pcm_channel *wrch = NULL, *rdch = NULL; 520 521 getchns(d, chan, &rdch, &wrch); 522 e = events & (POLLOUT | POLLWRNORM); 523 if (wrch && e) ret |= chn_poll(wrch, e, p); 524 e = events & (POLLIN | POLLRDNORM); 525 if (rdch && e) ret |= chn_poll(rdch, e, p); 526 return ret; 527 } 528 529 int 530 dsp_mmap(snddev_info *d, int chan, vm_offset_t offset, int nprot) 531 { 532 pcm_channel *wrch = NULL, *rdch = NULL, *c = NULL; 533 534 getchns(d, chan, &rdch, &wrch); 535 /* XXX this is broken by line 204 of vm/device_pager.c, so force write buffer */ 536 if (1 || (wrch && (nprot & PROT_WRITE))) c = wrch; 537 else if (rdch && (nprot & PROT_READ)) c = rdch; 538 if (c) { 539 c->flags |= CHN_F_MAPPED; 540 return atop(vtophys(c->buffer.buf + offset)); 541 } 542 return -1; 543 } 544 545