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