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